21
21
import java .util .List ;
22
22
import java .util .Map ;
23
23
import java .util .Objects ;
24
+ import java .util .Optional ;
25
+ import java .util .function .Supplier ;
24
26
25
27
import org .mybatis .dynamic .sql .BasicColumn ;
26
28
import org .mybatis .dynamic .sql .BindableColumn ;
@@ -49,13 +51,15 @@ public class QueryExpressionDSL<R> implements Buildable<R> {
49
51
private GroupByModel groupByModel ;
50
52
private JoinModel joinModel ;
51
53
private List <JoinSpecification > joinSpecifications = new ArrayList <>();
54
+ private Supplier <R > buildDelegateMethod ;
52
55
53
56
private QueryExpressionDSL (FromGatherer <R > fromGatherer ) {
54
57
connector = fromGatherer .connector ;
55
58
selectList = Arrays .asList (fromGatherer .selectList );
56
59
isDistinct = fromGatherer .isDistinct ;
57
60
selectDSL = Objects .requireNonNull (fromGatherer .selectDSL );
58
61
table = Objects .requireNonNull (fromGatherer .table );
62
+ buildDelegateMethod = this ::internalBuild ;
59
63
}
60
64
61
65
private QueryExpressionDSL (FromGatherer <R > fromGatherer , String tableAlias ) {
@@ -89,6 +93,10 @@ public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, Visitable
89
93
90
94
@ Override
91
95
public R build () {
96
+ return buildDelegateMethod .get ();
97
+ }
98
+
99
+ private R internalBuild () {
92
100
selectDSL .addQueryExpression (buildModel ());
93
101
return selectDSL .build ();
94
102
}
@@ -136,6 +144,7 @@ public GroupByFinisher groupBy(BasicColumn...columns) {
136
144
}
137
145
138
146
public SelectDSL <R > orderBy (SortSpecification ...columns ) {
147
+ buildDelegateMethod = selectDSL ::build ;
139
148
selectDSL .addQueryExpression (buildModel ());
140
149
selectDSL .setOrderByModel (OrderByModel .of (columns ));
141
150
return selectDSL ;
@@ -164,16 +173,19 @@ protected QueryExpressionModel buildModel() {
164
173
}
165
174
166
175
public SelectDSL <R >.LimitFinisher limit (long limit ) {
176
+ buildDelegateMethod = selectDSL ::build ;
167
177
selectDSL .addQueryExpression (buildModel ());
168
178
return selectDSL .limit (limit );
169
179
}
170
180
171
181
public SelectDSL <R >.OffsetFirstFinisher offset (long offset ) {
182
+ buildDelegateMethod = selectDSL ::build ;
172
183
selectDSL .addQueryExpression (buildModel ());
173
184
return selectDSL .offset (offset );
174
185
}
175
186
176
187
public SelectDSL <R >.FetchFirstFinisher fetchFirst (long fetchFirstRows ) {
188
+ buildDelegateMethod = selectDSL ::build ;
177
189
selectDSL .addQueryExpression (buildModel ());
178
190
return selectDSL .fetchFirst (fetchFirstRows );
179
191
}
@@ -184,29 +196,37 @@ public static class FromGatherer<R> {
184
196
private SelectDSL <R > selectDSL ;
185
197
private boolean isDistinct ;
186
198
private SqlTable table ;
199
+ private Optional <QueryExpressionDSL <R >> priorQuery ;
187
200
188
201
public FromGatherer (Builder <R > builder ) {
189
202
this .connector = builder .connector ;
190
203
this .selectList = Objects .requireNonNull (builder .selectList );
191
204
this .selectDSL = Objects .requireNonNull (builder .selectDSL );
192
205
this .isDistinct = builder .isDistinct ;
206
+ this .priorQuery = Optional .ofNullable (builder .priorQuery );
193
207
}
194
208
195
209
public QueryExpressionDSL <R > from (SqlTable table ) {
196
210
this .table = table ;
197
- return new QueryExpressionDSL <>(this );
211
+ return setPriorBuildDelegate ( new QueryExpressionDSL <>(this ) );
198
212
}
199
213
200
214
public QueryExpressionDSL <R > from (SqlTable table , String tableAlias ) {
201
215
this .table = table ;
202
- return new QueryExpressionDSL <>(this , tableAlias );
216
+ return setPriorBuildDelegate (new QueryExpressionDSL <>(this , tableAlias ));
217
+ }
218
+
219
+ private QueryExpressionDSL <R > setPriorBuildDelegate (QueryExpressionDSL <R > newQuery ) {
220
+ priorQuery .ifPresent (pq -> pq .buildDelegateMethod = newQuery ::build );
221
+ return newQuery ;
203
222
}
204
223
205
224
public static class Builder <R > {
206
225
private String connector ;
207
226
private BasicColumn [] selectList ;
208
227
private SelectDSL <R > selectDSL ;
209
228
private boolean isDistinct ;
229
+ private QueryExpressionDSL <R > priorQuery ;
210
230
211
231
public Builder <R > withConnector (String connector ) {
212
232
this .connector = connector ;
@@ -228,6 +248,11 @@ public Builder<R> isDistinct() {
228
248
return this ;
229
249
}
230
250
251
+ public Builder <R > withPriorQuery (QueryExpressionDSL <R > priorQuery ) {
252
+ this .priorQuery = priorQuery ;
253
+ return this ;
254
+ }
255
+
231
256
public FromGatherer <R > build () {
232
257
return new FromGatherer <>(this );
233
258
}
@@ -238,11 +263,13 @@ public class QueryExpressionWhereBuilder extends AbstractWhereDSL<QueryExpressio
238
263
implements Buildable <R > {
239
264
private <T > QueryExpressionWhereBuilder (BindableColumn <T > column , VisitableCondition <T > condition ) {
240
265
super (column , condition );
266
+ buildDelegateMethod = this ::internalBuild ;
241
267
}
242
268
243
269
private <T > QueryExpressionWhereBuilder (BindableColumn <T > column , VisitableCondition <T > condition ,
244
270
SqlCriterion <?>...subCriteria ) {
245
271
super (column , condition , subCriteria );
272
+ buildDelegateMethod = this ::internalBuild ;
246
273
}
247
274
248
275
public UnionBuilder union () {
@@ -258,6 +285,7 @@ public UnionBuilder unionAll() {
258
285
}
259
286
260
287
public SelectDSL <R > orderBy (SortSpecification ...columns ) {
288
+ buildDelegateMethod = selectDSL ::build ;
261
289
whereModel = buildWhereModel ();
262
290
selectDSL .addQueryExpression (buildModel ());
263
291
selectDSL .setOrderByModel (OrderByModel .of (columns ));
@@ -272,25 +300,32 @@ public GroupByFinisher groupBy(BasicColumn...columns) {
272
300
}
273
301
274
302
public SelectDSL <R >.LimitFinisher limit (long limit ) {
303
+ buildDelegateMethod = selectDSL ::build ;
275
304
whereModel = buildWhereModel ();
276
305
selectDSL .addQueryExpression (buildModel ());
277
306
return selectDSL .limit (limit );
278
307
}
279
308
280
309
public SelectDSL <R >.OffsetFirstFinisher offset (long offset ) {
310
+ buildDelegateMethod = selectDSL ::build ;
281
311
whereModel = buildWhereModel ();
282
312
selectDSL .addQueryExpression (buildModel ());
283
313
return selectDSL .offset (offset );
284
314
}
285
315
286
316
public SelectDSL <R >.FetchFirstFinisher fetchFirst (long fetchFirstRows ) {
317
+ buildDelegateMethod = selectDSL ::build ;
287
318
whereModel = buildWhereModel ();
288
319
selectDSL .addQueryExpression (buildModel ());
289
320
return selectDSL .fetchFirst (fetchFirstRows );
290
321
}
291
322
292
323
@ Override
293
324
public R build () {
325
+ return buildDelegateMethod .get ();
326
+ }
327
+
328
+ private R internalBuild () {
294
329
whereModel = buildWhereModel ();
295
330
selectDSL .addQueryExpression (buildModel ());
296
331
return selectDSL .build ();
@@ -322,7 +357,6 @@ public JoinSpecificationFinisher on(BasicColumn joinColumn, JoinCondition joinCo
322
357
}
323
358
324
359
public class JoinSpecificationFinisher implements Buildable <R > {
325
-
326
360
private SqlTable joinTable ;
327
361
private List <JoinCriterion > joinCriteria = new ArrayList <>();
328
362
private JoinType joinType ;
@@ -338,6 +372,7 @@ public JoinSpecificationFinisher(SqlTable table, BasicColumn joinColumn,
338
372
.build ();
339
373
340
374
joinCriteria .add (joinCriterion );
375
+ buildDelegateMethod = this ::internalbuild ;
341
376
}
342
377
343
378
public JoinSpecificationFinisher (SqlTable table , BasicColumn joinColumn ,
@@ -352,6 +387,7 @@ public JoinSpecificationFinisher(SqlTable table, BasicColumn joinColumn,
352
387
353
388
this .joinCriteria .add (joinCriterion );
354
389
this .joinCriteria .addAll (Arrays .asList (joinCriteria ));
390
+ buildDelegateMethod = this ::internalbuild ;
355
391
}
356
392
357
393
protected JoinSpecification buildJoinSpecification () {
@@ -368,6 +404,10 @@ protected JoinModel buildJoinModel() {
368
404
369
405
@ Override
370
406
public R build () {
407
+ return buildDelegateMethod .get ();
408
+ }
409
+
410
+ private R internalbuild () {
371
411
joinModel = buildJoinModel ();
372
412
selectDSL .addQueryExpression (buildModel ());
373
413
return selectDSL .build ();
@@ -434,51 +474,67 @@ public JoinSpecificationStarter fullJoin(SqlTable joinTable, String tableAlias)
434
474
}
435
475
436
476
public SelectDSL <R > orderBy (SortSpecification ...columns ) {
477
+ buildDelegateMethod = selectDSL ::build ;
437
478
joinModel = buildJoinModel ();
438
479
selectDSL .addQueryExpression (buildModel ());
439
480
selectDSL .setOrderByModel (OrderByModel .of (columns ));
440
481
return selectDSL ;
441
482
}
442
483
443
484
public SelectDSL <R >.LimitFinisher limit (long limit ) {
485
+ buildDelegateMethod = selectDSL ::build ;
444
486
joinModel = buildJoinModel ();
445
487
selectDSL .addQueryExpression (buildModel ());
446
488
return selectDSL .limit (limit );
447
489
}
448
490
449
491
public SelectDSL <R >.OffsetFirstFinisher offset (long offset ) {
492
+ buildDelegateMethod = selectDSL ::build ;
450
493
joinModel = buildJoinModel ();
451
494
selectDSL .addQueryExpression (buildModel ());
452
495
return selectDSL .offset (offset );
453
496
}
454
497
455
498
public SelectDSL <R >.FetchFirstFinisher fetchFirst (long fetchFirstRows ) {
499
+ buildDelegateMethod = selectDSL ::build ;
456
500
joinModel = buildJoinModel ();
457
501
selectDSL .addQueryExpression (buildModel ());
458
502
return selectDSL .fetchFirst (fetchFirstRows );
459
503
}
460
504
}
461
505
462
506
public class GroupByFinisher implements Buildable <R > {
507
+ public GroupByFinisher () {
508
+ buildDelegateMethod = this ::internalBuild ;
509
+ }
510
+
463
511
public SelectDSL <R > orderBy (SortSpecification ...columns ) {
512
+ buildDelegateMethod = selectDSL ::build ;
464
513
selectDSL .setOrderByModel (OrderByModel .of (columns ));
465
514
return selectDSL ;
466
515
}
467
516
468
517
@ Override
469
518
public R build () {
519
+ return buildDelegateMethod .get ();
520
+ }
521
+
522
+ private R internalBuild () {
470
523
return selectDSL .build ();
471
524
}
472
525
473
526
public SelectDSL <R >.LimitFinisher limit (long limit ) {
527
+ buildDelegateMethod = selectDSL ::build ;
474
528
return selectDSL .limit (limit );
475
529
}
476
530
477
531
public SelectDSL <R >.OffsetFirstFinisher offset (long offset ) {
532
+ buildDelegateMethod = selectDSL ::build ;
478
533
return selectDSL .offset (offset );
479
534
}
480
535
481
536
public SelectDSL <R >.FetchFirstFinisher fetchFirst (long fetchFirstRows ) {
537
+ buildDelegateMethod = selectDSL ::build ;
482
538
return selectDSL .fetchFirst (fetchFirstRows );
483
539
}
484
540
}
@@ -495,6 +551,7 @@ public FromGatherer<R> select(BasicColumn...selectList) {
495
551
.withConnector (connector )
496
552
.withSelectList (selectList )
497
553
.withSelectDSL (selectDSL )
554
+ .withPriorQuery (QueryExpressionDSL .this )
498
555
.build ();
499
556
}
500
557
@@ -504,6 +561,7 @@ public FromGatherer<R> selectDistinct(BasicColumn...selectList) {
504
561
.withSelectList (selectList )
505
562
.withSelectDSL (selectDSL )
506
563
.isDistinct ()
564
+ .withPriorQuery (QueryExpressionDSL .this )
507
565
.build ();
508
566
}
509
567
}
0 commit comments