Skip to content

Add Support for Sub-Queries in Select Statement "from" Clauses #282

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 21 commits into from
Nov 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
44500b2
Initial infrastructure for table expression support
jeffgbutler Oct 18, 2020
5795dc9
Initial rendering of a simple sub-query
jeffgbutler Oct 19, 2020
00f1542
Refactor for Clarity
jeffgbutler Oct 19, 2020
f7d13e8
Support for SubQueries with Aliases
jeffgbutler Oct 19, 2020
6acead9
Updates from detekt
jeffgbutler Oct 19, 2020
e31e74c
Add derived column to better support table aliases in sub queries
jeffgbutler Oct 20, 2020
0aa7a8f
Fix join renderer to handle sub queries properly
jeffgbutler Oct 20, 2020
970d106
Better method names for qualifying columns
jeffgbutler Oct 22, 2020
e2796a3
Merge branch 'master' into table-expressions
jeffgbutler Oct 30, 2020
b10c036
Add sub-query support to Kotlin DSL
jeffgbutler Oct 31, 2020
0aef09c
Polish Kotlin DSL
jeffgbutler Nov 1, 2020
4890f3f
Polish Kotlin DSL
jeffgbutler Nov 1, 2020
79dbdcb
Remove methods that could cause a name conflict
jeffgbutler Nov 1, 2020
470007c
Add support for Kotlin style subqueries in where conditions
jeffgbutler Nov 3, 2020
a61761a
Better pattern for overriding the table qualifier
jeffgbutler Nov 4, 2020
b6acb84
Coverage
jeffgbutler Nov 5, 2020
5d17daf
Yet another improvement to the Kotlin DSL for subqueries
jeffgbutler Nov 6, 2020
420c6e3
Kotlin helpers for InsertSelect statements
jeffgbutler Nov 6, 2020
ff48b5a
Kotlin helpers for InsertSelect statements with MyBatis
jeffgbutler Nov 6, 2020
ce51537
Documentation for new sub-query support
jeffgbutler Nov 6, 2020
e5b9d94
Update CHANGELOG.md
jeffgbutler Nov 6, 2020
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Kotlin DSL.

