Skip to content

Add Ability to Specify Table Aliases on DELETE and UPDATE Statements #489

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 5 commits into from
May 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ GitHub milestone: [https://github.com/mybatis/mybatis-dynamic-sql/issues?q=miles
1. Added support for criteria groups without an initial criteria. This makes it possible to create an independent list
of pre-created criteria and then add the list to a where clause. See the tests in the related pull request for
usage examples. ([#462](https://github.com/mybatis/mybatis-dynamic-sql/pull/462))
2. Added the ability to specify a table alias on DELETE and UPDATE statements.
This is especially useful when working with a sub-query with an exists or not exists condition.
([#489](https://github.com/mybatis/mybatis-dynamic-sql/pull/489))

## Release 1.4.0 - March 3, 2022

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/mybatis/dynamic/sql/SqlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ static DeleteDSL<DeleteModel> deleteFrom(SqlTable table) {
return DeleteDSL.deleteFrom(table);
}

static DeleteDSL<DeleteModel> deleteFrom(SqlTable table, String tableAlias) {
return DeleteDSL.deleteFrom(table, tableAlias);
}

static <T> InsertDSL.IntoGatherer<T> insert(T row) {
return InsertDSL.insert(row);
}
Expand Down Expand Up @@ -210,6 +214,10 @@ static UpdateDSL<UpdateModel> update(SqlTable table) {
return UpdateDSL.update(table);
}

static UpdateDSL<UpdateModel> update(SqlTable table, String tableAlias) {
return UpdateDSL.update(table, tableAlias);
}

static WhereDSL where() {
return WhereDSL.where();
}
Expand Down
19 changes: 13 additions & 6 deletions src/main/java/org/mybatis/dynamic/sql/delete/DeleteDSL.java
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 @@ -29,10 +29,12 @@ public class DeleteDSL<R> extends AbstractWhereSupport<DeleteDSL<R>.DeleteWhereB

private final Function<DeleteModel, R> adapterFunction;
private final SqlTable table;
private final String tableAlias;
private final DeleteWhereBuilder whereBuilder = new DeleteWhereBuilder();

private DeleteDSL(SqlTable table, Function<DeleteModel, R> adapterFunction) {
private DeleteDSL(SqlTable table, String tableAlias, Function<DeleteModel, R> adapterFunction) {
this.table = Objects.requireNonNull(table);
this.tableAlias = tableAlias;
this.adapterFunction = Objects.requireNonNull(adapterFunction);
}

Expand All @@ -42,7 +44,7 @@ public DeleteWhereBuilder where() {
}

/**
* WARNING! Calling this method could result in an delete statement that deletes
* WARNING! Calling this method could result in a delete statement that deletes
* all rows in a table.
*
* @return the model class
Expand All @@ -51,17 +53,22 @@ public DeleteWhereBuilder where() {
@Override
public R build() {
DeleteModel deleteModel = DeleteModel.withTable(table)
.withTableAlias(tableAlias)
.withWhereModel(whereBuilder.buildWhereModel())
.build();
return adapterFunction.apply(deleteModel);
}

public static <R> DeleteDSL<R> deleteFrom(Function<DeleteModel, R> adapterFunction, SqlTable table) {
return new DeleteDSL<>(table, adapterFunction);
public static <R> DeleteDSL<R> deleteFrom(Function<DeleteModel, R> adapterFunction, SqlTable table, String tableAlias) {
return new DeleteDSL<>(table, tableAlias, adapterFunction);
}

public static DeleteDSL<DeleteModel> deleteFrom(SqlTable table) {
return deleteFrom(Function.identity(), table);
return deleteFrom(Function.identity(), table, null);
}

public static DeleteDSL<DeleteModel> deleteFrom(SqlTable table, String tableAlias) {
return deleteFrom(Function.identity(), table, tableAlias);
}

public class DeleteWhereBuilder extends AbstractWhereDSL<DeleteWhereBuilder> implements Buildable<R> {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/mybatis/dynamic/sql/delete/DeleteModel.java
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,17 +27,23 @@

public class DeleteModel {
private final SqlTable table;
private final String tableAlias;
private final WhereModel whereModel;

private DeleteModel(Builder builder) {
table = Objects.requireNonNull(builder.table);
whereModel = builder.whereModel;
tableAlias = builder.tableAlias;
}

public SqlTable table() {
return table;
}

public Optional<String> tableAlias() {
return Optional.ofNullable(tableAlias);
}

public Optional<WhereModel> whereModel() {
return Optional.ofNullable(whereModel);
}
Expand All @@ -56,13 +62,19 @@ public static Builder withTable(SqlTable table) {

public static class Builder {
private SqlTable table;
private String tableAlias;
private WhereModel whereModel;

public Builder withTable(SqlTable table) {
this.table = table;
return this;
}

public Builder withTableAlias(String tableAlias) {
this.tableAlias = tableAlias;
return this;
}

public Builder withWhereModel(WhereModel whereModel) {
this.whereModel = whereModel;
return this;
Expand Down
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 All @@ -21,7 +21,9 @@
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;

import org.mybatis.dynamic.sql.SqlTable;
import org.mybatis.dynamic.sql.delete.DeleteModel;
import org.mybatis.dynamic.sql.render.ExplicitTableAliasCalculator;
import org.mybatis.dynamic.sql.render.RenderingStrategy;
import org.mybatis.dynamic.sql.render.TableAliasCalculator;
import org.mybatis.dynamic.sql.where.WhereModel;
Expand All @@ -31,10 +33,14 @@
public class DeleteRenderer {
private final DeleteModel deleteModel;
private final RenderingStrategy renderingStrategy;
private final TableAliasCalculator tableAliasCalculator;

private DeleteRenderer(Builder builder) {
deleteModel = Objects.requireNonNull(builder.deleteModel);
renderingStrategy = Objects.requireNonNull(builder.renderingStrategy);
tableAliasCalculator = builder.deleteModel.tableAlias()
.map(a -> ExplicitTableAliasCalculator.of(deleteModel.table(), a))
.orElseGet(TableAliasCalculator::empty);
}

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

private String calculateDeleteStatement() {
return "delete from" //$NON-NLS-1$
+ spaceBefore(deleteModel.table().tableNameAtRuntime());
SqlTable table = deleteModel.table();
String tableName = table.tableNameAtRuntime();
String aliasedTableName = tableAliasCalculator.aliasForTable(table)
.map(a -> tableName + " " + a).orElse(tableName); //$NON-NLS-1$

return "delete from" + spaceBefore(aliasedTableName); //$NON-NLS-1$
}

private DeleteStatementProvider renderWithoutWhereClause() {
Expand All @@ -69,7 +79,7 @@ private Optional<WhereClauseProvider> renderWhereClause(WhereModel whereModel) {
return WhereRenderer.withWhereModel(whereModel)
.withRenderingStrategy(renderingStrategy)
.withSequence(new AtomicInteger(1))
.withTableAliasCalculator(TableAliasCalculator.empty())
.withTableAliasCalculator(tableAliasCalculator)
.build()
.render();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ static TableAliasCalculator empty() {
return new TableAliasCalculator() {
@Override
public Optional<String> aliasForColumn(SqlTable table) {
return Optional.empty();
return table.tableAlias();
}

@Override
public Optional<String> aliasForTable(SqlTable table) {
return Optional.empty();
return table.tableAlias();
}
};
}
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/org/mybatis/dynamic/sql/update/UpdateDSL.java
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 @@ -45,10 +45,12 @@ public class UpdateDSL<R> extends AbstractWhereSupport<UpdateDSL<R>.UpdateWhereB
private final Function<UpdateModel, R> adapterFunction;
private final List<AbstractColumnMapping> columnMappings = new ArrayList<>();
private final SqlTable table;
private final String tableAlias;
private final UpdateWhereBuilder whereBuilder = new UpdateWhereBuilder();

private UpdateDSL(SqlTable table, Function<UpdateModel, R> adapterFunction) {
private UpdateDSL(SqlTable table, String tableAlias, Function<UpdateModel, R> adapterFunction) {
this.table = Objects.requireNonNull(table);
this.tableAlias = tableAlias;
this.adapterFunction = Objects.requireNonNull(adapterFunction);
}

Expand All @@ -71,18 +73,23 @@ public UpdateWhereBuilder where() {
@Override
public R build() {
UpdateModel updateModel = UpdateModel.withTable(table)
.withTableAlias(tableAlias)
.withColumnMappings(columnMappings)
.withWhereModel(whereBuilder.buildWhereModel())
.build();
return adapterFunction.apply(updateModel);
}

public static <R> UpdateDSL<R> update(Function<UpdateModel, R> adapterFunction, SqlTable table) {
return new UpdateDSL<>(table, adapterFunction);
public static <R> UpdateDSL<R> update(Function<UpdateModel, R> adapterFunction, SqlTable table, String tableAlias) {
return new UpdateDSL<>(table, tableAlias, adapterFunction);
}

public static UpdateDSL<UpdateModel> update(SqlTable table) {
return update(Function.identity(), table);
return update(Function.identity(), table, null);
}

public static UpdateDSL<UpdateModel> update(SqlTable table, String tableAlias) {
return update(Function.identity(), table, tableAlias);
}

public class SetClauseFinisher<T> {
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/org/mybatis/dynamic/sql/update/UpdateModel.java
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 @@ -32,19 +32,25 @@

public class UpdateModel {
private final SqlTable table;
private final String tableAlias;
private final WhereModel whereModel;
private final List<AbstractColumnMapping> columnMappings;

private UpdateModel(Builder builder) {
table = Objects.requireNonNull(builder.table);
whereModel = builder.whereModel;
columnMappings = Objects.requireNonNull(builder.columnMappings);
tableAlias = builder.tableAlias;
}

public SqlTable table() {
return table;
}

public Optional<String> tableAlias() {
return Optional.ofNullable(tableAlias);
}

public Optional<WhereModel> whereModel() {
return Optional.ofNullable(whereModel);
}
Expand All @@ -67,6 +73,7 @@ public static Builder withTable(SqlTable table) {

public static class Builder {
private SqlTable table;
private String tableAlias;
private WhereModel whereModel;
private final List<AbstractColumnMapping> columnMappings = new ArrayList<>();

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

public Builder withTableAlias(String tableAlias) {
this.tableAlias = tableAlias;
return this;
}

public Builder withColumnMappings(List<AbstractColumnMapping> columnMappings) {
this.columnMappings.addAll(columnMappings);
return this;
Expand Down
Loading