@@ -131,6 +131,9 @@ internal IDbCommand SetupCommand(IDbConnection cnn, Action<IDbCommand, object> p
131131
132132 private static SqlMapper . Link < Type , Action < IDbCommand > > commandInitCache ;
133133
134+ internal static void ResetCommandInitCache ( )
135+ => SqlMapper . Link < Type , Action < IDbCommand > > . Clear ( ref commandInitCache ) ;
136+
134137 private static Action < IDbCommand > GetInit ( Type commandType )
135138 {
136139 if ( commandType == null )
@@ -141,29 +144,42 @@ private static Action<IDbCommand> GetInit(Type commandType)
141144 }
142145 var bindByName = GetBasicPropertySetter ( commandType , "BindByName" , typeof ( bool ) ) ;
143146 var initialLongFetchSize = GetBasicPropertySetter ( commandType , "InitialLONGFetchSize" , typeof ( int ) ) ;
147+ var fetchSize = GetBasicPropertySetter ( commandType , "FetchSize" , typeof ( long ) ) ;
144148
145149 action = null ;
146- if ( bindByName != null || initialLongFetchSize != null )
150+ if ( bindByName is not null || initialLongFetchSize is not null || fetchSize is not null )
147151 {
148152 var method = new DynamicMethod ( commandType . Name + "_init" , null , new Type [ ] { typeof ( IDbCommand ) } ) ;
149153 var il = method . GetILGenerator ( ) ;
150154
151- if ( bindByName != null )
155+ if ( bindByName is not null )
152156 {
153157 // .BindByName = true
154158 il . Emit ( OpCodes . Ldarg_0 ) ;
155159 il . Emit ( OpCodes . Castclass , commandType ) ;
156160 il . Emit ( OpCodes . Ldc_I4_1 ) ;
157161 il . EmitCall ( OpCodes . Callvirt , bindByName , null ) ;
158162 }
159- if ( initialLongFetchSize != null )
163+ if ( initialLongFetchSize is not null )
160164 {
161165 // .InitialLONGFetchSize = -1
162166 il . Emit ( OpCodes . Ldarg_0 ) ;
163167 il . Emit ( OpCodes . Castclass , commandType ) ;
164168 il . Emit ( OpCodes . Ldc_I4_M1 ) ;
165169 il . EmitCall ( OpCodes . Callvirt , initialLongFetchSize , null ) ;
166170 }
171+ if ( fetchSize is not null )
172+ {
173+ var snapshot = SqlMapper . Settings . FetchSize ;
174+ if ( snapshot >= 0 )
175+ {
176+ // .FetchSize = {withValue}
177+ il . Emit ( OpCodes . Ldarg_0 ) ;
178+ il . Emit ( OpCodes . Castclass , commandType ) ;
179+ il . Emit ( OpCodes . Ldc_I8 , snapshot ) ; // bake it as a constant
180+ il . EmitCall ( OpCodes . Callvirt , fetchSize , null ) ;
181+ }
182+ }
167183 il . Emit ( OpCodes . Ret ) ;
168184 action = ( Action < IDbCommand > ) method . CreateDelegate ( typeof ( Action < IDbCommand > ) ) ;
169185 }
0 commit comments