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

panjuan 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 2c874fe  Support SHOW DATASOURCES (#8610)
2c874fe is described below

commit 2c874feda48ec2c78616d26d6400e83c57fbfa62
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Dec 14 17:30:31 2020 +0800

    Support SHOW DATASOURCES (#8610)
    
    * Add show datasources G4
    
    * Rename RDLBackendHandlerFactory
    
    * Refactor RDLRuleQueryBackendHandler
    
    * Add RDLDataSourcesQueryBackendHandler
    
    * Refactor RDLDataSourcesQueryBackendHandler
    
    * Refactor RDLRuleQueryBackendHandler
    
    * Fix test case
---
 .../src/main/antlr4/imports/RDLStatement.g4        |  4 ++
 .../distsql/parser/autogen/DistSQLStatement.g4     |  1 +
 .../distsql/parser/core/DistSQLVisitor.java        |  9 ++-
 .../rdl/show/impl/ShowDataSourcesStatement.java}   | 22 +++---
 .../rdl/ShowDataSourcesStatementContext.java       | 20 +++---
 .../text/TextProtocolBackendHandlerFactory.java    | 15 ++--
 .../metadata/rdl/RDLBackendHandlerFactory.java     | 61 +++++++++++++++++
 .../query/RDLDataSourcesQueryBackendHandler.java   | 79 ++++++++++++++++++++++
 .../RDLRuleQueryBackendHandler.java}               | 37 +++-------
 .../rdl/{ => update}/RDLUpdateBackendHandler.java  | 19 +++---
 .../text/metadata/rdl/RDLBackendHandlerTest.java   | 21 +++---
 11 files changed, 213 insertions(+), 75 deletions(-)

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 453b308..4c352b6 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
@@ -95,6 +95,10 @@ columName
     : IDENTIFIER
     ;
 
