Skip to content

Refactor QueryExpressionDSL #104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 66 additions & 44 deletions src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,31 @@ public class QueryExpressionDSL<R> implements Buildable<R> {
private List<JoinSpecification> joinSpecifications = new ArrayList<>();

private QueryExpressionDSL(FromGatherer<R> 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<R> fromGatherer, String tableAlias) {
this(fromGatherer);
tableAliases.put(table, tableAlias);
}

public static <R> FromGatherer<R> select(SelectDSL<R> selectDSL, BasicColumn...selectList) {
return new FromGatherer.Builder<R>()
.withSelectList(selectList)
.withSelectDSL(selectDSL)
.build();
}

public static <R> FromGatherer<R> selectDistinct(SelectDSL<R> selectDSL, BasicColumn...selectList) {
return new FromGatherer.Builder<R>()
.withSelectList(selectList)
.withSelectDSL(selectDSL)
.isDistinct()
.build();
}

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

public static class FromGatherer<R> {
private FromGathererBuilder<R> builder;
private Map<SqlTable, String> tableAliasMap = new HashMap<>();
private String connector;
private BasicColumn[] selectList;
private SelectDSL<R> selectDSL;
private boolean isDistinct;
private SqlTable table;

public FromGatherer(FromGathererBuilder<R> builder) {
this.builder = builder;
public FromGatherer(Builder<R> builder) {
this.connector = builder.connector;
this.selectList = Objects.requireNonNull(builder.selectList);
this.selectDSL = Objects.requireNonNull(builder.selectDSL);
this.isDistinct = builder.isDistinct;
}

public QueryExpressionDSL<R> from(SqlTable table) {
this.table = table;

return new QueryExpressionDSL<>(this);
}

public QueryExpressionDSL<R> from(SqlTable table, String tableAlias) {
this.table = table;
tableAliasMap.put(table, tableAlias);
return new QueryExpressionDSL<>(this);
}
}

public static class FromGathererBuilder<R> {
private String connector;
private BasicColumn[] selectList;
private SelectDSL<R> selectDSL;
private boolean isDistinct;

public FromGathererBuilder<R> withConnector(String connector) {
this.connector = connector;
return this;
}

public FromGathererBuilder<R> withSelectList(BasicColumn[] selectList) {
this.selectList = selectList;
return this;
}

public FromGathererBuilder<R> withSelectDSL(SelectDSL<R> selectDSL) {
this.selectDSL = selectDSL;
return this;
return new QueryExpressionDSL<>(this, tableAlias);
}

public FromGathererBuilder<R> isDistinct() {
this.isDistinct = true;
return this;
}

public FromGatherer<R> build() {
return new FromGatherer<>(this);
public static class Builder<R> {
private String connector;
private BasicColumn[] selectList;
private SelectDSL<R> selectDSL;
private boolean isDistinct;

public Builder<R> withConnector(String connector) {
this.connector = connector;
return this;
}

public Builder<R> withSelectList(BasicColumn[] selectList) {
this.selectList = selectList;
return this;
}

public Builder<R> withSelectDSL(SelectDSL<R> selectDSL) {
this.selectDSL = selectDSL;
return this;
}

public Builder<R> isDistinct() {
this.isDistinct = true;
return this;
}

public FromGatherer<R> build() {
return new FromGatherer<>(this);
}
}
}

Expand Down Expand Up @@ -469,19 +491,19 @@ public UnionBuilder(String connector) {
}

public FromGatherer<R> select(BasicColumn...selectList) {
return new FromGathererBuilder<R>()
return new FromGatherer.Builder<R>()
.withConnector(connector)
.withSelectList(selectList)
.withSelectDSL(selectDSL)
.build();
}

public FromGatherer<R> selectDistinct(BasicColumn...selectList) {
return new FromGathererBuilder<R>()
return new FromGatherer.Builder<R>()
.withConnector(connector)
.isDistinct()
.withSelectList(selectList)
.withSelectDSL(selectDSL)
.isDistinct()
.build();
}
}
Expand Down
37 changes: 10 additions & 27 deletions src/main/java/org/mybatis/dynamic/sql/select/SelectDSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

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

private FromGatherer<R> queryExpressionBuilder(BasicColumn...selectList) {
return new FromGathererBuilder<R>()
.withSelectDSL(this)
.withSelectList(selectList)
.build();
}

private FromGatherer<R> distinctQueryExpressionBuilder(BasicColumn...selectList) {
return new FromGathererBuilder<R>()
.withSelectDSL(this)
.withSelectList(selectList)
.isDistinct()
.build();
}

public static FromGatherer<SelectModel> select(BasicColumn...selectList) {
public static QueryExpressionDSL.FromGatherer<SelectModel> select(BasicColumn...selectList) {
return select(Function.identity(), selectList);
}

public static <R> FromGatherer<R> select(Function<SelectModel, R> adapterFunction,
public static <R> QueryExpressionDSL.FromGatherer<R> select(Function<SelectModel, R> adapterFunction,
BasicColumn...selectList) {
SelectDSL<R> selectModelBuilder = new SelectDSL<>(adapterFunction);
return selectModelBuilder.queryExpressionBuilder(selectList);
SelectDSL<R> selectDSL = new SelectDSL<>(adapterFunction);
return QueryExpressionDSL.select(selectDSL, selectList);
}

public static FromGatherer<SelectModel> selectDistinct(BasicColumn...selectList) {
public static QueryExpressionDSL.FromGatherer<SelectModel> selectDistinct(BasicColumn...selectList) {
return selectDistinct(Function.identity(), selectList);
}

public static <R> FromGatherer<R> selectDistinct(Function<SelectModel, R> adapterFunction,
public static <R> QueryExpressionDSL.FromGatherer<R> selectDistinct(Function<SelectModel, R> adapterFunction,
BasicColumn...selectList) {
SelectDSL<R> selectModelBuilder = new SelectDSL<>(adapterFunction);
return selectModelBuilder.distinctQueryExpressionBuilder(selectList);
SelectDSL<R> selectDSL = new SelectDSL<>(adapterFunction);
return QueryExpressionDSL.selectDistinct(selectDSL, selectList);
}

public static <T> FromGatherer<MyBatis3SelectModelAdapter<T>> selectWithMapper(
public static <T> QueryExpressionDSL.FromGatherer<MyBatis3SelectModelAdapter<T>> selectWithMapper(
Function<SelectStatementProvider, T> mapperMethod, BasicColumn...selectList) {
return select(selectModel -> MyBatis3SelectModelAdapter.of(selectModel, mapperMethod), selectList);
}

public static <T> FromGatherer<MyBatis3SelectModelAdapter<T>> selectDistinctWithMapper(
public static <T> QueryExpressionDSL.FromGatherer<MyBatis3SelectModelAdapter<T>> selectDistinctWithMapper(
Function<SelectStatementProvider, T> mapperMethod, BasicColumn...selectList) {
return selectDistinct(selectModel -> MyBatis3SelectModelAdapter.of(selectModel, mapperMethod),
selectList);
Expand Down