Repository navigation
Expand file tree
/
Copy pathDapperImplementor.cs
More file actions
478 lines (414 loc) · 24 KB
/
Copy pathDapperImplementor.cs
File metadata and controls
478 lines (414 loc) · 24 KB
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
using System;
using System.Collections.Generic;
using System.Data;
using System.Dynamic;
using System.Linq;
using System.Reflection;
using System.Text;
using Dapper;
using DapperExtensions.Mapper;
using DapperExtensions.Sql;
namespace DapperExtensions
{
public interface IDapperImplementor
{
ISqlGenerator SqlGenerator { get; }
T Get<T>(IDbConnection connection, dynamic id, IDbTransaction transaction, int? commandTimeout) where T : class;
void Insert<T>(IDbConnection connection, IEnumerable<T> entities, IDbTransaction transaction, int? commandTimeout) where T : class;
dynamic Insert<T>(IDbConnection connection, T entity, IDbTransaction transaction, int? commandTimeout) where T : class;
bool Update<T>(IDbConnection connection, T entity, IDbTransaction transaction, int? commandTimeout, bool ignoreAllKeyProperties) where T : class;
bool Delete<T>(IDbConnection connection, T entity, IDbTransaction transaction, int? commandTimeout) where T : class;
bool Delete<T>(IDbConnection connection, object predicate, IDbTransaction transaction, int? commandTimeout) where T : class;
IEnumerable<T> GetList<T>(IDbConnection connection, object predicate, IList<ISort> sort, IDbTransaction transaction, int? commandTimeout, bool buffered) where T : class;
IEnumerable<T> GetPage<T>(IDbConnection connection, object predicate, IList<ISort> sort, int page, int resultsPerPage, IDbTransaction transaction, int? commandTimeout, bool buffered) where T : class;
IEnumerable<T> GetSet<T>(IDbConnection connection, object predicate, IList<ISort> sort, int firstResult, int maxResults, IDbTransaction transaction, int? commandTimeout, bool buffered) where T : class;
int Count<T>(IDbConnection connection, object predicate, IDbTransaction transaction, int? commandTimeout) where T : class;
IMultipleResultReader GetMultiple(IDbConnection connection, GetMultiplePredicate predicate, IDbTransaction transaction, int? commandTimeout);
}
public class DapperImplementor : IDapperImplementor
{
public DapperImplementor(ISqlGenerator sqlGenerator)
{
SqlGenerator = sqlGenerator;
}
public ISqlGenerator SqlGenerator { get; private set; }
public T Get<T>(IDbConnection connection, dynamic id, IDbTransaction transaction, int? commandTimeout) where T : class
{
IClassMapper classMap = SqlGenerator.Configuration.GetMap<T>();
IPredicate predicate = GetIdPredicate(classMap, id);
T result = GetList<T>(connection, classMap, predicate, null, transaction, commandTimeout, true).SingleOrDefault();
return result;
}
public void Insert<T>(IDbConnection connection, IEnumerable<T> entities, IDbTransaction transaction, int? commandTimeout) where T : class
{
IEnumerable<PropertyInfo> properties = null;
IClassMapper classMap = SqlGenerator.Configuration.GetMap<T>();
var notKeyProperties = classMap.Properties.Where(p => p.KeyType != KeyType.NotAKey);
var triggerIdentityColumn = classMap.Properties.SingleOrDefault(p => p.KeyType == KeyType.TriggerIdentity);
var parameters = new List<DynamicParameters>();
if (triggerIdentityColumn != null)
{
properties = typeof (T).GetProperties(BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public)
.Where(p => p.Name != triggerIdentityColumn.PropertyInfo.Name);
}
foreach (var e in entities)
{
foreach (var column in notKeyProperties)
{
if (column.KeyType == KeyType.Guid && (Guid)column.PropertyInfo.GetValue(e, null) == Guid.Empty)
{
Guid comb = SqlGenerator.Configuration.GetNextGuid();
column.PropertyInfo.SetValue(e, comb, null);
}
}
if (triggerIdentityColumn != null)
{
var dynamicParameters = new DynamicParameters();
foreach (var prop in properties)
{
dynamicParameters.Add(prop.Name, prop.GetValue(e, null));
}
// defaultValue need for identify type of parameter
var defaultValue = typeof(T).GetProperty(triggerIdentityColumn.PropertyInfo.Name).GetValue(e, null);
dynamicParameters.Add("IdOutParam", direction: ParameterDirection.Output, value: defaultValue);
parameters.Add(dynamicParameters);
}
}
string sql = SqlGenerator.Insert(classMap);
if (triggerIdentityColumn == null)
{
connection.Execute(sql, entities, transaction, commandTimeout, CommandType.Text);
}
else
{
connection.Execute(sql, parameters, transaction, commandTimeout, CommandType.Text);
}
}
public dynamic Insert<T>(IDbConnection connection, T entity, IDbTransaction transaction, int? commandTimeout) where T : class
{
IClassMapper classMap = SqlGenerator.Configuration.GetMap<T>();
List<IPropertyMap> nonIdentityKeyProperties = classMap.Properties.Where(p => p.KeyType == KeyType.Guid || p.KeyType == KeyType.Assigned).ToList();
var identityColumn = classMap.Properties.SingleOrDefault(p => p.KeyType == KeyType.Identity);
var triggerIdentityColumn = classMap.Properties.SingleOrDefault(p => p.KeyType == KeyType.TriggerIdentity);
foreach (var column in nonIdentityKeyProperties)
{
if (column.KeyType == KeyType.Guid && (Guid)column.PropertyInfo.GetValue(entity, null) == Guid.Empty)
{
Guid comb = SqlGenerator.Configuration.GetNextGuid();
column.PropertyInfo.SetValue(entity, comb, null);
}
}
IDictionary<string, object> keyValues = new ExpandoObject();
string sql = SqlGenerator.Insert(classMap);
if (identityColumn != null)
{
IEnumerable<long> result;
if (SqlGenerator.SupportsMultipleStatements())
{
sql += SqlGenerator.Configuration.Dialect.BatchSeperator + SqlGenerator.IdentitySql(classMap);
result = connection.Query<long>(sql, entity, transaction, false, commandTimeout, CommandType.Text);
}
else
{
connection.Execute(sql, entity, transaction, commandTimeout, CommandType.Text);
sql = SqlGenerator.IdentitySql(classMap);
result = connection.Query<long>(sql, entity, transaction, false, commandTimeout, CommandType.Text);
}
// We are only interested in the first identity, but we are iterating over all resulting items (if any).
// This makes sure that ADO.NET drivers (like MySql) won't actively terminate the query.
bool hasResult = false;
int identityInt = 0;
foreach (var identityValue in result)
{
if (hasResult)
{
continue;
}
identityInt = Convert.ToInt32(identityValue);
hasResult = true;
}
if (!hasResult)
{
throw new InvalidOperationException("The source sequence is empty.");
}
keyValues.Add(identityColumn.Name, identityInt);
identityColumn.PropertyInfo.SetValue(entity, identityInt, null);
}
else if (triggerIdentityColumn != null)
{
var dynamicParameters = new DynamicParameters();
foreach (var prop in entity.GetType().GetProperties(BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public)
.Where(p => p.Name != triggerIdentityColumn.PropertyInfo.Name))
{
dynamicParameters.Add(prop.Name, prop.GetValue(entity, null));
}
// defaultValue need for identify type of parameter
var defaultValue = entity.GetType().GetProperty(triggerIdentityColumn.PropertyInfo.Name).GetValue(entity, null);
dynamicParameters.Add("IdOutParam", direction: ParameterDirection.Output, value: defaultValue);
connection.Execute(sql, dynamicParameters, transaction, commandTimeout, CommandType.Text);
var value = dynamicParameters.Get<object>(SqlGenerator.Configuration.Dialect.ParameterPrefix + "IdOutParam");
keyValues.Add(triggerIdentityColumn.Name, value);
triggerIdentityColumn.PropertyInfo.SetValue(entity, value, null);
}
else
{
connection.Execute(sql, entity, transaction, commandTimeout, CommandType.Text);
}
foreach (var column in nonIdentityKeyProperties)
{
keyValues.Add(column.Name, column.PropertyInfo.GetValue(entity, null));
}
if (keyValues.Count == 1)
{
return keyValues.First().Value;
}
return keyValues;
}
public bool Update<T>(IDbConnection connection, T entity, IDbTransaction transaction, int? commandTimeout, bool ignoreAllKeyProperties = false) where T : class
{
IClassMapper classMap = SqlGenerator.Configuration.GetMap<T>();
IPredicate predicate = GetKeyPredicate<T>(classMap, entity);
Dictionary<string, object> parameters = new Dictionary<string, object>();
string sql = SqlGenerator.Update(classMap, predicate, parameters, ignoreAllKeyProperties);
DynamicParameters dynamicParameters = new DynamicParameters();
var columns = ignoreAllKeyProperties
? classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly) && p.KeyType == KeyType.NotAKey)
: classMap.Properties.Where(p => !(p.Ignored || p.IsReadOnly || p.KeyType == KeyType.Identity || p.KeyType == KeyType.Assigned));
foreach (var property in ReflectionHelper.GetObjectValues(entity).Where(property => columns.Any(c => c.Name == property.Key)))
{
dynamicParameters.Add(property.Key, property.Value);
}
foreach (var parameter in parameters)
{
dynamicParameters.Add(parameter.Key, parameter.Value);
}
return connection.Execute(sql, dynamicParameters, transaction, commandTimeout, CommandType.Text) > 0;
}
public bool Delete<T>(IDbConnection connection, T entity, IDbTransaction transaction, int? commandTimeout) where T : class
{
IClassMapper classMap = SqlGenerator.Configuration.GetMap<T>();
IPredicate predicate = GetKeyPredicate<T>(classMap, entity);
return Delete<T>(connection, classMap, predicate, transaction, commandTimeout);
}
public bool Delete<T>(IDbConnection connection, object predicate, IDbTransaction transaction, int? commandTimeout) where T : class
{
IClassMapper classMap = SqlGenerator.Configuration.GetMap<T>();
IPredicate wherePredicate = GetPredicate(classMap, predicate);
return Delete<T>(connection, classMap, wherePredicate, transaction, commandTimeout);
}
public IEnumerable<T> GetList<T>(IDbConnection connection, object predicate, IList<ISort> sort, IDbTransaction transaction, int? commandTimeout, bool buffered) where T : class
{
IClassMapper classMap = SqlGenerator.Configuration.GetMap<T>();
IPredicate wherePredicate = GetPredicate(classMap, predicate);
return GetList<T>(connection, classMap, wherePredicate, sort, transaction, commandTimeout, buffered);
}
public IEnumerable<T> GetPage<T>(IDbConnection connection, object predicate, IList<ISort> sort, int page, int resultsPerPage, IDbTransaction transaction, int? commandTimeout, bool buffered) where T : class
{
IClassMapper classMap = SqlGenerator.Configuration.GetMap<T>();
IPredicate wherePredicate = GetPredicate(classMap, predicate);
return GetPage<T>(connection, classMap, wherePredicate, sort, page, resultsPerPage, transaction, commandTimeout, buffered);
}
public IEnumerable<T> GetSet<T>(IDbConnection connection, object predicate, IList<ISort> sort, int firstResult, int maxResults, IDbTransaction transaction, int? commandTimeout, bool buffered) where T : class
{
IClassMapper classMap = SqlGenerator.Configuration.GetMap<T>();
IPredicate wherePredicate = GetPredicate(classMap, predicate);
return GetSet<T>(connection, classMap, wherePredicate, sort, firstResult, maxResults, transaction, commandTimeout, buffered);
}
public int Count<T>(IDbConnection connection, object predicate, IDbTransaction transaction, int? commandTimeout) where T : class
{
IClassMapper classMap = SqlGenerator.Configuration.GetMap<T>();
IPredicate wherePredicate = GetPredicate(classMap, predicate);
Dictionary<string, object> parameters = new Dictionary<string, object>();
string sql = SqlGenerator.Count(classMap, wherePredicate, parameters);
DynamicParameters dynamicParameters = new DynamicParameters();
foreach (var parameter in parameters)
{
dynamicParameters.Add(parameter.Key, parameter.Value);
}
return (int)connection.Query(sql, dynamicParameters, transaction, false, commandTimeout, CommandType.Text).Single().Total;
}
public IMultipleResultReader GetMultiple(IDbConnection connection, GetMultiplePredicate predicate, IDbTransaction transaction, int? commandTimeout)
{
if (SqlGenerator.SupportsMultipleStatements())
{
return GetMultipleByBatch(connection, predicate, transaction, commandTimeout);
}
return GetMultipleBySequence(connection, predicate, transaction, commandTimeout);
}
protected IEnumerable<T> GetList<T>(IDbConnection connection, IClassMapper classMap, IPredicate predicate, IList<ISort> sort, IDbTransaction transaction, int? commandTimeout, bool buffered) where T : class
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
string sql = SqlGenerator.Select(classMap, predicate, sort, parameters);
DynamicParameters dynamicParameters = new DynamicParameters();
foreach (var parameter in parameters)
{
dynamicParameters.Add(parameter.Key, parameter.Value);
}
return connection.Query<T>(sql, dynamicParameters, transaction, buffered, commandTimeout, CommandType.Text);
}
protected IEnumerable<T> GetPage<T>(IDbConnection connection, IClassMapper classMap, IPredicate predicate, IList<ISort> sort, int page, int resultsPerPage, IDbTransaction transaction, int? commandTimeout, bool buffered) where T : class
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
string sql = SqlGenerator.SelectPaged(classMap, predicate, sort, page, resultsPerPage, parameters);
DynamicParameters dynamicParameters = new DynamicParameters();
foreach (var parameter in parameters)
{
dynamicParameters.Add(parameter.Key, parameter.Value);
}
return connection.Query<T>(sql, dynamicParameters, transaction, buffered, commandTimeout, CommandType.Text);
}
protected IEnumerable<T> GetSet<T>(IDbConnection connection, IClassMapper classMap, IPredicate predicate, IList<ISort> sort, int firstResult, int maxResults, IDbTransaction transaction, int? commandTimeout, bool buffered) where T : class
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
string sql = SqlGenerator.SelectSet(classMap, predicate, sort, firstResult, maxResults, parameters);
DynamicParameters dynamicParameters = new DynamicParameters();
foreach (var parameter in parameters)
{
dynamicParameters.Add(parameter.Key, parameter.Value);
}
return connection.Query<T>(sql, dynamicParameters, transaction, buffered, commandTimeout, CommandType.Text);
}
protected bool Delete<T>(IDbConnection connection, IClassMapper classMap, IPredicate predicate, IDbTransaction transaction, int? commandTimeout) where T : class
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
string sql = SqlGenerator.Delete(classMap, predicate, parameters);
DynamicParameters dynamicParameters = new DynamicParameters();
foreach (var parameter in parameters)
{
dynamicParameters.Add(parameter.Key, parameter.Value);
}
return connection.Execute(sql, dynamicParameters, transaction, commandTimeout, CommandType.Text) > 0;
}
protected IPredicate GetPredicate(IClassMapper classMap, object predicate)
{
IPredicate wherePredicate = predicate as IPredicate;
if (wherePredicate == null && predicate != null)
{
wherePredicate = GetEntityPredicate(classMap, predicate);
}
return wherePredicate;
}
protected IPredicate GetIdPredicate(IClassMapper classMap, object id)
{
bool isSimpleType = ReflectionHelper.IsSimpleType(id.GetType());
var keys = classMap.Properties.Where(p => p.KeyType != KeyType.NotAKey);
IDictionary<string, object> paramValues = null;
IList<IPredicate> predicates = new List<IPredicate>();
if (!isSimpleType)
{
paramValues = ReflectionHelper.GetObjectValues(id);
}
foreach (var key in keys)
{
object value = id;
if (!isSimpleType)
{
value = paramValues[key.Name];
}
Type predicateType = typeof(FieldPredicate<>).MakeGenericType(classMap.EntityType);
IFieldPredicate fieldPredicate = Activator.CreateInstance(predicateType) as IFieldPredicate;
fieldPredicate.Not = false;
fieldPredicate.Operator = Operator.Eq;
fieldPredicate.PropertyName = key.Name;
fieldPredicate.Value = value;
predicates.Add(fieldPredicate);
}
return predicates.Count == 1
? predicates[0]
: new PredicateGroup
{
Operator = GroupOperator.And,
Predicates = predicates
};
}
protected IPredicate GetKeyPredicate<T>(IClassMapper classMap, T entity) where T : class
{
var whereFields = classMap.Properties.Where(p => p.KeyType != KeyType.NotAKey);
if (!whereFields.Any())
{
throw new ArgumentException("At least one Key column must be defined.");
}
IList<IPredicate> predicates = (from field in whereFields
select new FieldPredicate<T>
{
Not = false,
Operator = Operator.Eq,
PropertyName = field.Name,
Value = field.PropertyInfo.GetValue(entity, null)
}).Cast<IPredicate>().ToList();
return predicates.Count == 1
? predicates[0]
: new PredicateGroup
{
Operator = GroupOperator.And,
Predicates = predicates
};
}
protected IPredicate GetEntityPredicate(IClassMapper classMap, object entity)
{
Type predicateType = typeof(FieldPredicate<>).MakeGenericType(classMap.EntityType);
IList<IPredicate> predicates = new List<IPredicate>();
foreach (var kvp in ReflectionHelper.GetObjectValues(entity))
{
IFieldPredicate fieldPredicate = Activator.CreateInstance(predicateType) as IFieldPredicate;
fieldPredicate.Not = false;
fieldPredicate.Operator = Operator.Eq;
fieldPredicate.PropertyName = kvp.Key;
fieldPredicate.Value = kvp.Value;
predicates.Add(fieldPredicate);
}
return predicates.Count == 1
? predicates[0]
: new PredicateGroup
{
Operator = GroupOperator.And,
Predicates = predicates
};
}
protected GridReaderResultReader GetMultipleByBatch(IDbConnection connection, GetMultiplePredicate predicate, IDbTransaction transaction, int? commandTimeout)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
StringBuilder sql = new StringBuilder();
foreach (var item in predicate.Items)
{
IClassMapper classMap = SqlGenerator.Configuration.GetMap(item.Type);
IPredicate itemPredicate = item.Value as IPredicate;
if (itemPredicate == null && item.Value != null)
{
itemPredicate = GetPredicate(classMap, item.Value);
}
sql.AppendLine(SqlGenerator.Select(classMap, itemPredicate, item.Sort, parameters) + SqlGenerator.Configuration.Dialect.BatchSeperator);
}
DynamicParameters dynamicParameters = new DynamicParameters();
foreach (var parameter in parameters)
{
dynamicParameters.Add(parameter.Key, parameter.Value);
}
SqlMapper.GridReader grid = connection.QueryMultiple(sql.ToString(), dynamicParameters, transaction, commandTimeout, CommandType.Text);
return new GridReaderResultReader(grid);
}
protected SequenceReaderResultReader GetMultipleBySequence(IDbConnection connection, GetMultiplePredicate predicate, IDbTransaction transaction, int? commandTimeout)
{
IList<SqlMapper.GridReader> items = new List<SqlMapper.GridReader>();
foreach (var item in predicate.Items)
{
Dictionary<string, object> parameters = new Dictionary<string, object>();
IClassMapper classMap = SqlGenerator.Configuration.GetMap(item.Type);
IPredicate itemPredicate = item.Value as IPredicate;
if (itemPredicate == null && item.Value != null)
{
itemPredicate = GetPredicate(classMap, item.Value);
}
string sql = SqlGenerator.Select(classMap, itemPredicate, item.Sort, parameters);
DynamicParameters dynamicParameters = new DynamicParameters();
foreach (var parameter in parameters)
{
dynamicParameters.Add(parameter.Key, parameter.Value);
}
SqlMapper.GridReader queryResult = connection.QueryMultiple(sql, dynamicParameters, transaction, commandTimeout, CommandType.Text);
items.Add(queryResult);
}
return new SequenceReaderResultReader(items);
}
}
}