Skip to content

Commit bb18534

Browse files
committed
Refactor having for better standalone function
1 parent 31f5dbe commit bb18534

File tree

9 files changed

+142
-128
lines changed

9 files changed

+142
-128
lines changed

src/main/java/org/mybatis/dynamic/sql/common/AbstractBooleanExpressionDSL.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ private void addSubCriteria(String connector, List<AndOrCriteriaGroup> criteria)
145145
.build());
146146
}
147147

148-
protected void setInitialCriterion(SqlCriterion initialCriterion) {
148+
protected void setInitialCriterion(SqlCriterion initialCriterion, String errorMessage) {
149149
if (this.initialCriterion != null) {
150-
throw new InvalidSqlException(Messages.getString("ERROR.32")); //$NON-NLS-1$
150+
throw new InvalidSqlException(Messages.getString(errorMessage)); //$NON-NLS-1$
151151
}
152152

153153
this.initialCriterion = initialCriterion;
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2016-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.select;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
21+
import org.mybatis.dynamic.sql.AndOrCriteriaGroup;
22+
import org.mybatis.dynamic.sql.BindableColumn;
23+
import org.mybatis.dynamic.sql.ColumnAndConditionCriterion;
24+
import org.mybatis.dynamic.sql.CriteriaGroup;
25+
import org.mybatis.dynamic.sql.SqlCriterion;
26+
import org.mybatis.dynamic.sql.VisitableCondition;
27+
import org.mybatis.dynamic.sql.common.AbstractBooleanExpressionDSL;
28+
29+
public abstract class AbstractHavingDSL<T extends AbstractHavingDSL<T>> extends AbstractBooleanExpressionDSL<T> {
30+
public <S> T having(BindableColumn<S> column, VisitableCondition<S> condition,
31+
AndOrCriteriaGroup... subCriteria) {
32+
return having(column, condition, Arrays.asList(subCriteria));
33+
}
34+
35+
public <S> T having(BindableColumn<S> column, VisitableCondition<S> condition,
36+
List<AndOrCriteriaGroup> subCriteria) {
37+
setInitialCriterion(ColumnAndConditionCriterion.withColumn(column)
38+
.withCondition(condition)
39+
.withSubCriteria(subCriteria)
40+
.build(), "ERROR.31"); //$NON-NLS-1$
41+
return getThis();
42+
}
43+
44+
public T having(SqlCriterion initialCriterion, AndOrCriteriaGroup... subCriteria) {
45+
return having(initialCriterion, Arrays.asList(subCriteria));
46+
}
47+
48+
public T having(SqlCriterion initialCriterion, List<AndOrCriteriaGroup> subCriteria) {
49+
setInitialCriterion(new CriteriaGroup.Builder()
50+
.withInitialCriterion(initialCriterion)
51+
.withSubCriteria(subCriteria)
52+
.build(), "ERROR.31"); //$NON-NLS-1$
53+
return getThis();
54+
}
55+
56+
public T applyHaving(HavingApplier havingApplier) {
57+
havingApplier.accept(this);
58+
return getThis();
59+
}
60+
61+
@Override
62+
protected abstract T getThis();
63+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2016-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.select;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
import java.util.function.Consumer;
21+
22+
import org.mybatis.dynamic.sql.AndOrCriteriaGroup;
23+
import org.mybatis.dynamic.sql.BindableColumn;
24+
import org.mybatis.dynamic.sql.SqlCriterion;
25+
import org.mybatis.dynamic.sql.VisitableCondition;
26+
27+
public abstract class AbstractHavingSupport<W extends AbstractHavingDSL<?>> {
28+
public abstract W having();
29+
30+
public <S> W having(BindableColumn<S> column, VisitableCondition<S> condition,
31+
AndOrCriteriaGroup... subCriteria) {
32+
return having(column, condition, Arrays.asList(subCriteria));
33+
}
34+
35+
public <S> W having(BindableColumn<S> column, VisitableCondition<S> condition,
36+
List<AndOrCriteriaGroup> subCriteria) {
37+
return apply(w -> w.having(column, condition, subCriteria));
38+
}
39+
40+
public W having(SqlCriterion initialCriterion, AndOrCriteriaGroup... subCriteria) {
41+
return having(initialCriterion, Arrays.asList(subCriteria));
42+
}
43+
44+
public W having(SqlCriterion initialCriterion, List<AndOrCriteriaGroup> subCriteria) {
45+
return apply(w -> w.having(initialCriterion, subCriteria));
46+
}
47+
48+
public W applyHaving(HavingApplier havingApplier) {
49+
return apply(w -> w.applyHaving(havingApplier));
50+
}
51+
52+
private W apply(Consumer<W> block) {
53+
W dsl = having();
54+
block.accept(dsl);
55+
return dsl;
56+
}
57+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 the original author or authors.
2+
* Copyright 2016-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,7 +22,7 @@
2222
@FunctionalInterface
2323
public interface HavingApplier {
2424

25-
void accept(HavingDSL havingDSL);
25+
void accept(AbstractHavingDSL<?> havingDSL);
2626

2727
/**
2828
* Return a composed having applier that performs this operation followed by the after operation.

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

Lines changed: 0 additions & 63 deletions
This file was deleted.

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

Lines changed: 8 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,16 @@
2323
import java.util.function.Consumer;
2424

2525
import org.jetbrains.annotations.NotNull;
26-
import org.mybatis.dynamic.sql.AndOrCriteriaGroup;
2726
import org.mybatis.dynamic.sql.BasicColumn;
28-
import org.mybatis.dynamic.sql.BindableColumn;
29-
import org.mybatis.dynamic.sql.ColumnAndConditionCriterion;
30-
import org.mybatis.dynamic.sql.CriteriaGroup;
3127
import org.mybatis.dynamic.sql.SortSpecification;
32-
import org.mybatis.dynamic.sql.SqlCriterion;
3328
import org.mybatis.dynamic.sql.SqlTable;
3429
import org.mybatis.dynamic.sql.TableExpression;
35-
import org.mybatis.dynamic.sql.VisitableCondition;
36-
import org.mybatis.dynamic.sql.common.AbstractBooleanExpressionDSL;
3730
import org.mybatis.dynamic.sql.configuration.StatementConfiguration;
38-
import org.mybatis.dynamic.sql.exception.InvalidSqlException;
3931
import org.mybatis.dynamic.sql.select.join.JoinCondition;
4032
import org.mybatis.dynamic.sql.select.join.JoinCriterion;
4133
import org.mybatis.dynamic.sql.select.join.JoinSpecification;
4234
import org.mybatis.dynamic.sql.select.join.JoinType;
4335
import org.mybatis.dynamic.sql.util.Buildable;
44-
import org.mybatis.dynamic.sql.util.Messages;
4536
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
4637
import org.mybatis.dynamic.sql.where.AbstractWhereSupport;
4738
import org.mybatis.dynamic.sql.where.WhereModel;
@@ -87,18 +78,10 @@ public QueryExpressionDSL<R> configureStatement(Consumer<StatementConfiguration>
8778
return this;
8879
}
8980

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) {
97-
if (havingBuilder != null) {
98-
throw new InvalidSqlException(Messages.getString("ERROR.31")); //$NON-NLS-1$
81+
protected QueryExpressionHavingBuilder having() {
82+
if (havingBuilder == null) {
83+
havingBuilder = new QueryExpressionHavingBuilder();
9984
}
100-
101-
havingBuilder = new QueryExpressionHavingBuilder(initialCriterion);
10285
return havingBuilder;
10386
}
10487

@@ -509,7 +492,7 @@ public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
509492
}
510493
}
511494

512-
public class GroupByFinisher implements Buildable<R> {
495+
public class GroupByFinisher extends AbstractHavingSupport<QueryExpressionHavingBuilder> implements Buildable<R> {
513496
public SelectDSL<R> orderBy(SortSpecification... columns) {
514497
return orderBy(Arrays.asList(columns));
515498
}
@@ -544,33 +527,9 @@ public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
544527
return QueryExpressionDSL.this.fetchFirst(fetchFirstRows);
545528
}
546529

547-
public <T> QueryExpressionHavingBuilder having(BindableColumn<T> column, VisitableCondition<T> condition,
548-
AndOrCriteriaGroup... subCriteria) {
549-
return having(column, condition, Arrays.asList(subCriteria));
550-
}
551-
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, AndOrCriteriaGroup... subCriteria) {
561-
return having(initialCriterion, Arrays.asList(subCriteria));
562-
}
563-
564-
public QueryExpressionHavingBuilder having(SqlCriterion initialCriterion,
565-
List<AndOrCriteriaGroup> subCriteria) {
566-
return QueryExpressionDSL.this.having(initialCriterion, subCriteria);
567-
}
568-
569-
public QueryExpressionDSL<R> applyHaving(HavingApplier havingApplier) {
570-
HavingDSL s = new HavingDSL();
571-
havingApplier.accept(s);
572-
QueryExpressionDSL.this.having(s.getInitialCriterion(), s.getSubCriteria());
573-
return QueryExpressionDSL.this;
530+
@Override
531+
public QueryExpressionHavingBuilder having() {
532+
return QueryExpressionDSL.this.having();
574533
}
575534
}
576535

@@ -607,13 +566,9 @@ public FromGatherer<R> selectDistinct(List<BasicColumn> selectList) {
607566
}
608567
}
609568

610-
public class QueryExpressionHavingBuilder extends AbstractBooleanExpressionDSL<QueryExpressionHavingBuilder>
569+
public class QueryExpressionHavingBuilder extends AbstractHavingDSL<QueryExpressionHavingBuilder>
611570
implements Buildable<R> {
612571

613-
public QueryExpressionHavingBuilder(SqlCriterion initialCriterion) {
614-
setInitialCriterion(initialCriterion);
615-
}
616-
617572
public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
618573
return QueryExpressionDSL.this.fetchFirst(fetchFirstRows);
619574
}

src/main/java/org/mybatis/dynamic/sql/where/AbstractWhereDSL.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public <S> T where(BindableColumn<S> column, VisitableCondition<S> condition,
5959
setInitialCriterion(ColumnAndConditionCriterion.withColumn(column)
6060
.withCondition(condition)
6161
.withSubCriteria(subCriteria)
62-
.build());
62+
.build(), "ERROR.32"); //$NON-NLS-1$
6363
return getThis();
6464
}
6565

@@ -71,7 +71,7 @@ public T where(ExistsPredicate existsPredicate, AndOrCriteriaGroup... subCriteri
7171
@NotNull
7272
public T where(ExistsPredicate existsPredicate, List<AndOrCriteriaGroup> subCriteria) {
7373
setInitialCriterion(new ExistsCriterion.Builder()
74-
.withExistsPredicate(existsPredicate).withSubCriteria(subCriteria).build());
74+
.withExistsPredicate(existsPredicate).withSubCriteria(subCriteria).build(), "ERROR.32"); //$NON-NLS-1$
7575
return getThis();
7676
}
7777

@@ -85,15 +85,15 @@ public T where(SqlCriterion initialCriterion, List<AndOrCriteriaGroup> subCriter
8585
setInitialCriterion(new CriteriaGroup.Builder()
8686
.withInitialCriterion(initialCriterion)
8787
.withSubCriteria(subCriteria)
88-
.build());
88+
.build(), "ERROR.32"); //$NON-NLS-1$
8989
return getThis();
9090
}
9191

9292
@NotNull
9393
public T where(List<AndOrCriteriaGroup> criteria) {
9494
setInitialCriterion(new CriteriaGroup.Builder()
9595
.withSubCriteria(criteria)
96-
.build());
96+
.build(), "ERROR.32"); //$NON-NLS-1$
9797
return getThis();
9898
}
9999

@@ -107,5 +107,6 @@ protected WhereModel internalBuild() {
107107
return new WhereModel(getInitialCriterion(), subCriteria, statementConfiguration);
108108
}
109109

110+
@Override
110111
protected abstract T getThis();
111112
}

src/main/kotlin/org/mybatis/dynamic/sql/util/kotlin/KotlinSelectBuilder.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class KotlinSelectBuilder(private val fromGatherer: QueryExpressionDSL.FromGathe
5353

5454
fun having(criteria: GroupingCriteriaReceiver): Unit =
5555
GroupingCriteriaCollector().apply(criteria).let {
56-
getDsl().applyHaving(it.initialCriterion, it.subCriteria)
56+
getDsl().having(it.initialCriterion, it.subCriteria)
5757
}
5858

5959
fun orderBy(vararg columns: SortSpecification) {
@@ -99,8 +99,8 @@ class KQueryExpressionDSL: QueryExpressionDSL<SelectModel> {
9999
constructor(fromGatherer: FromGatherer<SelectModel>, subQuery: KotlinQualifiedSubQueryBuilder) :
100100
super(fromGatherer, buildSubQuery(subQuery))
101101

102-
internal fun applyHaving(initialCriterion: SqlCriterion?, subCriteria: List<AndOrCriteriaGroup>) {
103-
having(initialCriterion, subCriteria)
102+
internal fun having(initialCriterion: SqlCriterion?, subCriteria: List<AndOrCriteriaGroup>) {
103+
having().applyHaving{ it.having(initialCriterion, subCriteria) }
104104
}
105105

106106
companion object {

0 commit comments

Comments
 (0)