27
27
import org .mybatis .dynamic .sql .BasicColumn ;
28
28
import org .mybatis .dynamic .sql .BindableColumn ;
29
29
import org .mybatis .dynamic .sql .ColumnAndConditionCriterion ;
30
+ import org .mybatis .dynamic .sql .CriteriaGroup ;
30
31
import org .mybatis .dynamic .sql .SortSpecification ;
32
+ import org .mybatis .dynamic .sql .SqlCriterion ;
31
33
import org .mybatis .dynamic .sql .SqlTable ;
32
34
import org .mybatis .dynamic .sql .TableExpression ;
33
35
import org .mybatis .dynamic .sql .VisitableCondition ;
@@ -55,17 +57,18 @@ public class QueryExpressionDSL<R>
55
57
private QueryExpressionWhereBuilder whereBuilder ;
56
58
private GroupByModel groupByModel ;
57
59
private final StatementConfiguration statementConfiguration = new StatementConfiguration ();
58
- private HavingBuilder havingBuilder ;
60
+ private QueryExpressionHavingBuilder havingBuilder ;
59
61
60
- QueryExpressionDSL (FromGatherer <R > fromGatherer , TableExpression table ) {
62
+ protected QueryExpressionDSL (FromGatherer <R > fromGatherer , TableExpression table ) {
61
63
super (table );
62
64
connector = fromGatherer .connector ;
63
65
selectList = fromGatherer .selectList ;
64
66
isDistinct = fromGatherer .isDistinct ;
65
67
selectDSL = Objects .requireNonNull (fromGatherer .selectDSL );
68
+ selectDSL .registerQueryExpression (this );
66
69
}
67
70
68
- QueryExpressionDSL (FromGatherer <R > fromGatherer , SqlTable table , String tableAlias ) {
71
+ protected QueryExpressionDSL (FromGatherer <R > fromGatherer , SqlTable table , String tableAlias ) {
69
72
this (fromGatherer , table );
70
73
addTableAlias (table , tableAlias );
71
74
}
@@ -78,22 +81,27 @@ public QueryExpressionWhereBuilder where() {
78
81
return whereBuilder ;
79
82
}
80
83
81
- public <T > HavingBuilder having (BindableColumn <T > column , VisitableCondition <T > condition ,
82
- List <AndOrCriteriaGroup > subCriteria ) {
84
+ @ Override
85
+ public QueryExpressionDSL <R > configureStatement (Consumer <StatementConfiguration > consumer ) {
86
+ consumer .accept (statementConfiguration );
87
+ return this ;
88
+ }
89
+
90
+ protected QueryExpressionHavingBuilder having (SqlCriterion initialCriterion , List <AndOrCriteriaGroup > subCriteria ) {
91
+ return having (new CriteriaGroup .Builder ().withInitialCriterion (initialCriterion )
92
+ .withSubCriteria (subCriteria )
93
+ .build ());
94
+ }
95
+
96
+ private QueryExpressionHavingBuilder having (SqlCriterion initialCriterion ) {
83
97
if (havingBuilder != null ) {
84
98
throw new InvalidSqlException (Messages .getString ("ERROR.31" )); //$NON-NLS-1$
85
99
}
86
100
87
- havingBuilder = new HavingBuilder ( column , condition , subCriteria );
101
+ havingBuilder = new QueryExpressionHavingBuilder ( initialCriterion );
88
102
return havingBuilder ;
89
103
}
90
104
91
- @ Override
92
- public QueryExpressionDSL <R > configureStatement (Consumer <StatementConfiguration > consumer ) {
93
- consumer .accept (statementConfiguration );
94
- return this ;
95
- }
96
-
97
105
@ NotNull
98
106
@ Override
99
107
public R build () {
@@ -223,25 +231,25 @@ public static class FromGatherer<R> {
223
231
224
232
public FromGatherer (Builder <R > builder ) {
225
233
this .connector = builder .connector ;
226
- this .selectList = Objects . requireNonNull ( builder .selectList ) ;
234
+ this .selectList = builder .selectList ;
227
235
this .selectDSL = Objects .requireNonNull (builder .selectDSL );
228
236
this .isDistinct = builder .isDistinct ;
229
237
}
230
238
231
239
public QueryExpressionDSL <R > from (Buildable <SelectModel > select ) {
232
- return selectDSL . newQueryExpression (this , buildSubQuery (select ));
240
+ return new QueryExpressionDSL <> (this , buildSubQuery (select ));
233
241
}
234
242
235
243
public QueryExpressionDSL <R > from (Buildable <SelectModel > select , String tableAlias ) {
236
- return selectDSL . newQueryExpression (this , buildSubQuery (select , tableAlias ));
244
+ return new QueryExpressionDSL <> (this , buildSubQuery (select , tableAlias ));
237
245
}
238
246
239
247
public QueryExpressionDSL <R > from (SqlTable table ) {
240
- return selectDSL . newQueryExpression (this , table );
248
+ return new QueryExpressionDSL <> (this , table );
241
249
}
242
250
243
251
public QueryExpressionDSL <R > from (SqlTable table , String tableAlias ) {
244
- return selectDSL . newQueryExpression (this , table , tableAlias );
252
+ return new QueryExpressionDSL <> (this , table , tableAlias );
245
253
}
246
254
247
255
public static class Builder <R > {
@@ -536,14 +544,21 @@ public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
536
544
return QueryExpressionDSL .this .fetchFirst (fetchFirstRows );
537
545
}
538
546
539
- public <T > HavingBuilder having (BindableColumn <T > column , VisitableCondition <T > condition ,
540
- AndOrCriteriaGroup ... subCriteria ) {
547
+ public <T > QueryExpressionHavingBuilder having (BindableColumn <T > column , VisitableCondition <T > condition ,
548
+ AndOrCriteriaGroup ... subCriteria ) {
541
549
return having (column , condition , Arrays .asList (subCriteria ));
542
550
}
543
551
544
- public <T > HavingBuilder having (BindableColumn <T > column , VisitableCondition <T > condition ,
545
- List <AndOrCriteriaGroup > subCriteria ) {
546
- return QueryExpressionDSL .this .having (column , condition , subCriteria );
552
+ public <T > QueryExpressionHavingBuilder having (BindableColumn <T > column , VisitableCondition <T > condition ,
553
+ List <AndOrCriteriaGroup > subCriteria ) {
554
+ return QueryExpressionDSL .this .having (ColumnAndConditionCriterion .withColumn (column )
555
+ .withCondition (condition )
556
+ .withSubCriteria (subCriteria )
557
+ .build ());
558
+ }
559
+
560
+ public QueryExpressionHavingBuilder having (SqlCriterion initialCriterion , List <AndOrCriteriaGroup > subCriteria ) {
561
+ return QueryExpressionDSL .this .having (initialCriterion , subCriteria );
547
562
}
548
563
}
549
564
@@ -580,14 +595,10 @@ public FromGatherer<R> selectDistinct(List<BasicColumn> selectList) {
580
595
}
581
596
}
582
597
583
- public class HavingBuilder extends AbstractBooleanExpressionDSL <HavingBuilder > implements Buildable <R > {
598
+ public class QueryExpressionHavingBuilder extends AbstractBooleanExpressionDSL <QueryExpressionHavingBuilder > implements Buildable <R > {
584
599
585
- public <T > HavingBuilder (BindableColumn <T > column , VisitableCondition <T > condition ,
586
- List <AndOrCriteriaGroup > subCriteria ) {
587
- initialCriterion = ColumnAndConditionCriterion .withColumn (column )
588
- .withCondition (condition )
589
- .withSubCriteria (subCriteria )
590
- .build ();
600
+ public QueryExpressionHavingBuilder (SqlCriterion initialCriterion ) {
601
+ this .initialCriterion = initialCriterion ;
591
602
}
592
603
593
604
public SelectDSL <R > orderBy (SortSpecification ... columns ) {
@@ -625,7 +636,7 @@ public R build() {
625
636
}
626
637
627
638
@ Override
628
- protected HavingBuilder getThis () {
639
+ protected QueryExpressionHavingBuilder getThis () {
629
640
return this ;
630
641
}
631
642
0 commit comments