1
|
|
|
2
|
|
|
3
|
|
|
4
|
|
|
5
|
|
|
6
|
|
|
7
|
|
|
8
|
|
package org.asyrinx.brownie.core.sql2;
|
9
|
|
|
10
|
|
import java.util.Collection;
|
11
|
|
import java.util.Iterator;
|
12
|
|
|
13
|
|
import org.apache.commons.collections.IteratorUtils;
|
14
|
|
import org.asyrinx.brownie.core.lang.ArrayUtils;
|
15
|
|
import org.asyrinx.brownie.core.lang.StringUtils;
|
16
|
|
import org.asyrinx.brownie.core.sql.Operator;
|
17
|
|
|
18
|
|
|
19
|
|
|
20
|
|
|
21
|
|
public class BasicSqlBuilder implements Visitor, SqlBuilder {
|
22
|
|
|
23
|
|
|
24
|
|
|
25
|
|
|
26
|
6
|
public BasicSqlBuilder() {
|
27
|
6
|
super();
|
28
|
|
}
|
29
|
|
|
30
|
|
private StringBuffer result = null;
|
31
|
|
|
32
|
|
private StringBuffer work = null;
|
33
|
|
|
34
|
|
|
35
|
|
|
36
|
|
|
37
|
6
|
public String toSql(Select select) {
|
38
|
6
|
select.accept(this);
|
39
|
6
|
return result.toString();
|
40
|
|
}
|
41
|
|
|
42
|
|
|
43
|
|
|
44
|
|
|
45
|
6
|
public void visit(Select select) {
|
46
|
6
|
result = new StringBuffer();
|
47
|
6
|
work = new StringBuffer();
|
48
|
6
|
select.getSelectFields().accept(this);
|
49
|
6
|
if (work.length() > 0) {
|
50
|
5
|
result.append("select ");
|
51
|
5
|
if (select.isDistinct())
|
52
|
0
|
result.append("distinct ");
|
53
|
5
|
result.append(work);
|
54
|
|
}
|
55
|
6
|
work = new StringBuffer();
|
56
|
6
|
select.getFromTables().accept(this);
|
57
|
6
|
if (work.length() > 0) {
|
58
|
6
|
if (result.length() > 0)
|
59
|
5
|
result.append(" ");
|
60
|
6
|
result.append("from ");
|
61
|
6
|
result.append(work);
|
62
|
|
}
|
63
|
6
|
work = new StringBuffer();
|
64
|
6
|
select.getWhereConditions().accept(this);
|
65
|
6
|
if (work.length() > 0) {
|
66
|
6
|
if (result.length() > 0)
|
67
|
6
|
result.append(" ");
|
68
|
6
|
result.append("where ");
|
69
|
6
|
result.append(work);
|
70
|
|
}
|
71
|
6
|
work = new StringBuffer();
|
72
|
6
|
select.getGroupByFields().accept(this);
|
73
|
6
|
if (work.length() > 0) {
|
74
|
0
|
if (result.length() > 0)
|
75
|
0
|
result.append(" ");
|
76
|
0
|
result.append("group by ");
|
77
|
0
|
result.append(work);
|
78
|
|
}
|
79
|
6
|
work = new StringBuffer();
|
80
|
6
|
select.getHavingConditions().accept(this);
|
81
|
6
|
if (work.length() > 0) {
|
82
|
0
|
if (result.length() > 0)
|
83
|
0
|
result.append(" ");
|
84
|
0
|
result.append("having ");
|
85
|
0
|
result.append(work);
|
86
|
|
}
|
87
|
6
|
work = new StringBuffer();
|
88
|
6
|
select.getOrderByFields().accept(this);
|
89
|
6
|
if (work.length() > 0) {
|
90
|
0
|
if (result.length() > 0)
|
91
|
0
|
result.append(" ");
|
92
|
0
|
result.append("order by ");
|
93
|
0
|
result.append(work);
|
94
|
|
}
|
95
|
|
}
|
96
|
|
|
97
|
40
|
protected String toString(Object value) {
|
98
|
40
|
if (value instanceof String) {
|
99
|
9
|
return StringUtils.toQuoted((String) value, '\'');
|
100
|
31
|
} else if (value instanceof Character) {
|
101
|
6
|
return StringUtils.toQuoted(value + "", '\'');
|
102
|
25
|
} else if (value.getClass().isArray()) {
|
103
|
6
|
if (value.getClass().getComponentType().isPrimitive()) {
|
104
|
4
|
return toString(IteratorUtils.arrayIterator(ArrayUtils
|
105
|
|
.toObjectArray(value)));
|
106
|
|
} else {
|
107
|
2
|
return toString(IteratorUtils.arrayIterator((Object[]) value));
|
108
|
|
}
|
109
|
19
|
} else if (value instanceof Collection) {
|
110
|
0
|
final Collection collection = (Collection) value;
|
111
|
0
|
if (collection.isEmpty())
|
112
|
0
|
return null;
|
113
|
|
else
|
114
|
0
|
return toString(((Collection) value).iterator());
|
115
|
19
|
} else if (value instanceof Iterator) {
|
116
|
0
|
return toString((Iterator) value);
|
117
|
|
} else {
|
118
|
19
|
return String.valueOf(value);
|
119
|
|
}
|
120
|
|
}
|
121
|
|
|
122
|
6
|
protected String toString(Iterator iterator) {
|
123
|
6
|
boolean isFirst = true;
|
124
|
6
|
final StringBuffer buf = new StringBuffer();
|
125
|
6
|
while (iterator.hasNext()) {
|
126
|
14
|
if (isFirst)
|
127
|
5
|
isFirst = false;
|
128
|
|
else
|
129
|
9
|
buf.append(", ");
|
130
|
14
|
Object value = iterator.next();
|
131
|
14
|
buf.append(toString(value));
|
132
|
|
}
|
133
|
6
|
final String res = buf.toString();
|
134
|
6
|
if ("".equals(res))
|
135
|
1
|
return null;
|
136
|
|
else
|
137
|
5
|
return res;
|
138
|
|
}
|
139
|
|
|
140
|
|
|
141
|
|
|
142
|
|
|
143
|
26
|
public void visit(Condition condition) {
|
144
|
26
|
final String value = toString(condition.getValue());
|
145
|
26
|
if (value == null)
|
146
|
1
|
return;
|
147
|
25
|
if (work.length() > 0)
|
148
|
11
|
work.append(" ").append(condition.getConnection()).append(" ");
|
149
|
25
|
if (StringUtils.isNotEmpty(condition.getFieldName()))
|
150
|
25
|
work.append(condition.getFieldName()).append(" ");
|
151
|
25
|
work.append(condition.getOperator()).append(" ");
|
152
|
25
|
if (condition.getOperator() == Operator.IN)
|
153
|
5
|
work.append("(");
|
154
|
25
|
work.append(value);
|
155
|
25
|
if (condition.getOperator() == Operator.IN)
|
156
|
5
|
work.append(")");
|
157
|
|
}
|
158
|
|
|
159
|
|
|
160
|
|
|
161
|
|
|
162
|
20
|
public void visit(Conditions conditions) {
|
163
|
20
|
final Iterator iterator = conditions.iterator();
|
164
|
20
|
while (iterator.hasNext()) {
|
165
|
34
|
final Object object = iterator.next();
|
166
|
34
|
if (object instanceof Condition) {
|
167
|
26
|
((Condition) object).accept(this);
|
168
|
8
|
} else if (object instanceof Conditions) {
|
169
|
8
|
final StringBuffer workBakup = work;
|
170
|
8
|
work = new StringBuffer();
|
171
|
8
|
try {
|
172
|
8
|
((Conditions) object).accept(this);
|
173
|
|
} finally {
|
174
|
8
|
if (work.length() > 0) {
|
175
|
8
|
if (workBakup.length() > 0)
|
176
|
8
|
workBakup.append(" ").append(
|
177
|
|
conditions.getConnection()).append(" ");
|
178
|
8
|
workBakup.append("(");
|
179
|
8
|
workBakup.append(work);
|
180
|
8
|
workBakup.append(")");
|
181
|
|
}
|
182
|
8
|
work = workBakup;
|
183
|
|
}
|
184
|
|
}
|
185
|
|
}
|
186
|
|
}
|
187
|
|
|
188
|
|
|
189
|
|
|
190
|
|
|
191
|
15
|
public void visit(Field field) {
|
192
|
15
|
if (StringUtils.isNotEmpty(field.getFunctionName()))
|
193
|
0
|
work.append(field.getFunctionName()).append("(");
|
194
|
15
|
if (StringUtils.isNotEmpty(field.getTableAlias()))
|
195
|
0
|
work.append(field.getTableAlias()).append(".");
|
196
|
15
|
work.append(field.getFieldName());
|
197
|
15
|
if (StringUtils.isNotEmpty(field.getFunctionName()))
|
198
|
0
|
work.append(")");
|
199
|
15
|
if (StringUtils.isNotEmpty(field.getAliasName()))
|
200
|
0
|
work.append(" as ").append(field.getAliasName());
|
201
|
|
}
|
202
|
|
|
203
|
|
|
204
|
|
|
205
|
|
|
206
|
18
|
public void visit(Fields fields) {
|
207
|
18
|
boolean isFirst = true;
|
208
|
18
|
final Iterator iterator = fields.iterator();
|
209
|
18
|
while (iterator.hasNext()) {
|
210
|
15
|
if (!isFirst)
|
211
|
10
|
work.append(", ");
|
212
|
|
else
|
213
|
5
|
isFirst = false;
|
214
|
15
|
final Object object = iterator.next();
|
215
|
15
|
if (object instanceof Field) {
|
216
|
15
|
((Field) object).accept(this);
|
217
|
0
|
} else if (object instanceof Fields) {
|
218
|
0
|
((Fields) object).accept(this);
|
219
|
|
}
|
220
|
|
}
|
221
|
|
}
|
222
|
|
|
223
|
|
|
224
|
|
|
225
|
|
|
226
|
6
|
public void visit(Table table) {
|
227
|
6
|
if (StringUtils.isNotEmpty(table.getTablePrefix()))
|
228
|
0
|
work.append(table.getTablePrefix()).append(".");
|
229
|
6
|
work.append(table.getTableName());
|
230
|
6
|
if (StringUtils.isNotEmpty(table.getTableAlias()))
|
231
|
0
|
work.append(" as ").append(table.getTableAlias());
|
232
|
6
|
if (StringUtils.isNotEmpty(table.getIndexName()))
|
233
|
0
|
work.append("(INDEX ").append(table.getIndexName()).append(")");
|
234
|
|
}
|
235
|
|
|
236
|
|
|
237
|
|
|
238
|
|
|
239
|
6
|
public void visit(Tables tables) {
|
240
|
6
|
boolean isFirst = true;
|
241
|
6
|
final Iterator iterator = tables.iterator();
|
242
|
6
|
while (iterator.hasNext()) {
|
243
|
6
|
if (!isFirst)
|
244
|
0
|
work.append(", ");
|
245
|
|
else
|
246
|
6
|
isFirst = false;
|
247
|
6
|
final Object object = iterator.next();
|
248
|
6
|
if (object instanceof Table) {
|
249
|
6
|
((Table) object).accept(this);
|
250
|
0
|
} else if (object instanceof Tables) {
|
251
|
0
|
((Tables) object).accept(this);
|
252
|
|
}
|
253
|
|
}
|
254
|
|
}
|
255
|
|
|
256
|
|
}
|