Skip to content

Commit 6cfe8c2

Browse files
authored
Merge pull request #295 from jeffgbutler/refactor-where-support
Refactor Where support
2 parents a1ef79d + 1cdda23 commit 6cfe8c2

File tree

13 files changed

+161
-160
lines changed

13 files changed

+161
-160
lines changed

src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,13 @@
1919
import java.util.function.Function;
2020

2121
import org.jetbrains.annotations.NotNull;
22-
import org.mybatis.dynamic.sql.BindableColumn;
23-
import org.mybatis.dynamic.sql.SqlCriterion;
2422
import org.mybatis.dynamic.sql.SqlTable;
25-
import org.mybatis.dynamic.sql.VisitableCondition;
2623
import org.mybatis.dynamic.sql.util.Buildable;
2724
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
28-
import org.mybatis.dynamic.sql.where.WhereApplier;
25+
import org.mybatis.dynamic.sql.where.AbstractWhereSupport;
2926
import org.mybatis.dynamic.sql.where.WhereModel;
3027

31-
public class DeleteDSL<R> implements Buildable<R> {
28+
public class DeleteDSL<R> extends AbstractWhereSupport<DeleteDSL<R>.DeleteWhereBuilder> implements Buildable<R> {
3229

3330
private final Function<DeleteModel, R> adapterFunction;
3431
private final SqlTable table;
@@ -39,20 +36,11 @@ private DeleteDSL(SqlTable table, Function<DeleteModel, R> adapterFunction) {
3936
this.adapterFunction = Objects.requireNonNull(adapterFunction);
4037
}
4138

39+
@Override
4240
public DeleteWhereBuilder where() {
4341
return whereBuilder;
4442
}
4543

46-
public <T> DeleteWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition,
47-
SqlCriterion<?>...subCriteria) {
48-
whereBuilder.where(column, condition, subCriteria);
49-
return whereBuilder;
50-
}
51-
52-
public DeleteWhereBuilder applyWhere(WhereApplier whereApplier) {
53-
return whereBuilder.applyWhere(whereApplier);
54-
}
55-
5644
/**
5745
* WARNING! Calling this method could result in an delete statement that deletes
5846
* all rows in a table.
@@ -93,9 +81,8 @@ protected DeleteWhereBuilder getThis() {
9381
return this;
9482
}
9583

96-
@Override
9784
protected WhereModel buildWhereModel() {
98-
return super.internalBuild();
85+
return internalBuild();
9986
}
10087
}
10188
}

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@
3131
import org.mybatis.dynamic.sql.select.join.JoinSpecification;
3232
import org.mybatis.dynamic.sql.select.join.JoinType;
3333
import org.mybatis.dynamic.sql.util.Buildable;
34+
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
35+
import org.mybatis.dynamic.sql.where.AbstractWhereSupport;
3436

35-
public abstract class AbstractQueryExpressionDSL<T extends AbstractQueryExpressionDSL<T, R>, R>
37+
public abstract class AbstractQueryExpressionDSL<W extends AbstractWhereDSL<?>,
38+
T extends AbstractQueryExpressionDSL<W, T, R>, R>
39+
extends AbstractWhereSupport<W>
3640
implements Buildable<R> {
3741

3842
private final List<JoinSpecification.Builder> joinSpecificationBuilders = new ArrayList<>();

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

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,10 @@
2020

2121
import org.jetbrains.annotations.NotNull;
2222
import org.mybatis.dynamic.sql.BasicColumn;
23-
import org.mybatis.dynamic.sql.BindableColumn;
2423
import org.mybatis.dynamic.sql.SqlBuilder;
25-
import org.mybatis.dynamic.sql.SqlCriterion;
2624
import org.mybatis.dynamic.sql.SqlTable;
27-
import org.mybatis.dynamic.sql.VisitableCondition;
2825
import org.mybatis.dynamic.sql.util.Buildable;
2926
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
30-
import org.mybatis.dynamic.sql.where.WhereApplier;
3127
import org.mybatis.dynamic.sql.where.WhereModel;
3228

3329
/**
@@ -39,7 +35,8 @@
3935
*
4036
* @author Jeff Butler
4137
*/
42-
public class CountDSL<R> extends AbstractQueryExpressionDSL<CountDSL<R>, R> implements Buildable<R> {
38+
public class CountDSL<R> extends AbstractQueryExpressionDSL<CountDSL<R>.CountWhereBuilder, CountDSL<R>, R>
39+
implements Buildable<R> {
4340

4441
private final Function<SelectModel, R> adapterFunction;
4542
private final CountWhereBuilder whereBuilder = new CountWhereBuilder();
@@ -51,20 +48,11 @@ private CountDSL(BasicColumn countColumn, SqlTable table, Function<SelectModel,
5148
this.adapterFunction = Objects.requireNonNull(adapterFunction);
5249
}
5350

51+
@Override
5452
public CountWhereBuilder where() {
5553
return whereBuilder;
5654
}
5755

58-
public <T> CountWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition,
59-
SqlCriterion<?>...subCriteria) {
60-
whereBuilder.where(column, condition, subCriteria);
61-
return whereBuilder;
62-
}
63-
64-
public CountWhereBuilder applyWhere(WhereApplier whereApplier) {
65-
return whereBuilder.applyWhere(whereApplier);
66-
}
67-
6856
@NotNull
6957
@Override
7058
public R build() {
@@ -143,9 +131,8 @@ protected CountWhereBuilder getThis() {
143131
return this;
144132
}
145133

146-
@Override
147134
protected WhereModel buildWhereModel() {
148-
return super.internalBuild();
135+
return internalBuild();
149136
}
150137
}
151138
}

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

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,20 @@
2323

2424
import org.jetbrains.annotations.NotNull;
2525
import org.mybatis.dynamic.sql.BasicColumn;
26-
import org.mybatis.dynamic.sql.BindableColumn;
2726
import org.mybatis.dynamic.sql.SortSpecification;
28-
import org.mybatis.dynamic.sql.SqlCriterion;
2927
import org.mybatis.dynamic.sql.SqlTable;
3028
import org.mybatis.dynamic.sql.TableExpression;
31-
import org.mybatis.dynamic.sql.VisitableCondition;
3229
import org.mybatis.dynamic.sql.select.join.JoinCondition;
3330
import org.mybatis.dynamic.sql.select.join.JoinCriterion;
3431
import org.mybatis.dynamic.sql.select.join.JoinSpecification;
3532
import org.mybatis.dynamic.sql.select.join.JoinType;
3633
import org.mybatis.dynamic.sql.util.Buildable;
3734
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
38-
import org.mybatis.dynamic.sql.where.WhereApplier;
35+
import org.mybatis.dynamic.sql.where.AbstractWhereSupport;
3936
import org.mybatis.dynamic.sql.where.WhereModel;
4037

41-
public class QueryExpressionDSL<R> extends AbstractQueryExpressionDSL<QueryExpressionDSL<R>, R>
38+
public class QueryExpressionDSL<R>
39+
extends AbstractQueryExpressionDSL<QueryExpressionDSL<R>.QueryExpressionWhereBuilder, QueryExpressionDSL<R>, R>
4240
implements Buildable<R> {
4341

4442
private final String connector;
@@ -61,20 +59,11 @@ public class QueryExpressionDSL<R> extends AbstractQueryExpressionDSL<QueryExpre
6159
tableAliases.put(table, tableAlias);
6260
}
6361

62+
@Override
6463
public QueryExpressionWhereBuilder where() {
6564
return whereBuilder;
6665
}
6766

68-
public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition,
69-
SqlCriterion<?>...subCriteria) {
70-
whereBuilder.where(column, condition, subCriteria);
71-
return whereBuilder;
72-
}
73-
74-
public QueryExpressionWhereBuilder applyWhere(WhereApplier whereApplier) {
75-
return whereBuilder.applyWhere(whereApplier);
76-
}
77-
7867
@NotNull
7968
@Override
8069
public R build() {
@@ -293,9 +282,8 @@ protected QueryExpressionWhereBuilder getThis() {
293282
return this;
294283
}
295284

296-
@Override
297285
protected WhereModel buildWhereModel() {
298-
return super.internalBuild();
286+
return internalBuild();
299287
}
300288
}
301289

