Mercurial > pub > bltoolkit
comparison Source/DataAccess/SqlQueryT.cs @ 0:f990fcb411a9
Копия текущей версии из github
| author | cin |
|---|---|
| date | Thu, 27 Mar 2014 21:46:09 +0400 |
| parents | |
| children | 99cd4f3947d8 |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:f990fcb411a9 |
|---|---|
| 1 using System; | |
| 2 using System.Collections.Generic; | |
| 3 | |
| 4 namespace BLToolkit.DataAccess | |
| 5 { | |
| 6 using Data; | |
| 7 using Mapping; | |
| 8 using Reflection.Extension; | |
| 9 | |
| 10 public class SqlQuery<T> : SqlQueryBase | |
| 11 { | |
| 12 #region Constructors | |
| 13 | |
| 14 public SqlQuery() | |
| 15 { | |
| 16 } | |
| 17 | |
| 18 public SqlQuery(DbManager dbManager) | |
| 19 : base(dbManager) | |
| 20 { | |
| 21 } | |
| 22 | |
| 23 public SqlQuery(DbManager dbManager, bool dispose) | |
| 24 : base(dbManager, dispose) | |
| 25 { | |
| 26 } | |
| 27 | |
| 28 public SqlQuery(ExtensionList extensions) | |
| 29 { | |
| 30 Extensions = extensions; | |
| 31 } | |
| 32 | |
| 33 #endregion | |
| 34 | |
| 35 #region Overrides | |
| 36 | |
| 37 public SqlQueryInfo GetSqlQueryInfo(DbManager db, string actionName) | |
| 38 { | |
| 39 return base.GetSqlQueryInfo(db, typeof(T), actionName); | |
| 40 } | |
| 41 | |
| 42 #endregion | |
| 43 | |
| 44 #region SelectByKey | |
| 45 | |
| 46 public virtual T SelectByKey(DbManager db, params object[] keys) | |
| 47 { | |
| 48 var query = GetSqlQueryInfo(db, typeof(T), "SelectByKey"); | |
| 49 | |
| 50 return db | |
| 51 .SetCommand(query.QueryText, query.GetParameters(db, keys)) | |
| 52 .ExecuteObject<T>(); | |
| 53 } | |
| 54 | |
| 55 public virtual T SelectByKey(params object[] keys) | |
| 56 { | |
| 57 var db = GetDbManager(); | |
| 58 | |
| 59 try | |
| 60 { | |
| 61 return SelectByKey(db, keys); | |
| 62 } | |
| 63 finally | |
| 64 { | |
| 65 if (DisposeDbManager) | |
| 66 db.Dispose(); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 #endregion | |
| 71 | |
| 72 #region SelectAll | |
| 73 | |
| 74 static SqlQueryInfo _selectAllQuery; | |
| 75 | |
| 76 public virtual List<T> SelectAll(DbManager db) | |
| 77 { | |
| 78 if (_selectAllQuery == null) | |
| 79 _selectAllQuery = GetSqlQueryInfo(db, typeof(T), "SelectAll"); | |
| 80 | |
| 81 return db | |
| 82 .SetCommand(_selectAllQuery.QueryText) | |
| 83 .ExecuteList<T>(); | |
| 84 } | |
| 85 | |
| 86 public virtual TL SelectAll<TL>(DbManager db, TL list) | |
| 87 where TL : IList<T> | |
| 88 { | |
| 89 var query = GetSqlQueryInfo(db, typeof(T), "SelectAll"); | |
| 90 | |
| 91 return db | |
| 92 .SetCommand(query.QueryText) | |
| 93 .ExecuteList<TL,T>(list); | |
| 94 } | |
| 95 | |
| 96 public virtual TL SelectAll<TL>(DbManager db) | |
| 97 where TL : IList<T>, new() | |
| 98 { | |
| 99 return SelectAll(db, new TL()); | |
| 100 } | |
| 101 | |
| 102 public virtual List<T> SelectAll() | |
| 103 { | |
| 104 var db = GetDbManager(); | |
| 105 | |
| 106 try | |
| 107 { | |
| 108 return SelectAll(db); | |
| 109 } | |
| 110 finally | |
| 111 { | |
| 112 if (DisposeDbManager) | |
| 113 db.Dispose(); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 public virtual TL SelectAll<TL>(TL list) | |
| 118 where TL : IList<T> | |
| 119 { | |
| 120 var db = GetDbManager(); | |
| 121 | |
| 122 try | |
| 123 { | |
| 124 return SelectAll(db, list); | |
| 125 } | |
| 126 finally | |
| 127 { | |
| 128 if (DisposeDbManager) | |
| 129 db.Dispose(); | |
| 130 } | |
| 131 } | |
| 132 | |
| 133 public virtual TL SelectAll<TL>() | |
| 134 where TL : IList<T>, new() | |
| 135 { | |
| 136 return SelectAll(new TL()); | |
| 137 } | |
| 138 | |
| 139 #endregion | |
| 140 | |
| 141 #region Insert | |
| 142 | |
| 143 public virtual int Insert(DbManager db, T obj) | |
| 144 { | |
| 145 var query = GetSqlQueryInfo(db, obj.GetType(), "Insert"); | |
| 146 | |
| 147 return db | |
| 148 .SetCommand(query.QueryText, query.GetParameters(db, obj)) | |
| 149 .ExecuteNonQuery(); | |
| 150 } | |
| 151 | |
| 152 public virtual int Insert(T obj) | |
| 153 { | |
| 154 var db = GetDbManager(); | |
| 155 | |
| 156 try | |
| 157 { | |
| 158 return Insert(db, obj); | |
| 159 } | |
| 160 finally | |
| 161 { | |
| 162 if (DisposeDbManager) | |
| 163 db.Dispose(); | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 public virtual int Insert(DbManager db, int maxBatchSize, IEnumerable<T> list) | |
| 168 { | |
| 169 var query = GetSqlQueryInfo(db, typeof(T), "InsertBatch"); | |
| 170 | |
| 171 return db.DataProvider.InsertBatch( | |
| 172 db, | |
| 173 query.QueryText, | |
| 174 list, | |
| 175 query.GetMemberMappers(), | |
| 176 maxBatchSize, | |
| 177 obj => query.GetParameters(db, obj)); | |
| 178 } | |
| 179 | |
| 180 public virtual int Insert(int maxBatchSize, IEnumerable<T> list) | |
| 181 { | |
| 182 var db = GetDbManager(); | |
| 183 | |
| 184 try | |
| 185 { | |
| 186 return Insert(db, maxBatchSize, list); | |
| 187 } | |
| 188 finally | |
| 189 { | |
| 190 if (DisposeDbManager) | |
| 191 db.Dispose(); | |
| 192 } | |
| 193 } | |
| 194 | |
| 195 public virtual int Insert(DbManager db, IEnumerable<T> list) | |
| 196 { | |
| 197 return Insert(db, int.MaxValue, list); | |
| 198 } | |
| 199 | |
| 200 public virtual int Insert(IEnumerable<T> list) | |
| 201 { | |
| 202 return Insert(int.MaxValue, list); | |
| 203 } | |
| 204 | |
| 205 #endregion | |
| 206 | |
| 207 #region Update | |
| 208 | |
| 209 public virtual int Update(DbManager db, T obj) | |
| 210 { | |
| 211 var query = GetSqlQueryInfo(db, obj.GetType(), "Update"); | |
| 212 | |
| 213 return db | |
| 214 .SetCommand(query.QueryText, query.GetParameters(db, obj)) | |
| 215 .ExecuteNonQuery(); | |
| 216 } | |
| 217 | |
| 218 public virtual int Update(T obj) | |
| 219 { | |
| 220 var db = GetDbManager(); | |
| 221 | |
| 222 try | |
| 223 { | |
| 224 return Update(db, obj); | |
| 225 } | |
| 226 finally | |
| 227 { | |
| 228 if (DisposeDbManager) | |
| 229 db.Dispose(); | |
| 230 } | |
| 231 } | |
| 232 | |
| 233 public virtual int Update(DbManager db, int maxBatchSize, IEnumerable<T> list) | |
| 234 { | |
| 235 var query = GetSqlQueryInfo(db, typeof(T), "UpdateBatch"); | |
| 236 | |
| 237 db.SetCommand(query.QueryText); | |
| 238 | |
| 239 return ExecuteForEach( | |
| 240 db, | |
| 241 list, | |
| 242 query.GetMemberMappers(), | |
| 243 maxBatchSize, | |
| 244 obj => query.GetParameters(db, obj)); | |
| 245 } | |
| 246 | |
| 247 public virtual int Update(int maxBatchSize, IEnumerable<T> list) | |
| 248 { | |
| 249 var db = GetDbManager(); | |
| 250 | |
| 251 try | |
| 252 { | |
| 253 return Update(db, maxBatchSize, list); | |
| 254 } | |
| 255 finally | |
| 256 { | |
| 257 if (DisposeDbManager) | |
| 258 db.Dispose(); | |
| 259 } | |
| 260 } | |
| 261 | |
| 262 public virtual int Update(DbManager db, IEnumerable<T> list) | |
| 263 { | |
| 264 return Update(db, int.MaxValue, list); | |
| 265 } | |
| 266 | |
| 267 public virtual int Update(IEnumerable<T> list) | |
| 268 { | |
| 269 return Update(int.MaxValue, list); | |
| 270 } | |
| 271 | |
| 272 #endregion | |
| 273 | |
| 274 #region DeleteByKey | |
| 275 | |
| 276 public virtual int DeleteByKey(DbManager db, params object[] key) | |
| 277 { | |
| 278 var query = GetSqlQueryInfo(db, typeof(T), "Delete"); | |
| 279 | |
| 280 return db | |
| 281 .SetCommand(query.QueryText, query.GetParameters(db, key)) | |
| 282 .ExecuteNonQuery(); | |
| 283 } | |
| 284 | |
| 285 public virtual int DeleteByKey(params object[] key) | |
| 286 { | |
| 287 var db = GetDbManager(); | |
| 288 | |
| 289 try | |
| 290 { | |
| 291 return DeleteByKey(db, key); | |
| 292 } | |
| 293 finally | |
| 294 { | |
| 295 if (DisposeDbManager) | |
| 296 db.Dispose(); | |
| 297 } | |
| 298 } | |
| 299 | |
| 300 #endregion | |
| 301 | |
| 302 #region Delete | |
| 303 | |
| 304 public virtual int Delete(DbManager db, T obj) | |
| 305 { | |
| 306 var query = GetSqlQueryInfo(db, obj.GetType(), "Delete"); | |
| 307 | |
| 308 return db | |
| 309 .SetCommand(query.QueryText, query.GetParameters(db, obj)) | |
| 310 .ExecuteNonQuery(); | |
| 311 } | |
| 312 | |
| 313 public virtual int Delete(T obj) | |
| 314 { | |
| 315 var db = GetDbManager(); | |
| 316 | |
| 317 try | |
| 318 { | |
| 319 return Delete(db, obj); | |
| 320 } | |
| 321 finally | |
| 322 { | |
| 323 if (DisposeDbManager) | |
| 324 db.Dispose(); | |
| 325 } | |
| 326 } | |
| 327 | |
| 328 public virtual int Delete(DbManager db, int maxBatchSize, IEnumerable<T> list) | |
| 329 { | |
| 330 var query = GetSqlQueryInfo(db, typeof(T), "DeleteBatch"); | |
| 331 | |
| 332 db.SetCommand(query.QueryText); | |
| 333 | |
| 334 return ExecuteForEach( | |
| 335 db, | |
| 336 list, | |
| 337 query.GetMemberMappers(), | |
| 338 maxBatchSize, | |
| 339 obj => query.GetParameters(db, obj)); | |
| 340 } | |
| 341 | |
| 342 public virtual int Delete(int maxBatchSize, IEnumerable<T> list) | |
| 343 { | |
| 344 var db = GetDbManager(); | |
| 345 | |
| 346 try | |
| 347 { | |
| 348 return Delete(db, list); | |
| 349 } | |
| 350 finally | |
| 351 { | |
| 352 if (DisposeDbManager) | |
| 353 db.Dispose(); | |
| 354 } | |
| 355 } | |
| 356 | |
| 357 public virtual int Delete(DbManager db, IEnumerable<T> list) | |
| 358 { | |
| 359 return Delete(db, int.MaxValue, list); | |
| 360 } | |
| 361 | |
| 362 public virtual int Delete(IEnumerable<T> list) | |
| 363 { | |
| 364 return Delete(int.MaxValue, list); | |
| 365 } | |
| 366 | |
| 367 #endregion | |
| 368 | |
| 369 #region Helpers | |
| 370 | |
| 371 protected int ExecuteForEach( | |
| 372 DbManager db, | |
| 373 IEnumerable<T> collection, | |
| 374 MemberMapper[] members, | |
| 375 int maxBatchSize, | |
| 376 DbManager.ParameterProvider<T> getParameters) | |
| 377 { | |
| 378 return db.ExecuteForEach(collection, members, maxBatchSize, getParameters); | |
| 379 } | |
| 380 | |
| 381 #endregion | |
| 382 } | |
| 383 } |
