Skip to content

Add the ability to call a build method on any intermediate object in a select statement #106

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 8 commits 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
### Added

- Changed the public SQLBuilder API to accept Collection instead of List for in conditions and batch record inserts. This should have no impact on existing code, but allow for some future flexibility
- Added the ability have have table catalog and/or schema calculated at query runtime. This is useful for situations where there are different database schemas for different environments, or in some sharding situations
- Added the ability have have table catalog and/or schema calculated at query runtime. This is useful for situations where there are different database schemas for different environments, or in some sharding situations
- Added the ability to call a builder method on any intermediate object in a select statement and receive a fully rendered statement. This makes it easier to build very dynamic queries


## Release 1.1.1 - April 7, 2019
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;

import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.BindableColumn;
Expand Down Expand Up @@ -49,13 +51,15 @@ public class QueryExpressionDSL<R> implements Buildable<R> {
private GroupByModel groupByModel;
private JoinModel joinModel;
private List<JoinSpecification> joinSpecifications = new ArrayList<>();
private Supplier<R> buildDelegateMethod;

private QueryExpressionDSL(FromGatherer<R> fromGatherer) {
connector = fromGatherer.connector;
selectList = Arrays.asList(fromGatherer.selectList);
isDistinct = fromGatherer.isDistinct;
selectDSL = Objects.requireNonNull(fromGatherer.selectDSL);
table = Objects.requireNonNull(fromGatherer.table);
buildDelegateMethod = this::internalBuild;
}

private QueryExpressionDSL(FromGatherer<R> fromGatherer, String tableAlias) {
Expand Down Expand Up @@ -89,6 +93,10 @@ public <T> QueryExpressionWhereBuilder where(BindableColumn<T> column, Visitable

@Override
public R build() {
return buildDelegateMethod.get();
}

private R internalBuild() {
selectDSL.addQueryExpression(buildModel());
return selectDSL.build();
}
Expand Down Expand Up @@ -136,6 +144,7 @@ public GroupByFinisher groupBy(BasicColumn...columns) {
}

public SelectDSL<R> orderBy(SortSpecification...columns) {
buildDelegateMethod = selectDSL::build;
selectDSL.addQueryExpression(buildModel());
selectDSL.setOrderByModel(OrderByModel.of(columns));
return selectDSL;
Expand Down Expand Up @@ -164,16 +173,19 @@ protected QueryExpressionModel buildModel() {
}

public SelectDSL<R>.LimitFinisher limit(long limit) {
buildDelegateMethod = selectDSL::build;
selectDSL.addQueryExpression(buildModel());
return selectDSL.limit(limit);
}

public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
buildDelegateMethod = selectDSL::build;
selectDSL.addQueryExpression(buildModel());
return selectDSL.offset(offset);
}

public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
buildDelegateMethod = selectDSL::build;
selectDSL.addQueryExpression(buildModel());
return selectDSL.fetchFirst(fetchFirstRows);
}
Expand All @@ -184,29 +196,37 @@ public static class FromGatherer<R> {
private SelectDSL<R> selectDSL;
private boolean isDistinct;
private SqlTable table;
private Optional<QueryExpressionDSL<R>> priorQuery;

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;
this.priorQuery = Optional.ofNullable(builder.priorQuery);
}

public QueryExpressionDSL<R> from(SqlTable table) {
this.table = table;
return new QueryExpressionDSL<>(this);
return setPriorBuildDelegate(new QueryExpressionDSL<>(this));
}

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

private QueryExpressionDSL<R> setPriorBuildDelegate(QueryExpressionDSL<R> newQuery) {
priorQuery.ifPresent(pq -> pq.buildDelegateMethod = newQuery::build);
return newQuery;
}

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

public Builder<R> withConnector(String connector) {
this.connector = connector;
Expand All @@ -228,6 +248,11 @@ public Builder<R> isDistinct() {
return this;
}

public Builder<R> withPriorQuery(QueryExpressionDSL<R> priorQuery) {
this.priorQuery = priorQuery;
return this;
}

public FromGatherer<R> build() {
return new FromGatherer<>(this);
}
Expand All @@ -238,11 +263,13 @@ public class QueryExpressionWhereBuilder extends AbstractWhereDSL<QueryExpressio
implements Buildable<R> {
private <T> QueryExpressionWhereBuilder(BindableColumn<T> column, VisitableCondition<T> condition) {
super(column, condition);
buildDelegateMethod = this::internalBuild;
}

private <T> QueryExpressionWhereBuilder(BindableColumn<T> column, VisitableCondition<T> condition,
SqlCriterion<?>...subCriteria) {
super(column, condition, subCriteria);
buildDelegateMethod = this::internalBuild;
}

public UnionBuilder union() {
Expand All @@ -258,6 +285,7 @@ public UnionBuilder unionAll() {
}

public SelectDSL<R> orderBy(SortSpecification...columns) {
buildDelegateMethod = selectDSL::build;
whereModel = buildWhereModel();
selectDSL.addQueryExpression(buildModel());
selectDSL.setOrderByModel(OrderByModel.of(columns));
Expand All @@ -272,25 +300,32 @@ public GroupByFinisher groupBy(BasicColumn...columns) {
}

public SelectDSL<R>.LimitFinisher limit(long limit) {
buildDelegateMethod = selectDSL::build;
whereModel = buildWhereModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.limit(limit);
}

public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
buildDelegateMethod = selectDSL::build;
whereModel = buildWhereModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.offset(offset);
}

public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
buildDelegateMethod = selectDSL::build;
whereModel = buildWhereModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.fetchFirst(fetchFirstRows);
}

@Override
public R build() {
return buildDelegateMethod.get();
}

private R internalBuild() {
whereModel = buildWhereModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.build();
Expand Down Expand Up @@ -322,7 +357,6 @@ public JoinSpecificationFinisher on(BasicColumn joinColumn, JoinCondition joinCo
}

public class JoinSpecificationFinisher implements Buildable<R> {

private SqlTable joinTable;
private List<JoinCriterion> joinCriteria = new ArrayList<>();
private JoinType joinType;
Expand All @@ -338,6 +372,7 @@ public JoinSpecificationFinisher(SqlTable table, BasicColumn joinColumn,
.build();

joinCriteria.add(joinCriterion);
buildDelegateMethod = this::internalbuild;
}

public JoinSpecificationFinisher(SqlTable table, BasicColumn joinColumn,
Expand All @@ -352,6 +387,7 @@ public JoinSpecificationFinisher(SqlTable table, BasicColumn joinColumn,

this.joinCriteria.add(joinCriterion);
this.joinCriteria.addAll(Arrays.asList(joinCriteria));
buildDelegateMethod = this::internalbuild;
}

protected JoinSpecification buildJoinSpecification() {
Expand All @@ -368,6 +404,10 @@ protected JoinModel buildJoinModel() {

@Override
public R build() {
return buildDelegateMethod.get();
}

private R internalbuild() {
joinModel = buildJoinModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.build();
Expand Down Expand Up @@ -434,51 +474,67 @@ public JoinSpecificationStarter fullJoin(SqlTable joinTable, String tableAlias)
}

public SelectDSL<R> orderBy(SortSpecification...columns) {
buildDelegateMethod = selectDSL::build;
joinModel = buildJoinModel();
selectDSL.addQueryExpression(buildModel());
selectDSL.setOrderByModel(OrderByModel.of(columns));
return selectDSL;
}

public SelectDSL<R>.LimitFinisher limit(long limit) {
buildDelegateMethod = selectDSL::build;
joinModel = buildJoinModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.limit(limit);
}

public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
buildDelegateMethod = selectDSL::build;
joinModel = buildJoinModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.offset(offset);
}

public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
buildDelegateMethod = selectDSL::build;
joinModel = buildJoinModel();
selectDSL.addQueryExpression(buildModel());
return selectDSL.fetchFirst(fetchFirstRows);
}
}

public class GroupByFinisher implements Buildable<R> {
public GroupByFinisher() {
buildDelegateMethod = this::internalBuild;
}

public SelectDSL<R> orderBy(SortSpecification...columns) {
buildDelegateMethod = selectDSL::build;
selectDSL.setOrderByModel(OrderByModel.of(columns));
return selectDSL;
}

@Override
public R build() {
return buildDelegateMethod.get();
}

private R internalBuild() {
return selectDSL.build();
}

public SelectDSL<R>.LimitFinisher limit(long limit) {
buildDelegateMethod = selectDSL::build;
return selectDSL.limit(limit);
}

public SelectDSL<R>.OffsetFirstFinisher offset(long offset) {
buildDelegateMethod = selectDSL::build;
return selectDSL.offset(offset);
}

public SelectDSL<R>.FetchFirstFinisher fetchFirst(long fetchFirstRows) {
buildDelegateMethod = selectDSL::build;
return selectDSL.fetchFirst(fetchFirstRows);
}
}
Expand All @@ -495,6 +551,7 @@ public FromGatherer<R> select(BasicColumn...selectList) {
.withConnector(connector)
.withSelectList(selectList)
.withSelectDSL(selectDSL)
.withPriorQuery(QueryExpressionDSL.this)
.build();
}

Expand All @@ -504,6 +561,7 @@ public FromGatherer<R> selectDistinct(BasicColumn...selectList) {
.withSelectList(selectList)
.withSelectDSL(selectDSL)
.isDistinct()
.withPriorQuery(QueryExpressionDSL.this)
.build();
}
}
Expand Down
Loading