This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 f15e1f2 Add RuleQueryResultSet to decouple
SchemaRequiredBackendHandler and RQL (#10897)
f15e1f2 is described below
commit f15e1f296e51e6a1cb59bcf344a2103b6a619baf
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Jun 21 21:52:45 2021 +0800
Add RuleQueryResultSet to decouple SchemaRequiredBackendHandler and RQL
(#10897)
* For code style
* Decouple RuleQueryResultSet and SchemaRequiredBackendHandler
* Decouple RuleQueryResultSet and SchemaRequiredBackendHandler
* Refactor ShardingBindingTableRuleQueryResultSet
* Refactor ShardingBroadcastTableRuleQueryResultSet
* Fix test cases
* Remove generic of RuleQueryResultSet
* Refactor RuleQueryBackendHandler
* Refactor RuleQueryBackendHandlerTest
* Update RuleQueryBackendHandlerTest
* Add DatabaseDiscoveryRuleQueryResultSetTest
* Update DatabaseDiscoveryRuleQueryResultSetTest
* Update DatabaseDiscoveryRuleQueryResultSetTest
* Update DatabaseDiscoveryRuleQueryResultSetTest
* Update EncryptRuleQueryResultSetTest
* Update ReadwriteSplittingRuleQueryResultSetTest
* Update ShardingBindingTableRuleQueryResultSetTest
* Update ShardingBroadcastTableRuleQueryResultSetTest
* Update ShardingTableRuleQueryResultSetTest
* Add BaseRuleQueryResultSet
* Remove useless classes
* Move package of RuleQueryBackendHandler
* Refactor RQLBackendHandlerFactory
* Refactor RQLBackendHandlerFactory
---
.../backend/text/SchemaRequiredBackendHandler.java | 2 +
.../text/distsql/DistSQLBackendHandlerFactory.java | 2 +-
.../text/distsql/rql/RQLBackendHandlerFactory.java | 46 ++++----
.../text/distsql/rql/RuleQueryBackendHandler.java | 61 +++++++++++
.../text/distsql/rql/RuleQueryResultSet.java | 56 ++++++++++
...va => DatabaseDiscoveryRuleQueryResultSet.java} | 36 ++----
...Handler.java => EncryptRuleQueryResultSet.java} | 52 +++------
...a => ReadwriteSplittingRuleQueryResultSet.java} | 40 ++-----
.../ShardingBindingTableRuleQueryResultSet.java | 58 ++++++++++
...ardingBindingTableRulesQueryBackendHandler.java | 83 --------------
.../ShardingBroadcastTableRuleQueryResultSet.java | 58 ++++++++++
...dingBroadcastTableRulesQueryBackendHandler.java | 79 --------------
...r.java => ShardingTableRuleQueryResultSet.java} | 67 ++++--------
.../text/distsql/RDLBackendHandlerFactoryTest.java | 4 +-
.../distsql/rql/RuleQueryBackendHandlerTest.java | 77 +++++++++++++
.../distsql/rql/impl/BaseRuleQueryResultSet.java | 49 +++++++++
.../DatabaseDiscoveryRuleQueryResultSetTest.java | 61 +++++++++++
...abaseDiscoveryRulesQueryBackendHandlerTest.java | 105 ------------------
.../rql/impl/EncryptRuleQueryResultSetTest.java | 64 +++++++++++
.../impl/EncryptRulesQueryBackendHandlerTest.java | 107 ------------------
.../ReadwriteSplittingRuleQueryResultSetTest.java | 66 +++++++++++
...writeSplittingRulesQueryBackendHandlerTest.java | 115 --------------------
...ShardingBindingTableRuleQueryResultSetTest.java | 55 ++++++++++
...ngBindingTableRulesQueryBackendHandlerTest.java | 99 -----------------
...ardingBroadcastTableRuleQueryResultSetTest.java | 50 +++++++++
...BroadcastTableRulesQueryBackendHandlerTest.java | 97 -----------------
.../impl/ShardingTableRuleQueryResultSetTest.java | 74 +++++++++++++
.../ShardingTableRulesQueryBackendHandlerTest.java | 121 ---------------------
28 files changed, 810 insertions(+), 974 deletions(-)
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/SchemaRequiredBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/SchemaRequiredBackendHandler.java
index 829440e..ec4874f 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/SchemaRequiredBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/SchemaRequiredBackendHandler.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.proxy.backend.text;
+import lombok.Getter;
import lombok.RequiredArgsConstructor;
import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
@@ -35,6 +36,7 @@ import java.util.Optional;
* @param <T> type of SQL statement
*/
@RequiredArgsConstructor
+@Getter
public abstract class SchemaRequiredBackendHandler<T extends SQLStatement>
implements TextProtocolBackendHandler {
private final T sqlStatement;
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/DistSQLBackendHandlerFactory.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/DistSQLBackendHandlerFactory.java
index aa431de..9efc776 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/DistSQLBackendHandlerFactory.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/DistSQLBackendHandlerFactory.java
@@ -49,7 +49,7 @@ public final class DistSQLBackendHandlerFactory {
*/
public static Optional<TextProtocolBackendHandler> newInstance(final
DatabaseType databaseType, final SQLStatement sqlStatement, final
BackendConnection backendConnection) throws SQLException {
if (sqlStatement instanceof RQLStatement) {
- return RQLBackendHandlerFactory.newInstance(sqlStatement,
backendConnection);
+ return
Optional.of(RQLBackendHandlerFactory.newInstance((RQLStatement) sqlStatement,
backendConnection));
}
if (sqlStatement instanceof RDLStatement) {
return RDLBackendHandlerFactory.newInstance(databaseType,
sqlStatement, backendConnection);
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 37aea25..3130cca 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
@@ -20,24 +20,22 @@ package
org.apache.shardingsphere.proxy.backend.text.distsql.rql;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.ShowDatabaseDiscoveryRulesStatement;
+import org.apache.shardingsphere.distsql.parser.statement.rql.RQLStatement;
+import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
import
org.apache.shardingsphere.encrypt.distsql.parser.statement.ShowEncryptRulesStatement;
+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;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.DatabaseDiscoveryRuleQueryResultSet;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.EncryptRuleQueryResultSet;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.ReadwriteSplittingRuleQueryResultSet;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.ShardingBindingTableRuleQueryResultSet;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.ShardingBroadcastTableRuleQueryResultSet;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.ShardingTableRuleQueryResultSet;
import
org.apache.shardingsphere.readwritesplitting.distsql.parser.statement.ShowReadwriteSplittingRulesStatement;
-import
org.apache.shardingsphere.distsql.parser.statement.rql.show.ShowResourcesStatement;
import
org.apache.shardingsphere.sharding.distsql.parser.statement.ShowShardingBindingTableRulesStatement;
import
org.apache.shardingsphere.sharding.distsql.parser.statement.ShowShardingBroadcastTableRulesStatement;
import
org.apache.shardingsphere.sharding.distsql.parser.statement.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;
-import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.DatabaseDiscoveryRulesQueryBackendHandler;
-import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl.EncryptRulesQueryBackendHandler;
-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;
/**
* RQL backend handler factory.
@@ -52,28 +50,32 @@ public final class RQLBackendHandlerFactory {
* @param backendConnection backend connection
* @return RDL backend handler
*/
- public static Optional<TextProtocolBackendHandler> newInstance(final
SQLStatement sqlStatement, final BackendConnection backendConnection) {
+ public static TextProtocolBackendHandler newInstance(final RQLStatement
sqlStatement, final BackendConnection backendConnection) {
if (sqlStatement instanceof ShowResourcesStatement) {
- return Optional.of(new
DataSourcesQueryBackendHandler((ShowResourcesStatement) sqlStatement,
backendConnection));
+ return new DataSourcesQueryBackendHandler((ShowResourcesStatement)
sqlStatement, backendConnection);
}
+ return new RuleQueryBackendHandler(sqlStatement, backendConnection,
getRuleQueryResultSet(sqlStatement));
+ }
+
+ private static RuleQueryResultSet getRuleQueryResultSet(final RQLStatement
sqlStatement) {
if (sqlStatement instanceof ShowShardingBindingTableRulesStatement) {
- return Optional.of(new
ShardingBindingTableRulesQueryBackendHandler((ShowShardingBindingTableRulesStatement)
sqlStatement, backendConnection));
+ return new ShardingBindingTableRuleQueryResultSet();
}
if (sqlStatement instanceof ShowShardingBroadcastTableRulesStatement) {
- return Optional.of(new
ShardingBroadcastTableRulesQueryBackendHandler((ShowShardingBroadcastTableRulesStatement)
sqlStatement, backendConnection));
+ return new ShardingBroadcastTableRuleQueryResultSet();
}
if (sqlStatement instanceof ShowReadwriteSplittingRulesStatement) {
- return Optional.of(new
ReadwriteSplittingRulesQueryBackendHandler((ShowReadwriteSplittingRulesStatement)
sqlStatement, backendConnection));
+ return new ReadwriteSplittingRuleQueryResultSet();
}
if (sqlStatement instanceof ShowDatabaseDiscoveryRulesStatement) {
- return Optional.of(new
DatabaseDiscoveryRulesQueryBackendHandler((ShowDatabaseDiscoveryRulesStatement)
sqlStatement, backendConnection));
+ return new DatabaseDiscoveryRuleQueryResultSet();
}
if (sqlStatement instanceof ShowEncryptRulesStatement) {
- return Optional.of(new
EncryptRulesQueryBackendHandler((ShowEncryptRulesStatement) sqlStatement,
backendConnection));
+ return new EncryptRuleQueryResultSet();
}
if (sqlStatement instanceof ShowShardingTableRulesStatement) {
- return Optional.of(new
ShardingTableRulesQueryBackendHandler((ShowShardingTableRulesStatement)
sqlStatement, backendConnection));
+ return new ShardingTableRuleQueryResultSet();
}
- return Optional.empty();
+ throw new UnsupportedOperationException(String.format("Cannot support
SQL statement %s", sqlStatement.getClass().getCanonicalName()));
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleQueryBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleQueryBackendHandler.java
new file mode 100644
index 0000000..8a42164
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleQueryBackendHandler.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.distsql.rql;
+
+import org.apache.shardingsphere.distsql.parser.statement.rql.RQLStatement;
+import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+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 java.sql.Types;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+
+/**
+ * Rule query backend handler.
+ */
+public final class RuleQueryBackendHandler extends
SchemaRequiredBackendHandler<RQLStatement> {
+
+ private final RuleQueryResultSet resultSet;
+
+ public RuleQueryBackendHandler(final RQLStatement sqlStatement, final
BackendConnection backendConnection, final RuleQueryResultSet resultSet) {
+ super(sqlStatement, backendConnection);
+ this.resultSet = resultSet;
+ }
+
+ @Override
+ protected ResponseHeader execute(final String schemaName, final
RQLStatement sqlStatement) {
+ resultSet.init(schemaName, sqlStatement);
+ List<QueryHeader> queryHeaders =
resultSet.getColumnNames().stream().map(
+ each -> new QueryHeader(schemaName, "", each, each, Types.CHAR,
"CHAR", 255, 0, false, false, false, false)).collect(Collectors.toList());
+ return new QueryResponseHeader(queryHeaders);
+ }
+
+ @Override
+ public boolean next() {
+ return resultSet.next();
+ }
+
+ @Override
+ public Collection<Object> getRowData() {
+ return resultSet.getRowData();
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleQueryResultSet.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleQueryResultSet.java
new file mode 100644
index 0000000..27addd7
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleQueryResultSet.java
@@ -0,0 +1,56 @@
+/*
+ * 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;
+
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+
+import java.util.Collection;
+
+/**
+ * Rule query result set.
+ */
+public interface RuleQueryResultSet {
+
+ /**
+ * Initialize rule data.
+ * @param schemaName schema name
+ * @param sqlStatement SQL statement
+ */
+ void init(String schemaName, SQLStatement sqlStatement);
+
+ /**
+ * Get result set column names.
+ *
+ * @return result set column names
+ */
+ Collection<String> getColumnNames();
+
+ /**
+ * Go to next data.
+ *
+ * @return true if next data exist
+ */
+ boolean next();
+
+ /**
+ * Get row data.
+ *
+ * @return row data
+ */
+ Collection<Object> getRowData();
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DatabaseDiscoveryRulesQueryBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DatabaseDiscoveryRuleQueryResultSet.java
similarity index 58%
rename from
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DatabaseDiscoveryRulesQueryBackendHandler.java
rename to
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DatabaseDiscoveryRuleQueryResultSet.java
index c56fd72..f2998b4 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DatabaseDiscoveryRulesQueryBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DatabaseDiscoveryRuleQueryResultSet.java
@@ -20,59 +20,39 @@ package
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl;
import com.google.common.base.Joiner;
import
org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
import
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
-import
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.ShowDatabaseDiscoveryRulesStatement;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.properties.PropertiesConverter;
-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.proxy.backend.text.distsql.rql.RuleQueryResultSet;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import java.sql.Types;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
- * Backend handler for show database discovery rules.
+ * Result set for show database discovery rule.
*/
-public final class DatabaseDiscoveryRulesQueryBackendHandler extends
SchemaRequiredBackendHandler<ShowDatabaseDiscoveryRulesStatement> {
+public final class DatabaseDiscoveryRuleQueryResultSet implements
RuleQueryResultSet {
private Iterator<DatabaseDiscoveryDataSourceRuleConfiguration> data;
private Map<String, ShardingSphereAlgorithmConfiguration> discoveryTypes;
- public DatabaseDiscoveryRulesQueryBackendHandler(final
ShowDatabaseDiscoveryRulesStatement sqlStatement, final BackendConnection
backendConnection) {
- super(sqlStatement, backendConnection);
- }
-
@Override
- protected ResponseHeader execute(final String schemaName, final
ShowDatabaseDiscoveryRulesStatement sqlStatement) {
- loadRuleConfiguration(schemaName);
- return new QueryResponseHeader(getQueryHeader(schemaName));
- }
-
- private void loadRuleConfiguration(final String schemaName) {
+ public void init(final String schemaName, final SQLStatement sqlStatement)
{
Optional<DatabaseDiscoveryRuleConfiguration> ruleConfig =
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations()
.stream().filter(each -> each instanceof
DatabaseDiscoveryRuleConfiguration).map(each ->
(DatabaseDiscoveryRuleConfiguration) each).findAny();
data = ruleConfig.map(optional ->
optional.getDataSources().iterator()).orElse(Collections.emptyIterator());
discoveryTypes =
ruleConfig.map(DatabaseDiscoveryRuleConfiguration::getDiscoveryTypes).orElse(Collections.emptyMap());
}
- private List<QueryHeader> getQueryHeader(final String schemaName) {
- List<QueryHeader> result = new LinkedList<>();
- result.add(new QueryHeader(schemaName, "", "name", "name", Types.CHAR,
"CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schemaName, "", "dataSourceNames",
"dataSourceNames", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schemaName, "", "discoverType",
"discoverType", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schemaName, "", "discoverProps",
"discoverProps", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- return result;
+ @Override
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("name", "dataSourceNames", "discoverType",
"discoverProps");
}
@Override
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/EncryptRulesQueryBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/EncryptRuleQueryResultSet.java
similarity index 55%
rename from
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/EncryptRulesQueryBackendHandler.java
rename to
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/EncryptRuleQueryResultSet.java
index c35d363..2d0efd5 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/EncryptRulesQueryBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/EncryptRuleQueryResultSet.java
@@ -26,21 +26,15 @@ import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfigu
import
org.apache.shardingsphere.encrypt.distsql.parser.statement.ShowEncryptRulesStatement;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.properties.PropertiesConverter;
-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.proxy.backend.text.distsql.rql.RuleQueryResultSet;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import java.sql.Types;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
@@ -48,56 +42,40 @@ import java.util.Optional;
import java.util.stream.Collectors;
/**
- * Backend handler for show encrypt rules.
+ * Result set for show encrypt rule.
*/
-public final class EncryptRulesQueryBackendHandler extends
SchemaRequiredBackendHandler<ShowEncryptRulesStatement> {
+public final class EncryptRuleQueryResultSet implements RuleQueryResultSet {
private Iterator<Entry<String, EncryptColumnRuleConfiguration>> data;
private Map<String, ShardingSphereAlgorithmConfiguration> encryptors;
- public EncryptRulesQueryBackendHandler(final ShowEncryptRulesStatement
sqlStatement, final BackendConnection backendConnection) {
- super(sqlStatement, backendConnection);
- }
-
@Override
- protected ResponseHeader execute(final String schemaName, final
ShowEncryptRulesStatement sqlStatement) {
- loadRuleConfiguration(schemaName, sqlStatement.getTableName());
- return new QueryResponseHeader(getQueryHeader(schemaName));
- }
-
- private void loadRuleConfiguration(final String schemaName, final String
tableName) {
+ public void init(final String schemaName, final SQLStatement sqlStatement)
{
Optional<EncryptRuleConfiguration> ruleConfig =
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations()
.stream().filter(each -> each instanceof
EncryptRuleConfiguration).map(each -> (EncryptRuleConfiguration)
each).findAny();
- data = ruleConfig.map(optional -> getAllEncryptColumns(optional,
tableName).entrySet().iterator()).orElse(Collections.emptyIterator());
+ data = ruleConfig.map(optional -> getAllEncryptColumns(optional,
((ShowEncryptRulesStatement)
sqlStatement).getTableName()).entrySet().iterator()).orElse(Collections.emptyIterator());
encryptors =
ruleConfig.map(EncryptRuleConfiguration::getEncryptors).orElse(Maps.newHashMap());
}
-
- private Map<String, EncryptColumnRuleConfiguration>
getAllEncryptColumns(final EncryptRuleConfiguration encryptRuleConfiguration,
final String tableName) {
+
+ private Map<String, EncryptColumnRuleConfiguration>
getAllEncryptColumns(final EncryptRuleConfiguration encryptRuleConfig, final
String tableName) {
Map<String, EncryptColumnRuleConfiguration> result = new HashMap<>();
if (Objects.nonNull(tableName)) {
- encryptRuleConfiguration.getTables().stream().filter(each ->
tableName.equalsIgnoreCase(each.getName()))
+ encryptRuleConfig.getTables().stream().filter(each ->
tableName.equalsIgnoreCase(each.getName()))
.findAny().ifPresent(each ->
result.putAll(buildEncryptColumnRuleConfigurationMap(each)));
} else {
- encryptRuleConfiguration.getTables().forEach(each ->
result.putAll(buildEncryptColumnRuleConfigurationMap(each)));
+ encryptRuleConfig.getTables().forEach(each ->
result.putAll(buildEncryptColumnRuleConfigurationMap(each)));
}
return result;
}
- private Map<String, EncryptColumnRuleConfiguration>
buildEncryptColumnRuleConfigurationMap(final EncryptTableRuleConfiguration
encryptTableRuleConfiguration) {
- return
encryptTableRuleConfiguration.getColumns().stream().collect(Collectors.toMap(each
-> Joiner.on(".")
- .join(encryptTableRuleConfiguration.getName(),
each.getLogicColumn()), each -> each));
+ private Map<String, EncryptColumnRuleConfiguration>
buildEncryptColumnRuleConfigurationMap(final EncryptTableRuleConfiguration
encryptTableRuleConfig) {
+ return
encryptTableRuleConfig.getColumns().stream().collect(Collectors.toMap(each ->
Joiner.on(".").join(encryptTableRuleConfig.getName(), each.getLogicColumn()),
each -> each));
}
- 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, "", "logicColumn",
"logicColumn", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schemaName, "", "cipherColumn",
"cipherColumn", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schemaName, "", "plainColumn",
"plainColumn", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schemaName, "", "encryptorType",
"encryptorType", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schemaName, "", "encryptorProps",
"encryptorProps", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- return result;
+ @Override
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("table", "logicColumn", "cipherColumn",
"plainColumn", "encryptorType", "encryptorProps");
}
@Override
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ReadwriteSplittingRulesQueryBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ReadwriteSplittingRuleQueryResultSet.java
similarity index 55%
rename from
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ReadwriteSplittingRulesQueryBackendHandler.java
rename to
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ReadwriteSplittingRuleQueryResultSet.java
index 09ab05d..0eb1710 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ReadwriteSplittingRulesQueryBackendHandler.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ReadwriteSplittingRuleQueryResultSet.java
@@ -21,61 +21,39 @@ import com.google.common.base.Joiner;
import com.google.common.collect.Maps;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.properties.PropertiesConverter;
-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.proxy.backend.text.distsql.rql.RuleQueryResultSet;
import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
-import
org.apache.shardingsphere.readwritesplitting.distsql.parser.statement.ShowReadwriteSplittingRulesStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import java.sql.Types;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
import java.util.Map;
import java.util.Optional;
/**
- * Backend handler for show readwrite splitting rules.
+ * Result set for show readwrite splitting rule.
*/
-public final class ReadwriteSplittingRulesQueryBackendHandler extends
SchemaRequiredBackendHandler<ShowReadwriteSplittingRulesStatement> {
+public final class ReadwriteSplittingRuleQueryResultSet implements
RuleQueryResultSet {
private Iterator<ReadwriteSplittingDataSourceRuleConfiguration> data;
-
- private Map<String, ShardingSphereAlgorithmConfiguration> loadBalancers;
- public ReadwriteSplittingRulesQueryBackendHandler(final
ShowReadwriteSplittingRulesStatement sqlStatement, final BackendConnection
backendConnection) {
- super(sqlStatement, backendConnection);
- }
+ private Map<String, ShardingSphereAlgorithmConfiguration> loadBalancers;
@Override
- protected ResponseHeader execute(final String schemaName, final
ShowReadwriteSplittingRulesStatement sqlStatement) {
- loadRuleConfiguration(schemaName);
- return new QueryResponseHeader(getQueryHeader(schemaName));
- }
-
- private void loadRuleConfiguration(final String schemaName) {
+ public void init(final String schemaName, final SQLStatement sqlStatement)
{
Optional<ReadwriteSplittingRuleConfiguration> ruleConfig =
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations()
.stream().filter(each -> each instanceof
ReadwriteSplittingRuleConfiguration).map(each ->
(ReadwriteSplittingRuleConfiguration) each).findAny();
data = ruleConfig.map(optional ->
optional.getDataSources().iterator()).orElse(Collections.emptyIterator());
loadBalancers =
ruleConfig.map(ReadwriteSplittingRuleConfiguration::getLoadBalancers).orElse(Maps.newHashMap());
}
- private List<QueryHeader> getQueryHeader(final String schemaName) {
- List<QueryHeader> result = new LinkedList<>();
- result.add(new QueryHeader(schemaName, "", "name", "name", Types.CHAR,
"CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schemaName, "", "autoAwareDataSourceName",
"autoAwareDataSourceName", Types.CHAR, "CHAR", 255, 0, false, false, false,
false));
- result.add(new QueryHeader(schemaName, "", "writeDataSourceName",
"writeDataSourceName", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schemaName, "", "readDataSourceNames",
"readDataSourceNames", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schemaName, "", "loadBalancerType",
"loadBalancerType", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- result.add(new QueryHeader(schemaName, "", "loadBalancerProps",
"loadBalancerProps", Types.CHAR, "CHAR", 255, 0, false, false, false, false));
- return result;
+ @Override
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("name", "autoAwareDataSourceName",
"writeDataSourceName", "readDataSourceNames", "loadBalancerType",
"loadBalancerProps");
}
@Override
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBindingTableRuleQueryResultSet.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBindingTableRuleQueryResultSet.java
new file mode 100644
index 0000000..2cce1df
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBindingTableRuleQueryResultSet.java
@@ -0,0 +1,58 @@
+/*
+ * 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.proxy.backend.context.ProxyContext;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.RuleQueryResultSet;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Optional;
+
+/**
+ * Result set for show sharding binding table rules.
+ */
+public final class ShardingBindingTableRuleQueryResultSet implements
RuleQueryResultSet {
+
+ private Iterator<String> data;
+
+ @Override
+ public void init(final String schemaName, final SQLStatement sqlStatement)
{
+ Optional<ShardingRuleConfiguration> shardingRuleConfig =
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations()
+ .stream().filter(each -> each instanceof
ShardingRuleConfiguration).map(each -> (ShardingRuleConfiguration)
each).findFirst();
+ data = shardingRuleConfig.map(shardingRuleConfiguration ->
shardingRuleConfiguration.getBindingTableGroups().iterator()).orElse(Collections.emptyIterator());
+ }
+
+ @Override
+ public Collection<String> getColumnNames() {
+ return Collections.singleton("shardingBindingTables");
+ }
+
+ @Override
+ public boolean next() {
+ return data.hasNext();
+ }
+
+ @Override
+ public Collection<Object> getRowData() {
+ return Collections.singleton(data.next());
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBindingTableRulesQueryBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBindingTableRulesQueryBackendHandler.java
deleted file mode 100644
index 7aa6a47..0000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBindingTableRulesQueryBackendHandler.java
+++ /dev/null
@@ -1,83 +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.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.distsql.parser.statement.ShowShardingBindingTableRulesStatement;
-
-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.Optional;
-
-/**
- * Backend handler for show sharding binding table rules.
- */
-public final class ShardingBindingTableRulesQueryBackendHandler extends
SchemaRequiredBackendHandler<ShowShardingBindingTableRulesStatement> {
-
- private Iterator<String> data;
-
- private final String schema;
-
- public ShardingBindingTableRulesQueryBackendHandler(final
ShowShardingBindingTableRulesStatement sqlStatement, final BackendConnection
backendConnection) {
- super(sqlStatement, backendConnection);
- schema = sqlStatement.getSchema().isPresent() ?
sqlStatement.getSchema().get().getIdentifier().getValue() :
backendConnection.getSchemaName();
- }
-
- @Override
- protected ResponseHeader execute(final String schemaName, final
ShowShardingBindingTableRulesStatement 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, "", "shardingBindingTables",
"shardingBindingTables", Types.CHAR, "CHAR", 255, 0, false, false, false,
false));
- return result;
- }
-
- private Iterator<String> loadRuleConfiguration() {
- Collection<String> result = new LinkedList<>();
- Optional<ShardingRuleConfiguration> shardingRuleConfiguration =
ProxyContext.getInstance().getMetaData(schema).getRuleMetaData().getConfigurations()
- .stream().filter(each -> each instanceof
ShardingRuleConfiguration).map(each -> (ShardingRuleConfiguration)
each).findFirst();
- if (shardingRuleConfiguration.isPresent()) {
- result = shardingRuleConfiguration.get().getBindingTableGroups();
- }
- return result.iterator();
- }
-
- @Override
- public boolean next() {
- return data.hasNext();
- }
-
- @Override
- public Collection<Object> getRowData() {
- return Collections.singleton(data.next());
- }
-}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBroadcastTableRuleQueryResultSet.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBroadcastTableRuleQueryResultSet.java
new file mode 100644
index 0000000..3d4e84c
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBroadcastTableRuleQueryResultSet.java
@@ -0,0 +1,58 @@
+/*
+ * 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.proxy.backend.context.ProxyContext;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.RuleQueryResultSet;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Optional;
+
+/**
+ * Result set for show sharding broadcast table rules.
+ */
+public final class ShardingBroadcastTableRuleQueryResultSet implements
RuleQueryResultSet {
+
+ private Iterator<String> data;
+
+ @Override
+ public void init(final String schemaName, final SQLStatement sqlStatement)
{
+ Optional<ShardingRuleConfiguration> shardingRuleConfig =
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations()
+ .stream().filter(each -> each instanceof
ShardingRuleConfiguration).map(each -> (ShardingRuleConfiguration)
each).findFirst();
+ data = shardingRuleConfig.map(optional ->
optional.getBroadcastTables().iterator()).orElse(Collections.emptyIterator());
+ }
+
+ @Override
+ public Collection<String> getColumnNames() {
+ return Collections.singleton("shardingBroadcastTables");
+ }
+
+ @Override
+ public boolean next() {
+ return data.hasNext();
+ }
+
+ @Override
+ public Collection<Object> getRowData() {
+ return Collections.singleton(data.next());
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBroadcastTableRulesQueryBackendHandler.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBroadcastTableRulesQueryBackendHandler.java
deleted file mode 100644
index f0555ad..0000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBroadcastTableRulesQueryBackendHandler.java
+++ /dev/null
@@ -1,79 +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.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.distsql.parser.statement.ShowShardingBroadcastTableRulesStatement;
-
-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.Optional;
-
-/**
- * Backend handler for show sharding broadcast table rules.
- */
-public final class ShardingBroadcastTableRulesQueryBackendHandler extends
SchemaRequiredBackendHandler<ShowShardingBroadcastTableRulesStatement> {
-
- private Iterator<String> data;
-
- private final String schema;
-
- public ShardingBroadcastTableRulesQueryBackendHandler(final
ShowShardingBroadcastTableRulesStatement sqlStatement, final BackendConnection
backendConnection) {
- super(sqlStatement, backendConnection);
- schema = sqlStatement.getSchema().isPresent() ?
sqlStatement.getSchema().get().getIdentifier().getValue() :
backendConnection.getSchemaName();
- }
-
- @Override
- protected ResponseHeader execute(final String schemaName, final
ShowShardingBroadcastTableRulesStatement sqlStatement) {
- List<QueryHeader> queryHeader = getQueryHeader();
- data = loadBroadcastTableRules();
- return new QueryResponseHeader(queryHeader);
- }
-
- private List<QueryHeader> getQueryHeader() {
- List<QueryHeader> result = new LinkedList<>();
- result.add(new QueryHeader(schema, "", "shardingBroadcastTables",
"shardingBroadcastTables", Types.CHAR, "CHAR", 255, 0, false, false, false,
false));
- return result;
- }
-
- private Iterator<String> loadBroadcastTableRules() {
- Optional<ShardingRuleConfiguration> shardingRuleConfig =
ProxyContext.getInstance().getMetaData(schema).getRuleMetaData().getConfigurations()
- .stream().filter(each -> each instanceof
ShardingRuleConfiguration).map(each -> (ShardingRuleConfiguration)
each).findFirst();
- return shardingRuleConfig.map(optional ->
optional.getBroadcastTables().iterator()).orElse(Collections.emptyIterator());
- }
-
- @Override
- public boolean next() {
- return data.hasNext();
- }
-
- @Override
- public Collection<Object> getRowData() {
- return Collections.singleton(data.next());
- }
-}
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/ShardingTableRuleQueryResultSet.java
similarity index 71%
rename from
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingTableRulesQueryBackendHandler.java
rename to
shardingsphere-proxy/shardingsphere-proxy-backend/src/main/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingTableRuleQueryResultSet.java
index 3ce2023..c695499 100644
---
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/ShardingTableRuleQueryResultSet.java
@@ -19,12 +19,8 @@ package
org.apache.shardingsphere.proxy.backend.text.distsql.rql.impl;
import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
import org.apache.shardingsphere.infra.properties.PropertiesConverter;
-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.proxy.backend.text.distsql.rql.RuleQueryResultSet;
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;
@@ -34,39 +30,31 @@ import
org.apache.shardingsphere.sharding.api.config.strategy.sharding.NoneShard
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.distsql.parser.statement.ShowShardingTableRulesStatement;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import java.sql.Types;
+import java.util.Arrays;
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.stream.Collectors;
/**
- * Backend handler for show sharding table rules.
+ * Result set for show sharding table rules.
*/
-public final class ShardingTableRulesQueryBackendHandler extends
SchemaRequiredBackendHandler<ShowShardingTableRulesStatement> {
+public final class ShardingTableRuleQueryResultSet implements
RuleQueryResultSet {
private Iterator<ShardingTableRuleConfiguration> tables;
private Iterator<ShardingAutoTableRuleConfiguration> autoTables;
- private ShardingRuleConfiguration shardingRuleConfiguration;
-
- public ShardingTableRulesQueryBackendHandler(final
ShowShardingTableRulesStatement sqlStatement, final BackendConnection
backendConnection) {
- super(sqlStatement, backendConnection);
- }
+ private ShardingRuleConfiguration shardingRuleConfig;
@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) {
+ public void init(final String schemaName, final SQLStatement sqlStatement)
{
+ String tableName = ((ShowShardingTableRulesStatement)
sqlStatement).getTableName();
Optional<ShardingRuleConfiguration> ruleConfig =
ProxyContext.getInstance().getMetaData(schemaName).getRuleMetaData().getConfigurations()
.stream().filter(each -> each instanceof
ShardingRuleConfiguration).map(each -> (ShardingRuleConfiguration)
each).findAny();
if (Objects.isNull(tableName)) {
@@ -78,26 +66,13 @@ public final class ShardingTableRulesQueryBackendHandler
extends SchemaRequiredB
autoTables = ruleConfig.map(optional
-> optional.getAutoTables().stream().filter(each ->
tableName.equalsIgnoreCase(each.getLogicTable())).collect(Collectors.toList()).iterator()).orElse(Collections.emptyIterator());
}
- shardingRuleConfiguration = ruleConfig.orElse(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;
+ shardingRuleConfig = ruleConfig.orElse(null);
+ }
+
+ @Override
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("table", "actualDataNodes", "actualDataSources",
"databaseStrategyType", "databaseShardingColumn",
"databaseShardingAlgorithmType", "databaseShardingAlgorithmProps",
+ "tableStrategyType", "tableShardingColumn",
"tableShardingAlgorithmType", "tableShardingAlgorithmProps",
"keyGenerateColumn", "keyGeneratorType", "keyGeneratorProps");
}
@Override
@@ -181,11 +156,11 @@ public final class ShardingTableRulesQueryBackendHandler
extends SchemaRequiredB
private Optional<ShardingStrategyConfiguration>
getDatabaseShardingStrategy(final ShardingTableRuleConfiguration
shardingTableRuleConfig) {
return null == shardingTableRuleConfig.getDatabaseShardingStrategy()
- ?
Optional.ofNullable(shardingRuleConfiguration.getDefaultDatabaseShardingStrategy())
: Optional.ofNullable(shardingTableRuleConfig.getDatabaseShardingStrategy());
+ ?
Optional.ofNullable(shardingRuleConfig.getDefaultDatabaseShardingStrategy()) :
Optional.ofNullable(shardingTableRuleConfig.getDatabaseShardingStrategy());
}
private ShardingSphereAlgorithmConfiguration
getAlgorithmConfiguration(final String algorithmName) {
- return
shardingRuleConfiguration.getShardingAlgorithms().get(algorithmName);
+ return shardingRuleConfig.getShardingAlgorithms().get(algorithmName);
}
private String getTableStrategyType(final ShardingStrategyConfiguration
shardingStrategyConfig) {
@@ -198,7 +173,7 @@ public final class ShardingTableRulesQueryBackendHandler
extends SchemaRequiredB
}
private Optional<ShardingStrategyConfiguration>
getTableShardingStrategy(final ShardingStrategyConfiguration
shardingStrategyConfig) {
- return null == shardingStrategyConfig ?
Optional.ofNullable(shardingRuleConfiguration.getDefaultTableShardingStrategy())
: Optional.of(shardingStrategyConfig);
+ return null == shardingStrategyConfig ?
Optional.ofNullable(shardingRuleConfig.getDefaultTableShardingStrategy()) :
Optional.of(shardingStrategyConfig);
}
private String getTableShardingColumn(final ShardingStrategyConfiguration
shardingStrategyConfig) {
@@ -212,15 +187,15 @@ public final class ShardingTableRulesQueryBackendHandler
extends SchemaRequiredB
private String getKeyGeneratorType(final KeyGenerateStrategyConfiguration
keyGenerateStrategyConfiguration) {
Optional<KeyGenerateStrategyConfiguration> keyGenerateStrategyConfig =
getKeyGenerateStrategyConfiguration(keyGenerateStrategyConfiguration);
- return keyGenerateStrategyConfig.isPresent() ?
shardingRuleConfiguration.getKeyGenerators().get(keyGenerateStrategyConfig.get().getKeyGeneratorName()).getType()
: "";
+ return keyGenerateStrategyConfig.isPresent() ?
shardingRuleConfig.getKeyGenerators().get(keyGenerateStrategyConfig.get().getKeyGeneratorName()).getType()
: "";
}
private String getKeyGeneratorProps(final KeyGenerateStrategyConfiguration
keyGenerateStrategyConfig) {
return
getKeyGenerateStrategyConfiguration(keyGenerateStrategyConfig).map(
- optional ->
PropertiesConverter.convert(shardingRuleConfiguration.getKeyGenerators().get(optional.getKeyGeneratorName()).getProps())).orElse("");
+ optional ->
PropertiesConverter.convert(shardingRuleConfig.getKeyGenerators().get(optional.getKeyGeneratorName()).getProps())).orElse("");
}
private Optional<KeyGenerateStrategyConfiguration>
getKeyGenerateStrategyConfiguration(final KeyGenerateStrategyConfiguration
keyGenerateStrategyConfig) {
- return null == keyGenerateStrategyConfig ?
Optional.ofNullable(shardingRuleConfiguration.getDefaultKeyGenerateStrategy())
: Optional.of(keyGenerateStrategyConfig);
+ return null == keyGenerateStrategyConfig ?
Optional.ofNullable(shardingRuleConfig.getDefaultKeyGenerateStrategy()) :
Optional.of(keyGenerateStrategyConfig);
}
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
index 37dcaf7..aa9edd0 100644
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/RDLBackendHandlerFactoryTest.java
@@ -269,9 +269,7 @@ public final class RDLBackendHandlerFactoryTest {
assertThat(ex.getMessage(), is("No Registry center to execute
`ShowResourcesStatement` SQL"));
}
setGovernanceMetaDataContexts(true);
- Optional<TextProtocolBackendHandler> rdlBackendHandler =
RQLBackendHandlerFactory.newInstance(mock(ShowResourcesStatement.class),
connection);
- assertTrue(rdlBackendHandler.isPresent());
- ResponseHeader response = rdlBackendHandler.get().execute();
+ ResponseHeader response =
RQLBackendHandlerFactory.newInstance(mock(ShowResourcesStatement.class),
connection).execute();
assertThat(response, instanceOf(QueryResponseHeader.class));
}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleQueryBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleQueryBackendHandlerTest.java
new file mode 100644
index 0000000..6dccaeb
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/RuleQueryBackendHandlerTest.java
@@ -0,0 +1,77 @@
+/*
+ * 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;
+
+import org.apache.shardingsphere.distsql.parser.statement.rql.RQLStatement;
+import
org.apache.shardingsphere.proxy.backend.communication.jdbc.connection.BackendConnection;
+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.junit.Test;
+
+import java.sql.Types;
+import java.util.Arrays;
+import java.util.Collection;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public final class RuleQueryBackendHandlerTest {
+
+ @Test
+ public void assertExecute() {
+ RuleQueryResultSet resultSet = mock(RuleQueryResultSet.class);
+ when(resultSet.getColumnNames()).thenReturn(Arrays.asList("foo",
"bar"));
+ RuleQueryBackendHandler handler = new
RuleQueryBackendHandler(mock(RQLStatement.class),
mock(BackendConnection.class), resultSet);
+ ResponseHeader responseHeader = handler.execute("test",
mock(RQLStatement.class));
+ assertThat(((QueryResponseHeader)
responseHeader).getQueryHeaders().size(), is(2));
+ assertQueryHeader(((QueryResponseHeader)
responseHeader).getQueryHeaders().get(0), "foo");
+ assertQueryHeader(((QueryResponseHeader)
responseHeader).getQueryHeaders().get(1), "bar");
+ }
+
+ private void assertQueryHeader(final QueryHeader queryHeader, final String
expectedColumnLabel) {
+ assertThat(queryHeader.getSchema(), is("test"));
+ assertThat(queryHeader.getTable(), is(""));
+ assertThat(queryHeader.getColumnLabel(), is(expectedColumnLabel));
+ assertThat(queryHeader.getColumnName(), is(expectedColumnLabel));
+ assertThat(queryHeader.getColumnType(), is(Types.CHAR));
+ assertThat(queryHeader.getColumnTypeName(), is("CHAR"));
+ assertThat(queryHeader.getColumnLength(), is(255));
+ assertThat(queryHeader.getDecimals(), is(0));
+ assertFalse(queryHeader.isSigned());
+ assertFalse(queryHeader.isPrimaryKey());
+ assertFalse(queryHeader.isNotNull());
+ assertFalse(queryHeader.isAutoIncrement());
+ }
+
+ @Test
+ public void assertGetRowData() {
+ RuleQueryResultSet resultSet = mock(RuleQueryResultSet.class);
+ when(resultSet.getRowData()).thenReturn(Arrays.asList("foo_value",
"bar_value"));
+ RuleQueryBackendHandler handler = new
RuleQueryBackendHandler(mock(RQLStatement.class),
mock(BackendConnection.class), resultSet);
+ handler.execute("test", mock(RQLStatement.class));
+ Collection<Object> rowData = handler.getRowData();
+ assertThat(rowData.size(), is(2));
+ assertTrue(rowData.contains("foo_value"));
+ assertTrue(rowData.contains("bar_value"));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/BaseRuleQueryResultSet.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/BaseRuleQueryResultSet.java
new file mode 100644
index 0000000..aa40146
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/BaseRuleQueryResultSet.java
@@ -0,0 +1,49 @@
+/*
+ * 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.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.proxy.backend.context.ProxyContext;
+import org.apache.shardingsphere.transaction.context.TransactionContexts;
+import org.junit.Before;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+public abstract class BaseRuleQueryResultSet {
+
+ @Before
+ public final void setUp() {
+ MetaDataContexts metaDataContexts = mock(MetaDataContexts.class);
+
when(metaDataContexts.getAllSchemaNames()).thenReturn(Collections.singleton("test"));
+ ShardingSphereRuleMetaData ruleMetaData =
mock(ShardingSphereRuleMetaData.class);
+
when(ruleMetaData.getConfigurations()).thenReturn(buildRuleConfigurations());
+ ShardingSphereMetaData shardingSphereMetaData =
mock(ShardingSphereMetaData.class);
+
when(shardingSphereMetaData.getRuleMetaData()).thenReturn(ruleMetaData);
+
when(metaDataContexts.getMetaData("test")).thenReturn(shardingSphereMetaData);
+ ProxyContext.getInstance().init(metaDataContexts,
mock(TransactionContexts.class));
+ }
+
+ protected abstract Collection<RuleConfiguration> buildRuleConfigurations();
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DatabaseDiscoveryRuleQueryResultSetTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DatabaseDiscoveryRuleQueryResultSetTest.java
new file mode 100644
index 0000000..ca5a432
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DatabaseDiscoveryRuleQueryResultSetTest.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.distsql.rql.impl;
+
+import
org.apache.shardingsphere.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
+import
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
+import
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.ShowDatabaseDiscoveryRulesStatement;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.RuleQueryResultSet;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+public final class DatabaseDiscoveryRuleQueryResultSetTest extends
BaseRuleQueryResultSet {
+
+ @Override
+ protected Collection<RuleConfiguration> buildRuleConfigurations() {
+ DatabaseDiscoveryDataSourceRuleConfiguration
databaseDiscoveryDataSourceRuleConfig = new
DatabaseDiscoveryDataSourceRuleConfiguration("ms_group", Arrays.asList("ds_0",
"ds_1"), "test");
+ ShardingSphereAlgorithmConfiguration shardingSphereAlgorithmConfig =
new ShardingSphereAlgorithmConfiguration("MGR", new Properties());
+ Map<String, ShardingSphereAlgorithmConfiguration> discoverTypes = new
HashMap<>(1, 1);
+ discoverTypes.put("test", shardingSphereAlgorithmConfig);
+ return Collections.singleton(new
DatabaseDiscoveryRuleConfiguration(Collections.singleton(databaseDiscoveryDataSourceRuleConfig),
discoverTypes));
+ }
+
+ @Test
+ public void assertGetRowData() {
+ RuleQueryResultSet resultSet = new
DatabaseDiscoveryRuleQueryResultSet();
+ resultSet.init("test",
mock(ShowDatabaseDiscoveryRulesStatement.class));
+ Collection<Object> actual = resultSet.getRowData();
+ assertThat(actual.size(), is(4));
+ assertTrue(actual.contains("ms_group"));
+ assertTrue(actual.contains("ds_0,ds_1"));
+ assertTrue(actual.contains("MGR"));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DatabaseDiscoveryRulesQueryBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DatabaseDiscoveryRulesQueryBackendHandlerTest.java
deleted file mode 100644
index 3ff974d..0000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/DatabaseDiscoveryRulesQueryBackendHandlerTest.java
+++ /dev/null
@@ -1,105 +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.dbdiscovery.api.config.DatabaseDiscoveryRuleConfiguration;
-import
org.apache.shardingsphere.dbdiscovery.api.config.rule.DatabaseDiscoveryDataSourceRuleConfiguration;
-import
org.apache.shardingsphere.dbdiscovery.distsql.parser.statement.ShowDatabaseDiscoveryRulesStatement;
-import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
-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.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.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-
-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 DatabaseDiscoveryRulesQueryBackendHandlerTest {
-
- @Mock
- private BackendConnection backendConnection;
-
- @Mock
- private ShowDatabaseDiscoveryRulesStatement sqlStatement;
-
- @Mock
- private MetaDataContexts metaDataContexts;
-
- @Mock
- private TransactionContexts transactionContexts;
-
- @Mock
- private ShardingSphereMetaData shardingSphereMetaData;
-
- @Mock
- private ShardingSphereRuleMetaData ruleMetaData;
-
- private DatabaseDiscoveryRulesQueryBackendHandler handler;
-
- @Before
- public void setUp() {
- ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
- handler = new DatabaseDiscoveryRulesQueryBackendHandler(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(Collections.singleton(buildDatabaseDiscoveryRuleConfiguration()));
- }
-
- @Test
- public void assertExecute() {
- ResponseHeader responseHeader = handler.execute("test", sqlStatement);
- assertNotNull(responseHeader);
- assertTrue(responseHeader instanceof QueryResponseHeader);
- Collection<Object> rowData = handler.getRowData();
- assertThat(rowData.size(), is(4));
- assertTrue(rowData.contains("ms_group"));
- assertTrue(rowData.contains("ds_0,ds_1"));
- assertTrue(rowData.contains("MGR"));
- }
-
- private DatabaseDiscoveryRuleConfiguration
buildDatabaseDiscoveryRuleConfiguration() {
- DatabaseDiscoveryDataSourceRuleConfiguration
databaseDiscoveryDataSourceRuleConfig =
- new DatabaseDiscoveryDataSourceRuleConfiguration("ms_group",
Arrays.asList("ds_0", "ds_1"), "test");
- ShardingSphereAlgorithmConfiguration shardingSphereAlgorithmConfig =
new ShardingSphereAlgorithmConfiguration("MGR", new Properties());
- Map<String, ShardingSphereAlgorithmConfiguration> discoverTypes = new
HashMap<>();
- discoverTypes.put("test", shardingSphereAlgorithmConfig);
- return new
DatabaseDiscoveryRuleConfiguration(Collections.singleton(databaseDiscoveryDataSourceRuleConfig),
discoverTypes);
- }
-}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/EncryptRuleQueryResultSetTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/EncryptRuleQueryResultSetTest.java
new file mode 100644
index 0000000..52e2222
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/EncryptRuleQueryResultSetTest.java
@@ -0,0 +1,64 @@
+/*
+ * 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.encrypt.api.config.EncryptRuleConfiguration;
+import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
+import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+import
org.apache.shardingsphere.encrypt.distsql.parser.statement.ShowEncryptRulesStatement;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.RuleQueryResultSet;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+public final class EncryptRuleQueryResultSetTest extends
BaseRuleQueryResultSet {
+
+ @Override
+ protected Collection<RuleConfiguration> buildRuleConfigurations() {
+ EncryptColumnRuleConfiguration encryptColumnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", "user_cipher", null, "user_plain",
"test");
+ EncryptTableRuleConfiguration encryptTableRuleConfig = new
EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(encryptColumnRuleConfig));
+ ShardingSphereAlgorithmConfiguration shardingSphereAlgorithmConfig =
new ShardingSphereAlgorithmConfiguration("md5", new Properties());
+ Map<String, ShardingSphereAlgorithmConfiguration> encryptors = new
HashMap<>();
+ encryptors.put("test", shardingSphereAlgorithmConfig);
+ return Collections.singleton(new
EncryptRuleConfiguration(Collections.singleton(encryptTableRuleConfig),
encryptors));
+ }
+
+ @Test
+ public void assertGetRowData() {
+ RuleQueryResultSet resultSet = new EncryptRuleQueryResultSet();
+ resultSet.init("test", mock(ShowEncryptRulesStatement.class));
+ Collection<Object> actual = resultSet.getRowData();
+ assertThat(actual.size(), is(6));
+ assertTrue(actual.contains("t_encrypt"));
+ assertTrue(actual.contains("user_id"));
+ assertTrue(actual.contains("user_cipher"));
+ assertTrue(actual.contains("user_plain"));
+ assertTrue(actual.contains("md5"));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/EncryptRulesQueryBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/EncryptRulesQueryBackendHandlerTest.java
deleted file mode 100644
index 06f0e0d..0000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/EncryptRulesQueryBackendHandlerTest.java
+++ /dev/null
@@ -1,107 +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.encrypt.distsql.parser.statement.ShowEncryptRulesStatement;
-import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
-import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
-import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
-import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
-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.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.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-
-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 EncryptRulesQueryBackendHandlerTest {
-
- @Mock
- private BackendConnection backendConnection;
-
- @Mock
- private ShowEncryptRulesStatement sqlStatement;
-
- @Mock
- private MetaDataContexts metaDataContexts;
-
- @Mock
- private TransactionContexts transactionContexts;
-
- @Mock
- private ShardingSphereMetaData shardingSphereMetaData;
-
- @Mock
- private ShardingSphereRuleMetaData ruleMetaData;
-
- private EncryptRulesQueryBackendHandler handler;
-
- @Before
- public void setUp() {
- ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
- handler = new EncryptRulesQueryBackendHandler(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(Collections.singleton(buildEncryptRuleConfiguration()));
- }
-
- @Test
- public void assertExecute() {
- ResponseHeader responseHeader = handler.execute("test", sqlStatement);
- assertNotNull(responseHeader);
- assertTrue(responseHeader instanceof QueryResponseHeader);
- Collection<Object> rowData = handler.getRowData();
- assertThat(rowData.size(), is(6));
- assertTrue(rowData.contains("t_encrypt"));
- assertTrue(rowData.contains("user_id"));
- assertTrue(rowData.contains("user_cipher"));
- assertTrue(rowData.contains("user_plain"));
- assertTrue(rowData.contains("md5"));
- }
-
- private EncryptRuleConfiguration buildEncryptRuleConfiguration() {
- EncryptColumnRuleConfiguration encryptColumnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", "user_cipher", null, "user_plain",
"test");
- EncryptTableRuleConfiguration encryptTableRuleConfig = new
EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(encryptColumnRuleConfig));
- ShardingSphereAlgorithmConfiguration shardingSphereAlgorithmConfig =
new ShardingSphereAlgorithmConfiguration("md5", new Properties());
- Map<String, ShardingSphereAlgorithmConfiguration> encryptors = new
HashMap<>();
- encryptors.put("test", shardingSphereAlgorithmConfig);
- return new
EncryptRuleConfiguration(Collections.singleton(encryptTableRuleConfig),
encryptors);
- }
-}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ReadwriteSplittingRuleQueryResultSetTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ReadwriteSplittingRuleQueryResultSetTest.java
new file mode 100644
index 0000000..737ab27
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ReadwriteSplittingRuleQueryResultSetTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.infra.config.RuleConfiguration;
+import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
+import
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
+import
org.apache.shardingsphere.readwritesplitting.distsql.parser.statement.ShowReadwriteSplittingRulesStatement;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+public final class ReadwriteSplittingRuleQueryResultSetTest extends
BaseRuleQueryResultSet {
+
+ @Override
+ protected Collection<RuleConfiguration> buildRuleConfigurations() {
+ ReadwriteSplittingDataSourceRuleConfiguration dataSourceRuleConfig =
+ new ReadwriteSplittingDataSourceRuleConfiguration("pr_ds",
"ms_group", "ds_primary", Arrays.asList("ds_slave_0", "ds_slave_1"), "test");
+ Properties props = new Properties();
+ props.setProperty("read_weight", "2:1");
+ ShardingSphereAlgorithmConfiguration
shardingSphereAlgorithmConfiguration = new
ShardingSphereAlgorithmConfiguration("random", props);
+ Map<String, ShardingSphereAlgorithmConfiguration> loadBalancers = new
HashMap<>();
+ loadBalancers.put("test", shardingSphereAlgorithmConfiguration);
+ return Collections.singleton(new
ReadwriteSplittingRuleConfiguration(Collections.singleton(dataSourceRuleConfig),
loadBalancers));
+ }
+
+ @Test
+ public void assertGetRowData() {
+ ReadwriteSplittingRuleQueryResultSet resultSet = new
ReadwriteSplittingRuleQueryResultSet();
+ resultSet.init("test",
mock(ShowReadwriteSplittingRulesStatement.class));
+ Collection<Object> actual = resultSet.getRowData();
+ assertThat(actual.size(), is(6));
+ assertTrue(actual.contains("pr_ds"));
+ assertTrue(actual.contains("ms_group"));
+ assertTrue(actual.contains("ds_primary"));
+ assertTrue(actual.contains("ds_slave_0,ds_slave_1"));
+ assertTrue(actual.contains("random"));
+ assertTrue(actual.contains("read_weight=2:1"));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ReadwriteSplittingRulesQueryBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ReadwriteSplittingRulesQueryBackendHandlerTest.java
deleted file mode 100644
index 7fc5784..0000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ReadwriteSplittingRulesQueryBackendHandlerTest.java
+++ /dev/null
@@ -1,115 +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.readwritesplitting.distsql.parser.statement.ShowReadwriteSplittingRulesStatement;
-import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
-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.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.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
-import
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
-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.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Properties;
-
-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 ReadwriteSplittingRulesQueryBackendHandlerTest {
-
- @Mock
- private BackendConnection backendConnection;
-
- @Mock
- private ShowReadwriteSplittingRulesStatement sqlStatement;
-
- @Mock
- private MetaDataContexts metaDataContexts;
-
- @Mock
- private TransactionContexts transactionContexts;
-
- @Mock
- private ShardingSphereMetaData shardingSphereMetaData;
-
- @Mock
- private ShardingSphereRuleMetaData ruleMetaData;
-
- private ReadwriteSplittingRulesQueryBackendHandler handler;
-
- @Before
- public void setUp() {
- ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
- handler = new ReadwriteSplittingRulesQueryBackendHandler(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(Collections.singleton(buildReadwriteSplittingRuleConfiguration()));
- }
-
- @Test
- public void assertExecute() {
- ResponseHeader responseHeader = handler.execute("test", sqlStatement);
- assertNotNull(responseHeader);
- assertTrue(responseHeader instanceof QueryResponseHeader);
- Collection<Object> rowData = handler.getRowData();
- assertThat(rowData.size(), is(6));
- assertTrue(rowData.contains("pr_ds"));
- assertTrue(rowData.contains("ms_group"));
- assertTrue(rowData.contains("ds_primary"));
- assertTrue(rowData.contains("ds_slave_0,ds_slave_1"));
- assertTrue(rowData.contains("random"));
- assertTrue(rowData.contains("read_weight=2:1"));
- }
-
- private ReadwriteSplittingRuleConfiguration
buildReadwriteSplittingRuleConfiguration() {
- ReadwriteSplittingDataSourceRuleConfiguration dataSourceRuleConfig =
- new ReadwriteSplittingDataSourceRuleConfiguration("pr_ds",
"ms_group",
- "ds_primary", Arrays.asList("ds_slave_0",
"ds_slave_1"), "test");
- ShardingSphereAlgorithmConfiguration
shardingSphereAlgorithmConfiguration = new
ShardingSphereAlgorithmConfiguration("random", buildProperties());
- Map<String, ShardingSphereAlgorithmConfiguration> loadBalancers = new
HashMap<>();
- loadBalancers.put("test", shardingSphereAlgorithmConfiguration);
- return new
ReadwriteSplittingRuleConfiguration(Collections.singleton(dataSourceRuleConfig),
loadBalancers);
- }
-
- private Properties buildProperties() {
- Properties result = new Properties();
- result.setProperty("read_weight", "2:1");
- return result;
- }
-}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBindingTableRuleQueryResultSetTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBindingTableRuleQueryResultSetTest.java
new file mode 100644
index 0000000..99421f3
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBindingTableRuleQueryResultSetTest.java
@@ -0,0 +1,55 @@
+/*
+ * 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.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
+import
org.apache.shardingsphere.sharding.distsql.parser.statement.ShowShardingBindingTableRulesStatement;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.mock;
+
+public final class ShardingBindingTableRuleQueryResultSetTest extends
BaseRuleQueryResultSet {
+
+ @Override
+ protected Collection<RuleConfiguration> buildRuleConfigurations() {
+ ShardingRuleConfiguration result = new ShardingRuleConfiguration();
+ result.getTables().add(new ShardingTableRuleConfiguration("t_order"));
+ result.getTables().add(new
ShardingTableRuleConfiguration("t_order_item"));
+ result.getTables().add(new ShardingTableRuleConfiguration("t_1"));
+ result.getTables().add(new ShardingTableRuleConfiguration("t_2"));
+
result.getBindingTableGroups().addAll(Collections.singleton("t_order,t_order_item"));
+ return Collections.singleton(result);
+ }
+
+ @Test
+ public void assertGetRowData() {
+ ShardingBindingTableRuleQueryResultSet resultSet = new
ShardingBindingTableRuleQueryResultSet();
+ resultSet.init("test",
mock(ShowShardingBindingTableRulesStatement.class));
+ Collection<Object> actual = resultSet.getRowData();
+ assertThat(actual.size(), is(1));
+ assertTrue(actual.contains("t_order,t_order_item"));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBindingTableRulesQueryBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBindingTableRulesQueryBackendHandlerTest.java
deleted file mode 100644
index fe3c412..0000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBindingTableRulesQueryBackendHandlerTest.java
+++ /dev/null
@@ -1,99 +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.infra.context.metadata.MetaDataContexts;
-import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import
org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData;
-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.sharding.api.config.ShardingRuleConfiguration;
-import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
-import
org.apache.shardingsphere.sharding.distsql.parser.statement.ShowShardingBindingTableRulesStatement;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
-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.util.Collections;
-import java.util.Optional;
-
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
-public final class ShardingBindingTableRulesQueryBackendHandlerTest {
-
- @Mock
- private BackendConnection backendConnection;
-
- @Mock
- private ShowShardingBindingTableRulesStatement sqlStatement;
-
- @Mock
- private MetaDataContexts metaDataContexts;
-
- @Mock
- private TransactionContexts transactionContexts;
-
- @Mock
- private ShardingSphereMetaData shardingSphereMetaData;
-
- @Mock
- private ShardingSphereRuleMetaData ruleMetaData;
-
- private ShardingBindingTableRulesQueryBackendHandler handler;
-
- @Before
- public void setUp() {
- ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
- when(sqlStatement.getSchema()).thenReturn(Optional.of(new
SchemaSegment(0, 1, new IdentifierValue("test"))));
- handler = new
ShardingBindingTableRulesQueryBackendHandler(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(Collections.singleton(buildShardingRuleConfiguration()));
- }
-
- @Test
- public void assertExecute() {
- ResponseHeader responseHeader = handler.execute("test", sqlStatement);
- assertNotNull(responseHeader);
- assertTrue(responseHeader instanceof QueryResponseHeader);
- assertTrue(handler.next());
- assertTrue(handler.getRowData().contains("t_order,t_order_item"));
- }
-
- private ShardingRuleConfiguration buildShardingRuleConfiguration() {
- ShardingRuleConfiguration result = new ShardingRuleConfiguration();
- result.getTables().add(new ShardingTableRuleConfiguration("t_order"));
- result.getTables().add(new
ShardingTableRuleConfiguration("t_order_item"));
- result.getTables().add(new ShardingTableRuleConfiguration("t_1"));
- result.getTables().add(new ShardingTableRuleConfiguration("t_2"));
-
result.getBindingTableGroups().addAll(Collections.singleton("t_order,t_order_item"));
- return result;
- }
-}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBroadcastTableRuleQueryResultSetTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBroadcastTableRuleQueryResultSetTest.java
new file mode 100644
index 0000000..709742c
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBroadcastTableRuleQueryResultSetTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
+import
org.apache.shardingsphere.sharding.distsql.parser.statement.ShowShardingBroadcastTableRulesStatement;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public final class ShardingBroadcastTableRuleQueryResultSetTest extends
BaseRuleQueryResultSet {
+
+ @Override
+ protected Collection<RuleConfiguration> buildRuleConfigurations() {
+ ShardingRuleConfiguration result = new ShardingRuleConfiguration();
+ result.getBroadcastTables().addAll(Arrays.asList("t_order",
"t_order_item"));
+ return Collections.singleton(result);
+ }
+
+ @Test
+ public void assertGetRowData() {
+ ShardingBroadcastTableRuleQueryResultSet resultSet = new
ShardingBroadcastTableRuleQueryResultSet();
+ resultSet.init("test",
mock(ShowShardingBroadcastTableRulesStatement.class));
+ Collection<Object> actual = resultSet.getRowData();
+ assertThat(actual.size(), is(1));
+ assertThat(actual, is(Collections.singleton("t_order")));
+ }
+}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBroadcastTableRulesQueryBackendHandlerTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBroadcastTableRulesQueryBackendHandlerTest.java
deleted file mode 100644
index e3f059a..0000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingBroadcastTableRulesQueryBackendHandlerTest.java
+++ /dev/null
@@ -1,97 +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.sharding.distsql.parser.statement.ShowShardingBroadcastTableRulesStatement;
-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.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.sharding.api.config.ShardingRuleConfiguration;
-import
org.apache.shardingsphere.sql.parser.sql.common.segment.generic.SchemaSegment;
-import
org.apache.shardingsphere.sql.parser.sql.common.value.identifier.IdentifierValue;
-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.util.Arrays;
-import java.util.Collections;
-import java.util.Optional;
-
-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;
-import static org.hamcrest.CoreMatchers.is;
-
-@RunWith(MockitoJUnitRunner.class)
-public final class ShardingBroadcastTableRulesQueryBackendHandlerTest {
-
- @Mock
- private BackendConnection backendConnection;
-
- @Mock
- private ShowShardingBroadcastTableRulesStatement sqlStatement;
-
- @Mock
- private MetaDataContexts metaDataContexts;
-
- @Mock
- private TransactionContexts transactionContexts;
-
- @Mock
- private ShardingSphereMetaData shardingSphereMetaData;
-
- @Mock
- private ShardingSphereRuleMetaData ruleMetaData;
-
- private ShardingBroadcastTableRulesQueryBackendHandler handler;
-
- @Before
- public void setUp() {
- ProxyContext.getInstance().init(metaDataContexts, transactionContexts);
- when(sqlStatement.getSchema()).thenReturn(Optional.of(new
SchemaSegment(0, 1, new IdentifierValue("test"))));
- handler = new
ShardingBroadcastTableRulesQueryBackendHandler(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(Collections.singleton(buildShardingRuleConfiguration()));
- }
-
- @Test
- public void assertExecute() {
- ResponseHeader responseHeader = handler.execute("test", sqlStatement);
- assertNotNull(responseHeader);
- assertTrue(responseHeader instanceof QueryResponseHeader);
- assertTrue(handler.next());
- assertThat(handler.getRowData(), is(Collections.singleton("t_order")));
- }
-
- private ShardingRuleConfiguration buildShardingRuleConfiguration() {
- ShardingRuleConfiguration result = new ShardingRuleConfiguration();
- result.getBroadcastTables().addAll(Arrays.asList("t_order",
"t_order_item"));
- return result;
- }
-}
diff --git
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingTableRuleQueryResultSetTest.java
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingTableRuleQueryResultSetTest.java
new file mode 100644
index 0000000..60620ba
--- /dev/null
+++
b/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingTableRuleQueryResultSetTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
+import
org.apache.shardingsphere.proxy.backend.text.distsql.rql.RuleQueryResultSet;
+import
org.apache.shardingsphere.sharding.distsql.parser.statement.ShowShardingTableRulesStatement;
+import org.junit.Test;
+
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public final class ShardingTableRuleQueryResultSetTest extends
BaseRuleQueryResultSet {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ protected Collection<RuleConfiguration> buildRuleConfigurations() {
+ 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());
+ }
+
+ @Test
+ public void assertGetRowData() {
+ RuleQueryResultSet resultSet = new ShardingTableRuleQueryResultSet();
+ resultSet.init("test", mock(ShowShardingTableRulesStatement.class));
+ List<Object> actual = new ArrayList<>(resultSet.getRowData());
+ assertThat(actual.size(), is(14));
+ assertThat(actual.get(0), is("t_order"));
+ assertThat(actual.get(1), is("ds_${0..1}.t_order_${0..1}"));
+ assertThat(actual.get(2), is(""));
+ assertThat(actual.get(3), is("INLINE"));
+ assertThat(actual.get(4), is("user_id"));
+ assertThat(actual.get(5), is("INLINE"));
+ assertThat(actual.get(6), is("algorithm-expression=ds_${user_id %
2}"));
+ assertThat(actual.get(7), is("INLINE"));
+ assertThat(actual.get(8), is("order_id"));
+ assertThat(actual.get(9), is("INLINE"));
+ assertThat(actual.get(10), is("algorithm-expression=t_order_${order_id
% 2}"));
+ assertThat(actual.get(11), is("order_id"));
+ assertThat(actual.get(12), is("SNOWFLAKE"));
+ assertThat(actual.get(13), is("worker-id=123"));
+ }
+}
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
deleted file mode 100644
index d767fa4..0000000
---
a/shardingsphere-proxy/shardingsphere-proxy-backend/src/test/java/org/apache/shardingsphere/proxy/backend/text/distsql/rql/impl/ShardingTableRulesQueryBackendHandlerTest.java
+++ /dev/null
@@ -1,121 +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 lombok.SneakyThrows;
-import
org.apache.shardingsphere.sharding.distsql.parser.statement.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());
- }
-}