1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
package org.asyrinx.brownie.core.sql;
|
9
|
|
|
10
|
|
import java.util.Iterator;
|
11
|
|
|
12
|
|
import org.apache.commons.lang.StringUtils;
|
13
|
|
|
14
|
|
|
15
|
|
|
16
|
|
|
17
|
|
public class BasicSqlBuilder implements IBuilder {
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
|
22
|
3
|
public BasicSqlBuilder() {
|
23
|
3
|
super();
|
24
|
|
}
|
25
|
|
|
26
|
|
private char tablePrefixDelimiter = '.';
|
27
|
|
|
28
|
|
private String aliasSigniture = "as";
|
29
|
|
|
30
|
|
|
31
|
|
|
32
|
|
|
33
|
|
|
34
|
|
|
35
|
6
|
public String buildField(String tableAlias, String fieldName,
|
36
|
|
String aliasName, String functionName) {
|
37
|
6
|
final StringBuffer val = new StringBuffer();
|
38
|
6
|
if (StringUtils.isNotEmpty(functionName)) {
|
39
|
0
|
val.append(functionName);
|
40
|
0
|
val.append("(");
|
41
|
|
}
|
42
|
6
|
if (StringUtils.isNotEmpty(tableAlias)) {
|
43
|
0
|
val.append(tableAlias);
|
44
|
0
|
val.append(getTablePrefixDelimiter());
|
45
|
|
}
|
46
|
6
|
val.append(fieldName);
|
47
|
6
|
if (StringUtils.isNotEmpty(functionName))
|
48
|
0
|
val.append(")");
|
49
|
6
|
if (StringUtils.isNotEmpty(aliasName)) {
|
50
|
0
|
if (StringUtils.isNotEmpty(getAliasSigniture())) {
|
51
|
0
|
val.append(" ");
|
52
|
0
|
val.append(getAliasSigniture());
|
53
|
0
|
val.append(" ");
|
54
|
|
}
|
55
|
0
|
val.append(aliasName);
|
56
|
|
}
|
57
|
6
|
return val.toString();
|
58
|
|
}
|
59
|
|
|
60
|
|
private String indexPrefix = "( INDEX ";
|
61
|
|
|
62
|
|
private String indexSuffix = ")";
|
63
|
|
|
64
|
|
|
65
|
|
|
66
|
|
|
67
|
|
|
68
|
|
|
69
|
3
|
public String buildTable(String tablePrefix, String tableName,
|
70
|
|
String tableAlias, String indexName) {
|
71
|
3
|
final StringBuffer result = new StringBuffer();
|
72
|
3
|
if (StringUtils.isNotEmpty(tablePrefix)) {
|
73
|
0
|
if (tablePrefix.charAt(tablePrefix.length() - 1) != getTablePrefixDelimiter())
|
74
|
0
|
tablePrefix += getTablePrefixDelimiter();
|
75
|
0
|
result.append(tablePrefix);
|
76
|
|
}
|
77
|
3
|
result.append(tableName);
|
78
|
3
|
if (StringUtils.isNotEmpty(tableAlias)) {
|
79
|
0
|
result.append(" ");
|
80
|
0
|
result.append(tableAlias);
|
81
|
|
}
|
82
|
3
|
if (StringUtils.isNotEmpty(indexName)) {
|
83
|
0
|
result.append(" ");
|
84
|
0
|
result.append(getIndexPrefix() + indexName + getIndexSuffix());
|
85
|
|
}
|
86
|
3
|
return result.toString();
|
87
|
|
}
|
88
|
|
|
89
|
|
private char quote = '\'';
|
90
|
|
|
91
|
|
|
92
|
|
|
93
|
|
|
94
|
|
|
95
|
|
|
96
|
8
|
public String buildCondition(String fieldName, String value,
|
97
|
|
Operator operator, boolean withQuote) {
|
98
|
8
|
if (operator == null)
|
99
|
0
|
operator = Operator.EQUAL;
|
100
|
8
|
if (withQuote)
|
101
|
0
|
value = getQuote() + value + getQuote();
|
102
|
8
|
return fieldName + " " + operator.toString() + " " + value;
|
103
|
|
}
|
104
|
|
|
105
|
|
|
106
|
|
|
107
|
|
|
108
|
|
|
109
|
3
|
public void build(DynamicSelect dynamicSelect, StringBuffer dest) {
|
110
|
3
|
buildSelect(dest, dynamicSelect);
|
111
|
3
|
buildFrom(dest, dynamicSelect.getFromTables());
|
112
|
3
|
buildWhere(dest, dynamicSelect.getWhereConditions());
|
113
|
3
|
buildGroupBy(dest, dynamicSelect.getGroupByFields());
|
114
|
3
|
buildHaving(dest, dynamicSelect.getHavingConditions());
|
115
|
3
|
buildOrderBy(dest, dynamicSelect.getOrderByFields());
|
116
|
|
}
|
117
|
|
|
118
|
3
|
protected void buildSelect(StringBuffer dest, DynamicSelect dynamicSelect) {
|
119
|
3
|
if (dynamicSelect.getSelectFields().isEmpty())
|
120
|
1
|
return;
|
121
|
2
|
dest.append("select ");
|
122
|
2
|
if (dynamicSelect.isDistinct())
|
123
|
0
|
dest.append(" distinct ");
|
124
|
2
|
buildFields(dest, dynamicSelect.getSelectFields());
|
125
|
2
|
dest.append(" ");
|
126
|
|
}
|
127
|
|
|
128
|
3
|
protected void buildFrom(StringBuffer dest, Tables fromTables) {
|
129
|
3
|
if (fromTables.isEmpty())
|
130
|
0
|
return;
|
131
|
3
|
dest.append("from ");
|
132
|
3
|
buildTables(dest, fromTables);
|
133
|
3
|
dest.append(" ");
|
134
|
|
}
|
135
|
|
|
136
|
3
|
protected void buildWhere(StringBuffer dest, Conditions conditions) {
|
137
|
3
|
if (conditions.isEmpty())
|
138
|
0
|
return;
|
139
|
3
|
dest.append("where ");
|
140
|
3
|
buildConditions(dest, conditions);
|
141
|
3
|
dest.append(" ");
|
142
|
|
}
|
143
|
|
|
144
|
3
|
protected void buildGroupBy(StringBuffer dest, Fields groupByFields) {
|
145
|
3
|
if (groupByFields.isEmpty())
|
146
|
3
|
return;
|
147
|
0
|
dest.append("group by ");
|
148
|
0
|
buildFields(dest, groupByFields);
|
149
|
0
|
dest.append(" ");
|
150
|
|
}
|
151
|
|
|
152
|
3
|
protected void buildHaving(StringBuffer dest, Conditions havingConditions) {
|
153
|
3
|
if (havingConditions.isEmpty())
|
154
|
3
|
return;
|
155
|
0
|
dest.append("having ");
|
156
|
0
|
buildConditions(dest, havingConditions);
|
157
|
0
|
dest.append(" ");
|
158
|
|
}
|
159
|
|
|
160
|
3
|
protected void buildOrderBy(StringBuffer dest, Fields orderByFields) {
|
161
|
3
|
if (orderByFields.isEmpty())
|
162
|
3
|
return;
|
163
|
0
|
dest.append("order by ");
|
164
|
0
|
buildFields(dest, orderByFields);
|
165
|
0
|
dest.append(" ");
|
166
|
|
}
|
167
|
|
|
168
|
2
|
protected void buildFields(StringBuffer dest, Fields fields) {
|
169
|
2
|
final Iterator iterator = fields.iterator();
|
170
|
2
|
boolean first = true;
|
171
|
2
|
while (iterator.hasNext()) {
|
172
|
6
|
if (first)
|
173
|
2
|
first = false;
|
174
|
|
else
|
175
|
4
|
dest.append(fields.getDelimiter());
|
176
|
6
|
final Object field = iterator.next();
|
177
|
6
|
if (field instanceof Fields) {
|
178
|
0
|
buildFields(dest, (Fields) field);
|
179
|
|
} else {
|
180
|
6
|
dest.append(field);
|
181
|
|
}
|
182
|
|
}
|
183
|
|
}
|
184
|
|
|
185
|
3
|
protected void buildTables(StringBuffer dest, Tables tables) {
|
186
|
3
|
final Iterator iterator = tables.iterator();
|
187
|
3
|
boolean first = true;
|
188
|
3
|
while (iterator.hasNext()) {
|
189
|
3
|
if (first)
|
190
|
3
|
first = false;
|
191
|
|
else
|
192
|
0
|
dest.append(tables.getDelimiter());
|
193
|
3
|
final Object table = iterator.next();
|
194
|
3
|
if (table instanceof Tables) {
|
195
|
0
|
buildTables(dest, (Tables) table);
|
196
|
|
} else {
|
197
|
3
|
dest.append(table);
|
198
|
|
}
|
199
|
|
}
|
200
|
|
}
|
201
|
|
|
202
|
5
|
protected void buildConditions(StringBuffer dest, Conditions conditions) {
|
203
|
5
|
final Iterator iterator = conditions.iterator();
|
204
|
5
|
boolean first = true;
|
205
|
5
|
while (iterator.hasNext()) {
|
206
|
10
|
if (first) {
|
207
|
5
|
first = false;
|
208
|
|
} else {
|
209
|
5
|
dest.append(" ");
|
210
|
5
|
dest.append(conditions.getConnection());
|
211
|
5
|
dest.append(" ");
|
212
|
|
}
|
213
|
10
|
final Object condition = iterator.next();
|
214
|
10
|
if (condition instanceof Conditions) {
|
215
|
2
|
dest.append("(");
|
216
|
2
|
buildConditions(dest, (Conditions) condition);
|
217
|
2
|
dest.append(")");
|
218
|
|
} else {
|
219
|
8
|
dest.append(condition);
|
220
|
|
}
|
221
|
|
}
|
222
|
|
}
|
223
|
|
|
224
|
|
|
225
|
|
|
226
|
|
|
227
|
0
|
public String getIndexPrefix() {
|
228
|
0
|
return indexPrefix;
|
229
|
|
}
|
230
|
|
|
231
|
|
|
232
|
|
|
233
|
|
|
234
|
0
|
public String getIndexSuffix() {
|
235
|
0
|
return indexSuffix;
|
236
|
|
}
|
237
|
|
|
238
|
|
|
239
|
|
|
240
|
|
|
241
|
0
|
public char getTablePrefixDelimiter() {
|
242
|
0
|
return tablePrefixDelimiter;
|
243
|
|
}
|
244
|
|
|
245
|
|
|
246
|
|
|
247
|
|
|
248
|
0
|
public void setIndexPrefix(String string) {
|
249
|
0
|
indexPrefix = string;
|
250
|
|
}
|
251
|
|
|
252
|
|
|
253
|
|
|
254
|
|
|
255
|
0
|
public void setIndexSuffix(String string) {
|
256
|
0
|
indexSuffix = string;
|
257
|
|
}
|
258
|
|
|
259
|
|
|
260
|
|
|
261
|
|
|
262
|
0
|
public void setTablePrefixDelimiter(char string) {
|
263
|
0
|
tablePrefixDelimiter = string;
|
264
|
|
}
|
265
|
|
|
266
|
|
|
267
|
|
|
268
|
|
|
269
|
0
|
public char getQuote() {
|
270
|
0
|
return quote;
|
271
|
|
}
|
272
|
|
|
273
|
|
|
274
|
|
|
275
|
|
|
276
|
0
|
public void setQuote(char c) {
|
277
|
0
|
quote = c;
|
278
|
|
}
|
279
|
|
|
280
|
|
|
281
|
|
|
282
|
|
|
283
|
0
|
public String getAliasSigniture() {
|
284
|
0
|
return aliasSigniture;
|
285
|
|
}
|
286
|
|
|
287
|
|
|
288
|
|
|
289
|
|
|
290
|
0
|
public void setAliasSigniture(String string) {
|
291
|
0
|
aliasSigniture = string;
|
292
|
|
}
|
293
|
|
|
294
|
|
}
|