0
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Data;
|
|
4 using System.Text;
|
|
5
|
|
6 using BLToolkit.Aspects;
|
|
7 using BLToolkit.Data;
|
|
8 using BLToolkit.DataAccess;
|
|
9 using BLToolkit.Reflection;
|
|
10
|
|
11 namespace PetShop.BusinessLogic.DataAccess
|
|
12 {
|
|
13 [Log, Counter]
|
|
14 public abstract class AccessorBase<D, A> : DataAccessor
|
|
15 where D : DbManager, new()
|
|
16 where A : AccessorBase<D, A>
|
|
17 {
|
|
18 #region Overrides
|
|
19
|
|
20 [NoInterception]
|
|
21 protected override DbManager CreateDbManager()
|
|
22 {
|
|
23 return new D();
|
|
24 }
|
|
25
|
|
26 #endregion
|
|
27
|
|
28 #region CreateInstance
|
|
29
|
|
30 public static A CreateInstance()
|
|
31 {
|
|
32 return TypeAccessor<A>.CreateInstance();
|
|
33 }
|
|
34
|
|
35 #endregion
|
|
36
|
|
37 #region Query
|
|
38
|
|
39 [Log, Counter]
|
|
40 public abstract class PetShopQuery : SqlQuery
|
|
41 {
|
|
42 [NoInterception]
|
|
43 protected override DbManager CreateDbManager()
|
|
44 {
|
|
45 return new D();
|
|
46 }
|
|
47
|
|
48 #region SelectByKeyWords
|
|
49
|
|
50 public virtual List<T> SelectByKeyWords<T>(string[] keyWords)
|
|
51 {
|
|
52 StringBuilder sql = new StringBuilder("SELECT * FROM ");
|
|
53
|
|
54 sql.Append(GetTableName(typeof(T)));
|
|
55 sql.Append(" WHERE ");
|
|
56
|
|
57 for (int i = 0; i < keyWords.Length; i++)
|
|
58 {
|
|
59 if (i > 0)
|
|
60 sql.Append(" OR ");
|
|
61
|
|
62 sql.AppendFormat("LOWER(Name) LIKE @kw{0} OR LOWER(CategoryId) LIKE @kw{0}", i);
|
|
63 }
|
|
64
|
|
65 using (DbManager db = GetDbManager())
|
|
66 {
|
|
67 IDbDataParameter[] parms = new IDbDataParameter[keyWords.Length];
|
|
68
|
|
69 for (int i = 0; i < keyWords.Length; i++)
|
|
70 parms[i] = db.Parameter("@kw" + i, "%" + keyWords[i].ToLower() + "%");
|
|
71
|
|
72 return db.SetCommand(sql.ToString(), parms).ExecuteList<T>();
|
|
73 }
|
|
74 }
|
|
75
|
|
76 #endregion
|
|
77
|
|
78 #region InsertAndGetIdentity
|
|
79
|
|
80 public virtual int InsertAndGetIdentity(DbManager db, object obj)
|
|
81 {
|
|
82 SqlQueryInfo query = GetSqlQueryInfo(db, obj.GetType(), "InsertAndGetIdentity");
|
|
83
|
|
84 return db
|
|
85 .SetCommand(query.QueryText, query.GetParameters(db, obj))
|
|
86 .ExecuteScalar<int>();
|
|
87 }
|
|
88
|
|
89 public virtual int InsertAndGetIdentity(object obj)
|
|
90 {
|
|
91 DbManager db = GetDbManager();
|
|
92
|
|
93 try
|
|
94 {
|
|
95 return InsertAndGetIdentity(db, obj);
|
|
96 }
|
|
97 finally
|
|
98 {
|
|
99 Dispose(db);
|
|
100 }
|
|
101 }
|
|
102
|
|
103 [NoInterception]
|
|
104 protected override SqlQueryInfo CreateSqlText(DbManager db, Type type, string actionName)
|
|
105 {
|
|
106 switch (actionName)
|
|
107 {
|
|
108 case "InsertAndGetIdentity":
|
|
109 SqlQueryInfo qi = CreateInsertSqlText(db, type, -1);
|
|
110
|
|
111 qi.QueryText += "\nSELECT Cast(SCOPE_IDENTITY() as int)";
|
|
112
|
|
113 return qi;
|
|
114
|
|
115 default :
|
|
116 return base.CreateSqlText(db, type, actionName);
|
|
117 }
|
|
118 }
|
|
119
|
|
120 #endregion
|
|
121 }
|
|
122
|
|
123 public PetShopQuery Query
|
|
124 {
|
|
125 get { return TypeAccessor<PetShopQuery>.CreateInstance(); }
|
|
126 }
|
|
127
|
|
128 #endregion
|
|
129 }
|
|
130 }
|