fix: update junit test

This commit is contained in:
Michael Yang 2024-03-08 10:57:22 +08:00
parent 4f53893012
commit 67981ec73b
6 changed files with 28 additions and 45 deletions

View File

@ -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()));
}

View File

@ -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;
/**

View File

@ -2321,22 +2321,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
return this;
}
/**
* 判断包含的where条件是否为空
* @return true: 为空 false: 不为空
*/
public boolean conditionIsEmpty(){
return whereQueryCondition == null || !whereQueryCondition.checkEffective();
}
/**
* 判断包含的where条件是否不为空
* @return true: 不为空 false: 为空
*/
public boolean conditionIsNotEmpty(){
return !conditionIsEmpty();
}
////////内部方法////////

View File

@ -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);
}

View File

@ -95,7 +95,7 @@ public class LogicDeleteTest {
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);
assertEquals("`user`.`role_id` = ? AND `user`.`deleted` = ?", whereSql);
Join join = CPI.getJoins(queryWrapper).get(0);
QueryCondition joinQueryCondition = CPI.getJoinQueryCondition(join);
@ -107,7 +107,5 @@ public class LogicDeleteTest {
QueryCondition joinCondition = CPI.getWhereQueryCondition(joinQueryWrapper);
String joinSql = joinCondition.toSql(Arrays.asList(userTable, roleTable), dialect);
assertEquals("(`user`.`role_id` = `role`.`id` OR `role`.`id` != ?) AND `role`.`deleted` = ?", joinSql);
}
}

View File

@ -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<Article> 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);
}