-
Notifications
You must be signed in to change notification settings - Fork 113
/
Extensions.cs
384 lines (315 loc) · 11.7 KB
/
Extensions.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
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Text.RegularExpressions;
using JetBrains.Annotations;
namespace BLToolkit.Data.Linq
{
using DataAccess;
public static class Extensions
{
#region Table Helpers
static public Table<T> GetTable<T>(this IDataContext dataContext)
where T : class
{
return new Table<T>(dataContext);
}
static public Table<T> GetTable<T>(
this IDataContext dataContext,
object instance,
[NotNull] MethodInfo methodInfo,
[NotNull] params object[] parameters)
where T : class
{
if (methodInfo == null) throw new ArgumentNullException("methodInfo");
if (parameters == null) throw new ArgumentNullException("parameters");
if (!typeof(Table<T>).IsAssignableFrom(methodInfo.ReturnType))
throw new LinqException(
"Method '{0}.{1}' must return type 'Table<{2}>'",
methodInfo.Name, methodInfo.DeclaringType.FullName, typeof(T).FullName);
Expression expr;
if (parameters.Length > 0)
{
var pis = methodInfo.GetParameters();
var args = new List<Expression>(parameters.Length);
for (var i = 0; i < parameters.Length; i++)
{
var type = pis[i].ParameterType;
args.Add(Expression.Constant(parameters[i], type.IsByRef ? type.GetElementType() : type));
}
expr = Expression.Call(Expression.Constant(instance), methodInfo, args);
}
else
expr = Expression.Call(Expression.Constant(instance), methodInfo);
return new Table<T>(dataContext, expr);
}
#endregion
#region Compile
/// <summary>
/// Compiles the query.
/// </summary>
/// <returns>
/// A generic delegate that represents the compiled query.
/// </returns>
/// <param name="dataContext"></param>
/// <param name="query">
/// The query expression to be compiled.
/// </param>
/// <typeparam name="TDc">
/// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
/// </typeparam>
/// <typeparam name="TResult">
/// Returned type of the delegate returned by the method.
/// </typeparam>
static public Func<TDc,TResult> Compile<TDc,TResult>(
[NotNull] this IDataContext dataContext,
[NotNull] Expression<Func<TDc,TResult>> query)
where TDc : IDataContext
{
return CompiledQuery.Compile(query);
}
/// <summary>
/// Compiles the query.
/// </summary>
/// <returns>
/// A generic delegate that represents the compiled query.
/// </returns>
/// <param name="dataContext"></param>
/// <param name="query">
/// The query expression to be compiled.
/// </param>
/// <typeparam name="TDc">
/// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
/// </typeparam>
/// <typeparam name="TArg1">
/// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
/// </typeparam>
/// <typeparam name="TResult">
/// Returned type of the delegate returned by the method.
/// </typeparam>
static public Func<TDc,TArg1,TResult> Compile<TDc,TArg1, TResult>(
[NotNull] this IDataContext dataContext,
[NotNull] Expression<Func<TDc,TArg1,TResult>> query)
where TDc : IDataContext
{
return CompiledQuery.Compile(query);
}
/// <summary>
/// Compiles the query.
/// </summary>
/// <returns>
/// A generic delegate that represents the compiled query.
/// </returns>
/// <param name="dataContext"></param>
/// <param name="query">
/// The query expression to be compiled.
/// </param>
/// <typeparam name="TDc">
/// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
/// </typeparam>
/// <typeparam name="TArg1">
/// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
/// </typeparam>
/// <typeparam name="TArg2">
/// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
/// </typeparam>
/// <typeparam name="TResult">
/// Returned type of the delegate returned by the method.
/// </typeparam>
static public Func<TDc,TArg1,TArg2,TResult> Compile<TDc,TArg1,TArg2,TResult>(
[NotNull] this IDataContext dataContext,
[NotNull] Expression<Func<TDc,TArg1,TArg2,TResult>> query)
where TDc : IDataContext
{
return CompiledQuery.Compile(query);
}
/// <summary>
/// Compiles the query.
/// </summary>
/// <returns>
/// A generic delegate that represents the compiled query.
/// </returns>
/// <param name="dataContext"></param>
/// <param name="query">
/// The query expression to be compiled.
/// </param>
/// <typeparam name="TDc">
/// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
/// </typeparam>
/// <typeparam name="TArg1">
/// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
/// </typeparam>
/// <typeparam name="TArg2">
/// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
/// </typeparam>
/// <typeparam name="TArg3">
/// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
/// </typeparam>
/// <typeparam name="TResult">
/// Returned type of the delegate returned by the method.
/// </typeparam>
static public Func<TDc,TArg1,TArg2,TArg3,TResult> Compile<TDc,TArg1,TArg2,TArg3,TResult>(
[NotNull] this IDataContext dataContext,
[NotNull] Expression<Func<TDc,TArg1,TArg2,TArg3,TResult>> query)
where TDc : IDataContext
{
return CompiledQuery.Compile(query);
}
#endregion
#region Object Operations
#region Insert
public static int Insert<T>([NotNull] this IDataContextInfo dataContextInfo, T obj)
{
if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
return Query<T>.Insert(dataContextInfo, obj);
}
public static int Insert<T>(this IDataContext dataContext, T obj)
{
return Query<T>.Insert(DataContextInfo.Create(dataContext), obj);
}
#endregion
#region InsertOrUpdate
[Obsolete("Use 'InsertOrReplace' instead.")]
public static int InsertOrUpdate<T>(this IDataContextInfo dataContextInfo, T obj)
{
return InsertOrReplace(dataContextInfo, obj);
}
public static int InsertOrReplace<T>([NotNull] this IDataContextInfo dataContextInfo, T obj)
{
if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
return Query<T>.InsertOrReplace(dataContextInfo, obj);
}
public static int InsertOrReplace<T>([NotNull] this IDataContextInfo dataContextInfo, IEnumerable<T> objs)
{
if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
int cnt = 0;
foreach (var obj in objs)
{
cnt += Query<T>.InsertOrReplace(dataContextInfo, obj);
}
return cnt;
}
[Obsolete("Use 'InsertOrReplace' instead.")]
public static int InsertOrUpdate<T>(this IDataContext dataContext, T obj)
{
return InsertOrReplace(dataContext, obj);
}
public static int InsertOrReplace<T>(this IDataContext dataContext, T obj)
{
return Query<T>.InsertOrReplace(DataContextInfo.Create(dataContext), obj);
}
public static int InsertOrReplace<T>(this IDataContext dataContext, IEnumerable<T> objs)
{
int cnt = 0;
foreach (var obj in objs)
{
cnt += Query<T>.InsertOrReplace(DataContextInfo.Create(dataContext), obj);
}
return cnt;
}
#endregion
#region InsertBatch
#if !SILVERLIGHT
public static int InsertBatch<T>(this DbManager dataContext, int maxBatchSize, IEnumerable<T> list)
{
return new SqlQuery<T>(dataContext).Insert(dataContext, maxBatchSize, list);
}
public static int InsertBatch<T>(this DbManager dataContext, IEnumerable<T> list)
{
return InsertBatch(dataContext, int.MaxValue, list);
}
public static int InsertBatch<T>(this DbManager dataContext, T[] list)
{
return InsertBatch(dataContext, int.MaxValue, list);
}
[Obsolete("Use InsertBatch instead.")]
public static int Insert<T>(this DbManager dataContext, T[] list)
{
return Insert(dataContext, int.MaxValue, list);
}
[Obsolete("Use InsertBatch instead.")]
public static int Insert<T>(this DbManager dataContext, int maxBatchSize, IEnumerable<T> list)
{
return new SqlQuery<T>().Insert(dataContext, maxBatchSize, list);
}
[Obsolete("Use InsertBatch instead.")]
public static int Insert<T>(this DbManager dataContext, IEnumerable<T> list)
{
return Insert(dataContext, int.MaxValue, list);
}
#endif
#endregion
#region InsertWithIdentity
public static object InsertWithIdentity<T>([NotNull] this IDataContextInfo dataContextInfo, T obj)
{
if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
return Query<T>.InsertWithIdentity(dataContextInfo, obj);
}
public static object InsertWithIdentity<T>(this IDataContext dataContext, T obj)
{
return Query<T>.InsertWithIdentity(DataContextInfo.Create(dataContext), obj);
}
#endregion
#region InsertWithOutput
public static object InsertWithOutput<T>([NotNull] this IDataContextInfo dataContextInfo, T obj)
{
if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
return Query<T>.InsertWithOutput(dataContextInfo, obj);
}
public static object InsertWithOutput<T>( this IDataContext dataContext, T obj )
{
return Query<T>.InsertWithOutput(DataContextInfo.Create( dataContext ), obj);
}
#endregion
#region Update
public static int Update<T>([NotNull] this IDataContextInfo dataContextInfo, T obj)
{
if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
return Query<T>.Update(dataContextInfo, obj);
}
public static int Update<T>(this IDataContext dataContext, T obj)
{
return Query<T>.Update(DataContextInfo.Create(dataContext), obj);
}
#if !SILVERLIGHT
public static int Update<T>(this DbManager dataContext, int maxBatchSize, IEnumerable<T> list)
{
return new SqlQuery<T>(dataContext).Update(dataContext, maxBatchSize, list);
}
public static int Update<T>(this DbManager dataContext, IEnumerable<T> list)
{
return Update(dataContext, int.MaxValue, list);
}
#endif
#endregion
#region Delete
public static int Delete<T>([NotNull] this IDataContextInfo dataContextInfo, T obj)
{
if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
return Query<T>.Delete(dataContextInfo, obj);
}
public static int Delete<T>([NotNull] this IDataContext dataContext, T obj)
{
return Query<T>.Delete(DataContextInfo.Create(dataContext), obj);
}
#if !SILVERLIGHT
public static int Delete<T>(this DbManager dataContext, int maxBatchSize, IEnumerable<T> list)
{
return new SqlQuery<T>(dataContext).Delete(dataContext, maxBatchSize, list);
}
public static int Delete<T>(this DbManager dataContext, IEnumerable<T> list)
{
return Delete(dataContext, int.MaxValue, list);
}
#endif
#endregion
#endregion
#region String Extensions
public static int ContainsExactly(this string s, string value)
{
return Regex.Matches(s, string.Format(@"(^|\s){0}(\s|$)", value), RegexOptions.IgnoreCase).Count;
}
#endregion
}
}