You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by zh...@apache.org on 2022/11/09 13:18:37 UTC

[shardingsphere] branch master updated: Refactor : refactor the SQL parser result processor with strategy pattern (#22044)

This is an automated email from the ASF dual-hosted git repository.

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 578c95316fa Refactor : refactor the SQL parser result processor with strategy pattern (#22044)
578c95316fa is described below

commit 578c95316fa982d0722d96551c436a88f93ae2ba
Author: 孙念君 Nianjun Sun <su...@apache.org>
AuthorDate: Wed Nov 9 21:18:31 2022 +0800

    Refactor : refactor the SQL parser result processor with strategy pattern (#22044)
    
    * Refactor : refactor the SQL parser result processor with strategy pattern (#21999)
    
    * Fix : fix checkstyle violation (#21999)
---
 .../DynamicLoadingSQLParserParameterizedTest.java  |  4 +-
 .../sql/parser/env/IntegrationTestEnvironment.java |  5 ++-
 .../parser/loader/DynamicSQLCaseGitHubLoader.java  |  4 +-
 .../parser/loader/DynamicSQLCaseGiteeLoader.java   |  4 +-
 .../parser/loader/DynamicSQLCaseLocalLoader.java   |  4 +-
 .../DynamicLoadingMySQLParserParameterizedIT.java  |  4 +-
 ...amicLoadingPostgreSQLParserParameterizedIT.java |  4 +-
 ...rator.java => SQLParserCSVResultProcessor.java} |  9 +++-
 .../parser/result/SQLParserLogResultProcessor.java | 38 ++++++++++++++++
 .../parser/result/SQLParserResultProcessor.java    | 38 ++++++++++++++++
 .../result/SQLParserResultProcessorManager.java    | 52 ++++++++++++++++++++++
 .../src/test/resources/env/it-env.properties       |  1 +
 12 files changed, 152 insertions(+), 15 deletions(-)

diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/base/DynamicLoadingSQLParserParameterizedTest.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/base/DynamicLoadingSQLParserParameterizedTest.java
index ff77329f716..f0a9d4df11d 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/base/DynamicLoadingSQLParserParameterizedTest.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/base/DynamicLoadingSQLParserParameterizedTest.java
@@ -26,7 +26,7 @@ import org.apache.shardingsphere.sql.parser.api.CacheOption;
 import org.apache.shardingsphere.sql.parser.api.SQLParserEngine;
 import org.apache.shardingsphere.sql.parser.api.SQLVisitorEngine;
 import org.apache.shardingsphere.sql.parser.core.ParseASTNode;
-import org.apache.shardingsphere.sql.parser.result.CSVResultGenerator;
+import org.apache.shardingsphere.sql.parser.result.SQLParserResultProcessor;
 import org.junit.Test;
 
 import java.io.BufferedReader;
@@ -52,7 +52,7 @@ public abstract class DynamicLoadingSQLParserParameterizedTest {
     private final String databaseType;
     
     // TODO this will refactor as an abstract
-    private final CSVResultGenerator resultGenerator;
+    private final SQLParserResultProcessor resultGenerator;
     
     protected static Collection<Object[]> getTestParameters(final String sqlCaseAPI, final URI sqlCaseURI) {
         Collection<Object[]> result = new LinkedList<>();
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/env/IntegrationTestEnvironment.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/env/IntegrationTestEnvironment.java
index 0d3e8554682..58acde28de1 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/env/IntegrationTestEnvironment.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/env/IntegrationTestEnvironment.java
@@ -35,11 +35,14 @@ public final class IntegrationTestEnvironment {
     
     private final String resultPath;
     
+    private final String resultProcessorType;
+    
     private IntegrationTestEnvironment() {
         props = loadProperties();
         String sqlParserITEnabledStr = null == System.getProperty("sql.parser.it.enabled") ? props.get("sql.parser.it.enabled").toString() : System.getProperty("sql.parser.it.enabled");
         sqlParserITEnabled = null == sqlParserITEnabledStr ? false : Boolean.parseBoolean(sqlParserITEnabledStr);
-        resultPath = props.get("sql.parser.it.report.path").toString();
+        resultPath = props.getOrDefault("sql.parser.it.report.path", "/tmp/").toString();
+        resultProcessorType = props.getOrDefault("sql.parser.it.report.type", "LOG").toString();
     }
     
     /**
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGitHubLoader.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGitHubLoader.java
index 83fbcd3e075..7acfc3b08ca 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGitHubLoader.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGitHubLoader.java
@@ -18,7 +18,7 @@
 package org.apache.shardingsphere.sql.parser.loader;
 
 import org.apache.shardingsphere.sql.parser.base.DynamicLoadingSQLParserParameterizedTest;
-import org.apache.shardingsphere.sql.parser.result.CSVResultGenerator;
+import org.apache.shardingsphere.sql.parser.result.SQLParserCSVResultProcessor;
 
 import java.net.URI;
 import java.util.Collection;
@@ -28,7 +28,7 @@ import java.util.Map;
 public class DynamicSQLCaseGitHubLoader extends DynamicLoadingSQLParserParameterizedTest implements DynamicSQLCaseLoaderStrategy {
     
     public DynamicSQLCaseGitHubLoader() {
-        super("", "", "", new CSVResultGenerator(""));
+        super("", "", "", new SQLParserCSVResultProcessor(""));
     }
     
     /**
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGiteeLoader.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGiteeLoader.java
index 66b1edc6bb3..9acfae04da5 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGiteeLoader.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseGiteeLoader.java
@@ -18,7 +18,7 @@
 package org.apache.shardingsphere.sql.parser.loader;
 
 import org.apache.shardingsphere.sql.parser.base.DynamicLoadingSQLParserParameterizedTest;
-import org.apache.shardingsphere.sql.parser.result.CSVResultGenerator;
+import org.apache.shardingsphere.sql.parser.result.SQLParserCSVResultProcessor;
 
 import java.net.URI;
 import java.util.Collection;
@@ -28,7 +28,7 @@ import java.util.Map;
 public class DynamicSQLCaseGiteeLoader extends DynamicLoadingSQLParserParameterizedTest implements DynamicSQLCaseLoaderStrategy {
     
     public DynamicSQLCaseGiteeLoader() {
-        super("", "", "", new CSVResultGenerator(""));
+        super("", "", "", new SQLParserCSVResultProcessor(""));
     }
     
     /**
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseLocalLoader.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseLocalLoader.java
index 7016b9664f2..07c2fb2688e 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseLocalLoader.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/loader/DynamicSQLCaseLocalLoader.java
@@ -18,7 +18,7 @@
 package org.apache.shardingsphere.sql.parser.loader;
 
 import org.apache.shardingsphere.sql.parser.base.DynamicLoadingSQLParserParameterizedTest;
-import org.apache.shardingsphere.sql.parser.result.CSVResultGenerator;
+import org.apache.shardingsphere.sql.parser.result.SQLParserCSVResultProcessor;
 
 import java.net.URI;
 import java.util.Collection;
@@ -27,7 +27,7 @@ import java.util.LinkedList;
 public class DynamicSQLCaseLocalLoader extends DynamicLoadingSQLParserParameterizedTest implements DynamicSQLCaseLoaderStrategy {
     
     public DynamicSQLCaseLocalLoader() {
-        super("", "", "", new CSVResultGenerator(""));
+        super("", "", "", new SQLParserCSVResultProcessor(""));
     }
     
     /**
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/mysql/DynamicLoadingMySQLParserParameterizedIT.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/mysql/DynamicLoadingMySQLParserParameterizedIT.java
index ae162d7f8f7..6c345fdd983 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/mysql/DynamicLoadingMySQLParserParameterizedIT.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/mysql/DynamicLoadingMySQLParserParameterizedIT.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.sql.parser.mysql;
 
 import org.apache.shardingsphere.sql.parser.base.DynamicLoadingSQLParserParameterizedTest;
 import org.apache.shardingsphere.sql.parser.env.IntegrationTestEnvironment;
-import org.apache.shardingsphere.sql.parser.result.CSVResultGenerator;
+import org.apache.shardingsphere.sql.parser.result.SQLParserResultProcessorManager;
 import org.apache.shardingsphere.test.runner.ShardingSphereParallelTestParameterized;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized.Parameters;
@@ -32,7 +32,7 @@ import java.util.Collections;
 public final class DynamicLoadingMySQLParserParameterizedIT extends DynamicLoadingSQLParserParameterizedTest {
     
     public DynamicLoadingMySQLParserParameterizedIT(final String sqlCaseId, final String sqlCaseValue) {
-        super(sqlCaseId, sqlCaseValue, "MySQL", new CSVResultGenerator("MySQL"));
+        super(sqlCaseId, sqlCaseValue, "MySQL", SQLParserResultProcessorManager.getProcessor("MySQL"));
     }
     
     /**
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/postgresql/DynamicLoadingPostgreSQLParserParameterizedIT.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/postgresql/DynamicLoadingPostgreSQLParserParameterizedIT.java
index 4bdd01cf1f0..7185c290090 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/postgresql/DynamicLoadingPostgreSQLParserParameterizedIT.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/postgresql/DynamicLoadingPostgreSQLParserParameterizedIT.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.sql.parser.postgresql;
 
 import org.apache.shardingsphere.sql.parser.base.DynamicLoadingSQLParserParameterizedTest;
 import org.apache.shardingsphere.sql.parser.env.IntegrationTestEnvironment;
-import org.apache.shardingsphere.sql.parser.result.CSVResultGenerator;
+import org.apache.shardingsphere.sql.parser.result.SQLParserCSVResultProcessor;
 import org.apache.shardingsphere.test.runner.ShardingSphereParallelTestParameterized;
 import org.junit.runner.RunWith;
 import org.junit.runners.Parameterized.Parameters;
@@ -32,7 +32,7 @@ import java.util.Collections;
 public final class DynamicLoadingPostgreSQLParserParameterizedIT extends DynamicLoadingSQLParserParameterizedTest {
     
     public DynamicLoadingPostgreSQLParserParameterizedIT(final String sqlCaseId, final String sqlCaseValue) {
-        super(sqlCaseId, sqlCaseValue, "PostgreSQL", new CSVResultGenerator("PostgreSQL"));
+        super(sqlCaseId, sqlCaseValue, "PostgreSQL", new SQLParserCSVResultProcessor("PostgreSQL"));
     }
     
     /**
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/CSVResultGenerator.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/SQLParserCSVResultProcessor.java
similarity index 91%
rename from test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/CSVResultGenerator.java
rename to test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/SQLParserCSVResultProcessor.java
index 821cb190879..302a8ce8176 100644
--- a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/CSVResultGenerator.java
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/SQLParserCSVResultProcessor.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.sql.parser.result;
 
+import lombok.Getter;
 import org.apache.commons.csv.CSVFormat;
 import org.apache.commons.csv.CSVPrinter;
 import org.apache.shardingsphere.sql.parser.env.IntegrationTestEnvironment;
@@ -28,11 +29,14 @@ import java.io.IOException;
 /**
  *  CSV result generator.
  */
-public final class CSVResultGenerator {
+public final class SQLParserCSVResultProcessor implements SQLParserResultProcessor {
     
     private final CSVPrinter printer;
     
-    public CSVResultGenerator(final String databaseType) {
+    @Getter
+    private final String type = "CSV";
+    
+    public SQLParserCSVResultProcessor(final String databaseType) {
         try {
             File csvFile = new File(IntegrationTestEnvironment.getInstance().getResultPath() + databaseType + "-result.csv");
             createHeader(csvFile);
@@ -58,6 +62,7 @@ public final class CSVResultGenerator {
      *
      * @param params the content for a row of CSV record
      */
+    @Override
     public void processResult(final Object... params) {
         try {
             printer.printRecord(params);
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/SQLParserLogResultProcessor.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/SQLParserLogResultProcessor.java
new file mode 100644
index 00000000000..9392563421e
--- /dev/null
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/SQLParserLogResultProcessor.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.sql.parser.result;
+
+import lombok.Getter;
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class SQLParserLogResultProcessor implements SQLParserResultProcessor {
+    
+    @Getter
+    private final String type = "LOG";
+    
+    public SQLParserLogResultProcessor(final String databaseType) {
+    
+    }
+    
+    @Override
+    public void processResult(final Object... params) {
+        // TODO set up the log format
+        log.info("Printing the SQL parser process result");
+    }
+}
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/SQLParserResultProcessor.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/SQLParserResultProcessor.java
new file mode 100644
index 00000000000..512fc2f5ff9
--- /dev/null
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/SQLParserResultProcessor.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.sql.parser.result;
+
+/**
+ * SQL parser result processor.
+ */
+public interface SQLParserResultProcessor {
+    
+    /**
+     * Process the result.
+     *
+     * @param params the content for a row of CSV record
+     */
+    void processResult(Object... params);
+    
+    /**
+     * Get the generator type.
+     *
+     * @return type
+     */
+    String getType();
+}
diff --git a/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/SQLParserResultProcessorManager.java b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/SQLParserResultProcessorManager.java
new file mode 100644
index 00000000000..24e607c6869
--- /dev/null
+++ b/test/integration-test/sql-parser/src/test/java/org/apache/shardingsphere/sql/parser/result/SQLParserResultProcessorManager.java
@@ -0,0 +1,52 @@
+package org.apache.shardingsphere.sql.parser.result;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.shardingsphere.sql.parser.env.IntegrationTestEnvironment;
+
+import java.io.File;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Field;
+import java.net.URL;
+import java.util.Objects;
+
+/**
+ * Get the corresponding result processor through config.
+ */
+@Slf4j
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public class SQLParserResultProcessorManager {
+    
+    /**
+     * Get the SQL parser result processor.
+     *
+     * @param databaseType database type
+     * @return the implementation of SQLParserResultProcessor
+     */
+    public static SQLParserResultProcessor getProcessor(final String databaseType) {
+        String type = IntegrationTestEnvironment.getInstance().getResultProcessorType();
+        try {
+            Class<?> interfaceClazz = Class.forName(SQLParserResultProcessor.class.getPackage().getName());
+            String packageName = interfaceClazz.getPackage().getName();
+            URL packagePath = Thread.currentThread().getContextClassLoader().getResource(packageName.replace(".", "/"));
+            File[] classFiles = new File(Objects.requireNonNull(packagePath).getFile()).listFiles((dir, name) -> name.endsWith(".class"));
+            for (File file : Objects.requireNonNull(classFiles)) {
+                String className = file.getName().replaceAll(".class$", "");
+                Class<?> clazz = Class.forName(packageName + "." + className);
+                if (SQLParserResultProcessor.class.isAssignableFrom(clazz)) {
+                    Field typeField = clazz.getDeclaredField("type");
+                    typeField.setAccessible(true);
+                    Constructor<?> constructor = clazz.getConstructor(String.class);
+                    SQLParserResultProcessor result = (SQLParserResultProcessor) constructor.newInstance(databaseType);
+                    if (type.equalsIgnoreCase(typeField.get(result).toString())) {
+                        return result;
+                    }
+                }
+            }
+        } catch (final ReflectiveOperationException ex) {
+            log.error("encounter exception when get SQLParserResultProcessor by reflection", ex);
+        }
+        throw new IllegalArgumentException("The processor type does not supported : " + type);
+    }
+}
diff --git a/test/integration-test/sql-parser/src/test/resources/env/it-env.properties b/test/integration-test/sql-parser/src/test/resources/env/it-env.properties
index 49c3ba632f0..6465df260d7 100644
--- a/test/integration-test/sql-parser/src/test/resources/env/it-env.properties
+++ b/test/integration-test/sql-parser/src/test/resources/env/it-env.properties
@@ -17,3 +17,4 @@
 
 sql.parser.it.enabled=false
 sql.parser.it.report.path=/tmp/
+sql.parser.it.report.type=csv