mirror of
https://github.com/DapperLib/Dapper.git
synced 2026-10-28 20:57:41 +08:00
Fix list handling when using dynamic parameters; see http://stackoverflow.com/q/12723922/23354
This commit is contained in:
+49
-32
@@ -408,7 +408,7 @@ namespace Dapper
|
||||
}
|
||||
|
||||
internal const string LinqBinary = "System.Data.Linq.Binary";
|
||||
private static DbType LookupDbType(Type type, string name)
|
||||
internal static DbType LookupDbType(Type type, string name)
|
||||
{
|
||||
DbType dbType;
|
||||
var nullUnderlyingType = Nullable.GetUnderlyingType(type);
|
||||
@@ -1394,7 +1394,7 @@ this IDbConnection cnn, string sql, Func<TFirst, TSecond, TThird, TFourth, TRetu
|
||||
/// Internal use only
|
||||
/// </summary>
|
||||
[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
|
||||
[Obsolete("This method is for internal usage only", true)]
|
||||
[Obsolete("This method is for internal usage only", false)]
|
||||
public static void PackListParameters(IDbCommand command, string namePrefix, object value)
|
||||
{
|
||||
// initially we tried TVP, however it performs quite poorly.
|
||||
@@ -2572,41 +2572,58 @@ string name, object value = null, DbType? dbType = null, ParameterDirection? dir
|
||||
|
||||
foreach (var param in parameters.Values)
|
||||
{
|
||||
string name = Clean(param.Name);
|
||||
bool add = !command.Parameters.Contains(name);
|
||||
IDbDataParameter p;
|
||||
if(add)
|
||||
{
|
||||
p = command.CreateParameter();
|
||||
p.ParameterName = name;
|
||||
} else
|
||||
{
|
||||
p = (IDbDataParameter)command.Parameters[name];
|
||||
}
|
||||
var dbType = param.DbType;
|
||||
var val = param.Value;
|
||||
p.Value = val ?? DBNull.Value;
|
||||
p.Direction = param.ParameterDirection;
|
||||
var s = val as string;
|
||||
if (s != null)
|
||||
string name = Clean(param.Name);
|
||||
|
||||
if (dbType == null && val != null) dbType = SqlMapper.LookupDbType(val.GetType(), name);
|
||||
|
||||
if (dbType == DbType.Xml)
|
||||
{ // actually represents "in" lists
|
||||
#pragma warning disable 612, 618
|
||||
SqlMapper.PackListParameters(command, name, val);
|
||||
#pragma warning restore 612, 618
|
||||
}
|
||||
else
|
||||
{
|
||||
if (s.Length <= 4000)
|
||||
|
||||
bool add = !command.Parameters.Contains(name);
|
||||
IDbDataParameter p;
|
||||
if (add)
|
||||
{
|
||||
p.Size = 4000;
|
||||
p = command.CreateParameter();
|
||||
p.ParameterName = name;
|
||||
}
|
||||
else
|
||||
{
|
||||
p = (IDbDataParameter)command.Parameters[name];
|
||||
}
|
||||
|
||||
p.Value = val ?? DBNull.Value;
|
||||
p.Direction = param.ParameterDirection;
|
||||
var s = val as string;
|
||||
if (s != null)
|
||||
{
|
||||
if (s.Length <= 4000)
|
||||
{
|
||||
p.Size = 4000;
|
||||
}
|
||||
}
|
||||
if (param.Size != null)
|
||||
{
|
||||
p.Size = param.Size.Value;
|
||||
}
|
||||
if (dbType != null)
|
||||
{
|
||||
p.DbType = dbType.Value;
|
||||
}
|
||||
if (add)
|
||||
{
|
||||
command.Parameters.Add(p);
|
||||
}
|
||||
param.AttachedParam = p;
|
||||
}
|
||||
if (param.Size != null)
|
||||
{
|
||||
p.Size = param.Size.Value;
|
||||
}
|
||||
if (param.DbType != null)
|
||||
{
|
||||
p.DbType = param.DbType.Value;
|
||||
}
|
||||
if (add)
|
||||
{
|
||||
command.Parameters.Add(p);
|
||||
}
|
||||
param.AttachedParam = p;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-1
@@ -122,7 +122,12 @@ end
|
||||
{
|
||||
method.Invoke(tester, null);
|
||||
Console.WriteLine(" - OK!");
|
||||
} catch (Exception ex)
|
||||
} catch(TargetInvocationException tie)
|
||||
{
|
||||
fail++;
|
||||
Console.WriteLine(" - " + tie.InnerException.Message);
|
||||
|
||||
}catch (Exception ex)
|
||||
{
|
||||
fail++;
|
||||
Console.WriteLine(" - " + ex.Message);
|
||||
|
||||
@@ -1598,6 +1598,34 @@ Order by p.Id";
|
||||
result[2].IsEqualTo(3);
|
||||
}
|
||||
|
||||
public void TestAppendingAListAsDictionary()
|
||||
{
|
||||
DynamicParameters p = new DynamicParameters();
|
||||
var list = new int[] { 1, 2, 3 };
|
||||
var args = new Dictionary<string, object>();
|
||||
args.Add("ids", list);
|
||||
p.AddDynamicParams(args);
|
||||
|
||||
var result = connection.Query<int>("select * from (select 1 A union all select 2 union all select 3) X where A in @ids", p).ToList();
|
||||
|
||||
result[0].IsEqualTo(1);
|
||||
result[1].IsEqualTo(2);
|
||||
result[2].IsEqualTo(3);
|
||||
}
|
||||
|
||||
public void TestAppendingAListByName()
|
||||
{
|
||||
DynamicParameters p = new DynamicParameters();
|
||||
var list = new int[] { 1, 2, 3 };
|
||||
p.Add("ids", list);
|
||||
|
||||
var result = connection.Query<int>("select * from (select 1 A union all select 2 union all select 3) X where A in @ids", p).ToList();
|
||||
|
||||
result[0].IsEqualTo(1);
|
||||
result[1].IsEqualTo(2);
|
||||
result[2].IsEqualTo(3);
|
||||
}
|
||||
|
||||
public void TestUniqueIdentifier()
|
||||
{
|
||||
var guid = Guid.NewGuid();
|
||||
|
||||
Reference in New Issue
Block a user