diff --git a/src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionDSL.java b/src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionDSL.java index 98c670806..b1465faf4 100644 --- a/src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionDSL.java +++ b/src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionDSL.java @@ -51,12 +51,31 @@ public class QueryExpressionDSL implements Buildable { private List joinSpecifications = new ArrayList<>(); private QueryExpressionDSL(FromGatherer fromGatherer) { - connector = fromGatherer.builder.connector; - selectList = Arrays.asList(fromGatherer.builder.selectList); - isDistinct = fromGatherer.builder.isDistinct; - selectDSL = Objects.requireNonNull(fromGatherer.builder.selectDSL); + connector = fromGatherer.connector; + selectList = Arrays.asList(fromGatherer.selectList); + isDistinct = fromGatherer.isDistinct; + selectDSL = Objects.requireNonNull(fromGatherer.selectDSL); table = Objects.requireNonNull(fromGatherer.table); - tableAliases.putAll(fromGatherer.tableAliasMap); + } + + private QueryExpressionDSL(FromGatherer fromGatherer, String tableAlias) { + this(fromGatherer); + tableAliases.put(table, tableAlias); + } + + public static FromGatherer select(SelectDSL selectDSL, BasicColumn...selectList) { + return new FromGatherer.Builder() + .withSelectList(selectList) + .withSelectDSL(selectDSL) + .build(); + } + + public static FromGatherer selectDistinct(SelectDSL selectDSL, BasicColumn...selectList) { + return new FromGatherer.Builder() + .withSelectList(selectList) + .withSelectDSL(selectDSL) + .isDistinct() + .build(); } public QueryExpressionWhereBuilder where(BindableColumn column, VisitableCondition condition) { @@ -160,55 +179,58 @@ public SelectDSL.FetchFirstFinisher fetchFirst(long fetchFirstRows) { } public static class FromGatherer { - private FromGathererBuilder builder; - private Map tableAliasMap = new HashMap<>(); + private String connector; + private BasicColumn[] selectList; + private SelectDSL selectDSL; + private boolean isDistinct; private SqlTable table; - public FromGatherer(FromGathererBuilder builder) { - this.builder = builder; + public FromGatherer(Builder builder) { + this.connector = builder.connector; + this.selectList = Objects.requireNonNull(builder.selectList); + this.selectDSL = Objects.requireNonNull(builder.selectDSL); + this.isDistinct = builder.isDistinct; } public QueryExpressionDSL from(SqlTable table) { this.table = table; - return new QueryExpressionDSL<>(this); } public QueryExpressionDSL from(SqlTable table, String tableAlias) { this.table = table; - tableAliasMap.put(table, tableAlias); - return new QueryExpressionDSL<>(this); - } - } - - public static class FromGathererBuilder { - private String connector; - private BasicColumn[] selectList; - private SelectDSL selectDSL; - private boolean isDistinct; - - public FromGathererBuilder withConnector(String connector) { - this.connector = connector; - return this; - } - - public FromGathererBuilder withSelectList(BasicColumn[] selectList) { - this.selectList = selectList; - return this; - } - - public FromGathererBuilder withSelectDSL(SelectDSL selectDSL) { - this.selectDSL = selectDSL; - return this; + return new QueryExpressionDSL<>(this, tableAlias); } - public FromGathererBuilder isDistinct() { - this.isDistinct = true; - return this; - } - - public FromGatherer build() { - return new FromGatherer<>(this); + public static class Builder { + private String connector; + private BasicColumn[] selectList; + private SelectDSL selectDSL; + private boolean isDistinct; + + public Builder withConnector(String connector) { + this.connector = connector; + return this; + } + + public Builder withSelectList(BasicColumn[] selectList) { + this.selectList = selectList; + return this; + } + + public Builder withSelectDSL(SelectDSL selectDSL) { + this.selectDSL = selectDSL; + return this; + } + + public Builder isDistinct() { + this.isDistinct = true; + return this; + } + + public FromGatherer build() { + return new FromGatherer<>(this); + } } } @@ -469,7 +491,7 @@ public UnionBuilder(String connector) { } public FromGatherer select(BasicColumn...selectList) { - return new FromGathererBuilder() + return new FromGatherer.Builder() .withConnector(connector) .withSelectList(selectList) .withSelectDSL(selectDSL) @@ -477,11 +499,11 @@ public FromGatherer select(BasicColumn...selectList) { } public FromGatherer selectDistinct(BasicColumn...selectList) { - return new FromGathererBuilder() + return new FromGatherer.Builder() .withConnector(connector) - .isDistinct() .withSelectList(selectList) .withSelectDSL(selectDSL) + .isDistinct() .build(); } } diff --git a/src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java b/src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java index 334066c2c..bbe2e4fd7 100644 --- a/src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java +++ b/src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java @@ -21,8 +21,6 @@ import java.util.function.Function; import org.mybatis.dynamic.sql.BasicColumn; -import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGatherer; -import org.mybatis.dynamic.sql.select.QueryExpressionDSL.FromGathererBuilder; import org.mybatis.dynamic.sql.select.render.SelectStatementProvider; import org.mybatis.dynamic.sql.util.Buildable; @@ -44,47 +42,32 @@ private SelectDSL(Function adapterFunction) { this.adapterFunction = Objects.requireNonNull(adapterFunction); } - private FromGatherer queryExpressionBuilder(BasicColumn...selectList) { - return new FromGathererBuilder() - .withSelectDSL(this) - .withSelectList(selectList) - .build(); - } - - private FromGatherer distinctQueryExpressionBuilder(BasicColumn...selectList) { - return new FromGathererBuilder() - .withSelectDSL(this) - .withSelectList(selectList) - .isDistinct() - .build(); - } - - public static FromGatherer select(BasicColumn...selectList) { + public static QueryExpressionDSL.FromGatherer select(BasicColumn...selectList) { return select(Function.identity(), selectList); } - public static FromGatherer select(Function adapterFunction, + public static QueryExpressionDSL.FromGatherer select(Function adapterFunction, BasicColumn...selectList) { - SelectDSL selectModelBuilder = new SelectDSL<>(adapterFunction); - return selectModelBuilder.queryExpressionBuilder(selectList); + SelectDSL selectDSL = new SelectDSL<>(adapterFunction); + return QueryExpressionDSL.select(selectDSL, selectList); } - public static FromGatherer selectDistinct(BasicColumn...selectList) { + public static QueryExpressionDSL.FromGatherer selectDistinct(BasicColumn...selectList) { return selectDistinct(Function.identity(), selectList); } - public static FromGatherer selectDistinct(Function adapterFunction, + public static QueryExpressionDSL.FromGatherer selectDistinct(Function adapterFunction, BasicColumn...selectList) { - SelectDSL selectModelBuilder = new SelectDSL<>(adapterFunction); - return selectModelBuilder.distinctQueryExpressionBuilder(selectList); + SelectDSL selectDSL = new SelectDSL<>(adapterFunction); + return QueryExpressionDSL.selectDistinct(selectDSL, selectList); } - public static FromGatherer> selectWithMapper( + public static QueryExpressionDSL.FromGatherer> selectWithMapper( Function mapperMethod, BasicColumn...selectList) { return select(selectModel -> MyBatis3SelectModelAdapter.of(selectModel, mapperMethod), selectList); } - public static FromGatherer> selectDistinctWithMapper( + public static QueryExpressionDSL.FromGatherer> selectDistinctWithMapper( Function mapperMethod, BasicColumn...selectList) { return selectDistinct(selectModel -> MyBatis3SelectModelAdapter.of(selectModel, mapperMethod), selectList);