Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always use TypeHandler for parameters if available #177

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Dapper NET40/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ internal static void SetHandler(ITypeHandler handler)
internal static DbType LookupDbType(Type type, string name, out ITypeHandler handler)
{
DbType dbType;
handler = null;
typeHandlers.TryGetValue(type, out handler);
var nullUnderlyingType = Nullable.GetUnderlyingType(type);
if (nullUnderlyingType != null) type = nullUnderlyingType;
if (type.IsEnum && !typeMap.ContainsKey(type))
Expand All @@ -869,7 +869,7 @@ internal static DbType LookupDbType(Type type, string name, out ITypeHandler han
return DynamicParameters.EnumerableMultiParameter;
}

if (typeHandlers.TryGetValue(type, out handler))
if (handler != null)
{
return DbType.Object;
}
Expand Down
80 changes: 79 additions & 1 deletion Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3841,7 +3841,7 @@ public void SO25297173_DynamicIn()
result.Contains(5).IsTrue();
result.Contains(6).IsTrue();
}

public void AllowIDictionaryParameters()
{
var parameters = new Dictionary<string, object>
Expand All @@ -3851,6 +3851,84 @@ public void AllowIDictionaryParameters()

connection.Query("SELECT @param1", parameters);
}

public void TypeHandlerSetValueCalledForBuiltInSqlTypes()
{
// Commented out tests are for types that aren't directly supported by SqlDbType

TypeHandlerSetValueCalledForBuiltInSqlType<byte>();
//TypeHandlerSetValueCalledForBuiltInSqlType<sbyte>();
TypeHandlerSetValueCalledForBuiltInSqlType<short>();
//TypeHandlerSetValueCalledForBuiltInSqlType<ushort>();
TypeHandlerSetValueCalledForBuiltInSqlType<int>();
//TypeHandlerSetValueCalledForBuiltInSqlType<uint>();
TypeHandlerSetValueCalledForBuiltInSqlType<long>();
//TypeHandlerSetValueCalledForBuiltInSqlType<ulong>();
TypeHandlerSetValueCalledForBuiltInSqlType<float>();
TypeHandlerSetValueCalledForBuiltInSqlType<double>();
TypeHandlerSetValueCalledForBuiltInSqlType<decimal>();
TypeHandlerSetValueCalledForBuiltInSqlType<bool>();
TypeHandlerSetValueCalledForBuiltInSqlType<string>();
TypeHandlerSetValueCalledForBuiltInSqlType<char>();
TypeHandlerSetValueCalledForBuiltInSqlType<Guid>();
TypeHandlerSetValueCalledForBuiltInSqlType<DateTime>((DateTime)SqlDateTime.MinValue);
TypeHandlerSetValueCalledForBuiltInSqlType<DateTimeOffset>();
TypeHandlerSetValueCalledForBuiltInSqlType<TimeSpan>();
TypeHandlerSetValueCalledForBuiltInSqlType<byte[]>();
TypeHandlerSetValueCalledForBuiltInSqlType<byte?>();
//TypeHandlerSetValueCalledForBuiltInSqlType<sbyte?>();
TypeHandlerSetValueCalledForBuiltInSqlType<short?>();
//TypeHandlerSetValueCalledForBuiltInSqlType<ushort?>();
TypeHandlerSetValueCalledForBuiltInSqlType<int?>();
//TypeHandlerSetValueCalledForBuiltInSqlType<uint?>();
TypeHandlerSetValueCalledForBuiltInSqlType<long?>();
//TypeHandlerSetValueCalledForBuiltInSqlType<ulong?>();
TypeHandlerSetValueCalledForBuiltInSqlType<float?>();
TypeHandlerSetValueCalledForBuiltInSqlType<double?>();
TypeHandlerSetValueCalledForBuiltInSqlType<decimal?>();
TypeHandlerSetValueCalledForBuiltInSqlType<bool?>();
TypeHandlerSetValueCalledForBuiltInSqlType<char?>();
TypeHandlerSetValueCalledForBuiltInSqlType<Guid?>();
TypeHandlerSetValueCalledForBuiltInSqlType<DateTime?>();
TypeHandlerSetValueCalledForBuiltInSqlType<DateTimeOffset?>();
TypeHandlerSetValueCalledForBuiltInSqlType<TimeSpan?>();
TypeHandlerSetValueCalledForBuiltInSqlType<object>();
}

private void TypeHandlerSetValueCalledForBuiltInSqlType<T>(T testValue = default(T))
{
Dapper.SqlMapper.ResetTypeHandlers();

var handler = new TypeHandlerSetValueCalledHandler();

// Test that logic still works without handler
connection.Query("SELECT @param", new { param = testValue });

// Now try with handler overriding default logic
Dapper.SqlMapper.AddTypeHandler(typeof(T), handler);
connection.Query("SELECT @param1", new { param1 = testValue });

Dapper.SqlMapper.ResetTypeHandlers();

handler.WasSet.IsTrue();
}

public class TypeHandlerSetValueCalledHandler : Dapper.SqlMapper.ITypeHandler
{
public bool WasSet = false;

public object Parse(Type destinationType, object value)
{
throw new NotImplementedException();
}

public void SetValue(IDbDataParameter parameter, object value)
{
WasSet = true;
parameter.Value = value;
}
}

#if POSTGRESQL

class Cat
Expand Down