mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-08 17:48:25 +08:00
1 fix:gitee issues/I9163G 逻辑删除时 保证前面的条件被括号包裹。增加对应的单元测试。
2 修改BaseQueryWrapper的setWhereQueryCondition方法实际功能与方法名不同,原先调用处改为调用重载的addWhereQueryCondition。并且新增真正的setWhereQueryCondition用来替换whereQueryCondition字段。 3 QueryWrapper的join新增一个直接接受QueryTable的重载方法,用于自连接时可以重用一个声明了别名的QueryTable对象。 4 QueryWrapper增加conditionIsEmpty方法判断条件是否为空。 关联的增强提案: https://gitee.com/mybatis-flex/mybatis-flex/issues/I8HC0I
This commit is contained in:
parent
b3dfd39064
commit
fa63aa1d7f
@ -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);
|
||||
|
||||
@ -246,17 +246,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;
|
||||
}
|
||||
|
||||
@ -399,6 +399,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);
|
||||
@ -432,6 +435,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);
|
||||
}
|
||||
@ -464,6 +471,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);
|
||||
}
|
||||
@ -496,6 +507,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);
|
||||
}
|
||||
@ -528,6 +543,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);
|
||||
}
|
||||
@ -560,6 +579,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);
|
||||
}
|
||||
@ -2277,6 +2300,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