21
21
import java .util .List ;
22
22
import java .util .Map ;
23
23
import java .util .Objects ;
24
+ import java .util .function .Supplier ;
24
25
25
26
import org .mybatis .dynamic .sql .BasicColumn ;
26
27
import org .mybatis .dynamic .sql .BindableColumn ;
@@ -49,6 +50,7 @@ public class QueryExpressionDSL<R> implements Buildable<R> {
49
50
private GroupByModel groupByModel ;
50
51
private JoinModel joinModel ;
51
52
private List <JoinSpecification > joinSpecifications = new ArrayList <>();
53
+ private Supplier <R > buildDelegateMethod ;
52
54
53
55
private QueryExpressionDSL (FromGatherer <R > fromGatherer ) {
54
56
connector = fromGatherer .builder .connector ;
@@ -57,6 +59,7 @@ private QueryExpressionDSL(FromGatherer<R> fromGatherer) {
57
59
selectDSL = Objects .requireNonNull (fromGatherer .builder .selectDSL );
58
60
table = Objects .requireNonNull (fromGatherer .table );
59
61
tableAliases .putAll (fromGatherer .tableAliasMap );
62
+ buildDelegateMethod = this ::internalBuild ;
60
63
}
61
64
62
65
public <T > QueryExpressionWhereBuilder where (BindableColumn <T > column , VisitableCondition <T > condition ) {
@@ -70,6 +73,10 @@ public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, Visitable
70
73
71
74
@ Override
72
75
public R build () {
76
+ return buildDelegateMethod .get ();
77
+ }
78
+
79
+ private R internalBuild () {
73
80
selectDSL .addQueryExpression (buildModel ());
74
81
return selectDSL .build ();
75
82
}
@@ -117,6 +124,7 @@ public GroupByFinisher groupBy(BasicColumn...columns) {
117
124
}
118
125
119
126
public SelectDSL <R > orderBy (SortSpecification ...columns ) {
127
+ buildDelegateMethod = selectDSL ::build ;
120
128
selectDSL .addQueryExpression (buildModel ());
121
129
selectDSL .setOrderByModel (OrderByModel .of (columns ));
122
130
return selectDSL ;
@@ -145,16 +153,19 @@ protected QueryExpressionModel buildModel() {
145
153
}
146
154
147
155
public SelectDSL <R >.LimitFinisher limit (long limit ) {
156
+ buildDelegateMethod = selectDSL ::build ;
148
157
selectDSL .addQueryExpression (buildModel ());
149
158
return selectDSL .limit (limit );
150
159
}
151
160
152
161
public SelectDSL <R >.OffsetFirstFinisher offset (long offset ) {
162
+ buildDelegateMethod = selectDSL ::build ;
153
163
selectDSL .addQueryExpression (buildModel ());
154
164
return selectDSL .offset (offset );
155
165
}
156
166
157
167
public SelectDSL <R >.FetchFirstFinisher fetchFirst (long fetchFirstRows ) {
168
+ buildDelegateMethod = selectDSL ::build ;
158
169
selectDSL .addQueryExpression (buildModel ());
159
170
return selectDSL .fetchFirst (fetchFirstRows );
160
171
}
@@ -216,11 +227,13 @@ public class QueryExpressionWhereBuilder extends AbstractWhereDSL<QueryExpressio
216
227
implements Buildable <R > {
217
228
private <T > QueryExpressionWhereBuilder (BindableColumn <T > column , VisitableCondition <T > condition ) {
218
229
super (column , condition );
230
+ buildDelegateMethod = this ::internalBuild ;
219
231
}
220
232
221
233
private <T > QueryExpressionWhereBuilder (BindableColumn <T > column , VisitableCondition <T > condition ,
222
234
SqlCriterion <?>...subCriteria ) {
223
235
super (column , condition , subCriteria );
236
+ buildDelegateMethod = this ::internalBuild ;
224
237
}
225
238
226
239
public UnionBuilder union () {
@@ -236,6 +249,7 @@ public UnionBuilder unionAll() {
236
249
}
237
250
238
251
public SelectDSL <R > orderBy (SortSpecification ...columns ) {
252
+ buildDelegateMethod = selectDSL ::build ;
239
253
whereModel = buildWhereModel ();
240
254
selectDSL .addQueryExpression (buildModel ());
241
255
selectDSL .setOrderByModel (OrderByModel .of (columns ));
@@ -262,13 +276,18 @@ public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
262
276
}
263
277
264
278
public SelectDSL <R >.FetchFirstFinisher fetchFirst (long fetchFirstRows ) {
279
+ buildDelegateMethod = selectDSL ::build ;
265
280
whereModel = buildWhereModel ();
266
281
selectDSL .addQueryExpression (buildModel ());
267
282
return selectDSL .fetchFirst (fetchFirstRows );
268
283
}
269
284
270
285
@ Override
271
286
public R build () {
287
+ return buildDelegateMethod .get ();
288
+ }
289
+
290
+ private R internalBuild () {
272
291
whereModel = buildWhereModel ();
273
292
selectDSL .addQueryExpression (buildModel ());
274
293
return selectDSL .build ();
@@ -300,7 +319,6 @@ public JoinSpecificationFinisher on(BasicColumn joinColumn, JoinCondition joinCo
300
319
}
301
320
302
321
public class JoinSpecificationFinisher implements Buildable <R > {
303
-
304
322
private SqlTable joinTable ;
305
323
private List <JoinCriterion > joinCriteria = new ArrayList <>();
306
324
private JoinType joinType ;
@@ -316,6 +334,7 @@ public JoinSpecificationFinisher(SqlTable table, BasicColumn joinColumn,
316
334
.build ();
317
335
318
336
joinCriteria .add (joinCriterion );
337
+ buildDelegateMethod = this ::internalbuild ;
319
338
}
320
339
321
340
public JoinSpecificationFinisher (SqlTable table , BasicColumn joinColumn ,
@@ -330,6 +349,7 @@ public JoinSpecificationFinisher(SqlTable table, BasicColumn joinColumn,
330
349
331
350
this .joinCriteria .add (joinCriterion );
332
351
this .joinCriteria .addAll (Arrays .asList (joinCriteria ));
352
+ buildDelegateMethod = this ::internalbuild ;
333
353
}
334
354
335
355
protected JoinSpecification buildJoinSpecification () {
@@ -346,6 +366,10 @@ protected JoinModel buildJoinModel() {
346
366
347
367
@ Override
348
368
public R build () {
369
+ return buildDelegateMethod .get ();
370
+ }
371
+
372
+ private R internalbuild () {
349
373
joinModel = buildJoinModel ();
350
374
selectDSL .addQueryExpression (buildModel ());
351
375
return selectDSL .build ();
@@ -412,51 +436,67 @@ public JoinSpecificationStarter fullJoin(SqlTable joinTable, String tableAlias)
412
436
}
413
437
414
438
public SelectDSL <R > orderBy (SortSpecification ...columns ) {
439
+ buildDelegateMethod = selectDSL ::build ;
415
440
joinModel = buildJoinModel ();
416
441
selectDSL .addQueryExpression (buildModel ());
417
442
selectDSL .setOrderByModel (OrderByModel .of (columns ));
418
443
return selectDSL ;
419
444
}
420
445
421
446
public SelectDSL <R >.LimitFinisher limit (long limit ) {
447
+ buildDelegateMethod = selectDSL ::build ;
422
448
joinModel = buildJoinModel ();
423
449
selectDSL .addQueryExpression (buildModel ());
424
450
return selectDSL .limit (limit );
425
451
}
426
452
427
453
public SelectDSL <R >.OffsetFirstFinisher offset (long offset ) {
454
+ buildDelegateMethod = selectDSL ::build ;
428
455
joinModel = buildJoinModel ();
429
456
selectDSL .addQueryExpression (buildModel ());
430
457
return selectDSL .offset (offset );
431
458
}
432
459
433
460
public SelectDSL <R >.FetchFirstFinisher fetchFirst (long fetchFirstRows ) {
461
+ buildDelegateMethod = selectDSL ::build ;
434
462
joinModel = buildJoinModel ();
435
463
selectDSL .addQueryExpression (buildModel ());
436
464
return selectDSL .fetchFirst (fetchFirstRows );
437
465
}
438
466
}
439
467
440
468
public class GroupByFinisher implements Buildable <R > {
469
+ public GroupByFinisher () {
470
+ buildDelegateMethod = this ::internalBuild ;
471
+ }
472
+
441
473
public SelectDSL <R > orderBy (SortSpecification ...columns ) {
474
+ buildDelegateMethod = selectDSL ::build ;
442
475
selectDSL .setOrderByModel (OrderByModel .of (columns ));
443
476
return selectDSL ;
444
477
}
445
478
446
479
@ Override
447
480
public R build () {
481
+ return buildDelegateMethod .get ();
482
+ }
483
+
484
+ private R internalBuild () {
448
485
return selectDSL .build ();
449
486
}
450
487
451
488
public SelectDSL <R >.LimitFinisher limit (long limit ) {
489
+ buildDelegateMethod = selectDSL ::build ;
452
490
return selectDSL .limit (limit );
453
491
}
454
492
455
493
public SelectDSL <R >.OffsetFirstFinisher offset (long offset ) {
494
+ buildDelegateMethod = selectDSL ::build ;
456
495
return selectDSL .offset (offset );
457
496
}
458
497
459
498
public SelectDSL <R >.FetchFirstFinisher fetchFirst (long fetchFirstRows ) {
499
+ buildDelegateMethod = selectDSL ::build ;
460
500
return selectDSL .fetchFirst (fetchFirstRows );
461
501
}
462
502
}
0 commit comments