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 2023/01/30 07:47:54 UTC

[shardingsphere] branch master updated: Replace `ShardingTableRuleResultSet` with `ShowShardingTableRuleExecutor` (#23833)

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 81679560a79 Replace `ShardingTableRuleResultSet` with `ShowShardingTableRuleExecutor` (#23833)
81679560a79 is described below

commit 81679560a79d19892a43127c09acf4b0260d579d
Author: Zichao <57...@users.noreply.github.com>
AuthorDate: Mon Jan 30 20:47:47 2023 +1300

    Replace `ShardingTableRuleResultSet` with `ShowShardingTableRuleExecutor` (#23833)
---
 ...Set.java => ShowShardingTableRuleExecutor.java} | 92 ++++++++--------------
 ...hardingsphere.distsql.handler.query.RQLExecutor | 18 +++++
 ...here.distsql.handler.resultset.DistSQLResultSet |  1 -
 ...java => ShowShardingTableRuleExecutorTest.java} | 76 ++++++++++++------
 4 files changed, 102 insertions(+), 85 deletions(-)

diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShardingTableRuleResultSet.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRuleExecutor.java
similarity index 71%
rename from features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShardingTableRuleResultSet.java
rename to features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRuleExecutor.java
index dd92dcdf89b..9964b12f5c7 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShardingTableRuleResultSet.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRuleExecutor.java
@@ -17,8 +17,9 @@
 
 package org.apache.shardingsphere.sharding.distsql.handler.query;
 
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
 import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
+import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.util.props.PropertiesConverter;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
@@ -32,7 +33,6 @@ import org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingS
 import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
 import org.apache.shardingsphere.sharding.distsql.parser.statement.ShowShardingTableRulesStatement;
 import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 
 import java.util.Arrays;
 import java.util.Collection;
@@ -44,24 +44,22 @@ import java.util.Optional;
 import java.util.stream.Collectors;
 
 /**
- * Result set for show sharding table rules.
+ * Show sharding table rules executor.
  */
-public final class ShardingTableRuleResultSet implements DatabaseDistSQLResultSet {
-    
-    private Iterator<ShardingTableRuleConfiguration> tables = Collections.emptyIterator();
-    
-    private Iterator<ShardingAutoTableRuleConfiguration> autoTables = Collections.emptyIterator();
+public final class ShowShardingTableRuleExecutor implements RQLExecutor<ShowShardingTableRulesStatement> {
     
     private ShardingRuleConfiguration shardingRuleConfig;
     
     @Override
-    public void init(final ShardingSphereDatabase database, final SQLStatement sqlStatement) {
+    public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableRulesStatement sqlStatement) {
         Optional<ShardingRule> rule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
         if (!rule.isPresent()) {
-            return;
+            return Collections.emptyList();
         }
         ShardingRuleConfiguration config = (ShardingRuleConfiguration) rule.get().getConfiguration();
-        String tableName = ((ShowShardingTableRulesStatement) sqlStatement).getTableName();
+        String tableName = sqlStatement.getTableName();
+        Iterator<ShardingTableRuleConfiguration> tables;
+        Iterator<ShardingAutoTableRuleConfiguration> autoTables;
         if (Objects.isNull(tableName)) {
             tables = config.getTables().iterator();
             autoTables = config.getAutoTables().iterator();
@@ -70,6 +68,14 @@ public final class ShardingTableRuleResultSet implements DatabaseDistSQLResultSe
             autoTables = config.getAutoTables().stream().filter(each -> tableName.equalsIgnoreCase(each.getLogicTable())).collect(Collectors.toList()).iterator();
         }
         shardingRuleConfig = config;
+        Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+        while (tables.hasNext()) {
+            result.add(buildTableRowData(tables.next()));
+        }
+        while (autoTables.hasNext()) {
+            result.add(buildAutoTableRowData(autoTables.next()));
+        }
+        return result;
     }
     
     @Override
@@ -79,59 +85,27 @@ public final class ShardingTableRuleResultSet implements DatabaseDistSQLResultSe
                 "key_generate_column", "key_generator_type", "key_generator_props", "auditor_types", "allow_hint_disable");
     }
     
-    @Override
-    public boolean next() {
-        return tables.hasNext() || autoTables.hasNext();
-    }
-    
-    @Override
-    public Collection<Object> getRowData() {
-        return tables.hasNext() ? buildTableRowData(tables.next()) : buildAutoTableRowData(autoTables.next());
-    }
-    
-    private Collection<Object> buildTableRowData(final ShardingTableRuleConfiguration shardingTableRuleConfig) {
-        Collection<Object> result = new LinkedList<>();
-        result.add(shardingTableRuleConfig.getLogicTable());
-        result.add(shardingTableRuleConfig.getActualDataNodes());
-        result.add("");
+    private LocalDataQueryResultRow buildTableRowData(final ShardingTableRuleConfiguration shardingTableRuleConfig) {
         Optional<ShardingStrategyConfiguration> databaseShardingStrategyConfig = getDatabaseShardingStrategy(shardingTableRuleConfig);
-        result.add(databaseShardingStrategyConfig.map(this::getStrategyType).orElse(""));
-        result.add(databaseShardingStrategyConfig.map(this::getShardingColumn).orElse(""));
-        result.add(databaseShardingStrategyConfig.map(this::getAlgorithmType).orElse(""));
-        result.add(databaseShardingStrategyConfig.map(this::getAlgorithmProperties).orElse(""));
         Optional<ShardingStrategyConfiguration> tableShardingStrategyConfig = getTableShardingStrategy(shardingTableRuleConfig.getTableShardingStrategy());
-        result.add(tableShardingStrategyConfig.map(this::getStrategyType).orElse(""));
-        result.add(tableShardingStrategyConfig.map(this::getShardingColumn).orElse(""));
-        result.add(tableShardingStrategyConfig.map(this::getAlgorithmType).orElse(""));
-        result.add(tableShardingStrategyConfig.map(this::getAlgorithmProperties).orElse(""));
-        result.add(getKeyGenerateColumn(shardingTableRuleConfig.getKeyGenerateStrategy()));
-        result.add(getKeyGeneratorType(shardingTableRuleConfig.getKeyGenerateStrategy()));
-        result.add(getKeyGeneratorProps(shardingTableRuleConfig.getKeyGenerateStrategy()));
-        result.add(getAuditorTypes(shardingTableRuleConfig.getAuditStrategy()));
-        result.add(getAllowHintDisable(shardingTableRuleConfig.getAuditStrategy()));
-        return result;
+        return new LocalDataQueryResultRow(shardingTableRuleConfig.getLogicTable(), shardingTableRuleConfig.getActualDataNodes(), "",
+                databaseShardingStrategyConfig.map(this::getStrategyType).orElse(""), databaseShardingStrategyConfig.map(this::getShardingColumn).orElse(""),
+                databaseShardingStrategyConfig.map(this::getAlgorithmType).orElse(""), databaseShardingStrategyConfig.map(this::getAlgorithmProperties).orElse(""),
+                tableShardingStrategyConfig.map(this::getStrategyType).orElse(""), tableShardingStrategyConfig.map(this::getShardingColumn).orElse(""),
+                tableShardingStrategyConfig.map(this::getAlgorithmType).orElse(""), tableShardingStrategyConfig.map(this::getAlgorithmProperties).orElse(""),
+                getKeyGenerateColumn(shardingTableRuleConfig.getKeyGenerateStrategy()), getKeyGeneratorType(shardingTableRuleConfig.getKeyGenerateStrategy()),
+                getKeyGeneratorProps(shardingTableRuleConfig.getKeyGenerateStrategy()), getAuditorTypes(shardingTableRuleConfig.getAuditStrategy()),
+                getAllowHintDisable(shardingTableRuleConfig.getAuditStrategy()));
     }
     
-    private Collection<Object> buildAutoTableRowData(final ShardingAutoTableRuleConfiguration shardingAutoTableRuleConfig) {
-        Collection<Object> result = new LinkedList<>();
-        result.add(shardingAutoTableRuleConfig.getLogicTable());
-        result.add("");
-        result.add(shardingAutoTableRuleConfig.getActualDataSources());
-        result.add("");
-        result.add("");
-        result.add("");
-        result.add("");
+    private LocalDataQueryResultRow buildAutoTableRowData(final ShardingAutoTableRuleConfiguration shardingAutoTableRuleConfig) {
         Optional<ShardingStrategyConfiguration> tableShardingStrategyConfig = getTableShardingStrategy(shardingAutoTableRuleConfig.getShardingStrategy());
-        result.add(tableShardingStrategyConfig.map(this::getStrategyType).orElse(""));
-        result.add(tableShardingStrategyConfig.map(this::getShardingColumn).orElse(""));
-        result.add(tableShardingStrategyConfig.map(this::getAlgorithmType).orElse(""));
-        result.add(tableShardingStrategyConfig.map(this::getAlgorithmProperties).orElse(""));
-        result.add(getKeyGenerateColumn(shardingAutoTableRuleConfig.getKeyGenerateStrategy()));
-        result.add(getKeyGeneratorType(shardingAutoTableRuleConfig.getKeyGenerateStrategy()));
-        result.add(getKeyGeneratorProps(shardingAutoTableRuleConfig.getKeyGenerateStrategy()));
-        result.add(getAuditorTypes(shardingAutoTableRuleConfig.getAuditStrategy()));
-        result.add(getAllowHintDisable(shardingAutoTableRuleConfig.getAuditStrategy()));
-        return result;
+        return new LocalDataQueryResultRow(shardingAutoTableRuleConfig.getLogicTable(), "", shardingAutoTableRuleConfig.getActualDataSources(), "", "", "", "",
+                tableShardingStrategyConfig.map(this::getStrategyType).orElse(""), tableShardingStrategyConfig.map(this::getShardingColumn).orElse(""),
+                tableShardingStrategyConfig.map(this::getAlgorithmType).orElse(""), tableShardingStrategyConfig.map(this::getAlgorithmProperties).orElse(""),
+                getKeyGenerateColumn(shardingAutoTableRuleConfig.getKeyGenerateStrategy()), getKeyGeneratorType(shardingAutoTableRuleConfig.getKeyGenerateStrategy()),
+                getKeyGeneratorProps(shardingAutoTableRuleConfig.getKeyGenerateStrategy()), getAuditorTypes(shardingAutoTableRuleConfig.getAuditStrategy()),
+                getAllowHintDisable(shardingAutoTableRuleConfig.getAuditStrategy()));
     }
     
     private String getShardingColumn(final ShardingStrategyConfiguration shardingStrategyConfig) {
diff --git a/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
new file mode 100644
index 00000000000..6e2a8cb4608
--- /dev/null
+++ b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
@@ -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.
+#
+
+org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingTableRuleExecutor
diff --git a/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
index 9a6a82d934b..06785209408 100644
--- a/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++ b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
@@ -15,7 +15,6 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.sharding.distsql.handler.query.ShardingTableRuleResultSet
 org.apache.shardingsphere.sharding.distsql.handler.query.ShardingTableReferenceRuleResultSet
 org.apache.shardingsphere.sharding.distsql.handler.query.BroadcastTableRuleResultSet
 org.apache.shardingsphere.sharding.distsql.handler.query.ShardingAlgorithmResultSet
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShardingTableRuleResultSetTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java
similarity index 62%
rename from features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShardingTableRuleResultSetTest.java
rename to features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java
index 4397b1e118a..cd2cd7c7164 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShardingTableRuleResultSetTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java
@@ -17,9 +17,10 @@
 
 package org.apache.shardingsphere.sharding.distsql.query;
 
-import org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
 import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
 import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
 import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
@@ -28,16 +29,16 @@ import org.apache.shardingsphere.sharding.api.config.strategy.audit.ShardingAudi
 import org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration;
 import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
-import org.apache.shardingsphere.sharding.distsql.handler.query.ShardingTableRuleResultSet;
+import org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingTableRuleExecutor;
 import org.apache.shardingsphere.sharding.distsql.parser.statement.ShowShardingTableRulesStatement;
 import org.apache.shardingsphere.sharding.rule.ShardingRule;
 import org.apache.shardingsphere.test.util.PropertiesBuilder;
 import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
 import org.junit.Test;
 
-import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Collections;
-import java.util.List;
+import java.util.Iterator;
 import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -46,30 +47,55 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
-public final class ShardingTableRuleResultSetTest {
+public final class ShowShardingTableRuleExecutorTest {
     
     @Test
     public void assertGetRowData() {
-        DatabaseDistSQLResultSet resultSet = new ShardingTableRuleResultSet();
-        resultSet.init(mockDatabase(), mock(ShowShardingTableRulesStatement.class));
-        List<Object> actual = new ArrayList<>(resultSet.getRowData());
-        assertThat(actual.size(), is(16));
-        assertThat(actual.get(0), is("t_order"));
-        assertThat(actual.get(1), is("ds_${0..1}.t_order_${0..1}"));
-        assertThat(actual.get(2), is(""));
-        assertThat(actual.get(3), is("STANDARD"));
-        assertThat(actual.get(4), is("user_id"));
-        assertThat(actual.get(5), is("INLINE"));
-        assertThat(actual.get(6), is("algorithm-expression=ds_${user_id % 2}"));
-        assertThat(actual.get(7), is("STANDARD"));
-        assertThat(actual.get(8), is("order_id"));
-        assertThat(actual.get(9), is("INLINE"));
-        assertThat(actual.get(10), is("algorithm-expression=t_order_${order_id % 2}"));
-        assertThat(actual.get(11), is("order_id"));
-        assertThat(actual.get(12), is("SNOWFLAKE"));
-        assertThat(actual.get(13), is(""));
-        assertThat(actual.get(14), is("DML_SHARDING_CONDITIONS"));
-        assertThat(actual.get(15), is("true"));
+        RQLExecutor<ShowShardingTableRulesStatement> executor = new ShowShardingTableRuleExecutor();
+        Collection<LocalDataQueryResultRow> actual = executor.getRows(mockDatabase(), mock(ShowShardingTableRulesStatement.class));
+        assertThat(actual.size(), is(1));
+        Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+        LocalDataQueryResultRow row = iterator.next();
+        assertThat(row.getCell(1), is("t_order"));
+        assertThat(row.getCell(2), is("ds_${0..1}.t_order_${0..1}"));
+        assertThat(row.getCell(3), is(""));
+        assertThat(row.getCell(4), is("STANDARD"));
+        assertThat(row.getCell(5), is("user_id"));
+        assertThat(row.getCell(6), is("INLINE"));
+        assertThat(row.getCell(7), is("algorithm-expression=ds_${user_id % 2}"));
+        assertThat(row.getCell(8), is("STANDARD"));
+        assertThat(row.getCell(9), is("order_id"));
+        assertThat(row.getCell(10), is("INLINE"));
+        assertThat(row.getCell(11), is("algorithm-expression=t_order_${order_id % 2}"));
+        assertThat(row.getCell(12), is("order_id"));
+        assertThat(row.getCell(13), is("SNOWFLAKE"));
+        assertThat(row.getCell(14), is(""));
+        assertThat(row.getCell(15), is("DML_SHARDING_CONDITIONS"));
+        assertThat(row.getCell(16), is("true"));
+    }
+    
+    @Test
+    public void assertGetColumnNames() {
+        RQLExecutor<ShowShardingTableRulesStatement> executor = new ShowShardingTableRuleExecutor();
+        Collection<String> columns = executor.getColumnNames();
+        assertThat(columns.size(), is(16));
+        Iterator<String> iterator = columns.iterator();
+        assertThat(iterator.next(), is("table"));
+        assertThat(iterator.next(), is("actual_data_nodes"));
+        assertThat(iterator.next(), is("actual_data_sources"));
+        assertThat(iterator.next(), is("database_strategy_type"));
+        assertThat(iterator.next(), is("database_sharding_column"));
+        assertThat(iterator.next(), is("database_sharding_algorithm_type"));
+        assertThat(iterator.next(), is("database_sharding_algorithm_props"));
+        assertThat(iterator.next(), is("table_strategy_type"));
+        assertThat(iterator.next(), is("table_sharding_column"));
+        assertThat(iterator.next(), is("table_sharding_algorithm_type"));
+        assertThat(iterator.next(), is("table_sharding_algorithm_props"));
+        assertThat(iterator.next(), is("key_generate_column"));
+        assertThat(iterator.next(), is("key_generator_type"));
+        assertThat(iterator.next(), is("key_generator_props"));
+        assertThat(iterator.next(), is("auditor_types"));
+        assertThat(iterator.next(), is("allow_hint_disable"));
     }
     
     private ShardingSphereDatabase mockDatabase() {