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

menghaoran 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 11c24ed  Support drop sharding rule RDL (#8637)
11c24ed is described below

commit 11c24ed0b46b49ca9744609016bcaee63316cd91
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Dec 16 07:31:56 2020 +0800

    Support drop sharding rule RDL (#8637)
    
    * Add dropShardingRule G4
    
    * Parse drop rule statement
    
    * Impl drop sharding rule
    
    * Check sharding tables not existed when drop rule
    
    * Fix test cases
    
    * Add case sensitive for rule type
---
 .../db/protocol/error/CommonErrorCode.java         |   6 +-
 .../db/protocol/error/CommonErrorCodeTest.java     |  15 ++-
 .../src/main/antlr4/imports/Keyword.g4             |  12 ++-
 .../src/main/antlr4/imports/RDLStatement.g4        |   4 +
 .../distsql/parser/autogen/DistSQLStatement.g4     |   1 +
 .../distsql/parser/core/DistSQLVisitor.java        |   7 ++
 .../statement/rdl/drop/DropRDLStatement.java}      |  16 ++-
 .../statement/rdl/drop/impl/DropRuleStatement.java |  20 ++--
 .../statement/rdl/DropRuleStatementContext.java    |  53 +++-------
 .../backend/exception/TablesInUsedException.java   |  20 ++--
 .../text/rdl/update/RDLUpdateBackendHandler.java   | 116 +++++++++++++++++++--
 .../frontend/mysql/MySQLErrPacketFactory.java      |   4 +
 .../frontend/mysql/MySQLErrPacketFactoryTest.java  |  67 +++++++-----
 .../postgresql/PostgreSQLErrPacketFactory.java     |   1 +
 14 files changed, 217 insertions(+), 125 deletions(-)

diff --git 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
 
b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
index 2b026fb..8b3c8d4 100644
--- 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
+++ 
b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
@@ -29,9 +29,11 @@ public enum CommonErrorCode implements SQLErrorCode {
     
     CIRCUIT_BREAK_MODE(10000, "C10000", "Circuit break mode is ON."),
     
-    UNSUPPORTED_COMMAND(10001, "C10001", "Unsupported command: [%s]"),
+    TABLES_IN_USED(11000, "C11000", "Can not drop rule, tables %s in the rule 
are still in used."),
     
-    UNKNOWN_EXCEPTION(10002, "C10002", "Unknown exception: [%s]");
+    UNSUPPORTED_COMMAND(19998, "C19998", "Unsupported command: [%s]"),
+    
+    UNKNOWN_EXCEPTION(19999, "C19999", "Unknown exception: [%s]");
     
     private final int errorCode;
     
diff --git 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/test/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCodeTest.java
 
b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/test/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCodeTest.java
index 908c5a3..c19b4c3 100644
--- 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/test/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCodeTest.java
+++ 
b/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/test/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCodeTest.java
@@ -32,16 +32,23 @@ public final class CommonErrorCodeTest {
     }
     
     @Test
+    public void assertTableInUsed() {
+        assertThat(CommonErrorCode.TABLES_IN_USED.getErrorCode(), is(11000));
+        assertThat(CommonErrorCode.TABLES_IN_USED.getSqlState(), is("C11000"));
+        assertThat(CommonErrorCode.TABLES_IN_USED.getErrorMessage(), is("Can 
not drop rule, tables %s in the rule are still in used."));
+    }
+    
+    @Test
     public void assertUnsupportedCommand() {
-        assertThat(CommonErrorCode.UNSUPPORTED_COMMAND.getErrorCode(), 
is(10001));
-        assertThat(CommonErrorCode.UNSUPPORTED_COMMAND.getSqlState(), 
is("C10001"));
+        assertThat(CommonErrorCode.UNSUPPORTED_COMMAND.getErrorCode(), 
is(19998));
+        assertThat(CommonErrorCode.UNSUPPORTED_COMMAND.getSqlState(), 
is("C19998"));
         assertThat(CommonErrorCode.UNSUPPORTED_COMMAND.getErrorMessage(), 
is("Unsupported command: [%s]"));
     }
     
     @Test
     public void assertUnknownException() {
-        assertThat(CommonErrorCode.UNKNOWN_EXCEPTION.getErrorCode(), 
is(10002));
-        assertThat(CommonErrorCode.UNKNOWN_EXCEPTION.getSqlState(), 
is("C10002"));
+        assertThat(CommonErrorCode.UNKNOWN_EXCEPTION.getErrorCode(), 
is(19999));
+        assertThat(CommonErrorCode.UNKNOWN_EXCEPTION.getSqlState(), 
is("C19999"));
         assertThat(CommonErrorCode.UNKNOWN_EXCEPTION.getErrorMessage(), 
is("Unknown exception: [%s]"));
     }
 }
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/Keyword.g4
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/Keyword.g4
index 61b2432..02a27e6 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/Keyword.g4
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/Keyword.g4
@@ -27,6 +27,14 @@ CREATE
     : C R E A T E
     ;
 
+DROP
+    : D R O P
+    ;
+
+SHOW
+    : S H O W
+    ;
+
 DATASOURCES
     : D A T A S O U R C E S
     ;
@@ -35,10 +43,6 @@ RULE
     :  R U L E
     ;
 
-SHOW
-    : S H O W
-    ;
-
 FROM
     : F R O M
     ;
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
index cf6b43a..94c2851 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RDLStatement.g4
@@ -99,6 +99,10 @@ columName
     : IDENTIFIER
     ;
 
+dropRule
+    : DROP ruleType RULE (FROM schemaName)?
+    ;
+
 showDataSources
     : SHOW DATASOURCES (FROM schemaName)?
     ;
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
index da27099..1fe5ca3 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
@@ -22,6 +22,7 @@ import Symbol, RDLStatement;
 execute
     : (createDataSources
     | createShardingRule
+    | dropRule
     | showDataSources
     | showRule
     ) SEMI?
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
index c5bfa15..2bebd6c 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/java/org/apache/shardingsphere/distsql/parser/core/DistSQLVisitor.java
@@ -22,6 +22,7 @@ import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.C
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.CreateShardingRuleContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DataSourceContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DataSourceDefinitionContext;
+import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.DropRuleContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.SchemaNameContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShardingAlgorithmPropertiesContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShardingAlgorithmPropertyContext;
@@ -32,6 +33,7 @@ import 
org.apache.shardingsphere.distsql.parser.segment.rdl.DataSourceConnection
 import org.apache.shardingsphere.distsql.parser.segment.rdl.TableRuleSegment;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateDataSourcesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingRuleStatement;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropRuleStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.show.impl.ShowDataSourcesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.show.impl.ShowRuleStatement;
 import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
@@ -106,6 +108,11 @@ public final class DistSQLVisitor extends 
DistSQLStatementBaseVisitor<ASTNode> {
     }
     
     @Override
+    public ASTNode visitDropRule(final DropRuleContext ctx) {
+        return new DropRuleStatement(ctx.ruleType().getText(), null == 
ctx.schemaName() ? null : (SchemaSegment) visit(ctx.schemaName()));
+    }
+    
+    @Override
     public ASTNode visitShowDataSources(final ShowDataSourcesContext ctx) {
         return new ShowDataSourcesStatement(null == ctx.schemaName() ? null : 
(SchemaSegment) visit(ctx.schemaName()));
     }
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/DropRDLStatement.java
similarity index 77%
copy from 
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
copy to 
shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/DropRDLStatement.java
index da27099..d6639f8 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/DropRDLStatement.java
@@ -15,14 +15,12 @@
  * limitations under the License.
  */
 
-grammar DistSQLStatement;
+package org.apache.shardingsphere.distsql.parser.statement.rdl.drop;
 
-import Symbol, RDLStatement;
+import org.apache.shardingsphere.distsql.parser.statement.rdl.RDLStatement;
 
-execute
-    : (createDataSources
-    | createShardingRule
-    | showDataSources
-    | showRule
-    ) SEMI?
-    ;
+/**
+ * Drop RDL statement.
+ */
+public abstract class DropRDLStatement extends RDLStatement {
+}
diff --git 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/impl/DropRuleStatement.java
similarity index 66%
copy from 
shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
copy to 
shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/impl/DropRuleStatement.java
index 2b026fb..8b80aa2 100644
--- 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/drop/impl/DropRuleStatement.java
@@ -15,27 +15,21 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.db.protocol.error;
+package org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl;
 
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.DropRDLStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
 
 /**
- * Common error code.
+ * Drop rule statement.
  */
 @RequiredArgsConstructor
 @Getter
-public enum CommonErrorCode implements SQLErrorCode {
+public final class DropRuleStatement extends DropRDLStatement {
     
-    CIRCUIT_BREAK_MODE(10000, "C10000", "Circuit break mode is ON."),
+    private final String ruleType;
     
-    UNSUPPORTED_COMMAND(10001, "C10001", "Unsupported command: [%s]"),
-    
-    UNKNOWN_EXCEPTION(10002, "C10002", "Unknown exception: [%s]");
-    
-    private final int errorCode;
-    
-    private final String sqlState;
-    
-    private final String errorMessage;
+    private final SchemaSegment schemaName;
 }
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/Keyword.g4
 
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/DropRuleStatementContext.java
similarity index 62%
copy from 
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/Keyword.g4
copy to 
shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/DropRuleStatementContext.java
index 61b2432..d5cddfc 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/Keyword.g4
+++ 
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/DropRuleStatementContext.java
@@ -15,46 +15,17 @@
  * limitations under the License.
  */
 
-lexer grammar Keyword;
+package org.apache.shardingsphere.infra.binder.statement.rdl;
 
-import Alphabet;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropRuleStatement;
+import 
org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
 
-WS
-    : [ \t\r\n] + ->skip
-    ;
-
-CREATE
-    : C R E A T E
-    ;
-
-DATASOURCES
-    : D A T A S O U R C E S
-    ;
-
-RULE
-    :  R U L E
-    ;
-
-SHOW
-    : S H O W
-    ;
-
-FROM
-    : F R O M
-    ;
-
-SHARDING
-    : S H A R D I N G
-    ;
-   
-REPLICA_QUERY
-    : R E P L I C A UL_ Q U E R Y
-    ;
-
-ENCRYPT
-    : E N C R Y P T
-    ;
-
-SHADOW
-    : S H A D O W
-    ;
+/**
+ * Drop rule statement context.
+ */
+public final class DropRuleStatementContext extends 
CommonSQLStatementContext<DropRuleStatement> {
+    
+    public DropRuleStatementContext(final DropRuleStatement sqlStatement) {
+        super(sqlStatement);
+    }
+}
diff --git 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/TablesInUsedException.java
similarity index 66%
copy from 
shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
copy to 
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/TablesInUsedException.java
index 2b026fb..c572181 100644
--- 
a/shardingsphere-db-protocol/shardingsphere-db-protocol-core/src/main/java/org/apache/shardingsphere/db/protocol/error/CommonErrorCode.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/TablesInUsedException.java
@@ -15,27 +15,21 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.db.protocol.error;
+package org.apache.shardingsphere.proxy.backend.exception;
 
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 
+import java.util.Collection;
+
 /**
- * Common error code.
+ * Tables in used exception.
  */
 @RequiredArgsConstructor
 @Getter
-public enum CommonErrorCode implements SQLErrorCode {
-    
-    CIRCUIT_BREAK_MODE(10000, "C10000", "Circuit break mode is ON."),
-    
-    UNSUPPORTED_COMMAND(10001, "C10001", "Unsupported command: [%s]"),
-    
-    UNKNOWN_EXCEPTION(10002, "C10002", "Unknown exception: [%s]");
-    
-    private final int errorCode;
+public final class TablesInUsedException extends BackendException {
     
-    private final String sqlState;
+    private static final long serialVersionUID = -6958306664517015097L;
     
-    private final String errorMessage;
+    private final Collection<String> tableNames;
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/rdl/update/RDLUpdateBackendHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/rdl/update/RDLUpdateBackendHandler.java
index f870b71..3421747 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/rdl/update/RDLUpdateBackendHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/rdl/update/RDLUpdateBackendHandler.java
@@ -20,7 +20,10 @@ package 
org.apache.shardingsphere.proxy.backend.text.rdl.update;
 import lombok.RequiredArgsConstructor;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateDataSourcesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateShardingRuleStatement;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.drop.impl.DropRuleStatement;
+import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
 import 
org.apache.shardingsphere.governance.core.event.model.datasource.DataSourcePersistEvent;
+import 
org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsChangedEvent;
 import 
org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsPersistEvent;
 import 
org.apache.shardingsphere.governance.core.event.model.schema.SchemaNamePersistEvent;
 import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
@@ -28,22 +31,31 @@ import 
org.apache.shardingsphere.infra.binder.statement.ddl.CreateDatabaseStatem
 import 
org.apache.shardingsphere.infra.binder.statement.ddl.DropDatabaseStatementContext;
 import 
org.apache.shardingsphere.infra.binder.statement.rdl.CreateDataSourcesStatementContext;
 import 
org.apache.shardingsphere.infra.binder.statement.rdl.CreateShardingRuleStatementContext;
+import 
org.apache.shardingsphere.infra.binder.statement.rdl.DropRuleStatementContext;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import 
org.apache.shardingsphere.infra.config.datasource.DataSourceConfiguration;
 import 
org.apache.shardingsphere.infra.context.metadata.impl.StandardMetaDataContexts;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
 import 
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
 import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
 import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import 
org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
+import 
org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
+import org.apache.shardingsphere.proxy.backend.exception.TablesInUsedException;
+import 
org.apache.shardingsphere.proxy.backend.exception.UnknownDatabaseException;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
 import 
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
 import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
 import 
org.apache.shardingsphere.proxy.config.util.DataSourceParameterConverter;
 import org.apache.shardingsphere.proxy.config.yaml.YamlDataSourceParameter;
 import 
org.apache.shardingsphere.proxy.converter.CreateDataSourcesStatementContextConverter;
+import 
org.apache.shardingsphere.replicaquery.api.config.ReplicaQueryRuleConfiguration;
+import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
 import 
org.apache.shardingsphere.sharding.converter.CreateShardingRuleStatementContextConverter;
+import org.apache.shardingsphere.sharding.rule.ShardingRule;
 import 
org.apache.shardingsphere.sharding.yaml.config.YamlShardingRuleConfiguration;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
@@ -52,7 +64,10 @@ import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropDatabas
 import java.sql.SQLException;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.LinkedList;
 import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
 
 /**
  * Backend handler for RDL update.
@@ -81,15 +96,6 @@ public final class RDLUpdateBackendHandler implements 
TextProtocolBackendHandler
         return new UpdateResponseHeader(context.getSqlStatement());
     }
     
-    private ResponseHeader execute(final DropDatabaseStatementContext context) 
{
-        if 
(!ProxyContext.getInstance().getAllSchemaNames().contains(context.getSqlStatement().getDatabaseName()))
 {
-            throw new 
DBCreateExistsException(context.getSqlStatement().getDatabaseName());
-        }
-        // TODO Need to get the executed feedback from registry center for 
returning.
-        ShardingSphereEventBus.getInstance().post(new 
SchemaNamePersistEvent(context.getSqlStatement().getDatabaseName(), true));
-        return new UpdateResponseHeader(context.getSqlStatement());
-    }
-    
     private ResponseHeader execute(final CreateDataSourcesStatementContext 
context) {
         Map<String, YamlDataSourceParameter> parameters = 
CreateDataSourcesStatementContextConverter.convert(context);
         Map<String, DataSourceConfiguration> dataSources = 
DataSourceParameterConverter.getDataSourceConfigurationMap(
@@ -107,6 +113,88 @@ public final class RDLUpdateBackendHandler implements 
TextProtocolBackendHandler
         return new UpdateResponseHeader(context.getSqlStatement());
     }
     
+    private ResponseHeader execute(final DropDatabaseStatementContext context) 
{
+        if 
(!ProxyContext.getInstance().getAllSchemaNames().contains(context.getSqlStatement().getDatabaseName()))
 {
+            throw new 
DBCreateExistsException(context.getSqlStatement().getDatabaseName());
+        }
+        // TODO Need to get the executed feedback from registry center for 
returning.
+        ShardingSphereEventBus.getInstance().post(new 
SchemaNamePersistEvent(context.getSqlStatement().getDatabaseName(), true));
+        return new UpdateResponseHeader(context.getSqlStatement());
+    }
+    
+    private ResponseHeader execute(final DropRuleStatementContext context) {
+        String schemaName = getSchemaName(context);
+        String ruleType = context.getSqlStatement().getRuleType();
+        Class<? extends RuleConfiguration> ruleConfigurationClass = 
getRuleConfigurationClass(ruleType);
+        switch (ruleType.toUpperCase()) {
+            case "SHARDING":
+                checkShardingTables(schemaName);
+                break;
+            case "REPLICA_QUERY":
+                // TODO
+            case "ENCRYPT":
+                // TODO
+                break;
+            case "SHADOW":
+                // TODO
+                break;
+            default:
+                throw new UnsupportedOperationException(ruleType);
+        }
+        Collection<RuleConfiguration> configs = 
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations();
+        ShardingSphereEventBus.getInstance().post(new 
RuleConfigurationsChangedEvent(schemaName, 
+                configs.stream().filter(each -> 
!ruleConfigurationClass.isAssignableFrom(each.getClass())).collect(Collectors.toList())));
+        // TODO Need to get the executed feedback from registry center for 
returning.
+        return new UpdateResponseHeader(context.getSqlStatement());
+    }
+    
+    private void checkShardingTables(final String schemaName) {
+        ShardingSphereMetaData metaData = 
ProxyContext.getInstance().getMetaData(schemaName);
+        Optional<ShardingRule> shardingRule = metaData.getRuleMetaData()
+                .getRules().stream().filter(each -> each instanceof 
ShardingRule).map(each -> (ShardingRule) each).findFirst();
+        if (!shardingRule.isPresent()) {
+            return;
+        }
+        Collection<String> inUsedTableNames = new LinkedList<>();
+        Collection<String> shardingTables = new 
LinkedList<>(shardingRule.get().getTables());
+        shardingTables.addAll(shardingRule.get().getBroadcastTables());
+        
shardingTables.addAll(shardingRule.get().getSingleTableRules().keySet());
+        for (String each : shardingTables) {
+            if (metaData.getSchema().containsTable(each)) {
+                inUsedTableNames.add(each);
+            }
+        }
+        if (!inUsedTableNames.isEmpty()) {
+            throw new TablesInUsedException(inUsedTableNames);
+        }
+    }
+    
+    private Class<? extends RuleConfiguration> getRuleConfigurationClass(final 
String ruleType) {
+        switch (ruleType.toUpperCase()) {
+            case "SHARDING":
+                return ShardingRuleConfiguration.class;
+            case "REPLICA_QUERY":
+                return ReplicaQueryRuleConfiguration.class;
+            case "ENCRYPT":
+                return EncryptRuleConfiguration.class;
+            case "SHADOW":
+                return ShadowRuleConfiguration.class;
+            default:
+                throw new UnsupportedOperationException(ruleType);
+        }
+    }
+    
+    private String getSchemaName(final DropRuleStatementContext context) {
+        String result = null == context.getSqlStatement().getSchemaName() ? 
backendConnection.getSchemaName() : 
context.getSqlStatement().getSchemaName().getIdentifier().getValue();
+        if (null == result) {
+            throw new NoDatabaseSelectedException();
+        }
+        if (!ProxyContext.getInstance().schemaExists(result)) {
+            throw new UnknownDatabaseException(result);
+        }
+        return result;
+    }
+    
     private boolean isRegistryCenterExisted() {
         return !(ProxyContext.getInstance().getMetaDataContexts() instanceof 
StandardMetaDataContexts);
     }
@@ -125,6 +213,9 @@ public final class RDLUpdateBackendHandler implements 
TextProtocolBackendHandler
         if (sqlStatement instanceof DropDatabaseStatement) {
             return new DropDatabaseStatementContext((DropDatabaseStatement) 
sqlStatement);
         }
+        if (sqlStatement instanceof DropRuleStatement) {
+            return new DropRuleStatementContext((DropRuleStatement) 
sqlStatement);
+        }
         throw new 
UnsupportedOperationException(sqlStatement.getClass().getName());
     }
     
@@ -135,11 +226,14 @@ public final class RDLUpdateBackendHandler implements 
TextProtocolBackendHandler
         if (context instanceof CreateDataSourcesStatementContext) {
             return execute((CreateDataSourcesStatementContext) context);
         }
+        if (context instanceof CreateShardingRuleStatementContext) {
+            return execute((CreateShardingRuleStatementContext) context);
+        }
         if (context instanceof DropDatabaseStatementContext) {
             return execute((DropDatabaseStatementContext) context);
         }
-        if (context instanceof CreateShardingRuleStatementContext) {
-            return execute((CreateShardingRuleStatementContext) context);
+        if (context instanceof DropRuleStatementContext) {
+            return execute((DropRuleStatementContext) context);
         }
         throw new UnsupportedOperationException(context.getClass().getName());
     }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java
index 4d23f85..95eb1c6 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/main/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactory.java
@@ -30,6 +30,7 @@ import 
org.apache.shardingsphere.proxy.backend.exception.LockWaitTimeoutExceptio
 import 
org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
 import 
org.apache.shardingsphere.proxy.backend.exception.RuleNotExistsException;
 import 
org.apache.shardingsphere.proxy.backend.exception.TableModifyInTransactionException;
+import org.apache.shardingsphere.proxy.backend.exception.TablesInUsedException;
 import 
org.apache.shardingsphere.proxy.backend.exception.UnknownDatabaseException;
 import org.apache.shardingsphere.proxy.backend.text.sctl.ShardingCTLErrorCode;
 import 
org.apache.shardingsphere.proxy.backend.text.sctl.exception.ShardingCTLException;
@@ -87,6 +88,9 @@ public final class MySQLErrPacketFactory {
         if (cause instanceof CircuitBreakException) {
             return new MySQLErrPacket(1, CommonErrorCode.CIRCUIT_BREAK_MODE);
         }
+        if (cause instanceof TablesInUsedException) {
+            return new MySQLErrPacket(1, CommonErrorCode.TABLES_IN_USED, 
((TablesInUsedException) cause).getTableNames());
+        }
         if (cause instanceof UnsupportedCommandException) {
             return new MySQLErrPacket(1, CommonErrorCode.UNSUPPORTED_COMMAND, 
((UnsupportedCommandException) cause).getCommandType());
         }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java
index 69e57b9..e190d02 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-mysql/src/test/java/org/apache/shardingsphere/proxy/frontend/mysql/MySQLErrPacketFactoryTest.java
@@ -25,6 +25,7 @@ import 
org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException
 import org.apache.shardingsphere.proxy.backend.exception.DBDropExistsException;
 import 
org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
 import 
org.apache.shardingsphere.proxy.backend.exception.TableModifyInTransactionException;
+import org.apache.shardingsphere.proxy.backend.exception.TablesInUsedException;
 import 
org.apache.shardingsphere.proxy.backend.exception.UnknownDatabaseException;
 import 
org.apache.shardingsphere.proxy.backend.text.sctl.exception.InvalidShardingCTLFormatException;
 import 
org.apache.shardingsphere.proxy.backend.text.sctl.exception.UnsupportedShardingCTLTypeException;
@@ -36,6 +37,7 @@ import 
org.apache.shardingsphere.sql.parser.exception.SQLParsingException;
 import org.junit.Test;
 
 import java.sql.SQLException;
+import java.util.Collections;
 
 import static org.hamcrest.CoreMatchers.endsWith;
 import static org.hamcrest.CoreMatchers.is;
@@ -120,15 +122,6 @@ public final class MySQLErrPacketFactoryTest {
     }
     
     @Test
-    public void assertNewInstanceWithOtherException() {
-        MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new 
RuntimeException("No reason"));
-        assertThat(actual.getSequenceId(), is(1));
-        assertThat(actual.getErrorCode(), is(10002));
-        assertThat(actual.getSqlState(), is("C10002"));
-        assertThat(actual.getErrorMessage(), is("Unknown exception: [No 
reason]"));
-    }
-    
-    @Test
     public void assertNewInstanceWithDBCreateExistsException() {
         MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new 
DBCreateExistsException("No reason"));
         assertThat(actual.getSequenceId(), is(1));
@@ -165,15 +158,6 @@ public final class MySQLErrPacketFactoryTest {
     }
     
     @Test
-    public void assertNewInstanceWithCircuitBreakException() {
-        MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new 
CircuitBreakException());
-        assertThat(actual.getSequenceId(), is(1));
-        assertThat(actual.getErrorCode(), is(10000));
-        assertThat(actual.getSqlState(), is("C10000"));
-        assertThat(actual.getErrorMessage(), is("Circuit break mode is ON."));
-    }
-    
-    @Test
     public void assertNewInstanceWithShardingSphereConfigurationException() {
         assertCommonException(MySQLErrPacketFactory.newInstance(new 
ShardingSphereConfigurationException("No reason")));
     }
@@ -183,13 +167,11 @@ public final class MySQLErrPacketFactoryTest {
         assertCommonException(MySQLErrPacketFactory.newInstance(new 
SQLParsingException("No reason")));
     }
     
-    @Test
-    public void assertNewInstanceWithUnsupportedCommandException() {
-        MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new 
UnsupportedCommandException("No reason"));
+    private void assertCommonException(final MySQLErrPacket actual) {
         assertThat(actual.getSequenceId(), is(1));
-        assertThat(actual.getErrorCode(), is(10001));
-        assertThat(actual.getSqlState(), is("C10001"));
-        assertThat(actual.getErrorMessage(), is("Unsupported command: [No 
reason]"));
+        assertThat(actual.getErrorCode(), is(1235));
+        assertThat(actual.getSqlState(), is("42000"));
+        assertThat(actual.getErrorMessage(), is("This version of ShardingProxy 
doesn't yet support this SQL. 'No reason'"));
     }
     
     @Test
@@ -201,10 +183,39 @@ public final class MySQLErrPacketFactoryTest {
         assertThat(actual.getErrorMessage(), is("This command is not supported 
in the prepared statement protocol yet"));
     }
     
-    private void assertCommonException(final MySQLErrPacket actual) {
+    @Test
+    public void assertNewInstanceWithCircuitBreakException() {
+        MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new 
CircuitBreakException());
         assertThat(actual.getSequenceId(), is(1));
-        assertThat(actual.getErrorCode(), is(1235));
-        assertThat(actual.getSqlState(), is("42000"));
-        assertThat(actual.getErrorMessage(), is("This version of ShardingProxy 
doesn't yet support this SQL. 'No reason'"));
+        assertThat(actual.getErrorCode(), is(10000));
+        assertThat(actual.getSqlState(), is("C10000"));
+        assertThat(actual.getErrorMessage(), is("Circuit break mode is ON."));
+    }
+    
+    @Test
+    public void assertNewInstanceWithTablesInUsedException() {
+        MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new 
TablesInUsedException(Collections.singleton("tbl")));
+        assertThat(actual.getSequenceId(), is(1));
+        assertThat(actual.getErrorCode(), is(11000));
+        assertThat(actual.getSqlState(), is("C11000"));
+        assertThat(actual.getErrorMessage(), is("Can not drop rule, tables 
[tbl] in the rule are still in used."));
+    }
+
+    @Test
+    public void assertNewInstanceWithUnsupportedCommandException() {
+        MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new 
UnsupportedCommandException("No reason"));
+        assertThat(actual.getSequenceId(), is(1));
+        assertThat(actual.getErrorCode(), is(19998));
+        assertThat(actual.getSqlState(), is("C19998"));
+        assertThat(actual.getErrorMessage(), is("Unsupported command: [No 
reason]"));
+    }
+    
+    @Test
+    public void assertNewInstanceWithOtherException() {
+        MySQLErrPacket actual = MySQLErrPacketFactory.newInstance(new 
RuntimeException("No reason"));
+        assertThat(actual.getSequenceId(), is(1));
+        assertThat(actual.getErrorCode(), is(19999));
+        assertThat(actual.getSqlState(), is("C19999"));
+        assertThat(actual.getErrorMessage(), is("Unknown exception: [No 
reason]"));
     }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLErrPacketFactory.java
 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLErrPacketFactory.java
index 283fe53..accf0f7 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLErrPacketFactory.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-frontend/shardingsphere-proxy-frontend-postgresql/src/main/java/org/apache/shardingsphere/proxy/frontend/postgresql/PostgreSQLErrPacketFactory.java
@@ -53,6 +53,7 @@ public final class PostgreSQLErrPacketFactory {
         }
         PostgreSQLErrorResponsePacket result = new 
PostgreSQLErrorResponsePacket();
         result.addField(PostgreSQLErrorResponsePacket.FIELD_TYPE_MESSAGE, 
cause.getMessage());
+        // TODO add common error code
         return result;
     }
 }

Reply via email to