@@ -19,15 +19,25 @@ internal static class OrmLiteWriteCommandExtensionsAsync
19
19
{
20
20
internal static ILog Log = LogManager . GetLogger ( typeof ( OrmLiteWriteCommandExtensionsAsync ) ) ;
21
21
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 )
23
27
{
24
- return dbCmd . SetParameters ( sqlParams ) . ExecuteSqlAsync ( sql , token ) ;
28
+ return dbCmd . SetParameters ( sqlParams ) . ExecuteSqlAsync ( sql , commandFilter , token ) ;
25
29
}
26
30
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 )
28
36
{
29
37
dbCmd . CommandText = sql ;
30
38
39
+ commandFilter ? . Invoke ( dbCmd ) ;
40
+
31
41
if ( Log . IsDebugEnabled )
32
42
Log . DebugCommand ( dbCmd ) ;
33
43
@@ -39,13 +49,19 @@ internal static Task<int> ExecuteSqlAsync(this IDbCommand dbCmd, string sql, Can
39
49
return dbCmd . GetDialectProvider ( ) . ExecuteNonQueryAsync ( dbCmd , token ) ;
40
50
}
41
51
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 )
43
57
{
44
58
if ( anonType != null )
45
59
dbCmd . SetParameters ( anonType . ToObjectDictionary ( ) , excludeDefaults : false , sql : ref sql ) ;
46
60
47
61
dbCmd . CommandText = sql ;
48
62
63
+ commandFilter ? . Invoke ( dbCmd ) ;
64
+
49
65
if ( Log . IsDebugEnabled )
50
66
Log . DebugCommand ( dbCmd ) ;
51
67
@@ -122,7 +138,8 @@ internal static async Task<int> UpdateAllAsync<T>(this IDbCommand dbCmd, IEnumer
122
138
OrmLiteConfig . UpdateFilter ? . Invoke ( dbCmd , obj ) ;
123
139
124
140
dialectProvider . SetParameterValues < T > ( dbCmd , obj ) ;
125
- commandFilter ? . Invoke ( dbCmd ) ;
141
+
142
+ commandFilter ? . Invoke ( dbCmd ) ; //filters can augment SQL & only should be invoked once
126
143
commandFilter = null ;
127
144
128
145
var rowsUpdated = await dbCmd . ExecNonQueryAsync ( token ) ;
@@ -192,10 +209,11 @@ internal static Task<int> DeleteNonDefaultsAsync<T>(this IDbCommand dbCmd, Cance
192
209
if ( filters . Length == 0 )
193
210
return TaskResult . Zero ;
194
211
195
- return DeleteAllAsync ( dbCmd , filters , o => o . AllFieldsMap < T > ( ) . NonDefaultsOnly ( ) , token ) ;
212
+ return DeleteAllAsync ( dbCmd , filters , o => o . AllFieldsMap < T > ( ) . NonDefaultsOnly ( ) , token : token ) ;
196
213
}
197
214
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 )
199
217
{
200
218
OrmLiteUtils . AssertNotAnonType < T > ( ) ;
201
219
@@ -218,6 +236,9 @@ private static async Task<int> DeleteAllAsync<T>(IDbCommand dbCmd, IEnumerable<T
218
236
dialectProvider . PrepareParameterizedDeleteStatement < T > ( dbCmd , fieldValues ) ;
219
237
220
238
dialectProvider . SetParameterValues < T > ( dbCmd , obj ) ;
239
+
240
+ commandFilter ? . Invoke ( dbCmd ) ; //filters can augment SQL & only should be invoked once
241
+ commandFilter = null ;
221
242
222
243
var rowsAffected = await dbCmd . ExecNonQueryAsync ( token ) ;
223
244
count += rowsAffected ;
@@ -228,34 +249,37 @@ private static async Task<int> DeleteAllAsync<T>(IDbCommand dbCmd, IEnumerable<T
228
249
return count ;
229
250
}
230
251
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 )
232
254
{
233
255
OrmLiteUtils . AssertNotAnonType < T > ( ) ;
234
256
235
257
var sql = dbCmd . DeleteByIdSql < T > ( id ) ;
236
- return dbCmd . ExecuteSqlAsync ( sql , token ) ;
258
+ return dbCmd . ExecuteSqlAsync ( sql , commandFilter , token ) ;
237
259
}
238
260
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 )
240
263
{
241
264
OrmLiteUtils . AssertNotAnonType < T > ( ) ;
242
265
243
266
var sql = dbCmd . DeleteByIdSql < T > ( id , rowVersion ) ;
244
267
245
- var rowsAffected = await dbCmd . ExecuteSqlAsync ( sql , token ) ;
268
+ var rowsAffected = await dbCmd . ExecuteSqlAsync ( sql , commandFilter , token ) ;
246
269
if ( rowsAffected == 0 )
247
270
throw new OptimisticConcurrencyException ( "The row was modified or deleted since the last read" ) ;
248
271
}
249
272
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 )
251
275
{
252
276
var sqlIn = dbCmd . SetIdsInSqlParams ( idValues ) ;
253
277
if ( string . IsNullOrEmpty ( sqlIn ) )
254
278
return TaskResult . Zero ;
255
279
256
280
var sql = OrmLiteWriteCommandExtensions . GetDeleteByIdsSql < T > ( sqlIn , dbCmd . GetDialectProvider ( ) ) ;
257
281
258
- return dbCmd . ExecuteSqlAsync ( sql , token ) ;
282
+ return dbCmd . ExecuteSqlAsync ( sql , commandFilter , token ) ;
259
283
}
260
284
261
285
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<
405
429
406
430
dialectProvider . PrepareParameterizedInsertStatement < T > ( dbCmd ) ;
407
431
408
- commandFilter ? . Invoke ( dbCmd ) ;
409
-
410
432
using ( dbTrans )
411
433
{
412
434
foreach ( var obj in objs )
@@ -415,6 +437,9 @@ internal static async Task InsertAllAsync<T>(this IDbCommand dbCmd, IEnumerable<
415
437
416
438
dialectProvider . SetParameterValues < T > ( dbCmd , obj ) ;
417
439
440
+ commandFilter ? . Invoke ( dbCmd ) ; //filters can augment SQL & only should be invoked once
441
+ commandFilter = null ;
442
+
418
443
await dbCmd . ExecNonQueryAsync ( token ) ;
419
444
}
420
445
dbTrans ? . Commit ( ) ;
0 commit comments