/
OrmLiteWriteExpressionsApi.cs
286 lines (266 loc) · 13.9 KB
/
OrmLiteWriteExpressionsApi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;
namespace ServiceStack.OrmLite
{
public static class OrmLiteWriteExpressionsApi
{
/// <summary>
/// Use an SqlExpression to select which fields to update and construct the where expression, E.g:
///
/// var q = db.From>Person<());
/// db.UpdateOnlyFields(new Person { FirstName = "JJ" }, q.Update(p => p.FirstName).Where(x => x.FirstName == "Jimi"));
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("FirstName" = 'Jimi')
///
/// What's not in the update expression doesn't get updated. No where expression updates all rows. E.g:
///
/// db.UpdateOnlyFields(new Person { FirstName = "JJ", LastName = "Hendo" }, ev.Update(p => p.FirstName));
/// UPDATE "Person" SET "FirstName" = 'JJ'
/// </summary>
public static int UpdateOnlyFields<T>(this IDbConnection dbConn,
T model, SqlExpression<T> onlyFields,
Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateOnlyFields(model, onlyFields, commandFilter));
}
/// <summary>
/// Update record, updating only fields specified in updateOnly that matches the where condition (if any), E.g:
///
/// db.UpdateOnly(new Person { FirstName = "JJ" }, new[]{ "FirstName" }, p => p.LastName == "Hendrix");
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')
/// </summary>
public static int UpdateOnlyFields<T>(this IDbConnection dbConn,
T obj,
string[] onlyFields,
Expression<Func<T, bool>> where = null,
Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateOnlyFields(obj, onlyFields, where, commandFilter));
}
/// <summary>
/// Update record, updating only fields specified in updateOnly that matches the where condition (if any), E.g:
///
/// db.UpdateOnly(new Person { FirstName = "JJ" }, p => p.FirstName, p => p.LastName == "Hendrix");
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')
///
/// db.UpdateOnly(new Person { FirstName = "JJ" }, p => p.FirstName);
/// UPDATE "Person" SET "FirstName" = 'JJ'
///
/// db.UpdateOnly(new Person { FirstName = "JJ", Age = 27 }, p => new { p.FirstName, p.Age );
/// UPDATE "Person" SET "FirstName" = 'JJ', "Age" = 27
/// </summary>
public static int UpdateOnlyFields<T>(this IDbConnection dbConn,
T obj,
Expression<Func<T, object>> onlyFields = null,
Expression<Func<T, bool>> where = null,
Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateOnlyFields(obj, onlyFields, where, commandFilter));
}
/// <summary>
/// Update only fields in the specified expression that matches the where condition (if any), E.g:
///
/// db.UpdateOnly(() => new Person { FirstName = "JJ" }, where: p => p.LastName == "Hendrix");
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')
///
/// db.UpdateOnly(() => new Person { FirstName = "JJ" });
/// UPDATE "Person" SET "FirstName" = 'JJ'
/// </summary>
public static int UpdateOnly<T>(this IDbConnection dbConn,
Expression<Func<T>> updateFields,
Expression<Func<T, bool>> where = null,
Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateOnly(updateFields, dbCmd.GetDialectProvider().SqlExpression<T>().Where(where), commandFilter));
}
/// <summary>
/// Update only fields in the specified expression that matches the where condition (if any), E.g:
///
/// db.UpdateOnly(() => new Person { FirstName = "JJ" }, db.From>Person<().Where(p => p.LastName == "Hendrix"));
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')
/// </summary>
public static int UpdateOnly<T>(this IDbConnection dbConn,
Expression<Func<T>> updateFields,
SqlExpression<T> q,
Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateOnly(updateFields, q, commandFilter));
}
/// <summary>
/// Update only fields in the specified expression that matches the where condition (if any), E.g:
///
/// var q = db.From>Person<().Where(p => p.LastName == "Hendrix");
/// db.UpdateOnly(() => new Person { FirstName = "JJ" }, q.WhereExpression, q.Params);
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')
/// </summary>
public static int UpdateOnly<T>(this IDbConnection dbConn,
Expression<Func<T>> updateFields,
string whereExpression,
IEnumerable<IDbDataParameter> sqlParams,
Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateOnly(updateFields, whereExpression, sqlParams, commandFilter));
}
/// <summary>
/// Updates all values from Object Dictionary matching the where condition. E.g
///
/// db.UpdateOnly<Person>(new Dictionary<string,object< { {"FirstName", "JJ"} }, where:p => p.FirstName == "Jimi");
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("FirstName" = 'Jimi')
/// </summary>
public static int UpdateOnly<T>(this IDbConnection dbConn, Dictionary<string, object> updateFields, Expression<Func<T, bool>> obj)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateOnly(updateFields, obj));
}
/// <summary>
/// Updates all values from Object Dictionary, Requires Id which is used as a Primary Key Filter. E.g
///
/// db.UpdateOnly<Person>(new Dictionary<string,object< { {"Id", 1}, {"FirstName", "JJ"} });
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("Id" = 1)
/// </summary>
public static int UpdateOnly<T>(this IDbConnection dbConn, Dictionary<string, object> updateFields,
Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateOnly<T>(updateFields, commandFilter));
}
/// <summary>
/// Updates all values from Object Dictionary matching the where condition. E.g
///
/// db.UpdateOnly<Person>(new Dictionary<string,object< { {"FirstName", "JJ"} }, "FirstName == {0}", new[] { "Jimi" });
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("FirstName" = 'Jimi')
/// </summary>
public static int UpdateOnly<T>(this IDbConnection dbConn,
Dictionary<string, object> updateFields, string whereExpression, object[] whereParams,
Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateOnly<T>(updateFields, whereExpression, whereParams, commandFilter));
}
/// <summary>
/// Update record, updating only fields specified in updateOnly that matches the where condition (if any), E.g:
/// Numeric fields generates an increment sql which is useful to increment counters, etc...
/// avoiding concurrency conflicts
///
/// db.UpdateAdd(() => new Person { Age = 5 }, where: p => p.LastName == "Hendrix");
/// UPDATE "Person" SET "Age" = "Age" + 5 WHERE ("LastName" = 'Hendrix')
///
/// db.UpdateAdd(() => new Person { Age = 5 });
/// UPDATE "Person" SET "Age" = "Age" + 5
/// </summary>
public static int UpdateAdd<T>(this IDbConnection dbConn,
Expression<Func<T>> updateFields,
Expression<Func<T, bool>> where = null,
Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateAdd(updateFields, dbCmd.GetDialectProvider().SqlExpression<T>().Where(where), commandFilter));
}
/// <summary>
/// Update record, updating only fields specified in updateOnly that matches the where condition (if any), E.g:
/// Numeric fields generates an increment sql which is useful to increment counters, etc...
/// avoiding concurrency conflicts
///
/// db.UpdateAdd(() => new Person { Age = 5 }, db.From<Person>().Where(p => p.LastName == "Hendrix"));
/// UPDATE "Person" SET "Age" = "Age" + 5 WHERE ("LastName" = 'Hendrix')
/// </summary>
public static int UpdateAdd<T>(this IDbConnection dbConn,
Expression<Func<T>> updateFields,
SqlExpression<T> q,
Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateAdd(updateFields, q, commandFilter));
}
/// <summary>
/// Updates all non-default values set on item matching the where condition (if any). E.g
///
/// db.UpdateNonDefaults(new Person { FirstName = "JJ" }, p => p.FirstName == "Jimi");
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("FirstName" = 'Jimi')
/// </summary>
public static int UpdateNonDefaults<T>(this IDbConnection dbConn, T item, Expression<Func<T, bool>> obj)
{
return dbConn.Exec(dbCmd => dbCmd.UpdateNonDefaults(item, obj));
}
/// <summary>
/// Updates all values set on item matching the where condition (if any). E.g
///
/// db.Update(new Person { Id = 1, FirstName = "JJ" }, p => p.LastName == "Hendrix");
/// UPDATE "Person" SET "Id" = 1,"FirstName" = 'JJ',"LastName" = NULL,"Age" = 0 WHERE ("LastName" = 'Hendrix')
/// </summary>
public static int Update<T>(this IDbConnection dbConn, T item, Expression<Func<T, bool>> where, Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.Update(item, where, commandFilter));
}
/// <summary>
/// Updates all matching fields populated on anonymousType that matches where condition (if any). E.g:
///
/// db.Update<Person>(new { FirstName = "JJ" }, p => p.LastName == "Hendrix");
/// UPDATE "Person" SET "FirstName" = 'JJ' WHERE ("LastName" = 'Hendrix')
/// </summary>
public static int Update<T>(this IDbConnection dbConn, object updateOnly, Expression<Func<T, bool>> where, Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.Update<T>(updateOnly, where, commandFilter));
}
/// <summary>
/// Using an SqlExpression to only Insert the fields specified, e.g:
///
/// db.InsertOnly(new Person { FirstName = "Amy" }, p => p.FirstName));
/// INSERT INTO "Person" ("FirstName") VALUES ('Amy');
///
/// db.InsertOnly(new Person { Id =1 , FirstName="Amy" }, p => new { p.Id, p.FirstName }));
/// INSERT INTO "Person" ("Id", "FirstName") VALUES (1, 'Amy');
/// </summary>
public static long InsertOnly<T>(this IDbConnection dbConn, T obj, Expression<Func<T, object>> onlyFields, bool selectIdentity = false)
{
return dbConn.Exec(dbCmd => dbCmd.InsertOnly(obj, onlyFields.GetFieldNames(), selectIdentity));
}
/// <summary>
/// Using an SqlExpression to only Insert the fields specified, e.g:
///
/// db.InsertOnly(new Person { FirstName = "Amy" }, new[]{ "FirstName" }));
/// INSERT INTO "Person" ("FirstName") VALUES ('Amy');
/// </summary>
public static long InsertOnly<T>(this IDbConnection dbConn, T obj, string[] onlyFields, bool selectIdentity = false)
{
return dbConn.Exec(dbCmd => dbCmd.InsertOnly(obj, onlyFields, selectIdentity));
}
/// <summary>
/// Using an SqlExpression to only Insert the fields specified, e.g:
///
/// db.InsertOnly(() => new Person { FirstName = "Amy" }));
/// INSERT INTO "Person" ("FirstName") VALUES (@FirstName);
/// </summary>
public static long InsertOnly<T>(this IDbConnection dbConn, Expression<Func<T>> insertFields, bool selectIdentity = false)
{
return dbConn.Exec(dbCmd => dbCmd.InsertOnly(insertFields, selectIdentity));
}
/// <summary>
/// Delete the rows that matches the where expression, e.g:
///
/// db.Delete<Person>(p => p.Age == 27);
/// DELETE FROM "Person" WHERE ("Age" = 27)
/// </summary>
public static int Delete<T>(this IDbConnection dbConn, Expression<Func<T, bool>> where, Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.Delete(where, commandFilter));
}
/// <summary>
/// Delete the rows that matches the where expression, e.g:
///
/// var q = db.From<Person>());
/// db.Delete<Person>(q.Where(p => p.Age == 27));
/// DELETE FROM "Person" WHERE ("Age" = 27)
/// </summary>
public static int Delete<T>(this IDbConnection dbConn, SqlExpression<T> where, Action<IDbCommand> commandFilter = null)
{
return dbConn.Exec(dbCmd => dbCmd.Delete(where, commandFilter));
}
/// <summary>
/// Delete the rows that matches the where filter, e.g:
///
/// db.DeleteWhere<Person>("Age = {0}", new object[] { 27 });
/// DELETE FROM "Person" WHERE ("Age" = 27)
/// </summary>
public static int DeleteWhere<T>(this IDbConnection dbConn, string whereFilter, object[] whereParams)
{
return dbConn.Exec(dbCmd => dbCmd.DeleteWhere<T>(whereFilter, whereParams));
}
}
}