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

zhangliang 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 c557979  support add feature in "alter replica_query rule" (#8997)
c557979 is described below

commit c5579797b95a0f664987e44585ff01e6cd051edd
Author: JingShang Lu <[email protected]>
AuthorDate: Thu Jan 14 23:11:50 2021 +0800

    support add feature in "alter replica_query rule" (#8997)
    
    * fix issue #8979
---
 .../db/protocol/error/CommonErrorCode.java         |  6 ++
 .../src/main/antlr4/imports/Keyword.g4             |  4 +
 .../src/main/antlr4/imports/RDLStatement.g4        |  2 +-
 .../distsql/parser/core/DistSQLVisitor.java        | 19 +++--
 .../segment/rdl/ReplicaQueryRuleSegment.java       |  4 +-
 .../impl/AlterReplicaQueryRuleStatement.java       |  4 +-
 .../AlterReplicaQueryRuleStatementConverter.java   | 13 ++--
 .../CreateReplicaQueryRuleStatementConverter.java  |  4 +-
 ...plicaQueryRuleDataSourcesExistedException.java} | 10 +--
 ...caQueryRuleDataSourcesNotExistedException.java} | 10 +--
 .../ReplicaQueryRuleNotExistedException.java       |  4 -
 .../impl/AlterReplicaQueryRuleBackendHandler.java  | 89 +++++++++++++++++-----
 .../impl/DropReplicaQueryRuleBackendHandler.java   |  5 +-
 .../frontend/mysql/MySQLErrPacketFactory.java      | 16 +++-
 14 files changed, 136 insertions(+), 54 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 3cc460e..da0eff3 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
