-
-
Notifications
You must be signed in to change notification settings - Fork 388
/
Copy pathSqlServerBatchRunner.cs
453 lines (381 loc) · 18.9 KB
/
SqlServerBatchRunner.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
using System;
using System.Data;
using System.Data.Common;
using System.Data.Entity.Core.EntityClient;
using System.Data.Entity.Core.Objects;
using System.Linq;
using EntityFramework.DynamicLinq;
using System.Linq.Expressions;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using EntityFramework.Extensions;
using EntityFramework.Mapping;
using EntityFramework.Reflection;
namespace EntityFramework.Batch
{
/// <summary>
/// A batch execution runner for SQL Server.
/// </summary>
public class SqlServerBatchRunner : IBatchRunner
{
/// <summary>
/// Create and run a batch delete statement.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="objectContext">The <see cref="ObjectContext"/> to get connection and metadata information from.</param>
/// <param name="entityMap">The <see cref="EntityMap"/> for <typeparamref name="TEntity"/>.</param>
/// <param name="query">The query to create the where clause from.</param>
/// <returns>
/// The number of rows deleted.
/// </returns>
public int Delete<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query) where TEntity : class
{
#if NET45
return InternalDelete(objectContext, entityMap, query, false).Result;
#else
return InternalDelete(objectContext, entityMap, query);
#endif
}
#if NET45
/// <summary>
/// Create and run a batch delete statement asynchronously.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="objectContext">The <see cref="ObjectContext"/> to get connection and metadata information from.</param>
/// <param name="entityMap">The <see cref="EntityMap"/> for <typeparamref name="TEntity"/>.</param>
/// <param name="query">The query to create the where clause from.</param>
/// <returns>
/// The number of rows deleted.
/// </returns>
public Task<int> DeleteAsync<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query) where TEntity : class
{
return InternalDelete(objectContext, entityMap, query, true);
}
#endif
#if NET45
private async Task<int> InternalDelete<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query, bool async = false)
where TEntity : class
#else
private int InternalDelete<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query)
where TEntity : class
#endif
{
DbConnection deleteConnection = null;
DbTransaction deleteTransaction = null;
DbCommand deleteCommand = null;
bool ownConnection = false;
bool ownTransaction = false;
try
{
// get store connection and transaction
var store = GetStore(objectContext);
deleteConnection = store.Item1;
deleteTransaction = store.Item2;
if (deleteConnection.State != ConnectionState.Open)
{
deleteConnection.Open();
ownConnection = true;
}
if (deleteTransaction == null)
{
deleteTransaction = deleteConnection.BeginTransaction();
ownTransaction = true;
}
deleteCommand = deleteConnection.CreateCommand();
deleteCommand.Transaction = deleteTransaction;
if (objectContext.CommandTimeout.HasValue)
deleteCommand.CommandTimeout = objectContext.CommandTimeout.Value;
var innerSelect = GetSelectSql(query, entityMap, deleteCommand);
var sqlBuilder = new StringBuilder(innerSelect.Length * 2);
sqlBuilder.Append("DELETE ");
sqlBuilder.Append(entityMap.TableName);
sqlBuilder.AppendLine();
sqlBuilder.AppendFormat("FROM {0} AS j0 INNER JOIN (", entityMap.TableName);
sqlBuilder.AppendLine();
sqlBuilder.AppendLine(innerSelect);
sqlBuilder.Append(") AS j1 ON (");
bool wroteKey = false;
foreach (var keyMap in entityMap.KeyMaps)
{
if (wroteKey)
sqlBuilder.Append(" AND ");
sqlBuilder.AppendFormat("j0.[{0}] = j1.[{0}]", keyMap.ColumnName);
wroteKey = true;
}
sqlBuilder.Append(")");
deleteCommand.CommandText = sqlBuilder.ToString();
#if NET45
int result = async
? await deleteCommand.ExecuteNonQueryAsync()
: deleteCommand.ExecuteNonQuery();
#else
int result = deleteCommand.ExecuteNonQuery();
#endif
// only commit if created transaction
if (ownTransaction)
deleteTransaction.Commit();
return result;
}
finally
{
if (deleteCommand != null)
deleteCommand.Dispose();
if (deleteTransaction != null && ownTransaction)
deleteTransaction.Dispose();
if (deleteConnection != null && ownConnection)
deleteConnection.Close();
}
}
/// <summary>
/// Create and run a batch update statement.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="objectContext">The <see cref="ObjectContext"/> to get connection and metadata information from.</param>
/// <param name="entityMap">The <see cref="EntityMap"/> for <typeparamref name="TEntity"/>.</param>
/// <param name="query">The query to create the where clause from.</param>
/// <param name="updateExpression">The update expression.</param>
/// <returns>
/// The number of rows updated.
/// </returns>
public int Update<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query, Expression<Func<TEntity, TEntity>> updateExpression) where TEntity : class
{
#if NET45
return InternalUpdate(objectContext, entityMap, query, updateExpression, false).Result;
#else
return InternalUpdate(objectContext, entityMap, query, updateExpression);
#endif
}
#if NET45
/// <summary>
/// Create and run a batch update statement asynchronously.
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="objectContext">The <see cref="ObjectContext"/> to get connection and metadata information from.</param>
/// <param name="entityMap">The <see cref="EntityMap"/> for <typeparamref name="TEntity"/>.</param>
/// <param name="query">The query to create the where clause from.</param>
/// <param name="updateExpression">The update expression.</param>
/// <returns>
/// The number of rows updated.
/// </returns>
public Task<int> UpdateAsync<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query, Expression<Func<TEntity, TEntity>> updateExpression) where TEntity : class
{
return InternalUpdate(objectContext, entityMap, query, updateExpression, true);
}
#endif
#if NET45
private async Task<int> InternalUpdate<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query, Expression<Func<TEntity, TEntity>> updateExpression, bool async = false)
where TEntity : class
#else
private int InternalUpdate<TEntity>(ObjectContext objectContext, EntityMap entityMap, ObjectQuery<TEntity> query, Expression<Func<TEntity, TEntity>> updateExpression, bool async = false)
where TEntity : class
#endif
{
DbConnection updateConnection = null;
DbTransaction updateTransaction = null;
DbCommand updateCommand = null;
bool ownConnection = false;
bool ownTransaction = false;
try
{
// get store connection and transaction
var store = GetStore(objectContext);
updateConnection = store.Item1;
updateTransaction = store.Item2;
if (updateConnection.State != ConnectionState.Open)
{
updateConnection.Open();
ownConnection = true;
}
// use existing transaction or create new
if (updateTransaction == null)
{
updateTransaction = updateConnection.BeginTransaction();
ownTransaction = true;
}
updateCommand = updateConnection.CreateCommand();
updateCommand.Transaction = updateTransaction;
if (objectContext.CommandTimeout.HasValue)
updateCommand.CommandTimeout = objectContext.CommandTimeout.Value;
var innerSelect = GetSelectSql(query, entityMap, updateCommand);
var sqlBuilder = new StringBuilder(innerSelect.Length * 2);
sqlBuilder.Append("UPDATE ");
sqlBuilder.Append(entityMap.TableName);
sqlBuilder.AppendLine(" SET ");
var memberInitExpression = updateExpression.Body as MemberInitExpression;
if (memberInitExpression == null)
throw new ArgumentException("The update expression must be of type MemberInitExpression.", "updateExpression");
int nameCount = 0;
bool wroteSet = false;
foreach (MemberBinding binding in memberInitExpression.Bindings)
{
if (wroteSet)
sqlBuilder.AppendLine(", ");
string propertyName = binding.Member.Name;
string columnName = entityMap.PropertyMaps
.Where(p => p.PropertyName == propertyName)
.Select(p => p.ColumnName)
.FirstOrDefault();
var memberAssignment = binding as MemberAssignment;
if (memberAssignment == null)
throw new ArgumentException("The update expression MemberBinding must only by type MemberAssignment.", "updateExpression");
Expression memberExpression = memberAssignment.Expression;
ParameterExpression parameterExpression = null;
memberExpression.Visit((ParameterExpression p) =>
{
if (p.Type == entityMap.EntityType)
parameterExpression = p;
return p;
});
if (parameterExpression == null)
{
object value;
if (memberExpression.NodeType == ExpressionType.Constant)
{
var constantExpression = memberExpression as ConstantExpression;
if (constantExpression == null)
throw new ArgumentException(
"The MemberAssignment expression is not a ConstantExpression.", "updateExpression");
value = constantExpression.Value;
}
else
{
LambdaExpression lambda = Expression.Lambda(memberExpression, null);
value = lambda.Compile().DynamicInvoke();
}
if (value != null)
{
string parameterName = "p__update__" + nameCount++;
var parameter = updateCommand.CreateParameter();
parameter.ParameterName = parameterName;
parameter.Value = value;
updateCommand.Parameters.Add(parameter);
sqlBuilder.AppendFormat("[{0}] = @{1}", columnName, parameterName);
}
else
{
sqlBuilder.AppendFormat("[{0}] = NULL", columnName);
}
}
else
{
// create clean objectset to build query from
var objectSet = objectContext.CreateObjectSet<TEntity>();
Type[] typeArguments = new[] { entityMap.EntityType, memberExpression.Type };
ConstantExpression constantExpression = Expression.Constant(objectSet);
LambdaExpression lambdaExpression = Expression.Lambda(memberExpression, parameterExpression);
MethodCallExpression selectExpression = Expression.Call(
typeof(Queryable),
"Select",
typeArguments,
constantExpression,
lambdaExpression);
// create query from expression
var selectQuery = objectSet.CreateQuery(selectExpression, entityMap.EntityType);
string sql = selectQuery.ToTraceString();
// parse select part of sql to use as update
string regex = @"SELECT\s*\r\n\s*(?<ColumnValue>.+)?\s*AS\s*(?<ColumnAlias>\[\w+\])\r\n\s*FROM\s*(?<TableName>\[\w+\]\.\[\w+\]|\[\w+\])\s*AS\s*(?<TableAlias>\[\w+\])";
Match match = Regex.Match(sql, regex);
if (!match.Success)
throw new ArgumentException("The MemberAssignment expression could not be processed.", "updateExpression");
string value = match.Groups["ColumnValue"].Value;
string alias = match.Groups["TableAlias"].Value;
value = value.Replace(alias + ".", "");
foreach (ObjectParameter objectParameter in selectQuery.Parameters)
{
string parameterName = "p__update__" + nameCount++;
var parameter = updateCommand.CreateParameter();
parameter.ParameterName = parameterName;
parameter.Value = objectParameter.Value ?? DBNull.Value;
updateCommand.Parameters.Add(parameter);
value = value.Replace(objectParameter.Name, parameterName);
}
sqlBuilder.AppendFormat("[{0}] = {1}", columnName, value);
}
wroteSet = true;
}
sqlBuilder.AppendLine(" ");
sqlBuilder.AppendFormat("FROM {0} AS j0 INNER JOIN (", entityMap.TableName);
sqlBuilder.AppendLine();
sqlBuilder.AppendLine(innerSelect);
sqlBuilder.Append(") AS j1 ON (");
bool wroteKey = false;
foreach (var keyMap in entityMap.KeyMaps)
{
if (wroteKey)
sqlBuilder.Append(" AND ");
sqlBuilder.AppendFormat("j0.[{0}] = j1.[{0}]", keyMap.ColumnName);
wroteKey = true;
}
sqlBuilder.Append(")");
updateCommand.CommandText = sqlBuilder.ToString();
#if NET45
int result = async
? await updateCommand.ExecuteNonQueryAsync()
: updateCommand.ExecuteNonQuery();
#else
int result = updateCommand.ExecuteNonQuery();
#endif
// only commit if created transaction
if (ownTransaction)
updateTransaction.Commit();
return result;
}
finally
{
if (updateCommand != null)
updateCommand.Dispose();
if (updateTransaction != null && ownTransaction)
updateTransaction.Dispose();
if (updateConnection != null && ownConnection)
updateConnection.Close();
}
}
private static Tuple<DbConnection, DbTransaction> GetStore(ObjectContext objectContext)
{
// TODO, re-eval if this is needed
DbConnection dbConnection = objectContext.Connection;
var entityConnection = dbConnection as EntityConnection;
// by-pass entity connection
if (entityConnection == null)
return new Tuple<DbConnection, DbTransaction>(dbConnection, null);
DbConnection connection = entityConnection.StoreConnection;
// get internal transaction
dynamic connectionProxy = new DynamicProxy(entityConnection);
dynamic entityTransaction = connectionProxy.CurrentTransaction;
if (entityTransaction == null)
return new Tuple<DbConnection, DbTransaction>(connection, null);
DbTransaction transaction = entityTransaction.StoreTransaction;
return new Tuple<DbConnection, DbTransaction>(connection, transaction);
}
private static string GetSelectSql<TEntity>(ObjectQuery<TEntity> query, EntityMap entityMap, DbCommand command)
where TEntity : class
{
// TODO change to esql?
// changing query to only select keys
var selector = new StringBuilder(50);
selector.Append("new(");
foreach (var propertyMap in entityMap.KeyMaps)
{
if (selector.Length > 4)
selector.Append((", "));
selector.Append(propertyMap.PropertyName);
}
selector.Append(")");
var selectQuery = DynamicQueryable.Select(query, selector.ToString());
var objectQuery = selectQuery as ObjectQuery;
if (objectQuery == null)
throw new ArgumentException("The query must be of type ObjectQuery.", "query");
string innerJoinSql = objectQuery.ToTraceString();
// create parameters
foreach (var objectParameter in objectQuery.Parameters)
{
var parameter = command.CreateParameter();
parameter.ParameterName = objectParameter.Name;
parameter.Value = objectParameter.Value ?? DBNull.Value;
command.Parameters.Add(parameter);
}
return innerJoinSql;
}
}
}