You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by wu...@apache.org on 2021/06/10 07:30:19 UTC

[shardingsphere] branch master updated: Minor refactor CreateSharidngTableRuleBackendHandler (#10749)

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

wuweijie 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 3d86628  Minor refactor CreateSharidngTableRuleBackendHandler (#10749)
3d86628 is described below

commit 3d86628e96c8310117ae64d67cb97f2679c2f829
Author: Haoran Meng <me...@gmail.com>
AuthorDate: Thu Jun 10 15:29:03 2021 +0800

    Minor refactor CreateSharidngTableRuleBackendHandler (#10749)
    
    * Minor refactor CreateShardingTableRuleBackendHandler
    
    * Minor refactor CreateShardingTableRuleBackendHandler
---
 .../impl/CreateShardingTableRuleBackendHandler.java  | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
index aebc60b..377e684 100644
--- a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
+++ b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/CreateShardingTableRuleBackendHandler.java
@@ -29,6 +29,8 @@ import org.apache.shardingsphere.proxy.backend.exception.InvalidKeyGeneratorsExc
 import org.apache.shardingsphere.proxy.backend.exception.InvalidShardingAlgorithmsException;
 import org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
 import org.apache.shardingsphere.sharding.converter.ShardingRuleStatementConverter;
 import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm;
 import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
@@ -36,6 +38,7 @@ import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.LinkedHashSet;
+import java.util.LinkedList;
 import java.util.Map.Entry;
 import java.util.Objects;
 import java.util.Optional;
@@ -64,7 +67,7 @@ public final class CreateShardingTableRuleBackendHandler extends RDLBackendHandl
         if (!notExistResources.isEmpty()) {
             throw new ResourceNotExistedException(schemaName, notExistResources);
         }
-        Collection<String> existLogicTables = getLogicTables(schemaName);
+        Collection<String> existLogicTables = getAllTables(schemaName);
         Set<String> duplicateTableNames = sqlStatement.getTables().stream().collect(Collectors.toMap(TableRuleSegment::getLogicTable, each -> 1, Integer::sum))
                 .entrySet().stream().filter(entry -> entry.getValue() > 1).map(Entry::getKey).collect(Collectors.toSet());
         duplicateTableNames.addAll(sqlStatement.getTables().stream().map(TableRuleSegment::getLogicTable).filter(existLogicTables::contains).collect(Collectors.toSet()));
@@ -99,8 +102,19 @@ public final class CreateShardingTableRuleBackendHandler extends RDLBackendHandl
         }
     }
     
-    private Collection<String> getLogicTables(final String schemaName) {
-        return ProxyContext.getInstance().getMetaData(schemaName).getSchema().getAllTableNames();
+    private Collection<String> getAllTables(final String schemaName) {
+        Collection<String> result = ProxyContext.getInstance().getMetaData(schemaName).getSchema().getAllTableNames();
+        if (getShardingRuleConfiguration(schemaName).isPresent()) {
+            result.addAll(getShardingTables(getShardingRuleConfiguration(schemaName).get()));
+        }
+        return result;
+    }
+    
+    private Collection<String> getShardingTables(final ShardingRuleConfiguration shardingRuleConfiguration) {
+        Collection<String> result = new LinkedList<>();
+        result.addAll(shardingRuleConfiguration.getTables().stream().map(ShardingTableRuleConfiguration::getLogicTable).collect(Collectors.toSet()));
+        result.addAll(shardingRuleConfiguration.getAutoTables().stream().map(ShardingAutoTableRuleConfiguration::getLogicTable).collect(Collectors.toSet()));
+        return result;
     }
     
     private Collection<String> getResources(final CreateShardingTableRuleStatement sqlStatement) {