@@ -37,6 +37,12 @@ public enum CommonErrorCode implements SQLErrorCode {
     
     RESOURCE_NOT_EXIST(11004, "C11004", "Can not drop resources, resources %s 
do not exist."),
     
+    REPLICA_QUERY_RULE_NOT_EXIST(11005, "C11005", "Replica query rule does not 
exist."),
+    
+    REPLICA_QUERY_RULE_DATA_SOURCE_NOT_EXIST(11006, "C11006", "Data sources %s 
in replica query rule do not exist."),
+    
+    ADD_REPLICA_QUERY_RULE_DATA_SOURCE_EXIST(11007, "C11007", "Can not add 
replica query rule, data sources %s in replica query rule already exists."),
+    
     REPLICA_QUERY_RULE_EXIST(11008, "C11008", "Replica query rule already 
exists."),
     
     UNSUPPORTED_COMMAND(19998, "C19998", "Unsupported command: [%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 723560f..3d6b781 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
@@ -35,6 +35,10 @@ ALTER
     : A L T E R
     ;
 
+MODIFY
+    : M O D I F Y
+    ;
+
 DROP
     : D R O P
     ;
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 ad6b91c..2f512a6 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
@@ -80,7 +80,7 @@ alterReplicaQueryRule
     ;
 
 alterReplicaQueryRuleDefinition
-    : ruleName=IDENTIFIER LP PRIMARY EQ primary=schemaName COMMA REPLICA EQ 
schemaNames RP (loadBalancer=IDENTIFIER LP algorithmProperties RP)?
+    : (MODIFY | ADD) ruleName=IDENTIFIER LP PRIMARY EQ primary=schemaName 
COMMA REPLICA EQ schemaNames RP (loadBalancer=IDENTIFIER LP algorithmProperties 
RP)?
     ;
 
 shardingTableRuleDefinition
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 dfdb630..73fd205 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
@@ -130,8 +130,8 @@ public final class DistSQLVisitor extends 
DistSQLStatementBaseVisitor<ASTNode> {
             props.setProperty(each.key.getText(), each.value.getText());
         }
         result.setName(ctx.ruleName.getText());
-        result.setPrimaryDatasource(ctx.primary.getText());
-        result.setReplicaDatasources(replicaDatasources);
+        result.setPrimaryDataSource(ctx.primary.getText());
+        result.setReplicaDataSources(replicaDatasources);
         result.setLoadBalancer(ctx.loadBalancer.getText());
         result.setProps(props);
         return result;
@@ -139,11 +139,16 @@ public final class DistSQLVisitor extends 
DistSQLStatementBaseVisitor<ASTNode> {
 
     @Override
     public ASTNode visitAlterReplicaQueryRule(final 
AlterReplicaQueryRuleContext ctx) {
-        Collection<ReplicaQueryRuleSegment> replicaQueryRules = new 
LinkedList<>();
+        Collection<ReplicaQueryRuleSegment> modifyReplicaQueryRules = new 
LinkedList<>();
+        Collection<ReplicaQueryRuleSegment> addReplicaQueryRules = new 
LinkedList<>();
         for (AlterReplicaQueryRuleDefinitionContext each : 
ctx.alterReplicaQueryRuleDefinition()) {
-            replicaQueryRules.add((ReplicaQueryRuleSegment) visit(each));
+            if (null != each.MODIFY()) {
+                modifyReplicaQueryRules.add((ReplicaQueryRuleSegment) 
visit(each));
+            } else {
+                addReplicaQueryRules.add((ReplicaQueryRuleSegment) 
visit(each));
+            }
         }
-        return new AlterReplicaQueryRuleStatement(replicaQueryRules);
+        return new AlterReplicaQueryRuleStatement(modifyReplicaQueryRules, 
addReplicaQueryRules);
     }
 
     @Override
@@ -163,8 +168,8 @@ public final class DistSQLVisitor extends 
DistSQLStatementBaseVisitor<ASTNode> {
             replicaDatasources.add(each.getText());
         }
         result.setName(ctx.ruleName.getText());
-        result.setPrimaryDatasource(ctx.primary.getText());
-        result.setReplicaDatasources(replicaDatasources);
+        result.setPrimaryDataSource(ctx.primary.getText());
+        result.setReplicaDataSources(replicaDatasources);
         if (null != ctx.loadBalancer) {
             Properties props = new Properties();
             for (AlgorithmPropertyContext each : 
ctx.algorithmProperties().algorithmProperty()) {
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/ReplicaQueryRuleSegment.java
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/ReplicaQueryRuleSegment.java
index c32c460..4306946 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/ReplicaQueryRuleSegment.java
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/segment/rdl/ReplicaQueryRuleSegment.java
@@ -33,9 +33,9 @@ public final class ReplicaQueryRuleSegment implements ASTNode 
{
     
     private String name;
     
-    private String primaryDatasource;
+    private String primaryDataSource;
     
-    private Collection<String> replicaDatasources;
+    private Collection<String> replicaDataSources;
     
     private String loadBalancer;
     
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/impl/AlterReplicaQueryRuleStatement.java
 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/impl/AlterReplicaQueryRuleStatement.java
index 7eee152..e23594e 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/impl/AlterReplicaQueryRuleStatement.java
+++ 
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rdl/create/impl/AlterReplicaQueryRuleStatement.java
@@ -31,5 +31,7 @@ import java.util.Collection;
 @Getter
 public final class AlterReplicaQueryRuleStatement extends CreateRDLStatement {
     
-    private final Collection<ReplicaQueryRuleSegment> replicaQueryRules;
+    private final Collection<ReplicaQueryRuleSegment> modifyReplicaQueryRules;
+    
+    private final Collection<ReplicaQueryRuleSegment> addReplicaQueryRules;
 }
diff --git 
a/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/yaml/converter/AlterReplicaQueryRuleStatementConverter.java
 
b/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/yaml/converter/AlterReplicaQueryRuleStatementConverter.java
index 65e68fd..db9221a 100644
--- 
a/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/yaml/converter/AlterReplicaQueryRuleStatementConverter.java
+++ 
b/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/yaml/converter/AlterReplicaQueryRuleStatementConverter.java
@@ -20,11 +20,12 @@ package 
org.apache.shardingsphere.replicaquery.yaml.converter;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
 import 
org.apache.shardingsphere.distsql.parser.segment.rdl.ReplicaQueryRuleSegment;
-import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AlterReplicaQueryRuleStatement;
 import 
org.apache.shardingsphere.infra.yaml.config.algorithm.YamlShardingSphereAlgorithmConfiguration;
 import 
org.apache.shardingsphere.replicaquery.yaml.config.YamlReplicaQueryRuleConfiguration;
 import 
org.apache.shardingsphere.replicaquery.yaml.config.rule.YamlReplicaQueryDataSourceRuleConfiguration;
 
+import java.util.Collection;
+
 /**
  * Alter replica query rule statement converter.
  */
@@ -34,16 +35,16 @@ public final class AlterReplicaQueryRuleStatementConverter {
     /**
      * Convert alter replica query rule statement context to YAML replica 
query rule configuration.
      *
-     * @param sqlStatement alter replica query rule statement
+     * @param rules alter replica query rules
      * @return YAML replica query rule configuration
      */
-    public static YamlReplicaQueryRuleConfiguration convert(final 
AlterReplicaQueryRuleStatement sqlStatement) {
+    public static YamlReplicaQueryRuleConfiguration convert(final 
Collection<ReplicaQueryRuleSegment> rules) {
         YamlReplicaQueryRuleConfiguration result = new 
YamlReplicaQueryRuleConfiguration();
-        for (ReplicaQueryRuleSegment each : 
sqlStatement.getReplicaQueryRules()) {
+        for (ReplicaQueryRuleSegment each : rules) {
             YamlReplicaQueryDataSourceRuleConfiguration 
dataSourceRuleConfiguration = new YamlReplicaQueryDataSourceRuleConfiguration();
             dataSourceRuleConfiguration.setName(each.getName());
-            
dataSourceRuleConfiguration.setPrimaryDataSourceName(each.getPrimaryDatasource());
-            
dataSourceRuleConfiguration.getReplicaDataSourceNames().addAll(each.getReplicaDatasources());
+            
dataSourceRuleConfiguration.setPrimaryDataSourceName(each.getPrimaryDataSource());
+            
dataSourceRuleConfiguration.getReplicaDataSourceNames().addAll(each.getReplicaDataSources());
             
dataSourceRuleConfiguration.setLoadBalancerName(each.getLoadBalancer());
             dataSourceRuleConfiguration.setProps(each.getProps());
             result.getDataSources().put(each.getName(), 
dataSourceRuleConfiguration);
diff --git 
a/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/yaml/converter/CreateReplicaQueryRuleStatementConverter.java
 
b/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/yaml/converter/CreateReplicaQueryRuleStatementConverter.java
index e7669d9..2f7a611 100644
--- 
a/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/yaml/converter/CreateReplicaQueryRuleStatementConverter.java
+++ 
b/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/yaml/converter/CreateReplicaQueryRuleStatementConverter.java
@@ -42,8 +42,8 @@ public final class CreateReplicaQueryRuleStatementConverter {
         for (ReplicaQueryRuleSegment each : 
sqlStatement.getReplicaQueryRules()) {
             YamlReplicaQueryDataSourceRuleConfiguration 
dataSourceRuleConfiguration = new YamlReplicaQueryDataSourceRuleConfiguration();
             dataSourceRuleConfiguration.setName(each.getName());
-            
dataSourceRuleConfiguration.setPrimaryDataSourceName(each.getPrimaryDatasource());
-            
dataSourceRuleConfiguration.getReplicaDataSourceNames().addAll(each.getReplicaDatasources());
+            
dataSourceRuleConfiguration.setPrimaryDataSourceName(each.getPrimaryDataSource());
+            
dataSourceRuleConfiguration.getReplicaDataSourceNames().addAll(each.getReplicaDataSources());
             
dataSourceRuleConfiguration.setLoadBalancerName(each.getLoadBalancer());
             dataSourceRuleConfiguration.setProps(each.getProps());
             result.getDataSources().put(each.getName(), 
dataSourceRuleConfiguration);
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/AddReplicaQueryRuleDataSourcesExistedException.java
similarity index 81%
copy from 
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
copy to 
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/AddReplicaQueryRuleDataSourcesExistedException.java
index bcc955b..77fb0a1 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/AddReplicaQueryRuleDataSourcesExistedException.java
@@ -23,13 +23,13 @@ import lombok.RequiredArgsConstructor;
 import java.util.Collection;
 
 /**
- * Replica query rule not existed exception.
+ * Add replica query rule data source existed exception.
  */
 @RequiredArgsConstructor
 @Getter
-public final class ReplicaQueryRuleNotExistedException extends 
BackendException {
-
-    private static final long serialVersionUID = -5119217255419990719L;
-
+public final class AddReplicaQueryRuleDataSourcesExistedException extends 
BackendException {
+    
+    private static final long serialVersionUID = -4169758880611482863L;
+    
     private final Collection<String> ruleNames;
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleDataSourcesNotExistedException.java
similarity index 81%
copy from 
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
copy to 
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleDataSourcesNotExistedException.java
index bcc955b..7e56b80 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleDataSourcesNotExistedException.java
@@ -23,13 +23,13 @@ import lombok.RequiredArgsConstructor;
 import java.util.Collection;
 
 /**
- * Replica query rule not existed exception.
+ * Replica query rule data source not existed exception.
  */
 @RequiredArgsConstructor
 @Getter
-public final class ReplicaQueryRuleNotExistedException extends 
BackendException {
-
-    private static final long serialVersionUID = -5119217255419990719L;
-
+public final class ReplicaQueryRuleDataSourcesNotExistedException extends 
BackendException {
+    
+    private static final long serialVersionUID = -2151854349124198061L;
+    
     private final Collection<String> ruleNames;
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
index bcc955b..3ff4fa4 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/exception/ReplicaQueryRuleNotExistedException.java
@@ -20,8 +20,6 @@ package org.apache.shardingsphere.proxy.backend.exception;
 import lombok.Getter;
 import lombok.RequiredArgsConstructor;
 
-import java.util.Collection;
-
 /**
  * Replica query rule not existed exception.
  */
@@ -30,6 +28,4 @@ import java.util.Collection;
 public final class ReplicaQueryRuleNotExistedException extends 
BackendException {
 
     private static final long serialVersionUID = -5119217255419990719L;
-
-    private final Collection<String> ruleNames;
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterReplicaQueryRuleBackendHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterReplicaQueryRuleBackendHandler.java
index ad6fad1..97a3388 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterReplicaQueryRuleBackendHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/AlterReplicaQueryRuleBackendHandler.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.proxy.backend.text.distsql.rdl.impl;
 
+import 
org.apache.shardingsphere.distsql.parser.segment.rdl.ReplicaQueryRuleSegment;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.AlterReplicaQueryRuleStatement;
 import 
org.apache.shardingsphere.governance.core.event.model.rule.RuleConfigurationsPersistEvent;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
@@ -25,7 +26,10 @@ import 
org.apache.shardingsphere.infra.yaml.config.algorithm.YamlShardingSphereA
 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.AddReplicaQueryRuleDataSourcesExistedException;
+import 
org.apache.shardingsphere.proxy.backend.exception.ReplicaQueryRuleDataSourcesNotExistedException;
 import 
org.apache.shardingsphere.proxy.backend.exception.ReplicaQueryRuleNotExistedException;
+import 
org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
 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.SchemaRequiredBackendHandler;
@@ -35,8 +39,12 @@ import 
org.apache.shardingsphere.replicaquery.yaml.config.YamlReplicaQueryRuleCo
 import 
org.apache.shardingsphere.replicaquery.yaml.config.rule.YamlReplicaQueryDataSourceRuleConfiguration;
 import 
org.apache.shardingsphere.replicaquery.yaml.converter.AlterReplicaQueryRuleStatementConverter;
 
+import javax.sql.DataSource;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
 import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
@@ -49,34 +57,73 @@ public final class AlterReplicaQueryRuleBackendHandler 
extends SchemaRequiredBac
     public AlterReplicaQueryRuleBackendHandler(final 
AlterReplicaQueryRuleStatement sqlStatement, final BackendConnection 
backendConnection) {
         super(sqlStatement, backendConnection);
     }
-
+    
     @Override
     public ResponseHeader execute(final String schemaName, final 
AlterReplicaQueryRuleStatement sqlStatement) {
-        YamlReplicaQueryRuleConfiguration config = 
AlterReplicaQueryRuleStatementConverter.convert(sqlStatement);
-        check(config, schemaName);
-        YamlReplicaQueryRuleConfiguration alterConfig = alter(config, 
schemaName);
-        Collection<RuleConfiguration> rules = new 
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singleton(alterConfig));
+        check(sqlStatement, schemaName);
+        YamlReplicaQueryRuleConfiguration alterConfig = alter(sqlStatement, 
schemaName);
+        YamlReplicaQueryRuleConfiguration addConfig = add(alterConfig, 
sqlStatement, schemaName);
+        Collection<RuleConfiguration> rules = new 
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singleton(addConfig));
         post(schemaName, rules);
         return new UpdateResponseHeader(sqlStatement);
     }
-
-    private void check(final YamlReplicaQueryRuleConfiguration config, final 
String schemaName) {
-//        TODO Check whether the database exists
+    
+    private void check(final AlterReplicaQueryRuleStatement statement, final 
String schemaName) {
+        checkRuleExist(schemaName);
+        checkAddDataSourceExist(statement, schemaName);
+        checkModifyDataSourceNotExist(statement, schemaName);
+        checkResourceExist(statement, schemaName);
+    }
+    
+    private void checkRuleExist(final String schemaName) {
         Optional<ReplicaQueryRuleConfiguration> replicaQueryRuleConfig = 
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().stream()
                 .filter(each -> each instanceof 
ReplicaQueryRuleConfiguration).map(each -> (ReplicaQueryRuleConfiguration) 
each).findFirst();
         if (!replicaQueryRuleConfig.isPresent()) {
-            throw new 
ReplicaQueryRuleNotExistedException(config.getDataSources().keySet());
+            throw new ReplicaQueryRuleNotExistedException();
         }
-        Set<String> existReplicaQueryDatasourceNames = 
replicaQueryRuleConfig.get().getDataSources().stream().map(each -> 
each.getName()).collect(Collectors.toSet());
-        Collection<String> notExistReplicaQueryDatasourceNames = 
config.getDataSources().keySet().stream().filter(each -> 
!existReplicaQueryDatasourceNames.contains(each))
-                .collect(Collectors.toList());
-        if (!notExistReplicaQueryDatasourceNames.isEmpty()) {
-            throw new 
ReplicaQueryRuleNotExistedException(notExistReplicaQueryDatasourceNames);
+    }
+    
+    private void checkAddDataSourceExist(final AlterReplicaQueryRuleStatement 
statement, final String schemaName) {
+        Optional<ReplicaQueryRuleConfiguration> replicaQueryRuleConfig = 
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().stream()
+                .filter(each -> each instanceof 
ReplicaQueryRuleConfiguration).map(each -> (ReplicaQueryRuleConfiguration) 
each).findFirst();
+        Set<String> existReplicaQueryDataSourceNames = 
replicaQueryRuleConfig.get().getDataSources().stream().map(each -> 
each.getName()).collect(Collectors.toSet());
+        Collection<String> addExistReplicaQueryDataSourceNames = 
statement.getAddReplicaQueryRules().stream().map(each -> each.getName())
+                .filter(each -> 
existReplicaQueryDataSourceNames.contains(each)).collect(Collectors.toList());
+        if (!addExistReplicaQueryDataSourceNames.isEmpty()) {
+            throw new 
AddReplicaQueryRuleDataSourcesExistedException(addExistReplicaQueryDataSourceNames);
         }
     }
-
-    private YamlReplicaQueryRuleConfiguration alter(final 
YamlReplicaQueryRuleConfiguration config, final String schemaName) {
-        YamlReplicaQueryRuleConfiguration result = config;
+    
+    private void checkResourceExist(final AlterReplicaQueryRuleStatement 
statement, final String schemaName) {
+        List<String> resources = new LinkedList<>();
+        for (ReplicaQueryRuleSegment each: 
statement.getModifyReplicaQueryRules()) {
+            resources.add(each.getPrimaryDataSource());
+            resources.addAll(each.getReplicaDataSources());
+        }
+        for (ReplicaQueryRuleSegment each: 
statement.getAddReplicaQueryRules()) {
+            resources.add(each.getPrimaryDataSource());
+            resources.addAll(each.getReplicaDataSources());
+        }
+        Map<String, DataSource> dataSourceMap = 
ProxyContext.getInstance().getMetaData(schemaName).getResource().getDataSources();
+        Collection<String> notExistResource = resources.stream().filter(each 
-> !dataSourceMap.containsKey(each)).collect(Collectors.toList());
+        if (!notExistResource.isEmpty()) {
+            throw new ResourceNotExistedException(notExistResource);
+        }
+    }
+    
+    private void checkModifyDataSourceNotExist(final 
AlterReplicaQueryRuleStatement statement, final String schemaName) {
+        Optional<ReplicaQueryRuleConfiguration> replicaQueryRuleConfig = 
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().stream()
+                .filter(each -> each instanceof 
ReplicaQueryRuleConfiguration).map(each -> (ReplicaQueryRuleConfiguration) 
each).findFirst();
+        Set<String> existReplicaQueryDataSourceNames = 
replicaQueryRuleConfig.get().getDataSources().stream().map(each -> 
each.getName()).collect(Collectors.toSet());
+        Collection<String> addExistReplicaQueryDataSourceNames = 
statement.getModifyReplicaQueryRules().stream().map(each -> each.getName())
+                .filter(each -> 
!existReplicaQueryDataSourceNames.contains(each)).collect(Collectors.toList());
+        if (!addExistReplicaQueryDataSourceNames.isEmpty()) {
+            throw new 
ReplicaQueryRuleDataSourcesNotExistedException(addExistReplicaQueryDataSourceNames);
+        }
+    }
+    
+    private YamlReplicaQueryRuleConfiguration alter(final 
AlterReplicaQueryRuleStatement statement, final String schemaName) {
+        YamlReplicaQueryRuleConfiguration result = 
AlterReplicaQueryRuleStatementConverter.convert(statement.getModifyReplicaQueryRules());
         ReplicaQueryRuleConfiguration replicaQueryRuleConfig = 
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().stream()
                 .filter(each -> each instanceof 
ReplicaQueryRuleConfiguration).map(each -> (ReplicaQueryRuleConfiguration) 
each).findFirst().get();
         for (ReplicaQueryDataSourceRuleConfiguration each : 
replicaQueryRuleConfig.getDataSources()) {
@@ -104,6 +151,14 @@ public final class AlterReplicaQueryRuleBackendHandler 
extends SchemaRequiredBac
         }
         return result;
     }
+    
+    private YamlReplicaQueryRuleConfiguration add(final 
YamlReplicaQueryRuleConfiguration config, final AlterReplicaQueryRuleStatement 
statement, final String schemaName) {
+        YamlReplicaQueryRuleConfiguration result = config;
+        YamlReplicaQueryRuleConfiguration addConfig = 
AlterReplicaQueryRuleStatementConverter.convert(statement.getAddReplicaQueryRules());
+        result.getDataSources().putAll(addConfig.getDataSources());
+        result.getLoadBalancers().putAll(addConfig.getLoadBalancers());
+        return result;
+    }
 
     private void post(final String schemaName, final 
Collection<RuleConfiguration> rules) {
         ShardingSphereEventBus.getInstance().post(new 
RuleConfigurationsPersistEvent(schemaName, rules));
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropReplicaQueryRuleBackendHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropReplicaQueryRuleBackendHandler.java
index f92251f..a8dd9d6 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropReplicaQueryRuleBackendHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rdl/impl/DropReplicaQueryRuleBackendHandler.java
@@ -24,6 +24,7 @@ import 
org.apache.shardingsphere.infra.eventbus.ShardingSphereEventBus;
 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.ReplicaQueryRuleDataSourcesNotExistedException;
 import 
org.apache.shardingsphere.proxy.backend.exception.ReplicaQueryRuleNotExistedException;
 import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
 import 
org.apache.shardingsphere.proxy.backend.response.header.update.UpdateResponseHeader;
@@ -58,12 +59,12 @@ public final class DropReplicaQueryRuleBackendHandler 
extends SchemaRequiredBack
         Optional<ReplicaQueryRuleConfiguration> replicaQueryRuleConfig = 
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations().stream()
                 .filter(each -> each instanceof 
ReplicaQueryRuleConfiguration).map(each -> (ReplicaQueryRuleConfiguration) 
each).findFirst();
         if (!replicaQueryRuleConfig.isPresent()) {
-            throw new ReplicaQueryRuleNotExistedException(ruleNames);
+            throw new ReplicaQueryRuleNotExistedException();
         }
         Collection<String> replicaQueryNames = 
replicaQueryRuleConfig.get().getDataSources().stream().map(each -> 
each.getName()).collect(Collectors.toList());
         Collection<String> notExistedRuleNames = 
ruleNames.stream().filter(each -> 
!replicaQueryNames.contains(each)).collect(Collectors.toList());
         if (!notExistedRuleNames.isEmpty()) {
-            throw new ReplicaQueryRuleNotExistedException(notExistedRuleNames);
+            throw new 
ReplicaQueryRuleDataSourcesNotExistedException(notExistedRuleNames);
         }
     }
 
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 e668437..af3532b 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
@@ -23,11 +23,14 @@ import 
org.apache.shardingsphere.db.protocol.error.CommonErrorCode;
 import 
org.apache.shardingsphere.db.protocol.mysql.constant.MySQLServerErrorCode;
 import 
org.apache.shardingsphere.db.protocol.mysql.packet.generic.MySQLErrPacket;
 import 
org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException;
+import 
org.apache.shardingsphere.proxy.backend.exception.AddReplicaQueryRuleDataSourcesExistedException;
 import org.apache.shardingsphere.proxy.backend.exception.CircuitBreakException;
 import 
org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
 import org.apache.shardingsphere.proxy.backend.exception.DBDropExistsException;
 import 
org.apache.shardingsphere.proxy.backend.exception.LockWaitTimeoutException;
 import 
org.apache.shardingsphere.proxy.backend.exception.NoDatabaseSelectedException;
+import 
org.apache.shardingsphere.proxy.backend.exception.ReplicaQueryRuleDataSourcesNotExistedException;
+import 
org.apache.shardingsphere.proxy.backend.exception.ReplicaQueryRuleNotExistedException;
 import 
org.apache.shardingsphere.proxy.backend.exception.ReplicaQueryRuleCreateExistsException;
 import 
org.apache.shardingsphere.proxy.backend.exception.ResourceInUsedException;
 import 
org.apache.shardingsphere.proxy.backend.exception.ResourceNotExistedException;
@@ -114,10 +117,19 @@ public final class MySQLErrPacketFactory {
             return new MySQLErrPacket(1, 
MySQLServerErrorCode.ER_LOCKING_SERVICE_TIMEOUT, ((LockWaitTimeoutException) 
cause).getTimeoutMilliseconds());
         }
         if (cause instanceof ResourceNotExistedException) {
-            return new MySQLErrPacket(1, CommonErrorCode.RESOURCE_NOT_EXIST, 
cause.getMessage());
+            return new MySQLErrPacket(1, CommonErrorCode.RESOURCE_NOT_EXIST, 
((ResourceNotExistedException) cause).getResourceNames());
         }
         if (cause instanceof ResourceInUsedException) {
-            return new MySQLErrPacket(1, CommonErrorCode.RESOURCE_IN_USED, 
cause.getMessage());
+            return new MySQLErrPacket(1, CommonErrorCode.RESOURCE_IN_USED, 
((ResourceInUsedException) cause).getResourceNames());
+        }
+        if (cause instanceof ReplicaQueryRuleNotExistedException) {
+            return new MySQLErrPacket(1, 
CommonErrorCode.REPLICA_QUERY_RULE_NOT_EXIST);
+        }
+        if (cause instanceof ReplicaQueryRuleDataSourcesNotExistedException) {
+            return new MySQLErrPacket(1, 
CommonErrorCode.REPLICA_QUERY_RULE_DATA_SOURCE_NOT_EXIST, 
((ReplicaQueryRuleDataSourcesNotExistedException) cause).getRuleNames());
+        }
+        if (cause instanceof AddReplicaQueryRuleDataSourcesExistedException) {
+            return new MySQLErrPacket(1, 
CommonErrorCode.ADD_REPLICA_QUERY_RULE_DATA_SOURCE_EXIST, 
((AddReplicaQueryRuleDataSourcesExistedException) cause).getRuleNames());
         }
         if (cause instanceof ReplicaQueryRuleCreateExistsException) {
             return new MySQLErrPacket(1, 
CommonErrorCode.REPLICA_QUERY_RULE_EXIST);

Reply via email to