-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryHelper.cs
More file actions
330 lines (300 loc) · 12.5 KB
/
QueryHelper.cs
File metadata and controls
330 lines (300 loc) · 12.5 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Dau.Data
{
public class QueryHelper
{
public const string DefaultDBProvider = "System.Data.SqlClient";
static Dictionary<string, char> _SqlParameterPrefix;
public static Dictionary<string, char> SqlParameterPrefix
{
get
{
if (_SqlParameterPrefix == null)
{
_SqlParameterPrefix = new Dictionary<string, char>();
_SqlParameterPrefix.Add("SQL", '@');
_SqlParameterPrefix.Add("System.Data.SqlClient", '@');
_SqlParameterPrefix.Add("Oracle", ':');
_SqlParameterPrefix.Add("Oracle.DataAccess.Client", ':');
_SqlParameterPrefix.Add("Oracle.DataAccess", ':');
_SqlParameterPrefix.Add("Oracle.DataAccess.Lite_w32", ':');
_SqlParameterPrefix.Add("System.Data.OleDb", '\0');
_SqlParameterPrefix.Add("System.Data.Odbc", '\0');
_SqlParameterPrefix.Add("System.Data.OracleClient", ':');
_SqlParameterPrefix.Add("System.Data.SqlServerCe", '@');
_SqlParameterPrefix.Add("MySql", '?');
_SqlParameterPrefix.Add("MySql.Data.MySqlClient", '?');
_SqlParameterPrefix.Add("Devart.Data.MySql", '@');
_SqlParameterPrefix.Add("SQLite", '@');
_SqlParameterPrefix.Add("System.Data.SQLite", '@');
_SqlParameterPrefix.Add("DB2", '?');
_SqlParameterPrefix.Add("IBM.Data.DB2", '?');
_SqlParameterPrefix.Add("Community.CsharpSqlite.SQLiteClient", '@');
_SqlParameterPrefix.Add("FirebirdSql.Data.FirebirdClient", '@');
_SqlParameterPrefix.Add("Sap.Data.Hana", '\0');
_SqlParameterPrefix.Add("Ingres.Client", '\0');
_SqlParameterPrefix.Add("Npgsql", ':');
_SqlParameterPrefix.Add("iAnywhere.Data.AsaClient", '\0');
_SqlParameterPrefix.Add("Sybase.AdoNet2.AseClient", '@');
_SqlParameterPrefix.Add("iAnywhere.Data.SQLAnywhere", ':');
}
return _SqlParameterPrefix;
}
}
public static Tuple<string, Dictionary<string, object>> Sql_Select<T>(string[] SelectColumns = null, Dictionary<string, object> WherePara = null, string WhereSql = null, string Orders = "", string Owner = null, string Table=null, string DBProvider = DefaultDBProvider)
{
StringBuilder sb_query = new StringBuilder("SELECT ");
if (SelectColumns == null)
{
sb_query.Append(" * ");
}else
{
sb_query.Append(string.Join(",", SelectColumns));
}
sb_query.Append(" FROM ");
sb_query.Append(GetTableName<T>(Owner, Table));
var p_where = ConvertWhere(WherePara, WhereSql, DBProvider);
sb_query.Append(p_where.Item1);
if (string.IsNullOrEmpty(Orders) == false)
{
sb_query.Append(" ORDER BY ");
sb_query.Append(Orders);
}
return new Tuple<string, Dictionary<string, object>>(sb_query.ToString(), p_where.Item2);
}
public static Tuple<string, Dictionary<string, object>> Sql_Update<T>(Dictionary<string, object> UpdatePara, Dictionary<string, object> WherePara, string WhereSql = null, string Owner = null, string Table = null, string DBProvider = DefaultDBProvider)
{
StringBuilder sb_query = new StringBuilder("UPDATE ");
sb_query.Append(GetTableName<T>(Owner, Table));
sb_query.Append(" SET ");
if (UpdatePara != null)
{
int seq = 0;
foreach (var key in UpdatePara.Keys)
{
if (seq != 0)
{
sb_query.Append(" , ");
}
sb_query.Append(key);
sb_query.Append("=");
sb_query.Append(SqlParameterPrefix[DBProvider]);
sb_query.Append(key);
seq += 1;
}
}
var p_where = ConvertWhere(WherePara, WhereSql, DBProvider);
sb_query.Append(p_where.Item1);
Dictionary<string, object> return_para = new Dictionary<string, object>();
foreach (var item in UpdatePara.Keys)
{
return_para.Add(item, UpdatePara[item]);
}
foreach (var item in p_where.Item2.Keys)
{
return_para.Add(item, p_where.Item2[item]);
}
return new Tuple<string, Dictionary<string, object>>(sb_query.ToString(), return_para);
}
public static Tuple<string, Dictionary<string, object>> Sql_Insert<T>(Dictionary<string, object> InsertPara, string Owner = null, string Table = null, string DBProvider = DefaultDBProvider)
{
StringBuilder sb_query = new StringBuilder("INSERT INTO ");
StringBuilder sb_col = new StringBuilder();
StringBuilder sb_para = new StringBuilder();
sb_query.Append(GetTableName<T>(Owner, Table));
if (InsertPara != null)
{
int seq = 0;
foreach (var key in InsertPara.Keys)
{
if (seq != 0)
{
sb_col.Append(" , ");
sb_para.Append(" , ");
}
sb_col.Append(key);
sb_para.Append(SqlParameterPrefix[DBProvider]);
sb_para.Append(key);
seq += 1;
}
}
sb_query.Append("(");
sb_query.Append(sb_col.ToString());
sb_query.Append(") values (");
sb_query.Append(sb_para.ToString());
sb_query.Append(")");
return new Tuple<string, Dictionary<string, object>>(sb_query.ToString(), InsertPara);
}
public static Tuple<string, Dictionary<string, object>> Sql_Delete<T>(Dictionary<string, object> WherePara, string WhereSql = null, string Owner = null, string Table = null, string DBProvider = DefaultDBProvider)
{
StringBuilder sb_query = new StringBuilder("DELETE FROM ");
sb_query.Append(GetTableName<T>(Owner, Table));
var p_where = ConvertWhere(WherePara, WhereSql, DBProvider);
sb_query.Append(p_where.Item1);
return new Tuple<string, Dictionary<string, object>>(sb_query.ToString(), p_where.Item2);
}
#region obj to dic
/*
public static Dictionary<string, object> ToDictionary<T>(T source, bool isExcludeNull = true)
{
IEnumerable<PropertyInfo> props;
Dictionary<string, object> dic;
if (isExcludeNull == true)
{
props = source.GetType().GetProperties()
.Where(p => p.GetValue(source) != null);
}
else
{
props = source.GetType().GetProperties()
.Where(p => p.GetValue(source) != null);
}
if (props != null)
{
dic = new Dictionary<string, object>();
foreach (var item in props)
{
dic.Add(item.Name, item.GetValue(source));
}
return dic;
}
return null;
}
*/
#endregion
#region convert query
private static string GetTableName<T>(string Owner = null, string Table=null)
{
var _t = typeof(T);
string _schema = string.IsNullOrEmpty(Owner) ? "" : Owner;
string _tbl = string.IsNullOrEmpty(Table) ? _t.Name : Table;
string tbl = string.IsNullOrEmpty(_schema) == false ? _schema + "." + _tbl : _tbl;
return tbl;
}
private static Tuple<string, Dictionary<string, object>> ConvertWhere(Dictionary<string, object> WherePara = null, string WhereSql = null, string DBProvider = DefaultDBProvider)
{
if (WherePara == null && WhereSql == null)
{
return new Tuple<string, Dictionary<string, object>>("", null);
}
Dictionary<string, object> _WherePara = new Dictionary<string, object>();
StringBuilder sb_where = new StringBuilder(" where ");
if (string.IsNullOrEmpty(WhereSql) == false)
{
sb_where.Append(ConvertSql(WhereSql, DBProvider, true));
if (WherePara != null)
{
foreach (var key in WherePara.Keys)
{
_WherePara.Add("p_" + key, WherePara[key]);
}
}
}
else
{
int seq = 0;
if (WherePara != null)
{
foreach (var key in WherePara.Keys)
{
if (seq != 0)
{
sb_where.Append(" and ");
}
sb_where.Append(key);
sb_where.Append("=");
sb_where.Append(SqlParameterPrefix[DBProvider]);
sb_where.Append("p_");
sb_where.Append(key);
_WherePara.Add("p_" + key, WherePara[key]);
seq += 1;
}
}
}
return new Tuple<string, Dictionary<string, object>>(sb_where.ToString(), _WherePara);
}
public static string ConvertSql(string Query, string DBProvider = "System.Data.SqlClient", bool isWhereClause=false)
{
char[] prefixs = new char[] { ':', '@', '?' };
//var qs = Query.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var cs = Query.ToCharArray();
StringBuilder sb = new StringBuilder();
int q_cnt = 0;
foreach (var c in cs)
{
if (c == '\'') q_cnt += 1;
if (q_cnt % 2 == 1)
{
sb.Append(c);
continue;
}
bool is_prefix = false;
foreach (var prefix in prefixs)
{
if (c == prefix)
{
is_prefix = true;
break;
}
}
if (is_prefix == true)
{
sb.Append(SqlParameterPrefix[DBProvider]);
if (isWhereClause==true)
{
sb.Append("p_");
}
}
else
{
sb.Append(c);
}
}
return sb.ToString();
//"GetParameterName"
}
/*
public static string ConvertSqlWhere(string Query, string DBProvider = "Oracle.DataAccess.Client")
{
char[] prefixs = new char[] { ':', '@', '?' };
//var qs = Query.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var cs = Query.ToCharArray();
StringBuilder sb = new StringBuilder();
int q_cnt = 0;
foreach (var c in cs)
{
if (c == '\'') q_cnt += 1;
if (q_cnt % 2 == 1)
{
sb.Append(c);
continue;
}
bool is_prefix = false;
foreach (var prefix in prefixs)
{
if (c == prefix)
{
is_prefix = true;
break;
}
}
if (is_prefix == true)
{
sb.Append(SqlParameterPrefix[DBProvider]);
sb.Append("p_");
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
*/
#endregion
}
}