You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/07/19 08:26:25 UTC

[shardingsphere] branch master updated: CREATE DATABASE & ADD RESOURCE distsql finish (#19280)

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

jianglongtao 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 6e406a2dbbc CREATE DATABASE & ADD RESOURCE distsql finish (#19280)
6e406a2dbbc is described below

commit 6e406a2dbbc389e9156f851da2642b3aedda9a35
Author: Xin Huang <57...@users.noreply.github.com>
AuthorDate: Tue Jul 19 16:26:18 2022 +0800

    CREATE DATABASE & ADD RESOURCE distsql finish (#19280)
    
    * create & add resource distsql finish conflict fixed
    
    * create & add resource distsql unpdate print format
    
    * create & add resource distsql unpdate if null format
---
 .../src/main/antlr4/imports/Keyword.g4             |   8 ++
 .../src/main/antlr4/imports/RALStatement.g4        |   4 +
 .../parser/autogen/KernelDistSQLStatement.g4       |   1 +
 .../core/kernel/KernelDistSQLStatementVisitor.java |   7 ++
 .../ConvertYamlConfigurationStatement.java         |  32 ++++++
 .../text/distsql/ral/RALBackendHandlerFactory.java |   3 +
 .../queryable/ConvertYamlConfigurationHandler.java | 127 +++++++++++++++++++++
 7 files changed, 182 insertions(+)

diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4 b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4
index a51cb2cff69..929aa4e13d2 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/Keyword.g4
@@ -275,6 +275,14 @@ IMPORT
     : I M P O R T
     ;
 
+CONVERT
+    : C O N V E R T
+    ;
+
+YAML
+    : Y A M L
+    ;
+
 CONFIGURATION
     : C O N F I G U R A T I O N
     ;
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RALStatement.g4 b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RALStatement.g4
index 4c978393663..cb9dd5e0a21 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RALStatement.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/imports/RALStatement.g4
@@ -139,6 +139,10 @@ importDatabaseConfiguration
     : IMPORT DATABASE (CONFIGURATION | CONFIG) FILE EQ filePath
     ;
 
+convertYamlConfiguration
+    : CONVERT YAML (CONFIGURATION | CONFIG) FILE EQ filePath
+    ;
+
 filePath
     : STRING
     ;
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/KernelDistSQLStatement.g4 b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/KernelDistSQLStatement.g4
index 85c5e312b1d..78fba169281 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/KernelDistSQLStatement.g4
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/KernelDistSQLStatement.g4
@@ -60,5 +60,6 @@ execute
     | exportDatabaseConfiguration
     | showRulesUsedResource
     | importDatabaseConfiguration
+    | convertYamlConfiguration
     ) SEMI?
     ;
diff --git a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
index e656a59a4b2..d4dfa25b96a 100644
--- a/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
+++ b/shardingsphere-distsql/shardingsphere-distsql-parser/src/main/java/org/apache/shardingsphere/distsql/parser/core/kernel/KernelDistSQLStatementVisitor.java
@@ -45,6 +45,7 @@ import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementPa
 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.DropTrafficRuleContext;
 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.EnableInstanceContext;
 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ExportDatabaseConfigurationContext;
+import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ConvertYamlConfigurationContext;
 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.FromSegmentContext;
 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.ImportDatabaseConfigurationContext;
 import org.apache.shardingsphere.distsql.parser.autogen.KernelDistSQLStatementParser.InstanceIdContext;
@@ -83,6 +84,7 @@ import org.apache.shardingsphere.distsql.parser.segment.TrafficRuleSegment;
 import org.apache.shardingsphere.distsql.parser.segment.TransactionProviderSegment;
 import org.apache.shardingsphere.distsql.parser.statement.ral.hint.ClearHintStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ExportDatabaseConfigurationStatement;
+import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ConvertYamlConfigurationStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ShowAllVariableStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ShowAuthorityRuleStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ShowInstanceModeStatement;
@@ -442,6 +444,11 @@ public final class KernelDistSQLStatementVisitor extends KernelDistSQLStatementB
         return new ExportDatabaseConfigurationStatement(null == ctx.databaseName() ? null : (DatabaseSegment) visit(ctx.databaseName()), getIdentifierValue(ctx.filePath()));
     }
     
+    @Override
+    public ASTNode visitConvertYamlConfiguration(final ConvertYamlConfigurationContext ctx) {
+        return new ConvertYamlConfigurationStatement(getIdentifierValue(ctx.filePath()));
+    }
+    
     @Override
     public ASTNode visitShowRulesUsedResource(final ShowRulesUsedResourceContext ctx) {
         return new ShowRulesUsedResourceStatement(getIdentifierValue(ctx.resourceName()), null == ctx.databaseName() ? null : (DatabaseSegment) visit(ctx.databaseName()));
diff --git a/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/queryable/ConvertYamlConfigurationStatement.java b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/queryable/ConvertYamlConfigurationStatement.java
new file mode 100644
index 00000000000..247e262e3b6
--- /dev/null
+++ b/shardingsphere-distsql/shardingsphere-distsql-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/ral/queryable/ConvertYamlConfigurationStatement.java
@@ -0,0 +1,32 @@
+/*
+ * 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.distsql.parser.statement.ral.queryable;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.distsql.parser.statement.ral.QueryableRALStatement;
+
+/**
+ * Convert yaml configuration statement.
+ */
+@RequiredArgsConstructor
+@Getter
+public class ConvertYamlConfigurationStatement extends QueryableRALStatement {
+    
+    private final String filePath;
+}
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/RALBackendHandlerFactory.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/RALBackendHandlerFactory.java
index 286911cfe21..312428ad4ff 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/RALBackendHandlerFactory.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/RALBackendHandlerFactory.java
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.distsql.parser.statement.ral.advanced.FormatSta
 import org.apache.shardingsphere.distsql.parser.statement.ral.advanced.ParseStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.advanced.PreviewStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ExportDatabaseConfigurationStatement;
+import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ConvertYamlConfigurationStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ShowAllVariableStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ShowAuthorityRuleStatement;
 import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ShowInstanceModeStatement;
@@ -57,6 +58,7 @@ import org.apache.shardingsphere.proxy.backend.text.distsql.ral.advanced.ParseDi
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.advanced.PreviewHandler;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.hint.HintRALBackendHandler;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.queryable.ExportDatabaseConfigurationHandler;
+import org.apache.shardingsphere.proxy.backend.text.distsql.ral.queryable.ConvertYamlConfigurationHandler;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.queryable.ShowAllVariableHandler;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.queryable.ShowAuthorityRuleHandler;
 import org.apache.shardingsphere.proxy.backend.text.distsql.ral.queryable.ShowInstanceHandler;
@@ -127,6 +129,7 @@ public final class RALBackendHandlerFactory {
         HANDLERS.put(ShowTrafficRulesStatement.class, ShowTrafficRulesHandler.class);
         HANDLERS.put(ShowTransactionRuleStatement.class, ShowTransactionRuleHandler.class);
         HANDLERS.put(ExportDatabaseConfigurationStatement.class, ExportDatabaseConfigurationHandler.class);
+        HANDLERS.put(ConvertYamlConfigurationStatement.class, ConvertYamlConfigurationHandler.class);
         HANDLERS.put(ParseStatement.class, ParseDistSQLHandler.class);
         HANDLERS.put(PreviewStatement.class, PreviewHandler.class);
         HANDLERS.put(FormatStatement.class, FormatSQLHandler.class);
diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ConvertYamlConfigurationHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ConvertYamlConfigurationHandler.java
new file mode 100644
index 00000000000..fa971efd1f8
--- /dev/null
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/ral/queryable/ConvertYamlConfigurationHandler.java
@@ -0,0 +1,127 @@
+/*
+ * 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.proxy.backend.text.distsql.ral.queryable;
+
+import com.zaxxer.hikari.HikariDataSource;
+import org.apache.shardingsphere.distsql.parser.statement.ral.queryable.ConvertYamlConfigurationStatement;
+import org.apache.shardingsphere.infra.datasource.props.DataSourceProperties;
+import org.apache.shardingsphere.infra.datasource.props.DataSourcePropertiesCreator;
+import org.apache.shardingsphere.infra.exception.ShardingSphereException;
+import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDataSourceConfiguration;
+import org.apache.shardingsphere.proxy.backend.config.yaml.YamlProxyDatabaseConfiguration;
+import org.apache.shardingsphere.proxy.backend.config.yaml.swapper.YamlProxyDataSourceConfigurationSwapper;
+import org.apache.shardingsphere.proxy.backend.text.distsql.ral.QueryableRALBackendHandler;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * Convert database configuration handler.
+ */
+public class ConvertYamlConfigurationHandler extends QueryableRALBackendHandler<ConvertYamlConfigurationStatement> {
+
+    private static final String CREATE_DATABASE = "CREATE DATABASE %s;";
+    
+    private static final String ADD_RESOURCE = "ADD RESOURCE";
+    
+    private static final String RESOURCES = " %s (" + System.lineSeparator()
+                                            + "%s"
+                                            + "    PROPERTIES(%s)" + System.lineSeparator()
+                                            + "),";
+
+    private final YamlProxyDataSourceConfigurationSwapper dataSourceConfigSwapper = new YamlProxyDataSourceConfigurationSwapper();
+
+    @Override
+    protected Collection<String> getColumnNames() {
+        return Collections.singleton("converted_distsql");
+    }
+    
+    @Override
+    protected Collection<LocalDataQueryResultRow> getRows(final ContextManager contextManager) {
+        File file = new File(getSqlStatement().getFilePath());
+        YamlProxyDatabaseConfiguration yamlConfig;
+        try {
+            yamlConfig = YamlEngine.unmarshal(file, YamlProxyDatabaseConfiguration.class);
+        } catch (final IOException ex) {
+            throw new ShardingSphereException(ex);
+        }
+        String convertedDistSQL = generateConvertedDistSQL(yamlConfig);
+        return Collections.singleton(new LocalDataQueryResultRow(convertedDistSQL));
+    }
+    
+    private String generateConvertedDistSQL(final YamlProxyDatabaseConfiguration yamlConfig) {
+        StringBuilder convetedDistSQL = new StringBuilder();
+        appendCreateDatabaseDistSQL(yamlConfig.getDatabaseName(), convetedDistSQL);
+        appendAddResourceDistSQL(yamlConfig.getDataSources(), convetedDistSQL);
+        return convetedDistSQL.toString();
+    }
+    
+    private void appendCreateDatabaseDistSQL(final String databaseName, final StringBuilder stringBuilder) {
+        if (databaseName.isEmpty()) {
+            return;
+        }
+        stringBuilder.append(String.format(CREATE_DATABASE, databaseName));
+    }
+    
+    private void appendAddResourceDistSQL(final Map<String, YamlProxyDataSourceConfiguration> yamlDataSourceMap, final StringBuilder stringBuilder) {
+        if (yamlDataSourceMap.isEmpty()) {
+            return;
+        }
+        if (null == stringBuilder) {
+            stringBuilder.append(ADD_RESOURCE);
+        } else {
+            stringBuilder.append(String.format(System.lineSeparator() + System.lineSeparator() + ADD_RESOURCE));
+        }
+        Map<String, DataSourceProperties> dataSourcePropsMap = new LinkedHashMap<>(yamlDataSourceMap.size(), 1);
+        for (Map.Entry<String, YamlProxyDataSourceConfiguration> entry : yamlDataSourceMap.entrySet()) {
+            dataSourcePropsMap.put(entry.getKey(), DataSourcePropertiesCreator.create(HikariDataSource.class.getName(), dataSourceConfigSwapper.swap(entry.getValue())));
+        }
+        stringBuilder.append(ADD_RESOURCE);
+        dataSourcePropsMap.forEach((key, value) -> addResources(key, value, stringBuilder));
+        stringBuilder.deleteCharAt(stringBuilder.length() - 1).append(";");
+    }
+
+    private void addResources(final String resourceName, final DataSourceProperties properties, final StringBuilder stringBuilder) {
+        String connectProperties = new String();
+        String poolProperties = new String();
+        for (Map.Entry<String, Object> entry : properties.getConnectionPropertySynonyms().getStandardProperties().entrySet()) {
+            connectProperties = connectProperties.concat(String.format("    %s=%s," + System.lineSeparator(), entry.getKey(), entry.getValue()));
+        }
+        for (Map.Entry<String, Object> entry : properties.getPoolPropertySynonyms().getStandardProperties().entrySet()) {
+            if (null != entry.getValue()) {
+                poolProperties = poolProperties.concat(String.format("\"%s\"=%s, ", entry.getKey(), entry.getValue()));
+            }
+        }
+        for (Map.Entry<String, Object> entry : properties.getCustomDataSourceProperties().getProperties().entrySet()) {
+            if (entry.getValue().equals(false) || entry.getValue().equals(true)) {
+                poolProperties = poolProperties.concat(String.format("\"%s\"=%s, ", entry.getKey(), entry.getValue()));
+            } else {
+                poolProperties = poolProperties.concat(String.format("\"%s\"=\"%s\", ", entry.getKey(), entry.getValue()));
+            }
+        }
+        poolProperties = poolProperties.substring(0, poolProperties.length() - 2);
+        stringBuilder.append(String.format(RESOURCES, resourceName, connectProperties, poolProperties));
+    }
+}