Skip to content

Commit 319396c

Browse files
authored
Merge pull request #104 from jeffgbutler/master
Refactor QueryExpressionDSL
2 parents d548fbc + e69d7e0 commit 319396c

File tree

2 files changed

+76
-71
lines changed

2 files changed

+76
-71
lines changed

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

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,31 @@ public class QueryExpressionDSL<R> implements Buildable<R> {
5151
private List<JoinSpecification> joinSpecifications = new ArrayList<>();
5252

5353
private QueryExpressionDSL(FromGatherer<R> fromGatherer) {
54-
connector = fromGatherer.builder.connector;
55-
selectList = Arrays.asList(fromGatherer.builder.selectList);
56-
isDistinct = fromGatherer.builder.isDistinct;
57-
selectDSL = Objects.requireNonNull(fromGatherer.builder.selectDSL);
54+
connector = fromGatherer.connector;
55+
selectList = Arrays.asList(fromGatherer.selectList);
56+
isDistinct = fromGatherer.isDistinct;
57+
selectDSL = Objects.requireNonNull(fromGatherer.selectDSL);
5858
table = Objects.requireNonNull(fromGatherer.table);
59-
tableAliases.putAll(fromGatherer.tableAliasMap);
59+
}
60+
61+
private QueryExpressionDSL(FromGatherer<R> fromGatherer, String tableAlias) {
62+
this(fromGatherer);
63+
tableAliases.put(table, tableAlias);
64+
}
65+
66+
public static <R> FromGatherer<R> select(SelectDSL<R> selectDSL, BasicColumn...selectList) {
67+
return new FromGatherer.Builder<R>()
68+
.withSelectList(selectList)
69+
.withSelectDSL(selectDSL)
70+
.build();
71+
}
72+
73+
public static <R> FromGatherer<R> selectDistinct(SelectDSL<R> selectDSL, BasicColumn...selectList) {
74+
return new FromGatherer.Builder<R>()
75+
.withSelectList(selectList)
76+
.withSelectDSL(selectDSL)
77+
.isDistinct()
78+
.build();
6079
}
6180

6281
public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition) {
@@ -160,55 +179,58 @@ public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
160179
}
161180

