forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqlSyntax.cs
More file actions
321 lines (304 loc) · 13.5 KB
/
Copy pathSqlSyntax.cs
File metadata and controls
321 lines (304 loc) · 13.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
using System;
using System.Collections.Generic;
using System.Text;
namespace CYQ.Data.SQL
{
/// <summary>
/// SQL语法解析规则
/// </summary>
internal class SqlSyntax
{
/// <summary>
/// 分析语法
/// </summary>
public static SqlSyntax Analyze(string sql)
{
return new SqlSyntax(sql);
}
public string SqlText;
private SqlSyntax(string sql)
{
SqlText = sql;
FormatSqlText(sql);
}
public string TableName = string.Empty;
public string Where = string.Empty;
public bool IsInsert = false;
public bool IsInsertInto = false;
public bool IsSelect = false;
public bool IsUpdate = false;
public bool IsDelete = false;
public bool IsFrom = false;
public bool IsGetCount = false;
// bool IsAll = false;
public bool IsTopN = false;
public bool IsDistinct = false;
public bool IsLimit = false;
public bool IsOffset = false;
public bool IsWhere = false;
public bool IsOrder = false;
public int TopN = -1;
public int PageIndex = 0;
public int PageSize = 0;
public List<string> FieldItems = new List<string>();
void FormatSqlText(string sqlText)
{
string[] items = sqlText.Split(' ');
foreach (string item in items)
{
#region MyRegion
switch (item.ToLower())
{
case "insert":
IsInsert = true;
break;
case "into":
if (IsInsert)
{
IsInsertInto = true;
}
break;
case "select":
IsSelect = true;
break;
case "update":
IsUpdate = true;
break;
case "delete":
IsDelete = true;
break;
case "from":
IsFrom = true;
break;
case "count(*)":
case "count(0)":
case "count(1)":
IsGetCount = true;
break;
case "limit":
if (IsSelect && (IsFrom || IsWhere))
{
IsLimit = true;
}
break;
case "offset":
if (IsLimit)
{
IsLimit = false;
IsOffset = true;
}
break;
case "order":
if (IsFrom || IsWhere)
{
IsOrder = true;
}
break;
case "by":
case "where":
if (IsFrom || IsUpdate)
{
IsFrom = false;
IsWhere = true;
int startIndex = sqlText.LastIndexOf(" " + item + " ") + item.Length + 2;
int limit = sqlText.IndexOf(" limit ", startIndex, StringComparison.OrdinalIgnoreCase);
if (limit == -1)
{
Where = sqlText.Substring(startIndex);
}
else
{
Where = sqlText.Substring(startIndex, limit - startIndex);
}
if (item.ToLower() == "by")
{
Where = "order by " + Where;
}
}
break;
case "top":
if (IsSelect && !IsFrom)
{
IsTopN = true;
}
break;
case "distinct":
if (IsSelect && !IsFrom)
{
IsDistinct = true;
}
break;
case "set":
if (IsUpdate && !string.IsNullOrEmpty(TableName) && FieldItems.Count == 0)
{
#region 解析Update的字段与值
int start = sqlText.IndexOf(item) + item.Length;
int end = sqlText.ToLower().IndexOf("where");
string itemText = sqlText.Substring(start, end == -1 ? sqlText.Length - start : end - start);
int quoteCount = 0, commaIndex = 0;
for (int i = 0; i < itemText.Length; i++)
{
if (i == itemText.Length - 1)
{
string keyValue = itemText.Substring(commaIndex).Trim();
if (!FieldItems.Contains(keyValue))
{
FieldItems.Add(keyValue.Trim());
}
}
else
{
switch (itemText[i])
{
case '\'':
quoteCount++;
break;
case ',':
if (quoteCount % 2 == 0)//双数,则允许分隔。
{
string keyValue = itemText.Substring(commaIndex, i - commaIndex).Trim();
if (!FieldItems.Contains(keyValue))
{
FieldItems.Add(keyValue);
}
commaIndex = i + 1;
}
break;
}
}
}
#endregion
}
break;
default:
if (IsOffset)
{
if (!int.TryParse(item, out PageIndex))
{
PageIndex = 0;
}
}
else if (IsLimit)
{
if (!int.TryParse(item, out PageSize))
{
PageSize = 0;
}
}
if (!IsWhere)
{
if (IsTopN && TopN == -1)
{
if (int.TryParse(item, out TopN))//查询TopN
{
PageSize = TopN;
}
IsTopN = false;//关闭topN
}
else if ((IsFrom || IsUpdate || IsInsertInto) && string.IsNullOrEmpty(TableName))
{
TableName = item.Split('(')[0].Trim();//获取表名。
}
else if (IsSelect && !IsFrom)//提取查询的中间条件。
{
#region Select 字段搜集
switch (item)
{
case "*":
case "count(*)":
case "count(0)":
case "count(1)":
case "distinct":
break;
default:
fieldText.Append(item + " ");
break;
}
#endregion
}
else if (IsInsertInto && !string.IsNullOrEmpty(TableName) && FieldItems.Count == 0)
{
#region 解析Insert Into的字段与值
int start = sqlText.IndexOf(TableName) + TableName.Length;
int end = sqlText.IndexOf("values", start, StringComparison.OrdinalIgnoreCase);
if (end == -1)
{
end = sqlText.IndexOf("select", start, StringComparison.OrdinalIgnoreCase);
if (end == -1) { break; }
}
string keys = sqlText.Substring(start, end - start).Trim();
string[] keyItems = keys.Substring(1, keys.Length - 2).Split(',');//去除两边括号再按逗号分隔。
string values = sqlText.Substring(end + 6).Trim();
if (IsSelect && IsFrom)
{
end = values.IndexOf("from");
if (end > 0)
{
//insert into ...select ...from 模式
values = values.Substring(0, end);//去除两边括号
}
}
else
{
// insert into ... values 模式。
values = values.Substring(1, values.Length - 2);//去除两边括号
}
int quoteCount = 0, commaIndex = 0, valueIndex = 0;
#region get values
for (int i = 0; i < values.Length; i++)
{
if (valueIndex >= keyItems.Length)
{
break;
}
if (i == values.Length - 1)
{
string value = values.Substring(commaIndex).Trim();
keyItems[valueIndex] += "=" + value;
}
else
{
switch (values[i])
{
case '\'':
quoteCount++;
break;
case ',':
if (quoteCount % 2 == 0)//双数,则允许分隔。
{
string value = values.Substring(commaIndex, i - commaIndex).Trim();
keyItems[valueIndex] += "=" + value;
commaIndex = i + 1;
valueIndex++;
}
break;
}
}
}
#endregion
FieldItems.AddRange(keyItems);
#endregion
}
}
break;
}
#endregion
}
#region Select 字段解析
if (fieldText.Length > 0)
{
if (FieldItems.Count == 0)
{
string[] fields = fieldText.ToString().Split(',');
foreach (string item in fields)
{
FieldItems.Add(item.Trim());
}
}
fieldText.Length = 0;
}
#endregion
}
private StringBuilder fieldText = new StringBuilder();
}
}