JoinWrapper.java
3.2 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
package com.skua.tool.query;
import com.google.common.base.CaseFormat;
import java.lang.reflect.Field;
import java.util.LinkedHashSet;
/**
* @author sonin
* @date 2021/12/4 19:31
* QueryWrapper条件e.g: demo_b.b_name = xxx
*/
public class JoinWrapper extends Wrapper {
private Class from;
JoinWrapper() {
this.classes = new LinkedHashSet<>();
}
@Override
public String initPrefixSql() {
StringBuilder stringBuilder = new StringBuilder(SELECT + SPACE);
if (this.selectedColumns != null && !this.selectedColumns.isEmpty()) {
String selectedColumns = String.join(COMMA + SPACE, this.selectedColumns);
stringBuilder.append(selectedColumns);
} else {
stringBuilder.append(initColumns());
}
stringBuilder.append(SPACE).append(FROM).append(SPACE).append(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, this.from.getSimpleName()));
if (this.conditions != null && !this.conditions.isEmpty()) {
stringBuilder.append(SPACE).append(String.join(SPACE, this.conditions));
}
return stringBuilder.toString();
}
@Override
public Wrapper from(Class... classes) {
this.from = classes[0];
this.classes.add(classes[0]);
return this;
}
@Override
public Wrapper innerJoin(Class clazz, Field leftField, Field rightField) {
directionJoin(clazz, leftField, rightField, INNER_JOIN);
return this;
}
@Override
public Wrapper leftJoin(Class clazz, Field leftField, Field rightField) {
directionJoin(clazz, leftField, rightField, LEFT_JOIN);
return this;
}
@Override
public Wrapper rightJoin(Class clazz, Field leftField, Field rightField) {
directionJoin(clazz, leftField, rightField, RIGHT_JOIN);
return this;
}
private void directionJoin(Class clazz, Field leftField, Field rightField, String direction) {
if (this.conditions == null) {
this.conditions = new LinkedHashSet<>();
}
this.classes.add(leftField.getDeclaringClass());
this.classes.add(rightField.getDeclaringClass());
// e.g: demo_b
String fromTableName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, clazz.getSimpleName());
// e.g: demo_b
String leftTableName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, leftField.getDeclaringClass().getSimpleName());
// e.g: a_id
String leftColumn = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, leftField.getName());
// e.g: demo_a
String rightTableName = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, rightField.getDeclaringClass().getSimpleName());
// e.g: id
String rightColumn = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, rightField.getName());
// e.g: inner/left/right join demo_b on demo_b.a_id = demo_a.id
this.conditions.add(direction + SPACE + fromTableName + SPACE + ON + SPACE + leftTableName + DOT + leftColumn + SPACE + EQUAL + SPACE + rightTableName + DOT + rightColumn);
}
@Override
public Wrapper and(Field leftField, Field rightField) {
return this;
}
}