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 a8b2d2e  Add `IF EXISTS` keyword to `DROP DB_DISCOVERY TYPE`. (#15802)
a8b2d2e is described below

commit a8b2d2ee6ac50f99f40c7e6667c42053d5dcf7e6
Author: yx9o <[email protected]>
AuthorDate: Tue Mar 8 09:56:48 2022 +0800

    Add `IF EXISTS` keyword to `DROP DB_DISCOVERY TYPE`. (#15802)
    
    * Add IF EXISTS keyword to DROP DB_DISCOVERY TYPE.
    
    * Update.
    
    * Update.
---
 .../DropDatabaseDiscoveryTypeStatementUpdater.java | 19 +++++++++++++----
 ...pDatabaseDiscoveryRuleStatementUpdaterTest.java |  2 +-
 ...pDatabaseDiscoveryTypeStatementUpdaterTest.java | 24 +++++++++++++++-------
 .../antlr4/imports/db-discovery/RDLStatement.g4    |  2 +-
 .../DatabaseDiscoveryDistSQLStatementVisitor.java  |  3 ++-
 .../DropDatabaseDiscoveryTypeStatement.java        |  5 +++++
 .../DropDatabaseDiscoveryTypeStatementAssert.java  |  1 +
 ...DropDataBaseDiscoveryTypeStatementTestCase.java |  4 ++++
 .../src/main/resources/case/rdl/drop.xml           |  5 +++++
 .../src/main/resources/sql/supported/rdl/drop.xml  |  1 +
 10 files changed, 52 insertions(+), 14 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/DropDatabaseDiscoveryTypeStatementUpdater.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 23b42c9..4235093 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/DropDatabaseDiscoveryTypeStatementUpdater.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/DropDatabaseDiscoveryTypeStatementUpdater.java
@@ -34,19 +34,23 @@ import java.util.stream.Collectors;
  */
 public final class DropDatabaseDiscoveryTypeStatementUpdater implements 
RuleDefinitionDropUpdater<DropDatabaseDiscoveryTypeStatement, 
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 DropDatabaseDiscoveryTypeStatement sqlStatement,
                                   final DatabaseDiscoveryRuleConfiguration 
currentRuleConfig) throws DistSQLException {
         String schemaName = shardingSphereMetaData.getName();
-        checkCurrentRuleConfiguration(schemaName, currentRuleConfig);
-        checkIsExist(schemaName, sqlStatement, currentRuleConfig);
+        checkCurrentRuleConfiguration(schemaName, sqlStatement, 
currentRuleConfig);
         checkIsInUse(schemaName, sqlStatement, currentRuleConfig);
     }
     
-    private void checkCurrentRuleConfiguration(final String schemaName, final 
DatabaseDiscoveryRuleConfiguration currentRuleConfig) throws DistSQLException {
+    private void checkCurrentRuleConfiguration(final String schemaName, final 
DropDatabaseDiscoveryTypeStatement 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 
DropDatabaseDiscoveryTypeStatement sqlStatement,
@@ -77,6 +81,13 @@ public final class DropDatabaseDiscoveryTypeStatementUpdater 
implements RuleDefi
         }
     }
     
+    @Override
+    public boolean hasAnyOneToBeDropped(final 
DropDatabaseDiscoveryTypeStatement sqlStatement, final 
DatabaseDiscoveryRuleConfiguration currentRuleConfig) {
+        return isExistRuleConfig(currentRuleConfig)
+                && 
!getIdenticalData(currentRuleConfig.getDataSources().stream().map(DatabaseDiscoveryDataSourceRuleConfiguration::getDiscoveryTypeName).collect(Collectors.toSet()),
+                sqlStatement.getTypes()).isEmpty();
+    }
+    
     private boolean isNotInUse(final DatabaseDiscoveryRuleConfiguration 
currentRuleConfig, final String toBeDroppedDiscoveryTypeName) {
         return currentRuleConfig.getDataSources().stream().noneMatch(each -> 
each.getDiscoveryTypeName().equals(toBeDroppedDiscoveryTypeName));
     }
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 2ac57a3..430cdc4 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
@@ -90,7 +90,7 @@ public final class 
DropDatabaseDiscoveryRuleStatementUpdaterTest {
     }
     
     private DropDatabaseDiscoveryRuleStatement createSQLStatement() {
-        return new 
DropDatabaseDiscoveryRuleStatement(Collections.singleton("ha_group"), false);
+        return new 
DropDatabaseDiscoveryRuleStatement(Collections.singleton("ha_group"));
     }
     
     private DropDatabaseDiscoveryRuleStatement 
createSQLStatementWithIfExists() {
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/DropDatabaseDiscoveryTypeStatementUpdaterTest.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 993c1ed..4746632 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/DropDatabaseDiscoveryTypeStatementUpdaterTest.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/DropDatabaseDiscoveryTypeStatementUpdaterTest.java
@@ -19,7 +19,6 @@ package 
org.apache.shardingsphere.dbdiscovery.distsql.handler.update;
 
 import 
org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
 import 
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
-import 
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryHeartBeatConfiguration;
 import 
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.DropDatabaseDiscoveryTypeStatement;
 import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 import org.apache.shardingsphere.infra.distsql.exception.DistSQLException;
@@ -36,8 +35,9 @@ import java.util.HashMap;
 import java.util.Map;
 import java.util.Properties;
 
+import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.assertThat;
 
 @RunWith(MockitoJUnitRunner.class)
 public final class DropDatabaseDiscoveryTypeStatementUpdaterTest {
@@ -69,19 +69,29 @@ public final class 
DropDatabaseDiscoveryTypeStatementUpdaterTest {
         DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfiguration 
= createCurrentRuleConfiguration();
         updater.updateCurrentRuleConfiguration(createSQLStatement(), 
databaseDiscoveryRuleConfiguration);
         
assertFalse(databaseDiscoveryRuleConfiguration.getDiscoveryTypes().containsKey("type_name"));
-        
assertTrue(databaseDiscoveryRuleConfiguration.getDiscoveryHeartbeats().containsKey("heartbeat_name"));
+    }
+    
+    @Test
+    public void assertUpdateCurrentRuleConfigurationWithIfExists() throws 
DistSQLException {
+        DatabaseDiscoveryRuleConfiguration databaseDiscoveryRuleConfiguration 
= createCurrentRuleConfiguration();
+        DropDatabaseDiscoveryTypeStatement dropDatabaseDiscoveryRuleStatement 
= createSQLStatementWithIfExists();
+        updater.checkSQLStatement(shardingSphereMetaData, 
dropDatabaseDiscoveryRuleStatement, databaseDiscoveryRuleConfiguration);
+        
assertFalse(updater.updateCurrentRuleConfiguration(dropDatabaseDiscoveryRuleStatement,
 databaseDiscoveryRuleConfiguration));
+        
assertThat(databaseDiscoveryRuleConfiguration.getDiscoveryTypes().size(), 
is(2));
     }
     
     private DropDatabaseDiscoveryTypeStatement createSQLStatement() {
         return new 
DropDatabaseDiscoveryTypeStatement(Collections.singleton("type_name"));
     }
     
+    private DropDatabaseDiscoveryTypeStatement 
createSQLStatementWithIfExists() {
+        return new 
DropDatabaseDiscoveryTypeStatement(Collections.singleton("type_name_0"), true);
+    }
+    
     private DatabaseDiscoveryRuleConfiguration 
createCurrentRuleConfiguration() {
-        Map<String, ShardingSphereAlgorithmConfiguration> discoveryTypes = new 
HashMap<>(1, 1);
+        Map<String, ShardingSphereAlgorithmConfiguration> discoveryTypes = new 
HashMap<>(2, 1);
         discoveryTypes.put("type_name", new 
ShardingSphereAlgorithmConfiguration("MGR", new Properties()));
         discoveryTypes.put("other", new 
ShardingSphereAlgorithmConfiguration("MGR", new Properties()));
-        Map<String, DatabaseDiscoveryHeartBeatConfiguration> 
discoveryHeartbeat = new HashMap<>(1, 1);
-        discoveryHeartbeat.put("heartbeat_name", new 
DatabaseDiscoveryHeartBeatConfiguration(new Properties()));
-        return new DatabaseDiscoveryRuleConfiguration(Collections.emptyList(), 
discoveryHeartbeat, discoveryTypes);
+        return new DatabaseDiscoveryRuleConfiguration(Collections.emptyList(), 
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/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 0c5f832..48e6682 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
@@ -44,7 +44,7 @@ alterDatabaseDiscoveryHeartbeat
     ;
 
 dropDatabaseDiscoveryType
-    : DROP DB_DISCOVERY TYPE discoveryTypeName (COMMA discoveryTypeName)*
+    : DROP DB_DISCOVERY TYPE existClause? discoveryTypeName (COMMA 
discoveryTypeName)*
     ;
 
 dropDatabaseDiscoveryHeartbeat
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 3b499de..19fc73f 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
@@ -182,7 +182,8 @@ public final class DatabaseDiscoveryDistSQLStatementVisitor 
extends DatabaseDisc
     
     @Override
     public ASTNode visitDropDatabaseDiscoveryType(final 
DropDatabaseDiscoveryTypeContext ctx) {
-        return new 
DropDatabaseDiscoveryTypeStatement(ctx.discoveryTypeName().stream().map(this::getIdentifierValue).collect(Collectors.toCollection(LinkedList::new)));
+        return new 
DropDatabaseDiscoveryTypeStatement(ctx.discoveryTypeName().stream().map(this::getIdentifierValue).collect(Collectors.toCollection(LinkedList::new)),
+                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/DropDatabaseDiscoveryTypeStatement.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 d6ea7a8..fa77d3c 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/DropDatabaseDiscoveryTypeStatement.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/DropDatabaseDiscoveryTypeStatement.java
@@ -31,4 +31,9 @@ import java.util.Collection;
 public final class DropDatabaseDiscoveryTypeStatement extends 
DropRuleStatement {
     
     private final Collection<String> types;
+    
+    public DropDatabaseDiscoveryTypeStatement(final Collection<String> types, 
final boolean containsExistClause) {
+        setContainsExistClause(containsExistClause);
+        this.types = types;
+    }
 }
diff --git 
a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/drop/impl/DropDatabaseDiscoveryTypeStatementAssert.java
 
b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/drop/impl/DropDatabaseDiscoveryTypeStatementAssert.java
index cbbebf7..695e923 100644
--- 
a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/drop/impl/DropDatabaseDiscoveryTypeStatementAssert.java
+++ 
b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/asserts/statement/distsql/rdl/drop/impl/DropDatabaseDiscoveryTypeStatementAssert.java
@@ -47,6 +47,7 @@ public final class DropDatabaseDiscoveryTypeStatementAssert {
         } else {
             assertNotNull(assertContext.getText("Actual statement should 
exist."), actual);
             assertThat(assertContext.getText("database discovery type 
assertion error: "), actual.getTypes(), is(expected.getTypes()));
+            assertThat(assertContext.getText("database discovery type 
assertion error: "), actual.isContainsExistClause(), 
is(expected.isContainsExistClause()));
         }
     }
 }
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/DropDataBaseDiscoveryTypeStatementTestCase.java
 
b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/distsql/rdl/drop/DropDataBaseDiscoveryTypeStatementTestCase.java
index 303609b..690243d 100644
--- 
a/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/distsql/rdl/drop/DropDataBaseDiscoveryTypeStatementTestCase.java
+++ 
b/shardingsphere-test/shardingsphere-parser-test/src/main/java/org/apache/shardingsphere/test/sql/parser/parameterized/jaxb/cases/domain/statement/distsql/rdl/drop/DropDataBaseDiscoveryTypeStatementTestCase.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 DropDataBaseDiscoveryTypeStatementTestCase 
extends SQLParserT
     
     @XmlElement(name = "type")
     private final List<String> types = 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 64adc6c..73548b5 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
@@ -68,6 +68,11 @@
         <type>type_name_0</type>
         <type>type_name_1</type>
     </drop-database-discovery-type>
+    
+    <drop-database-discovery-type 
sql-case-id="drop-database-discovery-type-if-exists" 
contains-exist-clause="true">
+        <type>type_name_0</type>
+        <type>type_name_1</type>
+    </drop-database-discovery-type>
 
     <drop-database-discovery-heartbeat 
sql-case-id="drop-database-discovery-heartbeat">
         <heartbeat>heartbeat_name_0</heartbeat>
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 e3f7cbf..7dc4a67 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
@@ -29,6 +29,7 @@
     <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-type-if-exists" value="DROP 
DB_DISCOVERY TYPE IF EXISTS 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" />
     <distsql-case id="drop-encrypt-rule-if-exists" value="DROP ENCRYPT RULE IF 
EXISTS t_encrypt,t_encrypt_order" />

Reply via email to