2121import java .util .List ;
2222import java .util .Map ;
2323import java .util .Objects ;
24+ import java .util .function .Supplier ;
2425
2526import org .mybatis .dynamic .sql .BasicColumn ;
2627import org .mybatis .dynamic .sql .BindableColumn ;
@@ -49,6 +50,7 @@ public class QueryExpressionDSL<R> implements Buildable<R> {
4950 private GroupByModel groupByModel ;
5051 private JoinModel joinModel ;
5152 private List <JoinSpecification > joinSpecifications = new ArrayList <>();
53+ private Supplier <R > buildDelegateMethod ;
5254
5355 private QueryExpressionDSL (FromGatherer <R > fromGatherer ) {
5456 connector = fromGatherer .builder .connector ;
@@ -57,6 +59,7 @@ private QueryExpressionDSL(FromGatherer<R> fromGatherer) {
5759 selectDSL = Objects .requireNonNull (fromGatherer .builder .selectDSL );
5860 table = Objects .requireNonNull (fromGatherer .table );
5961 tableAliases .putAll (fromGatherer .tableAliasMap );
62+ buildDelegateMethod = this ::internalBuild ;
6063 }
6164
6265 public <T > QueryExpressionWhereBuilder where (BindableColumn <T > column , VisitableCondition <T > condition ) {
@@ -70,6 +73,10 @@ public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, Visitable
7073
7174 @ Override
7275 public R build () {
76+ return buildDelegateMethod .get ();
77+ }
78+
79+ private R internalBuild () {
7380 selectDSL .addQueryExpression (buildModel ());
7481 return selectDSL .build ();
7582 }
@@ -117,6 +124,7 @@ public GroupByFinisher groupBy(BasicColumn...columns) {
117124 }
118125
119126 public SelectDSL <R > orderBy (SortSpecification ...columns ) {
127+ buildDelegateMethod = selectDSL ::build ;
120128 selectDSL .addQueryExpression (buildModel ());
121129 selectDSL .setOrderByModel (OrderByModel .of (columns ));
122130 return selectDSL ;
@@ -145,16 +153,19 @@ protected QueryExpressionModel buildModel() {
145153 }
146154
147155 public SelectDSL <R >.LimitFinisher limit (long limit ) {
156+ buildDelegateMethod = selectDSL ::build ;
148157 selectDSL .addQueryExpression (buildModel ());
149158 return selectDSL .limit (limit );
150159 }
151160
152161 public SelectDSL <R >.OffsetFirstFinisher offset (long offset ) {
162+ buildDelegateMethod = selectDSL ::build ;
153163 selectDSL .addQueryExpression (buildModel ());
154164 return selectDSL .offset (offset );
155165 }
156166
157167 public SelectDSL <R >.FetchFirstFinisher fetchFirst (long fetchFirstRows ) {
168+ buildDelegateMethod = selectDSL ::build ;
158169 selectDSL .addQueryExpression (buildModel ());
159170 return selectDSL .fetchFirst (fetchFirstRows );
160171 }
@@ -216,11 +227,13 @@ public class QueryExpressionWhereBuilder extends AbstractWhereDSL<QueryExpressio
216227 implements Buildable <R > {
217228 private <T > QueryExpressionWhereBuilder (BindableColumn <T > column , VisitableCondition <T > condition ) {
218229 super (column , condition );
230+ buildDelegateMethod = this ::internalBuild ;
219231 }
220232
221233 private <T > QueryExpressionWhereBuilder (BindableColumn <T > column , VisitableCondition <T > condition ,
222234 SqlCriterion <?>...subCriteria ) {
223235 super (column , condition , subCriteria );
236+ buildDelegateMethod = this ::internalBuild ;
224237 }
225238
226239 public UnionBuilder union () {
@@ -236,6 +249,7 @@ public UnionBuilder unionAll() {
236249 }
237250
238251 public SelectDSL <R > orderBy (SortSpecification ...columns ) {
252+ buildDelegateMethod = selectDSL ::build ;
239253 whereModel = buildWhereModel ();
240254 selectDSL .addQueryExpression (buildModel ());
241255 selectDSL .setOrderByModel (OrderByModel .of (columns ));
@@ -262,13 +276,18 @@ public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
262276 }
263277
264278 public SelectDSL <R >.FetchFirstFinisher fetchFirst (long fetchFirstRows ) {
279+ buildDelegateMethod = selectDSL ::build ;
265280 whereModel = buildWhereModel ();
266281 selectDSL .addQueryExpression (buildModel ());
267282 return selectDSL .fetchFirst (fetchFirstRows );
268283 }
269284
270285 @ Override
271286 public R build () {
287+ return buildDelegateMethod .get ();
288+ }
289+
290+ private R internalBuild () {
272291 whereModel = buildWhereModel ();
273292 selectDSL .addQueryExpression (buildModel ());
274293 return selectDSL .build ();
@@ -300,7 +319,6 @@ public JoinSpecificationFinisher on(BasicColumn joinColumn, JoinCondition joinCo
300319 }
301320
302321 public class JoinSpecificationFinisher implements Buildable <R > {
303-
304322 private SqlTable joinTable ;
305323 private List <JoinCriterion > joinCriteria = new ArrayList <>();
306324 private JoinType joinType ;
@@ -316,6 +334,7 @@ public JoinSpecificationFinisher(SqlTable table, BasicColumn joinColumn,
316334 .build ();
317335
318336 joinCriteria .add (joinCriterion );
337+ buildDelegateMethod = this ::internalbuild ;
319338 }
320339
321340 public JoinSpecificationFinisher (SqlTable table , BasicColumn joinColumn ,
@@ -330,6 +349,7 @@ public JoinSpecificationFinisher(SqlTable table, BasicColumn joinColumn,
330349
331350 this .joinCriteria .add (joinCriterion );
332351 this .joinCriteria .addAll (Arrays .asList (joinCriteria ));
352+ buildDelegateMethod = this ::internalbuild ;
333353 }
334354
335355 protected JoinSpecification buildJoinSpecification () {
@@ -346,6 +366,10 @@ protected JoinModel buildJoinModel() {
346366
347367 @ Override
348368 public R build () {
369+ return buildDelegateMethod .get ();
370+ }
371+
372+ private R internalbuild () {
349373 joinModel = buildJoinModel ();
350374 selectDSL .addQueryExpression (buildModel ());
351375 return selectDSL .build ();
@@ -412,51 +436,67 @@ public JoinSpecificationStarter fullJoin(SqlTable joinTable, String tableAlias)
412436 }
413437
414438 public SelectDSL <R > orderBy (SortSpecification ...columns ) {
439+ buildDelegateMethod = selectDSL ::build ;
415440 joinModel = buildJoinModel ();
416441 selectDSL .addQueryExpression (buildModel ());
417442 selectDSL .setOrderByModel (OrderByModel .of (columns ));
418443 return selectDSL ;
419444 }
420445
421446 public SelectDSL <R >.LimitFinisher limit (long limit ) {
447+ buildDelegateMethod = selectDSL ::build ;
422448 joinModel = buildJoinModel ();
423449 selectDSL .addQueryExpression (buildModel ());
424450 return selectDSL .limit (limit );
425451 }
426452
427453 public SelectDSL <R >.OffsetFirstFinisher offset (long offset ) {
454+ buildDelegateMethod = selectDSL ::build ;
428455 joinModel = buildJoinModel ();
429456 selectDSL .addQueryExpression (buildModel ());
430457 return selectDSL .offset (offset );
431458 }
432459
433460 public SelectDSL <R >.FetchFirstFinisher fetchFirst (long fetchFirstRows ) {
461+ buildDelegateMethod = selectDSL ::build ;
434462 joinModel = buildJoinModel ();
435463 selectDSL .addQueryExpression (buildModel ());
436464 return selectDSL .fetchFirst (fetchFirstRows );
437465 }
438466 }
439467
440468 public class GroupByFinisher implements Buildable <R > {
469+ public GroupByFinisher () {
470+ buildDelegateMethod = this ::internalBuild ;
471+ }
472+
441473 public SelectDSL <R > orderBy (SortSpecification ...columns ) {
474+ buildDelegateMethod = selectDSL ::build ;
442475 selectDSL .setOrderByModel (OrderByModel .of (columns ));
443476 return selectDSL ;
444477 }
445478
446479 @ Override
447480 public R build () {
481+ return buildDelegateMethod .get ();
482+ }
483+
484+ private R internalBuild () {
448485 return selectDSL .build ();
449486 }
450487
451488 public SelectDSL <R >.LimitFinisher limit (long limit ) {
489+ buildDelegateMethod = selectDSL ::build ;
452490 return selectDSL .limit (limit );
453491 }
454492
455493 public SelectDSL <R >.OffsetFirstFinisher offset (long offset ) {
494+ buildDelegateMethod = selectDSL ::build ;
456495 return selectDSL .offset (offset );
457496 }
458497
459498 public SelectDSL <R >.FetchFirstFinisher fetchFirst (long fetchFirstRows ) {
499+ buildDelegateMethod = selectDSL ::build ;
460500 return selectDSL .fetchFirst (fetchFirstRows );
461501 }
462502 }
0 commit comments