WhereWrapper.java
3.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
package com.skua.tool.query;
import com.google.common.base.CaseFormat;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.stream.Collectors;
/**
* @author sonin
* @date 2021/12/4 19:37
* QueryWrapper条件e.g: DemoB_bName = xxx
*/
public class WhereWrapper extends Wrapper {
WhereWrapper() {
this.classes = new LinkedHashSet<>();
}
@Override
public String initPrefixSql() {
String sql = "select ${var0} from (select ${var1} from ${var2} where ${var3}) as ${var4}";
String allClassName = this.classes.stream().map(Class::getSimpleName).collect(Collectors.joining(UNDERLINE));
sql = sql.replaceFirst("\\$\\{var0}", allClassName + DOT + ALL);
if (this.selectedColumns != null && !this.selectedColumns.isEmpty()) {
String selectedColumns = String.join(COMMA + SPACE, this.selectedColumns);
sql = sql.replaceFirst("\\$\\{var1}", selectedColumns);
} else {
sql = sql.replaceFirst("\\$\\{var1}", initColumns());
}
String tables = this.classes.stream().map(clazz -> CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, clazz.getSimpleName())).collect(Collectors.joining(COMMA + SPACE));
sql = sql.replaceFirst("\\$\\{var2}", tables);
if (this.conditions != null && !this.conditions.isEmpty()) {
sql = sql.replaceFirst("\\$\\{var3}", String.join(SPACE + AND + SPACE, this.conditions));
} else {
sql = sql.replaceFirst(SPACE + WHERE + SPACE + "\\$\\{var3}", EMPTY);
}
return sql.replaceFirst("\\$\\{var4}", allClassName);
}
@Override
public Wrapper from(Class... classes) {
this.classes.addAll(Arrays.asList(classes));
return this;
}
@Override
public Wrapper innerJoin(Class clazz, Field leftField, Field rightField) {
return this;
}
@Override
public Wrapper leftJoin(Class clazz, Field leftField, Field rightField) {
return this;
}
@Override
public Wrapper rightJoin(Class clazz, Field leftField, Field rightField) {
return this;
}
@Override
public Wrapper and(Field leftField, Field rightField) {
if (this.conditions == null) {
this.conditions = new LinkedHashSet<>();
}
this.classes.add(leftField.getDeclaringClass());
this.classes.add(rightField.getDeclaringClass());
// e.g: DemoA
String leftClassName = leftField.getDeclaringClass().getSimpleName();
// e.g: demo_a
String leftTableName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, leftClassName);
// e.g: id
String leftClassFieldName = leftField.getName();
// e.g: id
String leftTableFieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, leftClassFieldName);
// e.g: DemoB
String rightClassName = rightField.getDeclaringClass().getSimpleName();
// e.g: demo_b
String rightTableName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, rightClassName);
// e.g: aId
String rightClassFieldName = rightField.getName();
// e.g: a_id
String rightTableFieldName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, rightClassFieldName);
// e.g: demo_a.id = demo_b.a_id
this.conditions.add(leftTableName + DOT + leftTableFieldName + SPACE + EQUAL + SPACE + rightTableName + DOT + rightTableFieldName);
return this;
}
}