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

Commit 3048d87

Browse files
authored
Merge pull request #559 from BruceCowan-AI/AddGenericUpdateCallback
A start at implementing generic update callback
2 parents f7eb67c + 02c7761 commit 3048d87

18 files changed

+755
-122
lines changed

src/ServiceStack.OrmLite.Oracle.Tests/ServiceStack.OrmLite.Oracle.Tests.csproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@
301301
<Compile Include="..\..\tests\ServiceStack.OrmLite.Tests\Shared\BuiltInsFactory.cs">
302302
<Link>Shared\BuiltInsFactory.cs</Link>
303303
</Compile>
304+
<Compile Include="..\..\tests\ServiceStack.OrmLite.Tests\Shared\DefaultValues.cs">
305+
<Link>Shared\DefaultValues.cs</Link>
306+
</Compile>
304307
<Compile Include="..\..\tests\ServiceStack.OrmLite.Tests\Shared\IModelFactory.cs">
305308
<Link>Shared\IModelFactory.cs</Link>
306309
</Compile>
@@ -379,6 +382,9 @@
379382
<Compile Include="..\..\tests\ServiceStack.OrmLite.Tests\Shared\TaskQueue.cs">
380383
<Link>Shared\TaskQueue.cs</Link>
381384
</Compile>
385+
<Compile Include="..\..\tests\ServiceStack.OrmLite.Tests\Shared\UpdateCommandFilter.cs">
386+
<Link>Shared\UpdateCommandFilter.cs</Link>
387+
</Compile>
382388
<Compile Include="..\..\tests\ServiceStack.OrmLite.Tests\ShippersExample.cs">
383389
<Link>ShippersExample.cs</Link>
384390
</Compile>
@@ -458,6 +464,10 @@
458464
<Project>{517B64BA-D7A6-4A15-8719-821B38147C61}</Project>
459465
<Name>ServiceStack.OrmLite.Oracle</Name>
460466
</ProjectReference>
467+
<ProjectReference Include="..\ServiceStack.OrmLite.SqlServer\ServiceStack.OrmLite.SqlServer.csproj">
468+
<Project>{1887dc99-9139-43e3-a7aa-6d74714b3a5d}</Project>
469+
<Name>ServiceStack.OrmLite.SqlServer</Name>
470+
</ProjectReference>
461471
<ProjectReference Include="..\ServiceStack.OrmLite\ServiceStack.OrmLite.csproj">
462472
<Project>{96179AC6-F6F1-40C3-9FDD-4F6582F54C5C}</Project>
463473
<Name>ServiceStack.OrmLite</Name>

src/ServiceStack.OrmLite/Async/OrmLiteWriteCommandExtensionsAsync.cs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,17 @@ internal static Task<int> ExecuteSqlAsync(this IDbCommand dbCmd, string sql, obj
5252
return dbCmd.GetDialectProvider().ExecuteNonQueryAsync(dbCmd, token);
5353
}
5454

55-
internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, T obj, CancellationToken token)
55+
internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, T obj, CancellationToken token, Action<IDbCommand> commandFilter)
5656
{
57-
if (OrmLiteConfig.UpdateFilter != null)
58-
OrmLiteConfig.UpdateFilter(dbCmd, obj);
57+
OrmLiteConfig.UpdateFilter?.Invoke(dbCmd, obj);
5958

6059
var dialectProvider = dbCmd.GetDialectProvider();
6160
var hadRowVersion = dialectProvider.PrepareParameterizedUpdateStatement<T>(dbCmd);
6261
if (string.IsNullOrEmpty(dbCmd.CommandText))
6362
return TaskResult.Zero;
6463

6564
dialectProvider.SetParameterValues<T>(dbCmd, obj);
65+
commandFilter?.Invoke(dbCmd);
6666

6767
return dialectProvider.ExecuteNonQueryAsync(dbCmd, token)
6868
.Then(rowsUpdated =>
@@ -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, params T[] objs)
76+
internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, CancellationToken token, Action<IDbCommand> commandFilter, T[] objs)
7777
{
78-
return dbCmd.UpdateAllAsync(objs, token);
78+
return dbCmd.UpdateAllAsync(objs, token, commandFilter);
7979
}
8080

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

