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

Commit aa4107a

Browse files
committed
Add commandFilter to Insert, InsertAll and Update APIs
1 parent 82636e0 commit aa4107a

File tree

6 files changed

+137
-45
lines changed

6 files changed

+137
-45
lines changed

src/ServiceStack.OrmLite/Async/OrmLiteWriteCommandExtensionsAsync.cs

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, T obj, Cancellat
7373
});
7474
}
7575

76-
internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, CancellationToken token, Action<IDbCommand> commandFilter, T[] objs)
76+
internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, Action<IDbCommand> commandFilter, CancellationToken token, T[] objs)
7777
{
78-
return dbCmd.UpdateAllAsync(objs, token, commandFilter);
78+
return dbCmd.UpdateAllAsync(objs, commandFilter, token);
7979
}
8080

81-
internal static Task<int> UpdateAllAsync<T>(this IDbCommand dbCmd, IEnumerable<T> objs, CancellationToken token, Action<IDbCommand> commandFilter)
81+
internal static Task<int> UpdateAllAsync<T>(this IDbCommand dbCmd, IEnumerable<T> objs, Action<IDbCommand> commandFilter, CancellationToken token)
8282
{
8383
IDbTransaction dbTrans = null;
8484

@@ -262,7 +262,7 @@ internal static Task<int> DeleteAsync(this IDbCommand dbCmd, Type tableType, str
262262
return dbCmd.ExecuteSqlAsync(dbCmd.GetDialectProvider().ToDeleteStatement(tableType, sql), token);
263263
}
264264

265-
internal static Task<long> InsertAsync<T>(this IDbCommand dbCmd, T obj, bool selectIdentity, CancellationToken token)
265+
internal static Task<long> InsertAsync<T>(this IDbCommand dbCmd, T obj, Action<IDbCommand> commandFilter, bool selectIdentity, CancellationToken token)
266266
{
267267
OrmLiteConfig.InsertFilter?.Invoke(dbCmd, obj);
268268

@@ -273,18 +273,20 @@ internal static Task<long> InsertAsync<T>(this IDbCommand dbCmd, T obj, bool sel
273273

274274
dialectProvider.SetParameterValues<T>(dbCmd, obj);
275275

276+
commandFilter?.Invoke(dbCmd);
277+
276278
if (selectIdentity)
277279
return dialectProvider.InsertAndGetLastInsertIdAsync<T>(dbCmd, token);
278280

279281
return dbCmd.ExecNonQueryAsync(token).Then(i => (long)i);
280282
}
281283

282-
internal static Task InsertAsync<T>(this IDbCommand dbCmd, CancellationToken token, params T[] objs)
284+
internal static Task InsertAsync<T>(this IDbCommand dbCmd, Action<IDbCommand> commandFilter, CancellationToken token, params T[] objs)
283285
{
284-
return InsertAllAsync(dbCmd, objs, token);
286+
return InsertAllAsync(dbCmd, objs, commandFilter, token);
285287
}
286288

287-
internal static Task InsertAllAsync<T>(this IDbCommand dbCmd, IEnumerable<T> objs, CancellationToken token)
289+
internal static Task InsertAllAsync<T>(this IDbCommand dbCmd, IEnumerable<T> objs, Action<IDbCommand> commandFilter, CancellationToken token)
288290
{
289291
IDbTransaction dbTrans = null;
290292

@@ -295,6 +297,8 @@ internal static Task InsertAllAsync<T>(this IDbCommand dbCmd, IEnumerable<T> obj
295297

296298
dialectProvider.PrepareParameterizedInsertStatement<T>(dbCmd);
297299

300+
commandFilter?.Invoke(dbCmd);
301+
298302
return objs.EachAsync((obj, i) =>
299303
{
300304
OrmLiteConfig.InsertFilter?.Invoke(dbCmd, obj);
@@ -332,14 +336,14 @@ internal static async Task<bool> SaveAsync<T>(this IDbCommand dbCmd, T obj, Canc
332336
if (modelDef.HasAutoIncrementId)
333337
{
334338

335-
var newId = await dbCmd.InsertAsync(obj, selectIdentity: true, token:token);
339+
var newId = await dbCmd.InsertAsync(obj, commandFilter: null, selectIdentity: true, token:token);
336340
var safeId = dbCmd.GetDialectProvider().FromDbValue(newId, modelDef.PrimaryKey.FieldType);
337341
modelDef.PrimaryKey.SetValueFn(obj, safeId);
338342
id = newId;
339343
}
340344
else
341345
{
342-
await dbCmd.InsertAsync(token, obj);
346+
await dbCmd.InsertAsync(obj, commandFilter:null, selectIdentity:false, token:token);
343347
}
344348

345349
modelDef.RowVersion?.SetValueFn(obj, await dbCmd.GetRowVersionAsync(modelDef, id, token));
@@ -396,7 +400,7 @@ internal static async Task<int> SaveAllAsync<T>(this IDbCommand dbCmd, IEnumerab
396400
{
397401
if (modelDef.HasAutoIncrementId)
398402
{
399-
var newId = await dbCmd.InsertAsync(row, selectIdentity: true, token:token);
403+
var newId = await dbCmd.InsertAsync(row, commandFilter:null, selectIdentity:true, token:token);
400404
var safeId = dialectProvider.FromDbValue(newId, modelDef.PrimaryKey.FieldType);
401405
modelDef.PrimaryKey.SetValueFn(row, safeId);
402406
id = newId;
@@ -405,7 +409,7 @@ internal static async Task<int> SaveAllAsync<T>(this IDbCommand dbCmd, IEnumerab
405409
{
406410
OrmLiteConfig.InsertFilter?.Invoke(dbCmd, row);
407411

408-
await dbCmd.InsertAsync(token, row);
412+
await dbCmd.InsertAsync(row, commandFilter:null, selectIdentity:false, token:token);
409413
}
410414

411415
rowsAdded++;

src/ServiceStack.OrmLite/IUntypedApi.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections;
23
using System.Data;
34
using System.Threading;
@@ -16,9 +17,12 @@ public interface IUntypedApi
1617
Task<bool> SaveAsync(object obj, CancellationToken token);
1718

1819
void InsertAll(IEnumerable objs);
20+
void InsertAll(IEnumerable objs, Action<IDbCommand> commandFilter);
1921
long Insert(object obj, bool selectIdentity = false);
22+
long Insert(object obj, Action<IDbCommand> commandFilter, bool selectIdentity = false);
2023

2124
int UpdateAll(IEnumerable objs);
25+
int UpdateAll(IEnumerable objs, Action<IDbCommand> commandFilter);
2226
int Update(object obj);
2327

2428
int DeleteAll();

src/ServiceStack.OrmLite/OrmLiteWriteApi.cs

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ public static class OrmLiteWriteApi
1616
/// </summary>
1717
public static string GetLastSql(this IDbConnection dbConn)
1818
{
19-
var ormLiteConn = dbConn as OrmLiteConnection;
20-
return ormLiteConn != null ? ormLiteConn.LastCommandText : null;
19+
return dbConn is OrmLiteConnection ormLiteConn ? ormLiteConn.LastCommandText : null;
2120
}
2221

2322
public static string GetLastSqlAndParams(this IDbCommand dbCmd)
@@ -70,7 +69,16 @@ public static int ExecuteSql(this IDbConnection dbConn, string sql, Dictionary<s
7069
/// </summary>
7170
public static long Insert<T>(this IDbConnection dbConn, T obj, bool selectIdentity = false)
7271
{
73-
return dbConn.Exec(dbCmd => dbCmd.Insert(obj, selectIdentity));
72+
return dbConn.Exec(dbCmd => dbCmd.Insert(obj, commandFilter: null, selectIdentity: selectIdentity));
73+
}
74+
75+
/// <summary>
76+
/// Insert 1 POCO and modify populated IDbCommand with a commandFilter. E.g:
77+
/// <para>var id = db.Insert(new Person { Id = 1, FirstName = "Jimi }, dbCmd => applyFilter(dbCmd))</para>
78+
/// </summary>
79+
public static long Insert<T>(this IDbConnection dbConn, T obj, Action<IDbCommand> commandFilter, bool selectIdentity = false)
80+
{
81+
return dbConn.Exec(dbCmd => dbCmd.Insert(obj, commandFilter: commandFilter, selectIdentity: selectIdentity));
7482
}
7583

7684
/// <summary>
@@ -89,7 +97,17 @@ public static void InsertUsingDefaults<T>(this IDbConnection dbConn, params T[]
8997
/// </summary>
9098
public static void InsertAll<T>(this IDbConnection dbConn, IEnumerable<T> objs)
9199
{
92-
dbConn.Exec(dbCmd => dbCmd.InsertAll(objs));
100+
dbConn.Exec(dbCmd => dbCmd.InsertAll(objs, commandFilter:null));
101+
}
102+
103+
/// <summary>
104+
/// Insert a collection of POCOs in a transaction and modify populated IDbCommand with a commandFilter. E.g:
105+
/// <para>db.InsertAll(new[] { new Person { Id = 9, FirstName = "Biggie", LastName = "Smalls", Age = 24 } },</para>
106+
/// <para> dbCmd => applyFilter(dbCmd))</para>
107+
/// </summary>
108+
public static void InsertAll<T>(this IDbConnection dbConn, IEnumerable<T> objs, Action<IDbCommand> commandFilter)
109+
{
110+
dbConn.Exec(dbCmd => dbCmd.InsertAll(objs, commandFilter: commandFilter));
93111
}
94112

95113
/// <summary>
@@ -99,7 +117,18 @@ public static void InsertAll<T>(this IDbConnection dbConn, IEnumerable<T> objs)
99117
/// </summary>
100118
public static void Insert<T>(this IDbConnection dbConn, params T[] objs)
101119
{
102-
dbConn.Exec(dbCmd => dbCmd.Insert(objs));
120+
dbConn.Exec(dbCmd => dbCmd.Insert(commandFilter: null, objs: objs));
121+
}
122+
123+
/// <summary>
124+
/// Insert 1 or more POCOs in a transaction and modify populated IDbCommand with a commandFilter. E.g:
125+
/// <para>db.Insert(dbCmd => applyFilter(dbCmd),</para>
126+
/// <para> new Person { Id = 1, FirstName = "Tupac", LastName = "Shakur", Age = 25 },</para>
127+
/// <para> new Person { Id = 2, FirstName = "Biggie", LastName = "Smalls", Age = 24 })</para>
128+
/// </summary>
129+
public static void Insert<T>(this IDbConnection dbConn, Action<IDbCommand> commandFilter, params T[] objs)
130+
{
131+
dbConn.Exec(dbCmd => dbCmd.Insert(commandFilter: commandFilter, objs: objs));
103132
}
104133

105134
/// <summary>
@@ -118,7 +147,7 @@ public static int Update<T>(this IDbConnection dbConn, T obj, Action<IDbCommand>
118147
/// </summary>
119148
public static int Update<T>(this IDbConnection dbConn, params T[] objs)
120149
{
121-
return dbConn.Exec(dbCmd => dbCmd.Update(objs));
150+
return dbConn.Exec(dbCmd => dbCmd.Update(objs, commandFilter:null));
122151
}
123152
public static int Update<T>(this IDbConnection dbConn, Action<IDbCommand> commandFilter, params T[] objs)
124153
{

src/ServiceStack.OrmLite/OrmLiteWriteApiAsync.cs

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,30 +39,60 @@ public static class OrmLiteWriteApiAsync
3939
/// </summary>
4040
public static Task<long> InsertAsync<T>(this IDbConnection dbConn, T obj, bool selectIdentity = false, CancellationToken token = default(CancellationToken))
4141
{
42-
return dbConn.Exec(dbCmd => dbCmd.InsertAsync(obj, selectIdentity, token));
42+
return dbConn.Exec(dbCmd => dbCmd.InsertAsync(obj, commandFilter: null, selectIdentity: selectIdentity, token: token));
43+
}
44+
45+
/// <summary>
46+
/// Insert 1 POCO, use selectIdentity to retrieve the last insert AutoIncrement id (if any). E.g:
47+
/// <para>var id = db.Insert(new Person { Id = 1, FirstName = "Jimi }, selectIdentity:true)</para>
48+
/// </summary>
49+
public static Task<long> InsertAsync<T>(this IDbConnection dbConn, T obj, Action<IDbCommand> commandFilter, bool selectIdentity = false, CancellationToken token = default(CancellationToken))
50+
{
51+
return dbConn.Exec(dbCmd => dbCmd.InsertAsync(obj, commandFilter: commandFilter, selectIdentity: selectIdentity, token: token));
4352
}
4453

4554
/// <summary>
4655
/// Insert 1 or more POCOs in a transaction. E.g:
47-
/// <para>db.Insert(new Person { Id = 1, FirstName = "Tupac", LastName = "Shakur", Age = 25 },</para>
48-
/// <para> new Person { Id = 2, FirstName = "Biggie", LastName = "Smalls", Age = 24 })</para>
56+
/// <para>db.InsertAsync(new Person { Id = 1, FirstName = "Tupac", LastName = "Shakur", Age = 25 },</para>
57+
/// <para> new Person { Id = 2, FirstName = "Biggie", LastName = "Smalls", Age = 24 })</para>
4958
/// </summary>
5059
public static Task InsertAsync<T>(this IDbConnection dbConn, CancellationToken token, params T[] objs)
5160
{
52-
return dbConn.Exec(dbCmd => dbCmd.InsertAsync(token, objs));
61+
return dbConn.Exec(dbCmd => dbCmd.InsertAsync(commandFilter:null, token:token, objs:objs));
5362
}
5463
public static Task InsertAsync<T>(this IDbConnection dbConn, params T[] objs)
5564
{
56-
return dbConn.Exec(dbCmd => dbCmd.InsertAsync(default(CancellationToken), objs));
65+
return dbConn.Exec(dbCmd => dbCmd.InsertAsync(commandFilter:null, token:default(CancellationToken), objs:objs));
66+
}
67+
68+
/// <summary>
69+
/// Insert 1 or more POCOs in a transaction and modify populated IDbCommand with a commandFilter. E.g:
70+
/// <para>db.InsertAsync(dbCmd => applyFilter(dbCmd), token, </para>
71+
/// <para> new Person { Id = 1, FirstName = "Tupac", LastName = "Shakur", Age = 25 },</para>
72+
/// <para> new Person { Id = 2, FirstName = "Biggie", LastName = "Smalls", Age = 24 })</para>
73+
/// </summary>
74+
public static Task InsertAsync<T>(this IDbConnection dbConn, Action<IDbCommand> commandFilter, CancellationToken token, params T[] objs)
75+
{
76+
return dbConn.Exec(dbCmd => dbCmd.InsertAsync(commandFilter:commandFilter, token:token, objs:objs));
5777
}
5878

5979
/// <summary>
6080
/// Insert a collection of POCOs in a transaction. E.g:
61-
/// <para>db.InsertAll(new[] { new Person { Id = 9, FirstName = "Biggie", LastName = "Smalls", Age = 24 } })</para>
81+
/// <para>db.InsertAllAsync(new[] { new Person { Id = 9, FirstName = "Biggie", LastName = "Smalls", Age = 24 } })</para>
6282
/// </summary>
6383
public static Task InsertAllAsync<T>(this IDbConnection dbConn, IEnumerable<T> objs, CancellationToken token = default(CancellationToken))
6484
{
65-
return dbConn.Exec(dbCmd => dbCmd.InsertAllAsync(objs, token));
85+
return dbConn.Exec(dbCmd => dbCmd.InsertAllAsync(objs, commandFilter:null, token:token));
86+
}
87+
88+
/// <summary>
89+
/// Insert a collection of POCOs in a transaction and modify populated IDbCommand with a commandFilter. E.g:
90+
/// <para>db.InsertAllAsync(new[] { new Person { Id = 9, FirstName = "Biggie", LastName = "Smalls", Age = 24 } },</para>
91+
/// <para> dbCmd => applyFilter(dbCmd))</para>
92+
/// </summary>
93+
public static Task InsertAllAsync<T>(this IDbConnection dbConn, IEnumerable<T> objs, Action<IDbCommand> commandFilter, CancellationToken token = default(CancellationToken))
94+
{
95+
return dbConn.Exec(dbCmd => dbCmd.InsertAllAsync(objs, commandFilter:commandFilter, token:token));
6696
}
6797

6898
/// <summary>
@@ -81,19 +111,19 @@ public static Task InsertAsync<T>(this IDbConnection dbConn, params T[] objs)
81111
/// </summary>
82112
public static Task<int> UpdateAsync<T>(this IDbConnection dbConn, CancellationToken token, params T[] objs)
83113
{
84-
return dbConn.Exec(dbCmd => dbCmd.UpdateAsync(token, null, objs));
114+
return dbConn.Exec(dbCmd => dbCmd.UpdateAsync(commandFilter:null, token: token, objs: objs));
85115
}
86116
public static Task<int> UpdateAsync<T>(this IDbConnection dbConn, params T[] objs)
87117
{
88-
return dbConn.Exec(dbCmd => dbCmd.UpdateAsync(default(CancellationToken), null, objs));
118+
return dbConn.Exec(dbCmd => dbCmd.UpdateAsync(commandFilter: null, token: default(CancellationToken), objs: objs));
89119
}
90120
public static Task<int> UpdateAsync<T>(this IDbConnection dbConn, Action<IDbCommand> commandFilter, CancellationToken token, params T[] objs)
91121
{
92-
return dbConn.Exec(dbCmd => dbCmd.UpdateAsync(token, commandFilter, objs));
122+
return dbConn.Exec(dbCmd => dbCmd.UpdateAsync(commandFilter: commandFilter, token: token, objs: objs));
93123
}
94124
public static Task<int> UpdateAsync<T>(this IDbConnection dbConn, Action<IDbCommand> commandFilter, params T[] objs)
95125
{
96-
return dbConn.Exec(dbCmd => dbCmd.UpdateAsync(default(CancellationToken), commandFilter, objs));
126+
return dbConn.Exec(dbCmd => dbCmd.UpdateAsync(commandFilter: commandFilter, token:default(CancellationToken), objs: objs));
97127
}
98128

99129
/// <summary>
@@ -102,11 +132,11 @@ public static Task<int> UpdateAsync<T>(this IDbConnection dbConn, Action<IDbComm
102132
/// </summary>
103133
public static Task<int> UpdateAllAsync<T>(this IDbConnection dbConn, IEnumerable<T> objs, Action<IDbCommand> commandFilter = null, CancellationToken token = default(CancellationToken))
104134
{
105-
return dbConn.Exec(dbCmd => dbCmd.UpdateAllAsync(objs, token, commandFilter));
135+
return dbConn.Exec(dbCmd => dbCmd.UpdateAllAsync(objs, commandFilter, token));
106136
}
107137

108138
/// <summary>
109-
/// Delete rows using an anonymous type filter. E.g:
139+
/// Delete rows using an anonymous type commandFilter. E.g:
110140
/// <para>db.Delete&lt;Person&gt;(new { FirstName = "Jimi", Age = 27 })</para>
111141
/// </summary>
112142
/// <returns>number of rows deleted</returns>
@@ -116,7 +146,7 @@ public static Task<int> UpdateAsync<T>(this IDbConnection dbConn, Action<IDbComm
116146
}
117147

118148
/// <summary>
119-
/// Delete 1 row using all fields in the filter. E.g:
149+
/// Delete 1 row using all fields in the commandFilter. E.g:
120150
/// <para>db.Delete(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 })</para>
121151
/// </summary>
122152
/// <returns>number of rows deleted</returns>
@@ -126,7 +156,7 @@ public static Task<int> UpdateAsync<T>(this IDbConnection dbConn, Action<IDbComm
126156
}
127157

128158
/// <summary>
129-
/// Delete 1 or more rows in a transaction using all fields in the filter. E.g:
159+
/// Delete 1 or more rows in a transaction using all fields in the commandFilter. E.g:
130160
/// <para>db.Delete(new Person { Id = 1, FirstName = "Jimi", LastName = "Hendrix", Age = 27 })</para>
131161
/// </summary>
132162
public static Task<int> DeleteAsync<T>(this IDbConnection dbConn, CancellationToken token = default(CancellationToken), params T[] allFieldsFilters)
@@ -139,7 +169,7 @@ public static Task<int> DeleteAsync<T>(this IDbConnection dbConn, params T[] all
139169
}
140170

141171
/// <summary>
142-
/// Delete 1 or more rows using only field with non-default values in the filter. E.g:
172+
/// Delete 1 or more rows using only field with non-default values in the commandFilter. E.g:
143173
/// <para>db.DeleteNonDefaults(new Person { FirstName = "Jimi", Age = 27 })</para>
144174
/// </summary>
145175
/// <returns>number of rows deleted</returns>
@@ -149,7 +179,7 @@ public static Task<int> DeleteAsync<T>(this IDbConnection dbConn, params T[] all
149179
}
150180

151181
/// <summary>
152-
/// Delete 1 or more rows in a transaction using only field with non-default values in the filter. E.g:
182+
/// Delete 1 or more rows in a transaction using only field with non-default values in the commandFilter. E.g:
153183
/// <para>db.DeleteNonDefaults(new Person { FirstName = "Jimi", Age = 27 },
154184
/// new Person { FirstName = "Janis", Age = 27 })</para>
155185
/// </summary>

0 commit comments

Comments
 (0)