You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2024/01/16 00:08:55 UTC
(shardingsphere) branch master updated: Add RuleAwareRQLExecutor (#29733)
This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 5e84e311f32 Add RuleAwareRQLExecutor (#29733)
5e84e311f32 is described below
commit 5e84e311f328c048b051bb057d5f7e7625651ddb
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Tue Jan 16 08:08:48 2024 +0800
Add RuleAwareRQLExecutor (#29733)
* Add RuleAwareRQLExecutor
* Add RuleAwareRQLExecutor
* Add RuleAwareRQLExecutor
* Add RuleAwareRQLExecutor
* Add RuleAwareRQLExecutor
* Add RuleAwareRQLExecutor
* Add RuleAwareRQLExecutor
* Add RuleAwareRQLExecutor
* Add RuleAwareRQLExecutor
---
.../query/ShowBroadcastTableRuleExecutor.java | 20 +--
.../query/ShowBroadcastTableRuleExecutorTest.java | 4 +-
.../handler/query/ShowEncryptRuleExecutor.java | 37 +++---
.../apache/shardingsphere/mask/rule/MaskRule.java | 3 +-
.../handler/query/ShowMaskRuleExecutor.java | 28 ++---
.../handler/query/ShowMaskRuleExecutorTest.java | 5 +-
.../rule/ReadwriteSplittingRule.java | 11 +-
.../query/ShowReadwriteSplittingRuleExecutor.java | 50 ++++----
.../ShowReadwriteSplittingRuleExecutorTest.java | 5 +-
.../shardingsphere/shadow/rule/ShadowRule.java | 3 +-
.../query/ShowDefaultShadowAlgorithmExecutor.java | 28 ++---
.../query/ShowShadowAlgorithmsExecutor.java | 32 +++--
.../handler/query/ShowShadowRuleExecutor.java | 32 +++--
.../query/ShowShadowTableRulesExecutor.java | 44 +++----
.../ShowDefaultShadowAlgorithmExecutorTest.java | 3 +-
.../query/ShowShadowAlgorithmsExecutorTest.java | 3 +-
.../distsql/query/ShowShadowRuleExecutorTest.java | 3 +-
.../query/ShowShadowTableRulesExecutorTest.java | 3 +-
.../shardingsphere/sharding/rule/ShardingRule.java | 3 +-
.../query/ShowDefaultShardingStrategyExecutor.java | 28 ++---
.../query/ShowShardingAlgorithmExecutor.java | 31 ++---
.../query/ShowShardingAuditorsExecutor.java | 31 ++---
.../query/ShowShardingKeyGeneratorExecutor.java | 33 ++---
.../query/ShowShardingTableNodesExecutor.java | 29 ++---
.../ShowShardingTableReferenceRuleExecutor.java | 31 ++---
.../query/ShowShardingTableRuleExecutor.java | 134 +++++++++++----------
...howShardingTableRulesUsedAlgorithmExecutor.java | 47 ++++----
.../ShowShardingTableRulesUsedAuditorExecutor.java | 39 +++---
...ShardingTableRulesUsedKeyGeneratorExecutor.java | 41 +++----
.../ShowUnusedShardingAlgorithmsExecutor.java | 18 ++-
.../query/ShowUnusedShardingAuditorsExecutor.java | 28 ++---
.../ShowUnusedShardingKeyGeneratorExecutor.java | 42 +++----
.../ShowDefaultShardingStrategyExecutorTest.java | 5 +-
...ShowShardingTableReferenceRuleExecutorTest.java | 3 +-
.../query/ShowShardingTableRuleExecutorTest.java | 3 +-
.../ShowUnusedShardingAlgorithmsExecutorTest.java | 3 +-
.../ShowUnusedShardingAuditorsExecutorTest.java | 3 +-
...ShowUnusedShardingKeyGeneratorExecutorTest.java | 3 +-
.../type/rql/rule/RuleAwareRQLExecutor.java | 30 +++--
.../ShowDefaultSingleTableStorageUnitExecutor.java | 11 +-
.../query/ShowUnloadedSingleTableExecutor.java | 23 ++--
.../handler/query/ShowSingleTableExecutorTest.java | 8 +-
.../rql/rule/ShowRulesUsedStorageUnitExecutor.java | 18 +--
43 files changed, 426 insertions(+), 533 deletions(-)
diff --git a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutor.java b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutor.java
index 305363be861..f823d9ad65e 100644
--- a/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutor.java
+++ b/features/broadcast/distsql/handler/src/main/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutor.java
@@ -19,26 +19,21 @@ package org.apache.shardingsphere.broadcast.distsql.handler.query;
import org.apache.shardingsphere.broadcast.distsql.statement.ShowBroadcastTableRulesStatement;
import org.apache.shardingsphere.broadcast.rule.BroadcastRule;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import java.util.Collection;
import java.util.Collections;
-import java.util.LinkedList;
-import java.util.Optional;
+import java.util.stream.Collectors;
/**
* Show broadcast table rule executor.
*/
-public final class ShowBroadcastTableRuleExecutor implements RQLExecutor<ShowBroadcastTableRulesStatement> {
+public final class ShowBroadcastTableRuleExecutor extends RuleAwareRQLExecutor<ShowBroadcastTableRulesStatement, BroadcastRule> {
- @Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowBroadcastTableRulesStatement sqlStatement) {
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- Optional<BroadcastRule> rule = database.getRuleMetaData().findSingleRule(BroadcastRule.class);
- rule.ifPresent(optional -> optional.getConfiguration().getTables().forEach(each -> result.add(new LocalDataQueryResultRow(each))));
- return result;
+ public ShowBroadcastTableRuleExecutor() {
+ super(BroadcastRule.class);
}
@Override
@@ -46,6 +41,11 @@ public final class ShowBroadcastTableRuleExecutor implements RQLExecutor<ShowBro
return Collections.singleton("broadcast_table");
}
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowBroadcastTableRulesStatement sqlStatement, final BroadcastRule rule) {
+ return rule.getConfiguration().getTables().stream().map(LocalDataQueryResultRow::new).collect(Collectors.toList());
+ }
+
@Override
public Class<ShowBroadcastTableRulesStatement> getType() {
return ShowBroadcastTableRulesStatement.class;
diff --git a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutorTest.java b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutorTest.java
index 4ad727bd406..06b3cb2263e 100644
--- a/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutorTest.java
+++ b/features/broadcast/distsql/handler/src/test/java/org/apache/shardingsphere/broadcast/distsql/handler/query/ShowBroadcastTableRuleExecutorTest.java
@@ -29,6 +29,7 @@ import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
+import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -51,8 +52,9 @@ class ShowBroadcastTableRuleExecutorTest {
}
@Test
- void assertGetRowDataWithoutMaskRule() {
+ void assertGetRowDataWithoutBroadcastRule() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+ when(database.getRuleMetaData().findSingleRule(BroadcastRule.class)).thenReturn(Optional.empty());
Collection<LocalDataQueryResultRow> actual = new ShowBroadcastTableRuleExecutor().getRows(database, mock(ShowBroadcastTableRulesStatement.class));
assertTrue(actual.isEmpty());
}
diff --git a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutor.java b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutor.java
index 7cd63d1ca80..cda04fbeb0e 100644
--- a/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutor.java
+++ b/features/encrypt/distsql/handler/src/main/java/org/apache/shardingsphere/encrypt/distsql/handler/query/ShowEncryptRuleExecutor.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.encrypt.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
import org.apache.shardingsphere.encrypt.api.config.CompatibleEncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnItemRuleConfiguration;
@@ -32,27 +32,31 @@ import org.apache.shardingsphere.infra.props.PropertiesConverter;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;
-import java.util.Optional;
import java.util.stream.Collectors;
/**
* Show encrypt rule executor.
*/
-public final class ShowEncryptRuleExecutor implements RQLExecutor<ShowEncryptRulesStatement> {
+public final class ShowEncryptRuleExecutor extends RuleAwareRQLExecutor<ShowEncryptRulesStatement, EncryptRule> {
+
+ public ShowEncryptRuleExecutor() {
+ super(EncryptRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowEncryptRulesStatement sqlStatement) {
- Optional<EncryptRule> rule = database.getRuleMetaData().findSingleRule(EncryptRule.class);
- if (rule.isPresent()) {
- EncryptRuleConfiguration ruleConfig = rule.get().getConfiguration() instanceof CompatibleEncryptRuleConfiguration
- ? ((CompatibleEncryptRuleConfiguration) rule.get().getConfiguration()).convertToEncryptRuleConfiguration()
- : (EncryptRuleConfiguration) rule.get().getConfiguration();
- return buildData(ruleConfig, sqlStatement);
- }
- return Collections.emptyList();
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("table", "logic_column", "cipher_column",
+ "assisted_query_column", "like_query_column", "encryptor_type", "encryptor_props", "assisted_query_type", "assisted_query_props", "like_query_type", "like_query_props");
+ }
+
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowEncryptRulesStatement sqlStatement, final EncryptRule rule) {
+ EncryptRuleConfiguration ruleConfig = rule.getConfiguration() instanceof CompatibleEncryptRuleConfiguration
+ ? ((CompatibleEncryptRuleConfiguration) rule.getConfiguration()).convertToEncryptRuleConfiguration()
+ : (EncryptRuleConfiguration) rule.getConfiguration();
+ return buildData(ruleConfig, sqlStatement);
}
private Collection<LocalDataQueryResultRow> buildData(final EncryptRuleConfiguration ruleConfig, final ShowEncryptRulesStatement sqlStatement) {
@@ -87,13 +91,6 @@ public final class ShowEncryptRuleExecutor implements RQLExecutor<ShowEncryptRul
return null == obj ? "" : obj;
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("table", "logic_column", "cipher_column",
- "assisted_query_column", "like_query_column", "encryptor_type", "encryptor_props",
- "assisted_query_type", "assisted_query_props", "like_query_type", "like_query_props");
- }
-
@Override
public Class<ShowEncryptRulesStatement> getType() {
return ShowEncryptRulesStatement.class;
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/MaskRule.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/MaskRule.java
index 3c41572e8ab..2ace6855fdb 100644
--- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/MaskRule.java
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/rule/MaskRule.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.mask.rule;
import lombok.Getter;
-import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.identifier.type.TableContainedRule;
import org.apache.shardingsphere.infra.rule.identifier.type.TableNamesMapper;
@@ -37,7 +36,7 @@ import java.util.Optional;
public final class MaskRule implements DatabaseRule, TableContainedRule {
@Getter
- private final RuleConfiguration configuration;
+ private final MaskRuleConfiguration configuration;
private final Map<String, MaskAlgorithm> maskAlgorithms = new LinkedHashMap<>();
diff --git a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutor.java b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutor.java
index 7f791b0628e..f776496cd31 100644
--- a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutor.java
+++ b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutor.java
@@ -17,38 +17,39 @@
package org.apache.shardingsphere.mask.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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.props.PropertiesConverter;
-import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
import org.apache.shardingsphere.mask.distsql.statement.ShowMaskRulesStatement;
import org.apache.shardingsphere.mask.rule.MaskRule;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.LinkedList;
import java.util.Map;
-import java.util.Optional;
import java.util.stream.Collectors;
/**
* Show mask rule executor.
*/
-public final class ShowMaskRuleExecutor implements RQLExecutor<ShowMaskRulesStatement> {
+public final class ShowMaskRuleExecutor extends RuleAwareRQLExecutor<ShowMaskRulesStatement, MaskRule> {
+
+ public ShowMaskRuleExecutor() {
+ super(MaskRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowMaskRulesStatement sqlStatement) {
- Optional<MaskRule> rule = database.getRuleMetaData().findSingleRule(MaskRule.class);
- return rule.isPresent() ? buildData((MaskRuleConfiguration) rule.get().getConfiguration(), sqlStatement) : Collections.emptyList();
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("table", "column", "algorithm_type", "algorithm_props");
}
- private Collection<LocalDataQueryResultRow> buildData(final MaskRuleConfiguration ruleConfig, final ShowMaskRulesStatement sqlStatement) {
- return ruleConfig.getTables().stream().filter(each -> null == sqlStatement.getTableName() || each.getName().equals(sqlStatement.getTableName()))
- .map(each -> buildColumnData(each, ruleConfig.getMaskAlgorithms())).flatMap(Collection::stream).collect(Collectors.toList());
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowMaskRulesStatement sqlStatement, final MaskRule rule) {
+ return rule.getConfiguration().getTables().stream().filter(each -> null == sqlStatement.getTableName() || each.getName().equals(sqlStatement.getTableName()))
+ .map(each -> buildColumnData(each, rule.getConfiguration().getMaskAlgorithms())).flatMap(Collection::stream).collect(Collectors.toList());
}
private Collection<LocalDataQueryResultRow> buildColumnData(final MaskTableRuleConfiguration tableRuleConfig, final Map<String, AlgorithmConfiguration> algorithmMap) {
@@ -61,11 +62,6 @@ public final class ShowMaskRuleExecutor implements RQLExecutor<ShowMaskRulesStat
return result;
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("table", "column", "algorithm_type", "algorithm_props");
- }
-
@Override
public Class<ShowMaskRulesStatement> getType() {
return ShowMaskRulesStatement.class;
diff --git a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutorTest.java b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutorTest.java
index 4507af19121..9a55f7bd09e 100644
--- a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutorTest.java
+++ b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/query/ShowMaskRuleExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.mask.distsql.handler.query;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-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.RuleMetaData;
@@ -33,6 +32,7 @@ import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
+import java.util.Optional;
import java.util.Properties;
import static org.hamcrest.CoreMatchers.is;
@@ -61,6 +61,7 @@ class ShowMaskRuleExecutorTest {
@Test
void assertGetRowDataWithoutMaskRule() {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class, RETURNS_DEEP_STUBS);
+ when(database.getRuleMetaData().findSingleRule(MaskRule.class)).thenReturn(Optional.empty());
RQLExecutor<ShowMaskRulesStatement> executor = new ShowMaskRuleExecutor();
Collection<LocalDataQueryResultRow> actual = executor.getRows(database, mock(ShowMaskRulesStatement.class));
assertTrue(actual.isEmpty());
@@ -86,7 +87,7 @@ class ShowMaskRuleExecutorTest {
return result;
}
- private RuleConfiguration getRuleConfiguration() {
+ private MaskRuleConfiguration getRuleConfiguration() {
MaskColumnRuleConfiguration maskColumnRuleConfig = new MaskColumnRuleConfiguration("user_id", "t_mask_user_id_md5");
MaskTableRuleConfiguration maskTableRuleConfig = new MaskTableRuleConfiguration("t_mask", Collections.singleton(maskColumnRuleConfig));
AlgorithmConfiguration algorithmConfig = new AlgorithmConfiguration("md5", new Properties());
diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingRule.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingRule.java
index 68ec5cb4267..9b8e9c662bb 100644
--- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingRule.java
+++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/rule/ReadwriteSplittingRule.java
@@ -20,8 +20,8 @@ package org.apache.shardingsphere.readwritesplitting.rule;
import com.google.common.base.Preconditions;
import lombok.Getter;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
-import org.apache.shardingsphere.infra.state.datasource.DataSourceState;
+import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
+import org.apache.shardingsphere.infra.expr.core.InlineExpressionParserFactory;
import org.apache.shardingsphere.infra.instance.InstanceContext;
import org.apache.shardingsphere.infra.metadata.database.schema.QualifiedDatabase;
import org.apache.shardingsphere.infra.rule.event.DataSourceStatusChangedEvent;
@@ -32,16 +32,15 @@ import org.apache.shardingsphere.infra.rule.identifier.type.StorageConnectorReus
import org.apache.shardingsphere.infra.rule.identifier.type.exportable.ExportableRule;
import org.apache.shardingsphere.infra.rule.identifier.type.exportable.constant.ExportableConstants;
import org.apache.shardingsphere.infra.rule.identifier.type.exportable.constant.ExportableItemConstants;
-import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
-import org.apache.shardingsphere.infra.expr.core.InlineExpressionParserFactory;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.infra.state.datasource.DataSourceState;
import org.apache.shardingsphere.mode.event.storage.StorageNodeDataSourceChangedEvent;
import org.apache.shardingsphere.mode.event.storage.StorageNodeDataSourceDeletedEvent;
import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.exception.rule.InvalidInlineExpressionDataSourceNameException;
-import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import org.apache.shardingsphere.readwritesplitting.group.type.StaticReadwriteSplittingGroup;
+import org.apache.shardingsphere.readwritesplitting.spi.ReadQueryLoadBalanceAlgorithm;
import java.util.Collection;
import java.util.HashMap;
@@ -60,7 +59,7 @@ public final class ReadwriteSplittingRule implements DatabaseRule, DataSourceCon
private final String databaseName;
@Getter
- private final RuleConfiguration configuration;
+ private final ReadwriteSplittingRuleConfiguration configuration;
private final Map<String, ReadQueryLoadBalanceAlgorithm> loadBalancers;
diff --git a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutor.java b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutor.java
index c6c68fde5f3..243b49e9be2 100644
--- a/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutor.java
+++ b/features/readwrite-splitting/distsql/handler/src/main/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutor.java
@@ -18,13 +18,13 @@
package org.apache.shardingsphere.readwritesplitting.distsql.handler.query;
import com.google.common.base.Joiner;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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.props.PropertiesConverter;
import org.apache.shardingsphere.infra.rule.identifier.type.exportable.constant.ExportableConstants;
import org.apache.shardingsphere.infra.rule.identifier.type.exportable.constant.ExportableItemConstants;
-import org.apache.shardingsphere.infra.props.PropertiesConverter;
import org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
import org.apache.shardingsphere.readwritesplitting.distsql.statement.ShowReadwriteSplittingRulesStatement;
@@ -40,31 +40,24 @@ import java.util.Optional;
/**
* Show readwrite-splitting rule executor.
*/
-public final class ShowReadwriteSplittingRuleExecutor implements RQLExecutor<ShowReadwriteSplittingRulesStatement> {
-
- private Map<String, Map<String, String>> exportableDataSourceMap = Collections.emptyMap();
+public final class ShowReadwriteSplittingRuleExecutor extends RuleAwareRQLExecutor<ShowReadwriteSplittingRulesStatement, ReadwriteSplittingRule> {
- @Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowReadwriteSplittingRulesStatement sqlStatement) {
- Optional<ReadwriteSplittingRule> rule = database.getRuleMetaData().findSingleRule(ReadwriteSplittingRule.class);
- if (rule.isPresent()) {
- buildExportableMap(rule.get());
- return buildData(rule.get(), sqlStatement);
- }
- return Collections.emptyList();
+ public ShowReadwriteSplittingRuleExecutor() {
+ super(ReadwriteSplittingRule.class);
}
- @SuppressWarnings("unchecked")
- private void buildExportableMap(final ReadwriteSplittingRule rule) {
- Map<String, Object> exportedData = rule.getExportData();
- exportableDataSourceMap = (Map<String, Map<String, String>>) exportedData.get(ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE);
+ @Override
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("name", "write_storage_unit_name", "read_storage_unit_names", "transactional_read_query_strategy", "load_balancer_type", "load_balancer_props");
}
- private Collection<LocalDataQueryResultRow> buildData(final ReadwriteSplittingRule rule, final ShowReadwriteSplittingRulesStatement sqlStatement) {
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowReadwriteSplittingRulesStatement sqlStatement, final ReadwriteSplittingRule rule) {
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- ReadwriteSplittingRuleConfiguration ruleConfig = (ReadwriteSplittingRuleConfiguration) rule.getConfiguration();
+ Map<String, Map<String, String>> exportableDataSourceMap = getExportableDataSourceMap(rule);
+ ReadwriteSplittingRuleConfiguration ruleConfig = rule.getConfiguration();
ruleConfig.getDataSources().forEach(each -> {
- LocalDataQueryResultRow dataItem = buildDataItem(each, getLoadBalancers(ruleConfig));
+ LocalDataQueryResultRow dataItem = buildDataItem(exportableDataSourceMap, each, getLoadBalancers(ruleConfig));
if (null == sqlStatement.getRuleName() || sqlStatement.getRuleName().equalsIgnoreCase(each.getName())) {
result.add(dataItem);
}
@@ -72,7 +65,13 @@ public final class ShowReadwriteSplittingRuleExecutor implements RQLExecutor<Sho
return result;
}
- private LocalDataQueryResultRow buildDataItem(final ReadwriteSplittingDataSourceRuleConfiguration dataSourceRuleConfig, final Map<String, AlgorithmConfiguration> loadBalancers) {
+ @SuppressWarnings("unchecked")
+ private Map<String, Map<String, String>> getExportableDataSourceMap(final ReadwriteSplittingRule rule) {
+ return (Map<String, Map<String, String>>) rule.getExportData().get(ExportableConstants.EXPORT_STATIC_READWRITE_SPLITTING_RULE);
+ }
+
+ private LocalDataQueryResultRow buildDataItem(final Map<String, Map<String, String>> exportableDataSourceMap,
+ final ReadwriteSplittingDataSourceRuleConfiguration dataSourceRuleConfig, final Map<String, AlgorithmConfiguration> loadBalancers) {
String name = dataSourceRuleConfig.getName();
Map<String, String> exportDataSources = exportableDataSourceMap.get(name);
Optional<AlgorithmConfiguration> loadBalancer = Optional.ofNullable(loadBalancers.get(dataSourceRuleConfig.getLoadBalancerName()));
@@ -94,14 +93,7 @@ public final class ShowReadwriteSplittingRuleExecutor implements RQLExecutor<Sho
}
private String getReadDataSourceNames(final ReadwriteSplittingDataSourceRuleConfiguration dataSourceRuleConfig, final Map<String, String> exportDataSources) {
- return null == exportDataSources
- ? Joiner.on(",").join(dataSourceRuleConfig.getReadDataSourceNames())
- : exportDataSources.get(ExportableItemConstants.REPLICA_DATA_SOURCE_NAMES);
- }
-
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("name", "write_storage_unit_name", "read_storage_unit_names", "transactional_read_query_strategy", "load_balancer_type", "load_balancer_props");
+ return null == exportDataSources ? Joiner.on(",").join(dataSourceRuleConfig.getReadDataSourceNames()) : exportDataSources.get(ExportableItemConstants.REPLICA_DATA_SOURCE_NAMES);
}
@Override
diff --git a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutorTest.java b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutorTest.java
index a535f2761e4..66386cb51c8 100644
--- a/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutorTest.java
+++ b/features/readwrite-splitting/distsql/handler/src/test/java/org/apache/shardingsphere/readwritesplitting/distsql/handler/query/ShowReadwriteSplittingRuleExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.readwritesplitting.distsql.handler.query;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-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.RuleMetaData;
@@ -104,7 +103,7 @@ class ShowReadwriteSplittingRuleExecutorTest {
return result;
}
- private RuleConfiguration createRuleConfiguration() {
+ private ReadwriteSplittingRuleConfiguration createRuleConfiguration() {
ReadwriteSplittingDataSourceRuleConfiguration dataSourceRuleConfig =
new ReadwriteSplittingDataSourceRuleConfiguration("readwrite_ds", "ds_primary", Arrays.asList("ds_slave_0", "ds_slave_1"), "test");
return new ReadwriteSplittingRuleConfiguration(
@@ -131,7 +130,7 @@ class ShowReadwriteSplittingRuleExecutorTest {
assertThat(row.getCell(6), is(""));
}
- private RuleConfiguration createRuleConfigurationWithoutLoadBalancer() {
+ private ReadwriteSplittingRuleConfiguration createRuleConfigurationWithoutLoadBalancer() {
ReadwriteSplittingDataSourceRuleConfiguration dataSourceRuleConfig =
new ReadwriteSplittingDataSourceRuleConfiguration("readwrite_ds", "write_ds", Arrays.asList("read_ds_0", "read_ds_1"), null);
return new ReadwriteSplittingRuleConfiguration(Collections.singleton(dataSourceRuleConfig), null);
diff --git a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java
index 154fc28b50b..94e44ec5543 100644
--- a/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java
+++ b/features/shadow/core/src/main/java/org/apache/shardingsphere/shadow/rule/ShadowRule.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.shadow.rule;
import lombok.Getter;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule;
import org.apache.shardingsphere.infra.rule.identifier.type.DataSourceContainedRule;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
@@ -44,7 +43,7 @@ import java.util.Optional;
@Getter
public final class ShadowRule implements DatabaseRule, DataSourceContainedRule {
- private final RuleConfiguration configuration;
+ private final ShadowRuleConfiguration configuration;
private final Collection<String> hintShadowAlgorithmNames = new LinkedList<>();
diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowDefaultShadowAlgorithmExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowDefaultShadowAlgorithmExecutor.java
index 645309f64cf..5f07f8db756 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowDefaultShadowAlgorithmExecutor.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowDefaultShadowAlgorithmExecutor.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.shadow.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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;
@@ -28,26 +28,29 @@ import org.apache.shardingsphere.shadow.rule.ShadowRule;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;
-import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;
/**
* Show default shadow algorithm executor.
*/
-public final class ShowDefaultShadowAlgorithmExecutor implements RQLExecutor<ShowDefaultShadowAlgorithmStatement> {
+public final class ShowDefaultShadowAlgorithmExecutor extends RuleAwareRQLExecutor<ShowDefaultShadowAlgorithmStatement, ShadowRule> {
+
+ public ShowDefaultShadowAlgorithmExecutor() {
+ super(ShadowRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowDefaultShadowAlgorithmStatement sqlStatement) {
- Optional<ShadowRule> rule = database.getRuleMetaData().findSingleRule(ShadowRule.class);
- if (!rule.isPresent()) {
- return Collections.emptyList();
- }
- ShadowRuleConfiguration config = (ShadowRuleConfiguration) rule.get().getConfiguration();
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("shadow_algorithm_name", "type", "props");
+ }
+
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowDefaultShadowAlgorithmStatement sqlStatement, final ShadowRule rule) {
+ ShadowRuleConfiguration config = rule.getConfiguration();
String defaultAlgorithm = config.getDefaultShadowAlgorithmName();
Iterator<Entry<String, AlgorithmConfiguration>> data = config.getShadowAlgorithms().entrySet().stream().filter(each -> each.getKey().equals(defaultAlgorithm))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)).entrySet().iterator();
@@ -59,11 +62,6 @@ public final class ShowDefaultShadowAlgorithmExecutor implements RQLExecutor<Sho
return result;
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("shadow_algorithm_name", "type", "props");
- }
-
private String convertToString(final Properties props) {
return null == props ? "" : PropertiesConverter.convert(props);
}
diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowAlgorithmsExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowAlgorithmsExecutor.java
index a86d2ab50c7..252349afa04 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowAlgorithmsExecutor.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowAlgorithmsExecutor.java
@@ -17,38 +17,39 @@
package org.apache.shardingsphere.shadow.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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.props.PropertiesConverter;
-import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.distsql.statement.ShowShadowAlgorithmsStatement;
import org.apache.shardingsphere.shadow.rule.ShadowRule;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;
-import java.util.Optional;
import java.util.Properties;
/**
* Show shadow algorithms executor.
*/
-public final class ShowShadowAlgorithmsExecutor implements RQLExecutor<ShowShadowAlgorithmsStatement> {
+public final class ShowShadowAlgorithmsExecutor extends RuleAwareRQLExecutor<ShowShadowAlgorithmsStatement, ShadowRule> {
+
+ public ShowShadowAlgorithmsExecutor() {
+ super(ShadowRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShadowAlgorithmsStatement sqlStatement) {
- Optional<ShadowRule> rule = database.getRuleMetaData().findSingleRule(ShadowRule.class);
- if (!rule.isPresent()) {
- return Collections.emptyList();
- }
- ShadowRuleConfiguration config = (ShadowRuleConfiguration) rule.get().getConfiguration();
- Iterator<Entry<String, AlgorithmConfiguration>> data = config.getShadowAlgorithms().entrySet().iterator();
- String defaultAlgorithm = config.getDefaultShadowAlgorithmName();
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("shadow_algorithm_name", "type", "props", "is_default");
+ }
+
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShadowAlgorithmsStatement sqlStatement, final ShadowRule rule) {
+ Iterator<Entry<String, AlgorithmConfiguration>> data = rule.getConfiguration().getShadowAlgorithms().entrySet().iterator();
+ String defaultAlgorithm = rule.getConfiguration().getDefaultShadowAlgorithmName();
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
while (data.hasNext()) {
Entry<String, AlgorithmConfiguration> row = data.next();
@@ -57,11 +58,6 @@ public final class ShowShadowAlgorithmsExecutor implements RQLExecutor<ShowShado
return result;
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("shadow_algorithm_name", "type", "props", "is_default");
- }
-
private String convertToString(final Properties props) {
return null == props ? "" : PropertiesConverter.convert(props);
}
diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
index 234cf5f61cc..d6e905289a8 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowRuleExecutor.java
@@ -17,12 +17,11 @@
package org.apache.shardingsphere.shadow.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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.props.PropertiesConverter;
-import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
import org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
import org.apache.shardingsphere.shadow.distsql.statement.ShowShadowRulesStatement;
@@ -34,26 +33,30 @@ import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
-import java.util.Optional;
import java.util.stream.Collectors;
/**
* Show shadow rule executor.
*/
-public final class ShowShadowRuleExecutor implements RQLExecutor<ShowShadowRulesStatement> {
+public final class ShowShadowRuleExecutor extends RuleAwareRQLExecutor<ShowShadowRulesStatement, ShadowRule> {
+
+ public ShowShadowRuleExecutor() {
+ super(ShadowRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShadowRulesStatement sqlStatement) {
- Optional<ShadowRule> rule = database.getRuleMetaData().findSingleRule(ShadowRule.class);
- return rule.isPresent() ? buildData((ShadowRuleConfiguration) rule.get().getConfiguration(), sqlStatement) : Collections.emptyList();
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("shadow_table", "rule_name", "source_name", "shadow_name", "algorithm_type", "algorithm_props");
}
- private Collection<LocalDataQueryResultRow> buildData(final ShadowRuleConfiguration ruleConfig, final ShowShadowRulesStatement sqlStatement) {
- Map<String, Map<String, ShadowTableConfiguration>> dataSourceTableMap = convertToDataSourceTableMap(ruleConfig.getTables());
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShadowRulesStatement sqlStatement, final ShadowRule rule) {
+ Map<String, Map<String, ShadowTableConfiguration>> dataSourceTableMap = convertToDataSourceTableMap(rule.getConfiguration().getTables());
Collection<ShadowDataSourceConfiguration> specifiedConfigs = isSpecified(sqlStatement)
- ? ruleConfig.getDataSources().stream().filter(each -> each.getName().equalsIgnoreCase(sqlStatement.getRuleName())).collect(Collectors.toList())
- : ruleConfig.getDataSources();
- return specifiedConfigs.stream().map(each -> buildColumnData(each, dataSourceTableMap, ruleConfig.getShadowAlgorithms())).flatMap(Collection::stream).collect(Collectors.toList());
+ ? rule.getConfiguration().getDataSources().stream().filter(each -> each.getName().equalsIgnoreCase(sqlStatement.getRuleName())).collect(Collectors.toList())
+ : rule.getConfiguration().getDataSources();
+ return specifiedConfigs.stream()
+ .map(each -> buildColumnData(each, dataSourceTableMap, rule.getConfiguration().getShadowAlgorithms())).flatMap(Collection::stream).collect(Collectors.toList());
}
private Map<String, Map<String, ShadowTableConfiguration>> convertToDataSourceTableMap(final Map<String, ShadowTableConfiguration> tables) {
@@ -79,11 +82,6 @@ public final class ShowShadowRuleExecutor implements RQLExecutor<ShowShadowRules
return result;
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("shadow_table", "rule_name", "source_name", "shadow_name", "algorithm_type", "algorithm_props");
- }
-
@Override
public Class<ShowShadowRulesStatement> getType() {
return ShowShadowRulesStatement.class;
diff --git a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowTableRulesExecutor.java b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowTableRulesExecutor.java
index ad53c1f6b58..c5c1b2e2a19 100644
--- a/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowTableRulesExecutor.java
+++ b/features/shadow/distsql/handler/src/main/java/org/apache/shardingsphere/shadow/distsql/handler/query/ShowShadowTableRulesExecutor.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.shadow.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
@@ -27,39 +27,38 @@ import org.apache.shardingsphere.shadow.rule.ShadowRule;
import java.util.ArrayList;
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.Optional;
+import java.util.stream.Collectors;
/**
* Show shadow table rules executor.
*/
-public final class ShowShadowTableRulesExecutor implements RQLExecutor<ShowShadowTableRulesStatement> {
+public final class ShowShadowTableRulesExecutor extends RuleAwareRQLExecutor<ShowShadowTableRulesStatement, ShadowRule> {
private static final String SHADOW_TABLE = "shadow_table";
private static final String SHADOW_ALGORITHM_NAME = "shadow_algorithm_name";
+ public ShowShadowTableRulesExecutor() {
+ super(ShadowRule.class);
+ }
+
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShadowTableRulesStatement sqlStatement) {
- Optional<ShadowRule> rule = database.getRuleMetaData().findSingleRule(ShadowRule.class);
- Iterator<Map<String, String>> data = rule.map(optional -> buildData((ShadowRuleConfiguration) optional.getConfiguration(), sqlStatement).iterator()).orElse(Collections.emptyIterator());
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- while (data.hasNext()) {
- Map<String, String> row = data.next();
- result.add(new LocalDataQueryResultRow(row.get(SHADOW_TABLE), row.get(SHADOW_ALGORITHM_NAME)));
- }
- return result;
+ public Collection<String> getColumnNames() {
+ return Arrays.asList(SHADOW_TABLE, SHADOW_ALGORITHM_NAME);
+ }
+
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShadowTableRulesStatement sqlStatement, final ShadowRule rule) {
+ return buildData(rule.getConfiguration(), sqlStatement).stream()
+ .map(each -> new LocalDataQueryResultRow(each.get(SHADOW_TABLE), each.get(SHADOW_ALGORITHM_NAME))).collect(Collectors.toList());
}
- private List<Map<String, String>> buildData(final ShadowRuleConfiguration shadowRuleConfig, final ShowShadowTableRulesStatement sqlStatement) {
- List<Map<String, String>> result = new ArrayList<>();
+ private Collection<Map<String, String>> buildData(final ShadowRuleConfiguration ruleConfig, final ShowShadowTableRulesStatement sqlStatement) {
+ Collection<Map<String, String>> result = new ArrayList<>();
if (isSpecified(sqlStatement)) {
- shadowRuleConfig.getTables().forEach((key, value) -> {
+ ruleConfig.getTables().forEach((key, value) -> {
Map<String, String> map = new HashMap<>();
if (key.equalsIgnoreCase(sqlStatement.getTableName())) {
map.put(SHADOW_TABLE, key);
@@ -68,7 +67,7 @@ public final class ShowShadowTableRulesExecutor implements RQLExecutor<ShowShado
result.add(map);
});
} else {
- shadowRuleConfig.getTables().forEach((key, value) -> {
+ ruleConfig.getTables().forEach((key, value) -> {
Map<String, String> map = new HashMap<>();
map.put(SHADOW_TABLE, key);
map.put(SHADOW_ALGORITHM_NAME, convertToString(value.getShadowAlgorithmNames()));
@@ -78,11 +77,6 @@ public final class ShowShadowTableRulesExecutor implements RQLExecutor<ShowShado
return result;
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList(SHADOW_TABLE, SHADOW_ALGORITHM_NAME);
- }
-
private boolean isSpecified(final ShowShadowTableRulesStatement sqlStatement) {
return null != sqlStatement.getTableName() && !sqlStatement.getTableName().isEmpty();
}
diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowDefaultShadowAlgorithmExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowDefaultShadowAlgorithmExecutorTest.java
index aed1c5e1d0d..a16edcc03c0 100644
--- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowDefaultShadowAlgorithmExecutorTest.java
+++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowDefaultShadowAlgorithmExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.shadow.distsql.query;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-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.RuleMetaData;
@@ -75,7 +74,7 @@ class ShowDefaultShadowAlgorithmExecutorTest {
return result;
}
- private RuleConfiguration createRuleConfiguration() {
+ private ShadowRuleConfiguration createRuleConfiguration() {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
result.getTables().put("t_order", new ShadowTableConfiguration(Collections.emptyList(), Collections.singleton("shadowAlgorithmName")));
result.getShadowAlgorithms().put("shadowAlgorithmName", new AlgorithmConfiguration("sql_hint", PropertiesBuilder.build(new Property("foo", "bar"))));
diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowAlgorithmsExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowAlgorithmsExecutorTest.java
index 4498c2e3c11..76fee3b192e 100644
--- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowAlgorithmsExecutorTest.java
+++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowAlgorithmsExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.shadow.distsql.query;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-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.RuleMetaData;
@@ -77,7 +76,7 @@ class ShowShadowAlgorithmsExecutorTest {
return result;
}
- private RuleConfiguration createRuleConfiguration() {
+ private ShadowRuleConfiguration createRuleConfiguration() {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
result.getTables().put("t_order", new ShadowTableConfiguration(Collections.emptyList(), Collections.singleton("shadowAlgorithmName")));
result.getShadowAlgorithms().put("shadowAlgorithmName", new AlgorithmConfiguration("sql_hint", PropertiesBuilder.build(new Property("foo", "bar"))));
diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java
index ee561db9e60..43a1f0db3b9 100644
--- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java
+++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowRuleExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.shadow.distsql.query;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-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.RuleMetaData;
@@ -88,7 +87,7 @@ class ShowShadowRuleExecutorTest {
return result;
}
- private RuleConfiguration createRuleConfiguration() {
+ private ShadowRuleConfiguration createRuleConfiguration() {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
result.getDataSources().add(new ShadowDataSourceConfiguration("shadow_rule", "source", "shadow"));
result.getShadowAlgorithms().put("user_id_select_match_algorithm", new AlgorithmConfiguration("REGEX_MATCH", new Properties()));
diff --git a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowTableRulesExecutorTest.java b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowTableRulesExecutorTest.java
index 4e8910686a1..82f07d87c4c 100644
--- a/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowTableRulesExecutorTest.java
+++ b/features/shadow/distsql/handler/src/test/java/org/apache/shardingsphere/shadow/distsql/query/ShowShadowTableRulesExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.shadow.distsql.query;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-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.RuleMetaData;
@@ -74,7 +73,7 @@ class ShowShadowTableRulesExecutorTest {
return result;
}
- private RuleConfiguration createRuleConfiguration() {
+ private ShadowRuleConfiguration createRuleConfiguration() {
ShadowRuleConfiguration result = new ShadowRuleConfiguration();
result.getTables().put("t_order", new ShadowTableConfiguration(Collections.emptyList(), Arrays.asList("shadowAlgorithmName_1", "shadowAlgorithmName_2")));
result.getShadowAlgorithms().put("shadowAlgorithmName", new AlgorithmConfiguration("sql_hint", PropertiesBuilder.build(new Property("foo", "bar"))));
diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
index abfee819c33..cb9c74d1502 100644
--- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
+++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java
@@ -23,7 +23,6 @@ import com.google.common.base.Strings;
import lombok.Getter;
import org.apache.shardingsphere.infra.binder.context.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.context.statement.dml.SelectStatementContext;
-import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeRegistry;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
@@ -89,7 +88,7 @@ public final class ShardingRule implements DatabaseRule, DataNodeContainedRule,
private static final String ALGORITHM_EXPRESSION_KEY = "algorithm-expression";
- private final RuleConfiguration configuration;
+ private final ShardingRuleConfiguration configuration;
private final Collection<String> dataSourceNames;
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowDefaultShardingStrategyExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowDefaultShardingStrategyExecutor.java
index 4ebc5cc70a6..8b71ea66eed 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowDefaultShardingStrategyExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowDefaultShardingStrategyExecutor.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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;
@@ -29,27 +29,28 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
-import java.util.Optional;
/**
* Show default sharding strategy executor.
*/
-public final class ShowDefaultShardingStrategyExecutor implements RQLExecutor<ShowDefaultShardingStrategyStatement> {
+public final class ShowDefaultShardingStrategyExecutor extends RuleAwareRQLExecutor<ShowDefaultShardingStrategyStatement, ShardingRule> {
+
+ public ShowDefaultShardingStrategyExecutor() {
+ super(ShardingRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowDefaultShardingStrategyStatement sqlStatement) {
- Optional<ShardingRule> shardingRule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
- return shardingRule.map(this::buildData).orElse(Collections.emptyList());
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("name", "type", "sharding_column", "sharding_algorithm_name", "sharding_algorithm_type", "sharding_algorithm_props");
}
- private Collection<LocalDataQueryResultRow> buildData(final ShardingRule rule) {
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowDefaultShardingStrategyStatement sqlStatement, final ShardingRule rule) {
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- ShardingRuleConfiguration ruleConfig = (ShardingRuleConfiguration) rule.getConfiguration();
- result.add(buildDataItem("TABLE", ruleConfig, ruleConfig.getDefaultTableShardingStrategy()));
- result.add(buildDataItem("DATABASE", ruleConfig, ruleConfig.getDefaultDatabaseShardingStrategy()));
+ result.add(buildDataItem("TABLE", rule.getConfiguration(), rule.getConfiguration().getDefaultTableShardingStrategy()));
+ result.add(buildDataItem("DATABASE", rule.getConfiguration(), rule.getConfiguration().getDefaultDatabaseShardingStrategy()));
return result;
}
@@ -68,11 +69,6 @@ public final class ShowDefaultShardingStrategyExecutor implements RQLExecutor<Sh
return new LocalDataQueryResultRow(defaultType, strategyType.toString(), shardingColumn, algorithmName, algorithmConfig.getType(), algorithmConfig.getProps().toString());
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("name", "type", "sharding_column", "sharding_algorithm_name", "sharding_algorithm_type", "sharding_algorithm_props");
- }
-
@Override
public Class<ShowDefaultShardingStrategyStatement> getType() {
return ShowDefaultShardingStrategyStatement.class;
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingAlgorithmExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingAlgorithmExecutor.java
index dc5a13bb8a9..80cec1f0464 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingAlgorithmExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingAlgorithmExecutor.java
@@ -17,40 +17,25 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.props.PropertiesConverter;
-import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingAlgorithmsStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.Map.Entry;
-import java.util.Optional;
import java.util.Properties;
+import java.util.stream.Collectors;
/**
* Show sharding algorithm executor.
*/
-public final class ShowShardingAlgorithmExecutor implements RQLExecutor<ShowShardingAlgorithmsStatement> {
+public final class ShowShardingAlgorithmExecutor extends RuleAwareRQLExecutor<ShowShardingAlgorithmsStatement, ShardingRule> {
- @Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingAlgorithmsStatement sqlStatement) {
- Optional<ShardingRule> rule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
- Iterator<Entry<String, AlgorithmConfiguration>> data =
- rule.map(optional -> ((ShardingRuleConfiguration) optional.getConfiguration()).getShardingAlgorithms().entrySet().iterator()).orElse(Collections.emptyIterator());
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- while (data.hasNext()) {
- Entry<String, AlgorithmConfiguration> entry = data.next();
- result.add(new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), buildProps(entry.getValue().getProps())));
- }
- return result;
+ public ShowShardingAlgorithmExecutor() {
+ super(ShardingRule.class);
}
@Override
@@ -58,6 +43,12 @@ public final class ShowShardingAlgorithmExecutor implements RQLExecutor<ShowShar
return Arrays.asList("name", "type", "props");
}
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingAlgorithmsStatement sqlStatement, final ShardingRule rule) {
+ return rule.getConfiguration().getShardingAlgorithms().entrySet().stream()
+ .map(entry -> new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), buildProps(entry.getValue().getProps()))).collect(Collectors.toList());
+ }
+
private String buildProps(final Properties props) {
return null == props ? "" : PropertiesConverter.convert(props);
}
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingAuditorsExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingAuditorsExecutor.java
index d52d1aa1afd..7976c9e0bac 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingAuditorsExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingAuditorsExecutor.java
@@ -17,39 +17,24 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.props.PropertiesConverter;
-import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingAuditorsStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.Map.Entry;
-import java.util.Optional;
+import java.util.stream.Collectors;
/**
* Show sharding auditors executor.
*/
-public final class ShowShardingAuditorsExecutor implements RQLExecutor<ShowShardingAuditorsStatement> {
+public final class ShowShardingAuditorsExecutor extends RuleAwareRQLExecutor<ShowShardingAuditorsStatement, ShardingRule> {
- @Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingAuditorsStatement sqlStatement) {
- Optional<ShardingRule> rule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
- Iterator<Entry<String, AlgorithmConfiguration>> data =
- rule.map(optional -> ((ShardingRuleConfiguration) optional.getConfiguration()).getAuditors().entrySet().iterator()).orElse(Collections.emptyIterator());
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- while (data.hasNext()) {
- Entry<String, AlgorithmConfiguration> entry = data.next();
- result.add(new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), PropertiesConverter.convert(entry.getValue().getProps())));
- }
- return result;
+ public ShowShardingAuditorsExecutor() {
+ super(ShardingRule.class);
}
@Override
@@ -57,6 +42,12 @@ public final class ShowShardingAuditorsExecutor implements RQLExecutor<ShowShard
return Arrays.asList("name", "type", "props");
}
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingAuditorsStatement sqlStatement, final ShardingRule rule) {
+ return rule.getConfiguration().getAuditors().entrySet().stream()
+ .map(entry -> new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), PropertiesConverter.convert(entry.getValue().getProps()))).collect(Collectors.toList());
+ }
+
@Override
public Class<ShowShardingAuditorsStatement> getType() {
return ShowShardingAuditorsStatement.class;
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingKeyGeneratorExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingKeyGeneratorExecutor.java
index 16ab714df9b..64d994eafe5 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingKeyGeneratorExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingKeyGeneratorExecutor.java
@@ -17,41 +17,24 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.props.PropertiesConverter;
-import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
import org.apache.shardingsphere.sharding.distsql.statement.ShowShardingKeyGeneratorsStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.Map.Entry;
-import java.util.Optional;
+import java.util.stream.Collectors;
/**
* Show sharding key generator executor.
*/
-public final class ShowShardingKeyGeneratorExecutor implements RQLExecutor<ShowShardingKeyGeneratorsStatement> {
+public final class ShowShardingKeyGeneratorExecutor extends RuleAwareRQLExecutor<ShowShardingKeyGeneratorsStatement, ShardingRule> {
- @Override
- 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(), PropertiesConverter.convert(entry.getValue().getProps())));
- }
- return result;
+ public ShowShardingKeyGeneratorExecutor() {
+ super(ShardingRule.class);
}
@Override
@@ -59,6 +42,12 @@ public final class ShowShardingKeyGeneratorExecutor implements RQLExecutor<ShowS
return Arrays.asList("name", "type", "props");
}
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingKeyGeneratorsStatement sqlStatement, final ShardingRule rule) {
+ return rule.getConfiguration().getKeyGenerators().entrySet().stream()
+ .map(entry -> new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), PropertiesConverter.convert(entry.getValue().getProps()))).collect(Collectors.toList());
+ }
+
@Override
public Class<ShowShardingKeyGeneratorsStatement> getType() {
return ShowShardingKeyGeneratorsStatement.class;
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableNodesExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableNodesExecutor.java
index aebdb584f1b..645e43e290b 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableNodesExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableNodesExecutor.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
@@ -28,39 +28,34 @@ import org.apache.shardingsphere.sharding.rule.TableRule;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
-import java.util.Optional;
import java.util.stream.Collectors;
/**
* Show sharding table nodes executor.
*/
-public final class ShowShardingTableNodesExecutor implements RQLExecutor<ShowShardingTableNodesStatement> {
+public final class ShowShardingTableNodesExecutor extends RuleAwareRQLExecutor<ShowShardingTableNodesStatement, ShardingRule> {
- private static final String NAME = "name";
+ public ShowShardingTableNodesExecutor() {
+ super(ShardingRule.class);
+ }
- private static final String NODES = "nodes";
+ @Override
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("name", "nodes");
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableNodesStatement sqlStatement) {
- Optional<ShardingRule> shardingRule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
- if (!shardingRule.isPresent()) {
- return Collections.emptyList();
- }
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableNodesStatement sqlStatement, final ShardingRule rule) {
String tableName = sqlStatement.getTableName();
return null == tableName
- ? shardingRule.get().getTableRules().entrySet().stream().map(entry -> new LocalDataQueryResultRow(entry.getKey(), getTableNodes(entry.getValue()))).collect(Collectors.toList())
- : Collections.singleton(new LocalDataQueryResultRow(tableName, getTableNodes(shardingRule.get().getTableRule(tableName))));
+ ? rule.getTableRules().entrySet().stream().map(entry -> new LocalDataQueryResultRow(entry.getKey(), getTableNodes(entry.getValue()))).collect(Collectors.toList())
+ : Collections.singleton(new LocalDataQueryResultRow(tableName, getTableNodes(rule.getTableRule(tableName))));
}
private String getTableNodes(final TableRule tableRule) {
return tableRule.getActualDataNodes().stream().map(DataNode::format).collect(Collectors.joining(", "));
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList(NAME, NODES);
- }
-
@Override
public Class<ShowShardingTableNodesStatement> getType() {
return ShowShardingTableNodesStatement.class;
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableReferenceRuleExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableReferenceRuleExecutor.java
index 0317965384e..e0d81b44bab 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableReferenceRuleExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableReferenceRuleExecutor.java
@@ -17,38 +17,23 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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.statement.ShowShardingTableReferenceRulesStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
-import java.util.LinkedList;
-import java.util.Optional;
+import java.util.stream.Collectors;
/**
* Show sharding table reference rules executor.
*/
-public final class ShowShardingTableReferenceRuleExecutor implements RQLExecutor<ShowShardingTableReferenceRulesStatement> {
+public final class ShowShardingTableReferenceRuleExecutor extends RuleAwareRQLExecutor<ShowShardingTableReferenceRulesStatement, ShardingRule> {
- @Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableReferenceRulesStatement sqlStatement) {
- Optional<ShardingRule> rule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
- if (!rule.isPresent()) {
- return Collections.emptyList();
- }
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- for (ShardingTableReferenceRuleConfiguration referenceRule : ((ShardingRuleConfiguration) rule.get().getConfiguration()).getBindingTableGroups()) {
- if (null == sqlStatement.getRuleName() || referenceRule.getName().equalsIgnoreCase(sqlStatement.getRuleName())) {
- result.add(new LocalDataQueryResultRow(referenceRule.getName(), referenceRule.getReference()));
- }
- }
- return result;
+ public ShowShardingTableReferenceRuleExecutor() {
+ super(ShardingRule.class);
}
@Override
@@ -56,6 +41,12 @@ public final class ShowShardingTableReferenceRuleExecutor implements RQLExecutor
return Arrays.asList("name", "sharding_table_reference");
}
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableReferenceRulesStatement sqlStatement, final ShardingRule rule) {
+ return rule.getConfiguration().getBindingTableGroups().stream().filter(each -> null == sqlStatement.getRuleName() || each.getName().equalsIgnoreCase(sqlStatement.getRuleName()))
+ .map(each -> new LocalDataQueryResultRow(each.getName(), each.getReference())).collect(Collectors.toList());
+ }
+
@Override
public Class<ShowShardingTableReferenceRulesStatement> getType() {
return ShowShardingTableReferenceRulesStatement.class;
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRuleExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRuleExecutor.java
index 399e5c0e6d4..c4d83754856 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRuleExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRuleExecutor.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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;
@@ -36,7 +36,6 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Optional;
@@ -45,66 +44,65 @@ import java.util.stream.Collectors;
/**
* Show sharding table rules executor.
*/
-public final class ShowShardingTableRuleExecutor implements RQLExecutor<ShowShardingTableRulesStatement> {
+public final class ShowShardingTableRuleExecutor extends RuleAwareRQLExecutor<ShowShardingTableRulesStatement, ShardingRule> {
- private ShardingRuleConfiguration shardingRuleConfig;
+ public ShowShardingTableRuleExecutor() {
+ super(ShardingRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableRulesStatement sqlStatement) {
- Optional<ShardingRule> rule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
- if (!rule.isPresent()) {
- return Collections.emptyList();
- }
- ShardingRuleConfiguration config = (ShardingRuleConfiguration) rule.get().getConfiguration();
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("table", "actual_data_nodes", "actual_data_sources", "database_strategy_type", "database_sharding_column", "database_sharding_algorithm_type",
+ "database_sharding_algorithm_props", "table_strategy_type", "table_sharding_column", "table_sharding_algorithm_type", "table_sharding_algorithm_props",
+ "key_generate_column", "key_generator_type", "key_generator_props", "auditor_types", "allow_hint_disable");
+ }
+
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableRulesStatement sqlStatement, final ShardingRule rule) {
String tableName = sqlStatement.getTableName();
Iterator<ShardingTableRuleConfiguration> tables;
Iterator<ShardingAutoTableRuleConfiguration> autoTables;
if (null == tableName) {
- tables = config.getTables().iterator();
- autoTables = config.getAutoTables().iterator();
+ tables = rule.getConfiguration().getTables().iterator();
+ autoTables = rule.getConfiguration().getAutoTables().iterator();
} else {
- tables = config.getTables().stream().filter(each -> tableName.equalsIgnoreCase(each.getLogicTable())).collect(Collectors.toList()).iterator();
- autoTables = config.getAutoTables().stream().filter(each -> tableName.equalsIgnoreCase(each.getLogicTable())).collect(Collectors.toList()).iterator();
+ tables = rule.getConfiguration().getTables().stream().filter(each -> tableName.equalsIgnoreCase(each.getLogicTable())).collect(Collectors.toList()).iterator();
+ autoTables = rule.getConfiguration().getAutoTables().stream().filter(each -> tableName.equalsIgnoreCase(each.getLogicTable())).collect(Collectors.toList()).iterator();
}
- shardingRuleConfig = config;
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
while (tables.hasNext()) {
- result.add(buildTableRowData(tables.next()));
+ result.add(buildTableRowData(rule.getConfiguration(), tables.next()));
}
while (autoTables.hasNext()) {
- result.add(buildAutoTableRowData(autoTables.next()));
+ result.add(buildAutoTableRowData(rule.getConfiguration(), autoTables.next()));
}
return result;
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("table", "actual_data_nodes", "actual_data_sources", "database_strategy_type", "database_sharding_column", "database_sharding_algorithm_type",
- "database_sharding_algorithm_props", "table_strategy_type", "table_sharding_column", "table_sharding_algorithm_type", "table_sharding_algorithm_props",
- "key_generate_column", "key_generator_type", "key_generator_props", "auditor_types", "allow_hint_disable");
- }
-
- private LocalDataQueryResultRow buildTableRowData(final ShardingTableRuleConfiguration shardingTableRuleConfig) {
- Optional<ShardingStrategyConfiguration> databaseShardingStrategyConfig = getDatabaseShardingStrategy(shardingTableRuleConfig);
- Optional<ShardingStrategyConfiguration> tableShardingStrategyConfig = getTableShardingStrategy(shardingTableRuleConfig.getTableShardingStrategy());
+ private LocalDataQueryResultRow buildTableRowData(final ShardingRuleConfiguration ruleConfig, final ShardingTableRuleConfiguration shardingTableRuleConfig) {
+ Optional<ShardingStrategyConfiguration> databaseShardingStrategyConfig = getDatabaseShardingStrategy(ruleConfig, shardingTableRuleConfig);
+ Optional<ShardingStrategyConfiguration> tableShardingStrategyConfig = getTableShardingStrategy(ruleConfig, shardingTableRuleConfig.getTableShardingStrategy());
return new LocalDataQueryResultRow(shardingTableRuleConfig.getLogicTable(), shardingTableRuleConfig.getActualDataNodes(), "",
databaseShardingStrategyConfig.map(this::getStrategyType).orElse(""), databaseShardingStrategyConfig.map(this::getShardingColumn).orElse(""),
- databaseShardingStrategyConfig.map(this::getAlgorithmType).orElse(""), databaseShardingStrategyConfig.map(this::getAlgorithmProperties).orElse(""),
+ databaseShardingStrategyConfig.map(optional -> getAlgorithmType(ruleConfig, optional)).orElse(""),
+ databaseShardingStrategyConfig.map(optional -> getAlgorithmProperties(ruleConfig, optional)).orElse(""),
tableShardingStrategyConfig.map(this::getStrategyType).orElse(""), tableShardingStrategyConfig.map(this::getShardingColumn).orElse(""),
- tableShardingStrategyConfig.map(this::getAlgorithmType).orElse(""), tableShardingStrategyConfig.map(this::getAlgorithmProperties).orElse(""),
- getKeyGenerateColumn(shardingTableRuleConfig.getKeyGenerateStrategy()), getKeyGeneratorType(shardingTableRuleConfig.getKeyGenerateStrategy()),
- getKeyGeneratorProps(shardingTableRuleConfig.getKeyGenerateStrategy()), getAuditorTypes(shardingTableRuleConfig.getAuditStrategy()),
- getAllowHintDisable(shardingTableRuleConfig.getAuditStrategy()));
+ tableShardingStrategyConfig.map(optional -> getAlgorithmType(ruleConfig, optional)).orElse(""),
+ tableShardingStrategyConfig.map(optional -> getAlgorithmProperties(ruleConfig, optional)).orElse(""),
+ getKeyGenerateColumn(ruleConfig, shardingTableRuleConfig.getKeyGenerateStrategy()), getKeyGeneratorType(ruleConfig, shardingTableRuleConfig.getKeyGenerateStrategy()),
+ getKeyGeneratorProps(ruleConfig, shardingTableRuleConfig.getKeyGenerateStrategy()), getAuditorTypes(ruleConfig, shardingTableRuleConfig.getAuditStrategy()),
+ getAllowHintDisable(ruleConfig, shardingTableRuleConfig.getAuditStrategy()));
}
- private LocalDataQueryResultRow buildAutoTableRowData(final ShardingAutoTableRuleConfiguration shardingAutoTableRuleConfig) {
- Optional<ShardingStrategyConfiguration> tableShardingStrategyConfig = getTableShardingStrategy(shardingAutoTableRuleConfig.getShardingStrategy());
+ private LocalDataQueryResultRow buildAutoTableRowData(final ShardingRuleConfiguration ruleConfig, final ShardingAutoTableRuleConfiguration shardingAutoTableRuleConfig) {
+ Optional<ShardingStrategyConfiguration> tableShardingStrategyConfig = getTableShardingStrategy(ruleConfig, shardingAutoTableRuleConfig.getShardingStrategy());
return new LocalDataQueryResultRow(shardingAutoTableRuleConfig.getLogicTable(), "", shardingAutoTableRuleConfig.getActualDataSources(), "", "", "", "",
tableShardingStrategyConfig.map(this::getStrategyType).orElse(""), tableShardingStrategyConfig.map(this::getShardingColumn).orElse(""),
- tableShardingStrategyConfig.map(this::getAlgorithmType).orElse(""), tableShardingStrategyConfig.map(this::getAlgorithmProperties).orElse(""),
- getKeyGenerateColumn(shardingAutoTableRuleConfig.getKeyGenerateStrategy()), getKeyGeneratorType(shardingAutoTableRuleConfig.getKeyGenerateStrategy()),
- getKeyGeneratorProps(shardingAutoTableRuleConfig.getKeyGenerateStrategy()), getAuditorTypes(shardingAutoTableRuleConfig.getAuditStrategy()),
- getAllowHintDisable(shardingAutoTableRuleConfig.getAuditStrategy()));
+ tableShardingStrategyConfig.map(optional -> getAlgorithmType(ruleConfig, optional)).orElse(""),
+ tableShardingStrategyConfig.map(optional -> getAlgorithmProperties(ruleConfig, optional)).orElse(""),
+ getKeyGenerateColumn(ruleConfig, shardingAutoTableRuleConfig.getKeyGenerateStrategy()), getKeyGeneratorType(ruleConfig, shardingAutoTableRuleConfig.getKeyGenerateStrategy()),
+ getKeyGeneratorProps(ruleConfig, shardingAutoTableRuleConfig.getKeyGenerateStrategy()), getAuditorTypes(ruleConfig, shardingAutoTableRuleConfig.getAuditStrategy()),
+ getAllowHintDisable(ruleConfig, shardingAutoTableRuleConfig.getAuditStrategy()));
}
private String getShardingColumn(final ShardingStrategyConfiguration shardingStrategyConfig) {
@@ -117,71 +115,75 @@ public final class ShowShardingTableRuleExecutor implements RQLExecutor<ShowShar
return "";
}
- private String getAlgorithmType(final ShardingStrategyConfiguration shardingStrategyConfig) {
- return shardingStrategyConfig instanceof NoneShardingStrategyConfiguration ? "" : getAlgorithmConfiguration(shardingStrategyConfig.getShardingAlgorithmName()).getType();
+ private String getAlgorithmType(final ShardingRuleConfiguration ruleConfig, final ShardingStrategyConfiguration shardingStrategyConfig) {
+ return shardingStrategyConfig instanceof NoneShardingStrategyConfiguration ? "" : getAlgorithmConfiguration(ruleConfig, shardingStrategyConfig.getShardingAlgorithmName()).getType();
}
- private String getAlgorithmProperties(final ShardingStrategyConfiguration shardingStrategyConfig) {
+ private String getAlgorithmProperties(final ShardingRuleConfiguration ruleConfig, final ShardingStrategyConfiguration shardingStrategyConfig) {
return shardingStrategyConfig instanceof NoneShardingStrategyConfiguration
? ""
- : PropertiesConverter.convert(getAlgorithmConfiguration(shardingStrategyConfig.getShardingAlgorithmName()).getProps());
+ : PropertiesConverter.convert(getAlgorithmConfiguration(ruleConfig, shardingStrategyConfig.getShardingAlgorithmName()).getProps());
}
- private Optional<ShardingStrategyConfiguration> getDatabaseShardingStrategy(final ShardingTableRuleConfiguration shardingTableRuleConfig) {
+ private Optional<ShardingStrategyConfiguration> getDatabaseShardingStrategy(final ShardingRuleConfiguration ruleConfig, final ShardingTableRuleConfiguration shardingTableRuleConfig) {
return null == shardingTableRuleConfig.getDatabaseShardingStrategy()
- ? Optional.ofNullable(shardingRuleConfig.getDefaultDatabaseShardingStrategy())
+ ? Optional.ofNullable(ruleConfig.getDefaultDatabaseShardingStrategy())
: Optional.of(shardingTableRuleConfig.getDatabaseShardingStrategy());
}
- private AlgorithmConfiguration getAlgorithmConfiguration(final String algorithmName) {
- return shardingRuleConfig.getShardingAlgorithms().get(algorithmName);
+ private AlgorithmConfiguration getAlgorithmConfiguration(final ShardingRuleConfiguration ruleConfig, final String algorithmName) {
+ return ruleConfig.getShardingAlgorithms().get(algorithmName);
}
private String getStrategyType(final ShardingStrategyConfiguration shardingStrategyConfig) {
return shardingStrategyConfig.getType();
}
- private Optional<ShardingStrategyConfiguration> getTableShardingStrategy(final ShardingStrategyConfiguration shardingStrategyConfig) {
- return null == shardingStrategyConfig ? Optional.ofNullable(shardingRuleConfig.getDefaultTableShardingStrategy()) : Optional.of(shardingStrategyConfig);
+ private Optional<ShardingStrategyConfiguration> getTableShardingStrategy(final ShardingRuleConfiguration ruleConfig, final ShardingStrategyConfiguration shardingStrategyConfig) {
+ return null == shardingStrategyConfig ? Optional.ofNullable(ruleConfig.getDefaultTableShardingStrategy()) : Optional.of(shardingStrategyConfig);
}
- private String getKeyGenerateColumn(final KeyGenerateStrategyConfiguration keyGenerateStrategyConfig) {
- return getKeyGenerateStrategyConfiguration(keyGenerateStrategyConfig).isPresent() ? getKeyGenerateStrategyConfiguration(keyGenerateStrategyConfig).get().getColumn() : "";
+ private String getKeyGenerateColumn(final ShardingRuleConfiguration ruleConfig, final KeyGenerateStrategyConfiguration keyGenerateStrategyConfig) {
+ return getKeyGenerateStrategyConfiguration(ruleConfig, keyGenerateStrategyConfig).isPresent()
+ ? getKeyGenerateStrategyConfiguration(ruleConfig, keyGenerateStrategyConfig).get().getColumn()
+ : "";
}
- private String getKeyGeneratorType(final KeyGenerateStrategyConfiguration originalKeyGenerateStrategyConfig) {
- Optional<KeyGenerateStrategyConfiguration> keyGenerateStrategyConfig = getKeyGenerateStrategyConfiguration(originalKeyGenerateStrategyConfig);
- return keyGenerateStrategyConfig.isPresent() ? shardingRuleConfig.getKeyGenerators().get(keyGenerateStrategyConfig.get().getKeyGeneratorName()).getType() : "";
+ private String getKeyGeneratorType(final ShardingRuleConfiguration ruleConfig, final KeyGenerateStrategyConfiguration originalKeyGenerateStrategyConfig) {
+ Optional<KeyGenerateStrategyConfiguration> keyGenerateStrategyConfig = getKeyGenerateStrategyConfiguration(ruleConfig, originalKeyGenerateStrategyConfig);
+ return keyGenerateStrategyConfig.isPresent() ? ruleConfig.getKeyGenerators().get(keyGenerateStrategyConfig.get().getKeyGeneratorName()).getType() : "";
}
- private String getKeyGeneratorProps(final KeyGenerateStrategyConfiguration keyGenerateStrategyConfig) {
- return getKeyGenerateStrategyConfiguration(keyGenerateStrategyConfig)
- .map(optional -> PropertiesConverter.convert(shardingRuleConfig.getKeyGenerators().get(optional.getKeyGeneratorName()).getProps())).orElse("");
+ private String getKeyGeneratorProps(final ShardingRuleConfiguration ruleConfig, final KeyGenerateStrategyConfiguration keyGenerateStrategyConfig) {
+ return getKeyGenerateStrategyConfiguration(ruleConfig, keyGenerateStrategyConfig)
+ .map(optional -> PropertiesConverter.convert(ruleConfig.getKeyGenerators().get(optional.getKeyGeneratorName()).getProps())).orElse("");
}
- private Optional<KeyGenerateStrategyConfiguration> getKeyGenerateStrategyConfiguration(final KeyGenerateStrategyConfiguration keyGenerateStrategyConfig) {
- return null == keyGenerateStrategyConfig ? Optional.ofNullable(shardingRuleConfig.getDefaultKeyGenerateStrategy()) : Optional.of(keyGenerateStrategyConfig);
+ private Optional<KeyGenerateStrategyConfiguration> getKeyGenerateStrategyConfiguration(final ShardingRuleConfiguration ruleConfig,
+ final KeyGenerateStrategyConfiguration keyGenerateStrategyConfig) {
+ return null == keyGenerateStrategyConfig ? Optional.ofNullable(ruleConfig.getDefaultKeyGenerateStrategy()) : Optional.of(keyGenerateStrategyConfig);
}
- private String getAuditorTypes(final ShardingAuditStrategyConfiguration shardingAuditStrategyConfig) {
- Optional<ShardingAuditStrategyConfiguration> auditStrategyConfig = getShardingAuditStrategyConfiguration(shardingAuditStrategyConfig);
+ private String getAuditorTypes(final ShardingRuleConfiguration ruleConfig, final ShardingAuditStrategyConfiguration shardingAuditStrategyConfig) {
+ Optional<ShardingAuditStrategyConfiguration> auditStrategyConfig = getShardingAuditStrategyConfiguration(ruleConfig, shardingAuditStrategyConfig);
Collection<String> auditorTypes = new LinkedList<>();
if (auditStrategyConfig.isPresent()) {
for (String each : auditStrategyConfig.get().getAuditorNames()) {
- auditorTypes.add(shardingRuleConfig.getAuditors().get(each).getType());
+ auditorTypes.add(ruleConfig.getAuditors().get(each).getType());
}
}
return auditorTypes.isEmpty() ? "" : String.join(",", auditorTypes);
}
- private String getAllowHintDisable(final ShardingAuditStrategyConfiguration shardingAuditStrategyConfig) {
- return getShardingAuditStrategyConfiguration(shardingAuditStrategyConfig).isPresent()
- ? Boolean.toString(getShardingAuditStrategyConfiguration(shardingAuditStrategyConfig).get().isAllowHintDisable())
+ private String getAllowHintDisable(final ShardingRuleConfiguration ruleConfig, final ShardingAuditStrategyConfiguration shardingAuditStrategyConfig) {
+ return getShardingAuditStrategyConfiguration(ruleConfig, shardingAuditStrategyConfig).isPresent()
+ ? Boolean.toString(getShardingAuditStrategyConfiguration(ruleConfig, shardingAuditStrategyConfig).get().isAllowHintDisable())
: "";
}
- private Optional<ShardingAuditStrategyConfiguration> getShardingAuditStrategyConfiguration(final ShardingAuditStrategyConfiguration shardingAuditStrategyConfig) {
- return null == shardingAuditStrategyConfig ? Optional.ofNullable(shardingRuleConfig.getDefaultAuditStrategy()) : Optional.of(shardingAuditStrategyConfig);
+ private Optional<ShardingAuditStrategyConfiguration> getShardingAuditStrategyConfiguration(final ShardingRuleConfiguration ruleConfig,
+ final ShardingAuditStrategyConfiguration shardingAuditStrategyConfig) {
+ return null == shardingAuditStrategyConfig ? Optional.ofNullable(ruleConfig.getDefaultAuditStrategy()) : Optional.of(shardingAuditStrategyConfig);
}
@Override
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRulesUsedAlgorithmExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRulesUsedAlgorithmExecutor.java
index 4d32030e182..2c9dea09830 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRulesUsedAlgorithmExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRulesUsedAlgorithmExecutor.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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;
@@ -27,42 +27,46 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedList;
-import java.util.Optional;
/**
* Show sharding table rules used algorithm executor.
*/
-public final class ShowShardingTableRulesUsedAlgorithmExecutor implements RQLExecutor<ShowShardingTableRulesUsedAlgorithmStatement> {
+public final class ShowShardingTableRulesUsedAlgorithmExecutor extends RuleAwareRQLExecutor<ShowShardingTableRulesUsedAlgorithmStatement, ShardingRule> {
+
+ public ShowShardingTableRulesUsedAlgorithmExecutor() {
+ super(ShardingRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableRulesUsedAlgorithmStatement sqlStatement) {
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- Optional<ShardingRule> rule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
- rule.ifPresent(optional -> requireResult(sqlStatement, result, optional));
- return result;
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("type", "name");
}
- private void requireResult(final ShowShardingTableRulesUsedAlgorithmStatement statement, final Collection<LocalDataQueryResultRow> result, final ShardingRule rule) {
- if (!statement.getShardingAlgorithmName().isPresent()) {
- return;
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableRulesUsedAlgorithmStatement sqlStatement, final ShardingRule rule) {
+ if (!sqlStatement.getShardingAlgorithmName().isPresent()) {
+ return Collections.emptyList();
}
- ShardingRuleConfiguration config = (ShardingRuleConfiguration) rule.getConfiguration();
- String algorithmName = statement.getShardingAlgorithmName().get();
- boolean matchDefaultDatabaseShardingStrategy = null != config.getDefaultDatabaseShardingStrategy()
- && algorithmName.equals(config.getDefaultDatabaseShardingStrategy().getShardingAlgorithmName());
- boolean matchDefaultTableShardingStrategy = null != config.getDefaultTableShardingStrategy()
- && algorithmName.equals(config.getDefaultTableShardingStrategy().getShardingAlgorithmName());
- config.getTables().forEach(each -> {
+ Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+ ShardingRuleConfiguration ruleConfig = rule.getConfiguration();
+ String algorithmName = sqlStatement.getShardingAlgorithmName().get();
+ boolean matchDefaultDatabaseShardingStrategy = null != ruleConfig.getDefaultDatabaseShardingStrategy()
+ && algorithmName.equals(ruleConfig.getDefaultDatabaseShardingStrategy().getShardingAlgorithmName());
+ boolean matchDefaultTableShardingStrategy = null != ruleConfig.getDefaultTableShardingStrategy()
+ && algorithmName.equals(ruleConfig.getDefaultTableShardingStrategy().getShardingAlgorithmName());
+ ruleConfig.getTables().forEach(each -> {
if (isMatchDatabaseShardingStrategy(each, algorithmName, matchDefaultDatabaseShardingStrategy) || isMatchTableShardingStrategy(each, algorithmName, matchDefaultTableShardingStrategy)) {
result.add(new LocalDataQueryResultRow("table", each.getLogicTable()));
}
});
- config.getAutoTables().forEach(each -> {
+ ruleConfig.getAutoTables().forEach(each -> {
if (null != each.getShardingStrategy() && algorithmName.equals(each.getShardingStrategy().getShardingAlgorithmName())) {
result.add(new LocalDataQueryResultRow("auto_table", each.getLogicTable()));
}
});
+ return result;
}
private boolean isMatchDatabaseShardingStrategy(final ShardingTableRuleConfiguration tableRuleConfig, final String algorithmName, final boolean matchDefaultDatabaseShardingStrategy) {
@@ -89,11 +93,6 @@ public final class ShowShardingTableRulesUsedAlgorithmExecutor implements RQLExe
return null == tableRuleConfig.getTableShardingStrategy() && matchDefaultTableShardingStrategy;
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("type", "name");
- }
-
@Override
public Class<ShowShardingTableRulesUsedAlgorithmStatement> getType() {
return ShowShardingTableRulesUsedAlgorithmStatement.class;
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRulesUsedAuditorExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRulesUsedAuditorExecutor.java
index 179ae89bb83..d376a561d25 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRulesUsedAuditorExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRulesUsedAuditorExecutor.java
@@ -17,49 +17,48 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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.statement.ShowShardingTableRulesUsedAuditorStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedList;
/**
* Show sharding table rules used auditor executor.
*/
-public final class ShowShardingTableRulesUsedAuditorExecutor implements RQLExecutor<ShowShardingTableRulesUsedAuditorStatement> {
+public final class ShowShardingTableRulesUsedAuditorExecutor extends RuleAwareRQLExecutor<ShowShardingTableRulesUsedAuditorStatement, ShardingRule> {
+
+ public ShowShardingTableRulesUsedAuditorExecutor() {
+ super(ShardingRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableRulesUsedAuditorStatement sqlStatement) {
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- database.getRuleMetaData().findSingleRule(ShardingRule.class).ifPresent(optional -> requireResult(sqlStatement, result, optional));
- return result;
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("type", "name");
}
- private void requireResult(final ShowShardingTableRulesUsedAuditorStatement statement, final Collection<LocalDataQueryResultRow> result, final ShardingRule rule) {
- if (!statement.getAuditorName().isPresent()) {
- return;
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableRulesUsedAuditorStatement sqlStatement, final ShardingRule rule) {
+ if (!sqlStatement.getAuditorName().isPresent()) {
+ return Collections.emptyList();
}
- ShardingRuleConfiguration config = (ShardingRuleConfiguration) rule.getConfiguration();
- config.getTables().forEach(each -> {
- if (null != each.getAuditStrategy() && each.getAuditStrategy().getAuditorNames().contains(statement.getAuditorName().get())) {
+ Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+ rule.getConfiguration().getTables().forEach(each -> {
+ if (null != each.getAuditStrategy() && each.getAuditStrategy().getAuditorNames().contains(sqlStatement.getAuditorName().get())) {
result.add(new LocalDataQueryResultRow("table", each.getLogicTable()));
}
});
- config.getAutoTables().forEach(each -> {
- if (null != each.getAuditStrategy() && each.getAuditStrategy().getAuditorNames().contains(statement.getAuditorName().get())) {
+ rule.getConfiguration().getAutoTables().forEach(each -> {
+ if (null != each.getAuditStrategy() && each.getAuditStrategy().getAuditorNames().contains(sqlStatement.getAuditorName().get())) {
result.add(new LocalDataQueryResultRow("auto_table", each.getLogicTable()));
}
});
- }
-
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("type", "name");
+ return result;
}
@Override
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRulesUsedKeyGeneratorExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRulesUsedKeyGeneratorExecutor.java
index 3e50b5e4f66..2a952b10fe3 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRulesUsedKeyGeneratorExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowShardingTableRulesUsedKeyGeneratorExecutor.java
@@ -17,51 +17,48 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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.statement.ShowShardingTableRulesUsedKeyGeneratorStatement;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.LinkedList;
-import java.util.Optional;
/**
* Show sharding table rules used key generator executor.
*/
-public final class ShowShardingTableRulesUsedKeyGeneratorExecutor implements RQLExecutor<ShowShardingTableRulesUsedKeyGeneratorStatement> {
+public final class ShowShardingTableRulesUsedKeyGeneratorExecutor extends RuleAwareRQLExecutor<ShowShardingTableRulesUsedKeyGeneratorStatement, ShardingRule> {
+
+ public ShowShardingTableRulesUsedKeyGeneratorExecutor() {
+ super(ShardingRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableRulesUsedKeyGeneratorStatement sqlStatement) {
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- Optional<ShardingRule> rule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
- rule.ifPresent(optional -> requireResult(sqlStatement, result, optional));
- return result;
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("type", "name");
}
- private void requireResult(final ShowShardingTableRulesUsedKeyGeneratorStatement statement, final Collection<LocalDataQueryResultRow> result, final ShardingRule rule) {
- if (!statement.getKeyGeneratorName().isPresent()) {
- return;
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowShardingTableRulesUsedKeyGeneratorStatement sqlStatement, final ShardingRule rule) {
+ if (!sqlStatement.getKeyGeneratorName().isPresent()) {
+ return Collections.emptyList();
}
- ShardingRuleConfiguration config = (ShardingRuleConfiguration) rule.getConfiguration();
- config.getTables().forEach(each -> {
- if (null != each.getKeyGenerateStrategy() && statement.getKeyGeneratorName().get().equals(each.getKeyGenerateStrategy().getKeyGeneratorName())) {
+ Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+ rule.getConfiguration().getTables().forEach(each -> {
+ if (null != each.getKeyGenerateStrategy() && sqlStatement.getKeyGeneratorName().get().equals(each.getKeyGenerateStrategy().getKeyGeneratorName())) {
result.add(new LocalDataQueryResultRow("table", each.getLogicTable()));
}
});
- config.getAutoTables().forEach(each -> {
- if (null != each.getKeyGenerateStrategy() && statement.getKeyGeneratorName().get().equals(each.getKeyGenerateStrategy().getKeyGeneratorName())) {
+ rule.getConfiguration().getAutoTables().forEach(each -> {
+ if (null != each.getKeyGenerateStrategy() && sqlStatement.getKeyGeneratorName().get().equals(each.getKeyGenerateStrategy().getKeyGeneratorName())) {
result.add(new LocalDataQueryResultRow("auto_table", each.getLogicTable()));
}
});
- }
-
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("type", "name");
+ return result;
}
@Override
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowUnusedShardingAlgorithmsExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowUnusedShardingAlgorithmsExecutor.java
index c0601baf548..90a66a5f9c2 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowUnusedShardingAlgorithmsExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowUnusedShardingAlgorithmsExecutor.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
import com.google.common.base.Strings;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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;
@@ -30,25 +30,23 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
-import java.util.Optional;
import java.util.Properties;
/**
* Show unused sharding algorithms executor.
*/
-public final class ShowUnusedShardingAlgorithmsExecutor implements RQLExecutor<ShowUnusedShardingAlgorithmsStatement> {
+public final class ShowUnusedShardingAlgorithmsExecutor extends RuleAwareRQLExecutor<ShowUnusedShardingAlgorithmsStatement, ShardingRule> {
+
+ public ShowUnusedShardingAlgorithmsExecutor() {
+ super(ShardingRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowUnusedShardingAlgorithmsStatement sqlStatement) {
- Optional<ShardingRule> rule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
- if (!rule.isPresent()) {
- return Collections.emptyList();
- }
- ShardingRuleConfiguration shardingRuleConfig = (ShardingRuleConfiguration) rule.get().getConfiguration();
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowUnusedShardingAlgorithmsStatement sqlStatement, final ShardingRule rule) {
+ ShardingRuleConfiguration shardingRuleConfig = rule.getConfiguration();
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
Collection<String> inUsedAlgorithms = getUsedShardingAlgorithms(shardingRuleConfig);
for (Entry<String, AlgorithmConfiguration> entry : shardingRuleConfig.getShardingAlgorithms().entrySet()) {
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowUnusedShardingAuditorsExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowUnusedShardingAuditorsExecutor.java
index 9b9ef5c4134..7ab3e7fe0b7 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowUnusedShardingAuditorsExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowUnusedShardingAuditorsExecutor.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
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;
@@ -29,24 +29,27 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.Map.Entry;
-import java.util.Optional;
/**
* Show unused sharding auditors executor.
*/
-public final class ShowUnusedShardingAuditorsExecutor implements RQLExecutor<ShowUnusedShardingAuditorsStatement> {
+public final class ShowUnusedShardingAuditorsExecutor extends RuleAwareRQLExecutor<ShowUnusedShardingAuditorsStatement, ShardingRule> {
+
+ public ShowUnusedShardingAuditorsExecutor() {
+ super(ShardingRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowUnusedShardingAuditorsStatement sqlStatement) {
- Optional<ShardingRule> rule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
- if (!rule.isPresent()) {
- return Collections.emptyList();
- }
- ShardingRuleConfiguration shardingRuleConfig = (ShardingRuleConfiguration) rule.get().getConfiguration();
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("name", "type", "props");
+ }
+
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowUnusedShardingAuditorsStatement sqlStatement, final ShardingRule rule) {
+ ShardingRuleConfiguration shardingRuleConfig = rule.getConfiguration();
Collection<String> inUsedAuditors = getUsedAuditors(shardingRuleConfig);
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
for (Entry<String, AlgorithmConfiguration> entry : shardingRuleConfig.getAuditors().entrySet()) {
@@ -68,11 +71,6 @@ public final class ShowUnusedShardingAuditorsExecutor implements RQLExecutor<Sho
return result;
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("name", "type", "props");
- }
-
@Override
public Class<ShowUnusedShardingAuditorsStatement> getType() {
return ShowUnusedShardingAuditorsStatement.class;
diff --git a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowUnusedShardingKeyGeneratorExecutor.java b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowUnusedShardingKeyGeneratorExecutor.java
index 22f11df4c2c..bf08310d8d5 100644
--- a/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowUnusedShardingKeyGeneratorExecutor.java
+++ b/features/sharding/distsql/handler/src/main/java/org/apache/shardingsphere/sharding/distsql/handler/query/ShowUnusedShardingKeyGeneratorExecutor.java
@@ -18,8 +18,7 @@
package org.apache.shardingsphere.sharding.distsql.handler.query;
import com.google.common.base.Strings;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
-import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.infra.props.PropertiesConverter;
@@ -30,33 +29,29 @@ import org.apache.shardingsphere.sharding.rule.ShardingRule;
import java.util.Arrays;
import java.util.Collection;
-import java.util.Collections;
import java.util.LinkedHashSet;
-import java.util.LinkedList;
-import java.util.Map.Entry;
-import java.util.Optional;
import java.util.Properties;
+import java.util.stream.Collectors;
/**
* Show unused sharding key generator executor.
*/
-public final class ShowUnusedShardingKeyGeneratorExecutor implements RQLExecutor<ShowUnusedShardingKeyGeneratorsStatement> {
+public final class ShowUnusedShardingKeyGeneratorExecutor extends RuleAwareRQLExecutor<ShowUnusedShardingKeyGeneratorsStatement, ShardingRule> {
+
+ public ShowUnusedShardingKeyGeneratorExecutor() {
+ super(ShardingRule.class);
+ }
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowUnusedShardingKeyGeneratorsStatement sqlStatement) {
- Optional<ShardingRule> rule = database.getRuleMetaData().findSingleRule(ShardingRule.class);
- if (!rule.isPresent()) {
- return Collections.emptyList();
- }
- ShardingRuleConfiguration shardingRuleConfig = (ShardingRuleConfiguration) rule.get().getConfiguration();
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- Collection<String> inUsedKeyGenerators = getUsedKeyGenerators(shardingRuleConfig);
- for (Entry<String, AlgorithmConfiguration> entry : shardingRuleConfig.getKeyGenerators().entrySet()) {
- if (!inUsedKeyGenerators.contains(entry.getKey())) {
- result.add(new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), buildProps(entry.getValue().getProps())));
- }
- }
- return result;
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("name", "type", "props");
+ }
+
+ @Override
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowUnusedShardingKeyGeneratorsStatement sqlStatement, final ShardingRule rule) {
+ Collection<String> inUsedKeyGenerators = getUsedKeyGenerators(rule.getConfiguration());
+ return rule.getConfiguration().getKeyGenerators().entrySet().stream().filter(entry -> !inUsedKeyGenerators.contains(entry.getKey()))
+ .map(entry -> new LocalDataQueryResultRow(entry.getKey(), entry.getValue().getType(), buildProps(entry.getValue().getProps()))).collect(Collectors.toList());
}
private Collection<String> getUsedKeyGenerators(final ShardingRuleConfiguration shardingRuleConfig) {
@@ -70,11 +65,6 @@ public final class ShowUnusedShardingKeyGeneratorExecutor implements RQLExecutor
return result;
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("name", "type", "props");
- }
-
private String buildProps(final Properties props) {
return null == props ? "" : PropertiesConverter.convert(props);
}
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowDefaultShardingStrategyExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowDefaultShardingStrategyExecutorTest.java
index 0b7ab007817..ab295757ede 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowDefaultShardingStrategyExecutorTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowDefaultShardingStrategyExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.sharding.distsql.query;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-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.RuleMetaData;
@@ -108,7 +107,7 @@ class ShowDefaultShardingStrategyExecutorTest {
assertThat(iterator.next(), is("sharding_algorithm_props"));
}
- private RuleConfiguration createRuleConfiguration1() {
+ private ShardingRuleConfiguration createRuleConfiguration1() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}"))));
result.setDefaultTableShardingStrategy(new NoneShardingStrategyConfiguration());
@@ -116,7 +115,7 @@ class ShowDefaultShardingStrategyExecutorTest {
return result;
}
- private RuleConfiguration createRuleConfiguration2() {
+ private ShardingRuleConfiguration createRuleConfiguration2() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getShardingAlgorithms().put("database_inline", new AlgorithmConfiguration("INLINE", PropertiesBuilder.build(new Property("algorithm-expression", "ds_${user_id % 2}"))));
result.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("use_id", "database_inline"));
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableReferenceRuleExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableReferenceRuleExecutorTest.java
index 3afd1b31fb8..158321dd701 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableReferenceRuleExecutorTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableReferenceRuleExecutorTest.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.sharding.distsql.query;
import org.apache.shardingsphere.distsql.handler.type.rql.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.RuleMetaData;
@@ -82,7 +81,7 @@ class ShowShardingTableReferenceRuleExecutorTest {
return result;
}
- private RuleConfiguration createRuleConfiguration() {
+ private ShardingRuleConfiguration createRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getTables().add(new ShardingTableRuleConfiguration("t_order", null));
result.getTables().add(new ShardingTableRuleConfiguration("t_order_item", null));
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java
index a3f1d30c188..2a3acf678e9 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowShardingTableRuleExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.sharding.distsql.query;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-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.RuleMetaData;
@@ -106,7 +105,7 @@ class ShowShardingTableRuleExecutorTest {
return result;
}
- private RuleConfiguration createRuleConfiguration() {
+ private ShardingRuleConfiguration createRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getTables().add(createShardingTableRuleConfiguration());
result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "database_inline"));
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAlgorithmsExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAlgorithmsExecutorTest.java
index 08ca3125069..739a81946b3 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAlgorithmsExecutorTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAlgorithmsExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.sharding.distsql.query;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-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.RuleMetaData;
@@ -76,7 +75,7 @@ class ShowUnusedShardingAlgorithmsExecutorTest {
return result;
}
- private RuleConfiguration createRuleConfiguration() {
+ private ShardingRuleConfiguration createRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getShardingAlgorithms().put("database_inline", createShardingInlineAlgorithmConfiguration());
result.getShardingAlgorithms().put("t_order_hash_mod", createShardingHashModAlgorithmConfiguration());
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAuditorsExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAuditorsExecutorTest.java
index 1761e76a9f8..c69400131cc 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAuditorsExecutorTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingAuditorsExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.sharding.distsql.query;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-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.RuleMetaData;
@@ -77,7 +76,7 @@ class ShowUnusedShardingAuditorsExecutorTest {
return result;
}
- private RuleConfiguration createRuleConfiguration() {
+ private ShardingRuleConfiguration createRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties()));
result.getAuditors().put("fixture", new AlgorithmConfiguration("FIXTURE", PropertiesBuilder.build(new Property("key", "value"))));
diff --git a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingKeyGeneratorExecutorTest.java b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingKeyGeneratorExecutorTest.java
index ed6af8a6d1e..0cbb3469157 100644
--- a/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingKeyGeneratorExecutorTest.java
+++ b/features/sharding/distsql/handler/src/test/java/org/apache/shardingsphere/sharding/distsql/query/ShowUnusedShardingKeyGeneratorExecutorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.sharding.distsql.query;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
-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.RuleMetaData;
@@ -75,7 +74,7 @@ class ShowUnusedShardingKeyGeneratorExecutorTest {
return result;
}
- private RuleConfiguration createRuleConfiguration() {
+ private ShardingRuleConfiguration createRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getKeyGenerators().put("snowflake_key_generator", createSnowflakeKeyGeneratorConfiguration());
result.getKeyGenerators().put("uuid_key_generator", new AlgorithmConfiguration("UUID", null));
diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutor.java b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rql/rule/RuleAwareRQLExecutor.java
similarity index 54%
copy from kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutor.java
copy to infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rql/rule/RuleAwareRQLExecutor.java
index e2a7c57b224..3eb764f07c4 100644
--- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutor.java
+++ b/infra/distsql-handler/src/main/java/org/apache/shardingsphere/distsql/handler/type/rql/rule/RuleAwareRQLExecutor.java
@@ -15,35 +15,33 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.single.distsql.handler.query;
+package org.apache.shardingsphere.distsql.handler.type.rql.rule;
+import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.statement.rql.RQLStatement;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
-import org.apache.shardingsphere.single.distsql.statement.rql.ShowDefaultSingleTableStorageUnitStatement;
-import org.apache.shardingsphere.single.rule.SingleRule;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import java.util.Collection;
import java.util.Collections;
/**
- * Show default single table storage unit executor.
+ * Rule aware RQL executor.
+ *
+ * @param <T> type of RQL statement
+ * @param <R> type of ShardingSphere rule
*/
-public final class ShowDefaultSingleTableStorageUnitExecutor implements RQLExecutor<ShowDefaultSingleTableStorageUnitStatement> {
+@RequiredArgsConstructor
+public abstract class RuleAwareRQLExecutor<T extends RQLStatement, R extends ShardingSphereRule> implements RQLExecutor<T> {
- @Override
- public Collection<String> getColumnNames() {
- return Collections.singletonList("storage_unit_name");
- }
+ private final Class<R> ruleClass;
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowDefaultSingleTableStorageUnitStatement sqlStatement) {
- SingleRule rule = database.getRuleMetaData().getSingleRule(SingleRule.class);
- return Collections.singleton(new LocalDataQueryResultRow(rule.getConfiguration().getDefaultDataSource().orElse("RANDOM")));
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final T sqlStatement) {
+ return database.getRuleMetaData().findSingleRule(ruleClass).map(optional -> getRows(database, sqlStatement, optional)).orElse(Collections.emptyList());
}
- @Override
- public Class<ShowDefaultSingleTableStorageUnitStatement> getType() {
- return ShowDefaultSingleTableStorageUnitStatement.class;
- }
+ protected abstract Collection<LocalDataQueryResultRow> getRows(ShardingSphereDatabase database, T sqlStatement, R rule);
}
diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutor.java
index e2a7c57b224..ce9d3232722 100644
--- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutor.java
+++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowDefaultSingleTableStorageUnitExecutor.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.single.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
import org.apache.shardingsphere.single.distsql.statement.rql.ShowDefaultSingleTableStorageUnitStatement;
@@ -29,7 +29,11 @@ import java.util.Collections;
/**
* Show default single table storage unit executor.
*/
-public final class ShowDefaultSingleTableStorageUnitExecutor implements RQLExecutor<ShowDefaultSingleTableStorageUnitStatement> {
+public final class ShowDefaultSingleTableStorageUnitExecutor extends RuleAwareRQLExecutor<ShowDefaultSingleTableStorageUnitStatement, SingleRule> {
+
+ public ShowDefaultSingleTableStorageUnitExecutor() {
+ super(SingleRule.class);
+ }
@Override
public Collection<String> getColumnNames() {
@@ -37,8 +41,7 @@ public final class ShowDefaultSingleTableStorageUnitExecutor implements RQLExecu
}
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowDefaultSingleTableStorageUnitStatement sqlStatement) {
- SingleRule rule = database.getRuleMetaData().getSingleRule(SingleRule.class);
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowDefaultSingleTableStorageUnitStatement sqlStatement, final SingleRule rule) {
return Collections.singleton(new LocalDataQueryResultRow(rule.getConfiguration().getDefaultDataSource().orElse("RANDOM")));
}
diff --git a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTableExecutor.java b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTableExecutor.java
index 455f9f61e98..d65bdca6365 100644
--- a/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTableExecutor.java
+++ b/kernel/single/distsql/handler/src/main/java/org/apache/shardingsphere/single/distsql/handler/query/ShowUnloadedSingleTableExecutor.java
@@ -17,7 +17,7 @@
package org.apache.shardingsphere.single.distsql.handler.query;
-import org.apache.shardingsphere.distsql.handler.type.rql.RQLExecutor;
+import org.apache.shardingsphere.distsql.handler.type.rql.rule.RuleAwareRQLExecutor;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.merge.result.impl.local.LocalDataQueryResultRow;
import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
@@ -31,16 +31,18 @@ import javax.sql.DataSource;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashMap;
-import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
-import java.util.Optional;
import java.util.stream.Collectors;
/**
* Show unloaded single table executor.
*/
-public final class ShowUnloadedSingleTableExecutor implements RQLExecutor<ShowUnloadedSingleTableStatement> {
+public final class ShowUnloadedSingleTableExecutor extends RuleAwareRQLExecutor<ShowUnloadedSingleTableStatement, SingleRule> {
+
+ public ShowUnloadedSingleTableExecutor() {
+ super(SingleRule.class);
+ }
@Override
public Collection<String> getColumnNames() {
@@ -48,17 +50,12 @@ public final class ShowUnloadedSingleTableExecutor implements RQLExecutor<ShowUn
}
@Override
- public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowUnloadedSingleTableStatement sqlStatement) {
- Collection<LocalDataQueryResultRow> result = new LinkedList<>();
+ public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowUnloadedSingleTableStatement sqlStatement, final SingleRule rule) {
Map<String, Collection<DataNode>> actualDataNodes = getActualDataNodes(database);
- Optional<SingleRule> singleRule = database.getRuleMetaData().findSingleRule(SingleRule.class);
- if (singleRule.isPresent()) {
- for (String each : singleRule.get().getLogicTableMapper().getTableNames()) {
- actualDataNodes.remove(each.toLowerCase());
- }
+ for (String each : rule.getLogicTableMapper().getTableNames()) {
+ actualDataNodes.remove(each.toLowerCase());
}
- actualDataNodes.forEach((key, value) -> result.add(new LocalDataQueryResultRow(key, value.iterator().next().getDataSourceName())));
- return result;
+ return actualDataNodes.entrySet().stream().map(entry -> new LocalDataQueryResultRow(entry.getKey(), entry.getValue().iterator().next().getDataSourceName())).collect(Collectors.toList());
}
private Map<String, Collection<DataNode>> getActualDataNodes(final ShardingSphereDatabase database) {
diff --git a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutorTest.java b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutorTest.java
index fe94a351bdf..784d19c23c8 100644
--- a/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutorTest.java
+++ b/kernel/single/distsql/handler/src/test/java/org/apache/shardingsphere/single/distsql/handler/query/ShowSingleTableExecutorTest.java
@@ -56,8 +56,8 @@ class ShowSingleTableExecutorTest {
@BeforeEach
void before() {
Map<String, Collection<DataNode>> singleTableDataNodeMap = new HashMap<>();
- singleTableDataNodeMap.put("t_order", Collections.singletonList(new DataNode("ds_1", "t_order")));
- singleTableDataNodeMap.put("t_order_item", Collections.singletonList(new DataNode("ds_2", "t_order_item")));
+ singleTableDataNodeMap.put("t_order", Collections.singleton(new DataNode("ds_1", "t_order")));
+ singleTableDataNodeMap.put("t_order_item", Collections.singleton(new DataNode("ds_2", "t_order_item")));
RuleMetaData ruleMetaData = new RuleMetaData(new LinkedList<>(Collections.singleton(mockSingleRule(singleTableDataNodeMap))));
when(database.getRuleMetaData()).thenReturn(ruleMetaData);
}
@@ -79,8 +79,8 @@ class ShowSingleTableExecutorTest {
@Test
void assertGetRowDataMultipleRules() {
Map<String, Collection<DataNode>> singleTableDataNodeMap = new HashMap<>();
- singleTableDataNodeMap.put("t_order_multiple", Collections.singletonList(new DataNode("ds_1_multiple", "t_order_multiple")));
- singleTableDataNodeMap.put("t_order_item_multiple", Collections.singletonList(new DataNode("ds_2_multiple", "t_order_item_multiple")));
+ singleTableDataNodeMap.put("t_order_multiple", Collections.singleton(new DataNode("ds_1_multiple", "t_order_multiple")));
+ singleTableDataNodeMap.put("t_order_item_multiple", Collections.singleton(new DataNode("ds_2_multiple", "t_order_item_multiple")));
addShardingSphereRule(mockSingleRule(singleTableDataNodeMap));
RQLExecutor<ShowSingleTableStatement> executor = new ShowSingleTableExecutor();
Collection<LocalDataQueryResultRow> actual = executor.getRows(database, mock(ShowSingleTableStatement.class));
diff --git a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowRulesUsedStorageUnitExecutor.java b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowRulesUsedStorageUnitExecutor.java
index e0b6b453f53..ea71d5f9d15 100644
--- a/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowRulesUsedStorageUnitExecutor.java
+++ b/proxy/backend/core/src/main/java/org/apache/shardingsphere/proxy/backend/handler/distsql/rql/rule/ShowRulesUsedStorageUnitExecutor.java
@@ -58,6 +58,11 @@ public final class ShowRulesUsedStorageUnitExecutor implements RQLExecutor<ShowR
private static final String MASK = "mask";
+ @Override
+ public Collection<String> getColumnNames() {
+ return Arrays.asList("type", "name");
+ }
+
@Override
public Collection<LocalDataQueryResultRow> getRows(final ShardingSphereDatabase database, final ShowRulesUsedStorageUnitStatement sqlStatement) {
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
@@ -78,7 +83,7 @@ public final class ShowRulesUsedStorageUnitExecutor implements RQLExecutor<ShowR
return Collections.emptyList();
}
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- ShardingRuleConfiguration config = (ShardingRuleConfiguration) rule.get().getConfiguration();
+ ShardingRuleConfiguration config = rule.get().getConfiguration();
for (ShardingAutoTableRuleConfiguration each : config.getAutoTables()) {
result.add(buildRow(SHARDING, each.getLogicTable()));
}
@@ -94,7 +99,7 @@ public final class ShowRulesUsedStorageUnitExecutor implements RQLExecutor<ShowR
return Collections.emptyList();
}
Collection<LocalDataQueryResultRow> result = new LinkedList<>();
- ReadwriteSplittingRuleConfiguration config = (ReadwriteSplittingRuleConfiguration) rule.get().getConfiguration();
+ ReadwriteSplittingRuleConfiguration config = rule.get().getConfiguration();
for (ReadwriteSplittingDataSourceRuleConfiguration each : config.getDataSources()) {
if (each.getWriteDataSourceName().equalsIgnoreCase(resourceName)) {
result.add(buildRow(READWRITE_SPLITTING, each.getName()));
@@ -122,7 +127,7 @@ public final class ShowRulesUsedStorageUnitExecutor implements RQLExecutor<ShowR
if (!rule.isPresent()) {
return Collections.emptyList();
}
- ShadowRuleConfiguration config = (ShadowRuleConfiguration) rule.get().getConfiguration();
+ ShadowRuleConfiguration config = rule.get().getConfiguration();
return config.getDataSources().stream()
.filter(each -> each.getShadowDataSourceName().equalsIgnoreCase(resourceName) || each.getProductionDataSourceName().equalsIgnoreCase(resourceName))
.map(each -> buildRow(SHADOW, each.getName())).collect(Collectors.toList());
@@ -133,7 +138,7 @@ public final class ShowRulesUsedStorageUnitExecutor implements RQLExecutor<ShowR
if (!rule.isPresent()) {
return Collections.emptyList();
}
- MaskRuleConfiguration config = (MaskRuleConfiguration) rule.get().getConfiguration();
+ MaskRuleConfiguration config = rule.get().getConfiguration();
return config.getTables().stream().map(each -> buildRow(MASK, each.getName())).collect(Collectors.toList());
}
@@ -141,11 +146,6 @@ public final class ShowRulesUsedStorageUnitExecutor implements RQLExecutor<ShowR
return new LocalDataQueryResultRow(type, name);
}
- @Override
- public Collection<String> getColumnNames() {
- return Arrays.asList("type", "name");
- }
-
@Override
public Class<ShowRulesUsedStorageUnitStatement> getType() {
return ShowRulesUsedStorageUnitStatement.class;