Commit 5f7e88a4 authored by Marc Gravell's avatar Marc Gravell

Set InitialLONGFetchSize to -1 if it exists

parent fa7bfe4f
...@@ -141,8 +141,8 @@ public struct CommandDefinition ...@@ -141,8 +141,8 @@ public struct CommandDefinition
internal IDbCommand SetupCommand(IDbConnection cnn, Action<IDbCommand, object> paramReader) internal IDbCommand SetupCommand(IDbConnection cnn, Action<IDbCommand, object> paramReader)
{ {
var cmd = cnn.CreateCommand(); var cmd = cnn.CreateCommand();
var bindByName = GetBindByName(cmd.GetType()); var init = GetInit(cmd.GetType());
if (bindByName != null) bindByName(cmd, true); if (init != null) init(cmd);
if (transaction != null) if (transaction != null)
cmd.Transaction = transaction; cmd.Transaction = transaction;
cmd.CommandText = commandText; cmd.CommandText = commandText;
...@@ -157,37 +157,58 @@ internal IDbCommand SetupCommand(IDbConnection cnn, Action<IDbCommand, object> p ...@@ -157,37 +157,58 @@ internal IDbCommand SetupCommand(IDbConnection cnn, Action<IDbCommand, object> p
return cmd; return cmd;
} }
static SqlMapper.Link<Type, Action<IDbCommand, bool>> bindByNameCache; static SqlMapper.Link<Type, Action<IDbCommand>> commandInitCache;
static Action<IDbCommand, bool> GetBindByName(Type commandType) static Action<IDbCommand> GetInit(Type commandType)
{ {
if (commandType == null) return null; // GIGO if (commandType == null) return null; // GIGO
Action<IDbCommand, bool> action; Action<IDbCommand> action;
if (SqlMapper.Link<Type, Action<IDbCommand, bool>>.TryGet(bindByNameCache, commandType, out action)) if (SqlMapper.Link<Type, Action<IDbCommand>>.TryGet(commandInitCache, commandType, out action))
{ {
return action; return action;
} }
var prop = commandType.GetProperty("BindByName", BindingFlags.Public | BindingFlags.Instance); var bindByName = GetBasicPropertySetter(commandType, "BindByName", typeof(bool));
var initialLongFetchSize = GetBasicPropertySetter(commandType, "InitialLONGFetchSize", typeof(int));
action = null; action = null;
ParameterInfo[] indexers; if (bindByName != null || initialLongFetchSize != null)
MethodInfo setter;
if (prop != null && prop.CanWrite && prop.PropertyType == typeof(bool)
&& ((indexers = prop.GetIndexParameters()) == null || indexers.Length == 0)
&& (setter = prop.GetSetMethod()) != null
)
{ {
var method = new DynamicMethod(commandType.Name + "_BindByName", null, new Type[] { typeof(IDbCommand), typeof(bool) }); var method = new DynamicMethod(commandType.Name + "_init", null, new Type[] { typeof(IDbCommand) });
var il = method.GetILGenerator(); var il = method.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Castclass, commandType); if (bindByName != null)
il.Emit(OpCodes.Ldarg_1); {
il.EmitCall(OpCodes.Callvirt, setter, null); // .BindByName = true
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Castclass, commandType);
il.Emit(OpCodes.Ldc_I4_1);
il.EmitCall(OpCodes.Callvirt, bindByName, null);
}
if (initialLongFetchSize != null)
{
// .InitialLONGFetchSize = -1
il.Emit(OpCodes.Ldarg_0);
il.Emit(OpCodes.Castclass, commandType);
il.Emit(OpCodes.Ldc_I4_M1);
il.EmitCall(OpCodes.Callvirt, initialLongFetchSize, null);
}
il.Emit(OpCodes.Ret); il.Emit(OpCodes.Ret);
action = (Action<IDbCommand, bool>)method.CreateDelegate(typeof(Action<IDbCommand, bool>)); action = (Action<IDbCommand>)method.CreateDelegate(typeof(Action<IDbCommand>));
} }
// cache it // cache it
SqlMapper.Link<Type, Action<IDbCommand, bool>>.TryAdd(ref bindByNameCache, commandType, ref action); SqlMapper.Link<Type, Action<IDbCommand>>.TryAdd(ref commandInitCache, commandType, ref action);
return action; return action;
} }
static MethodInfo GetBasicPropertySetter(Type declaringType, string name, Type expectedType)
{
var prop = declaringType.GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
ParameterInfo[] indexers;
if (prop != null && prop.CanWrite && prop.PropertyType == expectedType
&& ((indexers = prop.GetIndexParameters()) == null || indexers.Length == 0))
{
return prop.GetSetMethod();
}
return null;
}
} }
/// <summary> /// <summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment