forked from opentiny/tiny-engine-backend-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicSqlProvider.java
More file actions
107 lines (84 loc) · 2.95 KB
/
DynamicSqlProvider.java
File metadata and controls
107 lines (84 loc) · 2.95 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
package com.tinyengine.it.dynamic.dao;
import org.apache.ibatis.jdbc.SQL;
import java.util.List;
import java.util.Map;
public class DynamicSqlProvider {
public String select(Map<String, Object> params) {
String tableName = (String) params.get("tableName");
List<String> fields = (List<String>) params.get("fields");
Map<String, Object> conditions = (Map<String, Object>) params.get("conditions");
Integer pageNum = (Integer) params.get("pageNum");
Integer pageSize = (Integer) params.get("pageSize");
String orderBy = (String) params.get("orderBy");
String orderType = (String) params.get("orderType");
SQL sql = new SQL();
// 选择字段
if (fields != null && !fields.isEmpty()) {
for (String field : fields) {
sql.SELECT(field);
}
} else {
sql.SELECT("*");
}
sql.FROM(tableName);
// 条件
if (conditions != null && !conditions.isEmpty()) {
for (Map.Entry<String, Object> entry : conditions.entrySet()) {
if (entry.getValue() != null) {
sql.WHERE(entry.getKey() + " = #{conditions." + entry.getKey() + "}");
}
}
}
// 排序
if (orderBy != null && !orderBy.isEmpty()) {
sql.ORDER_BY(orderBy + " " + orderType);
}
// 分页
if (pageNum != null && pageSize != null) {
return sql.toString() + " LIMIT " + (pageNum - 1) * pageSize + ", " + pageSize;
}
return sql.toString();
}
public String insert(Map<String, Object> params) {
String tableName = (String) params.get("tableName");
Map<String, Object> data = (Map<String, Object>) params.get("data");
SQL sql = new SQL();
sql.INSERT_INTO(tableName);
if (data != null && !data.isEmpty()) {
for (Map.Entry<String, Object> entry : data.entrySet()) {
sql.VALUES(entry.getKey(), "#{data." + entry.getKey() + "}");
}
}
return sql.toString();
}
public String update(Map<String, Object> params) {
String tableName = (String) params.get("tableName");
Map<String, Object> data = (Map<String, Object>) params.get("data");
Map<String, Object> conditions = (Map<String, Object>) params.get("conditions");
SQL sql = new SQL();
sql.UPDATE(tableName);
if (data != null && !data.isEmpty()) {
for (Map.Entry<String, Object> entry : data.entrySet()) {
sql.SET(entry.getKey() + " = #{data." + entry.getKey() + "}");
}
}
if (conditions != null && !conditions.isEmpty()) {
for (Map.Entry<String, Object> entry : conditions.entrySet()) {
sql.WHERE(entry.getKey() + " = #{conditions." + entry.getKey() + "}");
}
}
return sql.toString();
}
public String delete(Map<String, Object> params) {
String tableName = (String) params.get("tableName");
Map<String, Object> conditions = (Map<String, Object>) params.get("conditions");
SQL sql = new SQL();
sql.DELETE_FROM(tableName);
if (conditions != null && !conditions.isEmpty()) {
for (Map.Entry<String, Object> entry : conditions.entrySet()) {
sql.WHERE(entry.getKey() + " = #{conditions." + entry.getKey() + "}");
}
}
return sql.toString();
}
}