This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 0cd2efea4e8 Replace `ShardingTableReferenceRuleResultSet` with
`ShowShardingTableReferenceRuleExecutor` (#23876)
0cd2efea4e8 is described below
commit 0cd2efea4e886edc1fa3621ab78149de91879905
Author: Zichao <[email protected]>
AuthorDate: Wed Feb 1 01:12:40 2023 +1300
Replace `ShardingTableReferenceRuleResultSet` with
`ShowShardingTableReferenceRuleExecutor` (#23876)
---
...=> ShowShardingTableReferenceRuleExecutor.java} | 37 ++++++++--------------
...hardingsphere.distsql.handler.query.RQLExecutor | 1 +
...here.distsql.handler.resultset.DistSQLResultSet | 1 -
...howShardingTableReferenceRuleExecutorTest.java} | 30 ++++++++++++------
4 files changed, 36 insertions(+), 33 deletions(-)
diff --git
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShardingTableReferenceRuleResultSet.java
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableReferenceRuleExecutor.java
similarity index 65%
rename from
features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShardingTableReferenceRuleResultSet.java
rename to
features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableReferenceRuleExecutor.java
index 5526c521b21..cf05539e0d8 100644
---
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShardingTableReferenceRuleResultSet.java
+++
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableReferenceRuleExecutor.java
@@ -17,31 +17,36 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
-import
org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
+import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration;
import
org.apache.shardingsphere.sharding.distsql.parser.statement.ShowShardingTableReferenceRulesStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
-import java.util.Iterator;
+import java.util.LinkedList;
import java.util.Optional;
/**
- * Result set for show sharding table reference rules.
+ * Show sharding table reference rules executor.
*/
-public final class ShardingTableReferenceRuleResultSet implements
DatabaseDistSQLResultSet {
-
- private Iterator<ShardingTableReferenceRuleConfiguration> data =
Collections.emptyIterator();
+public final class ShowShardingTableReferenceRuleExecutor implements
RQLExecutor<ShowShardingTableReferenceRulesStatement> {
@Override
- public void init(final ShardingSphereDatabase database, final SQLStatement
sqlStatement) {
+ public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final ShowShardingTableReferenceRulesStatement
sqlStatement) {
Optional<ShardingRule> rule =
database.getRuleMetaData().findSingleRule(ShardingRule.class);
- rule.ifPresent(optional -> data =
buildData((ShardingRuleConfiguration) optional.getConfiguration()).iterator());
+ if (!rule.isPresent()) {
+ return Collections.emptyList();
+ }
+ Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+ for (final ShardingTableReferenceRuleConfiguration referenceRule :
((ShardingRuleConfiguration)
rule.get().getConfiguration()).getBindingTableGroups()) {
+ result.add(new LocalDataQueryResultRow(referenceRule.getName(),
referenceRule.getReference()));
+ }
+ return result;
}
private Collection<ShardingTableReferenceRuleConfiguration>
buildData(final ShardingRuleConfiguration ruleConfig) {
@@ -53,20 +58,6 @@ public final class ShardingTableReferenceRuleResultSet
implements DatabaseDistSQ
return Arrays.asList("name", "sharding_table_reference");
}
- @Override
- public boolean next() {
- return data.hasNext();
- }
-
- @Override
- public Collection<Object> getRowData() {
- return buildTableRowData(data.next());
- }
-
- private Collection<Object> buildTableRowData(final
ShardingTableReferenceRuleConfiguration ruleConfig) {
- return Arrays.asList(ruleConfig.getName(), ruleConfig.getReference());
- }
-
@Override
public String getType() {
return ShowShardingTableReferenceRulesStatement.class.getName();
diff --git
a/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
index 3a1ef2b6912..aa4dcfb947e 100644
---
a/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
+++
b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.query.RQLExecutor
@@ -28,3 +28,4 @@
org.apache.shardingsphere.sharding.distsql.handler.query.ShowUnusedShardingAlgor
org.apache.shardingsphere.sharding.distsql.handler.query.ShowUnusedShardingKeyGeneratorExecutor
org.apache.shardingsphere.sharding.distsql.handler.query.ShowUnusedShardingAuditorsExecutor
org.apache.shardingsphere.sharding.distsql.handler.query.CountShardingRuleExecutor
+org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingTableReferenceRuleExecutor
diff --git
a/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
index b0c759c7b1d..d39cdcf856a 100644
---
a/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
+++
b/features/sharding/distsql/handler/src/main/resources/META-INF/services/org.apache.shardingsphere.distsql.handler.resultset.DistSQLResultSet
@@ -15,5 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.sharding.distsql.handler.query.ShardingTableReferenceRuleResultSet
org.apache.shardingsphere.sharding.distsql.handler.query.BroadcastTableRuleResultSet
diff --git
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShardingTableReferenceRuleResultSetTest.java
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableReferenceRuleExecutorTest.java
similarity index 70%
rename from
features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShardingTableReferenceRuleResultSetTest.java
rename to
features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableReferenceRuleExecutorTest.java
index 73f2aa38fa1..837ebad9712 100644
---
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShardingTableReferenceRuleResultSetTest.java
+++
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableReferenceRuleExecutorTest.java
@@ -17,13 +17,15 @@
package org.apache.shardingsphere.sharding.distsql.query;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
+import
org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import
org.apache.shardingsphere.infra.metadata.database.rule.ShardingSphereRuleMetaData;
import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableReferenceRuleConfiguration;
import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
-import
org.apache.shardingsphere.sharding.distsql.handler.query.ShardingTableReferenceRuleResultSet;
+import
org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingTableReferenceRuleExecutor;
import
org.apache.shardingsphere.sharding.distsql.parser.statement.ShowShardingTableReferenceRulesStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.junit.Test;
@@ -38,17 +40,27 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public final class ShardingTableReferenceRuleResultSetTest {
+public final class ShowShardingTableReferenceRuleExecutorTest {
@Test
public void assertGetRowData() {
- ShardingTableReferenceRuleResultSet resultSet = new
ShardingTableReferenceRuleResultSet();
- resultSet.init(mockDatabase(),
mock(ShowShardingTableReferenceRulesStatement.class));
- Collection<Object> actual = resultSet.getRowData();
- assertThat(actual.size(), is(2));
- Iterator<Object> iterator = actual.iterator();
- assertThat(iterator.next(), is("foo"));
- assertThat(iterator.next(), is("t_order,t_order_item"));
+ RQLExecutor<ShowShardingTableReferenceRulesStatement> executor = new
ShowShardingTableReferenceRuleExecutor();
+ Collection<LocalDataQueryResultRow> actual =
executor.getRows(mockDatabase(),
mock(ShowShardingTableReferenceRulesStatement.class));
+ assertThat(actual.size(), is(1));
+ Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+ LocalDataQueryResultRow row = iterator.next();
+ assertThat(row.getCell(1), is("foo"));
+ assertThat(row.getCell(2), is("t_order,t_order_item"));
+ }
+
+ @Test
+ public void assertGetColumnNames() {
+ RQLExecutor<ShowShardingTableReferenceRulesStatement> executor = new
ShowShardingTableReferenceRuleExecutor();
+ Collection<String> columns = executor.getColumnNames();
+ assertThat(columns.size(), is(2));
+ Iterator<String> iterator = columns.iterator();
+ assertThat(iterator.next(), is("name"));
+ assertThat(iterator.next(), is("sharding_table_reference"));
}
private ShardingSphereDatabase mockDatabase() {