Skip to content

Commit bb76e8c

Browse files
committed
Remove a difficult to use inner class
This was making the code generated by MyBatis generator look terribly strange, and was also breaking the Java type parsing in MyBatis Generator.
1 parent caeaa7d commit bb76e8c

File tree

7 files changed

+160
-151
lines changed

7 files changed

+160
-151
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import org.mybatis.dynamic.sql.insert.BatchInsertDSL;
2424
import org.mybatis.dynamic.sql.insert.InsertDSL;
2525
import org.mybatis.dynamic.sql.insert.InsertSelectDSL;
26-
import org.mybatis.dynamic.sql.select.QueryExpressionDSL;
26+
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
2727
import org.mybatis.dynamic.sql.select.SelectDSL;
2828
import org.mybatis.dynamic.sql.select.SelectModel;
2929
import org.mybatis.dynamic.sql.select.SimpleSortSpecification;
@@ -94,11 +94,11 @@ static InsertSelectDSL.InsertColumnGatherer insertInto(SqlTable table) {
9494
return InsertSelectDSL.insertInto(table);
9595
}
9696

97-
static QueryExpressionDSL<SelectModel> select(BasicColumn...selectList) {
97+
static FromGatherer<SelectModel> select(BasicColumn...selectList) {
9898
return SelectDSL.select(selectList);
9999
}
100100

101-
static QueryExpressionDSL<SelectModel> selectDistinct(BasicColumn...selectList) {
101+
static FromGatherer<SelectModel> selectDistinct(BasicColumn...selectList) {
102102
return SelectDSL.selectDistinct(selectList);
103103
}
104104

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

Lines changed: 129 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737
import org.mybatis.dynamic.sql.where.AbstractWhereDSL;
3838
import org.mybatis.dynamic.sql.where.WhereModel;
3939

40-
public class QueryExpressionDSL<R> {
40+
public class QueryExpressionDSL<R> implements Buildable<R> {
4141

4242
private String connector;
43-
private SelectDSL<R> selectModelBuilder;
43+
private SelectDSL<R> selectDSL;
4444
private boolean isDistinct;
4545
private List<BasicColumn> selectList;
4646
private SqlTable table;
@@ -50,55 +50,83 @@ public class QueryExpressionDSL<R> {
5050
private JoinModel joinModel;
5151
private List<JoinSpecification> joinSpecifications = new ArrayList<>();
5252

53-
private QueryExpressionDSL(Builder<R> builder) {
54-
connector = builder.connector;
55-
selectList = Arrays.asList(builder.selectList);
56-
isDistinct = builder.isDistinct;
57-
selectModelBuilder = Objects.requireNonNull(builder.selectModelBuilder);
53+
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);
58+
table = Objects.requireNonNull(fromGatherer.table);
59+
tableAliases.putAll(fromGatherer.tableAliasMap);
5860
}
5961

60-
public QueryExpressionAfterFrom from(SqlTable table) {
61-
this.table = table;
62-
return new QueryExpressionAfterFrom();
62+
public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition) {
63+
return new QueryExpressionWhereBuilder(column, condition);
6364
}
6465

65-
public QueryExpressionAfterFrom from(SqlTable table, String tableAlias) {
66-
this.table = table;
67-
tableAliases.put(table, tableAlias);
68-
return new QueryExpressionAfterFrom();
66+
public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition,
67+
SqlCriterion<?>...subCriteria) {
68+
return new QueryExpressionWhereBuilder(column, condition, subCriteria);
69+
}
70+
71+
@Override
72+
public R build() {
73+
selectDSL.addQueryExpression(buildModel());
74+
return selectDSL.build();
6975
}
7076

71-
public static class Builder<R> {
72-
private String connector;
73-
private BasicColumn[] selectList;
74-
private boolean isDistinct;
75-
private SelectDSL<R> selectModelBuilder;
77+
public JoinSpecificationStarter join(SqlTable joinTable) {
78+
return new JoinSpecificationStarter(joinTable, JoinType.INNER);
79+
}
80+
81+
public JoinSpecificationStarter join(SqlTable joinTable, String tableAlias) {
82+
tableAliases.put(joinTable, tableAlias);
83+
return join(joinTable);
84+
}
7685

77-
public Builder<R> withConnector(String connector) {
78-
this.connector = connector;
79-
return this;
80-
}
81-
82-
public Builder<R> withSelectList(BasicColumn...selectList) {
83-
this.selectList = selectList;
84-
return this;
85-
}
86-
87-
public Builder<R> isDistinct() {
88-
this.isDistinct = true;
89-
return this;
90-
}
91-
92-
public Builder<R> withSelectModelBuilder(SelectDSL<R> selectModelBuilder) {
93-
this.selectModelBuilder = selectModelBuilder;
94-
return this;
95-
}
96-
97-
public QueryExpressionDSL<R> build() {
98-
return new QueryExpressionDSL<>(this);
99-
}
86+
public JoinSpecificationStarter leftJoin(SqlTable joinTable) {
87+
return new JoinSpecificationStarter(joinTable, JoinType.LEFT);
88+
}
89+
90+
public JoinSpecificationStarter leftJoin(SqlTable joinTable, String tableAlias) {
91+
tableAliases.put(joinTable, tableAlias);
92+
return leftJoin(joinTable);
93+
}
94+
95+
public JoinSpecificationStarter rightJoin(SqlTable joinTable) {
96+
return new JoinSpecificationStarter(joinTable, JoinType.RIGHT);
97+
}
98+
99+
public JoinSpecificationStarter rightJoin(SqlTable joinTable, String tableAlias) {
100+
tableAliases.put(joinTable, tableAlias);
101+
return rightJoin(joinTable);
102+
}
103+
104+
public JoinSpecificationStarter fullJoin(SqlTable joinTable) {
105+
return new JoinSpecificationStarter(joinTable, JoinType.FULL);
100106
}
101107

108+
public JoinSpecificationStarter fullJoin(SqlTable joinTable, String tableAlias) {
109+
tableAliases.put(joinTable, tableAlias);
110+
return fullJoin(joinTable);
111+
}
112+
113+
public GroupByFinisher groupBy(BasicColumn...columns) {
114+
groupByModel = GroupByModel.of(columns);
115+
selectDSL.addQueryExpression(buildModel());
116+
return new GroupByFinisher();
117+
}
118+
119+
public SelectDSL<R> orderBy(SortSpecification...columns) {
120+
selectDSL.addQueryExpression(buildModel());
121+
selectDSL.setOrderByModel(OrderByModel.of(columns));
122+
return selectDSL;
123+
}
124+
125+
public UnionBuilder union() {
126+
selectDSL.addQueryExpression(buildModel());
127+
return new UnionBuilder();
128+
}
129+
102130
protected QueryExpressionModel buildModel() {
103131
return new QueryExpressionModel.Builder()
104132
.withConnector(connector)
@@ -112,77 +140,56 @@ protected QueryExpressionModel buildModel() {
112140
.build();
113141
}
114142

115-
public class QueryExpressionAfterFrom implements Buildable<R> {
116-
private QueryExpressionAfterFrom() {
117-
super();
118-
}
143+
public static class FromGatherer<R> {
144+
private FromGathererBuilder<R> builder;
145+
private Map<SqlTable, String> tableAliasMap = new HashMap<>();
146+
private SqlTable table;
119147

120-
public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition) {
121-
return new QueryExpressionWhereBuilder(column, condition);
122-
}
123-
124-
public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition,
125-
SqlCriterion<?>...subCriteria) {
126-
return new QueryExpressionWhereBuilder(column, condition, subCriteria);
148+
public FromGatherer(FromGathererBuilder<R> builder) {
149+
this.builder = builder;
127150
}
128151

129-
@Override
130-
public R build() {
131-
selectModelBuilder.addQueryExpression(buildModel());
132-
return selectModelBuilder.build();
133-
}
134-
135-
public JoinSpecificationStarter join(SqlTable joinTable) {
136-
return new JoinSpecificationStarter(joinTable, JoinType.INNER);
137-
}
138-
139-
public JoinSpecificationStarter join(SqlTable joinTable, String tableAlias) {
140-
tableAliases.put(joinTable, tableAlias);
141-
return join(joinTable);
152+
public QueryExpressionDSL<R> from(SqlTable table) {
153+
this.table = table;
154+
155+
return new QueryExpressionDSL<>(this);
142156
}
143157

144-
public JoinSpecificationStarter leftJoin(SqlTable joinTable) {
145-
return new JoinSpecificationStarter(joinTable, JoinType.LEFT);
158+
public QueryExpressionDSL<R> from(SqlTable table, String tableAlias) {
159+
this.table = table;
160+
tableAliasMap.put(table, tableAlias);
161+
return new QueryExpressionDSL<>(this);
146162
}
163+
}
164+
165+
public static class FromGathererBuilder<R> {
166+
private String connector;
167+
private BasicColumn[] selectList;
168+
private SelectDSL<R> selectDSL;
169+
private boolean isDistinct;
147170

148-
public JoinSpecificationStarter leftJoin(SqlTable joinTable, String tableAlias) {
149-
tableAliases.put(joinTable, tableAlias);
150-
return leftJoin(joinTable);
171+
public FromGathererBuilder<R> withConnector(String connector) {
172+
this.connector = connector;
173+
return this;
151174
}
152175

153-
public JoinSpecificationStarter rightJoin(SqlTable joinTable) {
154-
return new JoinSpecificationStarter(joinTable, JoinType.RIGHT);
155-
}
156-
157-
public JoinSpecificationStarter rightJoin(SqlTable joinTable, String tableAlias) {
158-
tableAliases.put(joinTable, tableAlias);
159-
return rightJoin(joinTable);
176+
public FromGathererBuilder<R> withSelectList(BasicColumn[] selectList) {
177+
this.selectList = selectList;
178+
return this;
160179
}
161180

162-
public JoinSpecificationStarter fullJoin(SqlTable joinTable) {
163-
return new JoinSpecificationStarter(joinTable, JoinType.FULL);
181+
public FromGathererBuilder<R> withSelectDSL(SelectDSL<R> selectDSL) {
182+
this.selectDSL = selectDSL;
183+
return this;
164184
}
165185

166-
public JoinSpecificationStarter fullJoin(SqlTable joinTable, String tableAlias) {
167-
tableAliases.put(joinTable, tableAlias);
168-
return fullJoin(joinTable);
169-
}
170-
171-
public GroupByFinisher groupBy(BasicColumn...columns) {
172-
groupByModel = GroupByModel.of(columns);
173-
selectModelBuilder.addQueryExpression(buildModel());
174-
return new GroupByFinisher();
186+
public FromGathererBuilder<R> isDistinct() {
187+
this.isDistinct = true;
188+
return this;
175189
}
176190

177-
public SelectDSL<R> orderBy(SortSpecification...columns) {
178-
selectModelBuilder.addQueryExpression(buildModel());
179-
selectModelBuilder.setOrderByModel(OrderByModel.of(columns));
180-
return selectModelBuilder;
181-
}
182-
183-
public UnionBuilder union() {
184-
selectModelBuilder.addQueryExpression(buildModel());
185-
return new UnionBuilder();
191+
public FromGatherer<R> build() {
192+
return new FromGatherer<>(this);
186193
}
187194
}
188195

@@ -199,22 +206,22 @@ private <T> QueryExpressionWhereBuilder(BindableColumn<T> column, VisitableCondi
199206

200207
public UnionBuilder union() {
201208
whereModel = buildWhereModel();
202-
selectModelBuilder.addQueryExpression(buildModel());
209+
selectDSL.addQueryExpression(buildModel());
203210
return new UnionBuilder();
204211
}
205212

206213
public SelectDSL<R> orderBy(SortSpecification...columns) {
207214
whereModel = buildWhereModel();
208-
selectModelBuilder.addQueryExpression(buildModel());
209-
selectModelBuilder.setOrderByModel(OrderByModel.of(columns));
210-
return selectModelBuilder;
215+
selectDSL.addQueryExpression(buildModel());
216+
selectDSL.setOrderByModel(OrderByModel.of(columns));
217+
return selectDSL;
211218
}
212219

213220
@Override
214221
public R build() {
215222
whereModel = buildWhereModel();
216-
selectModelBuilder.addQueryExpression(buildModel());
217-
return selectModelBuilder.build();
223+
selectDSL.addQueryExpression(buildModel());
224+
return selectDSL.build();
218225
}
219226

220227
@Override
@@ -293,8 +300,8 @@ protected JoinModel buildJoinModel() {
293300
@Override
294301
public R build() {
295302
joinModel = buildJoinModel();
296-
selectModelBuilder.addQueryExpression(buildModel());
297-
return selectModelBuilder.build();
303+
selectDSL.addQueryExpression(buildModel());
304+
return selectDSL.build();
298305
}
299306

300307
public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition) {
@@ -360,39 +367,39 @@ public JoinSpecificationStarter fullJoin(SqlTable joinTable, String tableAlias)
360367

361368
public SelectDSL<R> orderBy(SortSpecification...columns) {
362369
joinModel = buildJoinModel();
363-
selectModelBuilder.addQueryExpression(buildModel());
364-
selectModelBuilder.setOrderByModel(OrderByModel.of(columns));
365-
return selectModelBuilder;
370+
selectDSL.addQueryExpression(buildModel());
371+
selectDSL.setOrderByModel(OrderByModel.of(columns));
372+
return selectDSL;
366373
}
367374
}
368375

369376
public class GroupByFinisher implements Buildable<R> {
370377
public SelectDSL<R> orderBy(SortSpecification...columns) {
371-
selectModelBuilder.setOrderByModel(OrderByModel.of(columns));
372-
return selectModelBuilder;
378+
selectDSL.setOrderByModel(OrderByModel.of(columns));
379+
return selectDSL;
373380
}
374381

375382
@Override
376383
public R build() {
377-
return selectModelBuilder.build();
384+
return selectDSL.build();
378385
}
379386
}
380387

381388
public class UnionBuilder {
382-
public QueryExpressionDSL<R> select(BasicColumn...selectList) {
383-
return new QueryExpressionDSL.Builder<R>()
384-
.withConnector("union") //$NON-NLS-1$
389+
public FromGatherer<R> select(BasicColumn...selectList) {
390+
return new FromGathererBuilder<R>()
391+
.withConnector("union")
385392
.withSelectList(selectList)
386-
.withSelectModelBuilder(selectModelBuilder)
393+
.withSelectDSL(selectDSL)
387394
.build();
388395
}
389-
390-
public QueryExpressionDSL<R> selectDistinct(BasicColumn...selectList) {
391-
return new QueryExpressionDSL.Builder<R>()
392-
.withConnector("union") //$NON-NLS-1$
393-
.withSelectList(selectList)
396+
397+
public FromGatherer<R> selectDistinct(BasicColumn...selectList) {
398+
return new FromGathererBuilder<R>()
399+
.withConnector("union")
394400
.isDistinct()
395-
.withSelectModelBuilder(selectModelBuilder)
401+
.withSelectList(selectList)
402+
.withSelectDSL(selectDSL)
396403
.build();
397404
}
398405
}

0 commit comments

Comments
 (0)