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 af6c0e2fab9 Replace `ShardingKeyGeneratorResultSet` with
`ShowShardingKeyGeneratorExecutor` (#23837)
af6c0e2fab9 is described below
commit af6c0e2fab95f63366dd81baedb62ac28ac9c5ca
Author: Zichao <[email protected]>
AuthorDate: Mon Jan 30 23:18:35 2023 +1300
Replace `ShardingKeyGeneratorResultSet` with
`ShowShardingKeyGeneratorExecutor` (#23837)
---
....java => ShowShardingKeyGeneratorExecutor.java} | 38 ++++++++++------------
...hardingsphere.distsql.handler.query.RQLExecutor | 1 +
...here.distsql.handler.resultset.DistSQLResultSet | 1 -
...a => ShowShardingKeyGeneratorExecutorTest.java} | 38 +++++++++++++++-------
4 files changed, 46 insertions(+), 32 deletions(-)
diff --git
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShardingKeyGeneratorResultSet.java
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingKeyGeneratorExecutor.java
similarity index 58%
rename from
features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShardingKeyGeneratorResultSet.java
rename to
features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingKeyGeneratorExecutor.java
index 1061c0ab6f2..f2a80fdcc82 100644
---
a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShardingKeyGeneratorResultSet.java
+++
b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingKeyGeneratorExecutor.java
@@ -17,31 +17,40 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import
org.apache.shardingsphere.distsql.handler.resultset.DatabaseDistSQLResultSet;
+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.distsql.parser.statement.ShowShardingKeyGeneratorsStatement;
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.Map;
+import java.util.LinkedList;
import java.util.Map.Entry;
+import java.util.Optional;
/**
- * Result set for show sharding key generator.
+ * Show sharding key generator executor.
*/
-public final class ShardingKeyGeneratorResultSet implements
DatabaseDistSQLResultSet {
-
- private Iterator<Entry<String, AlgorithmConfiguration>> data =
Collections.emptyIterator();
+public final class ShowShardingKeyGeneratorExecutor implements
RQLExecutor<ShowShardingKeyGeneratorsStatement> {
@Override
- public void init(final ShardingSphereDatabase database, final SQLStatement
sqlStatement) {
-
database.getRuleMetaData().findSingleRule(ShardingRule.class).map(optional ->
data = ((ShardingRuleConfiguration)
optional.getConfiguration()).getKeyGenerators().entrySet().iterator());
+ public Collection<LocalDataQueryResultRow> getRows(final
ShardingSphereDatabase database, final ShowShardingKeyGeneratorsStatement
sqlStatement) {
+ Optional<ShardingRule> rule =
database.getRuleMetaData().findSingleRule(ShardingRule.class);
+ if (!rule.isPresent()) {
+ return Collections.emptyList();
+ }
+ Iterator<Entry<String, AlgorithmConfiguration>> data =
((ShardingRuleConfiguration)
rule.get().getConfiguration()).getKeyGenerators().entrySet().iterator();
+ Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+ while (data.hasNext()) {
+ Entry<String, AlgorithmConfiguration> entry = data.next();
+ result.add(new LocalDataQueryResultRow(entry.getKey(),
entry.getValue().getType(), entry.getValue().getProps().toString()));
+ }
+ return result;
}
@Override
@@ -49,17 +58,6 @@ public final class ShardingKeyGeneratorResultSet implements
DatabaseDistSQLResul
return Arrays.asList("name", "type", "props");
}
- @Override
- public boolean next() {
- return data.hasNext();
- }
-
- @Override
- public Collection<Object> getRowData() {
- Map.Entry<String, AlgorithmConfiguration> entry = data.next();
- return Arrays.asList(entry.getKey(), entry.getValue().getType(),
entry.getValue().getProps());
- }
-
@Override
public String getType() {
return ShowShardingKeyGeneratorsStatement.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 7af870b8bed..7252ed26763 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
@@ -17,3 +17,4 @@
org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingTableRuleExecutor
org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingAlgorithmExecutor
+org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingKeyGeneratorExecutor
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 7f0bd9f9da7..b13404eb985 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
@@ -19,7 +19,6 @@
org.apache.shardingsphere.sharding.distsql.handler.query.ShardingTableReferenceR
org.apache.shardingsphere.sharding.distsql.handler.query.BroadcastTableRuleResultSet
org.apache.shardingsphere.sharding.distsql.handler.query.ShardingAuditorsResultSet
org.apache.shardingsphere.sharding.distsql.handler.query.ShardingTableNodesResultSet
-org.apache.shardingsphere.sharding.distsql.handler.query.ShardingKeyGeneratorResultSet
org.apache.shardingsphere.sharding.distsql.handler.query.DefaultShardingStrategyResultSet
org.apache.shardingsphere.sharding.distsql.handler.query.UnusedShardingAlgorithmsResultSet
org.apache.shardingsphere.sharding.distsql.handler.query.UnusedShardingKeyGeneratorResultSet
diff --git
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShardingKeyGeneratorResultSetTest.java
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingKeyGeneratorExecutorTest.java
similarity index 62%
rename from
features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShardingKeyGeneratorResultSetTest.java
rename to
features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingKeyGeneratorExecutorTest.java
index 01f916378e2..7ee3e3fbfac 100644
---
a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShardingKeyGeneratorResultSetTest.java
+++
b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingKeyGeneratorExecutorTest.java
@@ -17,27 +17,30 @@
package org.apache.shardingsphere.sharding.distsql.query;
+import org.apache.shardingsphere.distsql.handler.query.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+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.distsql.handler.query.ShardingKeyGeneratorResultSet;
+import
org.apache.shardingsphere.sharding.distsql.handler.query.ShowShardingKeyGeneratorExecutor;
import
org.apache.shardingsphere.sharding.distsql.parser.statement.ShowShardingKeyGeneratorsStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.junit.Test;
-import java.util.ArrayList;
+import java.util.Collection;
import java.util.Collections;
-import java.util.List;
+import java.util.Iterator;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
-public final class ShardingKeyGeneratorResultSetTest {
+public final class ShowShardingKeyGeneratorExecutorTest {
@Test
public void assertGetRowData() {
@@ -45,13 +48,26 @@ public final class ShardingKeyGeneratorResultSetTest {
ShardingRule rule = mock(ShardingRule.class);
when(rule.getConfiguration()).thenReturn(createRuleConfiguration());
when(database.getRuleMetaData()).thenReturn(new
ShardingSphereRuleMetaData(Collections.singleton(rule)));
- ShardingKeyGeneratorResultSet resultSet = new
ShardingKeyGeneratorResultSet();
- resultSet.init(database,
mock(ShowShardingKeyGeneratorsStatement.class));
- List<Object> actual = new ArrayList<>(resultSet.getRowData());
- assertThat(actual.size(), is(3));
- assertThat(actual.get(0), is("snowflake"));
- assertThat(actual.get(1), is("SNOWFLAKE"));
- assertThat(actual.get(2).toString(), is("{}"));
+ RQLExecutor<ShowShardingKeyGeneratorsStatement> executor = new
ShowShardingKeyGeneratorExecutor();
+ Collection<LocalDataQueryResultRow> actual =
executor.getRows(database, mock(ShowShardingKeyGeneratorsStatement.class));
+ assertThat(actual.size(), is(1));
+ Iterator<LocalDataQueryResultRow> iterator = actual.iterator();
+ LocalDataQueryResultRow row = iterator.next();
+ assertThat(row.getCell(1), is("snowflake"));
+ assertThat(row.getCell(2), is("SNOWFLAKE"));
+ assertThat(row.getCell(3), is("{}"));
+ }
+
+ @Test
+ public void assertGetColumnNames() {
+ RQLExecutor<ShowShardingKeyGeneratorsStatement> executor = new
ShowShardingKeyGeneratorExecutor();
+ Collection<String> columns = executor.getColumnNames();
+ assertThat(columns.size(), is(3));
+ Iterator<String> iterator = columns.iterator();
+ assertThat(iterator.next(), is("name"));
+ assertThat(iterator.next(), is("type"));
+ assertThat(iterator.next(), is("props"));
+ assertFalse(iterator.hasNext());
}
private ShardingRuleConfiguration createRuleConfiguration() {