Improved support for stored procedures invocation. Both typed and not typed. Also improved support for output arguments and added examples to docs.

This commit is contained in:
grzegorz.russek
2024-04-11 15:15:14 +02:00
parent 437403f966
commit be6c172ec8
6 changed files with 541 additions and 61 deletions

View File

@@ -44,6 +44,7 @@ using System.Collections.Concurrent;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Collections; using System.Collections;
using System.ComponentModel;
using System.Data.Common; using System.Data.Common;
using System.Data; using System.Data;
using System.Dynamic; using System.Dynamic;
@@ -1673,6 +1674,90 @@ namespace DynamORM
public DbProviderFactory Provider { get { return _provider; } } public DbProviderFactory Provider { get { return _provider; } }
/// <summary>Gets the procedures invoker.</summary> /// <summary>Gets the procedures invoker.</summary>
/// <example>
/// <code>
/// using (var db = GetORM())
/// {
/// db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_Scalar AS SELECT 42;");
/// var res0 = db.Procedures.sp_Exp_Scalar();
/// var res1 = db.Procedures.sp_Exp_Scalar<int>();
///
/// db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_ReturnInt AS RETURN 42;");
/// var res2 = db.Procedures.sp_Exp_ReturnInt();
///
/// db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_SomeData AS
/// SELECT 1 Id, 'Some Name 1' [Name], 'Some Desc 1' [Desc], GETDATE() [Date]
/// UNION ALL SELECT 2 Id, 'Some Name 2', 'Some Desc 2', GETDATE() [Date];");
/// var res3 = db.Procedures.sp_Exp_SomeData<sp_Exp_SomeData_Result>();
/// var res4 = db.Procedures.sp_Exp_SomeData<List<sp_Exp_SomeData_Result>>();
///
/// db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_SomeInputAndOutput
/// @Name nvarchar(50),
/// @Result nvarchar(256) OUTPUT
/// AS
/// SELECT @Result = 'Hi, ' + @Name + ' your lucky number is 42!';");
/// var res5 = db.Procedures.sp_Exp_SomeInputAndOutput<string, sp_Exp_SomeInputAndOutput_Result>(Name: "G4g4r1n", out_Result: new DynamicColumn
/// {
/// Schema = new DynamicSchemaColumn
/// {
/// Size = 256,
/// },
/// }, ret_Return: 0);
/// var res6 = db.Procedures.sp_Exp_SomeInputAndOutput<string, sp_Exp_SomeInputAndOutput_Result>(Name: "G4g4r1n", out_Result: new DynamicSchemaColumn
/// {
/// Size = 256,
/// }, ret_Return: 0);
///
/// db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_SomeInputAndOutputWithDataAndReturn
/// @Name nvarchar(50),
/// @Result nvarchar(256) OUTPUT
/// AS
/// SELECT @Result = 'Hi, ' + @Name + ' your lucky number is 42!'
///
/// SELECT 1 Id, 'Some Name 1' [Name], 'Some Desc 1' [Desc], GETDATE() [Date]
/// UNION ALL SELECT 2 Id, 'Some Name 2', 'Some Desc 2', GETDATE() [Date]
///
/// RETURN 42;");
/// var res7 = db.Procedures.sp_Exp_SomeInputAndOutputWithDataAndReturn<List<sp_Exp_SomeInputAndOutputWithDataAndReturn_Result.Data>, sp_Exp_SomeInputAndOutputWithDataAndReturn_Result>(Name: "G4g4r1n", out_Result: new DynamicColumn
/// {
/// Schema = new DynamicSchemaColumn
/// {
/// Size = 256,
/// },
/// }, ret_Return: 0);
/// var res8 = db.Procedures.sp_Exp_SomeInputAndOutputWithDataAndReturn<List<sp_Exp_SomeInputAndOutputWithDataAndReturn_Result.Data>, sp_Exp_SomeInputAndOutputWithDataAndReturn_Result>(Name: "G4g4r1n", out_Result: new DynamicSchemaColumn
/// {
/// Size = 256,
/// }, ret_Return: 0);
/// }
///
///private class sp_Exp_SomeData_Result
///{
/// public virtual int Id { get; set; }
/// public virtual string Name { get; set; }
/// public virtual string Desc { get; set; }
/// public virtual DateTime Date { get; set; }
///}
///private class sp_Exp_SomeInputAndOutput_Result
///{
/// public virtual string Result { get; set; }
/// public virtual string Return { get; set; }
///}
///private class sp_Exp_SomeInputAndOutputWithDataAndReturn_Result
///{
/// public class Data
/// {
/// public virtual int Id { get; set; }
/// public virtual string Name { get; set; }
/// public virtual string Desc { get; set; }
/// public virtual DateTime Date { get; set; }
/// }
/// public virtual List<Data> sp_Exp_SomeInputAndOutputWithDataAndReturn { get; set; }
/// public virtual string Result { get; set; }
/// public virtual string Return { get; set; }
///}
/// </code>
/// </example>
public dynamic Procedures public dynamic Procedures
{ {
get get
@@ -1680,7 +1765,7 @@ namespace DynamORM
if (_proc == null) if (_proc == null)
{ {
if ((Options & DynamicDatabaseOptions.SupportStoredProcedures) != DynamicDatabaseOptions.SupportStoredProcedures) if ((Options & DynamicDatabaseOptions.SupportStoredProcedures) != DynamicDatabaseOptions.SupportStoredProcedures)
throw new InvalidOperationException("Database connection desn't support stored procedures."); throw new InvalidOperationException("Database connection doesn't support stored procedures.");
_proc = new DynamicProcedureInvoker(this); _proc = new DynamicProcedureInvoker(this);
} }
@@ -5494,6 +5579,8 @@ namespace DynamORM
#region Prepare arguments #region Prepare arguments
int alen = args.Length; int alen = args.Length;
bool retIsAdded = false;
if (alen > 0) if (alen > 0)
{ {
for (int i = 0; i < alen; i++) for (int i = 0; i < alen; i++)
@@ -5508,14 +5595,38 @@ namespace DynamORM
{ {
var dcv = (DynamicColumn)arg; var dcv = (DynamicColumn)arg;
string paramName = dcv.Alias ?? dcv.ColumnName ?? string paramName = i.ToString();
bool isOut = false;
bool isRet = false;
bool isBoth = false;
if (info.ArgumentNames.Count > i)
{
isOut = info.ArgumentNames[i].StartsWith("out_");
isRet = info.ArgumentNames[i].StartsWith("ret_");
isBoth = info.ArgumentNames[i].StartsWith("both_");
paramName = isOut || isRet ?
info.ArgumentNames[i].Substring(4) :
isBoth ? info.ArgumentNames[i].Substring(5) :
info.ArgumentNames[i];
}
paramName = dcv.Alias ?? dcv.ColumnName ??
(dcv.Schema.HasValue ? dcv.Schema.Value.Name : null) ?? (dcv.Schema.HasValue ? dcv.Schema.Value.Name : null) ??
(info.ArgumentNames.Count > i ? info.ArgumentNames[i] : i.ToString()); paramName;
bool isOut = dcv.ParameterDirection == ParameterDirection.Output || if (!isOut && !isRet && !isBoth)
dcv.ParameterDirection == ParameterDirection.ReturnValue; {
isOut = dcv.ParameterDirection == ParameterDirection.Output;
isRet = dcv.ParameterDirection == ParameterDirection.ReturnValue;
isBoth = dcv.ParameterDirection == ParameterDirection.InputOutput;
}
if (isOut || dcv.ParameterDirection == ParameterDirection.InputOutput) if (isRet)
retIsAdded = true;
if (isOut || isRet || isBoth)
{ {
if (retParams == null) if (retParams == null)
retParams = new Dictionary<string, int>(); retParams = new Dictionary<string, int>();
@@ -5526,14 +5637,66 @@ namespace DynamORM
{ {
var ds = dcv.Schema.Value; var ds = dcv.Schema.Value;
cmd.AddParameter( cmd.AddParameter(
_db.GetParameterName(paramName), dcv.ParameterDirection, _db.GetParameterName(paramName),
isOut ? ParameterDirection.Output :
isRet ? ParameterDirection.ReturnValue :
isBoth ? ParameterDirection.InputOutput :
ParameterDirection.Input,
ds.Type, ds.Size, ds.Precision, ds.Scale, ds.Type, ds.Size, ds.Precision, ds.Scale,
isOut ? DBNull.Value : dcv.Value); (isOut || isRet) ? DBNull.Value : dcv.Value);
} }
else else
cmd.AddParameter( cmd.AddParameter(
_db.GetParameterName(paramName), dcv.ParameterDirection, _db.GetParameterName(paramName),
arg == null ? DbType.String : arg.GetType().ToDbType(), 0, isOut ? DBNull.Value : dcv.Value); isOut ? ParameterDirection.Output :
isRet ? ParameterDirection.ReturnValue :
isBoth ? ParameterDirection.InputOutput :
ParameterDirection.Input,
arg == null ? DbType.String : arg.GetType().ToDbType(),
isRet ? 4 : 0,
(isOut || isRet) ? DBNull.Value : dcv.Value);
}
else if (arg is DynamicSchemaColumn)
{
var dsc = (DynamicSchemaColumn)arg;
string paramName = i.ToString();
bool isOut = false;
bool isRet = false;
bool isBoth = false;
if (info.ArgumentNames.Count > i)
{
isOut = info.ArgumentNames[i].StartsWith("out_");
isRet = info.ArgumentNames[i].StartsWith("ret_");
isBoth = info.ArgumentNames[i].StartsWith("both_");
paramName = isOut || isRet ?
info.ArgumentNames[i].Substring(4) :
isBoth ? info.ArgumentNames[i].Substring(5) :
info.ArgumentNames[i];
}
paramName = dsc.Name ?? paramName;
if (isRet)
retIsAdded = true;
if (isOut || isRet || isBoth)
{
if (retParams == null)
retParams = new Dictionary<string, int>();
retParams.Add(paramName, cmd.Parameters.Count);
}
cmd.AddParameter(
_db.GetParameterName(paramName),
isOut ? ParameterDirection.Output :
isRet ? ParameterDirection.ReturnValue :
isBoth ? ParameterDirection.InputOutput :
ParameterDirection.Input,
dsc.Type, dsc.Size, dsc.Precision, dsc.Scale,
DBNull.Value);
} }
else else
{ {
@@ -5543,6 +5706,9 @@ namespace DynamORM
bool isRet = info.ArgumentNames[i].StartsWith("ret_"); bool isRet = info.ArgumentNames[i].StartsWith("ret_");
bool isBoth = info.ArgumentNames[i].StartsWith("both_"); bool isBoth = info.ArgumentNames[i].StartsWith("both_");
if (isRet)
retIsAdded = true;
string paramName = isOut || isRet ? string paramName = isOut || isRet ?
info.ArgumentNames[i].Substring(4) : info.ArgumentNames[i].Substring(4) :
isBoth ? info.ArgumentNames[i].Substring(5) : isBoth ? info.ArgumentNames[i].Substring(5) :
@@ -5559,8 +5725,11 @@ namespace DynamORM
_db.GetParameterName(paramName), _db.GetParameterName(paramName),
isOut ? ParameterDirection.Output : isOut ? ParameterDirection.Output :
isRet ? ParameterDirection.ReturnValue : isRet ? ParameterDirection.ReturnValue :
isBoth ? ParameterDirection.InputOutput : ParameterDirection.Input, isBoth ? ParameterDirection.InputOutput :
arg == null ? DbType.String : arg.GetType().ToDbType(), 0, isOut ? DBNull.Value : arg); ParameterDirection.Input,
arg == null ? isRet ? DbType.Int32 : DbType.String : arg.GetType().ToDbType(),
isRet ? 4 : 0,
(isOut || isRet) ? DBNull.Value : arg);
} }
else else
cmd.AddParameter(_db, arg); cmd.AddParameter(_db, arg);
@@ -5611,7 +5780,7 @@ namespace DynamORM
cache = rdr.CachedReader(); cache = rdr.CachedReader();
while (cache.Read()) while (cache.Read())
listInstance.Add(cache[0] == DBNull.Value ? defVal : argType.CastObject(cache[0])); listInstance.Add(cache[0] != null && cache[0] != DBNull.Value ? argType.CastObject(cache[0]) : defVal);
mainResult = listInstance; mainResult = listInstance;
} }
@@ -5628,7 +5797,7 @@ namespace DynamORM
while (cache.Read()) while (cache.Read())
{ {
if (cache[0] == DBNull.Value && Guid.TryParse(cache[0].ToString(), out Guid g)) if (cache[0] != null && cache[0] != DBNull.Value && Guid.TryParse(cache[0].ToString(), out Guid g))
listInstance.Add(g); listInstance.Add(g);
} }
@@ -5638,32 +5807,41 @@ namespace DynamORM
{ {
DynamicTypeMap mapper = DynamicMapperCache.GetMapper(argType); DynamicTypeMap mapper = DynamicMapperCache.GetMapper(argType);
if (mapper == null) if (mapper == null)
throw new InvalidCastException(string.Format("Don't konw what to do with this type: '{0}'.", argType.ToString())); throw new InvalidCastException(string.Format("Don't know what to do with this type: '{0}'.", argType.ToString()));
IDataReader cache = null; IDataReader cache = null;
using (IDataReader rdr = cmd.ExecuteReader()) using (IDataReader rdr = cmd.ExecuteReader())
cache = rdr.CachedReader(); cache = rdr.CachedReader();
mainResult = cache.EnumerateReader().MapEnumerable(argType).ToList(); var lt = typeof(List<>);
var ltc = lt.MakeGenericType(argType);
var instance = Activator.CreateInstance(ltc) as IList;
foreach (var item in cache.EnumerateReader())
instance.Add(DynamicExtensions.Map(item, argType));
mainResult = instance;
//mainResult = cache.EnumerateReader().MapEnumerable(argType).ToList();
} }
} }
else if (types[0].IsValueType || types[0] == typeof(string)) else if (types[0].IsValueType || types[0] == typeof(string))
{ {
mainResult = cmd.ExecuteScalar(); mainResult = cmd.ExecuteScalar();
if (mainResult != DBNull.Value) if (mainResult != null && mainResult != DBNull.Value)
mainResult = types[0].CastObject(mainResult); mainResult = types[0].CastObject(mainResult);
} }
else if (types[0] == typeof(Guid)) else if (types[0] == typeof(Guid))
{ {
mainResult = cmd.ExecuteScalar(); mainResult = cmd.ExecuteScalar();
if (mainResult != DBNull.Value && Guid.TryParse(mainResult.ToString(), out Guid g)) if (mainResult != null && mainResult != DBNull.Value && Guid.TryParse(mainResult.ToString(), out Guid g))
mainResult = g; mainResult = g;
} }
else else
{ {
DynamicTypeMap mapper = DynamicMapperCache.GetMapper(types[0]); DynamicTypeMap mapper = DynamicMapperCache.GetMapper(types[0]);
if (mapper == null) if (mapper == null)
throw new InvalidCastException(string.Format("Don't konw what to do with this type: '{0}'.", types[0].ToString())); throw new InvalidCastException(string.Format("Don't know what to do with this type: '{0}'.", types[0].ToString()));
using (IDataReader rdr = cmd.ExecuteReader()) using (IDataReader rdr = cmd.ExecuteReader())
if (rdr.Read()) if (rdr.Read())
@@ -5673,8 +5851,33 @@ namespace DynamORM
} }
} }
else else
{
var returnName = _db.GetParameterName("___result___");
if (!retIsAdded)
cmd.AddParameter(returnName, ParameterDirection.ReturnValue, DbType.Int32, 4, 0, 0, DBNull.Value);
mainResult = cmd.ExecuteNonQuery(); mainResult = cmd.ExecuteNonQuery();
IDbDataParameter returnParam = null;
if (!retIsAdded)
returnParam = cmd.Parameters[returnName] as IDbDataParameter;
else
{
foreach (var e in cmd.Parameters)
{
var p = e as IDbDataParameter;
if (p != null && p.Direction == ParameterDirection.ReturnValue)
{
returnParam = p;
break;
}
}
}
if (returnParam != null && returnParam.Value != null && returnParam.Value != DBNull.Value)
mainResult = returnParam.Value;
}
#endregion Get main result #endregion Get main result
#region Handle out params #region Handle out params

