Selaa lähdekoodia

fix: issues#6075 多租户查询出现问题 (#6091)

https://github.com/baomidou/mybatis-plus/issues/6075

Co-authored-by: wangqibo <wangqibo@51etc.com>
WangZhonghui 1 vuosi sitten
vanhempi
commit
3673c7a74d

+ 15 - 7
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/BaseMultiTableInnerInterceptor.java

@@ -24,6 +24,7 @@ import lombok.ToString;
 import net.sf.jsqlparser.expression.*;
 import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
 import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
+import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
 import net.sf.jsqlparser.expression.operators.relational.ExistsExpression;
 import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
 import net.sf.jsqlparser.expression.operators.relational.InExpression;
@@ -105,7 +106,7 @@ public abstract class BaseMultiTableInnerInterceptor extends JsqlParserSupport i
         // 处理 join
         List<Join> joins = plainSelect.getJoins();
         if (CollectionUtils.isNotEmpty(joins)) {
-             processJoins(mainTables, joins, whereSegment);
+            processJoins(mainTables, joins, whereSegment);
         }
 
         // 当有 mainTable 时,进行 where 条件追加
@@ -125,7 +126,7 @@ public abstract class BaseMultiTableInnerInterceptor extends JsqlParserSupport i
         if (fromItem instanceof Table) {
             Table fromTable = (Table) fromItem;
             mainTables.add(fromTable);
-        } else if (fromItem instanceof ParenthesedFromItem ) {
+        } else if (fromItem instanceof ParenthesedFromItem) {
             // SubJoin 类型则还需要添加上 where 条件
             List<Table> tables = processSubJoin((ParenthesedFromItem) fromItem, whereSegment);
             mainTables.addAll(tables);
@@ -219,6 +220,13 @@ public abstract class BaseMultiTableInnerInterceptor extends JsqlParserSupport i
                     processSelectBody(((Select) expression), whereSegment);
                 } else if (expression instanceof Function) {
                     processFunction((Function) expression, whereSegment);
+                } else if (expression instanceof EqualsTo) {
+                    if (((EqualsTo) expression).getLeftExpression() instanceof Select) {
+                        processSelectBody(((Select) ((EqualsTo) expression).getLeftExpression()), whereSegment);
+                    }
+                    if (((EqualsTo) expression).getRightExpression() instanceof Select) {
+                        processSelectBody(((Select) ((EqualsTo) expression).getRightExpression()), whereSegment);
+                    }
                 }
             });
         }
@@ -289,8 +297,8 @@ public abstract class BaseMultiTableInnerInterceptor extends JsqlParserSupport i
             if (joinItem instanceof Table) {
                 joinTables = new ArrayList<>();
                 joinTables.add((Table) joinItem);
-            } else if (joinItem instanceof ParenthesedFromItem ) {
-                joinTables = processSubJoin((ParenthesedFromItem ) joinItem, whereSegment);
+            } else if (joinItem instanceof ParenthesedFromItem) {
+                joinTables = processSubJoin((ParenthesedFromItem) joinItem, whereSegment);
             }
 
             if (joinTables != null) {
@@ -373,9 +381,9 @@ public abstract class BaseMultiTableInnerInterceptor extends JsqlParserSupport i
         }
         // 构造每张表的条件
         List<Expression> expressions = tables.stream()
-            .map(item -> buildTableExpression(item, currentExpression, whereSegment))
-            .filter(Objects::nonNull)
-            .collect(Collectors.toList());
+                .map(item -> buildTableExpression(item, currentExpression, whereSegment))
+                .filter(Objects::nonNull)
+                .collect(Collectors.toList());
 
         // 没有表需要处理直接返回
         if (CollectionUtils.isEmpty(expressions)) {

+ 6 - 0
mybatis-plus-extension/src/test/java/com/baomidou/mybatisplus/extension/plugins/inner/TenantLineInnerInterceptorTest.java

@@ -475,6 +475,12 @@ class TenantLineInnerInterceptorTest {
             "INSERT INTO entity (name, age, tenant_id) VALUES ('秋秋', 18, 1), ('秋秋', '22', 1) ON DUPLICATE KEY UPDATE age = 18, tenant_id = 1");
     }
 
+    @Test
+    void test6075() {
+        String sql = "Select a.*, b.*, IF((Select c.value From C as c Where c.code = \"1\") = 1, c.type, a.type) as type from A as a, B as b Where a.b_id = b.id";
+        System.out.println(interceptor.parserSingle(sql, null));
+    }
+
     void assertSql(String sql, String targetSql) {
         assertThat(interceptor.parserSingle(sql, null)).isEqualTo(targetSql);
     }