37
37
import org .mybatis .dynamic .sql .where .AbstractWhereDSL ;
38
38
import org .mybatis .dynamic .sql .where .WhereModel ;
39
39
40
- public class QueryExpressionDSL <R > {
40
+ public class QueryExpressionDSL <R > implements Buildable < R > {
41
41
42
42
private String connector ;
43
- private SelectDSL <R > selectModelBuilder ;
43
+ private SelectDSL <R > selectDSL ;
44
44
private boolean isDistinct ;
45
45
private List <BasicColumn > selectList ;
46
46
private SqlTable table ;
@@ -50,55 +50,83 @@ public class QueryExpressionDSL<R> {
50
50
private JoinModel joinModel ;
51
51
private List <JoinSpecification > joinSpecifications = new ArrayList <>();
52
52
53
- private QueryExpressionDSL (Builder <R > builder ) {
54
- connector = builder .connector ;
55
- selectList = Arrays .asList (builder .selectList );
56
- isDistinct = builder .isDistinct ;
57
- selectModelBuilder = Objects .requireNonNull (builder .selectModelBuilder );
53
+ private QueryExpressionDSL (FromGatherer <R > fromGatherer ) {
54
+ connector = fromGatherer .builder .connector ;
55
+ selectList = Arrays .asList (fromGatherer .builder .selectList );
56
+ isDistinct = fromGatherer .builder .isDistinct ;
57
+ selectDSL = Objects .requireNonNull (fromGatherer .builder .selectDSL );
58
+ table = Objects .requireNonNull (fromGatherer .table );
59
+ tableAliases .putAll (fromGatherer .tableAliasMap );
58
60
}
59
61
60
- public QueryExpressionAfterFrom from (SqlTable table ) {
61
- this .table = table ;
62
- return new QueryExpressionAfterFrom ();
62
+ public <T > QueryExpressionWhereBuilder where (BindableColumn <T > column , VisitableCondition <T > condition ) {
63
+ return new QueryExpressionWhereBuilder (column , condition );
63
64
}
64
65
65
- public QueryExpressionAfterFrom from (SqlTable table , String tableAlias ) {
66
- this .table = table ;
67
- tableAliases .put (table , tableAlias );
68
- return new QueryExpressionAfterFrom ();
66
+ public <T > QueryExpressionWhereBuilder where (BindableColumn <T > column , VisitableCondition <T > condition ,
67
+ SqlCriterion <?>...subCriteria ) {
68
+ return new QueryExpressionWhereBuilder (column , condition , subCriteria );
69
+ }
70
+
71
+ @ Override
72
+ public R build () {
73
+ selectDSL .addQueryExpression (buildModel ());
74
+ return selectDSL .build ();
69
75
}
70
76
71
- public static class Builder <R > {
72
- private String connector ;
73
- private BasicColumn [] selectList ;
74
- private boolean isDistinct ;
75
- private SelectDSL <R > selectModelBuilder ;
77
+ public JoinSpecificationStarter join (SqlTable joinTable ) {
78
+ return new JoinSpecificationStarter (joinTable , JoinType .INNER );
79
+ }
80
+
81
+ public JoinSpecificationStarter join (SqlTable joinTable , String tableAlias ) {
82
+ tableAliases .put (joinTable , tableAlias );
83
+ return join (joinTable );
84
+ }
76
85
77
- public Builder <R > withConnector (String connector ) {
78
- this .connector = connector ;
79
- return this ;
80
- }
81
-
82
- public Builder <R > withSelectList (BasicColumn ...selectList ) {
83
- this .selectList = selectList ;
84
- return this ;
85
- }
86
-
87
- public Builder <R > isDistinct () {
88
- this .isDistinct = true ;
89
- return this ;
90
- }
91
-
92
- public Builder <R > withSelectModelBuilder (SelectDSL <R > selectModelBuilder ) {
93
- this .selectModelBuilder = selectModelBuilder ;
94
- return this ;
95
- }
96
-
97
- public QueryExpressionDSL <R > build () {
98
- return new QueryExpressionDSL <>(this );
99
- }
86
+ public JoinSpecificationStarter leftJoin (SqlTable joinTable ) {
87
+ return new JoinSpecificationStarter (joinTable , JoinType .LEFT );
88
+ }
89
+
90
+ public JoinSpecificationStarter leftJoin (SqlTable joinTable , String tableAlias ) {
91
+ tableAliases .put (joinTable , tableAlias );
92
+ return leftJoin (joinTable );
93
+ }
94
+
95
+ public JoinSpecificationStarter rightJoin (SqlTable joinTable ) {
96
+ return new JoinSpecificationStarter (joinTable , JoinType .RIGHT );
97
+ }
98
+
99
+ public JoinSpecificationStarter rightJoin (SqlTable joinTable , String tableAlias ) {
100
+ tableAliases .put (joinTable , tableAlias );
101
+ return rightJoin (joinTable );
102
+ }
103
+
104
+ public JoinSpecificationStarter fullJoin (SqlTable joinTable ) {
105
+ return new JoinSpecificationStarter (joinTable , JoinType .FULL );
100
106
}
101
107
108
+ public JoinSpecificationStarter fullJoin (SqlTable joinTable , String tableAlias ) {
109
+ tableAliases .put (joinTable , tableAlias );
110
+ return fullJoin (joinTable );
111
+ }
112
+
113
+ public GroupByFinisher groupBy (BasicColumn ...columns ) {
114
+ groupByModel = GroupByModel .of (columns );
115
+ selectDSL .addQueryExpression (buildModel ());
116
+ return new GroupByFinisher ();
117
+ }
118
+
119
+ public SelectDSL <R > orderBy (SortSpecification ...columns ) {
120
+ selectDSL .addQueryExpression (buildModel ());
121
+ selectDSL .setOrderByModel (OrderByModel .of (columns ));
122
+ return selectDSL ;
123
+ }
124
+
125
+ public UnionBuilder union () {
126
+ selectDSL .addQueryExpression (buildModel ());
127
+ return new UnionBuilder ();
128
+ }
129
+
102
130
protected QueryExpressionModel buildModel () {
103
131
return new QueryExpressionModel .Builder ()
104
132
.withConnector (connector )
@@ -112,77 +140,56 @@ protected QueryExpressionModel buildModel() {
112
140
.build ();
113
141
}
114
142
115
- public class QueryExpressionAfterFrom implements Buildable <R > {
116
- private QueryExpressionAfterFrom () {
117
- super ();
118
- }
143
+ public static class FromGatherer <R > {
144
+ private FromGathererBuilder < R > builder ;
145
+ private Map < SqlTable , String > tableAliasMap = new HashMap <> ();
146
+ private SqlTable table ;
119
147
120
- public <T > QueryExpressionWhereBuilder where (BindableColumn <T > column , VisitableCondition <T > condition ) {
121
- return new QueryExpressionWhereBuilder (column , condition );
122
- }
123
-
124
- public <T > QueryExpressionWhereBuilder where (BindableColumn <T > column , VisitableCondition <T > condition ,
125
- SqlCriterion <?>...subCriteria ) {
126
- return new QueryExpressionWhereBuilder (column , condition , subCriteria );
148
+ public FromGatherer (FromGathererBuilder <R > builder ) {
149
+ this .builder = builder ;
127
150
}
128
151
129
- @ Override
130
- public R build () {
131
- selectModelBuilder .addQueryExpression (buildModel ());
132
- return selectModelBuilder .build ();
133
- }
134
-
135
- public JoinSpecificationStarter join (SqlTable joinTable ) {
136
- return new JoinSpecificationStarter (joinTable , JoinType .INNER );
137
- }
138
-
139
- public JoinSpecificationStarter join (SqlTable joinTable , String tableAlias ) {
140
- tableAliases .put (joinTable , tableAlias );
141
- return join (joinTable );
152
+ public QueryExpressionDSL <R > from (SqlTable table ) {
153
+ this .table = table ;
154
+
155
+ return new QueryExpressionDSL <>(this );
142
156
}
143
157
144
- public JoinSpecificationStarter leftJoin (SqlTable joinTable ) {
145
- return new JoinSpecificationStarter (joinTable , JoinType .LEFT );
158
+ public QueryExpressionDSL <R > from (SqlTable table , String tableAlias ) {
159
+ this .table = table ;
160
+ tableAliasMap .put (table , tableAlias );
161
+ return new QueryExpressionDSL <>(this );
146
162
}
163
+ }
164
+
165
+ public static class FromGathererBuilder <R > {
166
+ private String connector ;
167
+ private BasicColumn [] selectList ;
168
+ private SelectDSL <R > selectDSL ;
169
+ private boolean isDistinct ;
147
170
148
- public JoinSpecificationStarter leftJoin ( SqlTable joinTable , String tableAlias ) {
149
- tableAliases . put ( joinTable , tableAlias ) ;
150
- return leftJoin ( joinTable ) ;
171
+ public FromGathererBuilder < R > withConnector ( String connector ) {
172
+ this . connector = connector ;
173
+ return this ;
151
174
}
152
175
153
- public JoinSpecificationStarter rightJoin (SqlTable joinTable ) {
154
- return new JoinSpecificationStarter (joinTable , JoinType .RIGHT );
155
- }
156
-
157
- public JoinSpecificationStarter rightJoin (SqlTable joinTable , String tableAlias ) {
158
- tableAliases .put (joinTable , tableAlias );
159
- return rightJoin (joinTable );
176
+ public FromGathererBuilder <R > withSelectList (BasicColumn [] selectList ) {
177
+ this .selectList = selectList ;
178
+ return this ;
160
179
}
161
180
162
- public JoinSpecificationStarter fullJoin (SqlTable joinTable ) {
163
- return new JoinSpecificationStarter (joinTable , JoinType .FULL );
181
+ public FromGathererBuilder <R > withSelectDSL (SelectDSL <R > selectDSL ) {
182
+ this .selectDSL = selectDSL ;
183
+ return this ;
164
184
}
165
185
166
- public JoinSpecificationStarter fullJoin (SqlTable joinTable , String tableAlias ) {
167
- tableAliases .put (joinTable , tableAlias );
168
- return fullJoin (joinTable );
169
- }
170
-
171
- public GroupByFinisher groupBy (BasicColumn ...columns ) {
172
- groupByModel = GroupByModel .of (columns );
173
- selectModelBuilder .addQueryExpression (buildModel ());
174
- return new GroupByFinisher ();
186
+ public FromGathererBuilder <R > isDistinct () {
187
+ this .isDistinct = true ;
188
+ return this ;
175
189
}
176
190
177
- public SelectDSL <R > orderBy (SortSpecification ...columns ) {
178
- selectModelBuilder .addQueryExpression (buildModel ());
179
- selectModelBuilder .setOrderByModel (OrderByModel .of (columns ));
180
- return selectModelBuilder ;
181
- }
182
-
183
- public UnionBuilder union () {
184
- selectModelBuilder .addQueryExpression (buildModel ());
185
- return new UnionBuilder ();
191
+ public FromGatherer <R > build () {
192
+ return new FromGatherer <>(this );
186
193
}
187
194
}
188
195
@@ -199,22 +206,22 @@ private <T> QueryExpressionWhereBuilder(BindableColumn<T> column, VisitableCondi
199
206
200
207
public UnionBuilder union () {
201
208
whereModel = buildWhereModel ();
202
- selectModelBuilder .addQueryExpression (buildModel ());
209
+ selectDSL .addQueryExpression (buildModel ());
203
210
return new UnionBuilder ();
204
211
}
205
212
206
213
public SelectDSL <R > orderBy (SortSpecification ...columns ) {
207
214
whereModel = buildWhereModel ();
208
- selectModelBuilder .addQueryExpression (buildModel ());
209
- selectModelBuilder .setOrderByModel (OrderByModel .of (columns ));
210
- return selectModelBuilder ;
215
+ selectDSL .addQueryExpression (buildModel ());
216
+ selectDSL .setOrderByModel (OrderByModel .of (columns ));
217
+ return selectDSL ;
211
218
}
212
219
213
220
@ Override
214
221
public R build () {
215
222
whereModel = buildWhereModel ();
216
- selectModelBuilder .addQueryExpression (buildModel ());
217
- return selectModelBuilder .build ();
223
+ selectDSL .addQueryExpression (buildModel ());
224
+ return selectDSL .build ();
218
225
}
219
226
220
227
@ Override
@@ -293,8 +300,8 @@ protected JoinModel buildJoinModel() {
293
300
@ Override
294
301
public R build () {
295
302
joinModel = buildJoinModel ();
296
- selectModelBuilder .addQueryExpression (buildModel ());
297
- return selectModelBuilder .build ();
303
+ selectDSL .addQueryExpression (buildModel ());
304
+ return selectDSL .build ();
298
305
}
299
306
300
307
public <T > QueryExpressionWhereBuilder where (BindableColumn <T > column , VisitableCondition <T > condition ) {
@@ -360,39 +367,39 @@ public JoinSpecificationStarter fullJoin(SqlTable joinTable, String tableAlias)
360
367
361
368
public SelectDSL <R > orderBy (SortSpecification ...columns ) {
362
369
joinModel = buildJoinModel ();
363
- selectModelBuilder .addQueryExpression (buildModel ());
364
- selectModelBuilder .setOrderByModel (OrderByModel .of (columns ));
365
- return selectModelBuilder ;
370
+ selectDSL .addQueryExpression (buildModel ());
371
+ selectDSL .setOrderByModel (OrderByModel .of (columns ));
372
+ return selectDSL ;
366
373
}
367
374
}
368
375
369
376
public class GroupByFinisher implements Buildable <R > {
370
377
public SelectDSL <R > orderBy (SortSpecification ...columns ) {
371
- selectModelBuilder .setOrderByModel (OrderByModel .of (columns ));
372
- return selectModelBuilder ;
378
+ selectDSL .setOrderByModel (OrderByModel .of (columns ));
379
+ return selectDSL ;
373
380
}
374
381
375
382
@ Override
376
383
public R build () {
377
- return selectModelBuilder .build ();
384
+ return selectDSL .build ();
378
385
}
379
386
}
380
387
381
388
public class UnionBuilder {
382
- public QueryExpressionDSL <R > select (BasicColumn ...selectList ) {
383
- return new QueryExpressionDSL . Builder <R >()
384
- .withConnector ("union" ) //$NON-NLS-1$
389
+ public FromGatherer <R > select (BasicColumn ...selectList ) {
390
+ return new FromGathererBuilder <R >()
391
+ .withConnector ("union" )
385
392
.withSelectList (selectList )
386
- .withSelectModelBuilder ( selectModelBuilder )
393
+ .withSelectDSL ( selectDSL )
387
394
.build ();
388
395
}
389
-
390
- public QueryExpressionDSL <R > selectDistinct (BasicColumn ...selectList ) {
391
- return new QueryExpressionDSL .Builder <R >()
392
- .withConnector ("union" ) //$NON-NLS-1$
393
- .withSelectList (selectList )
396
+
397
+ public FromGatherer <R > selectDistinct (BasicColumn ...selectList ) {
398
+ return new FromGathererBuilder <R >()
399
+ .withConnector ("union" )
394
400
.isDistinct ()
395
- .withSelectModelBuilder (selectModelBuilder )
401
+ .withSelectList (selectList )
402
+ .withSelectDSL (selectDSL )
396
403
.build ();
397
404
}
398
405
}
0 commit comments