comparison Source/Data/Linq/Extensions.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Collections.Generic;
3 using System.Linq.Expressions;
4 using System.Reflection;
5 using System.Text.RegularExpressions;
6
7 using JetBrains.Annotations;
8
9 namespace BLToolkit.Data.Linq
10 {
11 using DataAccess;
12
13 public static class Extensions
14 {
15 #region Table Helpers
16
17 static public Table<T> GetTable<T>(this IDataContext dataContext)
18 where T : class
19 {
20 return new Table<T>(dataContext);
21 }
22
23 static public Table<T> GetTable<T>(
24 this IDataContext dataContext,
25 object instance,
26 [NotNull] MethodInfo methodInfo,
27 [NotNull] params object[] parameters)
28 where T : class
29 {
30 if (methodInfo == null) throw new ArgumentNullException("methodInfo");
31 if (parameters == null) throw new ArgumentNullException("parameters");
32
33 if (!typeof(Table<T>).IsAssignableFrom(methodInfo.ReturnType))
34 throw new LinqException(
35 "Method '{0}.{1}' must return type 'Table<{2}>'",
36 methodInfo.Name, methodInfo.DeclaringType.FullName, typeof(T).FullName);
37
38 Expression expr;
39
40 if (parameters.Length > 0)
41 {
42 var pis = methodInfo.GetParameters();
43 var args = new List<Expression>(parameters.Length);
44
45 for (var i = 0; i < parameters.Length; i++)
46 {
47 var type = pis[i].ParameterType;
48 args.Add(Expression.Constant(parameters[i], type.IsByRef ? type.GetElementType() : type));
49 }
50
51 expr = Expression.Call(Expression.Constant(instance), methodInfo, args);
52 }
53 else
54 expr = Expression.Call(Expression.Constant(instance), methodInfo);
55
56 return new Table<T>(dataContext, expr);
57 }
58
59 #endregion
60
61 #region Compile
62
63 /// <summary>
64 /// Compiles the query.
65 /// </summary>
66 /// <returns>
67 /// A generic delegate that represents the compiled query.
68 /// </returns>
69 /// <param name="dataContext"></param>
70 /// <param name="query">
71 /// The query expression to be compiled.
72 /// </param>
73 /// <typeparam name="TDc">
74 /// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
75 /// </typeparam>
76 /// <typeparam name="TResult">
77 /// Returned type of the delegate returned by the method.
78 /// </typeparam>
79 static public Func<TDc,TResult> Compile<TDc,TResult>(
80 [NotNull] this IDataContext dataContext,
81 [NotNull] Expression<Func<TDc,TResult>> query)
82 where TDc : IDataContext
83 {
84 return CompiledQuery.Compile(query);
85 }
86
87 /// <summary>
88 /// Compiles the query.
89 /// </summary>
90 /// <returns>
91 /// A generic delegate that represents the compiled query.
92 /// </returns>
93 /// <param name="dataContext"></param>
94 /// <param name="query">
95 /// The query expression to be compiled.
96 /// </param>
97 /// <typeparam name="TDc">
98 /// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
99 /// </typeparam>
100 /// <typeparam name="TArg1">
101 /// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
102 /// </typeparam>
103 /// <typeparam name="TResult">
104 /// Returned type of the delegate returned by the method.
105 /// </typeparam>
106 static public Func<TDc,TArg1,TResult> Compile<TDc,TArg1, TResult>(
107 [NotNull] this IDataContext dataContext,
108 [NotNull] Expression<Func<TDc,TArg1,TResult>> query)
109 where TDc : IDataContext
110 {
111 return CompiledQuery.Compile(query);
112 }
113
114 /// <summary>
115 /// Compiles the query.
116 /// </summary>
117 /// <returns>
118 /// A generic delegate that represents the compiled query.
119 /// </returns>
120 /// <param name="dataContext"></param>
121 /// <param name="query">
122 /// The query expression to be compiled.
123 /// </param>
124 /// <typeparam name="TDc">
125 /// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
126 /// </typeparam>
127 /// <typeparam name="TArg1">
128 /// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
129 /// </typeparam>
130 /// <typeparam name="TArg2">
131 /// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
132 /// </typeparam>
133 /// <typeparam name="TResult">
134 /// Returned type of the delegate returned by the method.
135 /// </typeparam>
136 static public Func<TDc,TArg1,TArg2,TResult> Compile<TDc,TArg1,TArg2,TResult>(
137 [NotNull] this IDataContext dataContext,
138 [NotNull] Expression<Func<TDc,TArg1,TArg2,TResult>> query)
139 where TDc : IDataContext
140 {
141 return CompiledQuery.Compile(query);
142 }
143
144 /// <summary>
145 /// Compiles the query.
146 /// </summary>
147 /// <returns>
148 /// A generic delegate that represents the compiled query.
149 /// </returns>
150 /// <param name="dataContext"></param>
151 /// <param name="query">
152 /// The query expression to be compiled.
153 /// </param>
154 /// <typeparam name="TDc">
155 /// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
156 /// </typeparam>
157 /// <typeparam name="TArg1">
158 /// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
159 /// </typeparam>
160 /// <typeparam name="TArg2">
161 /// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
162 /// </typeparam>
163 /// <typeparam name="TArg3">
164 /// Represents the type of the parameter that has to be passed in when executing the delegate returned by the method.
165 /// </typeparam>
166 /// <typeparam name="TResult">
167 /// Returned type of the delegate returned by the method.
168 /// </typeparam>
169 static public Func<TDc,TArg1,TArg2,TArg3,TResult> Compile<TDc,TArg1,TArg2,TArg3,TResult>(
170 [NotNull] this IDataContext dataContext,
171 [NotNull] Expression<Func<TDc,TArg1,TArg2,TArg3,TResult>> query)
172 where TDc : IDataContext
173 {
174 return CompiledQuery.Compile(query);
175 }
176
177 #endregion
178
179 #region Object Operations
180
181 #region Insert
182
183 public static int Insert<T>([NotNull] this IDataContextInfo dataContextInfo, T obj)
184 {
185 if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
186 return Query<T>.Insert(dataContextInfo, obj);
187 }
188
189 public static int Insert<T>(this IDataContext dataContext, T obj)
190 {
191 return Query<T>.Insert(DataContextInfo.Create(dataContext), obj);
192 }
193
194 #endregion
195
196 #region InsertOrUpdate
197
198 [Obsolete("Use 'InsertOrReplace' instead.")]
199 public static int InsertOrUpdate<T>(this IDataContextInfo dataContextInfo, T obj)
200 {
201 return InsertOrReplace(dataContextInfo, obj);
202 }
203
204 public static int InsertOrReplace<T>([NotNull] this IDataContextInfo dataContextInfo, T obj)
205 {
206 if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
207 return Query<T>.InsertOrReplace(dataContextInfo, obj);
208 }
209
210 public static int InsertOrReplace<T>([NotNull] this IDataContextInfo dataContextInfo, IEnumerable<T> objs)
211 {
212 if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
213 int cnt = 0;
214 foreach (var obj in objs)
215 {
216 cnt += Query<T>.InsertOrReplace(dataContextInfo, obj);
217 }
218 return cnt;
219 }
220
221 [Obsolete("Use 'InsertOrReplace' instead.")]
222 public static int InsertOrUpdate<T>(this IDataContext dataContext, T obj)
223 {
224 return InsertOrReplace(dataContext, obj);
225 }
226
227 public static int InsertOrReplace<T>(this IDataContext dataContext, T obj)
228 {
229 return Query<T>.InsertOrReplace(DataContextInfo.Create(dataContext), obj);
230 }
231
232 public static int InsertOrReplace<T>(this IDataContext dataContext, IEnumerable<T> objs)
233 {
234 int cnt = 0;
235 foreach (var obj in objs)
236 {
237 cnt += Query<T>.InsertOrReplace(DataContextInfo.Create(dataContext), obj);
238 }
239 return cnt;
240 }
241
242 #endregion
243
244 #region InsertBatch
245
246 #if !SILVERLIGHT
247
248 public static int InsertBatch<T>(this DbManager dataContext, int maxBatchSize, IEnumerable<T> list)
249 {
250 return new SqlQuery<T>(dataContext).Insert(dataContext, maxBatchSize, list);
251 }
252
253 public static int InsertBatch<T>(this DbManager dataContext, IEnumerable<T> list)
254 {
255 return InsertBatch(dataContext, int.MaxValue, list);
256 }
257
258 public static int InsertBatch<T>(this DbManager dataContext, T[] list)
259 {
260 return InsertBatch(dataContext, int.MaxValue, list);
261 }
262
263 [Obsolete("Use InsertBatch instead.")]
264 public static int Insert<T>(this DbManager dataContext, T[] list)
265 {
266 return Insert(dataContext, int.MaxValue, list);
267 }
268
269 [Obsolete("Use InsertBatch instead.")]
270 public static int Insert<T>(this DbManager dataContext, int maxBatchSize, IEnumerable<T> list)
271 {
272 return new SqlQuery<T>().Insert(dataContext, maxBatchSize, list);
273 }
274
275 [Obsolete("Use InsertBatch instead.")]
276 public static int Insert<T>(this DbManager dataContext, IEnumerable<T> list)
277 {
278 return Insert(dataContext, int.MaxValue, list);
279 }
280
281 #endif
282
283 #endregion
284
285 #region InsertWithIdentity
286
287 public static object InsertWithIdentity<T>([NotNull] this IDataContextInfo dataContextInfo, T obj)
288 {
289 if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
290 return Query<T>.InsertWithIdentity(dataContextInfo, obj);
291 }
292
293 public static object InsertWithIdentity<T>(this IDataContext dataContext, T obj)
294 {
295 return Query<T>.InsertWithIdentity(DataContextInfo.Create(dataContext), obj);
296 }
297
298 #endregion
299
300 #region Update
301
302 public static int Update<T>([NotNull] this IDataContextInfo dataContextInfo, T obj)
303 {
304 if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
305 return Query<T>.Update(dataContextInfo, obj);
306 }
307
308 public static int Update<T>(this IDataContext dataContext, T obj)
309 {
310 return Query<T>.Update(DataContextInfo.Create(dataContext), obj);
311 }
312
313 #if !SILVERLIGHT
314
315 public static int Update<T>(this DbManager dataContext, int maxBatchSize, IEnumerable<T> list)
316 {
317 return new SqlQuery<T>(dataContext).Update(dataContext, maxBatchSize, list);
318 }
319
320 public static int Update<T>(this DbManager dataContext, IEnumerable<T> list)
321 {
322 return Update(dataContext, int.MaxValue, list);
323 }
324
325 #endif
326
327 #endregion
328
329 #region Delete
330
331 public static int Delete<T>([NotNull] this IDataContextInfo dataContextInfo, T obj)
332 {
333 if (dataContextInfo == null) throw new ArgumentNullException("dataContextInfo");
334 return Query<T>.Delete(dataContextInfo, obj);
335 }
336
337 public static int Delete<T>([NotNull] this IDataContext dataContext, T obj)
338 {
339 return Query<T>.Delete(DataContextInfo.Create(dataContext), obj);
340 }
341
342 #if !SILVERLIGHT
343
344 public static int Delete<T>(this DbManager dataContext, int maxBatchSize, IEnumerable<T> list)
345 {
346 return new SqlQuery<T>(dataContext).Delete(dataContext, maxBatchSize, list);
347 }
348
349 public static int Delete<T>(this DbManager dataContext, IEnumerable<T> list)
350 {
351 return Delete(dataContext, int.MaxValue, list);
352 }
353
354 #endif
355
356 #endregion
357
358 #endregion
359
360 #region String Extensions
361
362 public static int ContainsExactly(this string s, string value)
363 {
364 return Regex.Matches(s, string.Format(@"(^|\s){0}(\s|$)", value), RegexOptions.IgnoreCase).Count;
365 }
366
367 #endregion
368 }
369 }