You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2020/07/09 11:49:35 UTC

[shardingsphere] branch master updated: Add sql statement -> yaml comfig

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

menghaoran 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 89522ea  Add sql statement -> yaml comfig
     new 17cccd7  Merge pull request #6312 from tristaZero/new
89522ea is described below

commit 89522ea1149373321f9c85771d13eb317241b22c
Author: tristaZero <ha...@163.com>
AuthorDate: Thu Jul 9 18:39:26 2020 +0800

    Add sql statement -> yaml comfig
---
 .../core/configcenter/ConfigCenter.java            |  1 +
 .../DatabaseCommunicationEngineFactory.java        | 12 +++---
 .../jdbc/execute/RegistryCenterExecuteEngine.java  | 10 +++--
 .../generator/YamlConfigurationGenerator.java      |  5 +--
 .../YamlDataSourceConfigurationGenerator.java      | 21 +++++++----
 ...java => CreateDataSourcesStatementContext.java} | 44 +++++++++++-----------
 .../ddl/CreateShardingRuleStatementContext.java    |  2 +-
 ...tement.java => CreateDataSourcesStatement.java} |  2 +-
 .../statement/ddl/CreateShardingRuleStatement.java |  2 +-
 9 files changed, 53 insertions(+), 46 deletions(-)

diff --git a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/main/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenter.java b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/main/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenter.java
index 2dd8ef9..d5e950f 100644
--- a/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/main/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenter.java
+++ b/shardingsphere-control-panel/shardingsphere-orchestration/shardingsphere-orchestration-core/shardingsphere-orchestration-core-configcenter/src/main/java/org/apache/shardingsphere/orchestration/core/configcenter/ConfigCenter.java
@@ -82,6 +82,7 @@ public final class ConfigCenter {
                                       final Collection<RuleConfiguration> ruleConfigurations, final boolean isOverwrite) {
         persistDataSourceConfiguration(shardingSchemaName, dataSourceConfigs, isOverwrite);
         persistRuleConfigurations(shardingSchemaName, ruleConfigurations, isOverwrite);
+        // TODO Consider removing the following one.
         persistShardingSchemaName(shardingSchemaName, isOverwrite);
     }
     
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngineFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngineFactory.java
index 87872f7..fff70e6 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngineFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/DatabaseCommunicationEngineFactory.java
@@ -29,7 +29,7 @@ import org.apache.shardingsphere.proxy.backend.communication.jdbc.wrapper.JDBCEx
 import org.apache.shardingsphere.proxy.backend.communication.jdbc.wrapper.PreparedStatementExecutorWrapper;
 import org.apache.shardingsphere.proxy.backend.communication.jdbc.wrapper.StatementExecutorWrapper;
 import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
-import org.apache.shardingsphere.sql.parser.sql.statement.ddl.CreateDataSourceStatement;
+import org.apache.shardingsphere.sql.parser.sql.statement.ddl.CreateDataSourcesStatement;
 import org.apache.shardingsphere.sql.parser.sql.statement.ddl.CreateShardingRuleStatement;
 
 import java.util.List;
