You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2020/12/29 14:41:07 UTC

[shardingsphere] branch master updated: Add SchemaEnvironmentManager.executeSQLScript (#8813)

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

panjuan 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 c7a4cac  Add SchemaEnvironmentManager.executeSQLScript (#8813)
c7a4cac is described below

commit c7a4cac0a4d1ff25447231390885479f5651ee28
Author: Liang Zhang <te...@163.com>
AuthorDate: Tue Dec 29 22:40:40 2020 +0800

    Add SchemaEnvironmentManager.executeSQLScript (#8813)
    
    * Use lambda for SchemaEnvironmentManager
    
    * Refactor SchemaEnvironmentManager
    
    * Refactor SchemaEnvironmentManager
    
    * Refactor SchemaEnvironmentManager
    
    * Add SchemaEnvironmentManager.executeSQLScript
---
 .../test/integration/engine/BaseIT.java            |  14 +-
 .../env/schema/SchemaEnvironmentManager.java       | 149 +++++++++------------
 2 files changed, 72 insertions(+), 91 deletions(-)

diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/BaseIT.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/BaseIT.java
index 360cb0c..a334a50 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/BaseIT.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/engine/BaseIT.java
@@ -93,34 +93,34 @@ public abstract class BaseIT {
         createTables();
     }
     
-    @SneakyThrows({JAXBException.class, IOException.class, SQLException.class})
+    @SneakyThrows({JAXBException.class, IOException.class})
     protected static void createDatabases() {
         for (String each : IntegrateTestEnvironment.getInstance().getRuleTypes()) {
-            SchemaEnvironmentManager.dropDatabase(each);
+            SchemaEnvironmentManager.dropDatabases(each);
         }
         for (String each : IntegrateTestEnvironment.getInstance().getRuleTypes()) {
-            SchemaEnvironmentManager.createDatabase(each);
+            SchemaEnvironmentManager.createDatabases(each);
         }
     }
     
-    @SneakyThrows({JAXBException.class, IOException.class, SQLException.class})
+    @SneakyThrows({JAXBException.class, IOException.class})
     protected static void createTables() {
         for (String each : IntegrateTestEnvironment.getInstance().getRuleTypes()) {
-            SchemaEnvironmentManager.createTable(each);
+            SchemaEnvironmentManager.createTables(each);
         }
     }
     
     @SneakyThrows({JAXBException.class, IOException.class})
     protected static void dropDatabases() {
         for (String each : IntegrateTestEnvironment.getInstance().getRuleTypes()) {
-            SchemaEnvironmentManager.dropDatabase(each);
+            SchemaEnvironmentManager.dropDatabases(each);
         }
     }
     
     @SneakyThrows({JAXBException.class, IOException.class})
     protected static void dropTables() {
         for (String each : IntegrateTestEnvironment.getInstance().getRuleTypes()) {
-            SchemaEnvironmentManager.dropTable(each);
+            SchemaEnvironmentManager.dropTables(each);
         }
     }
     
diff --git a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/schema/SchemaEnvironmentManager.java b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/schema/SchemaEnvironmentManager.java
index 6165410..31c1997 100644
--- a/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/schema/SchemaEnvironmentManager.java
+++ b/shardingsphere-test/shardingsphere-integration-test/shardingsphere-test-suite/src/test/java/org/apache/shardingsphere/test/integration/env/schema/SchemaEnvironmentManager.java
@@ -36,7 +36,7 @@ import java.sql.Connection;
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Collections;
-import java.util.LinkedList;
+import java.util.stream.Collectors;
 
 /**
  * Schema environment manager.
@@ -54,145 +54,126 @@ public final class SchemaEnvironmentManager {
      */
     public static Collection<String> getDataSourceNames(final String ruleType) throws IOException, JAXBException {
         return unmarshal(EnvironmentPath.getSchemaEnvironmentFile(ruleType)).getDatabases();
-    } 
+    }
+    
+    private static SchemaEnvironment unmarshal(final String schemaEnvironmentConfigFile) throws IOException, JAXBException {
+        try (FileReader reader = new FileReader(schemaEnvironmentConfigFile)) {
+            return (SchemaEnvironment) JAXBContext.newInstance(SchemaEnvironment.class).createUnmarshaller().unmarshal(reader);
+        }
+    }
     
     /**
-     * Create database.
+     * Create databases.
      *
      * @param ruleType rule type
      * @throws IOException IO exception
      * @throws JAXBException JAXB exception
-     * @throws SQLException SQL exception
      */
-    public static void createDatabase(final String ruleType) throws IOException, JAXBException, SQLException {
+    public static void createDatabases(final String ruleType) throws IOException, JAXBException {
         SchemaEnvironment schemaEnvironment = unmarshal(EnvironmentPath.getSchemaEnvironmentFile(ruleType));
         for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().keySet()) {
             DataSource dataSource = JdbcDataSourceBuilder.build(null, each);
-            try (
-                    Connection connection = dataSource.getConnection();
-                    StringReader stringReader = new StringReader(Joiner.on(";\n").skipNulls().join(generateCreateDatabaseSQLs(each, schemaEnvironment.getDatabases())))) {
-                RunScript.execute(connection, stringReader);
-            }
+            executeSQLScript(dataSource, generateCreateDatabaseSQLs(each, schemaEnvironment.getDatabases()));
+        }
+    }
+    
+    private static Collection<String> generateCreateDatabaseSQLs(final DatabaseType databaseType, final Collection<String> databaseNames) {
+        switch (databaseType.getName()) {
+            case "H2":
+                return Collections.emptyList();
+            case "Oracle":
+                return databaseNames.stream().map(each -> String.format("CREATE SCHEMA %s", each)).collect(Collectors.toList());
+            default:
+                return databaseNames.stream().map(each -> String.format("CREATE DATABASE %s", each)).collect(Collectors.toList());
         }
     }
     
     /**
-     * Drop database.
+     * Drop databases.
      *
      * @param ruleType rule type
      * @throws IOException IO exception
      * @throws JAXBException JAXB exception
      */
-    public static void dropDatabase(final String ruleType) throws IOException, JAXBException {
+    public static void dropDatabases(final String ruleType) throws IOException, JAXBException {
         SchemaEnvironment schemaEnvironment = unmarshal(EnvironmentPath.getSchemaEnvironmentFile(ruleType));
         for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().keySet()) {
             DataSource dataSource = JdbcDataSourceBuilder.build(null, each);
-            if ("PostgreSQL".equals(each.getName())) {
-                try (
-                        Connection connection = dataSource.getConnection();
-                        StringReader stringReader = new StringReader(Joiner.on(";\n").skipNulls().join(generateTerminateConnectionSQLs(schemaEnvironment.getDatabases())))) {
-                    RunScript.execute(connection, stringReader);
-                } catch (final SQLException ex) {
-                    // TODO database maybe not exist
-                }
-            }
-            try (
-                    Connection connection = dataSource.getConnection();
-                    StringReader stringReader = new StringReader(Joiner.on(";\n").skipNulls().join(generateDropDatabaseSQLs(each, schemaEnvironment.getDatabases())))) {
-                RunScript.execute(connection, stringReader);
-            } catch (final SQLException ex) {
-                // TODO database maybe not exist
-            }
+            executeSQLScript(dataSource, generatePrepareDropDatabaseSQLs(each, schemaEnvironment.getDatabases()));
+            executeSQLScript(dataSource, generateDropDatabaseSQLs(each, schemaEnvironment.getDatabases()));
         }
     }
     
-    private static SchemaEnvironment unmarshal(final String schemaEnvironmentConfigFile) throws IOException, JAXBException {
-        try (FileReader reader = new FileReader(schemaEnvironmentConfigFile)) {
-            return (SchemaEnvironment) JAXBContext.newInstance(SchemaEnvironment.class).createUnmarshaller().unmarshal(reader);
+    private static Collection<String> generatePrepareDropDatabaseSQLs(final DatabaseType databaseType, final Collection<String> databaseNames) {
+        if ("PostgreSQL".equals(databaseType.getName())) {
+            String sql = "SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '%s'";
+            return databaseNames.stream().map(each -> String.format(sql, each)).collect(Collectors.toList());
         }
+        return Collections.emptyList();
     }
     
-    private static Collection<String> generateCreateDatabaseSQLs(final DatabaseType databaseType, final Collection<String> databases) {
-        if ("H2".equals(databaseType.getName())) {
-            return Collections.emptyList();
-        }
-        String sql = "Oracle".equals(databaseType.getName()) ? "CREATE SCHEMA %s" : "CREATE DATABASE %s";
-        Collection<String> result = new LinkedList<>();
-        for (String each : databases) {
-            result.add(String.format(sql, each));
+    private static Collection<String> generateDropDatabaseSQLs(final DatabaseType databaseType, final Collection<String> databaseNames) {
+        switch (databaseType.getName()) {
+            case "H2":
+                return Collections.emptyList();
+            case "Oracle":
+                return databaseNames.stream().map(each -> String.format("DROP SCHEMA %s", each)).collect(Collectors.toList());
+            default:
+                return databaseNames.stream().map(each -> String.format("DROP DATABASE IF EXISTS %s", each)).collect(Collectors.toList());
         }
-        return result;
-    }
-    
-    private static Collection<String> generateTerminateConnectionSQLs(final Collection<String> databases) {
-        String sql = "SELECT pg_terminate_backend (pg_stat_activity.pid) FROM pg_stat_activity WHERE pg_stat_activity.datname = '%s'";
-        Collection<String> result = new LinkedList<>();
-        for (String each : databases) {
-            result.add(String.format(sql, each));
-        }
-        return result;
-    }
-    
-    private static Collection<String> generateDropDatabaseSQLs(final DatabaseType databaseType, final Collection<String> databases) {
-        if ("H2".equals(databaseType.getName())) {
-            return Collections.emptyList();
-        }
-        String sql = "Oracle".equals(databaseType.getName()) ? "DROP SCHEMA %s" : "DROP DATABASE IF EXISTS %s";
-        Collection<String> result = new LinkedList<>();
-        for (String each : databases) {
-            result.add(String.format(sql, each));
-        }
-        return result;
     }
     
     /**
-     * Create table.
+     * Create tables.
      *
      * @param ruleType rule type
      * @throws JAXBException JAXB exception
      * @throws IOException IO exception
-     * @throws SQLException SQL exception
      */
-    public static void createTable(final String ruleType) throws JAXBException, IOException, SQLException {
+    public static void createTables(final String ruleType) throws JAXBException, IOException {
+        SchemaEnvironment schemaEnvironment = unmarshal(EnvironmentPath.getSchemaEnvironmentFile(ruleType));
         for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().keySet()) {
-            SchemaEnvironment databaseEnvironmentSchema = unmarshal(EnvironmentPath.getSchemaEnvironmentFile(ruleType));
-            createTable(databaseEnvironmentSchema, each);
+            createTables(schemaEnvironment, each);
         }
     }
     
-    private static void createTable(final SchemaEnvironment databaseEnvironmentSchema, final DatabaseType databaseType) throws SQLException {
-        for (String each : databaseEnvironmentSchema.getDatabases()) {
+    private static void createTables(final SchemaEnvironment schemaEnvironment, final DatabaseType databaseType) {
+        for (String each : schemaEnvironment.getDatabases()) {
             DataSource dataSource = JdbcDataSourceBuilder.build(each, databaseType);
-            try (Connection connection = dataSource.getConnection();
-                 StringReader stringReader = new StringReader(Joiner.on(";\n").join(databaseEnvironmentSchema.getTableCreateSQLs()))) {
-                RunScript.execute(connection, stringReader);
-            }
+            executeSQLScript(dataSource, schemaEnvironment.getTableCreateSQLs());
         }
     }
     
     /**
-     * Drop table.
+     * Drop tables.
      *
      * @param ruleType rule type
      * @throws JAXBException JAXB exception
      * @throws IOException IO exception
      */
-    public static void dropTable(final String ruleType) throws JAXBException, IOException {
+    public static void dropTables(final String ruleType) throws JAXBException, IOException {
+        SchemaEnvironment schemaEnvironment = unmarshal(EnvironmentPath.getSchemaEnvironmentFile(ruleType));
         for (DatabaseType each : IntegrateTestEnvironment.getInstance().getDatabaseEnvironments().keySet()) {
-            SchemaEnvironment databaseEnvironmentSchema = unmarshal(EnvironmentPath.getSchemaEnvironmentFile(ruleType));
-            dropTable(databaseEnvironmentSchema, each);
+            dropTables(schemaEnvironment, each);
         }
     }
     
-    private static void dropTable(final SchemaEnvironment databaseEnvironmentSchema, final DatabaseType databaseType) {
-        for (String each : databaseEnvironmentSchema.getDatabases()) {
+    private static void dropTables(final SchemaEnvironment schemaEnvironment, final DatabaseType databaseType) {
+        for (String each : schemaEnvironment.getDatabases()) {
             DataSource dataSource = JdbcDataSourceBuilder.build(each, databaseType);
-            try (Connection connection = dataSource.getConnection();
-                 StringReader stringReader = new StringReader(Joiner.on(";\n").join(databaseEnvironmentSchema.getTableDropSQLs()))) {
-                RunScript.execute(connection, stringReader);
-            } catch (final SQLException ex) {
-                // TODO table maybe not exist
-            }
+            executeSQLScript(dataSource, schemaEnvironment.getTableDropSQLs());
+        }
+    }
+    
+    private static void executeSQLScript(final DataSource dataSource, final Collection<String> sqls) {
+        if (sqls.isEmpty()) {
+            return;
+        }
+        try (Connection connection = dataSource.getConnection();
+             StringReader sqlScript = new StringReader(Joiner.on(";\n").skipNulls().join(sqls))) {
+            RunScript.execute(connection, sqlScript);
+        } catch (final SQLException ignored) {
+            // TODO print err message if not drop not existed database/table
         }
     }
 }