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 2024/04/09 12:30:38 UTC

(shardingsphere) branch master updated: Check null data sources or null rules when convert yaml configuration (#30821)

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

zhaojinchao 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 be22e4227ae Check null data sources or null rules when convert yaml configuration (#30821)
be22e4227ae is described below

commit be22e4227ae549c8625a613afdb30c3f732a0276
Author: Raigor <ra...@gmail.com>
AuthorDate: Tue Apr 9 20:30:31 2024 +0800

    Check null data sources or null rules when convert yaml configuration (#30821)
---
 .../queryable/ConvertYamlConfigurationExecutor.java | 18 ++++++++++--------
 .../ConvertYamlConfigurationExecutorTest.java       |  5 +++++
 .../test/resources/conf/convert/database-empty.yaml | 21 +++++++++++++++++++++
 .../resources/expected/convert-empty-database.yaml  | 18 ++++++++++++++++++
 4 files changed, 54 insertions(+), 8 deletions(-)

diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutor.java
index fbb040f346a..7c5667d18d3 100644
--- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutor.java
+++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutor.java
@@ -81,16 +81,18 @@ public final class ConvertYamlConfigurationExecutor implements DistSQLQueryExecu
     private String convertYamlConfigToDistSQL(final YamlProxyDatabaseConfiguration yamlConfig) {
         StringBuilder result = new StringBuilder();
         result.append(convertDatabase(yamlConfig.getDatabaseName()));
-        if (!yamlConfig.getDataSources().isEmpty()) {
-            result.append(System.lineSeparator()).append(System.lineSeparator());
-            result.append(convertDataSources(yamlConfig.getDataSources()));
+        result.append(System.lineSeparator()).append(System.lineSeparator());
+        if (null == yamlConfig.getDataSources() || yamlConfig.getDataSources().isEmpty()) {
+            return result.toString();
+        }
+        result.append(convertDataSources(yamlConfig.getDataSources()));
+        result.append(System.lineSeparator()).append(System.lineSeparator());
+        if (null == yamlConfig.getRules() || yamlConfig.getRules().isEmpty()) {
+            return result.toString();
         }
-        if (!yamlConfig.getRules().isEmpty()) {
+        for (RuleConfiguration each : swapToRuleConfigs(yamlConfig).values()) {
+            result.append(TypedSPILoader.getService(RuleConfigurationToDistSQLConverter.class, each.getClass()).convert(each));
             result.append(System.lineSeparator()).append(System.lineSeparator());
-            for (RuleConfiguration each : swapToRuleConfigs(yamlConfig).values()) {
-                result.append(TypedSPILoader.getService(RuleConfigurationToDistSQLConverter.class, each.getClass()).convert(each));
-                result.append(System.lineSeparator()).append(System.lineSeparator());
-            }
         }
         return result.toString();
     }
diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutorTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutorTest.java
index bd9909b86ae..272ba82a3e2 100644
--- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutorTest.java
+++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutorTest.java
@@ -43,6 +43,11 @@ class ConvertYamlConfigurationExecutorTest {
     
     private final SQLParserRule sqlParserRule = new SQLParserRule(new DefaultSQLParserRuleConfigurationBuilder().build());
     
+    @Test
+    void assertExecuteWithEmptyDatabase() {
+        assertExecute("/conf/convert/database-empty.yaml", "/expected/convert-empty-database.yaml");
+    }
+    
     @Test
     void assertExecuteWithSharding() {
         assertExecute("/conf/convert/database-sharding.yaml", "/expected/convert-sharding.yaml");
diff --git a/proxy/backend/core/src/test/resources/conf/convert/database-empty.yaml b/proxy/backend/core/src/test/resources/conf/convert/database-empty.yaml
new file mode 100644
index 00000000000..aa2b0d41274
--- /dev/null
+++ b/proxy/backend/core/src/test/resources/conf/convert/database-empty.yaml
@@ -0,0 +1,21 @@
+#
+# 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.
+#
+
+databaseName: empty_db
+
+dataSources:
+rules:
diff --git a/proxy/backend/core/src/test/resources/expected/convert-empty-database.yaml b/proxy/backend/core/src/test/resources/expected/convert-empty-database.yaml
new file mode 100644
index 00000000000..d753be37d29
--- /dev/null
+++ b/proxy/backend/core/src/test/resources/expected/convert-empty-database.yaml
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+CREATE DATABASE empty_db;
+USE empty_db;