-
Notifications
You must be signed in to change notification settings - Fork 1
/
DBQuery.java
481 lines (443 loc) · 11.5 KB
/
DBQuery.java
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package com.base.db;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.ArrayUtils;
import com.base.utils.CoreMap;
import com.base.utils.StrUtils;
/**
* @author binjoo
* @description 数据库SQL语句构建类
* @date 2014-11-20
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public class DBQuery {
/** 插入操作 */
public final static int INSERT = 0x01;
/** 删除操作 */
public final static int DELETE = 0x02;
/** 更新操作 */
public final static int UPDATE = 0x03;
/** 查询操作 */
public final static int SELECT = 0x04;
/** 表内连接查询 */
public final static String INNER_JOIN = "inner";
/** 表外连接查询 */
public final static String OUTER_JOIN = "outer";
/** 表左连接查询 */
public final static String LEFT_JOIN = "left";
/** 表右连接查询 */
public final static String RIGHT_JOIN = "right";
/** 表全连接查询 */
public final static String FULL_JOIN = "full";
/** 升序查询 */
public final static String SORT_ASC = "asc";
/** 降序查询 */
public final static String SORT_DESC = "desc";
private String sql;
private Object[] params;
private Object[] wheres;
private int action;
private String fields;
private String table;
private List<String[]> join;
private String where;
private String limit;
private String offset;
private String order;
private String group;
private String having;
private CoreMap rows;
public DBQuery() {
this.sql = "";
// this.params = new Object[0];
this.wheres = new Object[0];
this.action = 0;
this.fields = "*";
this.join = new ArrayList();
this.where = "";
this.limit = "";
this.offset = "";
this.order = "";
this.group = "";
this.having = "";
this.rows = new CoreMap();
}
public Object[] getParams() {
return this.params;
}
private String getColumnFromParameters(Object[] params) {
String[] fields = null;
for (int i = 0; i < params.length; i++) {
Object param = params[i];
if (param instanceof String[]) {
String[] as = (String[]) param;
fields = (String[]) ArrayUtils.add(fields, as[0] + " as "
+ as[1]);
} else if (param instanceof String) {
String field = (String) param;
fields = (String[]) ArrayUtils.add(fields, field);
} else {
throw new IllegalArgumentException("field 参数类型错误。");
}
}
return StrUtils.implode(" , ", fields);
}
public DBQuery insert() {
this.action = DBQuery.INSERT;
return this;
}
public DBQuery delete() {
this.action = DBQuery.DELETE;
return this;
}
public DBQuery update() {
this.action = DBQuery.UPDATE;
return this;
}
public DBQuery select(Object... fields) {
this.action = DBQuery.SELECT;
if (fields == null || fields.length == 0) {
this.fields = "*";
} else {
this.fields = getColumnFromParameters(fields);
}
return this;
}
public DBQuery from(String table) {
this.table = table;
return this;
}
public DBQuery join(String table, String condition) {
return join(table, condition, null);
}
public DBQuery join(String table, String condition, String op) {
if (op == null || op.equals("")) {
op = DBQuery.LEFT_JOIN;
}
String[] join = { table, condition, op };
this.join.add(join);
return this;
}
/**
* 排序(order by asc)
*
* @param orderby
* @param sort
* @return
*/
public DBQuery order(String orderby) {
return order(orderby, null);
}
/**
* 排序(order by)
*
* @param orderby
* @param sort
* @return
*/
public DBQuery order(String orderby, String sort) {
if (sort == null || sort.equals("")) {
sort = DBQuery.SORT_ASC;
}
if (this.order != null && !this.order.equals("")) {
this.order += ", " + orderby + " " + sort;
} else {
this.order = " order by " + orderby + " " + sort;
}
return this;
}
/**
* 分组函数
*
* @param group
* @return
*/
public DBQuery group(String group) {
this.group = " group by " + group;
return this;
}
public DBQuery limit(int limit) {
this.limit = " limit " + limit;
return this;
}
public DBQuery offset(int offset) {
this.offset = " offset " + offset;
return this;
}
public DBQuery page(int page, int size) {
this.limit = " limit " + size;
this.offset = " offset " + (((page > 0 ? page : 1) - 1) * size);
return this;
}
public DBQuery where(Object... params) {
if (params == null) {
return this;
}
String condition = (String) params[0];
if (this.where != null && !this.where.equals("")) {
this.where += " and" + " (" + condition + ")";
} else {
this.where += " where" + " (" + condition + ")";
}
if (params.length > 1) {
params = ArrayUtils.remove(params, 0);
this.wheres = ArrayUtils.addAll(this.wheres, params);
}
return this;
}
public DBQuery rows(CoreMap rows) {
this.rows.putAll(rows);
return this;
}
/**
* 生成插入SQL
*
* @return
*/
private String buildInsert() {
StringBuffer out = new StringBuffer("insert into ");
out.append(this.table);
Object[] k = new String[0];
Object[] v = new String[0];
Iterator it = rows.entrySet().iterator();
while (it.hasNext()) {
Map.Entry e = (Map.Entry) it.next();
Object key = e.getKey();
Object value = e.getValue();
if (value == null) {
continue;
}
k = ArrayUtils.add(k, key);
v = ArrayUtils.add(v, "?");
this.params = ArrayUtils.add(this.params, value);
}
out.append(" (" + StrUtils.implode(" , ", k) + ") ");
out.append("values");
out.append(" (" + StrUtils.implode(" , ", v) + ") ");
return out.toString();
}
/**
* 生成删除SQL
*
* @return
*/
private String buildDelete() {
StringBuffer out = new StringBuffer("delete from ");
out.append(this.table);
out.append(this.where);
return out.toString();
}
/**
* 生成更新SQL
*
* @return
*/
private String buildUpdate() {
StringBuffer out = new StringBuffer("update ");
Object[] set = new String[0];
Iterator it = rows.entrySet().iterator();
while (it.hasNext()) {
Map.Entry e = (Map.Entry) it.next();
Object key = e.getKey();
Object value = e.getValue();
if (value == null) {
continue;
}
set = ArrayUtils.add(set, key + " = ?");
this.params = ArrayUtils.add(this.params, value);
}
out.append(this.table);
out.append(" set ");
out.append(StrUtils.implode(" , ", set));
out.append(this.where);
if (this.wheres.length > 0) {
for (int i = 0; i < this.wheres.length; i++) {
this.params = ArrayUtils.add(this.params, this.wheres[i]);
}
}
return out.toString();
}
/**
* 生成查询SQL
*
* @return
*/
private String buildSelect() {
return buildSelect(false);
}
private String buildSelect(boolean count) {
StringBuffer out = new StringBuffer("select ");
if (count) {
out.append("count(*) as stat from ");
} else {
out.append(this.fields + " from ");
}
if (this.join.size() > 0) {
for (int i = 0; i < this.join.size(); i++) {
String[] join = this.join.get(i);
this.table += " " + join[2] + " join " + join[0] + " on "
+ join[1];
}
}
out.append(this.table);
out.append(this.where);
if (this.wheres.length > 0) {
for (int i = 0; i < this.wheres.length; i++) {
this.params = ArrayUtils.add(this.params, this.wheres[i]);
}
}
out.append(this.group);
out.append(this.order);
if (!count) {
out.append(this.limit);
out.append(this.offset);
}
return out.toString();
}
/**
* 生成最终SQL语句
*
* @return
*/
public String build() {
this.params = new Object[0];
switch (this.action) {
case DBQuery.INSERT:
this.sql = buildInsert();
break;
case DBQuery.DELETE:
this.sql = buildDelete();
break;
case DBQuery.UPDATE:
this.sql = buildUpdate();
break;
case DBQuery.SELECT:
this.sql = buildSelect();
break;
default:
this.sql = null;
break;
}
return this.sql;
}
/**
* 生成数量统计SQL语句
*
* @return
*/
public String buildCount() {
return buildSelect(true);
}
public static void main(String[] args) {
DBQuery q = new DBQuery();
q.select("t1.id", "t1.name", "t2.remark").from("table_name t1");
q.join("table_name t2", "t2.t1_id = t1.id");
q.join("table_name t3", "t3.t2_id = t2.id", DBQuery.LEFT_JOIN);
System.out.println(q.build());
if(q.getParams().length >= 1){
System.out.print("> `Params: `{ ");
for (int i = 0; i < q.params.length; i++) {
if(i != 0){
System.out.print(", ");
}
Object param = q.params[i];
if (param instanceof String) {
System.out.print("\"" + String.valueOf(param) + "\"");
} else if (param instanceof Integer) {
System.out.print(Integer.valueOf(String.valueOf(param)));
}
}
System.out.print(" }");
}
// DBQuery q = new DBQuery();
// String[] n = {"name", "n"};
// String[] a = {"age", "a"};
// String[] e = {"email", "e"};
// q.select(n,"x").from("table_name");
// q.join("table_level", "table_level.user_id = table_name.id");
// q.where("name = ? or user_id = ?", "binjoo",
// 15).where("nick like '%?%'", "你好").where("email = 'binjoo@qq.com'");
// q.page(5, 10).order("create_date");
//
// System.out.println(q.build());
// System.out.print(q.build());
//
// System.out.println(sql);
// for (int i = 0; i < q.params.length; i++) {
// Object param = q.params[i];
// if(param instanceof String){
// System.out.println("String : " + String.valueOf(param));
// }else if(param instanceof Integer){
// System.out.println("Integer : " +
// Integer.valueOf(String.valueOf(param)));
// }
// }
//
// DBQuery ii = new DBQuery();
// ii.insert();
// CoreMap map = new CoreMap();
// map.put("name", "binjoo");
// map.put("age", 12);
// map.put("url", "http://baid.com");
// ii.rows(map);
// System.out.println(ii.build());
// for (int i = 0; i < ii.params.length; i++) {
// Object param = ii.params[i];
// if(param instanceof String){
// System.out.println("String : " + String.valueOf(param));
// }else if(param instanceof Integer){
// System.out.println("Integer : " +
// Integer.valueOf(String.valueOf(param)));
// }
// }
//
// DBQuery up = new DBQuery();
// up.update().from("tab_name");
// CoreMap map1 = new CoreMap();
// map1.put("name", "binjoo");
// map1.put("age", 12);
// map1.put("url", "http://baid.com");
// up.rows(map1);
// up.where("id = ?", "16");
// System.out.println(up.build());
// for (int i = 0; i < up.getParams().length; i++) {
// Object param = up.params[i];
// if (param instanceof String) {
// System.out.println("String : " + String.valueOf(param));
// } else if (param instanceof Integer) {
// System.out.println("Integer : "
// + Integer.valueOf(String.valueOf(param)));
// }
// }
// System.out.println(up.build());
// for (int i = 0; i < up.getParams().length; i++) {
// Object param = up.params[i];
// if (param instanceof String) {
// System.out.println("String : " + String.valueOf(param));
// } else if (param instanceof Integer) {
// System.out.println("Integer : "
// + Integer.valueOf(String.valueOf(param)));
// }
// }
// DBQuery d = new DBQuery();
// d.delete().from("table_name");
// d.where("name = ? or user_id = ?", "binjoo", 15).where("nick = ?",
// "你好").where("email = 'binjoo@qq.com'");
// d.page(5, 10).order("create_date");
//
// String sql1 = d.build();
//
// System.out.println(sql1);
// for (int i = 0; i < d.params.length; i++) {
// Object param = d.params[i];
// if(param instanceof String){
// System.out.println("String : " + String.valueOf(param));
// }else if(param instanceof Integer){
// System.out.println("Integer : " +
// Integer.valueOf(String.valueOf(param)));
// }
// }
}
}