Wrapper.java
11.5 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
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
package com.skua.tool.query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.CaseFormat;
import com.skua.modules.common.service.ICommonSqlService;
import com.skua.tool.util.CustomApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.support.TransactionTemplate;
import java.lang.reflect.Field;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author sonin
* @date 2021/12/4 19:27
*/
public abstract class Wrapper implements IWrapper {
/**
* SQL拼接语句
*/
Collection<Class> classes;
Collection<String> conditions;
Collection<String> selectedColumns;
private String prefixSql;
private QueryWrapper<?> queryWrapper;
/**
* 构造返回字段
*
* @return
*/
String initColumns() {
StringBuilder stringBuilder = new StringBuilder();
for (Class clazz : this.classes) {
String className = clazz.getSimpleName();
String tableName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, className);
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
String classFieldName = field.getName();
String tableFieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, classFieldName);
String alias = DOUBLE_QUOTES + className + UNDERLINE + classFieldName + DOUBLE_QUOTES;
stringBuilder.append(COMMA).append(SPACE).append(tableName).append(DOT).append(tableFieldName).append(SPACE).append(AS).append(SPACE).append(alias);
}
}
return stringBuilder.toString().replaceFirst(COMMA + SPACE, EMPTY);
}
/**
* 判断SQL注入
*
* @param param
* @throws Exception
*/
private void sqlInject(String param) throws Exception {
Pattern pattern = Pattern.compile("\\b(and|exec|insert|select|drop|grant|alter|delete|update|count|chr|mid|master|truncate|char|declare|or)\\b|(\\*|;|\\+|')");
Matcher matcher = pattern.matcher(param.toLowerCase());
if (matcher.find()) {
throw new Exception("SQL注入: " + param);
}
}
/**
* 构造完整SQL
*
* @return
* @throws Exception
*/
private String initSql() throws Exception {
String suffixSql = this.queryWrapper.getCustomSqlSegment();
Map<String, Object> paramNameValuePairs = this.queryWrapper.getParamNameValuePairs();
for (Map.Entry<String, Object> item : paramNameValuePairs.entrySet()) {
Object value = item.getValue();
sqlInject("" + value);
if (value instanceof String) {
value = "'" + value + "'";
}
suffixSql = suffixSql.replaceFirst("#\\{ew\\.paramNameValuePairs\\." + item.getKey() + "}", "" + value);
}
return prefixSql + SPACE + suffixSql;
}
/**
* 选择查询字段,格式: DemoA_aName
*
* @param fields
* @return
*/
public Wrapper select(Field... fields) {
if (this.selectedColumns == null) {
this.selectedColumns = new LinkedHashSet<>();
}
for (Field field : fields) {
String className = field.getDeclaringClass().getSimpleName();
String tableName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, className);
String fieldName = field.getName();
String column = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, fieldName);
String alias = tableName + DOT + column + SPACE + AS + SPACE + DOUBLE_QUOTES + className + UNDERLINE + fieldName + DOUBLE_QUOTES;
this.selectedColumns.add(alias);
}
return this;
}
/**
* 选择查询字段,格式自定义
*
* @param fields
* @return
*/
public Wrapper select(String... fields) {
if (this.selectedColumns == null) {
this.selectedColumns = new LinkedHashSet<>();
}
this.selectedColumns.addAll(Arrays.asList(fields));
return this;
}
/**
* === 以下抽象方法 ===
*/
/**
* 构造前缀SQL
*
* @return
*/
public abstract String initPrefixSql();
public abstract Wrapper from(Class... classes);
public abstract Wrapper innerJoin(Class clazz, Field leftField, Field rightField);
public abstract Wrapper leftJoin(Class clazz, Field leftField, Field rightField);
public abstract Wrapper rightJoin(Class clazz, Field leftField, Field rightField);
public abstract Wrapper and(Field leftField, Field rightField);
/**
* 准备构造查询条件
*
* @return
*/
public Wrapper where() {
this.prefixSql = initPrefixSql();
this.queryWrapper = new QueryWrapper<>();
return this;
}
/**
* === 以下方式提供QueryWrapper构造条件 ===
*/
public Wrapper eq(boolean condition, String column, Object val) {
this.queryWrapper.eq(condition, column, val);
return this;
}
public Wrapper ne(boolean condition, String column, Object val) {
this.queryWrapper.ne(condition, column, val);
return this;
}
public Wrapper gt(boolean condition, String column, Object val) {
this.queryWrapper.gt(condition, column, val);
return this;
}
public Wrapper ge(boolean condition, String column, Object val) {
this.queryWrapper.ge(condition, column, val);
return this;
}
public Wrapper lt(boolean condition, String column, Object val) {
this.queryWrapper.lt(condition, column, val);
return this;
}
public Wrapper le(boolean condition, String column, Object val) {
this.queryWrapper.le(condition, column, val);
return this;
}
public Wrapper between(boolean condition, String column, Object val1, Object val2) {
this.queryWrapper.between(condition, column, val1, val2);
return this;
}
public Wrapper notBetween(boolean condition, String column, Object val1, Object val2) {
this.queryWrapper.notBetween(condition, column, val1, val2);
return this;
}
public Wrapper like(boolean condition, String column, Object val) {
this.queryWrapper.like(condition, column, val);
return this;
}
public Wrapper notLike(boolean condition, String column, Object val) {
this.queryWrapper.notLike(condition, column, val);
return this;
}
public Wrapper likeLeft(boolean condition, String column, Object val) {
this.queryWrapper.likeLeft(condition, column, val);
return this;
}
public Wrapper likeRight(boolean condition, String column, Object val) {
this.queryWrapper.likeRight(condition, column, val);
return this;
}
public Wrapper isNull(boolean condition, String column) {
this.queryWrapper.isNull(condition, column);
return this;
}
public Wrapper isNotNull(boolean condition, String column) {
this.queryWrapper.isNotNull(condition, column);
return this;
}
public Wrapper in(boolean condition, String column, Collection<?> coll) {
this.queryWrapper.in(condition, column, coll);
return this;
}
public Wrapper notIn(boolean condition, String column, Collection<?> coll) {
this.queryWrapper.notIn(condition, column, coll);
return this;
}
public Wrapper inSql(boolean condition, String column, String inValue) {
this.queryWrapper.inSql(condition, column, inValue);
return this;
}
public Wrapper notInSql(boolean condition, String column, String inValue) {
this.queryWrapper.notInSql(condition, column, inValue);
return this;
}
public Wrapper groupBy(boolean condition, String... columns) {
this.queryWrapper.groupBy(condition, columns);
return this;
}
public Wrapper having(boolean condition, String sqlHaving, Object... params) {
this.queryWrapper.having(condition, sqlHaving, params);
return this;
}
public Wrapper or(boolean condition) {
this.queryWrapper.or(condition);
return this;
}
public Wrapper apply(boolean condition, String applySql, Object... value) {
this.queryWrapper.apply(condition, applySql, value);
return this;
}
public Wrapper last(boolean condition, String lastSql) {
this.queryWrapper.last(condition, lastSql);
return this;
}
public Wrapper comment(boolean condition, String comment) {
this.queryWrapper.comment(condition, comment);
return this;
}
public Wrapper exists(boolean condition, String existsSql) {
this.queryWrapper.exists(condition, existsSql);
return this;
}
public Wrapper notExists(boolean condition, String notExistsSql) {
this.queryWrapper.notExists(condition, notExistsSql);
return this;
}
public Wrapper orderBy(boolean condition, boolean isAsc, String... columns) {
this.queryWrapper.orderBy(condition, isAsc, columns);
return this;
}
/**
* === 以下方式获取请求结果 ===
*/
public Map<String, Object> queryWrapperForMap() {
ICommonSqlService commonSqlService = CustomApplicationContext.getBean(ICommonSqlService.class);
return commonSqlService.queryWrapperForMap(this.prefixSql, this.queryWrapper);
}
public Page<Map<String, Object>> queryWrapperForPage(Page<?> page) {
ICommonSqlService commonSqlService = CustomApplicationContext.getBean(ICommonSqlService.class);
return commonSqlService.queryWrapperForPage(page, this.prefixSql, this.queryWrapper);
}
public List<Map<String, Object>> queryWrapperForList() {
ICommonSqlService commonSqlService = CustomApplicationContext.getBean(ICommonSqlService.class);
return commonSqlService.queryWrapperForList(this.prefixSql, this.queryWrapper);
}
public Map<String, Object> queryDBForMap(String DBName) throws Exception {
JdbcTemplate jdbcTemplate = (JdbcTemplate) CustomApplicationContext.getBean(DBName);
return jdbcTemplate.queryForMap(initSql());
}
public Page<Map<String, Object>> queryDBForPage(Page page, String DBName, String customPageSql) throws Exception {
JdbcTemplate jdbcTemplate = (JdbcTemplate) CustomApplicationContext.getBean(DBName);
TransactionTemplate transactionTemplate = CustomApplicationContext.getBean(TransactionTemplate.class);
String countSql = SELECT + SPACE + COUNT_ALL + SPACE + FROM + SPACE + LEFT_BRACKET + initSql() + RIGHT_BRACKET + SPACE + AS + SPACE + "tmp";
if (customPageSql == null || "".equals(customPageSql)) {
queryWrapper.last(LIMIT + SPACE + (page.getCurrent() - 1) * page.getSize() + COMMA + SPACE + page.getCurrent() * page.getSize());
} else {
queryWrapper.last(customPageSql);
}
String pageSql = initSql();
transactionTemplate.execute((transactionStatus -> {
page.setTotal(Long.parseLong("" + jdbcTemplate.queryForMap(countSql).get(COUNT_ALL)));
page.setRecords(jdbcTemplate.queryForList(pageSql));
return 1;
}));
return page;
}
public List<Map<String, Object>> queryDBForList(String DBName) throws Exception {
JdbcTemplate jdbcTemplate = (JdbcTemplate) CustomApplicationContext.getBean(DBName);
return jdbcTemplate.queryForList(initSql());
}
}