- Added a new sort specification that is useful in selects with joins ([#269](https://github.com/mybatis/mybatis-dynamic-sql/pull/269))
- Added the capability to generate a camel cased alias for a column ([#272](https://github.com/mybatis/mybatis-dynamic-sql/issues/272))
- Added sub-query support for "from" clauses in a select statement ([#282](https://github.com/mybatis/mybatis-dynamic-sql/pull/282))
- Added Kotlin DSL updates to support sub-queries in select statements, where clauses, and insert statements ([#282](https://github.com/mybatis/mybatis-dynamic-sql/pull/282))

## Release 1.2.1 - September 29, 2020

Expand Down
129 changes: 129 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/DerivedColumn.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql;

import java.sql.JDBCType;
import java.util.Objects;
import java.util.Optional;

import org.mybatis.dynamic.sql.render.TableAliasCalculator;

/**
* A derived column is a column that is not directly related to a table. This is primarily
* used for supporting sub-queries. The main difference in this class and {@link SqlColumn} is
* that this class does not have a related {@link SqlTable} and therefore ignores any table
* qualifier set in a query. If a table qualifier is required it can be set directly in the
* builder for this class.
*
* @param <T> The Java type that corresponds to this column - not used except for compiler type checking
* for conditions
*/
public class DerivedColumn<T> implements BindableColumn<T> {
private final String name;
private final String tableQualifier;
private final String columnAlias;
private final JDBCType jdbcType;
private final String typeHandler;

protected DerivedColumn(Builder<T> builder) {
this.name = Objects.requireNonNull(builder.name);
this.tableQualifier = builder.tableQualifier;
this.columnAlias = builder.columnAlias;
this.jdbcType = builder.jdbcType;
this.typeHandler = builder.typeHandler;
}

@Override
public Optional<String> alias() {
return Optional.ofNullable(columnAlias);
}

@Override
public Optional<JDBCType> jdbcType() {
return Optional.ofNullable(jdbcType);
}

@Override
public Optional<String> typeHandler() {
return Optional.ofNullable(typeHandler);
}

@Override
public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
return tableQualifier == null ? name : tableQualifier + "." + name; //$NON-NLS-1$
}

@Override
public DerivedColumn<T> as(String columnAlias) {
return new Builder<T>()
.withName(name)
.withColumnAlias(columnAlias)
.withJdbcType(jdbcType)
.withTypeHandler(typeHandler)
.withTableQualifier(tableQualifier)
.build();
}

public static <T> DerivedColumn<T> of(String name) {
return new Builder<T>()
.withName(name)
.build();
}

public static <T> DerivedColumn<T> of(String name, String tableQualifier) {
return new Builder<T>()
.withName(name)
.withTableQualifier(tableQualifier)
.build();
}

public static class Builder<T> {
private String name;
private String tableQualifier;
private String columnAlias;
private JDBCType jdbcType;
private String typeHandler;

public Builder<T> withName(String name) {
this.name = name;
return this;
}

public Builder<T> withTableQualifier(String tableQualifier) {
this.tableQualifier = tableQualifier;
return this;
}

public Builder<T> withColumnAlias(String columnAlias) {
this.columnAlias = columnAlias;
return this;
}

public Builder<T> withJdbcType(JDBCType jdbcType) {
this.jdbcType = jdbcType;
return this;
}

public Builder<T> withTypeHandler(String typeHandler) {
this.typeHandler = typeHandler;
return this;
}

public DerivedColumn<T> build() {
return new DerivedColumn<>(this);
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

Expand Down Expand Up @@ -760,6 +761,11 @@ public InsertSelectDSL.SelectGatherer withColumnList(SqlColumn<?>...columns) {
.withColumnList(columns);
}

public InsertSelectDSL.SelectGatherer withColumnList(List<SqlColumn<?>> columns) {
return InsertSelectDSL.insertInto(table)
.withColumnList(columns);
}

public <T> GeneralInsertDSL.SetClauseFinisher<T> set(SqlColumn<T> column) {
return GeneralInsertDSL.insertInto(table)
.set(column);
Expand Down
31 changes: 28 additions & 3 deletions src/main/java/org/mybatis/dynamic/sql/SqlColumn.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.sql.JDBCType;
import java.util.Objects;
import java.util.Optional;
import java.util.function.BiFunction;

import org.jetbrains.annotations.NotNull;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
Expand All @@ -34,6 +35,7 @@ public class SqlColumn<T> implements BindableColumn<T>, SortSpecification {
protected final String typeHandler;
protected final RenderingStrategy renderingStrategy;
protected final ParameterTypeConverter<T, ?> parameterTypeConverter;
protected final BiFunction<TableAliasCalculator, SqlTable, Optional<String>> tableQualifierFunction;

private SqlColumn(Builder<T> builder) {
name = Objects.requireNonNull(builder.name);
Expand All @@ -44,6 +46,7 @@ private SqlColumn(Builder<T> builder) {
typeHandler = builder.typeHandler;
renderingStrategy = builder.renderingStrategy;
parameterTypeConverter = builder.parameterTypeConverter;
tableQualifierFunction = Objects.requireNonNull(builder.tableQualifierFunction);
}

public String name() {
Expand Down Expand Up @@ -86,6 +89,19 @@ public SqlColumn<T> as(String alias) {
return b.withAlias(alias).build();
}

/**
* Override the calculated table qualifier if there is one. This is useful for sub-queries
* where the calculated table qualifier may not be correct in all cases.
*
* @param tableQualifier the table qualifier to apply to the rendered column name
* @return a new column that will be rendered with the specified table qualifier
*/
public SqlColumn<T> qualifiedWith(String tableQualifier) {
Builder<T> b = copy();
b.withTableQualifierFunction((tac, t) -> Optional.of(tableQualifier));
return b.build();
}

/**
* Set an alias with a camel cased string based on the column name. The can be useful for queries using
* the {@link org.mybatis.dynamic.sql.util.mybatis3.CommonSelectMapper} where the columns are placed into
Expand Down Expand Up @@ -114,7 +130,7 @@ public String orderByName() {

@Override
public String renderWithTableAlias(TableAliasCalculator tableAliasCalculator) {
return tableAliasCalculator.aliasForColumn(table)
return tableQualifierFunction.apply(tableAliasCalculator, table)
.map(this::applyTableAlias)
.orElseGet(this::name);
}
Expand Down Expand Up @@ -160,8 +176,9 @@ private <S> Builder<S> copy() {
.withDescending(this.isDescending)
.withAlias(this.alias)
.withTypeHandler(this.typeHandler)
.withRenderingStrategy((this.renderingStrategy))
.withParameterTypeConverter((ParameterTypeConverter<S, ?>) this.parameterTypeConverter);
.withRenderingStrategy(this.renderingStrategy)
.withParameterTypeConverter((ParameterTypeConverter<S, ?>) this.parameterTypeConverter)
.withTableQualifierFunction(this.tableQualifierFunction);
}

private String applyTableAlias(String tableAlias) {
Expand Down Expand Up @@ -190,6 +207,8 @@ public static class Builder<T> {
protected String typeHandler;
protected RenderingStrategy renderingStrategy;
protected ParameterTypeConverter<T, ?> parameterTypeConverter;
protected BiFunction<TableAliasCalculator, SqlTable, Optional<String>> tableQualifierFunction =
TableAliasCalculator::aliasForColumn;

public Builder<T> withName(String name) {
this.name = name;
Expand Down Expand Up @@ -231,6 +250,12 @@ public Builder<T> withParameterTypeConverter(ParameterTypeConverter<T, ?> parame
return this;
}

private Builder<T> withTableQualifierFunction(
BiFunction<TableAliasCalculator, SqlTable, Optional<String>> tableQualifierFunction) {
this.tableQualifierFunction = tableQualifierFunction;
return this;
}

public SqlColumn<T> build() {
return new SqlColumn<>(this);
}
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/org/mybatis/dynamic/sql/SqlTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import org.jetbrains.annotations.NotNull;

public class SqlTable {
public class SqlTable implements TableExpression {

private final Supplier<String> nameSupplier;

Expand Down Expand Up @@ -103,6 +103,11 @@ public <T> SqlColumn<T> column(String name, JDBCType jdbcType, String typeHandle
return column.withTypeHandler(typeHandler);
}

@Override
public <R> R accept(TableExpressionVisitor<R> visitor) {
return visitor.visit(this);
}

public static SqlTable of(String name) {
return new SqlTable(name);
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/TableExpression.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql;

public interface TableExpression {

<R> R accept(TableExpressionVisitor<R> visitor);
}
24 changes: 24 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/TableExpressionVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.mybatis.dynamic.sql;

import org.mybatis.dynamic.sql.select.SubQuery;

public interface TableExpressionVisitor<R> {
R visit(SqlTable table);

R visit(SubQuery subQuery);
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ private InsertColumnGatherer(SqlTable table) {
}

public SelectGatherer withColumnList(SqlColumn<?>...columns) {
return new SelectGatherer(table, Arrays.asList(columns));
return withColumnList(Arrays.asList(columns));
}

public SelectGatherer withColumnList(List<SqlColumn<?>> columns) {
return new SelectGatherer(table, columns);
}

public InsertSelectDSL withSelectStatement(Buildable<SelectModel> selectModelBuilder) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Objects;
import java.util.Optional;

import org.jetbrains.annotations.NotNull;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.insert.render.InsertSelectRenderer;
import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider;
Expand Down Expand Up @@ -47,6 +48,7 @@ public Optional<InsertColumnListModel> columnList() {
return Optional.ofNullable(columnList);
}

@NotNull
public InsertSelectStatementProvider render(RenderingStrategy renderingStrategy) {
return InsertSelectRenderer.withInsertSelectModel(this)
.withRenderingStrategy(renderingStrategy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.stream.Collectors;

import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.TableExpression;
import org.mybatis.dynamic.sql.select.join.JoinCriterion;
import org.mybatis.dynamic.sql.select.join.JoinModel;
import org.mybatis.dynamic.sql.select.join.JoinSpecification;
Expand All @@ -36,13 +37,13 @@ public abstract class AbstractQueryExpressionDSL<T extends AbstractQueryExpressi

private final List<JoinSpecification.Builder> joinSpecificationBuilders = new ArrayList<>();
protected final Map<SqlTable, String> tableAliases = new HashMap<>();
private final SqlTable table;
private final TableExpression table;

protected AbstractQueryExpressionDSL(SqlTable table) {
protected AbstractQueryExpressionDSL(TableExpression table) {
this.table = Objects.requireNonNull(table);
}

public SqlTable table() {
public TableExpression table() {
return table;
}

Expand Down
Loading