@@ -318,7 +306,8 @@ public JoinSpecificationFinisher on(BasicColumn joinColumn, JoinCondition onJoin
318306
}
319307
}
320308

321-
public class JoinSpecificationFinisher implements Buildable<R> {
309+
public class JoinSpecificationFinisher extends AbstractWhereSupport<QueryExpressionWhereBuilder>
310+
implements Buildable<R> {
322311
private final JoinSpecification.Builder joinSpecificationBuilder;
323312

324313
public JoinSpecificationFinisher(TableExpression table, BasicColumn joinColumn,
@@ -358,19 +347,11 @@ public R build() {
358347
return QueryExpressionDSL.this.build();
359348
}
360349

350+
@Override
361351
public QueryExpressionWhereBuilder where() {
362352
return QueryExpressionDSL.this.where();
363353
}
364354

365-
public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition,
366-
SqlCriterion<?>...subCriteria) {
367-
return QueryExpressionDSL.this.where(column, condition, subCriteria);
368-
}
369-
370-
public QueryExpressionWhereBuilder applyWhere(WhereApplier whereApplier) {
371-
return QueryExpressionDSL.this.applyWhere(whereApplier);
372-
}
373-
374355
public JoinSpecificationFinisher and(BasicColumn joinColumn, JoinCondition joinCondition) {
375356
JoinCriterion joinCriterion = new JoinCriterion.Builder()
376357
.withConnector("and") //$NON-NLS-1$

src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@
2323

2424
import org.jetbrains.annotations.NotNull;
2525
import org.mybatis.dynamic.sql.BasicColumn;
26-
import org.mybatis.dynamic.sql.BindableColumn;
2726
import org.mybatis.dynamic.sql.SqlColumn;
28-
import org.mybatis.dynamic.sql.SqlCriterion;
2927
import org.mybatis.dynamic.sql.SqlTable;
30-
import org.mybatis.dynamic.sql.VisitableCondition;
3128
import org.mybatis.dynamic.sql.select.SelectModel;
3229
import org.mybatis.dynamic.sql.util.AbstractColumnMapping;
3330
import org.mybatis.dynamic.sql.util.Buildable;
@@ -39,10 +36,10 @@
3936
import org.mybatis.dynamic.sql.util.ValueMapping;
4037
import org.mybatis.dynamic.sql.util.ValueWhenPresentMapping;
4138
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
42-
import org.mybatis.dynamic.sql.where.WhereApplier;
39+
import org.mybatis.dynamic.sql.where.AbstractWhereSupport;
4340
import org.mybatis.dynamic.sql.where.WhereModel;
4441

45-
public class UpdateDSL<R> implements Buildable<R> {
42+
public class UpdateDSL<R> extends AbstractWhereSupport<UpdateDSL<R>.UpdateWhereBuilder> implements Buildable<R> {
4643

4744
private final Function<UpdateModel, R> adapterFunction;
4845
private final List<AbstractColumnMapping> columnMappings = new ArrayList<>();
@@ -58,20 +55,11 @@ public <T> SetClauseFinisher<T> set(SqlColumn<T> column) {
5855
return new SetClauseFinisher<>(column);
5956
}
6057

58+
@Override
6159
public UpdateWhereBuilder where() {
6260
return whereBuilder;
6361
}
6462

65-
public <T> UpdateWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition,
66-
SqlCriterion<?>...subCriteria) {
67-
whereBuilder.where(column, condition, subCriteria);
68-
return whereBuilder;
69-
}
70-
71-
public UpdateWhereBuilder applyWhere(WhereApplier whereApplier) {
72-
return whereBuilder.applyWhere(whereApplier);
73-
}
74-
7563
/**
7664
* WARNING! Calling this method could result in an update statement that updates
7765
* all rows in a table.
@@ -165,9 +153,8 @@ protected UpdateWhereBuilder getThis() {
165153
return this;
166154
}
167155

168-
@Override
169156
protected WhereModel buildWhereModel() {
170-
return super.internalBuild();
157+
return internalBuild();
171158
}
172159
}
173160
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,5 @@ protected WhereModel internalBuild() {
129129
return WhereModel.of(criteria);
130130
}
131131

132-
protected abstract WhereModel buildWhereModel();
133-
134132
protected abstract T getThis();
135133
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2016-2020 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+
* http://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.where;
17+
18+
import java.util.Arrays;
19+
import java.util.List;
20+
import java.util.function.Consumer;
21+
22+
import org.mybatis.dynamic.sql.BindableColumn;
23+
import org.mybatis.dynamic.sql.SqlCriterion;
24+
import org.mybatis.dynamic.sql.VisitableCondition;
25+
26+
/**
27+
* Base class for DSLs that support where clauses - which is every DSL except Insert.
28+
* The purpose of the class is to provide an implementation of the {@link AbstractWhereDSL}
29+
* that is customized for a particular DSL, and to add the initiating common "where"
30+
* methods.
31+
*
32+
* @param <W> the implementation of the Where DSL customized for a particular SQL statement.
33+
*/
34+
public abstract class AbstractWhereSupport<W extends AbstractWhereDSL<?>> {
35+
36+
public abstract W where();
37+
38+
public <T> W where(BindableColumn<T> column, VisitableCondition<T> condition, SqlCriterion<?>...subCriteria) {
39+
return where(column, condition, Arrays.asList(subCriteria));
40+
}
41+
42+
public <T> W where(BindableColumn<T> column, VisitableCondition<T> condition, List<SqlCriterion<?>> subCriteria) {
43+
return apply(w -> w.where(column, condition, subCriteria));
44+
}
45+
46+
public W applyWhere(WhereApplier whereApplier) {
47+
return apply(w -> w.applyWhere(whereApplier));
48+
}
49+
50+
private W apply(Consumer<W> block) {
51+
W dsl = where();
52+
block.accept(dsl);
53+
return dsl;
54+
}
55+
}

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

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,7 @@ public static WhereDSL where() {
3030
return new WhereDSL();
3131
}
3232

33-
@Override
34-
protected WhereModel buildWhereModel() {
35-
return super.internalBuild();
36-
}
37-
3833
public WhereModel build() {
39-
return buildWhereModel();
34+
return internalBuild();
4035
}
4136
}

0 commit comments

Comments
 (0)