mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +08:00
!432 修复逻辑删除条件问题 新增queryWrapper判断是否条件为空方法等
Merge pull request !432 from SWQXDBA/logic-delete-fix
This commit is contained in:
commit
4f53893012
@ -16,9 +16,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;
|
||||
|
||||
import static com.mybatisflex.core.constant.SqlConsts.EQUALS;
|
||||
@ -45,6 +43,12 @@ 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()));
|
||||
}
|
||||
|
||||
|
||||
@ -94,8 +94,12 @@ public class BaseQueryWrapper<T extends BaseQueryWrapper<T>> implements CloneSup
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
|
||||
protected T setWhereQueryCondition(QueryCondition queryCondition) {
|
||||
whereQueryCondition = queryCondition;
|
||||
return (T) this;
|
||||
}
|
||||
|
||||
protected T addWhereQueryCondition(QueryCondition queryCondition) {
|
||||
if (queryCondition != null) {
|
||||
if (whereQueryCondition != null) {
|
||||
queryCondition.connect(whereQueryCondition, SqlConnector.AND);
|
||||
|
||||
@ -145,10 +145,12 @@ public class CPI {
|
||||
public static QueryCondition getWhereQueryCondition(QueryWrapper queryWrapper) {
|
||||
return queryWrapper.getWhereQueryCondition();
|
||||
}
|
||||
|
||||
public static void setWhereQueryCondition(QueryWrapper queryWrapper, QueryCondition queryCondition) {
|
||||
queryWrapper.setWhereQueryCondition(queryCondition);
|
||||
}
|
||||
public static void addWhereQueryCondition(QueryWrapper queryWrapper, QueryCondition queryCondition) {
|
||||
queryWrapper.addWhereQueryCondition(queryCondition);
|
||||
}
|
||||
|
||||
public static void addWhereQueryCondition(QueryWrapper queryWrapper, QueryCondition queryCondition, SqlConnector connector) {
|
||||
queryWrapper.addWhereQueryCondition(queryCondition, connector);
|
||||
|
||||
@ -266,17 +266,17 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
}
|
||||
|
||||
public QueryWrapper where(QueryCondition queryCondition) {
|
||||
this.setWhereQueryCondition(queryCondition);
|
||||
this.addWhereQueryCondition(queryCondition);
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryWrapper where(String sql) {
|
||||
this.setWhereQueryCondition(new RawQueryCondition(sql));
|
||||
this.addWhereQueryCondition(new RawQueryCondition(sql));
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryWrapper where(String sql, Object... params) {
|
||||
this.setWhereQueryCondition(new RawQueryCondition(sql, params));
|
||||
this.addWhereQueryCondition(new RawQueryCondition(sql, params));
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -419,6 +419,9 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public <Q extends QueryWrapper> Joiner<Q> leftJoin(QueryTable table) {
|
||||
return joining(SqlConsts.LEFT_JOIN, table, true);
|
||||
}
|
||||
|
||||
public <Q extends QueryWrapper> Joiner<Q> leftJoin(String table) {
|
||||
return joining(SqlConsts.LEFT_JOIN, new QueryTable(table), true);
|
||||
@ -452,6 +455,10 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(SqlConsts.LEFT_JOIN, table, when);
|
||||
}
|
||||
|
||||
public <Q extends QueryWrapper> Joiner<Q> rightJoin(QueryTable table) {
|
||||
return joining(SqlConsts.RIGHT_JOIN, table, true);
|
||||
}
|
||||
|
||||
public <Q extends QueryWrapper> Joiner<Q> rightJoin(String table) {
|
||||
return joining(SqlConsts.RIGHT_JOIN, new QueryTable(table), true);
|
||||
}
|
||||
@ -484,6 +491,10 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(SqlConsts.RIGHT_JOIN, table, when);
|
||||
}
|
||||
|
||||
public <Q extends QueryWrapper> Joiner<Q> innerJoin(QueryTable table) {
|
||||
return joining(SqlConsts.INNER_JOIN, table, true);
|
||||
}
|
||||
|
||||
public <Q extends QueryWrapper> Joiner<Q> innerJoin(String table) {
|
||||
return joining(SqlConsts.INNER_JOIN, new QueryTable(table), true);
|
||||
}
|
||||
@ -516,6 +527,10 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(SqlConsts.INNER_JOIN, table, when);
|
||||
}
|
||||
|
||||
public <Q extends QueryWrapper> Joiner<Q> fullJoin(QueryTable table) {
|
||||
return joining(SqlConsts.FULL_JOIN, table, true);
|
||||
}
|
||||
|
||||
public <Q extends QueryWrapper> Joiner<Q> fullJoin(String table) {
|
||||
return joining(SqlConsts.FULL_JOIN, new QueryTable(table), true);
|
||||
}
|
||||
@ -548,6 +563,10 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(SqlConsts.FULL_JOIN, table, when);
|
||||
}
|
||||
|
||||
public <Q extends QueryWrapper> Joiner<Q> crossJoin(QueryTable table) {
|
||||
return joining(SqlConsts.CROSS_JOIN, table, true);
|
||||
}
|
||||
|
||||
public <Q extends QueryWrapper> Joiner<Q> crossJoin(String table) {
|
||||
return joining(SqlConsts.CROSS_JOIN, new QueryTable(table), true);
|
||||
}
|
||||
@ -580,6 +599,10 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return joining(SqlConsts.CROSS_JOIN, table, when);
|
||||
}
|
||||
|
||||
public <Q extends QueryWrapper> Joiner<Q> join(QueryTable table) {
|
||||
return joining(SqlConsts.JOIN, table, true);
|
||||
}
|
||||
|
||||
public <Q extends QueryWrapper> Joiner<Q> join(String table) {
|
||||
return joining(SqlConsts.JOIN, new QueryTable(table), true);
|
||||
}
|
||||
@ -2298,6 +2321,22 @@ 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();
|
||||
}
|
||||
|
||||
////////内部方法////////
|
||||
|
||||
|
||||
@ -268,6 +268,13 @@ public class QueryWrapperAdapter<R extends QueryWrapperAdapter<R>> extends Query
|
||||
return (R) this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <Q extends QueryWrapper> Joiner<Q> leftJoin(QueryTable table) {
|
||||
return super.leftJoin(table);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public Joiner<R> leftJoin(String table) {
|
||||
return super.leftJoin(table);
|
||||
@ -308,6 +315,12 @@ public class QueryWrapperAdapter<R extends QueryWrapperAdapter<R>> extends Query
|
||||
return super.leftJoin(table, when);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <Q extends QueryWrapper> Joiner<Q> rightJoin(QueryTable table) {
|
||||
return super.rightJoin(table);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Joiner<R> rightJoin(String table) {
|
||||
return super.rightJoin(table);
|
||||
@ -348,6 +361,11 @@ public class QueryWrapperAdapter<R extends QueryWrapperAdapter<R>> extends Query
|
||||
return super.rightJoin(table, when);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <Q extends QueryWrapper> Joiner<Q> innerJoin(QueryTable table) {
|
||||
return super.innerJoin(table);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Joiner<R> innerJoin(String table) {
|
||||
return super.innerJoin(table);
|
||||
@ -388,6 +406,11 @@ public class QueryWrapperAdapter<R extends QueryWrapperAdapter<R>> extends Query
|
||||
return super.innerJoin(table, when);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <Q extends QueryWrapper> Joiner<Q> fullJoin(QueryTable table) {
|
||||
return super.fullJoin(table);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Joiner<R> fullJoin(String table) {
|
||||
return super.fullJoin(table);
|
||||
@ -428,6 +451,11 @@ public class QueryWrapperAdapter<R extends QueryWrapperAdapter<R>> extends Query
|
||||
return super.fullJoin(table, when);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <Q extends QueryWrapper> Joiner<Q> crossJoin(QueryTable table) {
|
||||
return super.crossJoin(table);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Joiner<R> crossJoin(String table) {
|
||||
return super.crossJoin(table);
|
||||
@ -468,6 +496,11 @@ public class QueryWrapperAdapter<R extends QueryWrapperAdapter<R>> extends Query
|
||||
return super.crossJoin(table, when);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <Q extends QueryWrapper> Joiner<Q> join(QueryTable table) {
|
||||
return super.join(table);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Joiner<R> join(String table) {
|
||||
return super.join(table);
|
||||
|
||||
@ -18,13 +18,19 @@ package com.mybatisflex.coretest;
|
||||
|
||||
import com.mybatisflex.core.dialect.DialectFactory;
|
||||
import com.mybatisflex.core.dialect.IDialect;
|
||||
import com.mybatisflex.core.logicdelete.LogicDeleteManager;
|
||||
import com.mybatisflex.core.logicdelete.LogicDeleteProcessor;
|
||||
import com.mybatisflex.core.logicdelete.impl.*;
|
||||
import com.mybatisflex.core.query.*;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* 逻辑删除测试。
|
||||
*
|
||||
@ -35,6 +41,7 @@ import static org.junit.Assert.assertEquals;
|
||||
public class LogicDeleteTest {
|
||||
|
||||
private final String logicColumn = "deleted";
|
||||
|
||||
private final IDialect dialect = DialectFactory.getDialect();
|
||||
|
||||
@Test
|
||||
@ -60,4 +67,47 @@ public class LogicDeleteTest {
|
||||
assertEquals(actualLogicNormalCondition, logicNormalCondition);
|
||||
}
|
||||
|
||||
|
||||
//逻辑删除时 保证前面的条件被括号包裹
|
||||
//https://gitee.com/mybatis-flex/mybatis-flex/issues/I9163G
|
||||
@Test
|
||||
public void giteeIssueI9163G() {
|
||||
TableInfo userTableInfo = new TableInfo();
|
||||
userTableInfo.setTableName("user");
|
||||
userTableInfo.setLogicDeleteColumn("deleted");
|
||||
QueryTable userTable = new QueryTable("user");
|
||||
QueryColumn userRoleId = new QueryColumn(userTable, "role_id");
|
||||
|
||||
TableInfo roleTableInfo = new TableInfo();
|
||||
roleTableInfo.setTableName("role");
|
||||
roleTableInfo.setLogicDeleteColumn("deleted");
|
||||
QueryTable roleTable = new QueryTable("role");
|
||||
QueryColumn roleId = new QueryColumn(roleTable, "id");
|
||||
|
||||
|
||||
QueryWrapper queryWrapper = new QueryWrapper()
|
||||
.select("1")
|
||||
.from(userTable)
|
||||
.leftJoin(roleTable).on(userRoleId.eq(roleId).or(roleId.ne(0)))
|
||||
.where(userRoleId.eq(1));
|
||||
|
||||
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);
|
||||
|
||||
Join join = CPI.getJoins(queryWrapper).get(0);
|
||||
QueryCondition joinQueryCondition = CPI.getJoinQueryCondition(join);
|
||||
|
||||
QueryWrapper joinQueryWrapper = QueryWrapper.create()
|
||||
.where(joinQueryCondition);
|
||||
processor.buildQueryCondition(joinQueryWrapper, roleTableInfo, "role");
|
||||
|
||||
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);
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user