Skip to content

Commit f779d19

Browse files
committed
Union support step 1
Refactor to move a query expression out of the select model. Select becomes a query expression plus an order by phrase.
1 parent 765c90c commit f779d19

13 files changed

+420
-243
lines changed

src/main/java/org/mybatis/dynamic/sql/AbstractSqlSupport.java

-8
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,8 @@
1616
package org.mybatis.dynamic.sql;
1717

1818
import java.util.Objects;
19-
import java.util.Optional;
2019

2120
public abstract class AbstractSqlSupport {
22-
public static final String EMPTY_STRING = ""; //$NON-NLS-1$
23-
public static final String ONE_SPACE = " "; //$NON-NLS-1$
24-
2521
private String tableName;
2622

2723
public AbstractSqlSupport(String tableName) {
@@ -31,8 +27,4 @@ public AbstractSqlSupport(String tableName) {
3127
public String tableName() {
3228
return tableName;
3329
}
34-
35-
protected String spaceBefore(Optional<String> in) {
36-
return in.map(s -> ONE_SPACE + s).orElse(EMPTY_STRING);
37-
}
3830
}

src/main/java/org/mybatis/dynamic/sql/delete/render/DeleteSupport.java

+3-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Optional;
2222

2323
import org.mybatis.dynamic.sql.AbstractSqlSupport;
24+
import org.mybatis.dynamic.sql.util.StringUtilities;
2425

2526
public class DeleteSupport extends AbstractSqlSupport {
2627

@@ -34,11 +35,7 @@ private DeleteSupport(Builder builder) {
3435
}
3536

3637
public String getWhereClause() {
37-
return whereClause.orElse(EMPTY_STRING);
38-
}
39-
40-
public Optional<String> whereClause() {
41-
return whereClause;
38+
return whereClause.orElse(""); //$NON-NLS-1$
4239
}
4340

4441
public Map<String, Object> getParameters() {
@@ -48,7 +45,7 @@ public Map<String, Object> getParameters() {
4845
public String getFullDeleteStatement() {
4946
return "delete from " //$NON-NLS-1$
5047
+ tableName()
51-
+ whereClause().map(w -> ONE_SPACE + w).orElse(EMPTY_STRING);
48+
+ StringUtilities.spaceBefore(whereClause);
5249
}
5350

5451
public static class Builder {

src/main/java/org/mybatis/dynamic/sql/insert/render/InsertSupport.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ public T getRecord() {
4747
public String getFullInsertStatement() {
4848
return "insert into " //$NON-NLS-1$
4949
+ tableName()
50-
+ ONE_SPACE
51-
+ getColumnsPhrase()
52-
+ ONE_SPACE
53-
+ getValuesPhrase();
50+
+ " " //$NON-NLS-1$
51+
+ columnsPhrase
52+
+ " " //$NON-NLS-1$
53+
+ valuesPhrase;
5454
}
5555

5656
public static class Builder<T> {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
* Copyright 2016-2017 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.select;
17+
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
import java.util.Map;
22+
import java.util.Objects;
23+
import java.util.Optional;
24+
import java.util.function.Function;
25+
import java.util.stream.Stream;
26+
27+
import org.mybatis.dynamic.sql.SelectListItem;
28+
import org.mybatis.dynamic.sql.SqlTable;
29+
import org.mybatis.dynamic.sql.render.GuaranteedTableAliasCalculator;
30+
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
31+
import org.mybatis.dynamic.sql.select.join.JoinModel;
32+
import org.mybatis.dynamic.sql.where.WhereModel;
33+
34+
public class QueryExpression {
35+
private boolean isDistinct;
36+
private List<SelectListItem> selectList;
37+
private SqlTable table;
38+
private Optional<JoinModel> joinModel;
39+
private TableAliasCalculator tableAliasCalculator;
40+
private Optional<WhereModel> whereModel;
41+
42+
private QueryExpression(Builder builder) {
43+
isDistinct = builder.isDistinct;
44+
selectList = Objects.requireNonNull(builder.selectList);
45+
table = Objects.requireNonNull(builder.table);
46+
joinModel = Optional.ofNullable(builder.joinModel);
47+
tableAliasCalculator = joinModel.map(jm -> GuaranteedTableAliasCalculator.of(builder.tableAliases))
48+
.orElse(TableAliasCalculator.of(builder.tableAliases));
49+
whereModel = Optional.ofNullable(builder.whereModel);
50+
}
51+
52+
public boolean isDistinct() {
53+
return isDistinct;
54+
}
55+
56+
public <R> Stream<R> mapColumns(Function<SelectListItem, R> mapper) {
57+
return selectList.stream().map(mapper);
58+
}
59+
60+
public SqlTable table() {
61+
return table;
62+
}
63+
64+
public TableAliasCalculator tableAliasCalculator() {
65+
return tableAliasCalculator;
66+
}
67+
68+
public Optional<WhereModel> whereModel() {
69+
return whereModel;
70+
}
71+
72+
public Optional<JoinModel> joinModel() {
73+
return joinModel;
74+
}
75+
76+
public String calculateTableNameIncludingAlias(SqlTable table) {
77+
return tableAliasCalculator.aliasForTable(table)
78+
.map(a -> table.name() + " " + a) //$NON-NLS-1$
79+
.orElseGet(table::name);
80+
}
81+
82+
public static class Builder {
83+
private boolean isDistinct;
84+
private List<SelectListItem> selectList = new ArrayList<>();
85+
private SqlTable table;
86+
private Map<SqlTable, String> tableAliases = new HashMap<>();
87+
private WhereModel whereModel;
88+
private JoinModel joinModel;
89+
90+
public Builder withTable(SqlTable table) {
91+
this.table = table;
92+
return this;
93+
}
94+
95+
public Builder isDistinct(boolean isDistinct) {
96+
this.isDistinct = isDistinct;
97+
return this;
98+
}
99+
100+
public Builder withColumns(List<SelectListItem> selectList) {
101+
this.selectList.addAll(selectList);
102+
return this;
103+
}
104+
105+
public Builder withTableAliases(Map<SqlTable, String> tableAliases) {
106+
this.tableAliases.putAll(tableAliases);
107+
return this;
108+
}
109+
110+
public Builder withWhereModel(WhereModel whereModel) {
111+
this.whereModel = whereModel;
112+
return this;
113+
}
114+
115+
public Builder withJoinModel(JoinModel joinModel) {
116+
this.joinModel = joinModel;
117+
return this;
118+
}
119+
120+
public QueryExpression build() {
121+
return new QueryExpression(this);
122+
}
123+
}
124+
}

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

+7-85
Original file line numberDiff line numberDiff line change
@@ -15,67 +15,24 @@
1515
*/
1616
package org.mybatis.dynamic.sql.select;
1717

18-
import java.util.ArrayList;
19-
import java.util.HashMap;
20-
import java.util.List;
21-
import java.util.Map;
2218
import java.util.Objects;
2319
import java.util.Optional;
24-
import java.util.function.Function;
25-
import java.util.stream.Stream;
2620

27-
import org.mybatis.dynamic.sql.SelectListItem;
28-
import org.mybatis.dynamic.sql.SqlTable;
29-
import org.mybatis.dynamic.sql.render.GuaranteedTableAliasCalculator;
3021
import org.mybatis.dynamic.sql.render.RenderingStrategy;
31-
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
32-
import org.mybatis.dynamic.sql.select.join.JoinModel;
3322
import org.mybatis.dynamic.sql.select.render.SelectRenderer;
3423
import org.mybatis.dynamic.sql.select.render.SelectSupport;
35-
import org.mybatis.dynamic.sql.where.WhereModel;
3624

3725
public class SelectModel {
38-
private boolean isDistinct;
39-
private List<SelectListItem> selectList;
40-
private SqlTable table;
41-
private Optional<JoinModel> joinModel;
42-
private TableAliasCalculator tableAliasCalculator;
43-
private Optional<WhereModel> whereModel;
26+
private QueryExpression queryExpression;
4427
private Optional<OrderByModel> orderByModel;
4528

4629
private SelectModel(Builder builder) {
47-
isDistinct = builder.isDistinct;
48-
selectList = Objects.requireNonNull(builder.selectList);
49-
table = Objects.requireNonNull(builder.table);
50-
joinModel = Optional.ofNullable(builder.joinModel);
51-
tableAliasCalculator = joinModel.map(jm -> GuaranteedTableAliasCalculator.of(builder.tableAliases))
52-
.orElse(TableAliasCalculator.of(builder.tableAliases));
53-
whereModel = Optional.ofNullable(builder.whereModel);
30+
queryExpression = Objects.requireNonNull(builder.queryExpression);
5431
orderByModel = Optional.ofNullable(builder.orderByModel);
5532
}
5633

57-
public boolean isDistinct() {
58-
return isDistinct;
59-
}
60-
61-
public <R> Stream<R> mapColumns(Function<SelectListItem, R> mapper) {
62-
return selectList.stream().map(mapper);
63-
}
64-
65-
public SqlTable table() {
66-
return table;
67-
}
68-
69-
public TableAliasCalculator tableAliasCalculator() {
70-
return tableAliasCalculator;
71-
}
72-
73-
public Optional<WhereModel> whereModel() {
74-
return whereModel;
75-
}
76-
77-
public Optional<JoinModel> joinModel() {
78-
return joinModel;
34+
public QueryExpression queryExpression() {
35+
return queryExpression;
7936
}
8037

8138
public Optional<OrderByModel> orderByModel() {
@@ -86,55 +43,20 @@ public SelectSupport render(RenderingStrategy renderingStrategy) {
8643
return SelectRenderer.of(this).render(renderingStrategy);
8744
}
8845

89-
public String calculateTableNameIncludingAlias(SqlTable table) {
90-
return tableAliasCalculator.aliasForTable(table)
91-
.map(a -> table.name() + " " + a) //$NON-NLS-1$
92-
.orElseGet(table::name);
93-
}
94-
9546
public static class Builder {
96-
private boolean isDistinct;
97-
private List<SelectListItem> selectList = new ArrayList<>();
98-
private SqlTable table;
99-
private Map<SqlTable, String> tableAliases = new HashMap<>();
100-
private WhereModel whereModel;
47+
private QueryExpression queryExpression;
10148
private OrderByModel orderByModel;
102-
private JoinModel joinModel;
103-
104-
public Builder(SqlTable table) {
105-
this.table = table;
106-
}
10749

108-
public Builder isDistinct(boolean isDistinct) {
109-
this.isDistinct = isDistinct;
110-
return this;
111-
}
112-
113-
public Builder withColumns(List<SelectListItem> selectList) {
114-
this.selectList.addAll(selectList);
115-
return this;
116-
}
117-
118-
public Builder withTableAliases(Map<SqlTable, String> tableAliases) {
119-
this.tableAliases.putAll(tableAliases);
50+
public Builder withQueryExpression(QueryExpression queryExpression) {
51+
this.queryExpression = queryExpression;
12052
return this;
12153
}
12254

123-
public Builder withWhereModel(WhereModel whereModel) {
124-
this.whereModel = whereModel;
125-
return this;
126-
}
127-
12855
public Builder withOrderByModel(OrderByModel orderByModel) {
12956
this.orderByModel = orderByModel;
13057
return this;
13158
}
13259

133-
public Builder withJoinModel(JoinModel joinModel) {
134-
this.joinModel = joinModel;
135-
return this;
136-
}
137-
13860
public SelectModel build() {
13961
return new SelectModel(this);
14062
}

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

+7-2
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,19 @@ public static SelectModelBuilder ofDistinct(SelectListItem...selectList) {
7171
}
7272

7373
protected SelectModel buildModel() {
74-
return new SelectModel.Builder(table)
74+
QueryExpression queryExpression = new QueryExpression.Builder()
75+
.withTable(table)
7576
.isDistinct(isDistinct)
7677
.withColumns(selectList)
7778
.withTableAliases(tableAliases)
7879
.withWhereModel(whereModel)
79-
.withOrderByModel(orderByModel)
8080
.withJoinModel(joinModel)
8181
.build();
82+
83+
return new SelectModel.Builder()
84+
.withQueryExpression(queryExpression)
85+
.withOrderByModel(orderByModel)
86+
.build();
8287
}
8388

8489
@FunctionalInterface

0 commit comments

Comments
 (0)