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

lanchengxiang 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 06e7257  Add `IF EXISTS` keyword to `DROP DB_DISCOVERY RULE`. (#15684)
06e7257 is described below

commit 06e7257717c4b0c57c5b7bc12b52813cb245f7b9
Author: yx9o <[email protected]>
AuthorDate: Wed Mar 2 14:17:45 2022 +0800

    Add `IF EXISTS` keyword to `DROP DB_DISCOVERY RULE`. (#15684)
    
    * Add IF EXISTS keyword to DROP DB_DISCOVERY RULE.
    
    * Update.
    
    * Update.
---
 .../DropDatabaseDiscoveryRuleStatementUpdater.java | 27 ++++++++++++------
 ...pDatabaseDiscoveryRuleStatementUpdaterTest.java | 33 +++++++++++++++++-----
 .../main/antlr4/imports/db-discovery/Keyword.g4    |  8 ++++++
 .../antlr4/imports/db-discovery/RDLStatement.g4    |  6 +++-
 .../DatabaseDiscoveryDistSQLStatementVisitor.java  |  2 +-
 .../DropDatabaseDiscoveryRuleStatement.java        |  5 ++++
 ...DropDataBaseDiscoveryRuleStatementTestCase.java |  4 +++
 .../src/main/resources/case/rdl/drop.xml           |  5 ++++
 .../src/main/resources/sql/supported/rdl/drop.xml  |  1 +
 9 files changed, 74 insertions(+), 17 deletions(-)

diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDisco
 [...]
index 74ce19a..8d3f40c 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdater.java
@@ -17,7 +17,6 @@
 
 package org.apache.shardingsphere.dbdiscovery.distsql.handler.update;
 
-import com.google.common.base.Preconditions;
 import 
org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
 import 
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
 import 
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.DropDatabaseDiscoveryRuleStatement;
@@ -39,19 +38,23 @@ import java.util.stream.Collectors;
  */
 public final class DropDatabaseDiscoveryRuleStatementUpdater implements 
RuleDefinitionDropUpdater<DropDatabaseDiscoveryRuleStatement, 
DatabaseDiscoveryRuleConfiguration> {
     
-    private static final String RULE_TYPE = "database discovery";
+    private static final String RULE_TYPE = "Database discovery";
     
     @Override
     public void checkSQLStatement(final ShardingSphereMetaData 
shardingSphereMetaData, final DropDatabaseDiscoveryRuleStatement sqlStatement,
                                   final DatabaseDiscoveryRuleConfiguration 
currentRuleConfig) throws DistSQLException {
         String schemaName = shardingSphereMetaData.getName();
-        checkCurrentRuleConfiguration(schemaName, currentRuleConfig);
-        checkIsExist(schemaName, sqlStatement, currentRuleConfig);
+        checkCurrentRuleConfiguration(schemaName, sqlStatement, 
currentRuleConfig);
         checkIsInUse(schemaName, sqlStatement, shardingSphereMetaData);
     }
     
-    private void checkCurrentRuleConfiguration(final String schemaName, final 
DatabaseDiscoveryRuleConfiguration currentRuleConfig) throws DistSQLException {
-        DistSQLException.predictionThrow(null != currentRuleConfig, new 
RequiredRuleMissedException("Database discovery", schemaName));
+    private void checkCurrentRuleConfiguration(final String schemaName, final 
DropDatabaseDiscoveryRuleStatement sqlStatement,
+                                               final 
DatabaseDiscoveryRuleConfiguration currentRuleConfig) throws DistSQLException {
+        if (sqlStatement.isContainsExistClause()) {
+            return;
+        }
+        DistSQLException.predictionThrow(null != currentRuleConfig, new 
RequiredRuleMissedException(RULE_TYPE, schemaName));
+        checkIsExist(schemaName, sqlStatement, currentRuleConfig);
     }
     
     private void checkIsExist(final String schemaName, final 
DropDatabaseDiscoveryRuleStatement sqlStatement,
@@ -81,8 +84,16 @@ public final class DropDatabaseDiscoveryRuleStatementUpdater 
implements RuleDefi
     private void dropRule(final DatabaseDiscoveryRuleConfiguration 
currentRuleConfig, final String ruleName) {
         Optional<DatabaseDiscoveryDataSourceRuleConfiguration> 
dataSourceRuleConfig = 
currentRuleConfig.getDataSources().stream().filter(dataSource ->
                 dataSource.getGroupName().equals(ruleName)).findAny();
-        Preconditions.checkState(dataSourceRuleConfig.isPresent());
-        currentRuleConfig.getDataSources().remove(dataSourceRuleConfig.get());
+        dataSourceRuleConfig.ifPresent(op -> {
+            
currentRuleConfig.getDataSources().remove(dataSourceRuleConfig.get());
+        });
+    }
+    
+    @Override
+    public boolean hasAnyOneToBeDropped(final 
DropDatabaseDiscoveryRuleStatement sqlStatement, final 
DatabaseDiscoveryRuleConfiguration currentRuleConfig) {
+        return isExistRuleConfig(currentRuleConfig)
+                && 
!getIdenticalData(currentRuleConfig.getDataSources().stream().map(DatabaseDiscoveryDataSourceRuleConfiguration::getGroupName).collect(Collectors.toSet()),
+                sqlStatement.getRuleNames()).isEmpty();
     }
     
     @Override
diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseD
 [...]
index 9f9ce31..2ac57a3 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-handler/src/test/java/org/apache/shardingsphere/dbdiscovery/distsql/handler/update/DropDatabaseDiscoveryRuleStatementUpdaterTest.java
@@ -24,10 +24,9 @@ import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmC
 import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
 import 
org.apache.shardingsphere.infra.distsql.exception.rule.RequiredRuleMissedException;
 import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import 
org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
+import org.junit.Before;
 import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.Mock;
-import org.mockito.junit.MockitoJUnitRunner;
 
 import java.util.Arrays;
 import java.util.Collections;
@@ -40,15 +39,21 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertThat;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
-@RunWith(MockitoJUnitRunner.class)
 public final class DropDatabaseDiscoveryRuleStatementUpdaterTest {
     
-    @Mock
     private ShardingSphereMetaData shardingSphereMetaData;
     
     private final DropDatabaseDiscoveryRuleStatementUpdater updater = new 
DropDatabaseDiscoveryRuleStatementUpdater();
     
+    @Before
+    public void init() {
+        shardingSphereMetaData = mock(ShardingSphereMetaData.class);
+        when(shardingSphereMetaData.getRuleMetaData()).thenReturn(new 
ShardingSphereRuleMetaData(null, Collections.emptyList()));
+    }
+    
     @Test(expected = RequiredRuleMissedException.class)
     public void assertCheckSQLStatementWithoutCurrentRule() throws 
DistSQLException {
         updater.checkSQLStatement(shardingSphereMetaData, 
createSQLStatement(), null);
@@ -68,6 +73,16 @@ public final class 
DropDatabaseDiscoveryRuleStatementUpdaterTest {
     }
     
     @Test
+    public void assertUpdateCurrentRuleConfigurationWithIfExists() throws 
DistSQLException {
+        DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfiguration 
= createCurrentRuleConfiguration();
+        DropDatabaseDiscoveryRuleStatement dropDatabaseDiscoveryRuleStatement 
= createSQLStatementWithIfExists();
+        updater.checkSQLStatement(shardingSphereMetaData, 
dropDatabaseDiscoveryRuleStatement, databaseDiscoveryRuleConfiguration);
+        
assertFalse(updater.updateCurrentRuleConfiguration(dropDatabaseDiscoveryRuleStatement,
 databaseDiscoveryRuleConfiguration));
+        assertThat(databaseDiscoveryRuleConfiguration.getDataSources().size(), 
is(1));
+        
assertThat(databaseDiscoveryRuleConfiguration.getDiscoveryTypes().size(), 
is(1));
+    }
+    
+    @Test
     public void assertUpdateCurrentRuleConfigurationWithInUsedDiscoveryType() {
         DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfiguration 
= createMultipleCurrentRuleConfigurations();
         
assertFalse(updater.updateCurrentRuleConfiguration(createSQLStatement(), 
databaseDiscoveryRuleConfiguration));
@@ -75,7 +90,11 @@ public final class 
DropDatabaseDiscoveryRuleStatementUpdaterTest {
     }
     
     private DropDatabaseDiscoveryRuleStatement createSQLStatement() {
-        return new 
DropDatabaseDiscoveryRuleStatement(Collections.singleton("ha_group"));
+        return new 
DropDatabaseDiscoveryRuleStatement(Collections.singleton("ha_group"), false);
+    }
+    
+    private DropDatabaseDiscoveryRuleStatement 
createSQLStatementWithIfExists() {
+        return new 
DropDatabaseDiscoveryRuleStatement(Collections.singleton("ha_group_0"), true);
     }
     
     private DatabaseDiscoveryRuleConfiguration 
createCurrentRuleConfiguration() {
@@ -89,7 +108,7 @@ public final class 
DropDatabaseDiscoveryRuleStatementUpdaterTest {
         DatabaseDiscoveryDataSourceRuleConfiguration dataSourceRuleConfig = 
new DatabaseDiscoveryDataSourceRuleConfiguration("ha_group", 
Collections.emptyList(), "ha_heartbeat", "readwrite_ds_MGR");
         Map<String, ShardingSphereAlgorithmConfiguration> discoveryTypes = new 
HashMap<>(1, 1);
         discoveryTypes.put("readwrite_ds_MGR", new 
ShardingSphereAlgorithmConfiguration("readwrite_ds_MGR", new Properties()));
-        return new DatabaseDiscoveryRuleConfiguration(new 
LinkedList<>(Arrays.asList(dataSourceRuleConfig, 
+        return new DatabaseDiscoveryRuleConfiguration(new 
LinkedList<>(Arrays.asList(dataSourceRuleConfig,
                 new 
DatabaseDiscoveryDataSourceRuleConfiguration("ha_group_another", 
Collections.emptyList(), "ha_heartbeat", "readwrite_ds_MGR"))), 
Collections.emptyMap(), discoveryTypes);
     }
 }
diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/antlr4/imports/db-discovery/Keyword.g4
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/antlr4/imports/db-discovery/Keyword.g4
index 0038741..73dc480 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/antlr4/imports/db-discovery/Keyword.g4
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/antlr4/imports/db-discovery/Keyword.g4
@@ -82,3 +82,11 @@ DB_DISCOVERY
 HEARTBEAT
     : H E A R T B E A T
     ;
+
+IF  
+    : I F
+    ;
+
+EXISTS
+    : E X I S T S
+    ;
diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/antlr4/imports/db-discovery/RDLStatement.g4
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/antlr4/imports/db-discovery/RDLStatement.g4
index 480ab81..0c5f832 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/antlr4/imports/db-discovery/RDLStatement.g4
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/antlr4/imports/db-discovery/RDLStatement.g4
@@ -28,7 +28,7 @@ alterDatabaseDiscoveryRule
     ;
 
 dropDatabaseDiscoveryRule
-    : DROP DB_DISCOVERY RULE ruleName (COMMA ruleName)*
+    : DROP DB_DISCOVERY RULE existClause? ruleName (COMMA ruleName)*
     ;
 
 createDatabaseDiscoveryType
@@ -110,3 +110,7 @@ discoveryTypeName
 discoveryHeartbeatName
     : IDENTIFIER
     ;
+
+existClause
+    : IF EXISTS
+    ;
diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/core/DatabaseDiscoveryDistSQLStatementVisitor.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/core/DatabaseDiscoveryDistSQLSt
 [...]
index bf569c2..3b499de 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/core/DatabaseDiscoveryDistSQLStatementVisitor.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-parser/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/core/DatabaseDiscoveryDistSQLStatementVisitor.java
@@ -112,7 +112,7 @@ public final class DatabaseDiscoveryDistSQLStatementVisitor 
extends DatabaseDisc
     
     @Override
     public ASTNode visitDropDatabaseDiscoveryRule(final 
DropDatabaseDiscoveryRuleContext ctx) {
-        return new 
DropDatabaseDiscoveryRuleStatement(ctx.ruleName().stream().map(each -> 
getIdentifierValue(each)).collect(Collectors.toList()));
+        return new 
DropDatabaseDiscoveryRuleStatement(ctx.ruleName().stream().map(each -> 
getIdentifierValue(each)).collect(Collectors.toList()), null != 
ctx.existClause());
     }
     
     @Override
diff --git 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/statement/DropDatabaseDiscoveryRuleStatement.java
 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/statement/DropDatabaseDisc
 [...]
index aa7f477..8b80f5c 100644
--- 
a/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/statement/DropDatabaseDiscoveryRuleStatement.java
+++ 
b/shardingsphere-features/shardingsphere-db-discovery/shardingsphere-db-discovery-distsql/shardingsphere-db-discovery-distsql-statement/src/main/java/org/apache/shardingsphere/dbdiscovery/distsql/parser/statement/DropDatabaseDiscoveryRuleStatement.java
@@ -31,4 +31,9 @@ import java.util.Collection;
 public final class DropDatabaseDiscoveryRuleStatement extends 
DropRuleStatement {
     
     private final Collection<String> ruleNames;
+    
+    public DropDatabaseDiscoveryRuleStatement(final Collection<String> 
ruleNames, final boolean containsExistClause) {
+        setContainsExistClause(containsExistClause);
+        this.ruleNames = ruleNames;
+    }
 }
diff --git 
a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/distsql/rdl/drop/DropDataBaseDiscoveryRuleStatementTestCase.java
 
b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/distsql/rdl/drop/DropDataBaseDiscoveryRuleStatementTestCase.java
index 4a998af..4420e30 100644
--- 
a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/distsql/rdl/drop/DropDataBaseDiscoveryRuleStatementTestCase.java
+++ 
b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/distsql/rdl/drop/DropDataBaseDiscoveryRuleStatementTestCase.java
@@ -21,6 +21,7 @@ import lombok.Getter;
 import lombok.Setter;
 import 
org.apache.shardingsphere.test.sql.parser.parameterized.jaxb.cases.domain.statement.SQLParserTestCase;
 
+import javax.xml.bind.annotation.XmlAttribute;
 import javax.xml.bind.annotation.XmlElement;
 import java.util.LinkedList;
 import java.util.List;
@@ -34,4 +35,7 @@ public final class DropDataBaseDiscoveryRuleStatementTestCase 
extends SQLParserT
     
     @XmlElement(name = "rule")
     private final List<String> rules = new LinkedList<>();
+    
+    @XmlAttribute(name = "contains-exist-clause")
+    private boolean containsExistClause;
 }
diff --git 
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/rdl/drop.xml
 
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/rdl/drop.xml
index 05b3738..26d09c2 100644
--- 
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/rdl/drop.xml
+++ 
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/case/rdl/drop.xml
@@ -58,6 +58,11 @@
         <rule>ha_group_0</rule>
         <rule>ha_group_1</rule>
     </drop-database-discovery-rule>
+    
+    <drop-database-discovery-rule 
sql-case-id="drop-database-discovery-rule-if-exists" 
contains-exist-clause="true">
+        <rule>ha_group_0</rule>
+        <rule>ha_group_1</rule>
+    </drop-database-discovery-rule>
 
     <drop-database-discovery-type sql-case-id="drop-database-discovery-type">
         <type>type_name_0</type>
diff --git 
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/rdl/drop.xml
 
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/rdl/drop.xml
index 13557b7..480a7a1 100644
--- 
a/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/rdl/drop.xml
+++ 
b/shardingsphere-test/shardingsphere-parser-test/src/main/resources/sql/supported/rdl/drop.xml
@@ -27,6 +27,7 @@
     <distsql-case id="drop-sharding-broadcast-table-specified-table" 
value="DROP SHARDING BROADCAST TABLE RULES t_order" />
     <distsql-case id="drop-readwrite-splitting-rule" value="DROP 
READWRITE_SPLITTING RULE ms_group_0,ms_group_1" />
     <distsql-case id="drop-database-discovery-rule" value="DROP DB_DISCOVERY 
RULE ha_group_0,ha_group_1" />
+    <distsql-case id="drop-database-discovery-rule-if-exists" value="DROP 
DB_DISCOVERY RULE IF EXISTS ha_group_0,ha_group_1" />
     <distsql-case id="drop-database-discovery-type" value="DROP DB_DISCOVERY 
TYPE type_name_0,type_name_1" />
     <distsql-case id="drop-database-discovery-heartbeat" value="DROP 
DB_DISCOVERY HEARTBEAT heartbeat_name_0,heartbeat_name_1" />
     <distsql-case id="drop-encrypt-rule" value="DROP ENCRYPT RULE 
t_encrypt,t_encrypt_order" />

Reply via email to