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 7ee17ffae0b Refactor JDBC global rule config format specification 
(#25881)
7ee17ffae0b is described below

commit 7ee17ffae0b3983ded9b14bd4b4f385d3a8827e0
Author: ChenJiaHao <[email protected]>
AuthorDate: Thu May 25 16:12:15 2023 +0800

    Refactor JDBC global rule config format specification (#25881)
    
    * Refactor JDBC global rule config format specification
    
    * Fix comment
---
 .../driver/api/yaml/YamlJDBCConfiguration.java     | 87 ++++++++++++++++++++++
 .../yaml/YamlShardingSphereDataSourceFactory.java  | 79 ++++++++++++++------
 2 files changed, 143 insertions(+), 23 deletions(-)

diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/api/yaml/YamlJDBCConfiguration.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/api/yaml/YamlJDBCConfiguration.java
new file mode 100644
index 00000000000..40e914cfdfa
--- /dev/null
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/api/yaml/YamlJDBCConfiguration.java
@@ -0,0 +1,87 @@
+/*
+ * 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.driver.api.yaml;
+
+import com.google.common.base.Strings;
+import lombok.Getter;
+import lombok.Setter;
+import 
org.apache.shardingsphere.authority.yaml.config.YamlAuthorityRuleConfiguration;
+import 
org.apache.shardingsphere.globalclock.core.yaml.config.YamlGlobalClockRuleConfiguration;
+import org.apache.shardingsphere.infra.util.yaml.YamlConfiguration;
+import 
org.apache.shardingsphere.infra.yaml.config.pojo.mode.YamlModeConfiguration;
+import 
org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
+import 
org.apache.shardingsphere.logging.yaml.config.YamlLoggingRuleConfiguration;
+import 
org.apache.shardingsphere.parser.yaml.config.YamlSQLParserRuleConfiguration;
+import 
org.apache.shardingsphere.sqltranslator.yaml.config.YamlSQLTranslatorRuleConfiguration;
+import 
org.apache.shardingsphere.traffic.yaml.config.YamlTrafficRuleConfiguration;
+import 
org.apache.shardingsphere.transaction.yaml.config.YamlTransactionRuleConfiguration;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * YAML JDBC configuration.
+ */
+@Getter
+@Setter
+public final class YamlJDBCConfiguration implements YamlConfiguration {
+    
+    private String databaseName;
+    
+    /**
+     * Schema name.
+     * 
+     * @deprecated Should use databaseName, schemaName will remove in next 
version.
+     */
+    @Deprecated
+    private String schemaName;
+    
+    private Map<String, Map<String, Object>> dataSources = new HashMap<>();
+    
+    private Collection<YamlRuleConfiguration> rules = new LinkedList<>();
+    
+    private YamlModeConfiguration mode;
+    
+    private Properties props = new Properties();
+    
+    private YamlAuthorityRuleConfiguration authority;
+    
+    private YamlTransactionRuleConfiguration transaction;
+    
+    private YamlSQLParserRuleConfiguration sqlParser;
+    
+    private YamlSQLTranslatorRuleConfiguration sqlTranslator;
+    
+    private YamlTrafficRuleConfiguration traffic;
+    
+    private YamlLoggingRuleConfiguration logging;
+    
+    private YamlGlobalClockRuleConfiguration globalClock;
+    
+    /**
+     * Get database name.
+     * 
+     * @return database name
+     */
+    public String getDatabaseName() {
+        return Strings.isNullOrEmpty(databaseName) ? schemaName : databaseName;
+    }
+}
diff --git 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactory.java
 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactory.java
index ea6fc6db37c..09fcc5d6950 100644
--- 
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactory.java
+++ 
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactory.java
@@ -25,11 +25,12 @@ import 
org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import org.apache.shardingsphere.infra.database.DefaultDatabase;
 import 
org.apache.shardingsphere.infra.datasource.pool.destroyer.DataSourcePoolDestroyer;
+import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration;
+import 
org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlGlobalRuleConfiguration;
+import 
org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper;
 import 
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine;
-import 
org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper;
-import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
 
 import javax.sql.DataSource;
 import java.io.File;
@@ -52,7 +53,7 @@ public final class YamlShardingSphereDataSourceFactory {
     
     /**
      * Create ShardingSphere data source without cache.
-     *
+     * 
      * @param rootConfig rule configurations
      * @return ShardingSphere data source
      * @throws SQLException SQL exception
@@ -69,35 +70,41 @@ public final class YamlShardingSphereDataSourceFactory {
         }
     }
     
+    private static DataSource createDataSource(final Map<String, DataSource> 
dataSourceMap, final YamlRootConfiguration rootConfig) throws SQLException {
+        ModeConfiguration modeConfig = null == rootConfig.getMode() ? null : 
new YamlModeConfigurationSwapper().swapToObject(rootConfig.getMode());
+        Collection<RuleConfiguration> ruleConfigs = 
SWAPPER_ENGINE.swapToRuleConfigurations(rootConfig.getRules());
+        return 
ShardingSphereDataSourceFactory.createDataSource(rootConfig.getDatabaseName(), 
modeConfig, dataSourceMap, ruleConfigs, rootConfig.getProps());
+    }
+    
     /**
      * Create ShardingSphere data source.
-     *
+     * 
      * @param yamlFile YAML file for rule configurations
      * @return ShardingSphere data source
      * @throws SQLException SQL exception
      * @throws IOException IO exception
      */
     public static DataSource createDataSource(final File yamlFile) throws 
SQLException, IOException {
-        YamlRootConfiguration rootConfig = YamlEngine.unmarshal(yamlFile, 
YamlRootConfiguration.class);
+        YamlJDBCConfiguration rootConfig = YamlEngine.unmarshal(yamlFile, 
YamlJDBCConfiguration.class);
         return 
createDataSource(DATA_SOURCE_SWAPPER.swapToDataSources(rootConfig.getDataSources()),
 rootConfig);
     }
     
     /**
      * Create ShardingSphere data source.
-     *
+     * 
      * @param yamlBytes YAML bytes for rule configurations
      * @return ShardingSphere data source
      * @throws SQLException SQL exception
      * @throws IOException IO exception
      */
     public static DataSource createDataSource(final byte[] yamlBytes) throws 
SQLException, IOException {
-        YamlRootConfiguration rootConfig = YamlEngine.unmarshal(yamlBytes, 
YamlRootConfiguration.class);
+        YamlJDBCConfiguration rootConfig = YamlEngine.unmarshal(yamlBytes, 
YamlJDBCConfiguration.class);
         return 
createDataSource(DATA_SOURCE_SWAPPER.swapToDataSources(rootConfig.getDataSources()),
 rootConfig);
     }
     
     /**
      * Create ShardingSphere data source.
-     *
+     * 
      * @param dataSourceMap data source map
      * @param yamlFile YAML file for rule configurations
      * @return ShardingSphere data source
@@ -105,12 +112,12 @@ public final class YamlShardingSphereDataSourceFactory {
      * @throws IOException IO exception
      */
     public static DataSource createDataSource(final Map<String, DataSource> 
dataSourceMap, final File yamlFile) throws SQLException, IOException {
-        return createDataSource(dataSourceMap, YamlEngine.unmarshal(yamlFile, 
YamlRootConfiguration.class));
+        return createDataSource(dataSourceMap, YamlEngine.unmarshal(yamlFile, 
YamlJDBCConfiguration.class));
     }
     
     /**
      * Create ShardingSphere data source.
-     *
+     * 
      * @param dataSource data source
      * @param yamlFile YAML file for rule configurations
      * @return ShardingSphere data source
@@ -118,12 +125,12 @@ public final class YamlShardingSphereDataSourceFactory {
      * @throws IOException IO exception
      */
     public static DataSource createDataSource(final DataSource dataSource, 
final File yamlFile) throws SQLException, IOException {
-        return createDataSource(dataSource, YamlEngine.unmarshal(yamlFile, 
YamlRootConfiguration.class));
+        return createDataSource(dataSource, YamlEngine.unmarshal(yamlFile, 
YamlJDBCConfiguration.class));
     }
     
     /**
      * Create ShardingSphere data source.
-     *
+     * 
      * @param dataSourceMap data source map
      * @param yamlBytes YAML bytes for rule configurations
      * @return ShardingSphere data source
@@ -131,12 +138,12 @@ public final class YamlShardingSphereDataSourceFactory {
      * @throws IOException IO exception
      */
     public static DataSource createDataSource(final Map<String, DataSource> 
dataSourceMap, final byte[] yamlBytes) throws SQLException, IOException {
-        return createDataSource(dataSourceMap, YamlEngine.unmarshal(yamlBytes, 
YamlRootConfiguration.class));
+        return createDataSource(dataSourceMap, YamlEngine.unmarshal(yamlBytes, 
YamlJDBCConfiguration.class));
     }
     
     /**
      * Create ShardingSphere data source.
-     *
+     * 
      * @param dataSource data source
      * @param yamlBytes YAML bytes for rule configurations
      * @return ShardingSphere data source
@@ -144,18 +151,44 @@ public final class YamlShardingSphereDataSourceFactory {
      * @throws IOException IO exception
      */
     public static DataSource createDataSource(final DataSource dataSource, 
final byte[] yamlBytes) throws SQLException, IOException {
-        return createDataSource(dataSource, YamlEngine.unmarshal(yamlBytes, 
YamlRootConfiguration.class));
+        return createDataSource(dataSource, YamlEngine.unmarshal(yamlBytes, 
YamlJDBCConfiguration.class));
     }
     
-    private static DataSource createDataSource(final Map<String, DataSource> 
dataSourceMap, final YamlRootConfiguration rootConfig) throws SQLException {
-        ModeConfiguration modeConfig = null == rootConfig.getMode() ? null : 
new YamlModeConfigurationSwapper().swapToObject(rootConfig.getMode());
-        Collection<RuleConfiguration> ruleConfigs = 
SWAPPER_ENGINE.swapToRuleConfigurations(rootConfig.getRules());
-        return 
ShardingSphereDataSourceFactory.createDataSource(rootConfig.getDatabaseName(), 
modeConfig, dataSourceMap, ruleConfigs, rootConfig.getProps());
+    private static DataSource createDataSource(final DataSource dataSource, 
final YamlJDBCConfiguration jdbcConfig) throws SQLException {
+        Map<String, DataSource> dataSourceMap = new LinkedHashMap<>(
+                
Collections.singletonMap(Strings.isNullOrEmpty(jdbcConfig.getDatabaseName()) ? 
DefaultDatabase.LOGIC_NAME : jdbcConfig.getDatabaseName(), dataSource));
+        return createDataSource(dataSourceMap, jdbcConfig);
     }
     
-    private static DataSource createDataSource(final DataSource dataSource, 
final YamlRootConfiguration rootConfig) throws SQLException {
-        Map<String, DataSource> dataSourceMap = new LinkedHashMap<>(
-                
Collections.singletonMap(Strings.isNullOrEmpty(rootConfig.getDatabaseName()) ? 
DefaultDatabase.LOGIC_NAME : rootConfig.getDatabaseName(), dataSource));
-        return createDataSource(dataSourceMap, rootConfig);
+    private static DataSource createDataSource(final Map<String, DataSource> 
dataSourceMap, final YamlJDBCConfiguration jdbcConfig) throws SQLException {
+        rebuildGlobalRuleConfiguration(jdbcConfig);
+        ModeConfiguration modeConfig = null == jdbcConfig.getMode() ? null : 
new YamlModeConfigurationSwapper().swapToObject(jdbcConfig.getMode());
+        Collection<RuleConfiguration> ruleConfigs = 
SWAPPER_ENGINE.swapToRuleConfigurations(jdbcConfig.getRules());
+        return 
ShardingSphereDataSourceFactory.createDataSource(jdbcConfig.getDatabaseName(), 
modeConfig, dataSourceMap, ruleConfigs, jdbcConfig.getProps());
+    }
+    
+    private static void rebuildGlobalRuleConfiguration(final 
YamlJDBCConfiguration jdbcConfiguration) {
+        
jdbcConfiguration.getRules().removeIf(YamlGlobalRuleConfiguration.class::isInstance);
+        if (null != jdbcConfiguration.getAuthority()) {
+            jdbcConfiguration.getRules().add(jdbcConfiguration.getAuthority());
+        }
+        if (null != jdbcConfiguration.getTransaction()) {
+            
jdbcConfiguration.getRules().add(jdbcConfiguration.getTransaction());
+        }
+        if (null != jdbcConfiguration.getGlobalClock()) {
+            
jdbcConfiguration.getRules().add(jdbcConfiguration.getGlobalClock());
+        }
+        if (null != jdbcConfiguration.getSqlParser()) {
+            jdbcConfiguration.getRules().add(jdbcConfiguration.getSqlParser());
+        }
+        if (null != jdbcConfiguration.getSqlTranslator()) {
+            
jdbcConfiguration.getRules().add(jdbcConfiguration.getSqlTranslator());
+        }
+        if (null != jdbcConfiguration.getTraffic()) {
+            jdbcConfiguration.getRules().add(jdbcConfiguration.getTraffic());
+        }
+        if (null != jdbcConfiguration.getLogging()) {
+            jdbcConfiguration.getRules().add(jdbcConfiguration.getLogging());
+        }
     }
 }

Reply via email to