|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+package ltass.staudy.spring.data.jpa.sql;
|
|
|
2
|
+
|
|
|
3
|
+import java.io.Serializable;
|
|
|
4
|
+import java.util.ArrayList;
|
|
|
5
|
+import java.util.List;
|
|
|
6
|
+
|
|
|
7
|
+/**
|
|
|
8
|
+ * 参考了别人的代码
|
|
|
9
|
+ * url https://github.com/jkrasnay/sqlbuilder
|
|
|
10
|
+ */
|
|
|
11
|
+public class SelectBuilder extends AbstractSqlBuilder implements Cloneable, Serializable {
|
|
|
12
|
+
|
|
|
13
|
+ private static final long serialVersionUID = 1;
|
|
|
14
|
+
|
|
|
15
|
+ private boolean distinct;
|
|
|
16
|
+
|
|
|
17
|
+ private List<Object> columns = new ArrayList<Object>();
|
|
|
18
|
+
|
|
|
19
|
+ private List<String> tables = new ArrayList<String>();
|
|
|
20
|
+
|
|
|
21
|
+ private List<String> joins = new ArrayList<String>();
|
|
|
22
|
+
|
|
|
23
|
+ private List<String> leftJoins = new ArrayList<String>();
|
|
|
24
|
+
|
|
|
25
|
+ private List<String> wheres = new ArrayList<String>();
|
|
|
26
|
+
|
|
|
27
|
+ private List<String> groupBys = new ArrayList<String>();
|
|
|
28
|
+
|
|
|
29
|
+ private List<String> havings = new ArrayList<String>();
|
|
|
30
|
+
|
|
|
31
|
+ private List<SelectBuilder> unions = new ArrayList<SelectBuilder>();
|
|
|
32
|
+
|
|
|
33
|
+ private List<String> orderBys = new ArrayList<String>();
|
|
|
34
|
+
|
|
|
35
|
+ private int limit = 0;
|
|
|
36
|
+
|
|
|
37
|
+ private int offset = 0;
|
|
|
38
|
+
|
|
|
39
|
+ private boolean forUpdate;
|
|
|
40
|
+
|
|
|
41
|
+ private boolean noWait;
|
|
|
42
|
+
|
|
|
43
|
+ public SelectBuilder() {
|
|
|
44
|
+
|
|
|
45
|
+ }
|
|
|
46
|
+
|
|
|
47
|
+ public SelectBuilder(String table) {
|
|
|
48
|
+ tables.add(table);
|
|
|
49
|
+ }
|
|
|
50
|
+
|
|
|
51
|
+ /**
|
|
|
52
|
+ * Copy constructor. Used by {@link #clone()}.
|
|
|
53
|
+ *
|
|
|
54
|
+ * @param other
|
|
|
55
|
+ * SelectBuilder being cloned.
|
|
|
56
|
+ */
|
|
|
57
|
+ protected SelectBuilder(SelectBuilder other) {
|
|
|
58
|
+
|
|
|
59
|
+ this.distinct = other.distinct;
|
|
|
60
|
+ this.forUpdate = other.forUpdate;
|
|
|
61
|
+ this.noWait = other.noWait;
|
|
|
62
|
+
|
|
|
63
|
+ for (Object column : other.columns) {
|
|
|
64
|
+ if (column instanceof SubSelectBuilder) {
|
|
|
65
|
+ this.columns.add(((SubSelectBuilder) column).clone());
|
|
|
66
|
+ } else {
|
|
|
67
|
+ this.columns.add(column);
|
|
|
68
|
+ }
|
|
|
69
|
+ }
|
|
|
70
|
+
|
|
|
71
|
+ this.tables.addAll(other.tables);
|
|
|
72
|
+ this.joins.addAll(other.joins);
|
|
|
73
|
+ this.leftJoins.addAll(other.leftJoins);
|
|
|
74
|
+ this.wheres.addAll(other.wheres);
|
|
|
75
|
+ this.groupBys.addAll(other.groupBys);
|
|
|
76
|
+ this.havings.addAll(other.havings);
|
|
|
77
|
+
|
|
|
78
|
+ for (SelectBuilder sb : other.unions) {
|
|
|
79
|
+ this.unions.add(sb.clone());
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ this.orderBys.addAll(other.orderBys);
|
|
|
83
|
+ }
|
|
|
84
|
+
|
|
|
85
|
+ /**
|
|
|
86
|
+ * Alias for {@link #where(String)}.
|
|
|
87
|
+ */
|
|
|
88
|
+ public SelectBuilder and(String expr) {
|
|
|
89
|
+ return where(expr);
|
|
|
90
|
+ }
|
|
|
91
|
+
|
|
|
92
|
+ public SelectBuilder column(String name) {
|
|
|
93
|
+ columns.add(name);
|
|
|
94
|
+ return this;
|
|
|
95
|
+ }
|
|
|
96
|
+
|
|
|
97
|
+ public SelectBuilder column(SubSelectBuilder subSelect) {
|
|
|
98
|
+ columns.add(subSelect);
|
|
|
99
|
+ return this;
|
|
|
100
|
+ }
|
|
|
101
|
+
|
|
|
102
|
+ public SelectBuilder column(String name, boolean groupBy) {
|
|
|
103
|
+ columns.add(name);
|
|
|
104
|
+ if (groupBy) {
|
|
|
105
|
+ groupBys.add(name);
|
|
|
106
|
+ }
|
|
|
107
|
+ return this;
|
|
|
108
|
+ }
|
|
|
109
|
+
|
|
|
110
|
+ public SelectBuilder limit(int limit, int offset) {
|
|
|
111
|
+ this.limit = limit;
|
|
|
112
|
+ this.offset = offset;
|
|
|
113
|
+ return this;
|
|
|
114
|
+ }
|
|
|
115
|
+
|
|
|
116
|
+ public SelectBuilder limit(int limit) {
|
|
|
117
|
+ return limit(limit, 0);
|
|
|
118
|
+ }
|
|
|
119
|
+
|
|
|
120
|
+ @Override
|
|
|
121
|
+ public SelectBuilder clone() {
|
|
|
122
|
+ return new SelectBuilder(this);
|
|
|
123
|
+ }
|
|
|
124
|
+
|
|
|
125
|
+ public SelectBuilder distinct() {
|
|
|
126
|
+ this.distinct = true;
|
|
|
127
|
+ return this;
|
|
|
128
|
+ }
|
|
|
129
|
+
|
|
|
130
|
+ public SelectBuilder forUpdate() {
|
|
|
131
|
+ forUpdate = true;
|
|
|
132
|
+ return this;
|
|
|
133
|
+ }
|
|
|
134
|
+
|
|
|
135
|
+ public SelectBuilder from(String table) {
|
|
|
136
|
+ tables.add(table);
|
|
|
137
|
+ return this;
|
|
|
138
|
+ }
|
|
|
139
|
+
|
|
|
140
|
+ public List<SelectBuilder> getUnions() {
|
|
|
141
|
+ return unions;
|
|
|
142
|
+ }
|
|
|
143
|
+
|
|
|
144
|
+ public SelectBuilder groupBy(String expr) {
|
|
|
145
|
+ groupBys.add(expr);
|
|
|
146
|
+ return this;
|
|
|
147
|
+ }
|
|
|
148
|
+
|
|
|
149
|
+ public SelectBuilder having(String expr) {
|
|
|
150
|
+ havings.add(expr);
|
|
|
151
|
+ return this;
|
|
|
152
|
+ }
|
|
|
153
|
+
|
|
|
154
|
+ public SelectBuilder join(String join) {
|
|
|
155
|
+ joins.add(join);
|
|
|
156
|
+ return this;
|
|
|
157
|
+ }
|
|
|
158
|
+
|
|
|
159
|
+ public SelectBuilder leftJoin(String join) {
|
|
|
160
|
+ leftJoins.add(join);
|
|
|
161
|
+ return this;
|
|
|
162
|
+ }
|
|
|
163
|
+
|
|
|
164
|
+ public SelectBuilder noWait() {
|
|
|
165
|
+ if (!forUpdate) {
|
|
|
166
|
+ throw new RuntimeException("noWait without forUpdate cannot be called");
|
|
|
167
|
+ }
|
|
|
168
|
+ noWait = true;
|
|
|
169
|
+ return this;
|
|
|
170
|
+ }
|
|
|
171
|
+
|
|
|
172
|
+ public SelectBuilder orderBy(String name) {
|
|
|
173
|
+ orderBys.add(name);
|
|
|
174
|
+ return this;
|
|
|
175
|
+ }
|
|
|
176
|
+
|
|
|
177
|
+ /**
|
|
|
178
|
+ * Adds an ORDER BY item with a direction indicator.
|
|
|
179
|
+ *
|
|
|
180
|
+ * @param name
|
|
|
181
|
+ * Name of the column by which to sort.
|
|
|
182
|
+ * @param ascending
|
|
|
183
|
+ * If true, specifies the direction "asc", otherwise, specifies
|
|
|
184
|
+ * the direction "desc".
|
|
|
185
|
+ */
|
|
|
186
|
+ public SelectBuilder orderBy(String name, boolean ascending) {
|
|
|
187
|
+ if (ascending) {
|
|
|
188
|
+ orderBys.add(name + " asc");
|
|
|
189
|
+ } else {
|
|
|
190
|
+ orderBys.add(name + " desc");
|
|
|
191
|
+ }
|
|
|
192
|
+ return this;
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
|
195
|
+ @Override
|
|
|
196
|
+ public String toString() {
|
|
|
197
|
+
|
|
|
198
|
+ StringBuilder sql = new StringBuilder("select ");
|
|
|
199
|
+
|
|
|
200
|
+ if (distinct) {
|
|
|
201
|
+ sql.append("distinct ");
|
|
|
202
|
+ }
|
|
|
203
|
+
|
|
|
204
|
+ if (columns.size() == 0) {
|
|
|
205
|
+ sql.append("*");
|
|
|
206
|
+ } else {
|
|
|
207
|
+ appendList(sql, columns, "", ", ");
|
|
|
208
|
+ }
|
|
|
209
|
+
|
|
|
210
|
+ appendList(sql, tables, " from ", ", ");
|
|
|
211
|
+ appendList(sql, joins, " join ", " join ");
|
|
|
212
|
+ appendList(sql, leftJoins, " left join ", " left join ");
|
|
|
213
|
+ appendList(sql, wheres, " where ", " and ");
|
|
|
214
|
+ appendList(sql, groupBys, " group by ", ", ");
|
|
|
215
|
+ appendList(sql, havings, " having ", " and ");
|
|
|
216
|
+ appendList(sql, unions, " union ", " union ");
|
|
|
217
|
+ appendList(sql, orderBys, " order by ", ", ");
|
|
|
218
|
+
|
|
|
219
|
+ if (forUpdate) {
|
|
|
220
|
+ sql.append(" for update");
|
|
|
221
|
+ if (noWait) {
|
|
|
222
|
+ sql.append(" nowait");
|
|
|
223
|
+ }
|
|
|
224
|
+ }
|
|
|
225
|
+
|
|
|
226
|
+ if(limit > 0)
|
|
|
227
|
+ sql.append(" limit " + limit);
|
|
|
228
|
+ if(offset > 0)
|
|
|
229
|
+ sql.append(", " + offset);
|
|
|
230
|
+
|
|
|
231
|
+ return sql.toString();
|
|
|
232
|
+ }
|
|
|
233
|
+
|
|
|
234
|
+ /**
|
|
|
235
|
+ * Adds a "union" select builder. The generated SQL will union this query
|
|
|
236
|
+ * with the result of the main query. The provided builder must have the
|
|
|
237
|
+ * same columns as the parent select builder and must not use "order by" or
|
|
|
238
|
+ * "for update".
|
|
|
239
|
+ */
|
|
|
240
|
+ public SelectBuilder union(SelectBuilder unionBuilder) {
|
|
|
241
|
+ unions.add(unionBuilder);
|
|
|
242
|
+ return this;
|
|
|
243
|
+ }
|
|
|
244
|
+
|
|
|
245
|
+ public SelectBuilder where(String expr) {
|
|
|
246
|
+ wheres.add(expr);
|
|
|
247
|
+ return this;
|
|
|
248
|
+ }
|
|
|
249
|
+}
|