View File

@@ -118,10 +118,10 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="MSTest.TestAdapter"> <PackageReference Include="MSTest.TestAdapter">
<Version>3.1.1</Version> <Version>3.3.1</Version>
</PackageReference> </PackageReference>
<PackageReference Include="MSTest.TestFramework"> <PackageReference Include="MSTest.TestFramework">
<Version>3.1.1</Version> <Version>3.3.1</Version>
</PackageReference> </PackageReference>
<PackageReference Include="System.Data.SQLite"> <PackageReference Include="System.Data.SQLite">
<Version>1.0.118</Version> <Version>1.0.118</Version>

View File

@@ -122,6 +122,90 @@ namespace DynamORM
public DbProviderFactory Provider { get { return _provider; } } public DbProviderFactory Provider { get { return _provider; } }
/// <summary>Gets the procedures invoker.</summary> /// <summary>Gets the procedures invoker.</summary>
/// <example>
/// <code>
/// using (var db = GetORM())
/// {
/// db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_Scalar AS SELECT 42;");
/// var res0 = db.Procedures.sp_Exp_Scalar();
/// var res1 = db.Procedures.sp_Exp_Scalar<int>();
///
/// db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_ReturnInt AS RETURN 42;");
/// var res2 = db.Procedures.sp_Exp_ReturnInt();
///
/// db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_SomeData AS
/// SELECT 1 Id, 'Some Name 1' [Name], 'Some Desc 1' [Desc], GETDATE() [Date]
/// UNION ALL SELECT 2 Id, 'Some Name 2', 'Some Desc 2', GETDATE() [Date];");
/// var res3 = db.Procedures.sp_Exp_SomeData<sp_Exp_SomeData_Result>();
/// var res4 = db.Procedures.sp_Exp_SomeData<List<sp_Exp_SomeData_Result>>();
///
/// db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_SomeInputAndOutput
/// @Name nvarchar(50),
/// @Result nvarchar(256) OUTPUT
/// AS
/// SELECT @Result = 'Hi, ' + @Name + ' your lucky number is 42!';");
/// var res5 = db.Procedures.sp_Exp_SomeInputAndOutput<string, sp_Exp_SomeInputAndOutput_Result>(Name: "G4g4r1n", out_Result: new DynamicColumn
/// {
/// Schema = new DynamicSchemaColumn
/// {
/// Size = 256,
/// },
/// }, ret_Return: 0);
/// var res6 = db.Procedures.sp_Exp_SomeInputAndOutput<string, sp_Exp_SomeInputAndOutput_Result>(Name: "G4g4r1n", out_Result: new DynamicSchemaColumn
/// {
/// Size = 256,
/// }, ret_Return: 0);
///
/// db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_SomeInputAndOutputWithDataAndReturn
/// @Name nvarchar(50),
/// @Result nvarchar(256) OUTPUT
/// AS
/// SELECT @Result = 'Hi, ' + @Name + ' your lucky number is 42!'
///
/// SELECT 1 Id, 'Some Name 1' [Name], 'Some Desc 1' [Desc], GETDATE() [Date]
/// UNION ALL SELECT 2 Id, 'Some Name 2', 'Some Desc 2', GETDATE() [Date]
///
/// RETURN 42;");
/// var res7 = db.Procedures.sp_Exp_SomeInputAndOutputWithDataAndReturn<List<sp_Exp_SomeInputAndOutputWithDataAndReturn_Result.Data>, sp_Exp_SomeInputAndOutputWithDataAndReturn_Result>(Name: "G4g4r1n", out_Result: new DynamicColumn
/// {
/// Schema = new DynamicSchemaColumn
/// {
/// Size = 256,
/// },
/// }, ret_Return: 0);
/// var res8 = db.Procedures.sp_Exp_SomeInputAndOutputWithDataAndReturn<List<sp_Exp_SomeInputAndOutputWithDataAndReturn_Result.Data>, sp_Exp_SomeInputAndOutputWithDataAndReturn_Result>(Name: "G4g4r1n", out_Result: new DynamicSchemaColumn
/// {
/// Size = 256,
/// }, ret_Return: 0);
/// }
///
///private class sp_Exp_SomeData_Result
///{
/// public virtual int Id { get; set; }
/// public virtual string Name { get; set; }
/// public virtual string Desc { get; set; }
/// public virtual DateTime Date { get; set; }
///}
///private class sp_Exp_SomeInputAndOutput_Result
///{
/// public virtual string Result { get; set; }
/// public virtual string Return { get; set; }
///}
///private class sp_Exp_SomeInputAndOutputWithDataAndReturn_Result
///{
/// public class Data
/// {
/// public virtual int Id { get; set; }
/// public virtual string Name { get; set; }
/// public virtual string Desc { get; set; }
/// public virtual DateTime Date { get; set; }
/// }
/// public virtual List<Data> sp_Exp_SomeInputAndOutputWithDataAndReturn { get; set; }
/// public virtual string Result { get; set; }
/// public virtual string Return { get; set; }
///}
/// </code>
/// </example>
public dynamic Procedures public dynamic Procedures
{ {
get get
@@ -129,7 +213,7 @@ namespace DynamORM
if (_proc == null) if (_proc == null)
{ {
if ((Options & DynamicDatabaseOptions.SupportStoredProcedures) != DynamicDatabaseOptions.SupportStoredProcedures) if ((Options & DynamicDatabaseOptions.SupportStoredProcedures) != DynamicDatabaseOptions.SupportStoredProcedures)
throw new InvalidOperationException("Database connection desn't support stored procedures."); throw new InvalidOperationException("Database connection doesn't support stored procedures.");
_proc = new DynamicProcedureInvoker(this); _proc = new DynamicProcedureInvoker(this);
} }

View File

@@ -29,6 +29,7 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel;
using System.Data; using System.Data;
using System.Dynamic; using System.Dynamic;
using System.Linq; using System.Linq;
@@ -105,6 +106,8 @@ namespace DynamORM
#region Prepare arguments #region Prepare arguments
int alen = args.Length; int alen = args.Length;
bool retIsAdded = false;
if (alen > 0) if (alen > 0)
{ {
for (int i = 0; i < alen; i++) for (int i = 0; i < alen; i++)
@@ -119,30 +122,108 @@ namespace DynamORM
{ {
var dcv = (DynamicColumn)arg; var dcv = (DynamicColumn)arg;
string paramName = dcv.Alias ?? dcv.ColumnName ?? string paramName = i.ToString();
bool isOut = false;
bool isRet = false;
bool isBoth = false;
if (info.ArgumentNames.Count > i)
{
isOut = info.ArgumentNames[i].StartsWith("out_");
isRet = info.ArgumentNames[i].StartsWith("ret_");
isBoth = info.ArgumentNames[i].StartsWith("both_");
paramName = isOut || isRet ?
info.ArgumentNames[i].Substring(4) :
isBoth ? info.ArgumentNames[i].Substring(5) :
info.ArgumentNames[i];
}
paramName = dcv.Alias ?? dcv.ColumnName ??
(dcv.Schema.HasValue ? dcv.Schema.Value.Name : null) ?? (dcv.Schema.HasValue ? dcv.Schema.Value.Name : null) ??
(info.ArgumentNames.Count > i ? info.ArgumentNames[i] : i.ToString()); paramName;
bool isOut = dcv.ParameterDirection == ParameterDirection.Output || if (!isOut && !isRet && !isBoth)
dcv.ParameterDirection == ParameterDirection.ReturnValue; {
isOut = dcv.ParameterDirection == ParameterDirection.Output;
isRet = dcv.ParameterDirection == ParameterDirection.ReturnValue;
isBoth = dcv.ParameterDirection == ParameterDirection.InputOutput;
}
if (isOut || dcv.ParameterDirection == ParameterDirection.InputOutput) if (isRet)
retIsAdded = true;
if (isOut || isRet || isBoth)
{ {
if (retParams == null) if (retParams == null)
retParams = new Dictionary<string, int>(); retParams = new Dictionary<string, int>();
retParams.Add(paramName, cmd.Parameters.Count); retParams.Add(paramName, cmd.Parameters.Count);
} }
if (dcv.Schema != null) { if (dcv.Schema != null)
{
var ds = dcv.Schema.Value; var ds = dcv.Schema.Value;
cmd.AddParameter( cmd.AddParameter(
_db.GetParameterName(paramName), dcv.ParameterDirection, _db.GetParameterName(paramName),
isOut ? ParameterDirection.Output :
isRet ? ParameterDirection.ReturnValue :
isBoth ? ParameterDirection.InputOutput :
ParameterDirection.Input,
ds.Type, ds.Size, ds.Precision, ds.Scale, ds.Type, ds.Size, ds.Precision, ds.Scale,
isOut ? DBNull.Value : dcv.Value); (isOut || isRet) ? DBNull.Value : dcv.Value);
} else }
else
cmd.AddParameter( cmd.AddParameter(
_db.GetParameterName(paramName), dcv.ParameterDirection, _db.GetParameterName(paramName),
arg == null ? DbType.String : arg.GetType().ToDbType(), 0, isOut ? DBNull.Value : dcv.Value); isOut ? ParameterDirection.Output :
isRet ? ParameterDirection.ReturnValue :
isBoth ? ParameterDirection.InputOutput :
ParameterDirection.Input,
arg == null ? DbType.String : arg.GetType().ToDbType(),
isRet ? 4 : 0,
(isOut || isRet) ? DBNull.Value : dcv.Value);
}
else if (arg is DynamicSchemaColumn)
{
var dsc = (DynamicSchemaColumn)arg;
string paramName = i.ToString();
bool isOut = false;
bool isRet = false;
bool isBoth = false;
if (info.ArgumentNames.Count > i)
{
isOut = info.ArgumentNames[i].StartsWith("out_");
isRet = info.ArgumentNames[i].StartsWith("ret_");
isBoth = info.ArgumentNames[i].StartsWith("both_");
paramName = isOut || isRet ?
info.ArgumentNames[i].Substring(4) :
isBoth ? info.ArgumentNames[i].Substring(5) :
info.ArgumentNames[i];
}
paramName = dsc.Name ?? paramName;
if (isRet)
retIsAdded = true;
if (isOut || isRet || isBoth)
{
if (retParams == null)
retParams = new Dictionary<string, int>();
retParams.Add(paramName, cmd.Parameters.Count);
}
cmd.AddParameter(
_db.GetParameterName(paramName),
isOut ? ParameterDirection.Output :
isRet ? ParameterDirection.ReturnValue :
isBoth ? ParameterDirection.InputOutput :
ParameterDirection.Input,
dsc.Type, dsc.Size, dsc.Precision, dsc.Scale,
DBNull.Value);
} }
else else
{ {
@@ -152,6 +233,9 @@ namespace DynamORM
bool isRet = info.ArgumentNames[i].StartsWith("ret_"); bool isRet = info.ArgumentNames[i].StartsWith("ret_");
bool isBoth = info.ArgumentNames[i].StartsWith("both_"); bool isBoth = info.ArgumentNames[i].StartsWith("both_");
if (isRet)
retIsAdded = true;
string paramName = isOut || isRet ? string paramName = isOut || isRet ?
info.ArgumentNames[i].Substring(4) : info.ArgumentNames[i].Substring(4) :
isBoth ? info.ArgumentNames[i].Substring(5) : isBoth ? info.ArgumentNames[i].Substring(5) :
@@ -168,8 +252,11 @@ namespace DynamORM
_db.GetParameterName(paramName), _db.GetParameterName(paramName),
isOut ? ParameterDirection.Output : isOut ? ParameterDirection.Output :
isRet ? ParameterDirection.ReturnValue : isRet ? ParameterDirection.ReturnValue :
isBoth ? ParameterDirection.InputOutput : ParameterDirection.Input, isBoth ? ParameterDirection.InputOutput :
arg == null ? DbType.String : arg.GetType().ToDbType(), 0, isOut ? DBNull.Value : arg); ParameterDirection.Input,
arg == null ? isRet ? DbType.Int32 : DbType.String : arg.GetType().ToDbType(),
isRet ? 4 : 0,
(isOut || isRet) ? DBNull.Value : arg);
} }
else else
cmd.AddParameter(_db, arg); cmd.AddParameter(_db, arg);
@@ -220,7 +307,7 @@ namespace DynamORM
cache = rdr.CachedReader(); cache = rdr.CachedReader();
while (cache.Read()) while (cache.Read())
listInstance.Add(cache[0] == DBNull.Value ? defVal : argType.CastObject(cache[0])); listInstance.Add(cache[0] != null && cache[0] != DBNull.Value ? argType.CastObject(cache[0]) : defVal);
mainResult = listInstance; mainResult = listInstance;
} }
@@ -237,7 +324,7 @@ namespace DynamORM
while (cache.Read()) while (cache.Read())
{ {
if (cache[0] == DBNull.Value && Guid.TryParse(cache[0].ToString(), out Guid g)) if (cache[0] != null && cache[0] != DBNull.Value && Guid.TryParse(cache[0].ToString(), out Guid g))
listInstance.Add(g); listInstance.Add(g);
} }
@@ -247,32 +334,41 @@ namespace DynamORM
{ {
DynamicTypeMap mapper = DynamicMapperCache.GetMapper(argType); DynamicTypeMap mapper = DynamicMapperCache.GetMapper(argType);
if (mapper == null) if (mapper == null)
throw new InvalidCastException(string.Format("Don't konw what to do with this type: '{0}'.", argType.ToString())); throw new InvalidCastException(string.Format("Don't know what to do with this type: '{0}'.", argType.ToString()));
IDataReader cache = null; IDataReader cache = null;
using (IDataReader rdr = cmd.ExecuteReader()) using (IDataReader rdr = cmd.ExecuteReader())
cache = rdr.CachedReader(); cache = rdr.CachedReader();
mainResult = cache.EnumerateReader().MapEnumerable(argType).ToList(); var lt = typeof(List<>);
var ltc = lt.MakeGenericType(argType);
var instance = Activator.CreateInstance(ltc) as IList;
foreach (var item in cache.EnumerateReader())
instance.Add(DynamicExtensions.Map(item, argType));
mainResult = instance;
//mainResult = cache.EnumerateReader().MapEnumerable(argType).ToList();
} }
} }
else if (types[0].IsValueType || types[0] == typeof(string)) else if (types[0].IsValueType || types[0] == typeof(string))
{ {
mainResult = cmd.ExecuteScalar(); mainResult = cmd.ExecuteScalar();
if (mainResult != DBNull.Value) if (mainResult != null && mainResult != DBNull.Value)
mainResult = types[0].CastObject(mainResult); mainResult = types[0].CastObject(mainResult);
} }
else if (types[0] == typeof(Guid)) else if (types[0] == typeof(Guid))
{ {
mainResult = cmd.ExecuteScalar(); mainResult = cmd.ExecuteScalar();
if (mainResult != DBNull.Value && Guid.TryParse(mainResult.ToString(), out Guid g)) if (mainResult != null && mainResult != DBNull.Value && Guid.TryParse(mainResult.ToString(), out Guid g))
mainResult = g; mainResult = g;
} }
else else
{ {
DynamicTypeMap mapper = DynamicMapperCache.GetMapper(types[0]); DynamicTypeMap mapper = DynamicMapperCache.GetMapper(types[0]);
if (mapper == null) if (mapper == null)
throw new InvalidCastException(string.Format("Don't konw what to do with this type: '{0}'.", types[0].ToString())); throw new InvalidCastException(string.Format("Don't know what to do with this type: '{0}'.", types[0].ToString()));
using (IDataReader rdr = cmd.ExecuteReader()) using (IDataReader rdr = cmd.ExecuteReader())
if (rdr.Read()) if (rdr.Read())
@@ -282,8 +378,33 @@ namespace DynamORM
} }
} }
else else
{
var returnName = _db.GetParameterName("___result___");
if (!retIsAdded)
cmd.AddParameter(returnName, ParameterDirection.ReturnValue, DbType.Int32, 4, 0, 0, DBNull.Value);
mainResult = cmd.ExecuteNonQuery(); mainResult = cmd.ExecuteNonQuery();
IDbDataParameter returnParam = null;
if (!retIsAdded)
returnParam = cmd.Parameters[returnName] as IDbDataParameter;
else
{
foreach (var e in cmd.Parameters)
{
var p = e as IDbDataParameter;
if (p != null && p.Direction == ParameterDirection.ReturnValue)
{
returnParam = p;
break;
}
}
}
if (returnParam != null && returnParam.Value != null && returnParam.Value != DBNull.Value)
mainResult = returnParam.Value;
}
#endregion Get main result #endregion Get main result
#region Handle out params #region Handle out params

