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 e18d3cf Show sharding table rules % show sharding table rule table
(#10503)
e18d3cf is described below
commit e18d3cf6e02f2a26fb8e2c76094863fbd37e0cc9
Author: Haoran Meng <[email protected]>
AuthorDate: Thu May 27 19:16:34 2021 +0800
Show sharding table rules % show sharding table rule table (#10503)
Co-authored-by: menghaoranss <[email protected]>
---
.../src/main/antlr4/imports/RQLStatement.g4 | 12 +-
.../distsql/parser/autogen/DistSQLStatement.g4 | 2 +-
.../distsql/parser/core/DistSQLVisitor.java | 24 +--
...t.java => ShowShardingTableRulesStatement.java} | 26 +--
.../text/distsql/rql/RQLBackendHandlerFactory.java | 5 +
.../distsql/rql/impl/RuleQueryBackendHandler.java | 94 --------
.../rql/impl/ShardingRuleQueryBackendHandler.java | 157 --------------
.../ShardingTableRulesQueryBackendHandler.java | 238 +++++++++++++++++++++
.../ShardingTableRulesQueryBackendHandlerTest.java | 121 +++++++++++
.../src/test/resources/yaml/config-sharding.yaml | 51 +++++
10 files changed, 439 insertions(+), 291 deletions(-)
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RQLStatement.g4
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RQLStatement.g4
index 2714e30..a275d96 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RQLStatement.g4
+++
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-engine/src/main/antlr4/imports/RQLStatement.g4
@@ -23,10 +23,6 @@ showResources
: SHOW RESOURCES (FROM schemaName)?
;
-showRule
- : SHOW ruleType RULE (FROM schemaName)?
- ;
-
showShardingBindingTableRules
: SHOW SHARDING BINDING TABLE RULES (FROM schemaName)?
;
@@ -51,8 +47,12 @@ showEncryptTableRule
: SHOW ENCRYPT TABLE RULE tableName (FROM schemaName)?
;
-ruleType
- : SHARDING | REPLICA_QUERY | ENCRYPT | SHADOW
+showShardingTableRules
+ : SHOW SHARDING TABLE (tableRule | RULES) (FROM schemaName)?
+ ;
+
+tableRule
+ : RULE tableName
;
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 2798467..a7bb380 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
@@ -41,13 +41,13 @@ execute
| alterEncryptRule
| dropEncryptRule
| showResources
- | showRule
| showShardingBindingTableRules
| showShardingBroadcastTableRules
| showReadwriteSplittingRules
| showDatabaseDiscoveryRules
| showEncryptRules
| showEncryptTableRule
+ | showShardingTableRules
| showScalingJobList
| showScalingJobStatus
| startScalingJob
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 771900b..3af5c2b 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
@@ -41,11 +41,10 @@ import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.S
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowEncryptTableRuleContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowReadwriteSplittingRulesContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowResourcesContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowRuleContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowScalingJobListContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowScalingJobStatusContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowShardingBroadcastTableRulesContext;
-import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowShardingRuleContext;
+import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.ShowShardingTableRulesContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.StartScalingJobContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.StopScalingJobContext;
import
org.apache.shardingsphere.distsql.parser.autogen.DistSQLStatementParser.TableNameContext;
@@ -89,9 +88,9 @@ import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowEncryptRu
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowEncryptTableRuleStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowReadwriteSplittingRulesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
-import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRuleStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowShardingBindingTableRulesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowShardingBroadcastTableRulesStatement;
+import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowShardingTableRulesStatement;
import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.TableNameSegment;
@@ -370,24 +369,11 @@ public final class DistSQLVisitor extends
DistSQLStatementBaseVisitor<ASTNode> {
result.getRuleNames().addAll(ctx.IDENTIFIER().stream().map(TerminalNode::getText).collect(Collectors.toList()));
return result;
}
-
- @Override
- public ASTNode visitShowShardingRule(final ShowShardingRuleContext ctx) {
- if (null != ctx.schemaName()) {
- return new ShowRuleStatement("sharding", (SchemaSegment)
visitSchemaName(ctx.schemaName()));
- }
- return new ShowRuleStatement("sharding", null);
- }
@Override
public ASTNode visitShowResources(final ShowResourcesContext ctx) {
return new ShowResourcesStatement(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 visitShowShardingBindingTableRules(final
DistSQLStatementParser.ShowShardingBindingTableRulesContext ctx) {
@@ -435,6 +421,12 @@ public final class DistSQLVisitor extends
DistSQLStatementBaseVisitor<ASTNode> {
}
@Override
+ public ASTNode visitShowShardingTableRules(final
ShowShardingTableRulesContext ctx) {
+ return new
ShowShardingTableRulesStatement(Objects.nonNull(ctx.tableRule()) ?
ctx.tableRule().tableName().getText() : null,
+ Objects.nonNull(ctx.schemaName()) ? (SchemaSegment)
visit(ctx.schemaName()) : null);
+ }
+
+ @Override
public ASTNode visitShowDatabaseDiscoveryRules(final
ShowDatabaseDiscoveryRulesContext ctx) {
return new
ShowDatabaseDiscoveryRulesStatement(Objects.nonNull(ctx.schemaName()) ?
(SchemaSegment) visit(ctx.schemaName()) : null);
}
diff --git
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rql/show/ShowRuleStatement.java
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rql/show/ShowShardingTableRulesStatement.java
similarity index 62%
rename from
shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rql/show/ShowRuleStatement.java
rename to
shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rql/show/ShowShardingTableRulesStatement.java
index bf248b9..717f3cd 100644
---
a/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rql/show/ShowRuleStatement.java
+++
b/shardingsphere-distsql-parser/shardingsphere-distsql-parser-statement/src/main/java/org/apache/shardingsphere/distsql/parser/statement/rql/show/ShowShardingTableRulesStatement.java
@@ -18,26 +18,18 @@
package org.apache.shardingsphere.distsql.parser.statement.rql.show;
import lombok.Getter;
-import lombok.RequiredArgsConstructor;
-import org.apache.shardingsphere.distsql.parser.statement.rql.RQLStatement;
import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.statement.available.FromSchemaAvailable;
-
-import java.util.Optional;
/**
- * Show rule statement.
+ * Show sharding table rules statement.
*/
-@RequiredArgsConstructor
-public final class ShowRuleStatement extends RQLStatement implements
FromSchemaAvailable {
-
- @Getter
- private final String ruleType;
-
- private final SchemaSegment schema;
-
- @Override
- public Optional<SchemaSegment> getSchema() {
- return Optional.ofNullable(schema);
+@Getter
+public final class ShowShardingTableRulesStatement extends ShowRulesStatement {
+
+ private final String tableName;
+
+ public ShowShardingTableRulesStatement(final String tableName, final
SchemaSegment schema) {
+ super(schema);
+ this.tableName = tableName;
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RQLBackendHandlerFactory.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RQLBackendHandlerFactory.java
index e945d1e..4924b08 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RQLBackendHandlerFactory.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RQLBackendHandlerFactory.java
@@ -26,6 +26,7 @@ import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowReadwrite
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowShardingBindingTableRulesStatement;
import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowShardingBroadcastTableRulesStatement;
+import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowShardingTableRulesStatement;
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.distsql.rql.impl.DataSourcesQueryBackendHandler;
@@ -35,6 +36,7 @@ import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.EncryptTabl
import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.ReadwriteSplittingRulesQueryBackendHandler;
import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.ShardingBindingTableRulesQueryBackendHandler;
import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.ShardingBroadcastTableRulesQueryBackendHandler;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.ShardingTableRulesQueryBackendHandler;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Optional;
@@ -74,6 +76,9 @@ public final class RQLBackendHandlerFactory {
if (sqlStatement instanceof ShowEncryptTableRuleStatement) {
return Optional.of(new
EncryptTableRuleQueryBackendHandler((ShowEncryptTableRuleStatement)
sqlStatement, backendConnection));
}
+ if (sqlStatement instanceof ShowShardingTableRulesStatement) {
+ return Optional.of(new
ShardingTableRulesQueryBackendHandler((ShowShardingTableRulesStatement)
sqlStatement, backendConnection));
+ }
return Optional.empty();
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/RuleQueryBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/RuleQueryBackendHandler.java
deleted file mode 100644
index 1c8413f..0000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/RuleQueryBackendHandler.java
+++ /dev/null
@@ -1,94 +0,0 @@
-/*
- * 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.distsql.rql.impl;
-
-import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRuleStatement;
-import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
-import org.apache.shardingsphere.infra.config.RuleConfiguration;
-import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
-import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
-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.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.SchemaRequiredBackendHandler;
-import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
-import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
-import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
-
-import java.sql.Types;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.Optional;
-
-/**
- * Backend handler for show rules.
- */
-public final class RuleQueryBackendHandler extends
SchemaRequiredBackendHandler<ShowRuleStatement> {
-
- private Iterator<RuleConfiguration> data;
-
- public RuleQueryBackendHandler(final ShowRuleStatement sqlStatement, final
BackendConnection backendConnection) {
- super(sqlStatement, backendConnection);
- }
-
- @Override
- protected ResponseHeader execute(final String schemaName, final
ShowRuleStatement sqlStatement) {
- String ruleType = sqlStatement.getRuleType();
- QueryHeader queryHeader = new QueryHeader(schemaName, "", ruleType,
ruleType, Types.CHAR, "CHAR", 255, 0, false, false, false, false);
- data = loadRuleConfiguration(schemaName, ruleType);
- return new QueryResponseHeader(Collections.singletonList(queryHeader));
- }
-
- private Iterator<RuleConfiguration> loadRuleConfiguration(final String
schemaName, final String ruleType) {
- Class<? extends RuleConfiguration> ruleConfigurationClass =
getRuleConfigurationClass(ruleType);
- Optional<RuleConfiguration> ruleConfig =
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations()
- .stream().filter(each ->
ruleConfigurationClass.isAssignableFrom(each.getClass())).findAny();
- return ruleConfig.map(optional ->
Collections.singleton(optional).iterator()).orElse(Collections.emptyIterator());
- }
-
- private Class<? extends RuleConfiguration> getRuleConfigurationClass(final
String ruleType) {
- switch (ruleType.toUpperCase()) {
- case "SHARDING":
- return ShardingRuleConfiguration.class;
- case "REPLICA_QUERY":
- return ReadwriteSplittingRuleConfiguration.class;
- case "ENCRYPT":
- return EncryptRuleConfiguration.class;
- case "SHADOW":
- return ShadowRuleConfiguration.class;
- default:
- throw new UnsupportedOperationException(ruleType);
- }
- }
-
- @Override
- public boolean next() {
- return data.hasNext();
- }
-
- @Override
- public Collection<Object> getRowData() {
- RuleConfiguration ruleConfig = data.next();
- YamlRuleConfiguration yamlRuleConfig = new
YamlRuleConfigurationSwapperEngine().swapToYamlRuleConfigurations(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/distsql/rql/impl/ShardingRuleQueryBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingRuleQueryBackendHandler.java
deleted file mode 100644
index 9d49e8f..0000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingRuleQueryBackendHandler.java
+++ /dev/null
@@ -1,157 +0,0 @@
-/*
- * 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.distsql.rql.impl;
-
-import com.google.gson.Gson;
-import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowRuleStatement;
-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.SchemaRequiredBackendHandler;
-import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration;
-import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
-import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
-import org.apache.shardingsphere.sharding.rule.BindingTableRule;
-import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sharding.rule.TableRule;
-
-import java.sql.Types;
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-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;
-
-/**
- * Backend handler for show sharding rules.
- */
-public final class ShardingRuleQueryBackendHandler extends
SchemaRequiredBackendHandler<ShowRuleStatement> {
-
- private Iterator<Map<String, Object>> data;
-
- private final String schema;
-
- public ShardingRuleQueryBackendHandler(final ShowRuleStatement
sqlStatement, final BackendConnection backendConnection) {
- super(sqlStatement, backendConnection);
- if (sqlStatement.getSchema().isPresent()) {
- schema = sqlStatement.getSchema().get().getIdentifier().getValue();
- } else {
- schema = backendConnection.getSchemaName();
- }
- }
-
- @Override
- protected ResponseHeader execute(final String schemaName, final
ShowRuleStatement sqlStatement) {
- List<QueryHeader> queryHeader = getQueryHeader();
- data = loadRuleConfiguration();
- return new QueryResponseHeader(queryHeader);
- }
-
- private List<QueryHeader> getQueryHeader() {
- List<QueryHeader> result = new LinkedList<>();
- result.add(new QueryHeader(schema, "", "name", "name", Types.CHAR,
"CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schema, "", "actualDataNodes",
"actualDataNodes", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schema, "", "tableStrategy",
"tableStrategy", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schema, "", "databaseStrategy",
"databaseStrategy", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schema, "", "keyGenerateStrategy",
"keyGenerateStrategy", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schema, "", "bindingTable", "bindingTable",
Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- return result;
- }
-
- private Iterator<Map<String, Object>> loadRuleConfiguration() {
- List<Map<String, Object>> result = new LinkedList<>();
- Optional<ShardingRule> ruleConfig =
ProxyContext.getInstance().getMetaData(schema).getRuleMetaData().getRules()
- .stream().filter(each -> each instanceof
ShardingRule).map(each -> (ShardingRule) each).findFirst();
- if (ruleConfig.isPresent()) {
- Collection<Collection<String>> bindingTables =
ruleConfig.get().getBindingTableRules().stream().map(BindingTableRule::getAllLogicTables).collect(Collectors.toList());
- for (TableRule each : ruleConfig.get().getTableRules()) {
- Map<String, Object> table = new HashMap<>();
- table.put("name", each.getLogicTable());
- table.put("actualDataNodes", each.getActualDataNodes());
- ShardingStrategyConfiguration tableShardingStrategy = null !=
each.getTableShardingStrategyConfig()
- ? each.getTableShardingStrategyConfig() :
ruleConfig.get().getDefaultTableShardingStrategyConfig();
- table.put("tableStrategy",
generateShardingStrategy(ruleConfig.get(), tableShardingStrategy));
- ShardingStrategyConfiguration databaseShardingStrategy = null
!= each.getDatabaseShardingStrategyConfig()
- ? each.getDatabaseShardingStrategyConfig() :
ruleConfig.get().getDefaultDatabaseShardingStrategyConfig();
- table.put("databaseStrategy",
generateShardingStrategy(ruleConfig.get(), databaseShardingStrategy));
- table.put("keyGenerateStrategy",
generateKeyGenerateStrategy(ruleConfig.get(), each));
- table.put("bindingTable", generateBindingTable(bindingTables,
each.getLogicTable()));
- result.add(table);
- }
- }
-
- return result.iterator();
- }
-
- private String generateBindingTable(final Collection<Collection<String>>
bindingTableGroups, final String tableName) {
- Set<String> bindingTable = new HashSet<>();
- for (Collection<String> each : bindingTableGroups) {
- if (each.contains(tableName)) {
- for (String table : each) {
- if (!table.equals(tableName)) {
- bindingTable.add(table);
- }
- }
- }
- }
- return (new Gson()).toJson(bindingTable);
- }
-
- private String generateShardingStrategy(final ShardingRule rule, final
ShardingStrategyConfiguration shardingStrategy) {
- StringBuilder result = new StringBuilder();
- if (shardingStrategy instanceof ComplexShardingStrategyConfiguration) {
- result.append("shardingColumns:");
- result.append(((ComplexShardingStrategyConfiguration)
shardingStrategy).getShardingColumns());
- result.append(" ");
- } else if (shardingStrategy instanceof
StandardShardingStrategyConfiguration) {
- result.append("shardingColumn:");
- result.append(((StandardShardingStrategyConfiguration)
shardingStrategy).getShardingColumn());
- result.append(" ");
- }
- result.append((new
Gson()).toJson(rule.getShardingAlgorithms().get(shardingStrategy.getShardingAlgorithmName())));
- return result.toString();
- }
-
- private String generateKeyGenerateStrategy(final ShardingRule rule, final
TableRule tableRule) {
- StringBuilder result = new StringBuilder();
- result.append("column:");
- result.append(tableRule.getGenerateKeyColumn());
- result.append(" ");
- result.append((new
Gson()).toJson(rule.getKeyGenerators().get(tableRule.getKeyGeneratorName())));
- return result.toString();
- }
-
- @Override
- public boolean next() {
- return data.hasNext();
- }
-
- @Override
- public Collection<Object> getRowData() {
- Map<String, Object> table = data.next();
- return Arrays.asList(table.get("name"), table.get("actualDataNodes"),
table.get("tableStrategy"), table.get("databaseStrategy"),
table.get("keyGenerateStrategy"), table.get("bindingTable"));
- }
-}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingTableRulesQueryBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingTableRulesQueryBackendHandler.java
new file mode 100644
index 0000000..6371499
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingTableRulesQueryBackendHandler.java
@@ -0,0 +1,238 @@
+/*
+ * 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.distsql.rql.impl;
+
+import com.google.common.base.Joiner;
+import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowShardingTableRulesStatement;
+import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+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.SchemaRequiredBackendHandler;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import
org.apache.shardingsphere.sharding.api.config.rule.ShardingAutoTableRuleConfiguration;
+import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import
org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
+import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.ComplexShardingStrategyConfiguration;
+import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShardingStrategyConfiguration;
+import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.ShardingStrategyConfiguration;
+import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
+
+import java.sql.Types;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+/**
+ * Backend handler for show sharding table rules.
+ */
+public final class ShardingTableRulesQueryBackendHandler extends
SchemaRequiredBackendHandler<ShowShardingTableRulesStatement> {
+
+ private Iterator<ShardingTableRuleConfiguration> tables;
+
+ private Iterator<ShardingAutoTableRuleConfiguration> autoTables;
+
+ private ShardingRuleConfiguration shardingRuleConfiguration;
+
+ public ShardingTableRulesQueryBackendHandler(final
ShowShardingTableRulesStatement sqlStatement, final BackendConnection
backendConnection) {
+ super(sqlStatement, backendConnection);
+ }
+
+ @Override
+ protected ResponseHeader execute(final String schemaName, final
ShowShardingTableRulesStatement sqlStatement) {
+ loadRuleConfiguration(schemaName, sqlStatement.getTableName());
+ return new QueryResponseHeader(getQueryHeader(schemaName));
+ }
+
+ private void loadRuleConfiguration(final String schemaName, final String
tableName) {
+ Optional<ShardingRuleConfiguration> ruleConfig =
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations()
+ .stream().filter(each -> each instanceof
ShardingRuleConfiguration).map(each -> (ShardingRuleConfiguration)
each).findAny();
+ if (Objects.isNull(tableName)) {
+ tables = ruleConfig.map(optional ->
optional.getTables().iterator()).orElse(Collections.emptyIterator());
+ autoTables = ruleConfig.map(optional ->
optional.getAutoTables().iterator()).orElse(Collections.emptyIterator());
+ } else {
+ tables = ruleConfig.isPresent() ?
ruleConfig.get().getTables().stream().filter(each ->
tableName.equalsIgnoreCase(each.getLogicTable()))
+ .collect(Collectors.toList()).iterator() :
Collections.emptyIterator();
+ autoTables = ruleConfig.isPresent() ?
ruleConfig.get().getAutoTables().stream().filter(each ->
tableName.equalsIgnoreCase(each.getLogicTable()))
+ .collect(Collectors.toList()).iterator() :
Collections.emptyIterator();
+ }
+ shardingRuleConfiguration = ruleConfig.isPresent() ? ruleConfig.get()
: null;
+ }
+
+ private List<QueryHeader> getQueryHeader(final String schemaName) {
+ List<QueryHeader> result = new LinkedList<>();
+ result.add(new QueryHeader(schemaName, "", "table", "table",
Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schemaName, "", "actualDataNodes",
"actualDataNodes", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schemaName, "", "actualDataSources",
"actualDataSources", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schemaName, "", "databaseStrategyType",
"databaseStrategyType", Types.CHAR, "CHAR", 255, 0, false, false, false,
false));
+ result.add(new QueryHeader(schemaName, "", "databaseShardingColumn",
"databaseShardingColumn", Types.CHAR, "CHAR", 255, 0, false, false, false,
false));
+ result.add(new QueryHeader(schemaName, "",
"databaseShardingAlgorithmType", "databaseShardingAlgorithmType", Types.CHAR,
"CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schemaName, "",
"databaseShardingAlgorithmProps", "databaseShardingAlgorithmProps", Types.CHAR,
"CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schemaName, "", "tableStrategyType",
"tableStrategyType", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schemaName, "", "tableShardingColumn",
"tableShardingColumn", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schemaName, "",
"tableShardingAlgorithmType", "tableShardingAlgorithmType", Types.CHAR, "CHAR",
255, 0, false, false, false, false));
+ result.add(new QueryHeader(schemaName, "",
"tableShardingAlgorithmProps", "tableShardingAlgorithmProps", Types.CHAR,
"CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schemaName, "", "keyGenerateColumn",
"keyGenerateColumn", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schemaName, "", "keyGeneratorType",
"keyGeneratorType", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ result.add(new QueryHeader(schemaName, "", "keyGeneratorProps",
"keyGeneratorProps", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
+ return result;
+ }
+
+ @Override
+ public boolean next() {
+ return tables.hasNext() || autoTables.hasNext();
+ }
+
+ @Override
+ public Collection<Object> getRowData() {
+ return tables.hasNext() ? buildTableRowData(tables.next()) :
buildAutoTableRowData(autoTables.next());
+ }
+
+ private Collection<Object> buildTableRowData(final
ShardingTableRuleConfiguration shardingTableRuleConfiguration) {
+ Collection<Object> result = new LinkedList<>();
+ result.add(shardingTableRuleConfiguration.getLogicTable());
+ result.add(shardingTableRuleConfiguration.getActualDataNodes());
+ result.add("");
+ result.add(getDatabaseStrategyType(shardingTableRuleConfiguration));
+ result.add(getDatabaseShardingColumn(shardingTableRuleConfiguration));
+
result.add(getAlgorithmType(getDatabaseShardingStrategy(shardingTableRuleConfiguration)));
+
result.add(getAlgorithmProps(getDatabaseShardingStrategy(shardingTableRuleConfiguration)));
+
result.add(getTableStrategyType(shardingTableRuleConfiguration.getTableShardingStrategy()));
+
result.add(getTableShardingColumn(shardingTableRuleConfiguration.getTableShardingStrategy()));
+
result.add(getAlgorithmType(getTableShardingStrategy(shardingTableRuleConfiguration.getTableShardingStrategy())));
+
result.add(getAlgorithmProps(getTableShardingStrategy(shardingTableRuleConfiguration.getTableShardingStrategy())));
+
result.add(getKeyGenerateColumn(shardingTableRuleConfiguration.getKeyGenerateStrategy()));
+
result.add(getKeyGeneratorType(shardingTableRuleConfiguration.getKeyGenerateStrategy()));
+
result.add(getKeyGeneratorProps(shardingTableRuleConfiguration.getKeyGenerateStrategy()));
+ return result;
+ }
+
+ private Collection<Object> buildAutoTableRowData(final
ShardingAutoTableRuleConfiguration shardingAutoTableRuleConfiguration) {
+ Collection<Object> result = new LinkedList<>();
+ result.add(shardingAutoTableRuleConfiguration.getLogicTable());
+ result.add("");
+ result.add(shardingAutoTableRuleConfiguration.getActualDataSources());
+ result.add("");
+ result.add("");
+ result.add("");
+ result.add("");
+
result.add(getTableStrategyType(shardingAutoTableRuleConfiguration.getShardingStrategy()));
+
result.add(getTableShardingColumn(shardingAutoTableRuleConfiguration.getShardingStrategy()));
+
result.add(getAlgorithmType(getTableShardingStrategy(shardingAutoTableRuleConfiguration.getShardingStrategy())));
+
result.add(getAlgorithmProps(getTableShardingStrategy(shardingAutoTableRuleConfiguration.getShardingStrategy())));
+
result.add(getKeyGenerateColumn(shardingAutoTableRuleConfiguration.getKeyGenerateStrategy()));
+
result.add(getKeyGeneratorType(shardingAutoTableRuleConfiguration.getKeyGenerateStrategy()));
+
result.add(getKeyGeneratorProps(shardingAutoTableRuleConfiguration.getKeyGenerateStrategy()));
+ return result;
+ }
+
+ private String getDatabaseStrategyType(final
ShardingTableRuleConfiguration shardingTableRuleConfiguration) {
+ Optional<ShardingStrategyConfiguration> databaseShardingStrategy =
getDatabaseShardingStrategy(shardingTableRuleConfiguration);
+ return databaseShardingStrategy.isPresent() &&
!(databaseShardingStrategy.get() instanceof NoneShardingStrategyConfiguration)
+ ?
getAlgorithmConfiguration(databaseShardingStrategy.get().getShardingAlgorithmName()).getType()
: "";
+ }
+
+ private String getDatabaseShardingColumn(final
ShardingTableRuleConfiguration shardingTableRuleConfiguration) {
+ Optional<ShardingStrategyConfiguration> databaseShardingStrategy =
getDatabaseShardingStrategy(shardingTableRuleConfiguration);
+ return databaseShardingStrategy.isPresent() ?
getShardingColumn(databaseShardingStrategy.get()) : "";
+ }
+
+ private String getShardingColumn(final ShardingStrategyConfiguration
shardingStrategyConfiguration) {
+ if (shardingStrategyConfiguration instanceof
StandardShardingStrategyConfiguration) {
+ return ((StandardShardingStrategyConfiguration)
shardingStrategyConfiguration).getShardingColumn();
+ } else if (shardingStrategyConfiguration instanceof
ComplexShardingStrategyConfiguration) {
+ return ((ComplexShardingStrategyConfiguration)
shardingStrategyConfiguration).getShardingColumns();
+ }
+ return "";
+ }
+
+ private String getAlgorithmType(final
Optional<ShardingStrategyConfiguration> databaseShardingStrategy) {
+ return databaseShardingStrategy.isPresent() &&
!(databaseShardingStrategy.get() instanceof NoneShardingStrategyConfiguration)
+ ?
getAlgorithmConfiguration(databaseShardingStrategy.get().getShardingAlgorithmName()).getType()
: "";
+ }
+
+ private String getAlgorithmProps(final
Optional<ShardingStrategyConfiguration> databaseShardingStrategy) {
+ return databaseShardingStrategy.isPresent() &&
!(databaseShardingStrategy.get() instanceof NoneShardingStrategyConfiguration)
+ ?
buildProperties(getAlgorithmConfiguration(databaseShardingStrategy.get().getShardingAlgorithmName()).getProps())
: "";
+ }
+
+ private Optional<ShardingStrategyConfiguration>
getDatabaseShardingStrategy(final ShardingTableRuleConfiguration
shardingTableRuleConfiguration) {
+ return
Objects.nonNull(shardingTableRuleConfiguration.getDatabaseShardingStrategy())
+ ?
Optional.ofNullable(shardingTableRuleConfiguration.getDatabaseShardingStrategy())
+ :
Optional.ofNullable(shardingRuleConfiguration.getDefaultDatabaseShardingStrategy());
+ }
+
+ private ShardingSphereAlgorithmConfiguration
getAlgorithmConfiguration(final String algorithmName) {
+ return
shardingRuleConfiguration.getShardingAlgorithms().get(algorithmName);
+ }
+
+ private String getTableStrategyType(final ShardingStrategyConfiguration
shardingStrategyConfiguration) {
+ Optional<ShardingStrategyConfiguration> tableShardingStrategy =
getTableShardingStrategy(shardingStrategyConfiguration);
+ return tableShardingStrategy.isPresent() ?
getStrategyType(tableShardingStrategy.get()) : "";
+ }
+
+ private String getStrategyType(final ShardingStrategyConfiguration
shardingStrategyConfiguration) {
+ return shardingStrategyConfiguration instanceof
NoneShardingStrategyConfiguration ? "none"
+ :
getAlgorithmConfiguration(shardingStrategyConfiguration.getShardingAlgorithmName()).getType();
+ }
+
+ private Optional<ShardingStrategyConfiguration>
getTableShardingStrategy(final ShardingStrategyConfiguration
shardingStrategyConfiguration) {
+ return Objects.nonNull(shardingStrategyConfiguration)
+ ? Optional.ofNullable(shardingStrategyConfiguration)
+ :
Optional.ofNullable(shardingRuleConfiguration.getDefaultTableShardingStrategy());
+ }
+
+ private String getTableShardingColumn(final ShardingStrategyConfiguration
shardingStrategyConfiguration) {
+ Optional<ShardingStrategyConfiguration> tableShardingStrategy =
getTableShardingStrategy(shardingStrategyConfiguration);
+ return tableShardingStrategy.isPresent() ?
getShardingColumn(tableShardingStrategy.get()) : "";
+ }
+
+ private String getKeyGenerateColumn(final KeyGenerateStrategyConfiguration
keyGenerateStrategyConfiguration) {
+ return
getKeyGenerateStrategyConfiguration(keyGenerateStrategyConfiguration).isPresent()
+ ?
getKeyGenerateStrategyConfiguration(keyGenerateStrategyConfiguration).get().getColumn()
: "";
+ }
+
+ private String getKeyGeneratorType(final KeyGenerateStrategyConfiguration
keyGenerateStrategyConfiguration) {
+ Optional<KeyGenerateStrategyConfiguration> optional =
getKeyGenerateStrategyConfiguration(keyGenerateStrategyConfiguration);
+ return optional.isPresent() ?
shardingRuleConfiguration.getKeyGenerators().get(optional.get().getKeyGeneratorName()).getType()
: "";
+ }
+
+ private String getKeyGeneratorProps(final KeyGenerateStrategyConfiguration
keyGenerateStrategyConfiguration) {
+ Optional<KeyGenerateStrategyConfiguration> optional =
getKeyGenerateStrategyConfiguration(keyGenerateStrategyConfiguration);
+ return optional.isPresent() ?
buildProperties(shardingRuleConfiguration.getKeyGenerators().get(optional.get().getKeyGeneratorName()).getProps())
: "";
+ }
+
+ private Optional<KeyGenerateStrategyConfiguration>
getKeyGenerateStrategyConfiguration(final KeyGenerateStrategyConfiguration
keyGenerateStrategyConfiguration) {
+ return Objects.nonNull(keyGenerateStrategyConfiguration)
+ ? Optional.ofNullable(keyGenerateStrategyConfiguration)
+ :
Optional.ofNullable(shardingRuleConfiguration.getDefaultKeyGenerateStrategy());
+ }
+
+ private String buildProperties(final Properties properties) {
+ return Objects.nonNull(properties) ?
Joiner.on(",").join(properties.entrySet().stream()
+ .map(each -> Joiner.on(":").join(each.getKey(),
each.getValue())).collect(Collectors.toList())) : "";
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingTableRulesQueryBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingTableRulesQueryBackendHandlerTest.java
new file mode 100644
index 0000000..b1a60be
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingTableRulesQueryBackendHandlerTest.java
@@ -0,0 +1,121 @@
+/*
+ * 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.distsql.rql.impl;
+
+import lombok.SneakyThrows;
+import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowShardingTableRulesStatement;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
+import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import
org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+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.response.header.ResponseHeader;
+import
org.apache.shardingsphere.proxy.backend.response.header.query.QueryResponseHeader;
+import org.apache.shardingsphere.transaction.context.TransactionContexts;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.when;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class ShardingTableRulesQueryBackendHandlerTest {
+
+ @Mock
+ private BackendConnection backendConnection;
+
+ @Mock
+ private ShowShardingTableRulesStatement sqlStatement;
+
+ @Mock
+ private MetaDataContexts metaDataContexts;
+
+ @Mock
+ private TransactionContexts transactionContexts;
+
+ @Mock
+ private ShardingSphereMetaData shardingSphereMetaData;
+
+ @Mock
+ private ShardingSphereRuleMetaData ruleMetaData;
+
+ private ShardingTableRulesQueryBackendHandler handler;
+
+ @Before
+ public void setUp() {
+ ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
+ handler = new ShardingTableRulesQueryBackendHandler(sqlStatement,
backendConnection);
+
when(metaDataContexts.getAllSchemaNames()).thenReturn(Collections.singletonList("test"));
+
when(metaDataContexts.getMetaData(eq("test"))).thenReturn(shardingSphereMetaData);
+
when(shardingSphereMetaData.getRuleMetaData()).thenReturn(ruleMetaData);
+
when(ruleMetaData.getConfigurations()).thenReturn(buildShardingConfiguration());
+ }
+
+ @Test
+ public void assertExecute() {
+ ResponseHeader responseHeader = handler.execute("test", sqlStatement);
+ assertNotNull(responseHeader);
+ assertTrue(responseHeader instanceof QueryResponseHeader);
+ Collection<Object> rowData = handler.getRowData();
+ assertNotNull(rowData);
+ List<Object> rowDataList = new ArrayList<>(rowData);
+ assertThat(rowDataList.size(), is(14));
+ assertThat(rowDataList.get(0), is("t_order"));
+ assertThat(rowDataList.get(1), is("ds_${0..1}.t_order_${0..1}"));
+ assertThat(rowDataList.get(2), is(""));
+ assertThat(rowDataList.get(3), is("INLINE"));
+ assertThat(rowDataList.get(4), is("user_id"));
+ assertThat(rowDataList.get(5), is("INLINE"));
+ assertThat(rowDataList.get(6), is("algorithm-expression:ds_${user_id %
2}"));
+ assertThat(rowDataList.get(7), is("INLINE"));
+ assertThat(rowDataList.get(8), is("order_id"));
+ assertThat(rowDataList.get(9), is("INLINE"));
+ assertThat(rowDataList.get(10),
is("algorithm-expression:t_order_${order_id % 2}"));
+ assertThat(rowDataList.get(11), is("order_id"));
+ assertThat(rowDataList.get(12), is("SNOWFLAKE"));
+ assertThat(rowDataList.get(13), is("worker-id:123"));
+ }
+
+ private Collection<RuleConfiguration> buildShardingConfiguration() {
+ return new
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(YamlEngine.unmarshal(readYAML(),
Collection.class));
+ }
+
+ @SneakyThrows
+ private String readYAML() {
+ return
Files.readAllLines(Paths.get(ClassLoader.getSystemResource("yaml/config-sharding.yaml").toURI()))
+ .stream().map(each -> each +
System.lineSeparator()).collect(Collectors.joining());
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/yaml/config-sharding.yaml
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/yaml/config-sharding.yaml
new file mode 100644
index 0000000..146b380
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/resources/yaml/config-sharding.yaml
@@ -0,0 +1,51 @@
+#
+# 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.
+#
+- !SHARDING
+ tables:
+ t_order:
+ actualDataNodes: ds_${0..1}.t_order_${0..1}
+ tableStrategy:
+ standard:
+ shardingColumn: order_id
+ shardingAlgorithmName: t_order_inline
+ keyGenerateStrategy:
+ column: order_id
+ keyGeneratorName: snowflake
+ bindingTables:
+ - t_order,t_order_item
+ defaultDatabaseStrategy:
+ standard:
+ shardingColumn: user_id
+ shardingAlgorithmName: database_inline
+ defaultTableStrategy:
+ none:
+
+ shardingAlgorithms:
+ database_inline:
+ type: INLINE
+ props:
+ algorithm-expression: ds_${user_id % 2}
+ t_order_inline:
+ type: INLINE
+ props:
+ algorithm-expression: t_order_${order_id % 2}
+
+ keyGenerators:
+ snowflake:
+ type: SNOWFLAKE
+ props:
+ worker-id: 123