Skip to content

Commit 128ffb7

Browse files
committed
Table Alias Calculator should be an interface
1 parent bf5c72b commit 128ffb7

File tree

7 files changed

+91
-50
lines changed

7 files changed

+91
-50
lines changed
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 org.mybatis.dynamic.sql.SqlTable;
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
import java.util.Objects;
23+
import java.util.Optional;
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
}

src/main/java/org/mybatis/dynamic/sql/select/render/QueryExpressionRenderer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.mybatis.dynamic.sql.BasicColumn;
2727
import org.mybatis.dynamic.sql.TableExpression;
28+
import org.mybatis.dynamic.sql.render.ExplicitTableAliasCalculator;
2829
import org.mybatis.dynamic.sql.render.GuaranteedTableAliasCalculator;
2930
import org.mybatis.dynamic.sql.render.RenderingStrategy;
3031
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
@@ -60,13 +61,13 @@ private TableAliasCalculator determineJoinTableAliasCalculator(QueryExpressionMo
6061
return queryExpression.joinModel().map(JoinModel::containsSubQueries).map(containsSubQueries -> {
6162
if (containsSubQueries) {
6263
// if there are subQueries, then force explicit qualifiers
63-
return TableAliasCalculator.of(queryExpression.tableAliases());
64+
return ExplicitTableAliasCalculator.of(queryExpression.tableAliases());
6465
} else {
6566
// there are joins, but no sub-queries. In this case, we can use the
6667
// table names as qualifiers without requiring explicit qualifiers
6768
return GuaranteedTableAliasCalculator.of(queryExpression.tableAliases());
6869
}
69-
}).orElseGet(() -> TableAliasCalculator.of(queryExpression.tableAliases()));
70+
}).orElseGet(() -> ExplicitTableAliasCalculator.of(queryExpression.tableAliases()));
7071
}
7172

7273
public FragmentAndParameters render() {

src/test/java/examples/animal/data/AnimalDataTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@
5353
import org.mybatis.dynamic.sql.insert.render.GeneralInsertStatementProvider;
5454
import org.mybatis.dynamic.sql.insert.render.InsertSelectStatementProvider;
5555
import org.mybatis.dynamic.sql.insert.render.InsertStatementProvider;
56+
import org.mybatis.dynamic.sql.render.ExplicitTableAliasCalculator;
5657
import org.mybatis.dynamic.sql.render.RenderingStrategies;
57-
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
5858
import org.mybatis.dynamic.sql.select.SelectModel;
5959
import org.mybatis.dynamic.sql.select.render.SelectStatementProvider;
6060
import org.mybatis.dynamic.sql.update.render.UpdateStatementProvider;
@@ -314,7 +314,7 @@ void testComplexConditionWithStandaloneWhereAndTableAlias() {
314314

315315
WhereClauseProvider whereClause = where(id, isEqualTo(1), or(bodyWeight, isGreaterThan(1.0)))
316316
.build()
317-
.render(RenderingStrategies.MYBATIS3, TableAliasCalculator.of(animalData, "a"));
317+
.render(RenderingStrategies.MYBATIS3, ExplicitTableAliasCalculator.of(animalData, "a"));
318318

319319
assertThat(whereClause.getWhereClause()).isEqualTo("where (a.id = #{parameters.p1,jdbcType=INTEGER} or a.body_weight > #{parameters.p2,jdbcType=DOUBLE})");
320320

@@ -347,7 +347,8 @@ void testSelectRowsNotBetweenWithStandaloneWhereClauseAliasLimitAndOffset() {
347347

348348
WhereClauseProvider whereClause = where(id, isLessThan(60))
349349
.build()
350-
.render(RenderingStrategies.MYBATIS3, TableAliasCalculator.of(animalData, "b"), "whereClauseProvider");
350+
.render(RenderingStrategies.MYBATIS3, ExplicitTableAliasCalculator.of(animalData, "b"),
351+
"whereClauseProvider");
351352

352353
List<AnimalData> animals = mapper.selectWithWhereClauseAliasLimitAndOffset(whereClause, 3, 24);
353354
assertAll(

src/test/java/org/mybatis/dynamic/sql/mybatis3/CriterionRendererTest.java

Lines changed: 4 additions & 3 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.
@@ -28,6 +28,7 @@
2828
import org.mybatis.dynamic.sql.SqlBuilder;
2929
import org.mybatis.dynamic.sql.SqlColumn;
3030
import org.mybatis.dynamic.sql.SqlTable;
31+
import org.mybatis.dynamic.sql.render.ExplicitTableAliasCalculator;
3132
import org.mybatis.dynamic.sql.render.RenderingStrategies;
3233
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
3334
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
@@ -74,7 +75,7 @@ void testAliasWithoutIgnore() {
7475
CriterionRenderer renderer = new CriterionRenderer.Builder()
7576
.withSequence(sequence)
7677
.withRenderingStrategy(RenderingStrategies.MYBATIS3)
77-
.withTableAliasCalculator(TableAliasCalculator.of(tableAliases))
78+
.withTableAliasCalculator(ExplicitTableAliasCalculator.of(tableAliases))
7879
.build();
7980

8081
assertThat(criterion.accept(renderer)).hasValueSatisfying(rc -> {
@@ -127,7 +128,7 @@ void testTypeHandlerAndAlias() {
127128
CriterionRenderer renderer = new CriterionRenderer.Builder()
128129
.withSequence(sequence)
129130
.withRenderingStrategy(RenderingStrategies.MYBATIS3)
130-
.withTableAliasCalculator(TableAliasCalculator.of(tableAliases))
131+
.withTableAliasCalculator(ExplicitTableAliasCalculator.of(tableAliases))
131132
.build();
132133

133134
assertThat(criterion.accept(renderer)).hasValueSatisfying(rc -> {

src/test/java/org/mybatis/dynamic/sql/where/render/CriterionRendererTest.java

Lines changed: 3 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,6 +27,7 @@
2727
import org.mybatis.dynamic.sql.ColumnAndConditionCriterion;
2828
import org.mybatis.dynamic.sql.SqlColumn;
2929
import org.mybatis.dynamic.sql.SqlTable;
30+
import org.mybatis.dynamic.sql.render.ExplicitTableAliasCalculator;
3031
import org.mybatis.dynamic.sql.render.RenderingStrategies;
3132
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
3233
import org.mybatis.dynamic.sql.util.FragmentAndParameters;
@@ -74,7 +75,7 @@ void testAliasWithoutIgnore() {
7475
CriterionRenderer renderer = new CriterionRenderer.Builder()
7576
.withSequence(sequence)
7677
.withRenderingStrategy(RenderingStrategies.MYBATIS3)
77-
.withTableAliasCalculator(TableAliasCalculator.of(tableAliases))
78+
.withTableAliasCalculator(ExplicitTableAliasCalculator.of(tableAliases))
7879
.build();
7980

8081
assertThat(criterion.accept(renderer)).hasValueSatisfying(rc -> {

0 commit comments

Comments
 (0)