Skip to content

Commit dac18e3

Browse files
committed
Refactor Java DSL to support having in Kotlin
1 parent 112efe8 commit dac18e3

File tree

2 files changed

+42
-41
lines changed

2 files changed

+42
-41
lines changed

src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionDSL.java

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import org.mybatis.dynamic.sql.BasicColumn;
2828
import org.mybatis.dynamic.sql.BindableColumn;
2929
import org.mybatis.dynamic.sql.ColumnAndConditionCriterion;
30+
import org.mybatis.dynamic.sql.CriteriaGroup;
3031
import org.mybatis.dynamic.sql.SortSpecification;
32+
import org.mybatis.dynamic.sql.SqlCriterion;
3133
import org.mybatis.dynamic.sql.SqlTable;
3234
import org.mybatis.dynamic.sql.TableExpression;
3335
import org.mybatis.dynamic.sql.VisitableCondition;
@@ -55,17 +57,18 @@ public class QueryExpressionDSL<R>
5557
private QueryExpressionWhereBuilder whereBuilder;
5658
private GroupByModel groupByModel;
5759
private final StatementConfiguration statementConfiguration = new StatementConfiguration();
58-
private HavingBuilder havingBuilder;
60+
private QueryExpressionHavingBuilder havingBuilder;
5961

60-
QueryExpressionDSL(FromGatherer<R> fromGatherer, TableExpression table) {
62+
protected QueryExpressionDSL(FromGatherer<R> fromGatherer, TableExpression table) {
6163
super(table);
6264
connector = fromGatherer.connector;
6365
selectList = fromGatherer.selectList;
6466
isDistinct = fromGatherer.isDistinct;
6567
selectDSL = Objects.requireNonNull(fromGatherer.selectDSL);
68+
selectDSL.registerQueryExpression(this);
6669
}
6770

68-
QueryExpressionDSL(FromGatherer<R> fromGatherer, SqlTable table, String tableAlias) {
71+
protected QueryExpressionDSL(FromGatherer<R> fromGatherer, SqlTable table, String tableAlias) {
6972
this(fromGatherer, table);
7073
addTableAlias(table, tableAlias);
7174
}
@@ -78,22 +81,27 @@ public QueryExpressionWhereBuilder where() {
7881
return whereBuilder;
7982
}
8083

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) {
8397
if (havingBuilder != null) {
8498
throw new InvalidSqlException(Messages.getString("ERROR.31")); //$NON-NLS-1$
8599
}
86100

87-
havingBuilder = new HavingBuilder(column, condition, subCriteria);
101+
havingBuilder = new QueryExpressionHavingBuilder(initialCriterion);
88102
return havingBuilder;
89103
}
90104

91-
@Override
92-
public QueryExpressionDSL<R> configureStatement(Consumer<StatementConfiguration> consumer) {
93-
consumer.accept(statementConfiguration);
94-
return this;
95-
}
96-
97105
@NotNull
98106
@Override
99107
public R build() {
@@ -223,25 +231,25 @@ public static class FromGatherer<R> {
223231

224232
public FromGatherer(Builder<R> builder) {
225233
this.connector = builder.connector;
226-
this.selectList = Objects.requireNonNull(builder.selectList);
234+
this.selectList = builder.selectList;
227235
this.selectDSL = Objects.requireNonNull(builder.selectDSL);
228236
this.isDistinct = builder.isDistinct;
229237
}
230238

231239
public QueryExpressionDSL<R> from(Buildable<SelectModel> select) {
232-
return selectDSL.newQueryExpression(this, buildSubQuery(select));
240+
return new QueryExpressionDSL<>(this, buildSubQuery(select));
233241
}
234242

235243
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));
237245
}
238246

239247
public QueryExpressionDSL<R> from(SqlTable table) {
240-
return selectDSL.newQueryExpression(this, table);
248+
return new QueryExpressionDSL<>(this, table);
241249
}
242250

243251
public QueryExpressionDSL<R> from(SqlTable table, String tableAlias) {
244-
return selectDSL.newQueryExpression(this, table, tableAlias);
252+
return new QueryExpressionDSL<>(this, table, tableAlias);
245253
}
246254

247255
public static class Builder<R> {
@@ -536,14 +544,21 @@ public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
536544
return QueryExpressionDSL.this.fetchFirst(fetchFirstRows);
537545
}
538546

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) {
541549
return having(column, condition, Arrays.asList(subCriteria));
542550
}
543551

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);
547562
}
548563
}
549564

@@ -580,14 +595,10 @@ public FromGatherer<R> selectDistinct(List<BasicColumn> selectList) {
580595
}
581596
}
582597

583-
public class HavingBuilder extends AbstractBooleanExpressionDSL<HavingBuilder> implements Buildable<R> {
598+
public class QueryExpressionHavingBuilder extends AbstractBooleanExpressionDSL<QueryExpressionHavingBuilder> implements Buildable<R> {
584599

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;
591602
}
592603

593604
public SelectDSL<R> orderBy(SortSpecification... columns) {
@@ -625,7 +636,7 @@ public R build() {
625636
}
626637

627638
@Override
628-
protected HavingBuilder getThis() {
639+
protected QueryExpressionHavingBuilder getThis() {
629640
return this;
630641
}
631642

src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import org.jetbrains.annotations.NotNull;
2828
import org.mybatis.dynamic.sql.BasicColumn;
2929
import org.mybatis.dynamic.sql.SortSpecification;
30-
import org.mybatis.dynamic.sql.SqlTable;
31-
import org.mybatis.dynamic.sql.TableExpression;
3230
import org.mybatis.dynamic.sql.common.OrderByModel;
3331
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
3432
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
@@ -99,16 +97,8 @@ public static <R> QueryExpressionDSL.FromGatherer<R> selectDistinct(Function<Sel
9997
.build();
10098
}
10199

102-
QueryExpressionDSL<R> newQueryExpression(FromGatherer<R> fromGatherer, TableExpression table) {
103-
QueryExpressionDSL<R> queryExpression = new QueryExpressionDSL<>(fromGatherer, table);
100+
void registerQueryExpression(QueryExpressionDSL<R> queryExpression) {
104101
queryExpressions.add(queryExpression);
105-
return queryExpression;
106-
}
107-
108-
QueryExpressionDSL<R> newQueryExpression(FromGatherer<R> fromGatherer, SqlTable table, String tableAlias) {
109-
QueryExpressionDSL<R> queryExpression = new QueryExpressionDSL<>(fromGatherer, table, tableAlias);
110-
queryExpressions.add(queryExpression);
111-
return queryExpression;
112102
}
113103

114104
void orderBy(Collection<SortSpecification> columns) {

0 commit comments

Comments
 (0)