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

Commit d819d06

Browse files
committed
Execute filters after initializing CommandText
1 parent a5e3321 commit d819d06

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

src/ServiceStack.OrmLite/Async/OrmLiteWriteCommandExtensionsAsync.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,13 @@ internal static Task<int> ExecuteSqlAsync(this IDbCommand dbCmd, string sql, obj
5858

5959
internal static async Task<int> UpdateAsync<T>(this IDbCommand dbCmd, T obj, CancellationToken token, Action<IDbCommand> commandFilter)
6060
{
61-
OrmLiteConfig.UpdateFilter?.Invoke(dbCmd, obj);
62-
6361
var dialectProvider = dbCmd.GetDialectProvider();
6462
var hadRowVersion = dialectProvider.PrepareParameterizedUpdateStatement<T>(dbCmd);
6563
if (string.IsNullOrEmpty(dbCmd.CommandText))
6664
return 0;
6765

6866
dialectProvider.SetParameterValues<T>(dbCmd, obj);
67+
OrmLiteConfig.UpdateFilter?.Invoke(dbCmd, obj);
6968
commandFilter?.Invoke(dbCmd);
7069

7170
var rowsUpdated = await dialectProvider.ExecuteNonQueryAsync(dbCmd, token);
@@ -251,15 +250,14 @@ internal static Task<int> DeleteAsync(this IDbCommand dbCmd, Type tableType, str
251250

252251
internal static async Task<long> InsertAsync<T>(this IDbCommand dbCmd, T obj, Action<IDbCommand> commandFilter, bool selectIdentity, CancellationToken token)
253252
{
254-
OrmLiteConfig.InsertFilter?.Invoke(dbCmd, obj);
255-
256253
var dialectProvider = dbCmd.GetDialectProvider();
257254

258255
dialectProvider.PrepareParameterizedInsertStatement<T>(dbCmd,
259256
insertFields: dialectProvider.GetNonDefaultValueInsertFields(obj));
260257

261258
dialectProvider.SetParameterValues<T>(dbCmd, obj);
262259

260+
OrmLiteConfig.InsertFilter?.Invoke(dbCmd, obj);
263261
commandFilter?.Invoke(dbCmd);
264262

265263
if (dialectProvider.HasInsertReturnValues(ModelDefinition<T>.Definition))
@@ -415,8 +413,6 @@ internal static async Task<int> SaveAllAsync<T>(this IDbCommand dbCmd, IEnumerab
415413
var id = modelDef.GetPrimaryKey(row);
416414
if (id != defaultIdValue && existingRowsMap.ContainsKey(id))
417415
{
418-
OrmLiteConfig.UpdateFilter?.Invoke(dbCmd, row);
419-
420416
await dbCmd.UpdateAsync(row, token, null);
421417
}
422418
else
@@ -430,8 +426,6 @@ internal static async Task<int> SaveAllAsync<T>(this IDbCommand dbCmd, IEnumerab
430426
}
431427
else
432428
{
433-
OrmLiteConfig.InsertFilter?.Invoke(dbCmd, row);
434-
435429
await dbCmd.InsertAsync(row, commandFilter:null, selectIdentity:false, token:token);
436430
}
437431

src/ServiceStack.OrmLite/OrmLiteWriteCommandExtensions.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,14 @@ internal static object[] PopulateValues(this IDataReader reader, object[] values
410410

411411
internal static int Update<T>(this IDbCommand dbCmd, T obj, Action<IDbCommand> commandFilter = null)
412412
{
413-
OrmLiteConfig.UpdateFilter?.Invoke(dbCmd, obj);
414-
415413
var dialectProvider = dbCmd.GetDialectProvider();
416414
var hadRowVersion = dialectProvider.PrepareParameterizedUpdateStatement<T>(dbCmd);
417415
if (string.IsNullOrEmpty(dbCmd.CommandText))
418416
return 0;
419417

420418
dialectProvider.SetParameterValues<T>(dbCmd, obj);
421419

420+
OrmLiteConfig.UpdateFilter?.Invoke(dbCmd, obj);
422421
commandFilter?.Invoke(dbCmd);
423422
var rowsUpdated = dbCmd.ExecNonQuery();
424423

@@ -675,14 +674,13 @@ internal static int Delete(this IDbCommand dbCmd, Type tableType, string sql, ob
675674

676675
internal static long Insert<T>(this IDbCommand dbCmd, T obj, Action<IDbCommand> commandFilter, bool selectIdentity = false)
677676
{
678-
OrmLiteConfig.InsertFilter?.Invoke(dbCmd, obj);
679-
680677
var dialectProvider = dbCmd.GetDialectProvider();
681678
dialectProvider.PrepareParameterizedInsertStatement<T>(dbCmd,
682679
insertFields: dialectProvider.GetNonDefaultValueInsertFields(obj));
683680

684681
dialectProvider.SetParameterValues<T>(dbCmd, obj);
685682

683+
OrmLiteConfig.InsertFilter?.Invoke(dbCmd, obj);
686684
commandFilter?.Invoke(dbCmd); //dbCmd.OnConflictInsert() needs to be applied before last insert id
687685

688686
if (dialectProvider.HasInsertReturnValues(ModelDefinition<T>.Definition))
@@ -910,7 +908,6 @@ internal static int SaveAll<T>(this IDbCommand dbCmd, IEnumerable<T> objs)
910908
var id = modelDef.GetPrimaryKey(row);
911909
if (id != defaultIdValue && existingRowsMap.ContainsKey(id))
912910
{
913-
OrmLiteConfig.UpdateFilter?.Invoke(dbCmd, row);
914911
dbCmd.Update(row);
915912
}
916913
else
@@ -925,7 +922,6 @@ internal static int SaveAll<T>(this IDbCommand dbCmd, IEnumerable<T> objs)
925922
}
926923
else
927924
{
928-
OrmLiteConfig.InsertFilter?.Invoke(dbCmd, row);
929925
dbCmd.Insert(row, commandFilter: null);
930926
}
931927

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using NUnit.Framework;
4+
using ServiceStack.Common.Tests.Models;
5+
using ServiceStack.Text;
6+
7+
namespace ServiceStack.OrmLite.Tests
8+
{
9+
public class FilterTests : OrmLiteTestBase
10+
{
11+
[Test]
12+
public async Task Does_fire_correct_filters()
13+
{
14+
var sbInsert = new List<string>();
15+
var sbUpdate = new List<string>();
16+
OrmLiteConfig.InsertFilter = (cmd, o) => sbInsert.Add(cmd.CommandText);
17+
OrmLiteConfig.UpdateFilter = (cmd, o) => sbUpdate.Add(cmd.CommandText);
18+
19+
using (var db = OpenDbConnection())
20+
{
21+
db.DropAndCreateTable<Poco>();
22+
await db.SaveAllAsync(new[] {
23+
new Poco {Id = 1, Name = "A1"},
24+
new Poco {Id = 2, Name = "B1"},
25+
});
26+
27+
Assert.That(sbInsert[0], Does.StartWith("INSERT"));
28+
Assert.That(sbInsert[1], Does.StartWith("INSERT"));
29+
30+
await db.SaveAllAsync(new[] {
31+
new Poco {Id = 1, Name = "A2"},
32+
new Poco {Id = 2, Name = "B2"},
33+
});
34+
35+
Assert.That(sbUpdate[0], Does.StartWith("UPDATE"));
36+
Assert.That(sbUpdate[1], Does.StartWith("UPDATE"));
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)