@@ -94,10 +94,11 @@ internal static Task<int> UpdateAllAsync<T>(this IDbCommand dbCmd, IEnumerable<T
9494

9595
return objs.EachAsync((obj, i) =>
9696
{
97-
if (OrmLiteConfig.UpdateFilter != null)
98-
OrmLiteConfig.UpdateFilter(dbCmd, obj);
97+
OrmLiteConfig.UpdateFilter?.Invoke(dbCmd, obj);
9998

10099
dialectProvider.SetParameterValues<T>(dbCmd, obj);
100+
commandFilter?.Invoke(dbCmd);
101+
commandFilter = null;
101102

102103
return dbCmd.ExecNonQueryAsync(token).Then(rowsUpdated =>
103104
{
@@ -346,7 +347,7 @@ internal static async Task<bool> SaveAsync<T>(this IDbCommand dbCmd, T obj, Canc
346347
return true;
347348
}
348349

349-
await dbCmd.UpdateAsync(obj, token);
350+
await dbCmd.UpdateAsync(obj, token, null);
350351

351352
modelDef.RowVersion?.SetValueFn(obj, await dbCmd.GetRowVersionAsync(modelDef, id, token));
352353

@@ -389,7 +390,7 @@ internal static async Task<int> SaveAllAsync<T>(this IDbCommand dbCmd, IEnumerab
389390
{
390391
OrmLiteConfig.UpdateFilter?.Invoke(dbCmd, row);
391392

392-
await dbCmd.UpdateAsync(row, token);
393+
await dbCmd.UpdateAsync(row, token, null);
393394
}
394395
else
395396
{
@@ -474,7 +475,7 @@ public static async Task SaveAllReferencesAsync<T>(this IDbCommand dbCmd, T inst
474475
{
475476
var refPkValue = refModelDef.PrimaryKey.GetValue(result);
476477
refSelf.SetValueFn(instance, refPkValue);
477-
await dbCmd.UpdateAsync(instance, token);
478+
await dbCmd.UpdateAsync(instance, token, null);
478479
}
479480
}
480481
}
@@ -509,7 +510,7 @@ public static async Task SaveReferencesAsync<T, TRef>(this IDbCommand dbCmd, Can
509510
{
510511
var refPkValue = refModelDef.PrimaryKey.GetValue(oRef);
511512
refSelf.SetValueFn(instance, refPkValue);
512-
await dbCmd.UpdateAsync(instance, token);
513+
await dbCmd.UpdateAsync(instance, token, null);
513514
}
514515
}
515516
}

src/ServiceStack.OrmLite/Async/WriteExpressionCommandExtensionsAsync.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ namespace ServiceStack.OrmLite
99
{
1010
internal static class WriteExpressionCommandExtensionsAsync
1111
{
12-
internal static Task<int> UpdateOnlyAsync<T>(this IDbCommand dbCmd, T model, SqlExpression<T> onlyFields, CancellationToken token)
12+
internal static Task<int> UpdateOnlyAsync<T>(this IDbCommand dbCmd, T model, SqlExpression<T> onlyFields, Action<IDbCommand> commandFilter, CancellationToken token)
1313
{
1414
dbCmd.UpdateOnlySql(model, onlyFields);
15+
commandFilter?.Invoke(dbCmd);
1516
return dbCmd.ExecNonQueryAsync(token);
1617
}
1718

1819
internal static Task<int> UpdateOnlyAsync<T>(this IDbCommand dbCmd, T obj,
1920
Expression<Func<T, object>> onlyFields,
2021
Expression<Func<T, bool>> where,
22+
Action<IDbCommand> commandFilter,
2123
CancellationToken token)
2224
{
2325
if (onlyFields == null)
@@ -26,12 +28,13 @@ internal static Task<int> UpdateOnlyAsync<T>(this IDbCommand dbCmd, T obj,
2628
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
2729
q.Update(onlyFields);
2830
q.Where(where);
29-
return dbCmd.UpdateOnlyAsync(obj, q, token);
31+
return dbCmd.UpdateOnlyAsync(obj, q, commandFilter, token);
3032
}
3133

3234
internal static Task<int> UpdateOnlyAsync<T>(this IDbCommand dbCmd, T obj,
3335
string[] onlyFields,
3436
Expression<Func<T, bool>> where,
37+
Action<IDbCommand> commandFilter,
3538
CancellationToken token)
3639
{
3740
if (onlyFields == null)
@@ -40,23 +43,29 @@ internal static Task<int> UpdateOnlyAsync<T>(this IDbCommand dbCmd, T obj,
4043
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
4144
q.Update(onlyFields);
4245
q.Where(where);
43-
return dbCmd.UpdateOnlyAsync(obj, q, token);
46+
return dbCmd.UpdateOnlyAsync(obj, q, commandFilter, token);
4447
}
4548

4649
internal static Task<int> UpdateOnlyAsync<T>(this IDbCommand dbCmd,
4750
Expression<Func<T>> updateFields,
4851
SqlExpression<T> q,
52+
Action<IDbCommand> commandFilter,
4953
CancellationToken token)
5054
{
51-
return dbCmd.InitUpdateOnly(updateFields, q).ExecNonQueryAsync(token);
55+
var cmd = dbCmd.InitUpdateOnly(updateFields, q);
56+
commandFilter?.Invoke(cmd);
57+
return cmd.ExecNonQueryAsync(token);
5258
}
5359

5460
public static Task<int> UpdateAddAsync<T>(this IDbCommand dbCmd,
5561
Expression<Func<T>> updateFields,
5662
SqlExpression<T> q,
63+
Action<IDbCommand> commandFilter,
5764
CancellationToken token)
5865
{
59-
return dbCmd.InitUpdateAdd(updateFields, q).ExecNonQueryAsync(token);
66+
var cmd = dbCmd.InitUpdateAdd(updateFields, q);
67+
commandFilter?.Invoke(cmd);
68+
return cmd.ExecNonQueryAsync(token);
6069
}
6170

6271
internal static Task<int> UpdateNonDefaultsAsync<T>(this IDbCommand dbCmd, T item, Expression<Func<T, bool>> obj, CancellationToken token)
@@ -69,22 +78,24 @@ internal static Task<int> UpdateNonDefaultsAsync<T>(this IDbCommand dbCmd, T ite
6978
return dbCmd.ExecNonQueryAsync(token);
7079
}
7180

72-
internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, T item, Expression<Func<T, bool>> expression, CancellationToken token)
81+
internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, T item, Expression<Func<T, bool>> expression, Action<IDbCommand> commandFilter, CancellationToken token)
7382
{
7483
OrmLiteConfig.UpdateFilter?.Invoke(dbCmd, item);
7584

7685
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
7786
q.Where(expression);
7887
q.PrepareUpdateStatement(dbCmd, item);
88+
commandFilter?.Invoke(dbCmd);
7989
return dbCmd.ExecNonQueryAsync(token);
8090
}
8191

82-
internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, object updateOnly, Expression<Func<T, bool>> where, CancellationToken token)
92+
internal static Task<int> UpdateAsync<T>(this IDbCommand dbCmd, object updateOnly, Expression<Func<T, bool>> where, Action<IDbCommand> commandFilter, CancellationToken token)
8393
{
8494
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
85-
var whereSql = q.Where(@where).WhereExpression;
95+
var whereSql = q.Where(where).WhereExpression;
8696
q.CopyParamsTo(dbCmd);
8797
dbCmd.PrepareUpdateAnonSql<T>(dbCmd.GetDialectProvider(), updateOnly, whereSql);
98+
commandFilter?.Invoke(dbCmd);
8899

89100
return dbCmd.ExecNonQueryAsync(token);
90101
}

src/ServiceStack.OrmLite/Expressions/WriteExpressionCommandExtensions.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,13 @@ namespace ServiceStack.OrmLite
1010
{
1111
internal static class WriteExpressionCommandExtensions
1212
{
13-
public static int UpdateOnly<T>(this IDbCommand dbCmd, T model, SqlExpression<T> onlyFields)
13+
public static int UpdateOnly<T>(this IDbCommand dbCmd,
14+
T model,
15+
SqlExpression<T> onlyFields,
16+
Action<IDbCommand> commandFilter = null)
1417
{
1518
UpdateOnlySql(dbCmd, model, onlyFields);
19+
commandFilter?.Invoke(dbCmd);
1620
return dbCmd.ExecNonQuery();
1721
}
1822

@@ -34,35 +38,40 @@ internal static void UpdateOnlySql<T>(this IDbCommand dbCmd, T model, SqlExpress
3438

3539
internal static int UpdateOnly<T>(this IDbCommand dbCmd, T obj,
3640
Expression<Func<T, object>> onlyFields = null,
37-
Expression<Func<T, bool>> where = null)
41+
Expression<Func<T, bool>> where = null,
42+
Action<IDbCommand> commandFilter = null)
3843
{
3944
if (onlyFields == null)
4045
throw new ArgumentNullException(nameof(onlyFields));
4146

4247
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
4348
q.Update(onlyFields);
4449
q.Where(where);
45-
return dbCmd.UpdateOnly(obj, q);
50+
return dbCmd.UpdateOnly(obj, q, commandFilter);
4651
}
4752

4853
internal static int UpdateOnly<T>(this IDbCommand dbCmd, T obj,
4954
string[] onlyFields = null,
50-
Expression<Func<T, bool>> where = null)
55+
Expression<Func<T, bool>> where = null,
56+
Action<IDbCommand> commandFilter = null)
5157
{
5258
if (onlyFields == null)
5359
throw new ArgumentNullException(nameof(onlyFields));
5460

5561
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
5662
q.Update(onlyFields);
5763
q.Where(where);
58-
return dbCmd.UpdateOnly(obj, q);
64+
return dbCmd.UpdateOnly(obj, q, commandFilter);
5965
}
6066

6167
internal static int UpdateOnly<T>(this IDbCommand dbCmd,
6268
Expression<Func<T>> updateFields,
63-
SqlExpression<T> q)
69+
SqlExpression<T> q,
70+
Action<IDbCommand> commandFilter = null)
6471
{
65-
return dbCmd.InitUpdateOnly(updateFields, q).ExecNonQuery();
72+
var cmd = dbCmd.InitUpdateOnly(updateFields, q);
73+
commandFilter?.Invoke(cmd);
74+
return cmd.ExecNonQuery();
6675
}
6776

6877
internal static IDbCommand InitUpdateOnly<T>(this IDbCommand dbCmd, Expression<Func<T>> updateFields, SqlExpression<T> q)
@@ -82,9 +91,12 @@ internal static IDbCommand InitUpdateOnly<T>(this IDbCommand dbCmd, Expression<F
8291

8392
public static int UpdateAdd<T>(this IDbCommand dbCmd,
8493
Expression<Func<T>> updateFields,
85-
SqlExpression<T> q)
94+
SqlExpression<T> q,
95+
Action<IDbCommand> commandFilter)
8696
{
87-
return dbCmd.InitUpdateAdd(updateFields, q).ExecNonQuery();
97+
var cmd = dbCmd.InitUpdateAdd(updateFields, q);
98+
commandFilter?.Invoke(cmd);
99+
return cmd.ExecNonQuery();
88100
}
89101

90102
internal static IDbCommand InitUpdateAdd<T>(this IDbCommand dbCmd, Expression<Func<T>> updateFields, SqlExpression<T> q)
@@ -112,23 +124,25 @@ public static int UpdateNonDefaults<T>(this IDbCommand dbCmd, T item, Expression
112124
return dbCmd.ExecNonQuery();
113125
}
114126

115-
public static int Update<T>(this IDbCommand dbCmd, T item, Expression<Func<T, bool>> expression)
127+
public static int Update<T>(this IDbCommand dbCmd, T item, Expression<Func<T, bool>> expression, Action<IDbCommand> commandFilter = null)
116128
{
117129
OrmLiteConfig.UpdateFilter?.Invoke(dbCmd, item);
118130

119131
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
120132
q.Where(expression);
121133
q.PrepareUpdateStatement(dbCmd, item);
134+
commandFilter?.Invoke(dbCmd);
122135
return dbCmd.ExecNonQuery();
123136
}
124137

125-
public static int Update<T>(this IDbCommand dbCmd, object updateOnly, Expression<Func<T, bool>> where = null)
138+
public static int Update<T>(this IDbCommand dbCmd, object updateOnly, Expression<Func<T, bool>> where = null, Action<IDbCommand> commandFilter = null)
126139
{
127140
var q = dbCmd.GetDialectProvider().SqlExpression<T>();
128141
var whereSql = q.Where(where).WhereExpression;
129142
q.CopyParamsTo(dbCmd);
130143
dbCmd.PrepareUpdateAnonSql<T>(dbCmd.GetDialectProvider(), updateOnly, whereSql);
131144

145+
commandFilter?.Invoke(dbCmd);
132146
return dbCmd.ExecNonQuery();
133147
}
134148

src/ServiceStack.OrmLite/IOrmLiteDialectProvider.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public interface IOrmLiteDialectProvider
6161

6262
string GetQuotedValue(object value, Type fieldType);
6363

64+
string GetDefaultValue(Type tableType, string fieldName);
65+
6466
object GetParamValue(object value, Type fieldType);
6567

6668
object ToDbValue(object value, Type type);

src/ServiceStack.OrmLite/Legacy/WriteExpressionCommandExtensionsAsyncLegacy.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal static Task InsertOnlyAsync<T>(this IDbCommand dbCmd, T obj, Func<SqlEx
1919
[Obsolete("Use db.UpdateOnlyAsync(model, db.From<T>())")]
2020
internal static Task<int> UpdateOnlyAsync<T>(this IDbCommand dbCmd, T model, Func<SqlExpression<T>, SqlExpression<T>> onlyFields, CancellationToken token)
2121
{
22-
return dbCmd.UpdateOnlyAsync(model, onlyFields(dbCmd.GetDialectProvider().SqlExpression<T>()), token);
22+
return dbCmd.UpdateOnlyAsync(model, onlyFields(dbCmd.GetDialectProvider().SqlExpression<T>()), null, token);
2323
}
2424

2525
internal static Task<int> UpdateFmtAsync<T>(this IDbCommand dbCmd, string set, string where, CancellationToken token)
@@ -29,7 +29,7 @@ internal static Task<int> UpdateFmtAsync<T>(this IDbCommand dbCmd, string set, s
2929

3030
internal static Task<int> UpdateFmtAsync(this IDbCommand dbCmd, string table, string set, string where, CancellationToken token)
3131
{
32-
var sql = WriteExpressionCommandExtensionsLegacy.UpdateFmtSql(dbCmd.GetDialectProvider(), table, set, @where);
32+
var sql = WriteExpressionCommandExtensionsLegacy.UpdateFmtSql(dbCmd.GetDialectProvider(), table, set, where);
3333
return dbCmd.ExecuteSqlAsync(sql, token);
3434
}
3535

@@ -46,7 +46,7 @@ internal static Task<int> DeleteFmtAsync<T>(this IDbCommand dbCmd, string where,
4646

4747
internal static Task<int> DeleteFmtAsync(this IDbCommand dbCmd, string table, string where, CancellationToken token)
4848
{
49-
var sql = WriteExpressionCommandExtensionsLegacy.DeleteFmtSql(dbCmd.GetDialectProvider(), table, @where);
49+
var sql = WriteExpressionCommandExtensionsLegacy.DeleteFmtSql(dbCmd.GetDialectProvider(), table, where);
5050
return dbCmd.ExecuteSqlAsync(sql, token);
5151
}
5252

src/ServiceStack.OrmLite/OrmLiteDialectProviderBase.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,13 @@ public virtual string ToDeleteStatement(Type tableType, string sqlFilter, params
11851185
return StringBuilderCache.ReturnAndFree(sql);
11861186
}
11871187

1188+
public string GetDefaultValue(Type tableType, string fieldName)
1189+
{
1190+
var modelDef = tableType.GetModelDefinition();
1191+
var fieldDef = modelDef.GetFieldDefinition(fieldName);
1192+
return GetDefaultValue(fieldDef);
1193+
}
1194+
11881195
public virtual string GetDefaultValue(FieldDefinition fieldDef)
11891196
{
11901197
var defaultValue = fieldDef.DefaultValue;

0 commit comments

Comments
 (0)