@@ -19,15 +19,25 @@ internal static class OrmLiteWriteCommandExtensionsAsync
1919 {
2020 internal static ILog Log = LogManager . GetLogger ( typeof ( OrmLiteWriteCommandExtensionsAsync ) ) ;
2121
22- internal static Task < int > ExecuteSqlAsync ( this IDbCommand dbCmd , string sql , IEnumerable < IDbDataParameter > sqlParams , CancellationToken token )
22+ internal static Task < int > ExecuteSqlAsync ( this IDbCommand dbCmd , string sql , IEnumerable < IDbDataParameter > sqlParams , CancellationToken token ) =>
23+ dbCmd . SetParameters ( sqlParams ) . ExecuteSqlAsync ( sql , ( Action < IDbCommand > ) null , token ) ;
24+
25+ internal static Task < int > ExecuteSqlAsync ( this IDbCommand dbCmd , string sql , IEnumerable < IDbDataParameter > sqlParams ,
26+ Action < IDbCommand > commandFilter , CancellationToken token )
2327 {
24- return dbCmd . SetParameters ( sqlParams ) . ExecuteSqlAsync ( sql , token ) ;
28+ return dbCmd . SetParameters ( sqlParams ) . ExecuteSqlAsync ( sql , commandFilter , token ) ;
2529 }
2630
27- internal static Task < int > ExecuteSqlAsync ( this IDbCommand dbCmd , string sql , CancellationToken token )
31+ internal static Task < int > ExecuteSqlAsync ( this IDbCommand dbCmd , string sql , CancellationToken token ) =>
32+ dbCmd . ExecuteSqlAsync ( sql , ( Action < IDbCommand > ) null , token ) ;
33+
34+ internal static Task < int > ExecuteSqlAsync ( this IDbCommand dbCmd , string sql ,
35+ Action < IDbCommand > commandFilter , CancellationToken token )
2836 {
2937 dbCmd . CommandText = sql ;
3038
39+ commandFilter ? . Invoke ( dbCmd ) ;
40+
3141 if ( Log . IsDebugEnabled )
3242 Log . DebugCommand ( dbCmd ) ;
3343
@@ -39,13 +49,19 @@ internal static Task<int> ExecuteSqlAsync(this IDbCommand dbCmd, string sql, Can
3949 return dbCmd . GetDialectProvider ( ) . ExecuteNonQueryAsync ( dbCmd , token ) ;
4050 }
4151
42- internal static Task < int > ExecuteSqlAsync ( this IDbCommand dbCmd , string sql , object anonType , CancellationToken token )
52+ internal static Task < int > ExecuteSqlAsync ( this IDbCommand dbCmd , string sql , object anonType , CancellationToken token ) =>
53+ dbCmd . ExecuteSqlAsync ( sql , anonType , null , token ) ;
54+
55+ internal static Task < int > ExecuteSqlAsync ( this IDbCommand dbCmd , string sql , object anonType ,
56+ Action < IDbCommand > commandFilter , CancellationToken token )
4357 {
4458 if ( anonType != null )
4559 dbCmd . SetParameters ( anonType . ToObjectDictionary ( ) , excludeDefaults : false , sql : ref sql ) ;
4660
4761 dbCmd . CommandText = sql ;
4862
63+ commandFilter ? . Invoke ( dbCmd ) ;
64+
4965 if ( Log . IsDebugEnabled )
5066 Log . DebugCommand ( dbCmd ) ;
5167
@@ -122,7 +138,8 @@ internal static async Task<int> UpdateAllAsync<T>(this IDbCommand dbCmd, IEnumer
122138 OrmLiteConfig . UpdateFilter ? . Invoke ( dbCmd , obj ) ;
123139
124140 dialectProvider . SetParameterValues < T > ( dbCmd , obj ) ;
125- commandFilter ? . Invoke ( dbCmd ) ;
141+
142+ commandFilter ? . Invoke ( dbCmd ) ; //filters can augment SQL & only should be invoked once
126143 commandFilter = null ;
127144
128145 var rowsUpdated = await dbCmd . ExecNonQueryAsync ( token ) ;
@@ -192,10 +209,11 @@ internal static Task<int> DeleteNonDefaultsAsync<T>(this IDbCommand dbCmd, Cance
192209 if ( filters . Length == 0 )
193210 return TaskResult . Zero ;
194211
195- return DeleteAllAsync ( dbCmd , filters , o => o . AllFieldsMap < T > ( ) . NonDefaultsOnly ( ) , token ) ;
212+ return DeleteAllAsync ( dbCmd , filters , o => o . AllFieldsMap < T > ( ) . NonDefaultsOnly ( ) , token : token ) ;
196213 }
197214
198- private static async Task < int > DeleteAllAsync < T > ( IDbCommand dbCmd , IEnumerable < T > objs , Func < object , Dictionary < string , object > > fieldValuesFn = null , CancellationToken token = default ( CancellationToken ) )
215+ private static async Task < int > DeleteAllAsync < T > ( IDbCommand dbCmd , IEnumerable < T > objs , Func < object , Dictionary < string , object > > fieldValuesFn = null ,
216+ Action < IDbCommand > commandFilter = null , CancellationToken token = default )
199217 {
200218 OrmLiteUtils . AssertNotAnonType < T > ( ) ;
201219
@@ -218,6 +236,9 @@ private static async Task<int> DeleteAllAsync<T>(IDbCommand dbCmd, IEnumerable<T
218236 dialectProvider . PrepareParameterizedDeleteStatement < T > ( dbCmd , fieldValues ) ;
219237
220238 dialectProvider . SetParameterValues < T > ( dbCmd , obj ) ;
239+
240+ commandFilter ? . Invoke ( dbCmd ) ; //filters can augment SQL & only should be invoked once
241+ commandFilter = null ;
221242
222243 var rowsAffected = await dbCmd . ExecNonQueryAsync ( token ) ;
223244 count += rowsAffected ;
@@ -228,34 +249,37 @@ private static async Task<int> DeleteAllAsync<T>(IDbCommand dbCmd, IEnumerable<T
228249 return count ;
229250 }
230251
231- internal static Task < int > DeleteByIdAsync < T > ( this IDbCommand dbCmd , object id , CancellationToken token )
252+ internal static Task < int > DeleteByIdAsync < T > ( this IDbCommand dbCmd , object id ,
253+ Action < IDbCommand > commandFilter , CancellationToken token )
232254 {
233255 OrmLiteUtils . AssertNotAnonType < T > ( ) ;
234256
235257 var sql = dbCmd . DeleteByIdSql < T > ( id ) ;
236- return dbCmd . ExecuteSqlAsync ( sql , token ) ;
258+ return dbCmd . ExecuteSqlAsync ( sql , commandFilter , token ) ;
237259 }
238260
239- internal static async Task DeleteByIdAsync < T > ( this IDbCommand dbCmd , object id , ulong rowVersion , CancellationToken token )
261+ internal static async Task DeleteByIdAsync < T > ( this IDbCommand dbCmd , object id , ulong rowVersion ,
262+ Action < IDbCommand > commandFilter , CancellationToken token )
240263 {
241264 OrmLiteUtils . AssertNotAnonType < T > ( ) ;
242265
243266 var sql = dbCmd . DeleteByIdSql < T > ( id , rowVersion ) ;
244267
245- var rowsAffected = await dbCmd . ExecuteSqlAsync ( sql , token ) ;
268+ var rowsAffected = await dbCmd . ExecuteSqlAsync ( sql , commandFilter , token ) ;
246269 if ( rowsAffected == 0 )
247270 throw new OptimisticConcurrencyException ( "The row was modified or deleted since the last read" ) ;
248271 }
249272
250- internal static Task < int > DeleteByIdsAsync < T > ( this IDbCommand dbCmd , IEnumerable idValues , CancellationToken token )
273+ internal static Task < int > DeleteByIdsAsync < T > ( this IDbCommand dbCmd , IEnumerable idValues ,
274+ Action < IDbCommand > commandFilter , CancellationToken token )
251275 {
252276 var sqlIn = dbCmd . SetIdsInSqlParams ( idValues ) ;
253277 if ( string . IsNullOrEmpty ( sqlIn ) )
254278 return TaskResult . Zero ;
255279
256280 var sql = OrmLiteWriteCommandExtensions . GetDeleteByIdsSql < T > ( sqlIn , dbCmd . GetDialectProvider ( ) ) ;
257281
258- return dbCmd . ExecuteSqlAsync ( sql , token ) ;
282+ return dbCmd . ExecuteSqlAsync ( sql , commandFilter , token ) ;
259283 }
260284
261285 internal static Task < int > DeleteAllAsync < T > ( this IDbCommand dbCmd , CancellationToken token )
@@ -405,8 +429,6 @@ internal static async Task InsertAllAsync<T>(this IDbCommand dbCmd, IEnumerable<
405429
406430 dialectProvider . PrepareParameterizedInsertStatement < T > ( dbCmd ) ;
407431
408- commandFilter ? . Invoke ( dbCmd ) ;
409-
410432 using ( dbTrans )
411433 {
412434 foreach ( var obj in objs )
@@ -415,6 +437,9 @@ internal static async Task InsertAllAsync<T>(this IDbCommand dbCmd, IEnumerable<
415437
416438 dialectProvider . SetParameterValues < T > ( dbCmd , obj ) ;
417439
440+ commandFilter ? . Invoke ( dbCmd ) ; //filters can augment SQL & only should be invoked once
441+ commandFilter = null ;
442+
418443 await dbCmd . ExecNonQueryAsync ( token ) ;
419444 }
420445 dbTrans ? . Commit ( ) ;
0 commit comments