ソースを参照

多租户 with as

miemie 4 年 前
コミット
66542b06e3

+ 5 - 8
mybatis-plus-extension/src/main/java/com/baomidou/mybatisplus/extension/plugins/inner/TenantLineInnerInterceptor.java

@@ -16,10 +16,7 @@
 package com.baomidou.mybatisplus.extension.plugins.inner;
 
 import com.baomidou.mybatisplus.core.parser.SqlParserHelper;
-import com.baomidou.mybatisplus.core.toolkit.ClassUtils;
-import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
-import com.baomidou.mybatisplus.core.toolkit.PluginUtils;
-import com.baomidou.mybatisplus.core.toolkit.StringPool;
+import com.baomidou.mybatisplus.core.toolkit.*;
 import com.baomidou.mybatisplus.extension.parser.JsqlParserSupport;
 import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
 import com.baomidou.mybatisplus.extension.toolkit.PropertyMapper;
@@ -89,10 +86,10 @@ public class TenantLineInnerInterceptor extends JsqlParserSupport implements Inn
     @Override
     protected void processSelect(Select select, int index, Object obj) {
         processSelectBody(select.getSelectBody());
-//        List<WithItem> withItemsList = select.getWithItemsList();
-//        if (!CollectionUtils.isEmpty(withItemsList)){
-//            withItemsList.forEach(this::processSelectBody);
-//        }
+        List<WithItem> withItemsList = select.getWithItemsList();
+        if (!CollectionUtils.isEmpty(withItemsList)) {
+            withItemsList.forEach(this::processSelectBody);
+        }
     }
 
     protected void processSelectBody(SelectBody selectBody) {

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

@@ -1,5 +1,7 @@
 package com.baomidou.mybatisplus.extension.plugins.inner;
 
+import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
+import net.sf.jsqlparser.expression.Expression;
 import net.sf.jsqlparser.expression.LongValue;
 import org.junit.jupiter.api.Test;
 
@@ -11,7 +13,17 @@ import static org.assertj.core.api.Assertions.assertThat;
  */
 class TenantLineInnerInterceptorTest {
 
-    private final TenantLineInnerInterceptor interceptor = new TenantLineInnerInterceptor(() -> new LongValue(1));
+    private final TenantLineInnerInterceptor interceptor = new TenantLineInnerInterceptor(new TenantLineHandler() {
+        @Override
+        public Expression getTenantId() {
+            return new LongValue(1);
+        }
+
+        @Override
+        public boolean ignoreTable(String tableName) {
+            return tableName.startsWith("with_as");
+        }
+    });
 
     @Test
     void insert() {
@@ -87,6 +99,12 @@ class TenantLineInnerInterceptorTest {
 //                "WHERE (e.id = ? OR e.name = ?) AND e.tenant_id = 1");
     }
 
+    @Test
+    void selectWithAs() {
+        assertSql("with with_as_A as (select * from entity) select * from with_as_A",
+            "WITH with_as_A AS (SELECT * FROM entity WHERE tenant_id = 1) SELECT * FROM with_as_A");
+    }
+
     void assertSql(String sql, String targetSql) {
         assertThat(interceptor.parserSingle(sql, null)).isEqualTo(targetSql);
     }