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 2163fc0  Add in-use validation for drop syntax. (#14785)
2163fc0 is described below

commit 2163fc0dc5a04f4d817571cb010f548f7a88e895
Author: yx9o <[email protected]>
AuthorDate: Fri Jan 14 21:39:30 2022 +0800

    Add in-use validation for drop syntax. (#14785)
---
 .../DropShardingKeyGeneratorStatementUpdater.java  | 33 +++++++++++++++++++---
 ...teShardingKeyGeneratorStatementUpdaterTest.java |  6 ++--
 ...opShardingKeyGeneratorStatementUpdaterTest.java | 33 ++++++++++++++--------
 ...ption.java => KeyGeneratorInUsedException.java} | 16 ++++++-----
 .../rule/RequiredKeyGeneratorMissedException.java  |  5 ++--
 5 files changed, 65 insertions(+), 28 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingKeyGeneratorStatementUpdater.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingKeyGeneratorStatementUpdater.java
index a84e73c..9c412b4 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingKeyGeneratorStatementUpdater.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/update/DropShardingKeyGeneratorStatementUpdater.java
@@ -17,17 +17,22 @@
 
 package org.apache.shardingsphere.sharding.distsql.handler.update;
 
+import com.google.common.base.Strings;
 import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
 import 
org.apache.shardingsphere.infra.distsql.exception.rule.DuplicateKeyGeneratorException;
+import 
org.apache.shardingsphere.infra.distsql.exception.rule.KeyGeneratorInUsedException;
 import 
org.apache.shardingsphere.infra.distsql.exception.rule.RequiredKeyGeneratorMissedException;
 import 
org.apache.shardingsphere.infra.distsql.update.RuleDefinitionDropUpdater;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import 
org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
 import 
org.apache.shardingsphere.sharding.distsql.parser.statement.DropShardingKeyGeneratorStatement;
 
 import java.util.Collection;
+import java.util.LinkedHashSet;
 import java.util.LinkedList;
 import java.util.Map;
+import java.util.Objects;
 import java.util.stream.Collectors;
 
 /**
@@ -35,6 +40,8 @@ import java.util.stream.Collectors;
  */
 public final class DropShardingKeyGeneratorStatementUpdater implements 
RuleDefinitionDropUpdater<DropShardingKeyGeneratorStatement, 
ShardingRuleConfiguration> {
     
+    private static final String TYPE = 
DropShardingKeyGeneratorStatement.class.getCanonicalName();
+    
     @Override
     public void checkSQLStatement(final ShardingSphereMetaData 
shardingSphereMetaData, final DropShardingKeyGeneratorStatement sqlStatement,
                                   final ShardingRuleConfiguration 
currentRuleConfig) throws DistSQLException {
@@ -42,8 +49,9 @@ public final class DropShardingKeyGeneratorStatementUpdater 
implements RuleDefin
         Collection<String> keyGeneratorNames = 
sqlStatement.getKeyGeneratorNames().stream().collect(Collectors.toCollection(LinkedList::new));
         checkDuplicate(schemaName, keyGeneratorNames);
         checkExist(schemaName, keyGeneratorNames, currentRuleConfig);
+        checkInUsed(schemaName, keyGeneratorNames, currentRuleConfig);
     }
-
+    
     private void checkDuplicate(final String schemaName, final 
Collection<String> keyGeneratorNames) throws DistSQLException {
         Collection<String> duplicateNames = 
keyGeneratorNames.stream().collect(Collectors.groupingBy(each -> each, 
Collectors.counting())).entrySet().stream()
                 .filter(each -> each.getValue() > 
1).map(Map.Entry::getKey).collect(Collectors.toSet());
@@ -52,9 +60,15 @@ public final class DropShardingKeyGeneratorStatementUpdater 
implements RuleDefin
     
     private void checkExist(final String schemaName, final Collection<String> 
keyGeneratorNames, final ShardingRuleConfiguration currentRuleConfig) throws 
DistSQLException {
         Collection<String> notExistKeyGenerators = 
keyGeneratorNames.stream().filter(each -> 
!currentRuleConfig.getKeyGenerators().containsKey(each)).collect(Collectors.toCollection(LinkedList::new));
-        DistSQLException.predictionThrow(notExistKeyGenerators.isEmpty(), new 
RequiredKeyGeneratorMissedException("sharding", schemaName, 
notExistKeyGenerators));
+        DistSQLException.predictionThrow(notExistKeyGenerators.isEmpty(), new 
RequiredKeyGeneratorMissedException("Sharding", schemaName, 
notExistKeyGenerators));
     }
-
+    
+    private void checkInUsed(final String schemaName, final Collection<String> 
keyGeneratorNames, final ShardingRuleConfiguration currentRuleConfig) throws 
DistSQLException {
+        Collection<String> usedKeyGenerators = 
getUsedKeyGenerators(currentRuleConfig);
+        Collection<String> inUsedNames = 
keyGeneratorNames.stream().filter(each -> 
usedKeyGenerators.contains(each)).collect(Collectors.toCollection(LinkedList::new));
+        DistSQLException.predictionThrow(inUsedNames.isEmpty(), new 
KeyGeneratorInUsedException("Sharding", schemaName, inUsedNames));
+    }
+    
     @Override
     public boolean updateCurrentRuleConfiguration(final 
DropShardingKeyGeneratorStatement sqlStatement, final ShardingRuleConfiguration 
currentRuleConfig) {
         
currentRuleConfig.getKeyGenerators().keySet().removeIf(sqlStatement.getKeyGeneratorNames()::contains);
@@ -68,6 +82,17 @@ public final class DropShardingKeyGeneratorStatementUpdater 
implements RuleDefin
     
     @Override
     public String getType() {
-        return DropShardingKeyGeneratorStatement.class.getCanonicalName();
+        return TYPE;
+    }
+    
+    private Collection<String> getUsedKeyGenerators(final 
ShardingRuleConfiguration shardingRuleConfig) {
+        Collection<String> result = new LinkedHashSet<>();
+        shardingRuleConfig.getTables().stream().filter(each -> 
Objects.nonNull(each.getKeyGenerateStrategy())).forEach(each -> 
result.add(each.getKeyGenerateStrategy().getKeyGeneratorName()));
+        shardingRuleConfig.getAutoTables().stream().filter(each -> 
Objects.nonNull(each.getKeyGenerateStrategy())).forEach(each -> 
result.add(each.getKeyGenerateStrategy().getKeyGeneratorName()));
+        KeyGenerateStrategyConfiguration keyGenerateStrategy = 
shardingRuleConfig.getDefaultKeyGenerateStrategy();
+        if (Objects.nonNull(keyGenerateStrategy) && 
!Strings.isNullOrEmpty(keyGenerateStrategy.getKeyGeneratorName())) {
+            result.add(keyGenerateStrategy.getKeyGeneratorName());
+        }
+        return result;
     }
 }
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingKeyGeneratorStatementUpdaterTest.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingKeyGeneratorStatementUpdaterTest.java
index 687df62..1404a42 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingKeyGeneratorStatementUpdaterTest.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/CreateShardingKeyGeneratorStatementUpdaterTest.java
@@ -90,8 +90,8 @@ public final class 
CreateShardingKeyGeneratorStatementUpdaterTest {
     }
     
     private Properties buildProps() {
-        Properties props = new Properties();
-        props.put("worker-id", "123");
-        return props;
+        Properties result = new Properties();
+        result.put("worker-id", "123");
+        return result;
     }
 }
diff --git 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorStatementUpdaterTest.java
 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorStatementUpdaterTest.java
index 9a30346..bc27439 100644
--- 
a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorStatementUpdaterTest.java
+++ 
b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-distsql/shardingsphere-sharding-distsql-handler/src/test/java/org/apache/shardingsphere/sharding/distsql/update/DropShardingKeyGeneratorStatementUpdaterTest.java
@@ -17,15 +17,16 @@
 
 package org.apache.shardingsphere.sharding.distsql.update;
 
-import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
 import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
 import 
org.apache.shardingsphere.infra.distsql.exception.rule.DuplicateKeyGeneratorException;
+import 
org.apache.shardingsphere.infra.distsql.exception.rule.KeyGeneratorInUsedException;
 import 
org.apache.shardingsphere.infra.distsql.exception.rule.RequiredKeyGeneratorMissedException;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import 
org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
+import 
org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
 import 
org.apache.shardingsphere.sharding.distsql.handler.update.DropShardingKeyGeneratorStatementUpdater;
-import 
org.apache.shardingsphere.sharding.distsql.parser.segment.ShardingKeyGeneratorSegment;
 import 
org.apache.shardingsphere.sharding.distsql.parser.statement.DropShardingKeyGeneratorStatement;
 import org.junit.Before;
 import org.junit.Test;
@@ -61,7 +62,7 @@ public final class 
DropShardingKeyGeneratorStatementUpdaterTest {
     public void assertExecuteWithNotExist() throws DistSQLException {
         updater.checkSQLStatement(shardingSphereMetaData, 
createSQLStatement("uuid_key_generator"), new ShardingRuleConfiguration());
     }
-
+    
     @Test
     public void assertDropSpecifiedKeyGenerator() {
         ShardingRuleConfiguration currentRuleConfig = new 
ShardingRuleConfiguration();
@@ -70,17 +71,27 @@ public final class 
DropShardingKeyGeneratorStatementUpdaterTest {
         assertTrue(currentRuleConfig.getKeyGenerators().isEmpty());
     }
     
+    @Test(expected = KeyGeneratorInUsedException.class)
+    public void assertExecuteWithUsed() throws DistSQLException {
+        ShardingRuleConfiguration currentRuleConfig = new 
ShardingRuleConfiguration();
+        currentRuleConfig.getKeyGenerators().put("uuid_key_generator", new 
ShardingSphereAlgorithmConfiguration("UUID", null));
+        
currentRuleConfig.getAutoTables().add(createShardingAutoTableRuleConfiguration());
+        updater.checkSQLStatement(shardingSphereMetaData, 
createSQLStatement("uuid_key_generator"), currentRuleConfig);
+    }
+    
+    private ShardingAutoTableRuleConfiguration 
createShardingAutoTableRuleConfiguration() {
+        ShardingAutoTableRuleConfiguration result = new 
ShardingAutoTableRuleConfiguration("auto_table");
+        result.setKeyGenerateStrategy(new 
KeyGenerateStrategyConfiguration("order_id", "uuid_key_generator"));
+        return result;
+    }
+    
     private DropShardingKeyGeneratorStatement createSQLStatement(final 
String... keyGeneratorNames) {
         return new 
DropShardingKeyGeneratorStatement(Arrays.asList(keyGeneratorNames));
     }
-
-    private ShardingKeyGeneratorSegment buildShardingKeyGeneratorSegment() {
-        return new ShardingKeyGeneratorSegment("uuid_key_generator", new 
AlgorithmSegment("uuid", buildProps()));
-    }
-
+    
     private Properties buildProps() {
-        Properties props = new Properties();
-        props.put("worker-id", "123");
-        return props;
+        Properties result = new Properties();
+        result.put("worker-id", "123");
+        return result;
     }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/rule/RequiredKeyGeneratorMissedException.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/rule/KeyGeneratorInUsedException.java
similarity index 65%
copy from 
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/rule/RequiredKeyGeneratorMissedException.java
copy to 
shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/rule/KeyGeneratorInUsedException.java
index 9cde8a3..57460b5 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/rule/RequiredKeyGeneratorMissedException.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/rule/KeyGeneratorInUsedException.java
@@ -19,12 +19,14 @@ package 
org.apache.shardingsphere.infra.distsql.exception.rule;
 
 import java.util.Collection;
 
-public class RequiredKeyGeneratorMissedException extends 
RuleDefinitionViolationException {
-
-    private static final long serialVersionUID = -2391552466149640249L;
-
-    public RequiredKeyGeneratorMissedException(final String type, final String 
schemaName, final Collection<String> keyGeneratorNames) {
-        super(1118, String.format("%s key generator `%s` do not exist in 
schema `%s`.", type, keyGeneratorNames, schemaName));
-    }
+/**
+ * Key generator in used exception.
+ */
+public final class KeyGeneratorInUsedException extends 
RuleDefinitionViolationException {
+    
+    private static final long serialVersionUID = 477511600241124319L;
     
+    public KeyGeneratorInUsedException(final String ruleType, final String 
schemaName, final Collection<String> keyGeneratorNames) {
+        super(1121, String.format("%s key generator `%s` in schema `%s` are 
still in used.", ruleType, keyGeneratorNames, schemaName));
+    }
 }
diff --git 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/rule/RequiredKeyGeneratorMissedException.java
 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/rule/RequiredKeyGeneratorMissedException.java
index 9cde8a3..03b16a1 100644
--- 
a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/rule/RequiredKeyGeneratorMissedException.java
+++ 
b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/distsql/exception/rule/RequiredKeyGeneratorMissedException.java
@@ -20,11 +20,10 @@ package 
org.apache.shardingsphere.infra.distsql.exception.rule;
 import java.util.Collection;
 
 public class RequiredKeyGeneratorMissedException extends 
RuleDefinitionViolationException {
-
+    
     private static final long serialVersionUID = -2391552466149640249L;
-
+    
     public RequiredKeyGeneratorMissedException(final String type, final String 
schemaName, final Collection<String> keyGeneratorNames) {
         super(1118, String.format("%s key generator `%s` do not exist in 
schema `%s`.", type, keyGeneratorNames, schemaName));
     }
-    
 }

Reply via email to