1
|
|
|
2
|
|
#region Apache Notice
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
|
9
|
|
|
10
|
|
|
11
|
|
|
12
|
|
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
#endregion
|
26
|
|
|
27
|
|
|
28
|
|
using System;
|
29
|
|
using System.Text;
|
30
|
|
using System.Collections.Specialized;
|
31
|
|
|
32
|
|
using IBatisNet.DataMapper.Configuration.ParameterMapping;
|
33
|
|
|
34
|
|
namespace IBatisNet.DataMapper.Configuration.Statements
|
35
|
|
{
|
36
|
|
|
37
|
|
|
38
|
|
|
39
|
|
public class SqlGenerator
|
40
|
|
{
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
|
|
46
|
850
|
public static string BuildQuery(IStatement statement)
|
47
|
|
{
|
48
|
850
|
string sqlText = string.Empty;
|
49
|
|
|
50
|
850
|
if (statement is Select)
|
51
|
|
{
|
52
|
340
|
sqlText = BuildSelectQuery(statement);
|
53
|
|
}
|
54
|
510
|
else if (statement is Insert)
|
55
|
|
{
|
56
|
170
|
sqlText = BuildInsertQuery(statement);
|
57
|
|
}
|
58
|
340
|
else if (statement is Update)
|
59
|
|
{
|
60
|
170
|
sqlText = BuildUpdateQuery(statement);
|
61
|
|
}
|
62
|
170
|
else if (statement is Delete)
|
63
|
|
{
|
64
|
170
|
sqlText = BuildDeleteQuery(statement);
|
65
|
|
}
|
66
|
|
|
67
|
850
|
return sqlText;
|
68
|
|
}
|
69
|
|
|
70
|
|
|
71
|
|
|
72
|
|
|
73
|
|
|
74
|
|
|
75
|
|
|
76
|
340
|
private static string BuildSelectQuery(IStatement statement)
|
77
|
|
{
|
78
|
340
|
StringBuilder output = new StringBuilder();
|
79
|
340
|
Select select = (Select) statement;
|
80
|
340
|
int columnCount = statement.ParameterMap.PropertiesList.Count;
|
81
|
|
|
82
|
340
|
output.Append("SELECT ");
|
83
|
|
|
84
|
|
|
85
|
1360
|
for (int i = 0; i < columnCount; i++)
|
86
|
|
{
|
87
|
1020
|
ParameterProperty property = (ParameterProperty) statement.ParameterMap.PropertiesList[i];
|
88
|
|
|
89
|
1020
|
if (i < (columnCount - 1))
|
90
|
|
{
|
91
|
680
|
output.Append("\t" + property.ColumnName + " as "+property.PropertyName+",");
|
92
|
|
}
|
93
|
|
else
|
94
|
|
{
|
95
|
340
|
output.Append("\t" + property.ColumnName + " as "+property.PropertyName);
|
96
|
|
}
|
97
|
|
}
|
98
|
|
|
99
|
340
|
output.Append(" FROM ");
|
100
|
340
|
output.Append("\t" + select.Generate.Table + "");
|
101
|
|
|
102
|
|
|
103
|
|
|
104
|
340
|
string [] compositeKeyList = select.Generate.By.Split(new Char [] {','});
|
105
|
340
|
if (compositeKeyList.Length > 0 && select.Generate.By.Length>0)
|
106
|
|
{
|
107
|
170
|
output.Append(" WHERE ");
|
108
|
|
|
109
|
340
|
for (int i = 0; i < compositeKeyList.Length; i++)
|
110
|
|
{
|
111
|
170
|
string columnName = compositeKeyList[i];
|
112
|
|
|
113
|
170
|
if (i > 0)
|
114
|
|
{
|
115
|
0
|
output.Append("\tAND " + columnName + " = ?" );
|
116
|
|
}
|
117
|
|
else
|
118
|
|
{
|
119
|
170
|
output.Append("\t" + columnName + " = ?" );
|
120
|
|
}
|
121
|
|
}
|
122
|
|
}
|
123
|
|
|
124
|
|
|
125
|
340
|
if (statement.ParameterClass == null)
|
126
|
|
{
|
127
|
|
|
128
|
|
|
129
|
170
|
statement.ParameterMap = null;
|
130
|
|
}
|
131
|
|
|
132
|
340
|
return output.ToString();
|
133
|
|
}
|
134
|
|
|
135
|
|
|
136
|
|
|
137
|
|
|
138
|
|
|
139
|
|
|
140
|
|
|
141
|
170
|
private static string BuildInsertQuery(IStatement statement)
|
142
|
|
{
|
143
|
170
|
StringBuilder output = new StringBuilder();
|
144
|
170
|
Insert insert = (Insert) statement;
|
145
|
170
|
int columnCount = statement.ParameterMap.PropertiesList.Count;
|
146
|
|
|
147
|
170
|
output.Append("INSERT INTO " + insert.Generate.Table + " (");
|
148
|
|
|
149
|
|
|
150
|
510
|
for (int i = 0; i < columnCount; i++)
|
151
|
|
{
|
152
|
340
|
ParameterProperty property = (ParameterProperty) statement.ParameterMap.PropertiesList[i];
|
153
|
|
|
154
|
|
|
155
|
340
|
if (i < (columnCount - 1))
|
156
|
|
{
|
157
|
170
|
output.Append("\t" + property.ColumnName + ",");
|
158
|
|
}
|
159
|
|
else
|
160
|
|
{
|
161
|
170
|
output.Append("\t" + property.ColumnName + "");
|
162
|
|
}
|
163
|
|
}
|
164
|
|
|
165
|
170
|
output.Append(") VALUES (");
|
166
|
|
|
167
|
|
|
168
|
510
|
for (int i = 0; i < columnCount; i++)
|
169
|
|
{
|
170
|
340
|
ParameterProperty property = (ParameterProperty) statement.ParameterMap.PropertiesList[i];
|
171
|
|
|
172
|
|
|
173
|
340
|
if (i < (columnCount - 1))
|
174
|
|
{
|
175
|
170
|
output.Append("\t?,");
|
176
|
|
}
|
177
|
|
else
|
178
|
|
{
|
179
|
170
|
output.Append("\t?");
|
180
|
|
}
|
181
|
|
}
|
182
|
|
|
183
|
170
|
output.Append(")");
|
184
|
|
|
185
|
170
|
return output.ToString();
|
186
|
|
}
|
187
|
|
|
188
|
|
|
189
|
|
|
190
|
|
|
191
|
|
|
192
|
|
|
193
|
|
|
194
|
170
|
private static string BuildUpdateQuery(IStatement statement)
|
195
|
|
{
|
196
|
170
|
StringBuilder output = new StringBuilder();
|
197
|
170
|
Update update = (Update) statement;
|
198
|
170
|
int columnCount = statement.ParameterMap.PropertiesList.Count;
|
199
|
170
|
string[] keysList = update.Generate.By.Split(',');
|
200
|
|
|
201
|
170
|
output.Append("UPDATE ");
|
202
|
170
|
output.Append("\t" + update.Generate.Table + " ");
|
203
|
170
|
output.Append("SET ");
|
204
|
|
|
205
|
|
|
206
|
680
|
for (int i = 0; i < columnCount; i++)
|
207
|
|
{
|
208
|
510
|
ParameterProperty property = (ParameterProperty) statement.ParameterMap.PropertiesList[i];
|
209
|
|
|
210
|
|
|
211
|
510
|
if (update.Generate.By.IndexOf(property.ColumnName) < 0)
|
212
|
|
{
|
213
|
340
|
if (i < (columnCount-keysList.Length - 1))
|
214
|
|
{
|
215
|
170
|
output.Append("\t" + property.ColumnName + " = ?,");
|
216
|
|
}
|
217
|
|
else
|
218
|
|
{
|
219
|
170
|
output.Append("\t" + property.ColumnName + " = ? ");
|
220
|
|
}
|
221
|
|
}
|
222
|
|
}
|
223
|
|
|
224
|
170
|
output.Append(" WHERE ");
|
225
|
|
|
226
|
|
|
227
|
340
|
for (int i = 0; i < keysList.Length; i++)
|
228
|
|
{
|
229
|
170
|
string columnName = keysList[i];
|
230
|
|
|
231
|
170
|
if (i > 0)
|
232
|
|
{
|
233
|
0
|
output.Append("\tAND " + columnName + " = ?");
|
234
|
|
}
|
235
|
|
else
|
236
|
|
{
|
237
|
170
|
output.Append("\t " + columnName + " = ?");
|
238
|
|
}
|
239
|
|
}
|
240
|
|
|
241
|
170
|
return output.ToString();
|
242
|
|
}
|
243
|
|
|
244
|
|
|
245
|
|
|
246
|
|
|
247
|
|
|
248
|
|
|
249
|
170
|
private static string BuildDeleteQuery(IStatement statement)
|
250
|
|
{
|
251
|
170
|
StringBuilder output = new StringBuilder();
|
252
|
170
|
Delete delete = (Delete) statement;
|
253
|
170
|
string[] keysList = delete.Generate.By.Split(',');
|
254
|
|
|
255
|
170
|
output.Append("DELETE FROM");
|
256
|
170
|
output.Append("\t" + delete.Generate.Table + "");
|
257
|
170
|
output.Append(" WHERE ");
|
258
|
|
|
259
|
|
|
260
|
510
|
for (int i = 0; i < keysList.Length; i++)
|
261
|
|
{
|
262
|
340
|
string columnName = keysList[i].Trim();
|
263
|
|
|
264
|
340
|
if (i > 0)
|
265
|
|
{
|
266
|
170
|
output.Append("\tAND " + columnName + " = ?");
|
267
|
|
}
|
268
|
|
else
|
269
|
|
{
|
270
|
170
|
output.Append("\t " + columnName + " = ?");
|
271
|
|
}
|
272
|
|
}
|
273
|
|
|
274
|
170
|
return output.ToString();
|
275
|
|
}
|
276
|
|
}
|
277
|
|
}
|
278
|
|
|