-
Notifications
You must be signed in to change notification settings - Fork 1
/
DapperExtensions.cs
460 lines (408 loc) · 22.9 KB
/
DapperExtensions.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
454
455
456
457
458
459
460
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using DapperExtensions.Sql;
using DapperExtensions.Mapper;
using System.Threading.Tasks;
namespace DapperExtensions
{
public static class DapperExtensions
{
private readonly static object _lock = new object();
private static Func<IDapperExtensionsConfiguration, IDapperImplementor> _instanceFactory;
private static IDapperImplementor _instance;
private static IDapperExtensionsConfiguration _configuration;
/// <summary>
/// Gets or sets the default class mapper to use when generating class maps. If not specified, AutoClassMapper<T> is used.
/// DapperExtensions.Configure(Type, IList<Assembly>, ISqlDialect) can be used instead to set all values at once
/// </summary>
public static Type DefaultMapper
{
get
{
return _configuration.DefaultMapper;
}
set
{
Configure(value, _configuration.MappingAssemblies, _configuration.Dialect);
}
}
/// <summary>
/// Gets or sets the type of sql to be generated.
/// DapperExtensions.Configure(Type, IList<Assembly>, ISqlDialect) can be used instead to set all values at once
/// </summary>
public static ISqlDialect SqlDialect
{
get
{
return _configuration.Dialect;
}
set
{
Configure(_configuration.DefaultMapper, _configuration.MappingAssemblies, value);
}
}
/// <summary>
/// Get or sets the Dapper Extensions Implementation Factory.
/// </summary>
public static Func<IDapperExtensionsConfiguration, IDapperImplementor> InstanceFactory
{
get
{
if (_instanceFactory == null)
{
_instanceFactory = config => new DapperImplementor(new SqlGeneratorImpl(config));
}
return _instanceFactory;
}
set
{
_instanceFactory = value;
Configure(_configuration.DefaultMapper, _configuration.MappingAssemblies, _configuration.Dialect);
}
}
/// <summary>
/// Gets the Dapper Extensions Implementation
/// </summary>
private static IDapperImplementor Instance
{
get
{
if (_instance == null)
{
lock (_lock)
{
if (_instance == null)
{
_instance = InstanceFactory(_configuration);
}
}
}
return _instance;
}
}
static DapperExtensions()
{
Configure(typeof(AutoClassMapper<>), new List<Assembly>(), new SqlServerDialect());
}
/// <summary>
/// Add other assemblies that Dapper Extensions will search if a mapping is not found in the same assembly of the POCO.
/// </summary>
/// <param name="assemblies"></param>
public static void SetMappingAssemblies(IList<Assembly> assemblies)
{
Configure(_configuration.DefaultMapper, assemblies, _configuration.Dialect);
}
/// <summary>
/// Configure DapperExtensions extension methods.
/// </summary>
/// <param name="defaultMapper"></param>
/// <param name="mappingAssemblies"></param>
/// <param name="sqlDialect"></param>
public static void Configure(IDapperExtensionsConfiguration configuration)
{
_instance = null;
_configuration = configuration;
}
/// <summary>
/// Configure DapperExtensions extension methods.
/// </summary>
/// <param name="defaultMapper"></param>
/// <param name="mappingAssemblies"></param>
/// <param name="sqlDialect"></param>
public static void Configure(Type defaultMapper, IList<Assembly> mappingAssemblies, ISqlDialect sqlDialect)
{
Configure(new DapperExtensionsConfiguration(defaultMapper, mappingAssemblies, sqlDialect));
}
#region 增删改
/// <summary>
/// Executes an insert query for the specified entity.
/// </summary>
public static int Inserts<T>(this IDbConnection connection, IEnumerable<T> entities, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return Instance.Insert<T>(connection, entities, transaction, commandTimeout);
}
/// <summary>
/// Executes an insert query for the specified entity, returning the primary key.
/// If the entity has a single key, just the value is returned.
/// If the entity has a composite key, an IDictionary<string, object> is returned with the key values.
/// The key value for the entity will also be updated if the KeyType is a Guid or Identity.
/// </summary>
public static dynamic Insert<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return Instance.Insert<T>(connection, entity, transaction, commandTimeout);
}
/// <summary>
/// Executes an insert query for the specified entity.
/// </summary>
public async static Task<int> InsertsAsync<T>(this IDbConnection connection, IEnumerable<T> entities, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return await Instance.InsertAsync<T>(connection, entities, transaction, commandTimeout);
}
/// <summary>
/// Executes an insert query for the specified entity, returning the primary key.
/// If the entity has a single key, just the value is returned.
/// If the entity has a composite key, an IDictionary<string, object> is returned with the key values.
/// The key value for the entity will also be updated if the KeyType is a Guid or Identity.
/// </summary>
public static async Task<dynamic> InsertAsync<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return await Instance.InsertAsync<T>(connection, entity, transaction, commandTimeout);
}
/// <summary>
/// Executes an update query for the specified entity.
/// </summary>
public static bool Update<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null, bool ignoreAllKeyProperties = false) where T : class
{
return Instance.Update<T>(connection, entity, transaction, commandTimeout, ignoreAllKeyProperties);
}
/// <summary>
/// Executes an update query for the specified entity.
/// </summary>
public static async Task<bool> UpdateAsync<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null, bool ignoreAllKeyProperties = false) where T : class
{
return await Instance.UpdateAsync<T>(connection, entity, transaction, commandTimeout, ignoreAllKeyProperties);
}
/// <summary>
/// Executes a delete query for the specified entity.
/// </summary>
public static int Delete<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return Instance.Delete<T>(connection, entity, transaction, commandTimeout);
}
/// <summary>
/// Executes a delete query using the specified predicate.
/// </summary>
public static int Delete<T>(this IDbConnection connection, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return Instance.Delete<T>(connection, predicate, transaction, commandTimeout);
}
/// <summary>
/// Executes a delete query for the specified entity.
/// </summary>
public static async Task<int> DeleteAsync<T>(this IDbConnection connection, T entity, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return await Instance.DeleteAsync<T>(connection, entity, transaction, commandTimeout);
}
/// <summary>
/// Executes a delete query using the specified predicate.
/// </summary>
public static async Task<int> DeleteAsync<T>(this IDbConnection connection, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return await Instance.DeleteAsync<T>(connection, predicate, transaction, commandTimeout);
}
#endregion
#region Get
/// <summary>
/// Executes a query for the specified id, returning the data typed as per T
/// </summary>
public static T Get<T>(this IDbConnection connection, dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
var result = Instance.Get<T>(connection, id, transaction, commandTimeout);
return (T)result;
}
/// <summary>
/// Executes a query for the specified id, returning the data typed as per T
/// </summary>
public static async Task<T> GetAsync<T>(this IDbConnection connection, dynamic id, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
var result =await Instance.GetAsync<T>(connection, id, transaction, commandTimeout);
return result;
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// </summary>
public static IEnumerable<T> GetList<T>(this IDbConnection connection, object predicate = null, IList<ISort> sort = null, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where T : class
{
return Instance.GetList<T>(connection, predicate, sort, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// </summary>
public async static Task<IEnumerable<T>> GetListAsync<T>(this IDbConnection connection, object predicate = null, IList<ISort> sort = null, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return await Instance.GetListAsync<T>(connection, predicate, sort, transaction, commandTimeout);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// </summary>
public static IEnumerable<T> GetListWithColumns<T>(this IDbConnection connection, IEnumerable<string> columnNames = null, object predicate = null, IList<ISort> sort = null, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where T : class
{
return Instance.GetList<T>(connection, columnNames, predicate, sort, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// </summary>
public static async Task<IEnumerable<T>> GetListWithColumnsAsync<T>(this IDbConnection connection, IEnumerable<string> columnNames = null, object predicate = null, IList<ISort> sort = null, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where T : class
{
return await Instance.GetListAsync<T>(connection, columnNames, predicate, sort, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// Data returned is dependent upon the specified page and resultsPerPage.
/// </summary>
public async static Task<(IEnumerable<T>, long)> GetPageAsync<T>(this IDbConnection connection, object predicate, IList<ISort> sort, int page, int resultsPerPage, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return await Instance.GetPageAsync<T>(connection, predicate, sort, page, resultsPerPage, transaction, commandTimeout);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// Data returned is dependent upon the specified page and resultsPerPage.
/// </summary>
public static (IEnumerable<T>, long) GetPage<T>(this IDbConnection connection, object predicate, IList<ISort> sort, int page, int resultsPerPage, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where T : class
{
return Instance.GetPage<T>(connection, predicate, sort, page, resultsPerPage, transaction, commandTimeout, buffered);
}
/// <summary>
/// Executes a select query using the specified predicate, returning an IEnumerable data typed as per T.
/// Data returned is dependent upon the specified firstResult and maxResults.
/// </summary>
public static IEnumerable<T> GetSet<T>(this IDbConnection connection, object predicate, IList<ISort> sort, int firstResult, int maxResults, IDbTransaction transaction = null, int? commandTimeout = null, bool buffered = false) where T : class
{
return Instance.GetSet<T>(connection, predicate, sort, firstResult, maxResults, transaction, commandTimeout, buffered);
}
/// <summary>
/// 多表关联查询,此查询会扫描Tmain的外键属性ForeignKey,执行关联查询,查询并映射关联属性。
/// </summary>
/// <typeparam name="TMain">主表类型</typeparam>
/// <typeparam name="TRel1">外键对象1</typeparam>
/// <typeparam name="TRel2">外键对象2</typeparam>
/// <typeparam name="TRel3">外键对象3</typeparam>
/// <typeparam name="TRel4">外键对象4</typeparam>
/// <typeparam name="TRel5">外键对象5</typeparam>
/// <param name="connection">数据库连接</param>
/// <param name="func">用于主表和子表对象组合的委托</param>
/// <param name="predicate">主表条件</param>
/// <param name="sort">主表排序</param>
/// <param name="transaction">事务</param>
/// <param name="buffered">是否使用缓存</param>
/// <param name="commandTimeout">连接超时时间</param>
/// <returns>IEnumerable`TMain</returns>
public static async Task<IEnumerable<TMain>> QueryRelationalAsync<TMain, TRel1, TRel2, TRel3, TRel4, TRel5>(IDbConnection connection, Func<TMain, TRel1, TRel2, TRel3, TRel4, TRel5, TMain> func, object predicate, IList<ISort> sort, IDbTransaction transaction=null, bool buffered=true, int? commandTimeout=180) where TMain : class
{
return await Instance.QueryRelationalAsync(connection, func, predicate, sort, transaction, buffered, commandTimeout);
}
public static async Task<IEnumerable<TMain>> QueryRelationalAsync<TMain, TRel1, TRel2, TRel3, TRel4>(IDbConnection connection, Func<TMain, TRel1, TRel2, TRel3, TRel4, TMain> func, object predicate, IList<ISort> sort, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = 180) where TMain : class
{
return await Instance.QueryRelationalAsync(connection, func, predicate, sort, transaction, buffered, commandTimeout);
}
public static async Task<IEnumerable<TMain>> QueryRelationalAsync<TMain, TRel1, TRel2, TRel3>(IDbConnection connection, Func<TMain, TRel1, TRel2, TRel3, TMain> func, object predicate, IList<ISort> sort, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = 180) where TMain : class
{
return await Instance.QueryRelationalAsync(connection, func, predicate, sort, transaction, buffered, commandTimeout);
}
public static async Task<IEnumerable<TMain>> QueryRelationalAsync<TMain, TRel1, TRel2>(IDbConnection connection, Func<TMain, TRel1, TRel2, TMain> func, object predicate, IList<ISort> sort, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = 180) where TMain : class
{
return await Instance.QueryRelationalAsync(connection, func, predicate, sort, transaction, buffered, commandTimeout);
}
public static async Task<IEnumerable<TMain>> QueryRelationalAsync<TMain, TRel1>(IDbConnection connection, Func<TMain, TRel1, TMain> func, object predicate, IList<ISort> sort, IDbTransaction transaction = null, bool buffered = true, int? commandTimeout = 180) where TMain : class
{
return await Instance.QueryRelationalAsync(connection, func, predicate, sort, transaction, buffered, commandTimeout);
}
#endregion
#region count max min sum avg
/// <summary>
/// Executes a query using the specified predicate, returning an integer that represents the number of rows that match the query.
/// </summary>
public static int Count<T>(this IDbConnection connection, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return Instance.Count<T>(connection, predicate, transaction, commandTimeout);
}
/// <summary>
/// 求某列最大值
/// </summary>
public static TValue Max<TValue, TEntity>(this IDbConnection connection, string attrName, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where TEntity : class
{
return Instance.Max<TValue,TEntity>(connection, attrName, transaction, predicate, commandTimeout);
}
/// <summary>
/// 求某列最小值
/// </summary>
public static TValue Min<TValue, TEntity>(this IDbConnection connection, string attrName, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where TEntity : class
{
return Instance.Min<TValue, TEntity>(connection, attrName, transaction, predicate, commandTimeout);
}
/// <summary>
/// 求某列和
/// </summary>
public static TValue Sum<TValue, TEntity>(this IDbConnection connection, string attrName, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where TEntity : class
{
return Instance.Sum<TValue, TEntity>(connection, attrName, transaction, predicate, commandTimeout);
}
/// <summary>
/// 求某列平均值。
/// </summary>
public static TValue AVG<TValue, TEntity>(this IDbConnection connection, string attrName, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where TEntity : class
{
return Instance.AVG<TValue, TEntity>(connection, attrName, transaction, predicate, commandTimeout);
}
/// <summary>
/// 求某列最大值
/// </summary>
public static async Task<TValue> MaxAsync<TValue, TEntity>(this IDbConnection connection, string attrName, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where TEntity : class
{
return await Instance.MaxAsync<TValue, TEntity>(connection, attrName, transaction, predicate, commandTimeout);
}
/// <summary>
/// Executes a query using the specified predicate, returning an integer that represents the number of rows that match the query.
/// </summary>
public async static Task<int> CountAsync<T>(this IDbConnection connection, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where T : class
{
return await Instance.CountAsync<T>(connection, predicate, transaction, commandTimeout);
}
/// <summary>
/// 求某列最小值
/// </summary>
public static async Task<TValue> MinAsync<TValue, TEntity>(this IDbConnection connection, string attrName, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where TEntity : class
{
return await Instance.MinAsync<TValue, TEntity>(connection, attrName, transaction, predicate, commandTimeout);
}
/// <summary>
/// 求某列和
/// </summary>
public static async Task<TValue> SumAsync<TValue, TEntity>(this IDbConnection connection, string attrName, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where TEntity : class
{
return await Instance.SumAsync<TValue, TEntity>(connection, attrName, transaction, predicate, commandTimeout);
}
/// <summary>
/// 求某列平均值。
/// </summary>
public static async Task<TValue> AVGAsync<TValue, TEntity>(this IDbConnection connection, string attrName, object predicate, IDbTransaction transaction = null, int? commandTimeout = null) where TEntity : class
{
return await Instance.AVGAsync<TValue, TEntity>(connection, attrName, transaction, predicate, commandTimeout);
}
#endregion
/// <summary>
/// Executes a select query for multiple objects, returning IMultipleResultReader for each predicate.
/// </summary>
public static IMultipleResultReader GetMultiple(this IDbConnection connection, GetMultiplePredicate predicate, IDbTransaction transaction = null, int? commandTimeout = null)
{
return Instance.GetMultiple(connection, predicate, transaction, commandTimeout);
}
/// <summary>
/// Gets the appropriate mapper for the specified type T.
/// If the mapper for the type is not yet created, a new mapper is generated from the mapper type specifed by DefaultMapper.
/// </summary>
public static IClassMapper GetMap<T>() where T : class
{
return Instance.SqlGenerator.Configuration.GetMap<T>();
}
/// <summary>
/// Clears the ClassMappers for each type.
/// </summary>
public static void ClearCache()
{
Instance.SqlGenerator.Configuration.ClearCache();
}
/// <summary>
/// Generates a COMB Guid which solves the fragmented index issue.
/// See: http://davybrion.com/blog/2009/05/using-the-guidcomb-identifier-strategy
/// </summary>
public static string GetNextID()
{
return Instance.SqlGenerator.Configuration.GetNextID().ToString();
}
}
}