-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FetchSize global setting (#1946)
* fix #1945 * PR number * docs
Showing
4 changed files
with
47 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,9 @@ internal IDbCommand SetupCommand(IDbConnection cnn, Action<IDbCommand, object> p | |
|
||
private static SqlMapper.Link<Type, Action<IDbCommand>> commandInitCache; | ||
|
||
internal static void ResetCommandInitCache() | ||
=> SqlMapper.Link<Type, Action<IDbCommand>>.Clear(ref commandInitCache); | ||
|
||
private static Action<IDbCommand> GetInit(Type commandType) | ||
{ | ||
if (commandType == null) | ||
|
@@ -141,29 +144,42 @@ private static Action<IDbCommand> GetInit(Type commandType) | |
} | ||
var bindByName = GetBasicPropertySetter(commandType, "BindByName", typeof(bool)); | ||
var initialLongFetchSize = GetBasicPropertySetter(commandType, "InitialLONGFetchSize", typeof(int)); | ||
var fetchSize = GetBasicPropertySetter(commandType, "FetchSize", typeof(long)); | ||
|
||
action = null; | ||
if (bindByName != null || initialLongFetchSize != null) | ||
if (bindByName is not null || initialLongFetchSize is not null || fetchSize is not null) | ||
{ | ||
var method = new DynamicMethod(commandType.Name + "_init", null, new Type[] { typeof(IDbCommand) }); | ||
var il = method.GetILGenerator(); | ||
|
||
if (bindByName != null) | ||
if (bindByName is not 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) | ||
if (initialLongFetchSize is not 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); | ||
} | ||
if (fetchSize is not null) | ||
{ | ||
var snapshot = SqlMapper.Settings.FetchSize; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mgravell
Author
Member
|
||
if (snapshot >= 0) | ||
{ | ||
// .FetchSize = {withValue} | ||
il.Emit(OpCodes.Ldarg_0); | ||
il.Emit(OpCodes.Castclass, commandType); | ||
il.Emit(OpCodes.Ldc_I8, snapshot); // bake it as a constant | ||
il.EmitCall(OpCodes.Callvirt, fetchSize, null); | ||
} | ||
} | ||
il.Emit(OpCodes.Ret); | ||
action = (Action<IDbCommand>)method.CreateDelegate(typeof(Action<IDbCommand>)); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Thank you for making this improvement. We found this change to be useful when executing Oracle queries where we were returning large columns. In a few cases we've seen 100x improvement in query speed. However, I wonder about the implementation here where FetchSize can only be set globally. Technically it could be set on each command and it seems this would be more appropriate. Why not just add another constructor argument to the CommandDefinition class so that the fetch size could be more carefully applied as the situation called for it?