Skip to content

Commit 1954e43

Browse files
committed
Setup build delegate method for union queries
1 parent 5840ffe commit 1954e43

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
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.Optional;
2425
import java.util.function.Supplier;
2526

2627
import org.mybatis.dynamic.sql.BasicColumn;
@@ -195,29 +196,37 @@ public static class FromGatherer<R> {
195196
private SelectDSL<R> selectDSL;
196197
private boolean isDistinct;
197198
private SqlTable table;
199+
private Optional<QueryExpressionDSL<R>> priorQuery;
198200

199201
public FromGatherer(Builder<R> builder) {
200202
this.connector = builder.connector;
201203
this.selectList = Objects.requireNonNull(builder.selectList);
202204
this.selectDSL = Objects.requireNonNull(builder.selectDSL);
203205
this.isDistinct = builder.isDistinct;
206+
this.priorQuery = Optional.ofNullable(builder.priorQuery);
204207
}
205208

206209
public QueryExpressionDSL<R> from(SqlTable table) {
207210
this.table = table;
208-
return new QueryExpressionDSL<>(this);
211+
return setPriorBuildDelegate(new QueryExpressionDSL<>(this));
209212
}
210213

211214
public QueryExpressionDSL<R> from(SqlTable table, String tableAlias) {
212215
this.table = table;
213-
return new QueryExpressionDSL<>(this, tableAlias);
216+
return setPriorBuildDelegate(new QueryExpressionDSL<>(this, tableAlias));
217+
}
218+
219+
private QueryExpressionDSL<R> setPriorBuildDelegate(QueryExpressionDSL<R> newQuery) {
220+
priorQuery.ifPresent(pq -> pq.buildDelegateMethod = newQuery::build);
221+
return newQuery;
214222
}
215223

216224
public static class Builder<R> {
217225
private String connector;
218226
private BasicColumn[] selectList;
219227
private SelectDSL<R> selectDSL;
220228
private boolean isDistinct;
229+
private QueryExpressionDSL<R> priorQuery;
221230

222231
public Builder<R> withConnector(String connector) {
223232
this.connector = connector;
@@ -239,6 +248,11 @@ public Builder<R> isDistinct() {
239248
return this;
240249
}
241250

251+
public Builder<R> withPriorQuery(QueryExpressionDSL<R> priorQuery) {
252+
this.priorQuery = priorQuery;
253+
return this;
254+
}
255+
242256
public FromGatherer<R> build() {
243257
return new FromGatherer<>(this);
244258
}
@@ -535,6 +549,7 @@ public FromGatherer<R> select(BasicColumn...selectList) {
535549
.withConnector(connector)
536550
.withSelectList(selectList)
537551
.withSelectDSL(selectDSL)
552+
.withPriorQuery(QueryExpressionDSL.this)
538553
.build();
539554
}
540555

@@ -544,6 +559,7 @@ public FromGatherer<R> selectDistinct(BasicColumn...selectList) {
544559
.withSelectList(selectList)
545560
.withSelectDSL(selectDSL)
546561
.isDistinct()
562+
.withPriorQuery(QueryExpressionDSL.this)
547563
.build();
548564
}
549565
}

0 commit comments

Comments
 (0)