+showDataSources
+    : SHOW DATASOURCES (FROM schemaName)?
+    ;
+
 showRule
     : SHOW ruleType RULE (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 7ac8e8e..da27099 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
+    | 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 f5770ee..261f8d1 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
@@ -23,6 +23,7 @@ import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.C
 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.SchemaNameContext;
+import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowDataSourcesContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowRuleContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.StrategyPropContext;
 import 
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.StrategyPropsContext;
@@ -31,6 +32,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.show.impl.ShowDataSourcesStatement;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.show.impl.ShowRuleStatement;
 import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
 import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
@@ -104,10 +106,15 @@ public final class DistSQLVisitor extends 
DistSQLStatementBaseVisitor<ASTNode> {
     }
     
     @Override
+    public ASTNode visitShowDataSources(final ShowDataSourcesContext ctx) {
+        return new ShowDataSourcesStatement(null == ctx.schemaName() ? null : 
(SchemaSegment) visit(ctx.schemaName()));
+    }
+    
+    @Override
     public ASTNode visitShowRule(final ShowRuleContext ctx) {
         return new ShowRuleStatement(ctx.ruleType().getText(), null == 
ctx.schemaName() ? null : (SchemaSegment) visit(ctx.schemaName()));
     }
-
+    
     @Override
     public ASTNode visitSchemaName(final SchemaNameContext ctx) {
         return new SchemaSegment(ctx.getStart().getStartIndex(), 
ctx.getStop().getStopIndex(), new IdentifierValue(ctx.getText()));
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/show/impl/ShowDataSourcesStatement.java
similarity index 61%
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/show/impl/ShowDataSourcesStatement.java
index 7ac8e8e..3793285 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/show/impl/ShowDataSourcesStatement.java
@@ -15,13 +15,19 @@
  * limitations under the License.
  */
 
-grammar DistSQLStatement;
+package org.apache.shardingsphere.distsql.parser.statement.rdl.show.impl;
 
-import Symbol, RDLStatement;
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.show.ShowRDLStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
 
-execute
-    : (createDataSources
-    | createShardingRule
-    | showRule
-    ) SEMI?
-    ;
+/**
+ * Show data sources statement.
+ */
+@RequiredArgsConstructor
+@Getter
+public final class ShowDataSourcesStatement extends ShowRDLStatement {
+    
+    private final SchemaSegment schemaName;
+}
diff --git 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
 
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/ShowDataSourcesStatementContext.java
similarity index 60%
copy from 
shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
copy to 
shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/ShowDataSourcesStatementContext.java
index 7ac8e8e..bf21219 100644
--- 
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/org/apache/shardingsphere/distsql/parser/autogen/DistSQLStatement.g4
+++ 
b/shardingsphere-infra/shardingsphere-infra-binder/src/main/java/org/apache/shardingsphere/infra/binder/statement/rdl/ShowDataSourcesStatementContext.java
@@ -15,13 +15,17 @@
  * limitations under the License.
  */
 
-grammar DistSQLStatement;
+package org.apache.shardingsphere.infra.binder.statement.rdl;
 
-import Symbol, RDLStatement;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.show.impl.ShowDataSourcesStatement;
+import 
org.apache.shardingsphere.infra.binder.statement.CommonSQLStatementContext;
 
-execute
-    : (createDataSources
-    | createShardingRule
-    | showRule
-    ) SEMI?
-    ;
+/**
+ * Show data sources statement context.
+ */
+public final class ShowDataSourcesStatementContext extends 
CommonSQLStatementContext<ShowDataSourcesStatement> {
+    
+    public ShowDataSourcesStatementContext(final ShowDataSourcesStatement 
sqlStatement) {
+        super(sqlStatement);
+    }
+}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
index 6e440a5..0c49627 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/TextProtocolBackendHandlerFactory.java
@@ -20,14 +20,11 @@ package org.apache.shardingsphere.proxy.backend.text;
 import com.google.common.base.Strings;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.distsql.parser.statement.rdl.RDLStatement;
-import 
org.apache.shardingsphere.distsql.parser.statement.rdl.show.ShowRDLStatement;
 import org.apache.shardingsphere.infra.database.type.DatabaseType;
 import org.apache.shardingsphere.infra.parser.ShardingSphereSQLParserEngine;
 import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
 import 
org.apache.shardingsphere.proxy.backend.text.data.DatabaseBackendHandlerFactory;
-import 
org.apache.shardingsphere.proxy.backend.text.metadata.rdl.RDLQueryBackendHandler;
-import 
org.apache.shardingsphere.proxy.backend.text.metadata.rdl.RDLUpdateBackendHandler;
+import 
org.apache.shardingsphere.proxy.backend.text.metadata.rdl.RDLBackendHandlerFactory;
 import 
org.apache.shardingsphere.proxy.backend.text.metadata.schema.SchemaBackendHandler;
 import 
org.apache.shardingsphere.proxy.backend.text.metadata.schema.SchemaBackendHandlerFactory;
 import 
org.apache.shardingsphere.proxy.backend.text.sctl.ShardingCTLBackendHandlerFactory;
@@ -35,8 +32,6 @@ import 
org.apache.shardingsphere.proxy.backend.text.sctl.utils.SCTLUtils;
 import org.apache.shardingsphere.proxy.backend.text.skip.SkipBackendHandler;
 import 
org.apache.shardingsphere.proxy.backend.text.transaction.TransactionBackendHandlerFactory;
 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
-import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropDatabaseStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.tcl.TCLStatement;
 
 import java.util.Optional;
@@ -72,11 +67,9 @@ public final class TextProtocolBackendHandlerFactory {
         if (schemaBackendHandler.isPresent()) {
             return schemaBackendHandler.get();
         }
-        if (sqlStatement instanceof ShowRDLStatement) {
-            return new RDLQueryBackendHandler(backendConnection, sqlStatement);
-        }
-        if (sqlStatement instanceof RDLStatement || sqlStatement instanceof 
CreateDatabaseStatement || sqlStatement instanceof DropDatabaseStatement) {
-            return new RDLUpdateBackendHandler(backendConnection, 
sqlStatement);
+        Optional<TextProtocolBackendHandler> rdlBackendHandler = 
RDLBackendHandlerFactory.newInstance(sqlStatement, backendConnection);
+        if (rdlBackendHandler.isPresent()) {
+            return rdlBackendHandler.get();
         }
         return DatabaseBackendHandlerFactory.newInstance(sqlStatement, sql, 
backendConnection);
     }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLBackendHandlerFactory.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLBackendHandlerFactory.java
new file mode 100644
index 0000000..3db01f2
--- /dev/null
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLBackendHandlerFactory.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.text.metadata.rdl;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.distsql.parser.statement.rdl.RDLStatement;
+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.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
+import 
org.apache.shardingsphere.proxy.backend.text.metadata.rdl.query.RDLDataSourcesQueryBackendHandler;
+import 
org.apache.shardingsphere.proxy.backend.text.metadata.rdl.query.RDLRuleQueryBackendHandler;
+import 
org.apache.shardingsphere.proxy.backend.text.metadata.rdl.update.RDLUpdateBackendHandler;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
+import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropDatabaseStatement;
+
+import java.util.Optional;
+
+/**
+ * RDL backend handler factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class RDLBackendHandlerFactory {
+    
+    /**
+     * Create new instance of RDL backend handler.
+     *
+     * @param sqlStatement SQL statement
+     * @param backendConnection backend connection
+     * @return text protocol backend handler
+     */
+    public static Optional<TextProtocolBackendHandler> newInstance(final 
SQLStatement sqlStatement, final BackendConnection backendConnection) {
+        if (sqlStatement instanceof ShowRuleStatement) {
+            return Optional.of(new 
RDLRuleQueryBackendHandler((ShowRuleStatement) sqlStatement, 
backendConnection));
+        }
+        if (sqlStatement instanceof ShowDataSourcesStatement) {
+            return Optional.of(new 
RDLDataSourcesQueryBackendHandler((ShowDataSourcesStatement) sqlStatement, 
backendConnection));
+        }
+        if (sqlStatement instanceof RDLStatement || sqlStatement instanceof 
CreateDatabaseStatement || sqlStatement instanceof DropDatabaseStatement) {
+            return Optional.of(new RDLUpdateBackendHandler(sqlStatement, 
backendConnection));
+        }
+        return Optional.empty();
+    }
+}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/query/RDLDataSourcesQueryBackendHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/query/RDLDataSourcesQueryBackendHandler.java
new file mode 100644
index 0000000..1325592
--- /dev/null
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/query/RDLDataSourcesQueryBackendHandler.java
@@ -0,0 +1,79 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.proxy.backend.text.metadata.rdl.query;
+
+import lombok.RequiredArgsConstructor;
+import 
org.apache.shardingsphere.distsql.parser.statement.rdl.show.impl.ShowDataSourcesStatement;
+import 
org.apache.shardingsphere.infra.binder.statement.rdl.ShowDataSourcesStatementContext;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceConverter;
+import org.apache.shardingsphere.infra.config.datasource.DataSourceParameter;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import 
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
+import 
org.apache.shardingsphere.proxy.backend.response.header.query.QueryResponseHeader;
+import 
org.apache.shardingsphere.proxy.backend.response.header.query.impl.QueryHeader;
+import org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
+import 
org.apache.shardingsphere.proxy.config.util.DataSourceParameterConverter;
+
+import java.sql.Types;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * Backend handler for RDL data sources query.
+ */
+@RequiredArgsConstructor
+public final class RDLDataSourcesQueryBackendHandler implements 
TextProtocolBackendHandler {
+    
+    private final ShowDataSourcesStatement sqlStatement;
+    
+    private final BackendConnection backendConnection;
+    
+    private Map<String, DataSourceParameter> dataSourceParameterMap;
+    
+    private Iterator<String> dataSourceNames;
+    
+    @Override
+    public ResponseHeader execute() {
+        return execute(new ShowDataSourcesStatementContext(sqlStatement));
+    }
+    
+    private ResponseHeader execute(final ShowDataSourcesStatementContext 
context) {
+        String schemaName = null == context.getSqlStatement().getSchemaName() 
? backendConnection.getSchemaName() : 
context.getSqlStatement().getSchemaName().getIdentifier().getValue();
+        QueryHeader nameQueryHeader = new QueryHeader(schemaName, "", "name", 
"name", Types.CHAR, "CHAR", 255, 0, false, false, false, false);
+        QueryHeader contentQueryHeader = new QueryHeader(schemaName, "", "data 
source", "data source", Types.CHAR, "CHAR", 255, 0, false, false, false, false);
+        dataSourceParameterMap = 
DataSourceParameterConverter.getDataSourceParameterMap(
+                
DataSourceConverter.getDataSourceConfigurationMap(ProxyContext.getInstance().getMetaData(schemaName).getResource().getDataSources()));
+        dataSourceNames = dataSourceParameterMap.keySet().iterator();
+        return new QueryResponseHeader(Arrays.asList(nameQueryHeader, 
contentQueryHeader));
+    }
+    
+    @Override
+    public boolean next() {
+        return dataSourceNames.hasNext();
+    }
+    
+    @Override
+    public Collection<Object> getRowData() {
+        String dataSourceName = dataSourceNames.next();
+        return Arrays.asList(dataSourceName, 
YamlEngine.marshal(dataSourceParameterMap.get(dataSourceName)));
+    }
+}
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLQueryBackendHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/query/RDLRuleQueryBackendHandler.java
similarity index 78%
rename from 
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLQueryBackendHandler.java
rename to 
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/query/RDLRuleQueryBackendHandler.java
index 311a77e..2e4776a 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLQueryBackendHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/query/RDLRuleQueryBackendHandler.java
@@ -15,12 +15,11 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.proxy.backend.text.metadata.rdl;
+package org.apache.shardingsphere.proxy.backend.text.metadata.rdl.query;
 
 import lombok.RequiredArgsConstructor;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.show.impl.ShowRuleStatement;
 import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
 import 
org.apache.shardingsphere.infra.binder.statement.rdl.ShowRuleStatementContext;
 import org.apache.shardingsphere.infra.config.RuleConfiguration;
 import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
@@ -35,7 +34,6 @@ import 
org.apache.shardingsphere.proxy.backend.text.TextProtocolBackendHandler;
 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.sql.parser.sql.common.statement.SQLStatement;
 
 import java.sql.Types;
 import java.util.Collection;
@@ -44,28 +42,27 @@ import java.util.Iterator;
 import java.util.Optional;
 
 /**
- * Backend handler for RDL query.
+ * Backend handler for RDL rule query.
  */
 @RequiredArgsConstructor
-public final class RDLQueryBackendHandler implements 
TextProtocolBackendHandler {
+public final class RDLRuleQueryBackendHandler implements 
TextProtocolBackendHandler {
     
-    private final BackendConnection backendConnection;
+    private final ShowRuleStatement sqlStatement;
     
-    private final SQLStatement sqlStatement;
+    private final BackendConnection backendConnection;
     
-    private Iterator<RuleConfiguration> ruleConfigData;
+    private Iterator<RuleConfiguration> data;
     
     @Override
     public ResponseHeader execute() {
-        SQLStatementContext<?> context = getSQLStatementContext();
-        return getResponseHeader(context);
+        return execute(new ShowRuleStatementContext(sqlStatement));
     }
     
     private ResponseHeader execute(final ShowRuleStatementContext context) {
         String schemaName = null == context.getSqlStatement().getSchemaName() 
? backendConnection.getSchemaName() : 
context.getSqlStatement().getSchemaName().getIdentifier().getValue();
         String ruleType = context.getSqlStatement().getRuleType();
         QueryHeader queryHeader = new QueryHeader(schemaName, "", ruleType, 
ruleType, Types.CHAR, "CHAR", 255, 0, false, false, false, false);
-        ruleConfigData = loadRuleConfiguration(schemaName, ruleType);
+        data = loadRuleConfiguration(schemaName, ruleType);
         return new QueryResponseHeader(Collections.singletonList(queryHeader));
     }
     
@@ -91,28 +88,14 @@ public final class RDLQueryBackendHandler implements 
TextProtocolBackendHandler
         }
     }
     
-    private SQLStatementContext<?> getSQLStatementContext() {
-        if (sqlStatement instanceof ShowRuleStatement) {
-            return new ShowRuleStatementContext((ShowRuleStatement) 
sqlStatement);
-        }
-        throw new 
UnsupportedOperationException(sqlStatement.getClass().getName());
-    }
-    
-    private ResponseHeader getResponseHeader(final SQLStatementContext<?> 
context) {
-        if (context instanceof ShowRuleStatementContext) {
-            return execute((ShowRuleStatementContext) context);
-        }
-        throw new UnsupportedOperationException(context.getClass().getName());
-    }
-    
     @Override
     public boolean next() {
-        return ruleConfigData.hasNext();
+        return data.hasNext();
     }
     
     @Override
     public Collection<Object> getRowData() {
-        RuleConfiguration ruleConfig = ruleConfigData.next();
+        RuleConfiguration ruleConfig = data.next();
         YamlRuleConfiguration yamlRuleConfig = new 
YamlRuleConfigurationSwapperEngine().swapToYamlConfigurations(Collections.singleton(ruleConfig)).iterator().next();
         return Collections.singleton(YamlEngine.marshal(yamlRuleConfig));
     }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLUpdateBackendHandler.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/update/RDLUpdateBackendHandler.java
similarity index 97%
rename from 
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLUpdateBackendHandler.java
rename to 
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/update/RDLUpdateBackendHandler.java
index c06dfd9..667671a 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLUpdateBackendHandler.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/update/RDLUpdateBackendHandler.java
@@ -15,7 +15,7 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.proxy.backend.text.metadata.rdl;
+package org.apache.shardingsphere.proxy.backend.text.metadata.rdl.update;
 
 import lombok.RequiredArgsConstructor;
 import 
org.apache.shardingsphere.distsql.parser.statement.rdl.create.impl.CreateDataSourcesStatement;
@@ -60,17 +60,16 @@ import java.util.Map;
 @RequiredArgsConstructor
 public final class RDLUpdateBackendHandler implements 
TextProtocolBackendHandler {
     
-    private final BackendConnection backendConnection;
-    
     private final SQLStatement sqlStatement;
     
+    private final BackendConnection backendConnection;
+    
     @Override
     public ResponseHeader execute() throws SQLException {
-        SQLStatementContext<?> context = getSQLStatementContext();
         if (!isRegistryCenterExisted()) {
-            throw new SQLException(String.format("No Registry center to 
execute `%s` SQL", context.getClass().getSimpleName()));
+            throw new SQLException(String.format("No Registry center to 
execute `%s` SQL", sqlStatement.getClass().getSimpleName()));
         }
-        return getResponseHeader(context);
+        return getResponseHeader(getSQLStatementContext());
     }
     
     private ResponseHeader execute(final CreateDatabaseStatementContext 
context) {
@@ -108,6 +107,10 @@ public final class RDLUpdateBackendHandler implements 
TextProtocolBackendHandler
         return new UpdateResponseHeader(context.getSqlStatement());
     }
     
+    private boolean isRegistryCenterExisted() {
+        return !(ProxyContext.getInstance().getMetaDataContexts() instanceof 
StandardMetaDataContexts);
+    }
+    
     private SQLStatementContext<?> getSQLStatementContext() {
         DatabaseType databaseType = 
ProxyContext.getInstance().getMetaDataContexts().getMetaData(backendConnection.getSchemaName()).getResource().getDatabaseType();
         if (sqlStatement instanceof CreateDataSourcesStatement) {
@@ -140,8 +143,4 @@ public final class RDLUpdateBackendHandler implements 
TextProtocolBackendHandler
         }
         throw new UnsupportedOperationException(context.getClass().getName());
     }
-    
-    private boolean isRegistryCenterExisted() {
-        return !(ProxyContext.getInstance().getMetaDataContexts() instanceof 
StandardMetaDataContexts);
-    }
 }
diff --git 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLBackendHandlerTest.java
 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLBackendHandlerTest.java
index f2ccc23..fef5349 100644
--- 
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLBackendHandlerTest.java
+++ 
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/metadata/rdl/RDLBackendHandlerTest.java
@@ -32,6 +32,7 @@ import 
org.apache.shardingsphere.proxy.backend.context.ProxyContext;
 import 
org.apache.shardingsphere.proxy.backend.exception.DBCreateExistsException;
 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.metadata.rdl.update.RDLUpdateBackendHandler;
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateDatabaseStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DropDatabaseStatement;
 import 
org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.ddl.MySQLCreateDatabaseStatement;
@@ -80,11 +81,11 @@ public final class RDLBackendHandlerTest {
         BackendConnection connection = mock(BackendConnection.class);
         when(connection.getSchemaName()).thenReturn("schema");
         sqlStatement.setDatabaseName("new_db");
-        RDLUpdateBackendHandler executeEngine = new 
RDLUpdateBackendHandler(connection, sqlStatement);
+        RDLUpdateBackendHandler executeEngine = new 
RDLUpdateBackendHandler(sqlStatement, connection);
         try {
             executeEngine.execute();
         } catch (final SQLException ex) {
-            assertThat(ex.getMessage(), is("No Registry center to execute 
`CreateDatabaseStatementContext` SQL"));
+            assertThat(ex.getMessage(), is(String.format("No Registry center 
to execute `%s` SQL", sqlStatement.getClass().getSimpleName())));
         }
         setGovernanceMetaDataContexts(true);
         ResponseHeader response = executeEngine.execute();
@@ -105,11 +106,11 @@ public final class RDLBackendHandlerTest {
         BackendConnection connection = mock(BackendConnection.class);
         when(connection.getSchemaName()).thenReturn("schema");
         sqlStatement.setDatabaseName("schema");
-        RDLUpdateBackendHandler executeEngine = new 
RDLUpdateBackendHandler(connection, sqlStatement);
+        RDLUpdateBackendHandler executeEngine = new 
RDLUpdateBackendHandler(sqlStatement, connection);
         try {
             executeEngine.execute();
         } catch (final SQLException ex) {
-            assertThat(ex.getMessage(), is("No Registry center to execute 
`DropDatabaseStatementContext` SQL"));
+            assertThat(ex.getMessage(), is(String.format("No Registry center 
to execute `%s` SQL", sqlStatement.getClass().getSimpleName())));
         }
         setGovernanceMetaDataContexts(true);
         ResponseHeader response = executeEngine.execute();
@@ -130,11 +131,11 @@ public final class RDLBackendHandlerTest {
         BackendConnection connection = mock(BackendConnection.class);
         when(connection.getSchemaName()).thenReturn("schema");
         sqlStatement.setDatabaseName("schema");
-        RDLUpdateBackendHandler executeEngine = new 
RDLUpdateBackendHandler(connection, sqlStatement);
+        RDLUpdateBackendHandler executeEngine = new 
RDLUpdateBackendHandler(sqlStatement, connection);
         try {
             executeEngine.execute();
         } catch (final SQLException ex) {
-            assertThat(ex.getMessage(), is("No Registry center to execute 
`CreateDatabaseStatementContext` SQL"));
+            assertThat(ex.getMessage(), is(String.format("No Registry center 
to execute `%s` SQL", sqlStatement.getClass().getSimpleName())));
         }
         setGovernanceMetaDataContexts(true);
         try {
@@ -154,11 +155,11 @@ public final class RDLBackendHandlerTest {
     public void assertExecuteDataSourcesContext() throws SQLException {
         BackendConnection connection = mock(BackendConnection.class);
         when(connection.getSchemaName()).thenReturn("schema");
-        RDLUpdateBackendHandler executeEngine = new 
RDLUpdateBackendHandler(connection, mock(CreateDataSourcesStatement.class));
+        RDLUpdateBackendHandler executeEngine = new 
RDLUpdateBackendHandler(mock(CreateDataSourcesStatement.class), connection);
         try {
             executeEngine.execute();
         } catch (final SQLException ex) {
-            assertThat(ex.getMessage(), is("No Registry center to execute 
`CreateDataSourcesStatementContext` SQL"));
+            assertThat(ex.getMessage(), is("No Registry center to execute 
`CreateDataSourcesStatement` SQL"));
         }
         setGovernanceMetaDataContexts(true);
         ResponseHeader response = executeEngine.execute();
@@ -169,11 +170,11 @@ public final class RDLBackendHandlerTest {
     public void assertExecuteShardingRuleContext() throws SQLException {
         BackendConnection connection = mock(BackendConnection.class);
         when(connection.getSchemaName()).thenReturn("schema");
-        RDLUpdateBackendHandler executeEngine = new 
RDLUpdateBackendHandler(connection, mock(CreateShardingRuleStatement.class));
+        RDLUpdateBackendHandler executeEngine = new 
RDLUpdateBackendHandler(mock(CreateShardingRuleStatement.class), connection);
         try {
             executeEngine.execute();
         } catch (final SQLException ex) {
-            assertThat(ex.getMessage(), is("No Registry center to execute 
`CreateShardingRuleStatementContext` SQL"));
+            assertThat(ex.getMessage(), is("No Registry center to execute 
`CreateShardingRuleStatement` SQL"));
         }
         setGovernanceMetaDataContexts(true);
         ResponseHeader response = executeEngine.execute();

Reply via email to