comparison Source/DataAccess/SqlQuery.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;
3
4 using BLToolkit.Data;
5 using BLToolkit.Reflection.Extension;
6
7 namespace BLToolkit.DataAccess
8 {
9 public class SqlQuery : SqlQueryBase
10 {
11 #region Constructors
12
13 public SqlQuery()
14 {
15 }
16
17 public SqlQuery(DbManager dbManager)
18 : base(dbManager)
19 {
20 }
21
22 public SqlQuery(DbManager dbManager, bool dispose)
23 : base(dbManager, dispose)
24 {
25 }
26
27 public SqlQuery(ExtensionList extensions)
28 {
29 Extensions = extensions;
30 }
31
32 #endregion
33
34 #region Overrides
35
36 public SqlQueryInfo GetSqlQueryInfo<T>(DbManager db, string actionName)
37 {
38 return base.GetSqlQueryInfo(db, typeof(T), actionName);
39 }
40
41 #endregion
42
43 #region SelectByKey
44
45 public virtual object SelectByKey(DbManager db, Type type, params object[] keys)
46 {
47 var query = GetSqlQueryInfo(db, type, "SelectByKey");
48
49 return db
50 .SetCommand(query.QueryText, query.GetParameters(db, keys))
51 .ExecuteObject(type);
52 }
53
54 public virtual object SelectByKey(Type type, params object[] keys)
55 {
56 var db = GetDbManager();
57
58 try
59 {
60 return SelectByKey(db, type, keys);
61 }
62 finally
63 {
64 if (DisposeDbManager)
65 db.Dispose();
66 }
67 }
68
69 public virtual T SelectByKey<T>(DbManager db, params object[] keys)
70 {
71 var query = GetSqlQueryInfo(db, typeof(T), "SelectByKey");
72
73 return db
74 .SetCommand(query.QueryText, query.GetParameters(db, keys))
75 .ExecuteObject<T>();
76 }
77
78 public virtual T SelectByKey<T>(params object[] keys)
79 {
80 var db = GetDbManager();
81
82 try
83 {
84 return SelectByKey<T>(db, keys);
85 }
86 finally
87 {
88 if (DisposeDbManager)
89 db.Dispose();
90 }
91 }
92
93 #endregion
94
95 #region SelectAll
96
97 public virtual ArrayList SelectAll(DbManager db, Type type)
98 {
99 var query = GetSqlQueryInfo(db, type, "SelectAll");
100
101 return db
102 .SetCommand(query.QueryText)
103 .ExecuteList(type);
104 }
105
106 public virtual IList SelectAll(DbManager db, IList list, Type type)
107 {
108 var query = GetSqlQueryInfo(db, type, "SelectAll");
109
110 return db
111 .SetCommand(query.QueryText)
112 .ExecuteList(list, type);
113 }
114
115 public virtual ArrayList SelectAll(Type type)
116 {
117 var db = GetDbManager();
118
119 try
120 {
121 return SelectAll(db, type);
122 }
123 finally
124 {
125 Dispose(db);
126 }
127 }
128
129 public virtual IList SelectAll(IList list, Type type)
130 {
131 var db = GetDbManager();
132
133 try
134 {
135 return SelectAll(db, list, type);
136 }
137 finally
138 {
139 Dispose(db);
140 }
141 }
142
143 public virtual System.Collections.Generic.List<T> SelectAll<T>(DbManager db)
144 {
145 var query = GetSqlQueryInfo(db, typeof(T), "SelectAll");
146
147 return db
148 .SetCommand(query.QueryText)
149 .ExecuteList<T>();
150 }
151
152 public virtual TL SelectAll<TL,T>(DbManager db, TL list)
153 where TL : System.Collections.Generic.IList<T>
154 {
155 var query = GetSqlQueryInfo(db, typeof(T), "SelectAll");
156
157 return db
158 .SetCommand(query.QueryText)
159 .ExecuteList<TL,T>(list);
160 }
161
162 public virtual TL SelectAll<TL,T>(DbManager db)
163 where TL : System.Collections.Generic.IList<T>, new()
164 {
165 return SelectAll<TL,T>(db, new TL());
166 }
167
168 public virtual System.Collections.Generic.List<T> SelectAll<T>()
169 {
170 var db = GetDbManager();
171
172 try
173 {
174 return SelectAll<T>(db);
175 }
176 finally
177 {
178 Dispose(db);
179 }
180 }
181
182 public virtual TL SelectAll<TL,T>(TL list)
183 where TL : System.Collections.Generic.IList<T>
184 {
185 var db = GetDbManager();
186
187 try
188 {
189 return SelectAll<TL,T>(db, list);
190 }
191 finally
192 {
193 Dispose(db);
194 }
195 }
196
197 public virtual TL SelectAll<TL,T>()
198 where TL : System.Collections.Generic.IList<T>, new()
199 {
200 return SelectAll<TL,T>(new TL());
201 }
202
203 #endregion
204
205 #region Insert
206
207 public virtual int Insert(DbManager db, object obj)
208 {
209 var query = GetSqlQueryInfo(db, obj.GetType(), "Insert");
210
211 return db
212 .SetCommand(query.QueryText, query.GetParameters(db, obj))
213 .ExecuteNonQuery();
214 }
215
216 public virtual int Insert(object obj)
217 {
218 var db = GetDbManager();
219
220 try
221 {
222 return Insert(db, obj);
223 }
224 finally
225 {
226 Dispose(db);
227 }
228 }
229
230 #endregion
231
232 #region Update
233
234 public virtual int Update(DbManager db, object obj)
235 {
236 var query = GetSqlQueryInfo(db, obj.GetType(), "Update");
237
238 return db
239 .SetCommand(query.QueryText, query.GetParameters(db, obj))
240 .ExecuteNonQuery();
241 }
242
243 public virtual int Update(object obj)
244 {
245 var db = GetDbManager();
246
247 try
248 {
249 return Update(db, obj);
250 }
251 finally
252 {
253 Dispose(db);
254 }
255 }
256
257 #endregion
258
259 #region DeleteByKey
260
261 public virtual int DeleteByKey(DbManager db, Type type, params object[] key)
262 {
263 var query = GetSqlQueryInfo(db, type, "Delete");
264
265 return db
266 .SetCommand(query.QueryText, query.GetParameters(db, key))
267 .ExecuteNonQuery();
268 }
269
270 public virtual int DeleteByKey(Type type, params object[] key)
271 {
272 var db = GetDbManager();
273
274 try
275 {
276 return DeleteByKey(db, type, key);
277 }
278 finally
279 {
280 Dispose(db);
281 }
282 }
283
284 public virtual int DeleteByKey<T>(DbManager db, params object[] key)
285 {
286 var query = GetSqlQueryInfo(db, typeof(T), "Delete");
287
288 return db
289 .SetCommand(query.QueryText, query.GetParameters(db, key))
290 .ExecuteNonQuery();
291 }
292
293 public virtual int DeleteByKey<T>(params object[] key)
294 {
295 var db = GetDbManager();
296
297 try
298 {
299 return DeleteByKey<T>(db, key);
300 }
301 finally
302 {
303 Dispose(db);
304 }
305 }
306
307 #endregion
308
309 #region Delete
310
311 public virtual int Delete(DbManager db, object obj)
312 {
313 var query = GetSqlQueryInfo(db, obj.GetType(), "Delete");
314
315 return db
316 .SetCommand(query.QueryText, query.GetParameters(db, obj))
317 .ExecuteNonQuery();
318 }
319
320 public virtual int Delete(object obj)
321 {
322 var db = GetDbManager();
323
324 try
325 {
326 return Delete(db, obj);
327 }
328 finally
329 {
330 Dispose(db);
331 }
332 }
333
334 #endregion
335 }
336 }