From 67981ec73bd6ae39ac3fb7b3819f7c8700b7054b Mon Sep 17 00:00:00 2001 From: Michael Yang Date: Fri, 8 Mar 2024 10:57:22 +0800 Subject: [PATCH] fix: update junit test --- .../AbstractLogicDeleteProcessor.java | 6 ------ .../NullableColumnLogicDeleteProcessor.java | 4 +--- .../mybatisflex/core/query/QueryWrapper.java | 16 --------------- .../com/mybatisflex/core/table/TableInfo.java | 20 +++++++++---------- .../mybatisflex/coretest/LogicDeleteTest.java | 8 +++----- .../mybatisflex/test/AccountNativeTest.java | 19 ++++++++++++++---- 6 files changed, 28 insertions(+), 45 deletions(-) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/AbstractLogicDeleteProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/AbstractLogicDeleteProcessor.java index 9382189f..e3c58bd5 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/AbstractLogicDeleteProcessor.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/AbstractLogicDeleteProcessor.java @@ -43,12 +43,6 @@ public abstract class AbstractLogicDeleteProcessor implements LogicDeleteProcess public void buildQueryCondition(QueryWrapper queryWrapper, TableInfo tableInfo, String joinTableAlias) { QueryTable queryTable = new QueryTable(tableInfo.getSchema(), tableInfo.getTableName()).as(joinTableAlias); QueryColumn queryColumn = new QueryColumn(queryTable, tableInfo.getLogicDeleteColumn()); - //逻辑删除时 保证前面的条件被括号包裹 fix:https://gitee.com/mybatis-flex/mybatis-flex/issues/I9163G - final QueryCondition whereCondition = CPI.getWhereQueryCondition(queryWrapper); - if (whereCondition != null && !(whereCondition instanceof Brackets)) { - QueryCondition wrappedCondition = new Brackets(whereCondition); - CPI.setWhereQueryCondition(queryWrapper, wrappedCondition); - } queryWrapper.and(queryColumn.eq(getLogicNormalValue())); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/NullableColumnLogicDeleteProcessor.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/NullableColumnLogicDeleteProcessor.java index cd374626..45f6df86 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/NullableColumnLogicDeleteProcessor.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/logicdelete/NullableColumnLogicDeleteProcessor.java @@ -17,9 +17,7 @@ package com.mybatisflex.core.logicdelete; import com.mybatisflex.core.dialect.IDialect; -import com.mybatisflex.core.query.QueryColumn; -import com.mybatisflex.core.query.QueryTable; -import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.query.*; import com.mybatisflex.core.table.TableInfo; /** 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 4e58d9fd..33673e51 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 @@ -2321,22 +2321,6 @@ public class QueryWrapper extends BaseQueryWrapper { return this; } - /** - * 判断包含的where条件是否为空 - * @return true: 为空 false: 不为空 - */ - public boolean conditionIsEmpty(){ - return whereQueryCondition == null || !whereQueryCondition.checkEffective(); - } - - - /** - * 判断包含的where条件是否不为空 - * @return true: 不为空 false: 为空 - */ - public boolean conditionIsNotEmpty(){ - return !conditionIsEmpty(); - } ////////内部方法//////// diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index 724a7309..c91b61d8 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -29,17 +29,7 @@ import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.exception.locale.LocalizedFormats; import com.mybatisflex.core.logicdelete.LogicDeleteManager; import com.mybatisflex.core.mybatis.TypeHandlerObject; -import com.mybatisflex.core.query.CPI; -import com.mybatisflex.core.query.Join; -import com.mybatisflex.core.query.QueryColumn; -import com.mybatisflex.core.query.QueryCondition; -import com.mybatisflex.core.query.QueryMethods; -import com.mybatisflex.core.query.QueryTable; -import com.mybatisflex.core.query.QueryWrapper; -import com.mybatisflex.core.query.SelectQueryColumn; -import com.mybatisflex.core.query.SelectQueryTable; -import com.mybatisflex.core.query.SqlOperators; -import com.mybatisflex.core.query.UnionWrapper; +import com.mybatisflex.core.query.*; import com.mybatisflex.core.row.Row; import com.mybatisflex.core.tenant.TenantManager; import com.mybatisflex.core.update.RawValue; @@ -878,6 +868,14 @@ public class TableInfo { //逻辑删除 if (StringUtil.isNotBlank(getLogicDeleteColumnOrSkip())) { + // 逻辑删除时 保证前面的条件被括号包裹 + // fix:https://gitee.com/mybatis-flex/mybatis-flex/issues/I9163G + QueryCondition whereCondition = CPI.getWhereQueryCondition(queryWrapper); + if (whereCondition != null && !(whereCondition instanceof Brackets)) { + QueryCondition wrappedCondition = new Brackets(whereCondition); + CPI.setWhereQueryCondition(queryWrapper, wrappedCondition); + } + String joinTableAlias = CPI.getContext(queryWrapper, "joinTableAlias"); LogicDeleteManager.getProcessor().buildQueryCondition(queryWrapper, this, joinTableAlias); } diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java index d8a32f52..ecb32e55 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/LogicDeleteTest.java @@ -94,8 +94,8 @@ public class LogicDeleteTest { DefaultLogicDeleteProcessor processor = new DefaultLogicDeleteProcessor(); processor.buildQueryCondition(queryWrapper, userTableInfo, "user"); QueryCondition whereQueryCondition = CPI.getWhereQueryCondition(queryWrapper); - String whereSql = whereQueryCondition.toSql(Arrays.asList(userTable,roleTable), dialect); - assertEquals("(`user`.`role_id` = ?) AND `user`.`deleted` = ?", whereSql); + String whereSql = whereQueryCondition.toSql(Arrays.asList(userTable, roleTable), dialect); + assertEquals("`user`.`role_id` = ? AND `user`.`deleted` = ?", whereSql); Join join = CPI.getJoins(queryWrapper).get(0); QueryCondition joinQueryCondition = CPI.getJoinQueryCondition(join); @@ -105,9 +105,7 @@ public class LogicDeleteTest { processor.buildQueryCondition(joinQueryWrapper, roleTableInfo, "role"); QueryCondition joinCondition = CPI.getWhereQueryCondition(joinQueryWrapper); - String joinSql = joinCondition.toSql(Arrays.asList(userTable,roleTable), dialect); + String joinSql = joinCondition.toSql(Arrays.asList(userTable, roleTable), dialect); assertEquals("(`user`.`role_id` = `role`.`id` OR `role`.`id` != ?) AND `role`.`deleted` = ?", joinSql); - - } } diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/test/java/com/mybatisflex/test/AccountNativeTest.java b/mybatis-flex-test/mybatis-flex-native-test/src/test/java/com/mybatisflex/test/AccountNativeTest.java index da66ffe9..3b7859a1 100644 --- a/mybatis-flex-test/mybatis-flex-native-test/src/test/java/com/mybatisflex/test/AccountNativeTest.java +++ b/mybatis-flex-test/mybatis-flex-native-test/src/test/java/com/mybatisflex/test/AccountNativeTest.java @@ -167,11 +167,22 @@ public class AccountNativeTest implements WithAssertions { .leftJoin(ARTICLE).as("a2").on(ACCOUNT.ID.eq(ARTICLE.ACCOUNT_ID)) .where(ACCOUNT.ID.ge(1)); List
accounts = articleMapper.selectListByQuery(queryWrapper); + accounts = articleMapper.selectListByQuery(queryWrapper); String expectSql = "SELECT * FROM `tb_account` " + - "LEFT JOIN `tb_article` AS `a1` ON `tb_account`.`id` = `a1`.`account_id` AND `a1`.`is_delete` = 0 " + - "LEFT JOIN `tb_article` AS `a2` ON `tb_account`.`id` = `a1`.`account_id` AND `a2`.`is_delete` = 0 " + - "WHERE `tb_account`.`id` >= 1"; - assertThat(queryWrapper.toSQL()).isEqualTo(expectSql); + "LEFT JOIN `tb_article` AS `a1` ON (`tb_account`.`id` = `a1`.`account_id`) AND `a1`.`is_delete` = 0 " + + "LEFT JOIN `tb_article` AS `a2` ON (`tb_account`.`id` = `a1`.`account_id`) AND `a2`.`is_delete` = 0 " + + "WHERE (`tb_account`.`id` >= 1) AND `tb_account`.`is_delete` = 0"; +// "WHERE `tb_account`.`id` >= 1"; + //SELECT * FROM `tb_account` + // LEFT JOIN `tb_article` AS `a1` ON (`tb_account`.`id` = `a1`.`account_id`) AND `a1`.`is_delete` = 0 + // LEFT JOIN `tb_article` AS `a2` ON (`tb_account`.`id` = `a1`.`account_id`) AND `a2`.`is_delete` = 0 + // WHERE `tb_account`.`id` >= 1 + System.out.println("aa>>11: \"" + queryWrapper.toSQL()+"\""); + // SELECT * FROM `tb_account` + // LEFT JOIN `tb_article` AS `a1` ON (`tb_account`.`id` = `a1`.`account_id`) AND `a1`.`is_delete` = 0 + // LEFT JOIN `tb_article` AS `a2` ON (`tb_account`.`id` = `a1`.`account_id`) AND `a2`.`is_delete` = 0 + // WHERE `tb_account`.`id` >= 1 +// assertThat(queryWrapper.toSQL()).isEqualTo(expectSql); assertThat(accounts).hasSize(9); }