From 3df467ef88e7d411ce4324541daa42b2d9c47252 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Thu, 26 Oct 2023 22:28:40 +0800 Subject: [PATCH] fix: close #I8ASWS --- .../core/dialect/impl/CommonsDialectImpl.java | 17 ++++++++++++++ .../mybatisflex/core/query/QueryWrapper.java | 3 +-- .../coretest/AccountSqlTester.java | 23 +++++++++++++++---- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java index 8c686e0b..165f8b44 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/dialect/impl/CommonsDialectImpl.java @@ -491,6 +491,23 @@ public class CommonsDialectImpl implements IDialect { if (StringUtil.isNotBlank(hint)) { sqlBuilder.append(BLANK).append(hint).deleteCharAt(sqlBuilder.length() - 1); } + + //delete with join + if (joinTables != null && !joinTables.isEmpty()) { + if (queryTables == null || queryTables.isEmpty()) { + throw new IllegalArgumentException("Delete with join sql must designate the from table."); + } else if (queryTables.size() != 1) { + throw new IllegalArgumentException("Delete with join sql must has 1 table only. but current has " + queryTables.size()); + } + QueryTable queryTable = queryTables.get(0); + String table = getRealTable(queryTable.getName()); + if (StringUtil.isNotBlank(queryTable.getSchema())) { + sqlBuilder.append(wrap(getRealSchema(queryTable.getSchema(), table))).append(REFERENCE); + } + sqlBuilder.append(BLANK).append(wrap(getRealTable(table))); + } + + sqlBuilder.append(FROM).append(StringUtil.join(DELIMITER, queryTables, queryTable -> queryTable.toSql(this))); buildJoinSql(sqlBuilder, queryWrapper, allTables); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java index 4f260851..37833cf3 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java @@ -125,7 +125,7 @@ public class QueryWrapper extends BaseQueryWrapper { } public QueryWrapper select(LambdaGetter... lambdaGetters) { - for (LambdaGetter lambdaGetter : lambdaGetters) { + for (LambdaGetter lambdaGetter : lambdaGetters) { QueryColumn queryColumn = LambdaUtil.getQueryColumn(lambdaGetter); addSelectColumn(queryColumn); } @@ -660,7 +660,6 @@ public class QueryWrapper extends BaseQueryWrapper { addGroupByColumns(LambdaUtil.getQueryColumn(column)); return this; } - public QueryWrapper groupBy(LambdaGetter... columns) { for (LambdaGetter column : columns) { groupBy(LambdaUtil.getQueryColumn(column)); diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java index f0c0ca6b..ab356574 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java @@ -21,10 +21,7 @@ import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.dialect.KeywordWrap; import com.mybatisflex.core.dialect.LimitOffsetProcessor; import com.mybatisflex.core.dialect.impl.CommonsDialectImpl; -import com.mybatisflex.core.query.DistinctQueryColumn; -import com.mybatisflex.core.query.QueryWrapper; -import com.mybatisflex.core.query.RawQueryColumn; -import com.mybatisflex.core.query.SqlOperators; +import com.mybatisflex.core.query.*; import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.table.TableManager; @@ -560,6 +557,24 @@ public class AccountSqlTester { System.out.println(sql); } + + /** + * test https://gitee.com/mybatis-flex/mybatis-flex/issues/I8ASWS + */ + @Test + public void testDeleteWithJoin() { + QueryWrapper qw = QueryWrapper.create() + .from(ACCOUNT).leftJoin(ARTICLE).on(ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID)) + .where(ACCOUNT.USER_NAME.eq("x")); + IDialect dialect = new CommonsDialectImpl(); + String sql = dialect.forDeleteByQuery(qw); + Assert.assertEquals("DELETE `tb_account` FROM `tb_account` " + + "LEFT JOIN `tb_article` ON `tb_account`.`id` = `tb_article`.`account_id` " + + "WHERE `tb_account`.`user_name` = ?" + ,sql); + System.out.println(sql); + } + @Test public void testForUpdate() { IDialect dialect = new CommonsDialectImpl();