From fa63aa1d7f214de7b0f4418395c7e040abc6507e Mon Sep 17 00:00:00 2001 From: SWQXDBA <983110853@qq.com> Date: Thu, 7 Mar 2024 16:52:21 +0800 Subject: [PATCH] =?UTF-8?q?1=20fix:gitee=20issues/I9163G=20=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=88=A0=E9=99=A4=E6=97=B6=20=E4=BF=9D=E8=AF=81?= =?UTF-8?q?=E5=89=8D=E9=9D=A2=E7=9A=84=E6=9D=A1=E4=BB=B6=E8=A2=AB=E6=8B=AC?= =?UTF-8?q?=E5=8F=B7=E5=8C=85=E8=A3=B9=E3=80=82=E5=A2=9E=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E5=BA=94=E7=9A=84=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 2 修改BaseQueryWrapper的setWhereQueryCondition方法实际功能与方法名不同,原先调用处改为调用重载的addWhereQueryCondition。并且新增真正的setWhereQueryCondition用来替换whereQueryCondition字段。 3 QueryWrapper的join新增一个直接接受QueryTable的重载方法,用于自连接时可以重用一个声明了别名的QueryTable对象。 4 QueryWrapper增加conditionIsEmpty方法判断条件是否为空。 关联的增强提案: https://gitee.com/mybatis-flex/mybatis-flex/issues/I8HC0I --- .../AbstractLogicDeleteProcessor.java | 10 ++-- .../core/query/BaseQueryWrapper.java | 6 ++- .../java/com/mybatisflex/core/query/CPI.java | 4 +- .../mybatisflex/core/query/QueryWrapper.java | 45 +++++++++++++++-- .../core/query/QueryWrapperAdapter.java | 33 ++++++++++++ .../mybatisflex/coretest/LogicDeleteTest.java | 50 +++++++++++++++++++ 6 files changed, 140 insertions(+), 8 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 7a00c5b7..9382189f 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 @@ -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())); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java index 83305084..3d7154b2 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java @@ -94,8 +94,12 @@ public class BaseQueryWrapper> 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); diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java index ee39690e..c333542f 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java @@ -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); 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 e6fe4648..e1c45916 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 @@ -246,17 +246,17 @@ public class QueryWrapper extends BaseQueryWrapper { } 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 { return this; } + public Joiner leftJoin(QueryTable table) { + return joining(SqlConsts.LEFT_JOIN, table, true); + } public Joiner leftJoin(String table) { return joining(SqlConsts.LEFT_JOIN, new QueryTable(table), true); @@ -432,6 +435,10 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(SqlConsts.LEFT_JOIN, table, when); } + public Joiner rightJoin(QueryTable table) { + return joining(SqlConsts.RIGHT_JOIN, table, true); + } + public Joiner rightJoin(String table) { return joining(SqlConsts.RIGHT_JOIN, new QueryTable(table), true); } @@ -464,6 +471,10 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(SqlConsts.RIGHT_JOIN, table, when); } + public Joiner innerJoin(QueryTable table) { + return joining(SqlConsts.INNER_JOIN, table, true); + } + public Joiner innerJoin(String table) { return joining(SqlConsts.INNER_JOIN, new QueryTable(table), true); } @@ -496,6 +507,10 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(SqlConsts.INNER_JOIN, table, when); } + public Joiner fullJoin(QueryTable table) { + return joining(SqlConsts.FULL_JOIN, table, true); + } + public Joiner fullJoin(String table) { return joining(SqlConsts.FULL_JOIN, new QueryTable(table), true); } @@ -528,6 +543,10 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(SqlConsts.FULL_JOIN, table, when); } + public Joiner crossJoin(QueryTable table) { + return joining(SqlConsts.CROSS_JOIN, table, true); + } + public Joiner crossJoin(String table) { return joining(SqlConsts.CROSS_JOIN, new QueryTable(table), true); } @@ -560,6 +579,10 @@ public class QueryWrapper extends BaseQueryWrapper { return joining(SqlConsts.CROSS_JOIN, table, when); } + public Joiner join(QueryTable table) { + return joining(SqlConsts.JOIN, table, true); + } + public Joiner join(String table) { return joining(SqlConsts.JOIN, new QueryTable(table), true); } @@ -2277,6 +2300,22 @@ 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/query/QueryWrapperAdapter.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapperAdapter.java index 33919eb9..4a3fd927 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapperAdapter.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapperAdapter.java @@ -268,6 +268,13 @@ public class QueryWrapperAdapter> extends Query return (R) this; } + @Override + public Joiner leftJoin(QueryTable table) { + return super.leftJoin(table); + } + + + @Override public Joiner leftJoin(String table) { return super.leftJoin(table); @@ -308,6 +315,12 @@ public class QueryWrapperAdapter> extends Query return super.leftJoin(table, when); } + @Override + public Joiner rightJoin(QueryTable table) { + return super.rightJoin(table); + } + + @Override public Joiner rightJoin(String table) { return super.rightJoin(table); @@ -348,6 +361,11 @@ public class QueryWrapperAdapter> extends Query return super.rightJoin(table, when); } + @Override + public Joiner innerJoin(QueryTable table) { + return super.innerJoin(table); + } + @Override public Joiner innerJoin(String table) { return super.innerJoin(table); @@ -388,6 +406,11 @@ public class QueryWrapperAdapter> extends Query return super.innerJoin(table, when); } + @Override + public Joiner fullJoin(QueryTable table) { + return super.fullJoin(table); + } + @Override public Joiner fullJoin(String table) { return super.fullJoin(table); @@ -428,6 +451,11 @@ public class QueryWrapperAdapter> extends Query return super.fullJoin(table, when); } + @Override + public Joiner crossJoin(QueryTable table) { + return super.crossJoin(table); + } + @Override public Joiner crossJoin(String table) { return super.crossJoin(table); @@ -468,6 +496,11 @@ public class QueryWrapperAdapter> extends Query return super.crossJoin(table, when); } + @Override + public Joiner join(QueryTable table) { + return super.join(table); + } + @Override public Joiner join(String table) { return super.join(table); 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 5109283e..d8a32f52 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 @@ -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); + + + } }