Skip to content

Expose Table Aliases to Sub Queries in Where Clauses #459

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 12 commits into from
Mar 2, 2022
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This log will detail notable changes to MyBatis Dynamic SQL. Full details are av

## Release 1.4.0 - Unreleased

The release includes new function in the Where Clause DSL to support arbitrary grouping of conditions, and also use
The release includes new functionality in the Where Clause DSL to support arbitrary grouping of conditions, and also use
of a "not" condition. It should now be possible to write any type of where clause.

Additionally, there were significant updates to the Kotlin DSL - both to support the new functionality in the
Expand Down Expand Up @@ -44,6 +44,10 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
insertBatch, and insertMultiple, the "into" function is moved inside the completer lambda. The old methods are now
deprecated and will be removed in version 1.5.0 of the library. This also allowed us to make some insert DSL
methods into infix functions. ([#452](https://github.com/mybatis/mybatis-dynamic-sql/pull/452))
8. Updated the where clause to expose table aliases specified in an outer query to sub queries in the where clause
(either an "exists" clause, or a sub query to column comparison condition) This makes it easier to use these types
of sub queries without having to re-specify the aliases for columns from the outer query.
([#459](https://github.com/mybatis/mybatis-dynamic-sql/pull/459))

## Release 1.3.1 - December 18, 2021

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2016-2022 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.
Expand All @@ -19,8 +19,8 @@ public interface InsertStatementProvider<T> {
/**
* Return the row associated with this insert statement.
*
* @deprecated in favor of {@link InsertStatementProvider#getRow()}
* @return the row associated with this insert statement.
* @deprecated in favor of {@link InsertStatementProvider#getRow()}
*/
@Deprecated
T getRecord();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2016-2022 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.render;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.mybatis.dynamic.sql.SqlTable;

public class ExplicitTableAliasCalculator implements TableAliasCalculator {
private final Map<SqlTable, String> aliases;

protected ExplicitTableAliasCalculator(Map<SqlTable, String> aliases) {
this.aliases = Objects.requireNonNull(aliases);
}

@Override
public Optional<String> aliasForColumn(SqlTable table) {
return explicitAliasOrTableAlias(table);
}

@Override
public Optional<String> aliasForTable(SqlTable table) {
return explicitAliasOrTableAlias(table);
}

private Optional<String> explicitAliasOrTableAlias(SqlTable table) {
String alias = aliases.get(table);
if (alias == null) {
return table.tableAlias();
} else {
return Optional.of(alias);
}
}

public static TableAliasCalculator of(SqlTable table, String alias) {
Map<SqlTable, String> tableAliases = new HashMap<>();
tableAliases.put(table, alias);
return of(tableAliases);
}

public static TableAliasCalculator of(Map<SqlTable, String> aliases) {
return new ExplicitTableAliasCalculator(aliases);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2016-2022 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.
Expand Down Expand Up @@ -27,7 +27,7 @@
* @author Jeff Butler
*
*/
public class GuaranteedTableAliasCalculator extends TableAliasCalculator {
public class GuaranteedTableAliasCalculator extends ExplicitTableAliasCalculator {

private GuaranteedTableAliasCalculator(Map<SqlTable, String> aliases) {
super(aliases);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2021 the original author or authors.
* Copyright 2016-2022 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.
Expand All @@ -15,50 +15,27 @@
*/
package org.mybatis.dynamic.sql.render;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import org.mybatis.dynamic.sql.SqlTable;

public class TableAliasCalculator {
public interface TableAliasCalculator {

private final Map<SqlTable, String> aliases;
Optional<String> aliasForColumn(SqlTable table);

protected TableAliasCalculator(Map<SqlTable, String> aliases) {
this.aliases = Objects.requireNonNull(aliases);
}

public Optional<String> aliasForColumn(SqlTable table) {
return explicitAliasOrTableAlias(table);
}

public Optional<String> aliasForTable(SqlTable table) {
return explicitAliasOrTableAlias(table);
}
Optional<String> aliasForTable(SqlTable table);

private Optional<String> explicitAliasOrTableAlias(SqlTable table) {
String alias = aliases.get(table);
if (alias == null) {
return table.tableAlias();
} else {
return Optional.of(alias);
}
}

public static TableAliasCalculator of(SqlTable table, String alias) {
Map<SqlTable, String> tableAliases = new HashMap<>();
tableAliases.put(table, alias);
return of(tableAliases);
}

public static TableAliasCalculator of(Map<SqlTable, String> aliases) {
return new TableAliasCalculator(aliases);
}
static TableAliasCalculator empty() {
return new TableAliasCalculator() {
@Override
public Optional<String> aliasForColumn(SqlTable table) {
return Optional.empty();
}

public static TableAliasCalculator empty() {
return of(Collections.emptyMap());
@Override
public Optional<String> aliasForTable(SqlTable table) {
return Optional.empty();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2016-2022 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.render;

import java.util.Objects;
import java.util.Optional;

import org.mybatis.dynamic.sql.SqlTable;

public class TableAliasCalculatorWithParent implements TableAliasCalculator {
private final TableAliasCalculator parent;
private final TableAliasCalculator child;

private TableAliasCalculatorWithParent(Builder builder) {
parent = Objects.requireNonNull(builder.parent);
child = Objects.requireNonNull(builder.child);
}

@Override
public Optional<String> aliasForColumn(SqlTable table) {
Optional<String> answer = child.aliasForColumn(table);
if (answer.isPresent()) {
return answer;
}
return parent.aliasForColumn(table);
}

@Override
public Optional<String> aliasForTable(SqlTable table) {
Optional<String> answer = child.aliasForTable(table);
if (answer.isPresent()) {
return answer;
}
return parent.aliasForTable(table);
}

public static class Builder {
private TableAliasCalculator parent;
private TableAliasCalculator child;

public Builder withParent(TableAliasCalculator parent) {
this.parent = parent;
return this;
}

public Builder withChild(TableAliasCalculator child) {
this.child = child;
return this;
}

public TableAliasCalculatorWithParent build() {
return new TableAliasCalculatorWithParent(this);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2020 the original author or authors.
* Copyright 2016-2022 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.
Expand Down Expand Up @@ -27,8 +27,6 @@
import org.mybatis.dynamic.sql.BasicColumn;
import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.TableExpression;
import org.mybatis.dynamic.sql.render.GuaranteedTableAliasCalculator;
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
import org.mybatis.dynamic.sql.select.join.JoinModel;
import org.mybatis.dynamic.sql.where.WhereModel;

Expand All @@ -38,7 +36,7 @@ public class QueryExpressionModel {
private final List<BasicColumn> selectList;
private final TableExpression table;
private final JoinModel joinModel;
private final TableAliasCalculator tableAliasCalculator;
private final Map<SqlTable, String> tableAliases;
private final WhereModel whereModel;
private final GroupByModel groupByModel;

Expand All @@ -48,22 +46,11 @@ private QueryExpressionModel(Builder builder) {
selectList = Objects.requireNonNull(builder.selectList);
table = Objects.requireNonNull(builder.table);
joinModel = builder.joinModel;
tableAliasCalculator = joinModel().map(jm -> determineJoinTableAliasCalculator(jm, builder.tableAliases))
.orElseGet(() -> TableAliasCalculator.of(builder.tableAliases));
tableAliases = builder.tableAliases;
whereModel = builder.whereModel;
groupByModel = builder.groupByModel;
}

private TableAliasCalculator determineJoinTableAliasCalculator(JoinModel joinModel, Map<SqlTable,
String> tableAliases) {
if (joinModel.containsSubQueries()) {
// if there are subQueries, then force explicit qualifiers
return TableAliasCalculator.of(tableAliases);
} else {
return GuaranteedTableAliasCalculator.of(tableAliases);
}
}

public Optional<String> connector() {
return Optional.ofNullable(connector);
}
Expand All @@ -80,8 +67,8 @@ public TableExpression table() {
return table;
}

public TableAliasCalculator tableAliasCalculator() {
return tableAliasCalculator;
public Map<SqlTable, String> tableAliases() {
return tableAliases;
}

public Optional<WhereModel> whereModel() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright 2016-2022 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.select.render;

import java.util.concurrent.atomic.AtomicInteger;

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

public abstract class AbstractQueryRendererBuilder<T extends AbstractQueryRendererBuilder<T>> {
RenderingStrategy renderingStrategy;
AtomicInteger sequence;
TableAliasCalculator parentTableAliasCalculator;

public T withRenderingStrategy(RenderingStrategy renderingStrategy) {
this.renderingStrategy = renderingStrategy;
return getThis();
}

public T withSequence(AtomicInteger sequence) {
this.sequence = sequence;
return getThis();
}

public T withParentTableAliasCalculator(TableAliasCalculator parentTableAliasCalculator) {
this.parentTableAliasCalculator = parentTableAliasCalculator;
return getThis();
}

abstract T getThis();
}
Loading