Skip to content

Commit 1106b86

Browse files
authored
Merge pull request mybatis#489 from jeffgbutler/mybatisgh-488
Add Ability to Specify Table Aliases on DELETE and UPDATE Statements
2 parents 0fbdb6d + 8e84c3e commit 1106b86

File tree

19 files changed

+405
-45
lines changed

19 files changed

+405
-45
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
99
1. Added support for criteria groups without an initial criteria. This makes it possible to create an independent list
1010
of pre-created criteria and then add the list to a where clause. See the tests in the related pull request for
1111
usage examples. ([#462](https://github.com/mybatis/mybatis-dynamic-sql/pull/462))
12+
2. Added the ability to specify a table alias on DELETE and UPDATE statements.
13+
This is especially useful when working with a sub-query with an exists or not exists condition.
14+
([#489](https://github.com/mybatis/mybatis-dynamic-sql/pull/489))
1215

1316
## Release 1.4.0 - March 3, 2022
1417

src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ static DeleteDSL<DeleteModel> deleteFrom(SqlTable table) {
128128
return DeleteDSL.deleteFrom(table);
129129
}
130130

131+
static DeleteDSL<DeleteModel> deleteFrom(SqlTable table, String tableAlias) {
132+
return DeleteDSL.deleteFrom(table, tableAlias);
133+
}
134+
131135
static <T> InsertDSL.IntoGatherer<T> insert(T row) {
132136
return InsertDSL.insert(row);
133137
}
@@ -210,6 +214,10 @@ static UpdateDSL<UpdateModel> update(SqlTable table) {
210214
return UpdateDSL.update(table);
211215
}
212216

217+
static UpdateDSL<UpdateModel> update(SqlTable table, String tableAlias) {
218+
return UpdateDSL.update(table, tableAlias);
219+
}
220+
213221
static WhereDSL where() {
214222
return WhereDSL.where();
215223
}

src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java

Lines changed: 13 additions & 6 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.
@@ -29,10 +29,12 @@ public class DeleteDSL<R> extends AbstractWhereSupport<DeleteDSL<R>.DeleteWhereB
2929

3030
private final Function<DeleteModel, R> adapterFunction;
3131
private final SqlTable table;
32+
private final String tableAlias;
3233
private final DeleteWhereBuilder whereBuilder = new DeleteWhereBuilder();
3334

34-
private DeleteDSL(SqlTable table, Function<DeleteModel, R> adapterFunction) {
35+
private DeleteDSL(SqlTable table, String tableAlias, Function<DeleteModel, R> adapterFunction) {
3536
this.table = Objects.requireNonNull(table);
37+
this.tableAlias = tableAlias;
3638
this.adapterFunction = Objects.requireNonNull(adapterFunction);
3739
}
3840

@@ -42,7 +44,7 @@ public DeleteWhereBuilder where() {
4244
}
4345

4446
/**
45-
* WARNING! Calling this method could result in an delete statement that deletes
47+
* WARNING! Calling this method could result in a delete statement that deletes
4648
* all rows in a table.
4749
*
4850
* @return the model class
@@ -51,17 +53,22 @@ public DeleteWhereBuilder where() {
5153
@Override
5254
public R build() {
5355
DeleteModel deleteModel = DeleteModel.withTable(table)
56+
.withTableAlias(tableAlias)
5457
.withWhereModel(whereBuilder.buildWhereModel())
5558
.build();
5659
return adapterFunction.apply(deleteModel);
5760
}
5861

59-
public static <R> DeleteDSL<R> deleteFrom(Function<DeleteModel, R> adapterFunction, SqlTable table) {
60-
return new DeleteDSL<>(table, adapterFunction);
62+
public static <R> DeleteDSL<R> deleteFrom(Function<DeleteModel, R> adapterFunction, SqlTable table, String tableAlias) {
63+
return new DeleteDSL<>(table, tableAlias, adapterFunction);
6164
}
6265

6366
public static DeleteDSL<DeleteModel> deleteFrom(SqlTable table) {
64-
return deleteFrom(Function.identity(), table);
67+
return deleteFrom(Function.identity(), table, null);
68+
}
69+
70+
public static DeleteDSL<DeleteModel> deleteFrom(SqlTable table, String tableAlias) {
71+
return deleteFrom(Function.identity(), table, tableAlias);
6572
}
6673

6774
public class DeleteWhereBuilder extends AbstractWhereDSL<DeleteWhereBuilder> implements Buildable<R> {

src/main/java/org/mybatis/dynamic/sql/delete/DeleteModel.java

Lines changed: 13 additions & 1 deletion
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,17 +27,23 @@
2727

2828
public class DeleteModel {
2929
private final SqlTable table;
30+
private final String tableAlias;
3031
private final WhereModel whereModel;
3132

3233
private DeleteModel(Builder builder) {
3334
table = Objects.requireNonNull(builder.table);
3435
whereModel = builder.whereModel;
36+
tableAlias = builder.tableAlias;
3537
}
3638

3739
public SqlTable table() {
3840
return table;
3941
}
4042

43+
public Optional<String> tableAlias() {
44+
return Optional.ofNullable(tableAlias);
45+
}
46+
4147
public Optional<WhereModel> whereModel() {
4248
return Optional.ofNullable(whereModel);
4349
}
@@ -56,13 +62,19 @@ public static Builder withTable(SqlTable table) {
5662

5763
public static class Builder {
5864
private SqlTable table;
65+
private String tableAlias;
5966
private WhereModel whereModel;
6067

6168
public Builder withTable(SqlTable table) {
6269
this.table = table;
6370
return this;
6471
}
6572

73+
public Builder withTableAlias(String tableAlias) {
74+
this.tableAlias = tableAlias;
75+
return this;
76+
}
77+
6678
public Builder withWhereModel(WhereModel whereModel) {
6779
this.whereModel = whereModel;
6880
return this;

src/main/java/org/mybatis/dynamic/sql/delete/render/DeleteRenderer.java

Lines changed: 14 additions & 4 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.
@@ -21,7 +21,9 @@
2121
import java.util.Optional;
2222
import java.util.concurrent.atomic.AtomicInteger;
2323

24+
import org.mybatis.dynamic.sql.SqlTable;
2425
import org.mybatis.dynamic.sql.delete.DeleteModel;
26+
import org.mybatis.dynamic.sql.render.ExplicitTableAliasCalculator;
2527
import org.mybatis.dynamic.sql.render.RenderingStrategy;
2628
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
2729
import org.mybatis.dynamic.sql.where.WhereModel;
@@ -31,10 +33,14 @@
3133
public class DeleteRenderer {
3234
private final DeleteModel deleteModel;
3335
private final RenderingStrategy renderingStrategy;
36+
private final TableAliasCalculator tableAliasCalculator;
3437

3538
private DeleteRenderer(Builder builder) {
3639
deleteModel = Objects.requireNonNull(builder.deleteModel);
3740
renderingStrategy = Objects.requireNonNull(builder.renderingStrategy);
41+
tableAliasCalculator = builder.deleteModel.tableAlias()
42+
.map(a -> ExplicitTableAliasCalculator.of(deleteModel.table(), a))
43+
.orElseGet(TableAliasCalculator::empty);
3844
}
3945

4046
public DeleteStatementProvider render() {
@@ -56,8 +62,12 @@ private String calculateDeleteStatement(WhereClauseProvider whereClause) {
5662
}
5763

5864
private String calculateDeleteStatement() {
59-
return "delete from" //$NON-NLS-1$
60-
+ spaceBefore(deleteModel.table().tableNameAtRuntime());
65+
SqlTable table = deleteModel.table();
66+
String tableName = table.tableNameAtRuntime();
67+
String aliasedTableName = tableAliasCalculator.aliasForTable(table)
68+
.map(a -> tableName + " " + a).orElse(tableName); //$NON-NLS-1$
69+
70+
return "delete from" + spaceBefore(aliasedTableName); //$NON-NLS-1$
6171
}
6272

6373
private DeleteStatementProvider renderWithoutWhereClause() {
@@ -69,7 +79,7 @@ private Optional<WhereClauseProvider> renderWhereClause(WhereModel whereModel) {
6979
return WhereRenderer.withWhereModel(whereModel)
7080
.withRenderingStrategy(renderingStrategy)
7181
.withSequence(new AtomicInteger(1))
72-
.withTableAliasCalculator(TableAliasCalculator.empty())
82+
.withTableAliasCalculator(tableAliasCalculator)
7383
.build()
7484
.render();
7585
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ static TableAliasCalculator empty() {
2929
return new TableAliasCalculator() {
3030
@Override
3131
public Optional<String> aliasForColumn(SqlTable table) {
32-
return Optional.empty();
32+
return table.tableAlias();
3333
}
3434

3535
@Override
3636
public Optional<String> aliasForTable(SqlTable table) {
37-
return Optional.empty();
37+
return table.tableAlias();
3838
}
3939
};
4040
}

src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java

Lines changed: 12 additions & 5 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.
@@ -45,10 +45,12 @@ public class UpdateDSL<R> extends AbstractWhereSupport<UpdateDSL<R>.UpdateWhereB
4545
private final Function<UpdateModel, R> adapterFunction;
4646
private final List<AbstractColumnMapping> columnMappings = new ArrayList<>();
4747
private final SqlTable table;
48+
private final String tableAlias;
4849
private final UpdateWhereBuilder whereBuilder = new UpdateWhereBuilder();
4950

50-
private UpdateDSL(SqlTable table, Function<UpdateModel, R> adapterFunction) {
51+
private UpdateDSL(SqlTable table, String tableAlias, Function<UpdateModel, R> adapterFunction) {
5152
this.table = Objects.requireNonNull(table);
53+
this.tableAlias = tableAlias;
5254
this.adapterFunction = Objects.requireNonNull(adapterFunction);
5355
}
5456

@@ -71,18 +73,23 @@ public UpdateWhereBuilder where() {
7173
@Override
7274
public R build() {
7375
UpdateModel updateModel = UpdateModel.withTable(table)
76+
.withTableAlias(tableAlias)
7477
.withColumnMappings(columnMappings)
7578
.withWhereModel(whereBuilder.buildWhereModel())
7679
.build();
7780
return adapterFunction.apply(updateModel);
7881
}
7982

80-
public static <R> UpdateDSL<R> update(Function<UpdateModel, R> adapterFunction, SqlTable table) {
81-
return new UpdateDSL<>(table, adapterFunction);
83+
public static <R> UpdateDSL<R> update(Function<UpdateModel, R> adapterFunction, SqlTable table, String tableAlias) {
84+
return new UpdateDSL<>(table, tableAlias, adapterFunction);
8285
}
8386

8487
public static UpdateDSL<UpdateModel> update(SqlTable table) {
85-
return update(Function.identity(), table);
88+
return update(Function.identity(), table, null);
89+
}
90+
91+
public static UpdateDSL<UpdateModel> update(SqlTable table, String tableAlias) {
92+
return update(Function.identity(), table, tableAlias);
8693
}
8794

8895
public class SetClauseFinisher<T> {

src/main/java/org/mybatis/dynamic/sql/update/UpdateModel.java

Lines changed: 13 additions & 1 deletion
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.
@@ -32,19 +32,25 @@
3232

3333
public class UpdateModel {
3434
private final SqlTable table;
35+
private final String tableAlias;
3536
private final WhereModel whereModel;
3637
private final List<AbstractColumnMapping> columnMappings;
3738

3839
private UpdateModel(Builder builder) {
3940
table = Objects.requireNonNull(builder.table);
4041
whereModel = builder.whereModel;
4142
columnMappings = Objects.requireNonNull(builder.columnMappings);
43+
tableAlias = builder.tableAlias;
4244
}
4345

4446
public SqlTable table() {
4547
return table;
4648
}
4749

50+
public Optional<String> tableAlias() {
51+
return Optional.ofNullable(tableAlias);
52+
}
53+
4854
public Optional<WhereModel> whereModel() {
4955
return Optional.ofNullable(whereModel);
5056
}
@@ -67,6 +73,7 @@ public static Builder withTable(SqlTable table) {
6773

6874
public static class Builder {
6975
private SqlTable table;
76+
private String tableAlias;
7077
private WhereModel whereModel;
7178
private final List<AbstractColumnMapping> columnMappings = new ArrayList<>();
7279

@@ -75,6 +82,11 @@ public Builder withTable(SqlTable table) {
7582
return this;
7683
}
7784

85+
public Builder withTableAlias(String tableAlias) {
86+
this.tableAlias = tableAlias;
87+
return this;
88+
}
89+
7890
public Builder withColumnMappings(List<AbstractColumnMapping> columnMappings) {
7991
this.columnMappings.addAll(columnMappings);
8092
return this;

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy