mirror of
https://github.com/DapperLib/Dapper.git
synced 2026-10-21 08:20:14 +08:00
Add SqlMapper.RemoveTypeMap() method
This commit is contained in:
@@ -2484,6 +2484,52 @@ end");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public class RecordingTypeHandler<T> : Dapper.SqlMapper.TypeHandler<T>
|
||||
{
|
||||
public override void SetValue(IDbDataParameter parameter, T value)
|
||||
{
|
||||
SetValueWasCalled = true;
|
||||
parameter.Value = value;
|
||||
}
|
||||
|
||||
public override T Parse(object value)
|
||||
{
|
||||
ParseWasCalled = true;
|
||||
return (T)value;
|
||||
}
|
||||
|
||||
public bool SetValueWasCalled { get; set; }
|
||||
public bool ParseWasCalled { get; set; }
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Test_RemoveTypeMap()
|
||||
{
|
||||
Dapper.SqlMapper.ResetTypeHandlers();
|
||||
Dapper.SqlMapper.RemoveTypeMap(typeof(DateTime));
|
||||
|
||||
var dateTimeHandler = new RecordingTypeHandler<DateTime>();
|
||||
Dapper.SqlMapper.AddTypeHandler(dateTimeHandler);
|
||||
|
||||
connection.Execute("CREATE TABLE #Test_RemoveTypeMap (x datetime NOT NULL);");
|
||||
|
||||
try
|
||||
{
|
||||
connection.Execute(@"INSERT INTO #Test_RemoveTypeMap VALUES (@Now)", new { DateTime.Now });
|
||||
connection.Query<DateTime>("SELECT * FROM #Test_RemoveTypeMap");
|
||||
|
||||
dateTimeHandler.ParseWasCalled.IsTrue();
|
||||
dateTimeHandler.SetValueWasCalled.IsTrue();
|
||||
}
|
||||
finally
|
||||
{
|
||||
connection.Execute("DROP TABLE #Test_RemoveTypeMap");
|
||||
Dapper.SqlMapper.AddTypeMap(typeof(DateTime), DbType.DateTime); // or an option to reset type map?
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Issue130_IConvertible()
|
||||
{
|
||||
|
||||
@@ -260,6 +260,22 @@ namespace Dapper
|
||||
typeMap = newCopy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the specified type from the Type/DbType mapping table
|
||||
/// </summary>
|
||||
public static void RemoveTypeMap(Type type)
|
||||
{
|
||||
// use clone, mutate, replace to avoid threading issues
|
||||
var snapshot = typeMap;
|
||||
|
||||
if (!snapshot.ContainsKey(type)) return; // nothing to do
|
||||
|
||||
var newCopy = new Dictionary<Type, DbType>(snapshot);
|
||||
newCopy.Remove(type);
|
||||
|
||||
typeMap = newCopy;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configure the specified type to be processed by a custom handler
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user