@@ -61,7 +61,7 @@ public final class DatabaseCommunicationEngineFactory {
      */
     public DatabaseCommunicationEngine newTextProtocolInstance(final SchemaContext schema, final String sql, final BackendConnection backendConnection) {
         SQLStatement sqlStatement = schema.getRuntimeContext().getSqlParserEngine().parse(sql, false);
-        return new JDBCDatabaseCommunicationEngine(sql, backendConnection, createSQLExecuteEngine(sqlStatement, backendConnection, new StatementExecutorWrapper(schema, sqlStatement)));
+        return new JDBCDatabaseCommunicationEngine(sql, backendConnection, createSQLExecuteEngine(schema, sqlStatement, backendConnection, new StatementExecutorWrapper(schema, sqlStatement)));
     }
     
     /**
@@ -76,11 +76,11 @@ public final class DatabaseCommunicationEngineFactory {
     public DatabaseCommunicationEngine newBinaryProtocolInstance(final SchemaContext schema, final String sql, final List<Object> parameters, final BackendConnection backendConnection) {
         SQLStatement sqlStatement = schema.getRuntimeContext().getSqlParserEngine().parse(sql, true);
         return new JDBCDatabaseCommunicationEngine(sql,
-                backendConnection, createSQLExecuteEngine(sqlStatement, backendConnection, new PreparedStatementExecutorWrapper(schema, sqlStatement, parameters)));
+                backendConnection, createSQLExecuteEngine(schema, sqlStatement, backendConnection, new PreparedStatementExecutorWrapper(schema, sqlStatement, parameters)));
     }
     
-    private SQLExecuteEngine createSQLExecuteEngine(final SQLStatement sqlStatement, final BackendConnection backendConnection, final JDBCExecutorWrapper executorWrapper) {
-        return sqlStatement instanceof CreateDataSourceStatement || sqlStatement instanceof CreateShardingRuleStatement
-                ? new RegistryCenterExecuteEngine(sqlStatement) : new JDBCExecuteEngine(backendConnection, executorWrapper);
+    private SQLExecuteEngine createSQLExecuteEngine(final SchemaContext schema, final SQLStatement sqlStatement, final BackendConnection backendConnection, final JDBCExecutorWrapper executorWrapper) {
+        return sqlStatement instanceof CreateDataSourcesStatement || sqlStatement instanceof CreateShardingRuleStatement
+                ? new RegistryCenterExecuteEngine(schema.getName(), sqlStatement) : new JDBCExecuteEngine(backendConnection, executorWrapper);
     }
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RegistryCenterExecuteEngine.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RegistryCenterExecuteEngine.java
index 3a0c7fe..fab7307 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RegistryCenterExecuteEngine.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/RegistryCenterExecuteEngine.java
@@ -22,7 +22,7 @@ import org.apache.shardingsphere.infra.executor.sql.context.ExecutionContext;
 import org.apache.shardingsphere.proxy.backend.response.BackendResponse;
 import org.apache.shardingsphere.proxy.backend.response.update.UpdateResponse;
 import org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext;
-import org.apache.shardingsphere.sql.parser.binder.statement.ddl.CreateDataSourceStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.ddl.CreateDataSourcesStatementContext;
 import org.apache.shardingsphere.sql.parser.binder.statement.ddl.CreateShardingRuleStatementContext;
 import org.apache.shardingsphere.sql.parser.sql.statement.SQLStatement;
 
@@ -34,6 +34,8 @@ import java.util.LinkedList;
 @RequiredArgsConstructor
 public class RegistryCenterExecuteEngine implements SQLExecuteEngine {
     
+    private final String schemaName;
+    
     private final SQLStatement sqlStatement;
     
     @Override
@@ -43,13 +45,13 @@ public class RegistryCenterExecuteEngine implements SQLExecuteEngine {
     
     @Override
     public final BackendResponse execute(final ExecutionContext executionContext) {
-        if (executionContext.getSqlStatementContext() instanceof CreateDataSourceStatementContext) {
-            return execute((CreateDataSourceStatementContext) executionContext.getSqlStatementContext());
+        if (executionContext.getSqlStatementContext() instanceof CreateDataSourcesStatementContext) {
+            return execute((CreateDataSourcesStatementContext) executionContext.getSqlStatementContext());
         }
         return execute((CreateShardingRuleStatementContext) executionContext.getSqlStatementContext());
     }
     
-    private BackendResponse execute(final CreateDataSourceStatementContext context) {
+    private BackendResponse execute(final CreateDataSourcesStatementContext context) {
         UpdateResponse result = new UpdateResponse();
         result.setType("CREATE");
         return result;
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/generator/YamlConfigurationGenerator.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/generator/YamlConfigurationGenerator.java
index eb6dac4..1240986 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/generator/YamlConfigurationGenerator.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/generator/YamlConfigurationGenerator.java
@@ -17,19 +17,18 @@
 
 package org.apache.shardingsphere.proxy.backend.communication.jdbc.execute.generator;
 
-import org.apache.shardingsphere.infra.yaml.config.YamlConfiguration;
 import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
 
 /**
  * Yaml configuration generator.
  */
-public interface YamlConfigurationGenerator<I extends SQLStatementContext, O extends YamlConfiguration> {
+public interface YamlConfigurationGenerator<I extends SQLStatementContext, O> {
     
     /**
      * Generate yaml configuration.
      *
      * @param sqlStatement sql statement
-     * @return yaml configuration
+     * @return yaml configurations
      */
     O generate(I sqlStatement);
 }
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/generator/YamlDataSourceConfigurationGenerator.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/generator/YamlDataSourceConfigurationGenerator.java
index 50c732f..87d1359 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/generator/YamlDataSourceConfigurationGenerator.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/communication/jdbc/execute/generator/YamlDataSourceConfigurationGenerator.java
@@ -18,19 +18,26 @@
 package org.apache.shardingsphere.proxy.backend.communication.jdbc.execute.generator;
 
 import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
-import org.apache.shardingsphere.sql.parser.binder.statement.ddl.CreateDataSourceStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.ddl.CreateDataSourcesStatementContext;
+
+import java.util.Collection;
+import java.util.LinkedList;
 
 /**
  * Yaml dataSource configuration generator.
  */
-public class YamlDataSourceConfigurationGenerator implements YamlConfigurationGenerator<CreateDataSourceStatementContext, YamlDataSourceParameter> {
+public class YamlDataSourceConfigurationGenerator implements YamlConfigurationGenerator<CreateDataSourcesStatementContext, Collection<YamlDataSourceParameter>> {
     
     @Override
-    public YamlDataSourceParameter generate(final CreateDataSourceStatementContext sqlStatement) {
-        YamlDataSourceParameter result = new YamlDataSourceParameter();
-        result.setUrl(sqlStatement.getUrl());
-        result.setUsername(sqlStatement.getUsername());
-        result.setPassword(sqlStatement.getPassword());
+    public Collection<YamlDataSourceParameter> generate(final CreateDataSourcesStatementContext sqlStatement) {
+        Collection<YamlDataSourceParameter> result = new LinkedList<>();
+        for (CreateDataSourcesStatementContext.DataSourceContext each : sqlStatement.getDataSourceContexts()) {
+            YamlDataSourceParameter dataSource = new YamlDataSourceParameter();
+            dataSource.setUrl(each.getUrl());
+            dataSource.setUsername(each.getUserName());
+            dataSource.setPassword(each.getPassword());
+            result.add(dataSource);
+        }
         return result;
     }
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateDataSourceStatementContext.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateDataSourcesStatementContext.java
similarity index 60%
rename from shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateDataSourceStatementContext.java
rename to shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateDataSourcesStatementContext.java
index 6950d0a..be64d13 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateDataSourceStatementContext.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateDataSourcesStatementContext.java
@@ -17,42 +17,40 @@
 
 package org.apache.shardingsphere.sql.parser.binder.statement.ddl;
 
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
 import org.apache.shardingsphere.sql.parser.binder.statement.CommonSQLStatementContext;
-import org.apache.shardingsphere.sql.parser.sql.statement.ddl.CreateDataSourceStatement;
+import org.apache.shardingsphere.sql.parser.sql.statement.ddl.CreateDataSourcesStatement;
+
+import java.util.Collection;
+import java.util.LinkedList;
 
 /**
  * Create dataSource statement context.
  */
-public class CreateDataSourceStatementContext extends CommonSQLStatementContext<CreateDataSourceStatement> {
+public final class CreateDataSourcesStatementContext extends CommonSQLStatementContext<CreateDataSourcesStatement> {
     
-    public CreateDataSourceStatementContext(final CreateDataSourceStatement sqlStatement) {
+    public CreateDataSourcesStatementContext(final CreateDataSourcesStatement sqlStatement) {
         super(sqlStatement);
     }
     
     /**
-     * Get url.
+     * Get dataSource contexts.
      *
-     * @return datasource url
+     * @return dataSource contexts
      */
-    public String getUrl() {
-        return "";
+    public Collection<DataSourceContext> getDataSourceContexts() {
+        return new LinkedList<>();
     }
     
-    /**
-     * Get username.
-     *
-     * @return username
-     */
-    public String getUsername() {
-        return "";
-    }
-    
-    /**
-     * Get password.
-     *
-     * @return password
-     */
-    public String getPassword() {
-        return "";
+    @RequiredArgsConstructor
+    @Getter
+    public final class DataSourceContext {
+        
+        private final String url;
+        
+        private final String userName;
+        
+        private final String password;
     }
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateShardingRuleStatementContext.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateShardingRuleStatementContext.java
index 416b305..bb8337b 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateShardingRuleStatementContext.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-binder/src/main/java/org/apache/shardingsphere/sql/parser/binder/statement/ddl/CreateShardingRuleStatementContext.java
@@ -27,7 +27,7 @@ import java.util.Properties;
 /**
  * Create sharding rule statement context.
  */
-public class CreateShardingRuleStatementContext extends CommonSQLStatementContext<CreateShardingRuleStatement> {
+public final class CreateShardingRuleStatementContext extends CommonSQLStatementContext<CreateShardingRuleStatement> {
     
     public CreateShardingRuleStatementContext(final CreateShardingRuleStatement sqlStatement) {
         super(sqlStatement);
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/ddl/CreateDataSourceStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/ddl/CreateDataSourcesStatement.java
similarity index 92%
rename from shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/ddl/CreateDataSourceStatement.java
rename to shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/ddl/CreateDataSourcesStatement.java
index 6090ba5..f5bc824 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/ddl/CreateDataSourceStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/ddl/CreateDataSourcesStatement.java
@@ -20,5 +20,5 @@ package org.apache.shardingsphere.sql.parser.sql.statement.ddl;
 /**
  * Create dataSource statement.
  */
-public class CreateDataSourceStatement extends DDLStatement {
+public final class CreateDataSourcesStatement extends DDLStatement {
 }
diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/ddl/CreateShardingRuleStatement.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/ddl/CreateShardingRuleStatement.java
index cf0ca41..9d761b8 100644
--- a/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/ddl/CreateShardingRuleStatement.java
+++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/statement/ddl/CreateShardingRuleStatement.java
@@ -20,5 +20,5 @@ package org.apache.shardingsphere.sql.parser.sql.statement.ddl;
 /**
  * Create sharding rule statement.
  */
-public class CreateShardingRuleStatement extends DDLStatement {
+public final class CreateShardingRuleStatement extends DDLStatement {
 }