Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

Commit ac28db5

Browse files
Delete command filter (#647)
1 parent 2dc535d commit ac28db5

File tree

4 files changed

+34
-25
lines changed

4 files changed

+34
-25
lines changed

src/ServiceStack.OrmLite/Expressions/WriteExpressionCommandExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,17 +371,17 @@ internal static IDbCommand InitInsertOnly<T>(this IDbCommand dbCmd, Expression<F
371371
return dbCmd;
372372
}
373373

374-
public static int Delete<T>(this IDbCommand dbCmd, Expression<Func<T, bool>> where)
374+
public static int Delete<T>(this IDbCommand dbCmd, Expression<Func<T, bool>> where, Action<IDbCommand> commandFilter = null)
375375
{
376376
var ev = dbCmd.GetDialectProvider().SqlExpression<T>();
377377
ev.Where(where);
378-
return dbCmd.Delete(ev);
378+
return dbCmd.Delete(ev, commandFilter);
379379
}
380380

381-
public static int Delete<T>(this IDbCommand dbCmd, SqlExpression<T> where)
381+
public static int Delete<T>(this IDbCommand dbCmd, SqlExpression<T> where, Action<IDbCommand> commandFilter = null)
382382
{
383383
var sql = where.ToDeleteRowStatement();
384-
return dbCmd.ExecuteSql(sql, where.Params);
384+
return dbCmd.ExecuteSql(sql, where.Params, commandFilter);
385385
}
386386

387387
public static int DeleteWhere<T>(this IDbCommand dbCmd, string whereFilter, object[] whereParams)

src/ServiceStack.OrmLite/OrmLiteWriteApi.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ public static int UpdateAll<T>(this IDbConnection dbConn, IEnumerable<T> objs, A
213213
/// <para>db.Delete&lt;Person&gt;(new { FirstName = "Jimi", Age = 27 })</para>
214214
/// </summary>
215215
/// <returns>number of rows deleted</returns>
216-
public static int Delete<T>(this IDbConnection dbConn, object anonFilter)
216+
public static int Delete<T>(this IDbConnection dbConn, object anonFilter, Action<IDbCommand> commandFilter = null)
217217
{
218-
return dbConn.Exec(dbCmd => dbCmd.Delete<T>(anonFilter));
218+
return dbConn.Exec(dbCmd => dbCmd.Delete<T>(anonFilter, commandFilter));
219219
}
220220

221221
/// <summary>
@@ -233,9 +233,9 @@ public static int Delete<T>(this IDbConnection dbConn, Dictionary<string, object
233233
/// <para>db.Delete(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 })</para>
234234
/// </summary>
235235
/// <returns>number of rows deleted</returns>
236-
public static int Delete<T>(this IDbConnection dbConn, T allFieldsFilter)
236+
public static int Delete<T>(this IDbConnection dbConn, T allFieldsFilter, Action<IDbCommand> commandFilter = null)
237237
{
238-
return dbConn.Exec(dbCmd => dbCmd.Delete(allFieldsFilter));
238+
return dbConn.Exec(dbCmd => dbCmd.Delete(allFieldsFilter, commandFilter));
239239
}
240240

241241
/// <summary>
@@ -273,9 +273,9 @@ public static int DeleteNonDefaults<T>(this IDbConnection dbConn, params T[] non
273273
/// <para>db.DeleteById&lt;Person&gt;(1)</para>
274274
/// </summary>
275275
/// <returns>number of rows deleted</returns>
276-
public static int DeleteById<T>(this IDbConnection dbConn, object id)
276+
public static int DeleteById<T>(this IDbConnection dbConn, object id, Action<IDbCommand> commandFilter = null)
277277
{
278-
return dbConn.Exec(dbCmd => dbCmd.DeleteById<T>(id));
278+
return dbConn.Exec(dbCmd => dbCmd.DeleteById<T>(id, commandFilter));
279279
}
280280

281281
/// <summary>
@@ -284,9 +284,9 @@ public static int DeleteById<T>(this IDbConnection dbConn, object id)
284284
/// row does not exist or has a different row version.
285285
/// E.g: <para>db.DeleteById&lt;Person&gt;(1)</para>
286286
/// </summary>
287-
public static void DeleteById<T>(this IDbConnection dbConn, object id, ulong rowVersion)
287+
public static void DeleteById<T>(this IDbConnection dbConn, object id, ulong rowVersion, Action<IDbCommand> commandFilter = null)
288288
{
289-
dbConn.Exec(dbCmd => dbCmd.DeleteById<T>(id, rowVersion));
289+
dbConn.Exec(dbCmd => dbCmd.DeleteById<T>(id, rowVersion, commandFilter));
290290
}
291291

292292
/// <summary>

src/ServiceStack.OrmLite/OrmLiteWriteCommandExtensions.cs

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,14 @@ internal static string LastSql(this IDbCommand dbCmd)
257257
return dbCmd.CommandText;
258258
}
259259

260-
internal static int ExecuteSql(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams = null)
260+
internal static int ExecuteSql(this IDbCommand dbCmd, string sql, IEnumerable<IDbDataParameter> sqlParams = null, Action<IDbCommand> commandFilter = null)
261261
{
262262
dbCmd.CommandText = sql;
263263

264264
dbCmd.SetParameters(sqlParams);
265265

266+
commandFilter?.Invoke(dbCmd);
267+
266268
if (Log.IsDebugEnabled)
267269
Log.DebugCommand(dbCmd);
268270

@@ -274,13 +276,15 @@ internal static int ExecuteSql(this IDbCommand dbCmd, string sql, IEnumerable<ID
274276
return dbCmd.ExecuteNonQuery();
275277
}
276278

277-
internal static int ExecuteSql(this IDbCommand dbCmd, string sql, object anonType)
279+
internal static int ExecuteSql(this IDbCommand dbCmd, string sql, object anonType, Action<IDbCommand> commandFilter = null)
278280
{
279281
if (anonType != null)
280282
dbCmd.SetParameters(anonType.ToObjectDictionary(), excludeDefaults: false, sql:ref sql);
281283

282284
dbCmd.CommandText = sql;
283285

286+
commandFilter?.Invoke(dbCmd);
287+
284288
if (Log.IsDebugEnabled)
285289
Log.DebugCommand(dbCmd);
286290

@@ -510,12 +514,12 @@ private static int AssertRowsUpdated(IDbCommand dbCmd, bool hadRowVersion)
510514
return rowsUpdated;
511515
}
512516

513-
internal static int Delete<T>(this IDbCommand dbCmd, T anonType)
517+
internal static int Delete<T>(this IDbCommand dbCmd, T anonType, Action<IDbCommand> commandFilter = null)
514518
{
515-
return dbCmd.Delete<T>((object)anonType);
519+
return dbCmd.Delete<T>((object)anonType, commandFilter);
516520
}
517521

518-
internal static int Delete<T>(this IDbCommand dbCmd, object anonType)
522+
internal static int Delete<T>(this IDbCommand dbCmd, object anonType, Action<IDbCommand> commandFilter = null)
519523
{
520524
var dialectProvider = dbCmd.GetDialectProvider();
521525

@@ -524,6 +528,8 @@ internal static int Delete<T>(this IDbCommand dbCmd, object anonType)
524528

525529
dialectProvider.SetParameterValues<T>(dbCmd, anonType);
526530

531+
commandFilter?.Invoke(dbCmd);
532+
527533
return AssertRowsUpdated(dbCmd, hadRowVersion);
528534
}
529535

@@ -553,7 +559,7 @@ internal static int DeleteNonDefaults<T>(this IDbCommand dbCmd, T[] filters)
553559
return DeleteAll(dbCmd, filters, o => o.AllFieldsMap<T>().NonDefaultsOnly());
554560
}
555561

556-
private static int DeleteAll<T>(IDbCommand dbCmd, IEnumerable<T> objs, Func<object,Dictionary<string,object>> fieldValuesFn=null)
562+
private static int DeleteAll<T>(IDbCommand dbCmd, IEnumerable<T> objs, Func<object,Dictionary<string,object>> fieldValuesFn=null, Action<IDbCommand> commandFilter = null)
557563
{
558564
OrmLiteUtils.AssertNotAnonType<T>();
559565

@@ -577,6 +583,9 @@ private static int DeleteAll<T>(IDbCommand dbCmd, IEnumerable<T> objs, Func<obje
577583

578584
dialectProvider.SetParameterValues<T>(dbCmd, obj);
579585

586+
commandFilter?.Invoke(dbCmd);
587+
commandFilter = null;
588+
580589
count += dbCmd.ExecNonQuery();
581590
}
582591

@@ -590,11 +599,11 @@ private static int DeleteAll<T>(IDbCommand dbCmd, IEnumerable<T> objs, Func<obje
590599
return count;
591600
}
592601

593-
internal static int DeleteById<T>(this IDbCommand dbCmd, object id)
602+
internal static int DeleteById<T>(this IDbCommand dbCmd, object id, Action<IDbCommand> commandFilter = null)
594603
{
595604
var sql = DeleteByIdSql<T>(dbCmd, id);
596605

597-
return dbCmd.ExecuteSql(sql);
606+
return dbCmd.ExecuteSql(sql, commandFilter: commandFilter);
598607
}
599608

600609
internal static string DeleteByIdSql<T>(this IDbCommand dbCmd, object id)
@@ -613,11 +622,11 @@ internal static string DeleteByIdSql<T>(this IDbCommand dbCmd, object id)
613622
return sql;
614623
}
615624

616-
internal static void DeleteById<T>(this IDbCommand dbCmd, object id, ulong rowVersion)
625+
internal static void DeleteById<T>(this IDbCommand dbCmd, object id, ulong rowVersion, Action<IDbCommand> commandFilter = null)
617626
{
618627
var sql = DeleteByIdSql<T>(dbCmd, id, rowVersion);
619628

620-
var rowsAffected = dbCmd.ExecuteSql(sql);
629+
var rowsAffected = dbCmd.ExecuteSql(sql, commandFilter: commandFilter);
621630
if (rowsAffected == 0)
622631
throw new OptimisticConcurrencyException("The row was modified or deleted since the last read");
623632
}

src/ServiceStack.OrmLite/OrmLiteWriteExpressionsApi.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,9 @@ public static long InsertOnly<T>(this IDbConnection dbConn, Expression<Func<T>>
248248
/// db.Delete&lt;Person&gt;(p => p.Age == 27);
249249
/// DELETE FROM "Person" WHERE ("Age" = 27)
250250
/// </summary>
251-
public static int Delete<T>(this IDbConnection dbConn, Expression<Func<T, bool>> where)
251+
public static int Delete<T>(this IDbConnection dbConn, Expression<Func<T, bool>> where, Action<IDbCommand> commandFilter = null)
252252
{
253-
return dbConn.Exec(dbCmd => dbCmd.Delete(where));
253+
return dbConn.Exec(dbCmd => dbCmd.Delete(where, commandFilter));
254254
}
255255

256256
/// <summary>
@@ -260,7 +260,7 @@ public static int Delete<T>(this IDbConnection dbConn, Expression<Func<T, bool>>
260260
/// db.Delete&lt;Person&gt;(q.Where(p => p.Age == 27));
261261
/// DELETE FROM "Person" WHERE ("Age" = 27)
262262
/// </summary>
263-
public static int Delete<T>(this IDbConnection dbConn, SqlExpression<T> where)
263+
public static int Delete<T>(this IDbConnection dbConn, SqlExpression<T> where, Action<IDbCommand> commandFilter = null)
264264
{
265265
return dbConn.Exec(dbCmd => dbCmd.Delete(where));
266266
}

0 commit comments

Comments
 (0)