-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepository.cs
238 lines (225 loc) · 6.72 KB
/
Repository.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
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.SQLite;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace AppLibrary
{
public class Repository<T> : IRepository<T> where T : class
{
private static string conStr = null;
private DBHelper dbSelector;
private StringBuilder strSql;
public Repository(string sDBAliasName)
{
dbSelector = new DBHelper();
conStr = dbSelector.GetConnectionString(sDBAliasName);
strSql = new StringBuilder();
}
public void Delete(T entity)
{
strSql.Clear();
try
{
using (var cn = new SQLiteConnection(conStr))
{
cn.Open();
SQLiteCommand cmd = new SQLiteCommand(strSql.ToString(), cn);
Type myLoadClass = entity.GetType();
string tbName = dbSelector.GetTbName(myLoadClass);
string keyCondition = dbSelector.GetKeyCondition(entity);
var keys = entity.GetType().GetProperties().Where(prop => prop.IsDefined(typeof(KeyAttribute), false));
if (keys.Count() == 0)
{
strSql.AppendLine("必須設定Key值");
throw new Exception(strSql.ToString());
}
else
{
foreach (var key in keys)
{
var keyName = key.Name;
var keyValue = entity.GetType().GetProperty(keyName).GetValue(entity, null);
if (keyValue == null)
{
strSql.Append("Key值: ").Append(keyName).Append(" 不得為空").AppendLine();
throw new Exception(strSql.ToString());
}
cmd.Parameters.Add(new SQLiteParameter(keyName, keyValue));
}
strSql.Append("delete from ").Append(tbName).Append(" where 1=1 ").Append(keyCondition).AppendLine();
cmd.CommandText = strSql.ToString();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
}
catch (Exception e)
{
throw new Exception(strSql.ToString(), e);
}
}
public void Insert(T entity)
{
strSql.Clear();
try
{
using (var cn = new SQLiteConnection(conStr))
{
cn.Open();
SQLiteCommand cmd = new SQLiteCommand(strSql.ToString(), cn);
Type myLoadClass = entity.GetType();
string tbName = dbSelector.GetTbName(myLoadClass);
PropertyInfo[] props = myLoadClass.GetProperties();
string colName = "";
string colValue = "";
foreach (var prop in props)
{
string tName = prop.Name;
object tValue = prop.GetValue(entity, null);
colName += tName + ",";
colValue += "@" + tName + ",";
if (tValue != null)
{
var vType = tValue.GetType();
cmd.Parameters.Add(new SQLiteParameter(tName, tValue));
}
else
{
cmd.Parameters.Add(new SQLiteParameter(tName, DBNull.Value));
}
}
strSql.Append("insert into ").Append(tbName).Append(" (").Append(colName.TrimEnd(',')).Append(") values (").Append(colValue.TrimEnd(',')).Append(")").AppendLine();
cmd.CommandText = strSql.ToString();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
catch (Exception e)
{
throw new Exception(strSql.ToString(), e);
}
}
public IList<T> Select(T entity)
{
strSql.Clear();
try
{
IList<T> result = new List<T>();
using (var cn = new SQLiteConnection(conStr))
{
cn.Open();
SQLiteCommand cmd = new SQLiteCommand(strSql.ToString(), cn);
Type myLoadClass = entity.GetType();
string tbName = dbSelector.GetTbName(myLoadClass);
PropertyInfo[] props = myLoadClass.GetProperties();
string condition = "";
foreach (var prop in props)
{
string tName = prop.Name;
object tValue = prop.GetValue(entity, null);
if (tValue != null)
{
condition += " and " + tName + " =@" + tName + " ";
//var vType = tValue.GetType();
cmd.Parameters.Add(new SQLiteParameter(tName, tValue));
}
}
strSql.Append("select * from ").Append(tbName).Append(" where 1=1 ").Append(condition).AppendLine();
cmd.CommandText = strSql.ToString();
List<object> t1 = new List<object>();
var dr = cmd.ExecuteReader();
while (dr.Read())
{
Dictionary<string, object> tmpDic = new Dictionary<string, object>();
for (int i = 0; i < dr.FieldCount; i++)
{
var val = dr.GetValue(i);
string key = dr.GetName(i).ToString();
tmpDic.Add(key, val);
}
object tmpObj = Activator.CreateInstance(myLoadClass);
foreach (var prop in props)
{
string tName = prop.Name.ToUpper();
if (tmpDic.ContainsKey(tName))
{
var type = tmpDic[tName].GetType();
var value = type.Name.Equals("DBNull") ? null : tmpDic[tName];
prop.SetValue(tmpObj, value);
}
}
if (tmpObj is T)
{
var d = (T)Convert.ChangeType(tmpObj, entity.GetType());
result.Add(d);
}
}
return result;
}
}
catch (Exception e)
{
throw new Exception(strSql.ToString(), e);
}
}
public void Update(T entity)
{
strSql.Clear();
try
{
using (var cn = new SQLiteConnection(conStr))
{
cn.Open();
SQLiteCommand cmd = new SQLiteCommand(strSql.ToString(), cn);
Type myLoadClass = entity.GetType();
string keyCondition = dbSelector.GetKeyCondition(entity);
string tbName = dbSelector.GetTbName(myLoadClass);
PropertyInfo[] props = myLoadClass.GetProperties();
var keys = entity.GetType().GetProperties().Where(prop => prop.IsDefined(typeof(KeyAttribute), false));
if (keys.Count() == 0)
{
strSql.AppendLine("必須設定Key值").AppendLine();
throw new Exception(strSql.ToString());
}
else
{
foreach (var key in keys)
{
var keyName = key.Name;
var keyValue = entity.GetType().GetProperty(keyName).GetValue(entity, null);
if (keyValue == null)
{
strSql.Append("Key值: ").Append(keyName).Append(" 不得為空").AppendLine();
throw new Exception(strSql.ToString());
}
}
string colName = "";
string colValue = "";
string updateValue = "";
foreach (var prop in props)
{
string tName = prop.Name;
object tValue = prop.GetValue(entity, null) == null ? DBNull.Value : prop.GetValue(entity, null);
updateValue += tName + "=@" + tName + ",";
colName += tName + ",";
colValue += "@" + tName + ",";
cmd.Parameters.Add(new SQLiteParameter(tName, tValue));
}
strSql.Append("update ").Append(tbName).Append(" set ").Append(updateValue.Trim(',')).Append(" where 1=1 ").Append(keyCondition).AppendLine();
cmd.CommandText = strSql.ToString();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
}
}
catch (Exception e)
{
throw new Exception(strSql.ToString(), e);
}
}
}
}