162181
public static class FromGatherer<R> {
163-
private FromGathererBuilder<R> builder;
164-
private Map<SqlTable, String> tableAliasMap = new HashMap<>();
182+
private String connector;
183+
private BasicColumn[] selectList;
184+
private SelectDSL<R> selectDSL;
185+
private boolean isDistinct;
165186
private SqlTable table;
166187

167-
public FromGatherer(FromGathererBuilder<R> builder) {
168-
this.builder = builder;
188+
public FromGatherer(Builder<R> builder) {
189+
this.connector = builder.connector;
190+
this.selectList = Objects.requireNonNull(builder.selectList);
191+
this.selectDSL = Objects.requireNonNull(builder.selectDSL);
192+
this.isDistinct = builder.isDistinct;
169193
}
170194

171195
public QueryExpressionDSL<R> from(SqlTable table) {
172196
this.table = table;
173-
174197
return new QueryExpressionDSL<>(this);
175198
}
176199

177200
public QueryExpressionDSL<R> from(SqlTable table, String tableAlias) {
178201
this.table = table;
179-
tableAliasMap.put(table, tableAlias);
180-
return new QueryExpressionDSL<>(this);
181-
}
182-
}
183-
184-
public static class FromGathererBuilder<R> {
185-
private String connector;
186-
private BasicColumn[] selectList;
187-
private SelectDSL<R> selectDSL;
188-
private boolean isDistinct;
189-
190-
public FromGathererBuilder<R> withConnector(String connector) {
191-
this.connector = connector;
192-
return this;
193-
}
194-
195-
public FromGathererBuilder<R> withSelectList(BasicColumn[] selectList) {
196-
this.selectList = selectList;
197-
return this;
198-
}
199-
200-
public FromGathererBuilder<R> withSelectDSL(SelectDSL<R> selectDSL) {
201-
this.selectDSL = selectDSL;
202-
return this;
202+
return new QueryExpressionDSL<>(this, tableAlias);
203203
}
204204

205-
public FromGathererBuilder<R> isDistinct() {
206-
this.isDistinct = true;
207-
return this;
208-
}
209-
210-
public FromGatherer<R> build() {
211-
return new FromGatherer<>(this);
205+
public static class Builder<R> {
206+
private String connector;
207+
private BasicColumn[] selectList;
208+
private SelectDSL<R> selectDSL;
209+
private boolean isDistinct;
210+
211+
public Builder<R> withConnector(String connector) {
212+
this.connector = connector;
213+
return this;
214+
}
215+
216+
public Builder<R> withSelectList(BasicColumn[] selectList) {
217+
this.selectList = selectList;
218+
return this;
219+
}
220+
221+
public Builder<R> withSelectDSL(SelectDSL<R> selectDSL) {
222+
this.selectDSL = selectDSL;
223+
return this;
224+
}
225+
226+
public Builder<R> isDistinct() {
227+
this.isDistinct = true;
228+
return this;
229+
}
230+
231+
public FromGatherer<R> build() {
232+
return new FromGatherer<>(this);
233+
}
212234
}
213235
}
214236

@@ -469,19 +491,19 @@ public UnionBuilder(String connector) {
469491
}
470492

471493
public FromGatherer<R> select(BasicColumn...selectList) {
472-
return new FromGathererBuilder<R>()
494+
return new FromGatherer.Builder<R>()
473495
.withConnector(connector)
474496
.withSelectList(selectList)
475497
.withSelectDSL(selectDSL)
476498
.build();
477499
}
478500

479501
public FromGatherer<R> selectDistinct(BasicColumn...selectList) {
480-
return new FromGathererBuilder<R>()
502+
return new FromGatherer.Builder<R>()
481503
.withConnector(connector)
482-
.isDistinct()
483504
.withSelectList(selectList)
484505
.withSelectDSL(selectDSL)
506+
.isDistinct()
485507
.build();
486508
}
487509
}

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

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import java.util.function.Function;
2222

2323
import org.mybatis.dynamic.sql.BasicColumn;
24-
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
25-
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGathererBuilder;
2624
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
2725
import org.mybatis.dynamic.sql.util.Buildable;
2826

@@ -44,47 +42,32 @@ private SelectDSL(Function<SelectModel, R> adapterFunction) {
4442
this.adapterFunction = Objects.requireNonNull(adapterFunction);
4543
}
4644

47-
private FromGatherer<R> queryExpressionBuilder(BasicColumn...selectList) {
48-
return new FromGathererBuilder<R>()
49-
.withSelectDSL(this)
50-
.withSelectList(selectList)
51-
.build();
52-
}
53-
54-
private FromGatherer<R> distinctQueryExpressionBuilder(BasicColumn...selectList) {
55-
return new FromGathererBuilder<R>()
56-
.withSelectDSL(this)
57-
.withSelectList(selectList)
58-
.isDistinct()
59-
.build();
60-
}
61-
62-
public static FromGatherer<SelectModel> select(BasicColumn...selectList) {
45+
public static QueryExpressionDSL.FromGatherer<SelectModel> select(BasicColumn...selectList) {
6346
return select(Function.identity(), selectList);
6447
}
6548

66-
public static <R> FromGatherer<R> select(Function<SelectModel, R> adapterFunction,
49+
public static <R> QueryExpressionDSL.FromGatherer<R> select(Function<SelectModel, R> adapterFunction,
6750
BasicColumn...selectList) {
68-
SelectDSL<R> selectModelBuilder = new SelectDSL<>(adapterFunction);
69-
return selectModelBuilder.queryExpressionBuilder(selectList);
51+
SelectDSL<R> selectDSL = new SelectDSL<>(adapterFunction);
52+
return QueryExpressionDSL.select(selectDSL, selectList);
7053
}
7154

72-
public static FromGatherer<SelectModel> selectDistinct(BasicColumn...selectList) {
55+
public static QueryExpressionDSL.FromGatherer<SelectModel> selectDistinct(BasicColumn...selectList) {
7356
return selectDistinct(Function.identity(), selectList);
7457
}
7558

76-
public static <R> FromGatherer<R> selectDistinct(Function<SelectModel, R> adapterFunction,
59+
public static <R> QueryExpressionDSL.FromGatherer<R> selectDistinct(Function<SelectModel, R> adapterFunction,
7760
BasicColumn...selectList) {
78-
SelectDSL<R> selectModelBuilder = new SelectDSL<>(adapterFunction);
79-
return selectModelBuilder.distinctQueryExpressionBuilder(selectList);
61+
SelectDSL<R> selectDSL = new SelectDSL<>(adapterFunction);
62+
return QueryExpressionDSL.selectDistinct(selectDSL, selectList);
8063
}
8164

82-
public static <T> FromGatherer<MyBatis3SelectModelAdapter<T>> selectWithMapper(
65+
public static <T> QueryExpressionDSL.FromGatherer<MyBatis3SelectModelAdapter<T>> selectWithMapper(
8366
Function<SelectStatementProvider, T> mapperMethod, BasicColumn...selectList) {
8467
return select(selectModel -> MyBatis3SelectModelAdapter.of(selectModel, mapperMethod), selectList);
8568
}
8669

87-
public static <T> FromGatherer<MyBatis3SelectModelAdapter<T>> selectDistinctWithMapper(
70+
public static <T> QueryExpressionDSL.FromGatherer<MyBatis3SelectModelAdapter<T>> selectDistinctWithMapper(
8871
Function<SelectStatementProvider, T> mapperMethod, BasicColumn...selectList) {
8972
return selectDistinct(selectModel -> MyBatis3SelectModelAdapter.of(selectModel, mapperMethod),
9073
selectList);

0 commit comments

Comments
 (0)