View File

@@ -12,9 +12,7 @@ namespace Tester
private static DynamicDatabase GetORM() private static DynamicDatabase GetORM()
{ {
return new DynamicDatabase(System.Data.SqlClient.SqlClientFactory.Instance, return new DynamicDatabase(System.Data.SqlClient.SqlClientFactory.Instance,
"packet size=4096;User Id=sa;Password=sa123;data source=127.0.0.1,1435;initial catalog=MOM_LANGOWSKI_WMS_TEST;", "packet size=4096;User Id=sa;Password=;data source=192.168.22.;initial catalog=PLAYGROUND;",
//"packet size=4096;User Id=sa;Password=sa123;data source=192.168.1.9,1433;initial catalog=MOM_NEXT_Florentyna_WMS_PROD;",
//"packet size=4096;User Id=sa;Password=sa123;data source=127.0.0.1;initial catalog=DynamORM;",
DynamicDatabaseOptions.SingleConnection | DynamicDatabaseOptions.SingleTransaction | DynamicDatabaseOptions.SupportSchema | DynamicDatabaseOptions.SingleConnection | DynamicDatabaseOptions.SingleTransaction | DynamicDatabaseOptions.SupportSchema |
DynamicDatabaseOptions.SupportStoredProcedures | DynamicDatabaseOptions.SupportTop | DynamicDatabaseOptions.DumpCommands); DynamicDatabaseOptions.SupportStoredProcedures | DynamicDatabaseOptions.SupportTop | DynamicDatabaseOptions.DumpCommands);
@@ -31,6 +29,58 @@ namespace Tester
using (var db = GetORM()) using (var db = GetORM())
{ {
db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_Scalar AS SELECT 42;");
var res0 = db.Procedures.sp_Exp_Scalar();
var res1 = db.Procedures.sp_Exp_Scalar<int>();
db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_ReturnInt AS RETURN 42;");
var res2 = db.Procedures.sp_Exp_ReturnInt();
db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_SomeData AS
SELECT 1 Id, 'Some Name 1' [Name], 'Some Desc 1' [Desc], GETDATE() [Date]
UNION ALL SELECT 2 Id, 'Some Name 2', 'Some Desc 2', GETDATE() [Date];");
var res3 = db.Procedures.sp_Exp_SomeData<sp_Exp_SomeData_Result>();
var res4 = db.Procedures.sp_Exp_SomeData<List<sp_Exp_SomeData_Result>>();
db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_SomeInputAndOutput
@Name nvarchar(50),
@Result nvarchar(256) OUTPUT
AS
SELECT @Result = 'Hi, ' + @Name + ' your lucky number is 42!';");
var res5 = db.Procedures.sp_Exp_SomeInputAndOutput<string, sp_Exp_SomeInputAndOutput_Result>(Name: "G4g4r1n", out_Result: new DynamicColumn
{
Schema = new DynamicSchemaColumn
{
Size = 256,
},
}, ret_Return: 0);
var res6 = db.Procedures.sp_Exp_SomeInputAndOutput<string, sp_Exp_SomeInputAndOutput_Result>(Name: "G4g4r1n", out_Result: new DynamicSchemaColumn
{
Size = 256,
}, ret_Return: 0);
db.Execute(@"CREATE OR ALTER PROCEDURE sp_Exp_SomeInputAndOutputWithDataAndReturn
@Name nvarchar(50),
@Result nvarchar(256) OUTPUT
AS
SELECT @Result = 'Hi, ' + @Name + ' your lucky number is 42!'
SELECT 1 Id, 'Some Name 1' [Name], 'Some Desc 1' [Desc], GETDATE() [Date]
UNION ALL SELECT 2 Id, 'Some Name 2', 'Some Desc 2', GETDATE() [Date]
RETURN 42;");
var res7 = db.Procedures.sp_Exp_SomeInputAndOutputWithDataAndReturn<List<sp_Exp_SomeInputAndOutputWithDataAndReturn_Result.Data>, sp_Exp_SomeInputAndOutputWithDataAndReturn_Result>(Name: "G4g4r1n", out_Result: new DynamicColumn
{
Schema = new DynamicSchemaColumn
{
Size = 256,
},
}, ret_Return: 0);
var res8 = db.Procedures.sp_Exp_SomeInputAndOutputWithDataAndReturn<List<sp_Exp_SomeInputAndOutputWithDataAndReturn_Result.Data>, sp_Exp_SomeInputAndOutputWithDataAndReturn_Result>(Name: "G4g4r1n", out_Result: new DynamicSchemaColumn
{
Size = 256,
}, ret_Return: 0);
//try //try
//{ //{
// db.Execute("DROP TABLE Experiments "); // db.Execute("DROP TABLE Experiments ");
@@ -50,28 +100,50 @@ namespace Tester
//db.Execute("DROP TABLE Experiments "); //db.Execute("DROP TABLE Experiments ");
IDataReader rdr = db.Procedures.sp_getdate<IDataReader>(); //IDataReader rdr = db.Procedures.sp_getdate<IDataReader>();
var dt = rdr.ToDataTable(); //var dt = rdr.ToDataTable();
var dt2 = db.Procedures.sp_getdate<DataTable>(); //var dt2 = db.Procedures.sp_getdate<DataTable>();
db.Procedures.usp_API_Generate_Doc_Number<string>(key: Guid.NewGuid(), mdn_id: "ZZ"); //db.Procedures.usp_API_Generate_Doc_Number<string>(key: Guid.NewGuid(), mdn_id: "ZZ");
var resL = (db.Procedures.GetProductDesc<IList<GetProductDesc_Result>>() as IEnumerable<dynamic>) //var resL = (db.Procedures.GetProductDesc<IList<GetProductDesc_Result>>() as IEnumerable<dynamic>)
.Cast<GetProductDesc_Result>() // .Cast<GetProductDesc_Result>()
.ToArray(); // .ToArray();
var res = db.Procedures.GetProductDesc_withparameters<GetProductDesc_Result>(PID: 707); //var res = db.Procedures.GetProductDesc_withparameters<GetProductDesc_Result>(PID: 707);
res = db.Procedures.GetProductDesc_withDefaultparameters<GetProductDesc_Result>(); //res = db.Procedures.GetProductDesc_withDefaultparameters<GetProductDesc_Result>();
int id = -1; //int id = -1;
var resD = db.Procedures.ins_NewEmp_with_outputparamaters(Ename: "Test2", out_EId: id); //var resD = db.Procedures.ins_NewEmp_with_outputparamaters(Ename: "Test2", out_EId: id);
} }
} }
private class GetProductDesc_Result private class sp_Exp_SomeData_Result
{ {
public virtual int ProductID { get; set; } public virtual int Id { get; set; }
public virtual string ProductName { get; set; } public virtual string Name { get; set; }
public virtual string ProductDescription { get; set; } public virtual string Desc { get; set; }
public virtual DateTime Date { get; set; }
}
private class sp_Exp_SomeInputAndOutput_Result
{
public virtual string Result { get; set; }
public virtual string Return { get; set; }
}
private class sp_Exp_SomeInputAndOutputWithDataAndReturn_Result
{
public class Data
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Desc { get; set; }
public virtual DateTime Date { get; set; }
}
public virtual List<Data> sp_Exp_SomeInputAndOutputWithDataAndReturn { get; set; }
public virtual string Result { get; set; }
public virtual string Return { get; set; }
} }
} }
} }

View File

@@ -17,7 +17,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" /> <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
<PackageReference Include="System.Data.Common" Version="4.3.0" /> <PackageReference Include="System.Data.Common" Version="4.3.0" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.5" /> <PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
</ItemGroup> </ItemGroup>
<ItemGroup Condition="$(TargetFramework.StartsWith('net4')) AND '$(MSBuildRuntimeType)' == 'Core' AND '$(OS)' != 'Windows_NT'"> <ItemGroup Condition="$(TargetFramework.StartsWith('net4')) AND '$(MSBuildRuntimeType)' == 'Core' AND '$(OS)' != 'Windows_NT'">
<PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" /> <PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" PrivateAssets="All" />