Skip to content

Commit 005fc68

Browse files
committed
Beginning of a fix for 100 and 102
1 parent a907072 commit 005fc68

File tree

2 files changed

+86
-18
lines changed

2 files changed

+86
-18
lines changed

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.List;
2222
import java.util.Map;
2323
import java.util.Objects;
24+
import java.util.function.Supplier;
2425

2526
import org.mybatis.dynamic.sql.BasicColumn;
2627
import org.mybatis.dynamic.sql.BindableColumn;
@@ -49,6 +50,7 @@ public class QueryExpressionDSL<R> implements Buildable<R> {
4950
private GroupByModel groupByModel;
5051
private JoinModel joinModel;
5152
private List<JoinSpecification> joinSpecifications = new ArrayList<>();
53+
private Supplier<R> buildDelegateMethod;
5254

5355
private QueryExpressionDSL(FromGatherer<R> fromGatherer) {
5456
connector = fromGatherer.builder.connector;
@@ -57,6 +59,7 @@ private QueryExpressionDSL(FromGatherer<R> fromGatherer) {
5759
selectDSL = Objects.requireNonNull(fromGatherer.builder.selectDSL);
5860
table = Objects.requireNonNull(fromGatherer.table);
5961
tableAliases.putAll(fromGatherer.tableAliasMap);
62+
buildDelegateMethod = this::internalBuild;
6063
}
6164

6265
public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, VisitableCondition<T> condition) {
@@ -70,6 +73,10 @@ public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, Visitable
7073

7174
@Override
7275
public R build() {
76+
return buildDelegateMethod.get();
77+
}
78+
79+
private R internalBuild() {
7380
selectDSL.addQueryExpression(buildModel());
7481
return selectDSL.build();
7582
}
@@ -117,6 +124,7 @@ public GroupByFinisher groupBy(BasicColumn...columns) {
117124
}
118125

119126
public SelectDSL<R> orderBy(SortSpecification...columns) {
127+
buildDelegateMethod = selectDSL::build;
120128
selectDSL.addQueryExpression(buildModel());
121129
selectDSL.setOrderByModel(OrderByModel.of(columns));
122130
return selectDSL;
@@ -145,16 +153,19 @@ protected QueryExpressionModel buildModel() {
145153
}
146154

147155
public SelectDSL<R>.LimitFinisher limit(long limit) {
156+
buildDelegateMethod = selectDSL::build;
148157
selectDSL.addQueryExpression(buildModel());
149158
return selectDSL.limit(limit);
150159
}
151160

152161
public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
162+
buildDelegateMethod = selectDSL::build;
153163
selectDSL.addQueryExpression(buildModel());
154164
return selectDSL.offset(offset);
155165
}
156166

157167
public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
168+
buildDelegateMethod = selectDSL::build;
158169
selectDSL.addQueryExpression(buildModel());
159170
return selectDSL.fetchFirst(fetchFirstRows);
160171
}
@@ -216,11 +227,13 @@ public class QueryExpressionWhereBuilder extends AbstractWhereDSL<QueryExpressio
216227
implements Buildable<R> {
217228
private <T> QueryExpressionWhereBuilder(BindableColumn<T> column, VisitableCondition<T> condition) {
218229
super(column, condition);
230+
buildDelegateMethod = this::internalBuild;
219231
}
220232

221233
private <T> QueryExpressionWhereBuilder(BindableColumn<T> column, VisitableCondition<T> condition,
222234
SqlCriterion<?>...subCriteria) {
223235
super(column, condition, subCriteria);
236+
buildDelegateMethod = this::internalBuild;
224237
}
225238

226239
public UnionBuilder union() {
@@ -236,6 +249,7 @@ public UnionBuilder unionAll() {
236249
}
237250

238251
public SelectDSL<R> orderBy(SortSpecification...columns) {
252+
buildDelegateMethod = selectDSL::build;
239253
whereModel = buildWhereModel();
240254
selectDSL.addQueryExpression(buildModel());
241255
selectDSL.setOrderByModel(OrderByModel.of(columns));
@@ -262,13 +276,18 @@ public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
262276
}
263277

264278
public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
279+
buildDelegateMethod = selectDSL::build;
265280
whereModel = buildWhereModel();
266281
selectDSL.addQueryExpression(buildModel());
267282
return selectDSL.fetchFirst(fetchFirstRows);
268283
}
269284

270285
@Override
271286
public R build() {
287+
return buildDelegateMethod.get();
288+
}
289+
290+
private R internalBuild() {
272291
whereModel = buildWhereModel();
273292
selectDSL.addQueryExpression(buildModel());
274293
return selectDSL.build();
@@ -300,7 +319,6 @@ public JoinSpecificationFinisher on(BasicColumn joinColumn, JoinCondition joinCo
300319
}
301320

302321
public class JoinSpecificationFinisher implements Buildable<R> {
303-
304322
private SqlTable joinTable;
305323
private List<JoinCriterion> joinCriteria = new ArrayList<>();
306324
private JoinType joinType;
@@ -316,6 +334,7 @@ public JoinSpecificationFinisher(SqlTable table, BasicColumn joinColumn,
316334
.build();
317335

318336
joinCriteria.add(joinCriterion);
337+
buildDelegateMethod = this::internalbuild;
319338
}
320339

321340
public JoinSpecificationFinisher(SqlTable table, BasicColumn joinColumn,
@@ -330,6 +349,7 @@ public JoinSpecificationFinisher(SqlTable table, BasicColumn joinColumn,
330349

331350
this.joinCriteria.add(joinCriterion);
332351
this.joinCriteria.addAll(Arrays.asList(joinCriteria));
352+
buildDelegateMethod = this::internalbuild;
333353
}
334354

335355
protected JoinSpecification buildJoinSpecification() {
@@ -346,6 +366,10 @@ protected JoinModel buildJoinModel() {
346366

347367
@Override
348368
public R build() {
369+
return buildDelegateMethod.get();
370+
}
371+
372+
private R internalbuild() {
349373
joinModel = buildJoinModel();
350374
selectDSL.addQueryExpression(buildModel());
351375
return selectDSL.build();
@@ -412,51 +436,67 @@ public JoinSpecificationStarter fullJoin(SqlTable joinTable, String tableAlias)
412436
}
413437

414438
public SelectDSL<R> orderBy(SortSpecification...columns) {
439+
buildDelegateMethod = selectDSL::build;
415440
joinModel = buildJoinModel();
416441
selectDSL.addQueryExpression(buildModel());
417442
selectDSL.setOrderByModel(OrderByModel.of(columns));
418443
return selectDSL;
419444
}
420445

421446
public SelectDSL<R>.LimitFinisher limit(long limit) {
447+
buildDelegateMethod = selectDSL::build;
422448
joinModel = buildJoinModel();
423449
selectDSL.addQueryExpression(buildModel());
424450
return selectDSL.limit(limit);
425451
}
426452

427453
public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
454+
buildDelegateMethod = selectDSL::build;
428455
joinModel = buildJoinModel();
429456
selectDSL.addQueryExpression(buildModel());
430457
return selectDSL.offset(offset);
431458
}
432459

433460
public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
461+
buildDelegateMethod = selectDSL::build;
434462
joinModel = buildJoinModel();
435463
selectDSL.addQueryExpression(buildModel());
436464
return selectDSL.fetchFirst(fetchFirstRows);
437465
}
438466
}
439467

440468
public class GroupByFinisher implements Buildable<R> {
469+
public GroupByFinisher() {
470+
buildDelegateMethod = this::internalBuild;
471+
}
472+
441473
public SelectDSL<R> orderBy(SortSpecification...columns) {
474+
buildDelegateMethod = selectDSL::build;
442475
selectDSL.setOrderByModel(OrderByModel.of(columns));
443476
return selectDSL;
444477
}
445478

446479
@Override
447480
public R build() {
481+
return buildDelegateMethod.get();
482+
}
483+
484+
private R internalBuild() {
448485
return selectDSL.build();
449486
}
450487

451488
public SelectDSL<R>.LimitFinisher limit(long limit) {
489+
buildDelegateMethod = selectDSL::build;
452490
return selectDSL.limit(limit);
453491
}
454492

455493
public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
494+
buildDelegateMethod = selectDSL::build;
456495
return selectDSL.offset(offset);
457496
}
458497

459498
public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
499+
buildDelegateMethod = selectDSL::build;
460500
return selectDSL.fetchFirst(fetchFirstRows);
461501
}
462502
}

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

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@
1919
import java.util.List;
2020
import java.util.Objects;
2121
import java.util.function.Function;
22+
import java.util.function.Supplier;
2223

2324
import org.mybatis.dynamic.sql.BasicColumn;
24-
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer;
25-
import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGathererBuilder;
2625
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
2726
import org.mybatis.dynamic.sql.util.Buildable;
2827

@@ -39,52 +38,54 @@ public class SelectDSL<R> implements Buildable<R> {
3938
private List<QueryExpressionModel> queryExpressions = new ArrayList<>();
4039
private OrderByModel orderByModel;
4140
private PagingModel pagingModel;
41+
private Supplier<R> buildDelegateMethod;
4242

4343
private SelectDSL(Function<SelectModel, R> adapterFunction) {
4444
this.adapterFunction = Objects.requireNonNull(adapterFunction);
45+
buildDelegateMethod = this::internalBuild;
4546
}
4647

47-
private FromGatherer<R> queryExpressionBuilder(BasicColumn...selectList) {
48-
return new FromGathererBuilder<R>()
48+
private QueryExpressionDSL.FromGatherer<R> queryExpressionBuilder(BasicColumn...selectList) {
49+
return new QueryExpressionDSL.FromGathererBuilder<R>()
4950
.withSelectDSL(this)
5051
.withSelectList(selectList)
5152
.build();
5253
}
5354

54-
private FromGatherer<R> distinctQueryExpressionBuilder(BasicColumn...selectList) {
55-
return new FromGathererBuilder<R>()
55+
private QueryExpressionDSL.FromGatherer<R> distinctQueryExpressionBuilder(BasicColumn...selectList) {
56+
return new QueryExpressionDSL.FromGathererBuilder<R>()
5657
.withSelectDSL(this)
5758
.withSelectList(selectList)
5859
.isDistinct()
5960
.build();
6061
}
6162

62-
public static FromGatherer<SelectModel> select(BasicColumn...selectList) {
63+
public static QueryExpressionDSL.FromGatherer<SelectModel> select(BasicColumn...selectList) {
6364
return select(Function.identity(), selectList);
6465
}
6566

66-
public static <R> FromGatherer<R> select(Function<SelectModel, R> adapterFunction,
67+
public static <R> QueryExpressionDSL.FromGatherer<R> select(Function<SelectModel, R> adapterFunction,
6768
BasicColumn...selectList) {
6869
SelectDSL<R> selectModelBuilder = new SelectDSL<>(adapterFunction);
6970
return selectModelBuilder.queryExpressionBuilder(selectList);
7071
}
7172

72-
public static FromGatherer<SelectModel> selectDistinct(BasicColumn...selectList) {
73+
public static QueryExpressionDSL.FromGatherer<SelectModel> selectDistinct(BasicColumn...selectList) {
7374
return selectDistinct(Function.identity(), selectList);
7475
}
7576

76-
public static <R> FromGatherer<R> selectDistinct(Function<SelectModel, R> adapterFunction,
77+
public static <R> QueryExpressionDSL.FromGatherer<R> selectDistinct(Function<SelectModel, R> adapterFunction,
7778
BasicColumn...selectList) {
7879
SelectDSL<R> selectModelBuilder = new SelectDSL<>(adapterFunction);
7980
return selectModelBuilder.distinctQueryExpressionBuilder(selectList);
8081
}
8182

82-
public static <T> FromGatherer<MyBatis3SelectModelAdapter<T>> selectWithMapper(
83+
public static <T> QueryExpressionDSL.FromGatherer<MyBatis3SelectModelAdapter<T>> selectWithMapper(
8384
Function<SelectStatementProvider, T> mapperMethod, BasicColumn...selectList) {
8485
return select(selectModel -> MyBatis3SelectModelAdapter.of(selectModel, mapperMethod), selectList);
8586
}
8687

87-
public static <T> FromGatherer<MyBatis3SelectModelAdapter<T>> selectDistinctWithMapper(
88+
public static <T> QueryExpressionDSL.FromGatherer<MyBatis3SelectModelAdapter<T>> selectDistinctWithMapper(
8889
Function<SelectStatementProvider, T> mapperMethod, BasicColumn...selectList) {
8990
return selectDistinct(selectModel -> MyBatis3SelectModelAdapter.of(selectModel, mapperMethod),
9091
selectList);
@@ -112,6 +113,10 @@ public FetchFirstFinisher fetchFirst(long fetchFirstRows) {
112113

113114
@Override
114115
public R build() {
116+
return buildDelegateMethod.get();
117+
}
118+
119+
private R internalBuild() {
115120
SelectModel selectModel = SelectModel.withQueryExpressions(queryExpressions)
116121
.withOrderByModel(orderByModel)
117122
.withPagingModel(pagingModel)
@@ -124,6 +129,7 @@ public class LimitFinisher implements Buildable<R> {
124129

125130
public LimitFinisher(long limit) {
126131
this.limit = limit;
132+
buildDelegateMethod = this::internalBuild;
127133
}
128134

129135
public OffsetFinisher offset(long offset) {
@@ -132,15 +138,20 @@ public OffsetFinisher offset(long offset) {
132138

133139
@Override
134140
public R build() {
135-
SelectDSL.this.pagingModel = new LimitAndOffsetPagingModel.Builder()
141+
return buildDelegateMethod.get();
142+
}
143+
144+
private R internalBuild() {
145+
pagingModel = new LimitAndOffsetPagingModel.Builder()
136146
.withLimit(limit)
137147
.build();
138-
return SelectDSL.this.build();
148+
return SelectDSL.this.internalBuild();
139149
}
140150
}
141151

142152
public class OffsetFinisher implements Buildable<R> {
143153
public OffsetFinisher(long limit, long offset) {
154+
buildDelegateMethod = this::internalBuild;
144155
SelectDSL.this.pagingModel = new LimitAndOffsetPagingModel.Builder()
145156
.withLimit(limit)
146157
.withOffset(offset)
@@ -149,7 +160,11 @@ public OffsetFinisher(long limit, long offset) {
149160

150161
@Override
151162
public R build() {
152-
return SelectDSL.this.build();
163+
return buildDelegateMethod.get();
164+
}
165+
166+
private R internalBuild() {
167+
return SelectDSL.this.internalBuild();
153168
}
154169
}
155170

@@ -158,6 +173,7 @@ public class OffsetFirstFinisher implements Buildable<R> {
158173

159174
public OffsetFirstFinisher(long offset) {
160175
this.offset = offset;
176+
buildDelegateMethod = this::internalBuild;
161177
}
162178

163179
public FetchFirstFinisher fetchFirst(long fetchFirstRows) {
@@ -166,10 +182,14 @@ public FetchFirstFinisher fetchFirst(long fetchFirstRows) {
166182

167183
@Override
168184
public R build() {
185+
return buildDelegateMethod.get();
186+
}
187+
188+
private R internalBuild() {
169189
SelectDSL.this.pagingModel = new FetchFirstPagingModel.Builder()
170190
.withOffset(offset)
171191
.build();
172-
return SelectDSL.this.build();
192+
return SelectDSL.this.internalBuild();
173193
}
174194
}
175195

@@ -193,9 +213,17 @@ public RowsOnlyFinisher rowsOnly() {
193213
}
194214

195215
public class RowsOnlyFinisher implements Buildable<R> {
216+
public RowsOnlyFinisher() {
217+
buildDelegateMethod = this::internalBuild;
218+
}
219+
196220
@Override
197221
public R build() {
198-
return SelectDSL.this.build();
222+
return buildDelegateMethod.get();
223+
}
224+
225+
private R internalBuild() {
226+
return SelectDSL.this.internalBuild();
199227
}
200228
}
201229
}

0 commit comments

Comments
 (0)