Skip to content

Commit 7e90820

Browse files
authored
Merge pull request #459 from jeffgbutler/gh-437
Expose Table Aliases to Sub Queries in Where Clauses
2 parents 440be15 + 25af53f commit 7e90820

22 files changed

+533
-159
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This log will detail notable changes to MyBatis Dynamic SQL. Full details are av
44

55
## Release 1.4.0 - Unreleased
66

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

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

4852
## Release 1.3.1 - December 18, 2021
4953

src/main/java/org/mybatis/dynamic/sql/insert/render/InsertStatementProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,8 +19,8 @@ public interface InsertStatementProvider<T> {
1919
/**
2020
* Return the row associated with this insert statement.
2121
*
22-
* @deprecated in favor of {@link InsertStatementProvider#getRow()}
2322
* @return the row associated with this insert statement.
23+
* @deprecated in favor of {@link InsertStatementProvider#getRow()}
2424
*/
2525
@Deprecated
2626
T getRecord();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2016-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.render;
17+
18+
import java.util.HashMap;
19+
import java.util.Map;
20+
import java.util.Objects;
21+
import java.util.Optional;
22+
23+
import org.mybatis.dynamic.sql.SqlTable;
24+
25+
public class ExplicitTableAliasCalculator implements TableAliasCalculator {
26+
private final Map<SqlTable, String> aliases;
27+
28+
protected ExplicitTableAliasCalculator(Map<SqlTable, String> aliases) {
29+
this.aliases = Objects.requireNonNull(aliases);
30+
}
31+
32+
@Override
33+
public Optional<String> aliasForColumn(SqlTable table) {
34+
return explicitAliasOrTableAlias(table);
35+
}
36+
37+
@Override
38+
public Optional<String> aliasForTable(SqlTable table) {
39+
return explicitAliasOrTableAlias(table);
40+
}
41+
42+
private Optional<String> explicitAliasOrTableAlias(SqlTable table) {
43+
String alias = aliases.get(table);
44+
if (alias == null) {
45+
return table.tableAlias();
46+
} else {
47+
return Optional.of(alias);
48+
}
49+
}
50+
51+
public static TableAliasCalculator of(SqlTable table, String alias) {
52+
Map<SqlTable, String> tableAliases = new HashMap<>();
53+
tableAliases.put(table, alias);
54+
return of(tableAliases);
55+
}
56+
57+
public static TableAliasCalculator of(Map<SqlTable, String> aliases) {
58+
return new ExplicitTableAliasCalculator(aliases);
59+
}
60+
}

src/main/java/org/mybatis/dynamic/sql/render/GuaranteedTableAliasCalculator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,7 +27,7 @@
2727
* @author Jeff Butler
2828
*
2929
*/
30-
public class GuaranteedTableAliasCalculator extends TableAliasCalculator {
30+
public class GuaranteedTableAliasCalculator extends ExplicitTableAliasCalculator {
3131

3232
private GuaranteedTableAliasCalculator(Map<SqlTable, String> aliases) {
3333
super(aliases);
Lines changed: 15 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2021 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,50 +15,27 @@
1515
*/
1616
package org.mybatis.dynamic.sql.render;
1717

18-
import java.util.Collections;
19-
import java.util.HashMap;
20-
import java.util.Map;
21-
import java.util.Objects;
2218
import java.util.Optional;
2319

2420
import org.mybatis.dynamic.sql.SqlTable;
2521

26-
public class TableAliasCalculator {
22+
public interface TableAliasCalculator {
2723

28-
private final Map<SqlTable, String> aliases;
24+
Optional<String> aliasForColumn(SqlTable table);
2925

30-
protected TableAliasCalculator(Map<SqlTable, String> aliases) {
31-
this.aliases = Objects.requireNonNull(aliases);
32-
}
33-
34-
public Optional<String> aliasForColumn(SqlTable table) {
35-
return explicitAliasOrTableAlias(table);
36-
}
37-
38-
public Optional<String> aliasForTable(SqlTable table) {
39-
return explicitAliasOrTableAlias(table);
40-
}
26+
Optional<String> aliasForTable(SqlTable table);
4127

42-
private Optional<String> explicitAliasOrTableAlias(SqlTable table) {
43-
String alias = aliases.get(table);
44-
if (alias == null) {
45-
return table.tableAlias();
46-
} else {
47-
return Optional.of(alias);
48-
}
49-
}
50-
51-
public static TableAliasCalculator of(SqlTable table, String alias) {
52-
Map<SqlTable, String> tableAliases = new HashMap<>();
53-
tableAliases.put(table, alias);
54-
return of(tableAliases);
55-
}
56-
57-
public static TableAliasCalculator of(Map<SqlTable, String> aliases) {
58-
return new TableAliasCalculator(aliases);
59-
}
28+
static TableAliasCalculator empty() {
29+
return new TableAliasCalculator() {
30+
@Override
31+
public Optional<String> aliasForColumn(SqlTable table) {
32+
return Optional.empty();
33+
}
6034

61-
public static TableAliasCalculator empty() {
62-
return of(Collections.emptyMap());
35+
@Override
36+
public Optional<String> aliasForTable(SqlTable table) {
37+
return Optional.empty();
38+
}
39+
};
6340
}
6441
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2016-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.render;
17+
18+
import java.util.Objects;
19+
import java.util.Optional;
20+
21+
import org.mybatis.dynamic.sql.SqlTable;
22+
23+
public class TableAliasCalculatorWithParent implements TableAliasCalculator {
24+
private final TableAliasCalculator parent;
25+
private final TableAliasCalculator child;
26+
27+
private TableAliasCalculatorWithParent(Builder builder) {
28+
parent = Objects.requireNonNull(builder.parent);
29+
child = Objects.requireNonNull(builder.child);
30+
}
31+
32+
@Override
33+
public Optional<String> aliasForColumn(SqlTable table) {
34+
Optional<String> answer = child.aliasForColumn(table);
35+
if (answer.isPresent()) {
36+
return answer;
37+
}
38+
return parent.aliasForColumn(table);
39+
}
40+
41+
@Override
42+
public Optional<String> aliasForTable(SqlTable table) {
43+
Optional<String> answer = child.aliasForTable(table);
44+
if (answer.isPresent()) {
45+
return answer;
46+
}
47+
return parent.aliasForTable(table);
48+
}
49+
50+
public static class Builder {
51+
private TableAliasCalculator parent;
52+
private TableAliasCalculator child;
53+
54+
public Builder withParent(TableAliasCalculator parent) {
55+
this.parent = parent;
56+
return this;
57+
}
58+
59+
public Builder withChild(TableAliasCalculator child) {
60+
this.child = child;
61+
return this;
62+
}
63+
64+
public TableAliasCalculatorWithParent build() {
65+
return new TableAliasCalculatorWithParent(this);
66+
}
67+
}
68+
}

src/main/java/org/mybatis/dynamic/sql/select/QueryExpressionModel.java

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2020 the original author or authors.
2+
* Copyright 2016-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -27,8 +27,6 @@
2727
import org.mybatis.dynamic.sql.BasicColumn;
2828
import org.mybatis.dynamic.sql.SqlTable;
2929
import org.mybatis.dynamic.sql.TableExpression;
30-
import org.mybatis.dynamic.sql.render.GuaranteedTableAliasCalculator;
31-
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
3230
import org.mybatis.dynamic.sql.select.join.JoinModel;
3331
import org.mybatis.dynamic.sql.where.WhereModel;
3432

@@ -38,7 +36,7 @@ public class QueryExpressionModel {
3836
private final List<BasicColumn> selectList;
3937
private final TableExpression table;
4038
private final JoinModel joinModel;
41-
private final TableAliasCalculator tableAliasCalculator;
39+
private final Map<SqlTable, String> tableAliases;
4240
private final WhereModel whereModel;
4341
private final GroupByModel groupByModel;
4442

@@ -48,22 +46,11 @@ private QueryExpressionModel(Builder builder) {
4846
selectList = Objects.requireNonNull(builder.selectList);
4947
table = Objects.requireNonNull(builder.table);
5048
joinModel = builder.joinModel;
51-
tableAliasCalculator = joinModel().map(jm -> determineJoinTableAliasCalculator(jm, builder.tableAliases))
52-
.orElseGet(() -> TableAliasCalculator.of(builder.tableAliases));
49+
tableAliases = builder.tableAliases;
5350
whereModel = builder.whereModel;
5451
groupByModel = builder.groupByModel;
5552
}
5653

57-
private TableAliasCalculator determineJoinTableAliasCalculator(JoinModel joinModel, Map<SqlTable,
58-
String> tableAliases) {
59-
if (joinModel.containsSubQueries()) {
60-
// if there are subQueries, then force explicit qualifiers
61-
return TableAliasCalculator.of(tableAliases);
62-
} else {
63-
return GuaranteedTableAliasCalculator.of(tableAliases);
64-
}
65-
}
66-
6754
public Optional<String> connector() {
6855
return Optional.ofNullable(connector);
6956
}
@@ -80,8 +67,8 @@ public TableExpression table() {
8067
return table;
8168
}
8269

83-
public TableAliasCalculator tableAliasCalculator() {
84-
return tableAliasCalculator;
70+
public Map<SqlTable, String> tableAliases() {
71+
return tableAliases;
8572
}
8673

8774
public Optional<WhereModel> whereModel() {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2016-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.mybatis.dynamic.sql.select.render;
17+
18+
import java.util.concurrent.atomic.AtomicInteger;
19+
20+
import org.mybatis.dynamic.sql.render.RenderingStrategy;
21+
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
22+
23+
public abstract class AbstractQueryRendererBuilder<T extends AbstractQueryRendererBuilder<T>> {
24+
RenderingStrategy renderingStrategy;
25+
AtomicInteger sequence;
26+
TableAliasCalculator parentTableAliasCalculator;
27+
28+
public T withRenderingStrategy(RenderingStrategy renderingStrategy) {
29+
this.renderingStrategy = renderingStrategy;
30+
return getThis();
31+
}
32+
33+
public T withSequence(AtomicInteger sequence) {
34+
this.sequence = sequence;
35+
return getThis();
36+
}
37+
38+
public T withParentTableAliasCalculator(TableAliasCalculator parentTableAliasCalculator) {
39+
this.parentTableAliasCalculator = parentTableAliasCalculator;
40+
return getThis();
41+
}
42+
43+
abstract T getThis();
44+
}

0 commit comments

Comments
 (0)