This is an automated email from the ASF dual-hosted git repository. menghaoran 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 aaeed27 Add ResourceAware to simplify rule builder (#8132) aaeed27 is described below commit aaeed27cacceb6c7fac3811eaaf1c83c57dd87f7 Author: Liang Zhang <terrym...@163.com> AuthorDate: Wed Nov 11 16:08:15 2020 +0800 Add ResourceAware to simplify rule builder (#8132) * Add generic of ShardingRouteEngineFactory * Add generic of ShardingTableBroadcastRoutingEngine * Add SingleTableRule * Add ResourceAware * Add ResourceAware * Remove dataSourceNames parameter in ShardingSphereRuleBuilder * Refactor ShardingRule --- .../AlgorithmProvidedEncryptRuleBuilder.java | 4 +- .../encrypt/rule/builder/EncryptRuleBuilder.java | 4 +- .../AlgorithmProvidedEncryptRuleBuilderTest.java | 2 +- .../rule/builder/EncryptRuleBuilderTest.java | 2 +- .../EncryptSQLRewriterParameterizedTest.java | 8 ++- .../biulder/AlgorithmProvidedHARuleBuilder.java | 8 +-- .../ha/rule/biulder/HARuleBuilder.java | 6 +- .../AlgorithmProvidedHARuleBuilderTest.java | 2 +- .../ha/rule/biulder/HARuleBuilderTest.java | 2 +- .../AlgorithmProvidedReplicaQueryRuleBuilder.java | 4 +- .../rule/biulder/ReplicaQueryRuleBuilder.java | 6 +- ...gorithmProvidedReplicaQueryRuleBuilderTest.java | 2 +- .../rule/biulder/ReplicaQueryRuleBuilderTest.java | 2 +- .../shadow/rule/builder/ShadowRuleBuilder.java | 4 +- .../shadow/rule/builder/ShadowRuleBuilderTest.java | 3 +- .../sharding/metadata/SingleTableRuleLoader.java | 77 ++++++++++++++++++++++ .../shardingsphere/sharding/rule/ShardingRule.java | 14 ++-- .../sharding/rule/SingleTableRule.java | 29 ++++---- .../AlgorithmProvidedShardingRuleBuilder.java | 19 ++++-- .../sharding/rule/builder/ShardingRuleBuilder.java | 19 ++++-- .../AlgorithmProvidedShardingRuleBuilderTest.java | 7 +- .../rule/builder/ShardingRuleBuilderTest.java | 7 +- .../MixSQLRewriterParameterizedTest.java | 10 +-- .../ShardingSQLRewriterParameterizedTest.java | 10 +-- .../engine/type/ShardingRouteEngineFactory.java | 2 +- .../ShardingTableBroadcastRoutingEngine.java | 2 +- .../rule/builder/ShardingSphereRuleBuilder.java | 5 +- .../rule/builder/ShardingSphereRulesBuilder.java | 26 ++++++-- .../infra/rule/builder/aware/ResourceAware.java} | 37 ++++++----- .../infra/rule/ShardingSphereRulesBuilderTest.java | 6 +- .../fixture/TestShardingSphereRuleBuilder.java | 8 +-- .../context/metadata/MetaDataContextsBuilder.java | 2 +- .../infra/context/fixture/FixtureRuleBuilder.java | 4 +- 33 files changed, 225 insertions(+), 118 deletions(-) diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/AlgorithmProvidedEncryptRuleBuilder.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/AlgorithmProvidedEncryptRuleBuilder.java index 0ea15e2..8437380 100644 --- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/AlgorithmProvidedEncryptRuleBuilder.java +++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/AlgorithmProvidedEncryptRuleBuilder.java @@ -22,15 +22,13 @@ import org.apache.shardingsphere.encrypt.constant.EncryptOrder; import org.apache.shardingsphere.encrypt.rule.EncryptRule; import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; -import java.util.Collection; - /** * Algorithm provided encrypt rule builder. */ public final class AlgorithmProvidedEncryptRuleBuilder implements ShardingSphereRuleBuilder<EncryptRule, AlgorithmProvidedEncryptRuleConfiguration> { @Override - public EncryptRule build(final AlgorithmProvidedEncryptRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) { + public EncryptRule build(final AlgorithmProvidedEncryptRuleConfiguration ruleConfig) { return new EncryptRule(ruleConfig); } diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilder.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilder.java index 31fb9ec..06e8752 100644 --- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilder.java +++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/main/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilder.java @@ -22,15 +22,13 @@ import org.apache.shardingsphere.encrypt.constant.EncryptOrder; import org.apache.shardingsphere.encrypt.rule.EncryptRule; import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; -import java.util.Collection; - /** * Encrypt rule builder. */ public final class EncryptRuleBuilder implements ShardingSphereRuleBuilder<EncryptRule, EncryptRuleConfiguration> { @Override - public EncryptRule build(final EncryptRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) { + public EncryptRule build(final EncryptRuleConfiguration ruleConfig) { return new EncryptRule(ruleConfig); } diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/rule/builder/AlgorithmProvidedEncryptRuleBuilderTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/rule/builder/AlgorithmProvidedEncryptRuleBuilderTest.java index 9f9c579..82a8e12 100644 --- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/rule/builder/AlgorithmProvidedEncryptRuleBuilderTest.java +++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/rule/builder/AlgorithmProvidedEncryptRuleBuilderTest.java @@ -41,6 +41,6 @@ public final class AlgorithmProvidedEncryptRuleBuilderTest { public void assertBuild() { AlgorithmProvidedEncryptRuleConfiguration ruleConfig = mock(AlgorithmProvidedEncryptRuleConfiguration.class); ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig); - assertThat(builder.build(ruleConfig, Collections.emptyList()), instanceOf(EncryptRule.class)); + assertThat(builder.build(ruleConfig), instanceOf(EncryptRule.class)); } } diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilderTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilderTest.java index f71ba36..df8d84b 100644 --- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilderTest.java +++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-common/src/test/java/org/apache/shardingsphere/encrypt/rule/builder/EncryptRuleBuilderTest.java @@ -41,6 +41,6 @@ public final class EncryptRuleBuilderTest { public void assertBuild() { EncryptRuleConfiguration ruleConfig = mock(EncryptRuleConfiguration.class); ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig); - assertThat(builder.build(ruleConfig, Collections.emptyList()), instanceOf(EncryptRule.class)); + assertThat(builder.build(ruleConfig), instanceOf(EncryptRule.class)); } } diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameterized/EncryptSQLRewriterParameterizedTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameterized/EncryptSQLRewriterParameterizedTest.java index 533d365..da154ff 100644 --- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameterized/EncryptSQLRewriterParameterizedTest.java +++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-rewrite/src/test/java/org/apache/shardingsphere/encrypt/rewrite/parameterized/EncryptSQLRewriterParameterizedTest.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.infra.binder.LogicSQL; import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory; import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext; import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; +import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource; import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData; @@ -70,9 +71,10 @@ public final class EncryptSQLRewriterParameterizedTest extends AbstractSQLRewrit @Override protected Collection<SQLRewriteUnit> createSQLRewriteUnits() throws IOException { YamlRootRuleConfigurations ruleConfigurations = createRuleConfigurations(); - Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build( - new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(ruleConfigurations.getRules()), ruleConfigurations.getDataSources().keySet()); - SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType()); + String databaseType = null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType(); + Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations( + ruleConfigurations.getRules()), DatabaseTypeRegistry.getTrunkDatabaseType(databaseType), ruleConfigurations.getDataSources()); + SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(databaseType); ShardingSphereSchema schema = mockSchema(); ConfigurationProperties props = new ConfigurationProperties(ruleConfigurations.getProps()); SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance( diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/biulder/AlgorithmProvidedHARuleBuilder.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/biulder/AlgorithmProvidedHARuleBuilder.java index 83aa08b..dc5df97 100644 --- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/biulder/AlgorithmProvidedHARuleBuilder.java +++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/biulder/AlgorithmProvidedHARuleBuilder.java @@ -17,12 +17,10 @@ package org.apache.shardingsphere.ha.rule.biulder; -import org.apache.shardingsphere.ha.rule.HARule; -import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; import org.apache.shardingsphere.ha.algorithm.config.AlgorithmProvidedHARuleConfiguration; import org.apache.shardingsphere.ha.constant.HAOrder; - -import java.util.Collection; +import org.apache.shardingsphere.ha.rule.HARule; +import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; /** * Algorithm provided HA rule builder. @@ -30,7 +28,7 @@ import java.util.Collection; public final class AlgorithmProvidedHARuleBuilder implements ShardingSphereRuleBuilder<HARule, AlgorithmProvidedHARuleConfiguration> { @Override - public HARule build(final AlgorithmProvidedHARuleConfiguration ruleConfig, final Collection<String> dataSourceNames) { + public HARule build(final AlgorithmProvidedHARuleConfiguration ruleConfig) { return new HARule(ruleConfig); } diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilder.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilder.java index ad11d17..74a4504 100644 --- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilder.java +++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/main/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilder.java @@ -19,10 +19,8 @@ package org.apache.shardingsphere.ha.rule.biulder; import org.apache.shardingsphere.ha.api.config.HARuleConfiguration; import org.apache.shardingsphere.ha.constant.HAOrder; -import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; import org.apache.shardingsphere.ha.rule.HARule; - -import java.util.Collection; +import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; /** * HA rule builder. @@ -30,7 +28,7 @@ import java.util.Collection; public final class HARuleBuilder implements ShardingSphereRuleBuilder<HARule, HARuleConfiguration> { @Override - public HARule build(final HARuleConfiguration ruleConfig, final Collection<String> dataSourceNames) { + public HARule build(final HARuleConfiguration ruleConfig) { return new HARule(ruleConfig); } diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/AlgorithmProvidedHARuleBuilderTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/AlgorithmProvidedHARuleBuilderTest.java index f05e17f..670073f 100644 --- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/AlgorithmProvidedHARuleBuilderTest.java +++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/AlgorithmProvidedHARuleBuilderTest.java @@ -47,6 +47,6 @@ public final class AlgorithmProvidedHARuleBuilderTest { when(algorithmProvidedRuleConfig.getDataSources()).thenReturn(Collections.singletonList(ruleConfig)); ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices( Collections.singletonList(algorithmProvidedRuleConfig), ShardingSphereRuleBuilder.class).get(algorithmProvidedRuleConfig); - assertThat(builder.build(algorithmProvidedRuleConfig, Collections.emptyList()), instanceOf(HARule.class)); + assertThat(builder.build(algorithmProvidedRuleConfig), instanceOf(HARule.class)); } } diff --git a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilderTest.java b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilderTest.java index da55ce1..01b107e 100644 --- a/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilderTest.java +++ b/shardingsphere-features/shardingsphere-ha/shardingsphere-ha-common/src/test/java/org/apache/shardingsphere/ha/rule/biulder/HARuleBuilderTest.java @@ -46,6 +46,6 @@ public final class HARuleBuilderTest { "name", "primaryDataSourceName", Collections.singletonList("name"), "loadBalancerName", true); when(ruleConfig.getDataSources()).thenReturn(Collections.singletonList(dataSourceRuleConfig)); ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig); - assertThat(builder.build(ruleConfig, Collections.emptyList()), instanceOf(HARule.class)); + assertThat(builder.build(ruleConfig), instanceOf(HARule.class)); } } diff --git a/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/rule/biulder/AlgorithmProvidedReplicaQueryRuleBuilder.java b/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/rule/biulder/AlgorithmProvidedReplicaQueryRuleBuilder.java index a834951..d9e33cf 100644 --- a/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/rule/biulder/AlgorithmProvidedReplicaQueryRuleBuilder.java +++ b/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/rule/biulder/AlgorithmProvidedReplicaQueryRuleBuilder.java @@ -22,15 +22,13 @@ import org.apache.shardingsphere.replicaquery.algorithm.config.AlgorithmProvided import org.apache.shardingsphere.replicaquery.constant.ReplicaQueryOrder; import org.apache.shardingsphere.replicaquery.rule.ReplicaQueryRule; -import java.util.Collection; - /** * Algorithm provided replica query rule builder. */ public final class AlgorithmProvidedReplicaQueryRuleBuilder implements ShardingSphereRuleBuilder<ReplicaQueryRule, AlgorithmProvidedReplicaQueryRuleConfiguration> { @Override - public ReplicaQueryRule build(final AlgorithmProvidedReplicaQueryRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) { + public ReplicaQueryRule build(final AlgorithmProvidedReplicaQueryRuleConfiguration ruleConfig) { return new ReplicaQueryRule(ruleConfig); } diff --git a/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/rule/biulder/ReplicaQueryRuleBuilder.java b/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/rule/biulder/ReplicaQueryRuleBuilder.java index 8cf2043..56ab6e3 100644 --- a/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/rule/biulder/ReplicaQueryRuleBuilder.java +++ b/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/main/java/org/apache/shardingsphere/replicaquery/rule/biulder/ReplicaQueryRuleBuilder.java @@ -17,20 +17,18 @@ package org.apache.shardingsphere.replicaquery.rule.biulder; +import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; import org.apache.shardingsphere.replicaquery.api.config.ReplicaQueryRuleConfiguration; import org.apache.shardingsphere.replicaquery.constant.ReplicaQueryOrder; -import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; import org.apache.shardingsphere.replicaquery.rule.ReplicaQueryRule; -import java.util.Collection; - /** * Replica query rule builder. */ public final class ReplicaQueryRuleBuilder implements ShardingSphereRuleBuilder<ReplicaQueryRule, ReplicaQueryRuleConfiguration> { @Override - public ReplicaQueryRule build(final ReplicaQueryRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) { + public ReplicaQueryRule build(final ReplicaQueryRuleConfiguration ruleConfig) { return new ReplicaQueryRule(ruleConfig); } diff --git a/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/test/java/org/apache/shardingsphere/replicaquery/rule/biulder/AlgorithmProvidedReplicaQueryRuleBuilderTest.java b/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/test/java/org/apache/shardingsphere/replicaquery/rule/biulder/AlgorithmProvidedReplicaQueryRuleBuilderTest.java index 8c7afdd..2c52bc1 100644 --- a/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/test/java/org/apache/shardingsphere/replicaquery/rule/biulder/AlgorithmProvidedReplicaQueryRuleBuilderTest.java +++ b/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/test/java/org/apache/shardingsphere/replicaquery/rule/biulder/AlgorithmProvidedReplicaQueryRuleBuilderTest.java @@ -47,6 +47,6 @@ public final class AlgorithmProvidedReplicaQueryRuleBuilderTest { when(algorithmProvidedRuleConfig.getDataSources()).thenReturn(Collections.singletonList(ruleConfig)); ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices( Collections.singletonList(algorithmProvidedRuleConfig), ShardingSphereRuleBuilder.class).get(algorithmProvidedRuleConfig); - assertThat(builder.build(algorithmProvidedRuleConfig, Collections.emptyList()), instanceOf(ReplicaQueryRule.class)); + assertThat(builder.build(algorithmProvidedRuleConfig), instanceOf(ReplicaQueryRule.class)); } } diff --git a/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/test/java/org/apache/shardingsphere/replicaquery/rule/biulder/ReplicaQueryRuleBuilderTest.java b/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/test/java/org/apache/shardingsphere/replicaquery/rule/biulder/ReplicaQueryRuleBuilderTest.java index 80984d3..78b3356 100644 --- a/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/test/java/org/apache/shardingsphere/replicaquery/rule/biulder/ReplicaQueryRuleBuilderTest.java +++ b/shardingsphere-features/shardingsphere-replica-query/shardingsphere-replica-query-common/src/test/java/org/apache/shardingsphere/replicaquery/rule/biulder/ReplicaQueryRuleBuilderTest.java @@ -46,6 +46,6 @@ public final class ReplicaQueryRuleBuilderTest { "name", "primaryDataSourceName", Collections.singletonList("name"), "loadBalancerName"); when(ruleConfig.getDataSources()).thenReturn(Collections.singletonList(dataSourceRuleConfig)); ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig); - assertThat(builder.build(ruleConfig, Collections.emptyList()), instanceOf(ReplicaQueryRule.class)); + assertThat(builder.build(ruleConfig), instanceOf(ReplicaQueryRule.class)); } } diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-common/src/main/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilder.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-common/src/main/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilder.java index 3d631f5..5d04615 100644 --- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-common/src/main/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilder.java +++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-common/src/main/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilder.java @@ -22,15 +22,13 @@ import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration; import org.apache.shardingsphere.shadow.constant.ShadowOrder; import org.apache.shardingsphere.shadow.rule.ShadowRule; -import java.util.Collection; - /** * Shadow rule builder. */ public final class ShadowRuleBuilder implements ShardingSphereRuleBuilder<ShadowRule, ShadowRuleConfiguration> { @Override - public ShadowRule build(final ShadowRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) { + public ShadowRule build(final ShadowRuleConfiguration ruleConfig) { return new ShadowRule(ruleConfig); } diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-common/src/test/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilderTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-common/src/test/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilderTest.java index 79c83cb..2a2bee2 100644 --- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-common/src/test/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilderTest.java +++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-common/src/test/java/org/apache/shardingsphere/shadow/rule/builder/ShadowRuleBuilderTest.java @@ -36,10 +36,11 @@ public final class ShadowRuleBuilderTest { ShardingSphereServiceLoader.register(ShardingSphereRuleBuilder.class); } + @SuppressWarnings({"rawtypes", "unchecked"}) @Test public void assertBuild() { ShadowRuleConfiguration ruleConfig = mock(ShadowRuleConfiguration.class); ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig); - assertThat(builder.build(ruleConfig, Collections.emptyList()), instanceOf(ShadowRule.class)); + assertThat(builder.build(ruleConfig), instanceOf(ShadowRule.class)); } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/SingleTableRuleLoader.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/SingleTableRuleLoader.java new file mode 100644 index 0000000..272f426 --- /dev/null +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/SingleTableRuleLoader.java @@ -0,0 +1,77 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.sharding.metadata; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; +import org.apache.shardingsphere.infra.database.type.DatabaseType; +import org.apache.shardingsphere.infra.metadata.schema.builder.loader.SchemaMetaDataLoader; +import org.apache.shardingsphere.sharding.rule.ShardingRule; +import org.apache.shardingsphere.sharding.rule.SingleTableRule; + +import javax.sql.DataSource; +import java.sql.SQLException; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Map.Entry; + +/** + * Single table rule loader. + */ +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class SingleTableRuleLoader { + + /** + * Load single table rules. + * + * @param databaseType database type + * @param dataSourceMap data source map + * @param shardingRule sharding rule + * @return single table rule map + * @throws SQLException SQL exception + */ + @SuppressWarnings("CollectionWithoutInitialCapacity") + public static Map<String, SingleTableRule> load(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap, final ShardingRule shardingRule) throws SQLException { + Collection<String> excludedTables = getExcludedTables(shardingRule); + Map<String, SingleTableRule> result = new HashMap<>(); + for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) { + result.putAll(load(databaseType, entry.getKey(), entry.getValue(), excludedTables)); + } + return result; + } + + private static Map<String, SingleTableRule> load(final DatabaseType databaseType, + final String dataSourceName, final DataSource dataSource, final Collection<String> excludedTables) throws SQLException { + Collection<String> tables = SchemaMetaDataLoader.loadAllTableNames(dataSource, databaseType); + Map<String, SingleTableRule> result = new HashMap<>(tables.size(), 1); + for (String each : tables) { + if (!excludedTables.contains(each)) { + result.put(each, new SingleTableRule(each, dataSourceName)); + } + } + return result; + } + + private static Collection<String> getExcludedTables(final ShardingRule shardingRule) { + Collection<String> result = new HashSet<>(shardingRule.getTables()); + result.addAll(shardingRule.getAllActualTables()); + return result; + } +} diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java index cd71219..647f2e9 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/ShardingRule.java @@ -24,6 +24,7 @@ import lombok.AccessLevel; import lombok.Getter; import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory; import org.apache.shardingsphere.infra.config.exception.ShardingSphereConfigurationException; +import org.apache.shardingsphere.infra.database.type.DatabaseType; import org.apache.shardingsphere.infra.datanode.DataNode; import org.apache.shardingsphere.infra.rule.type.DataNodeContainedRule; import org.apache.shardingsphere.infra.rule.type.TableContainedRule; @@ -43,6 +44,7 @@ import org.apache.shardingsphere.sharding.api.sharding.ShardingAutoTableAlgorith import org.apache.shardingsphere.sharding.spi.KeyGenerateAlgorithm; import org.apache.shardingsphere.sharding.spi.ShardingAlgorithm; +import javax.sql.DataSource; import java.util.Collection; import java.util.LinkedHashMap; import java.util.LinkedHashSet; @@ -84,10 +86,10 @@ public final class ShardingRule implements DataNodeContainedRule, TableContained private final KeyGenerateAlgorithm defaultKeyGenerateAlgorithm; - public ShardingRule(final ShardingRuleConfiguration config, final Collection<String> dataSourceNames) { + public ShardingRule(final ShardingRuleConfiguration config, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) { Preconditions.checkArgument(null != config, "Sharding rule configuration cannot be null."); - Preconditions.checkArgument(null != dataSourceNames && !dataSourceNames.isEmpty(), "Data sources cannot be empty."); - this.dataSourceNames = getDataSourceNames(config.getTables(), dataSourceNames); + Preconditions.checkArgument(null != dataSourceMap && !dataSourceMap.isEmpty(), "Data sources cannot be empty."); + dataSourceNames = getDataSourceNames(config.getTables(), dataSourceMap.keySet()); config.getShardingAlgorithms().forEach((key, value) -> shardingAlgorithms.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value, ShardingAlgorithm.class))); config.getKeyGenerators().forEach((key, value) -> keyGenerators.put(key, ShardingSphereAlgorithmFactory.createAlgorithm(value, KeyGenerateAlgorithm.class))); tableRules = new LinkedList<>(createTableRules(config.getTables(), config.getDefaultKeyGenerateStrategy())); @@ -100,10 +102,10 @@ public final class ShardingRule implements DataNodeContainedRule, TableContained ? TypedSPIRegistry.getRegisteredService(KeyGenerateAlgorithm.class) : keyGenerators.get(config.getDefaultKeyGenerateStrategy().getKeyGeneratorName()); } - public ShardingRule(final AlgorithmProvidedShardingRuleConfiguration config, final Collection<String> dataSourceNames) { + public ShardingRule(final AlgorithmProvidedShardingRuleConfiguration config, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) { Preconditions.checkArgument(null != config, "Sharding rule configuration cannot be null."); - Preconditions.checkArgument(null != dataSourceNames && !dataSourceNames.isEmpty(), "Data sources cannot be empty."); - this.dataSourceNames = getDataSourceNames(config.getTables(), dataSourceNames); + Preconditions.checkArgument(null != dataSourceMap && !dataSourceMap.isEmpty(), "Data sources cannot be empty."); + dataSourceNames = getDataSourceNames(config.getTables(), dataSourceMap.keySet()); shardingAlgorithms.putAll(config.getShardingAlgorithms()); keyGenerators.putAll(config.getKeyGenerators()); tableRules = new LinkedList<>(createTableRules(config.getTables(), config.getDefaultKeyGenerateStrategy())); diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/fixture/FixtureRuleBuilder.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/SingleTableRule.java similarity index 55% copy from shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/fixture/FixtureRuleBuilder.java copy to shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/SingleTableRule.java index 92b2b01..5ec522d 100644 --- a/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/fixture/FixtureRuleBuilder.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/SingleTableRule.java @@ -15,26 +15,19 @@ * limitations under the License. */ -package org.apache.shardingsphere.infra.context.fixture; +package org.apache.shardingsphere.sharding.rule; -import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; +import lombok.Getter; +import lombok.RequiredArgsConstructor; -import java.util.Collection; - -public final class FixtureRuleBuilder implements ShardingSphereRuleBuilder<FixtureRule, FixtureRuleConfiguration> { - - @Override - public FixtureRule build(final FixtureRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) { - return new FixtureRule(); - } +/** + * Single table rule. + */ +@RequiredArgsConstructor +@Getter +public final class SingleTableRule { - @Override - public int getOrder() { - return 0; - } + private final String tableName; - @Override - public Class<FixtureRuleConfiguration> getTypeClass() { - return FixtureRuleConfiguration.class; - } + private final String dataSourceName; } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/builder/AlgorithmProvidedShardingRuleBuilder.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/builder/AlgorithmProvidedShardingRuleBuilder.java index 6fe89c8..71bc0b7 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/builder/AlgorithmProvidedShardingRuleBuilder.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/builder/AlgorithmProvidedShardingRuleBuilder.java @@ -17,21 +17,30 @@ package org.apache.shardingsphere.sharding.rule.builder; +import lombok.Setter; +import org.apache.shardingsphere.infra.database.type.DatabaseType; import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; +import org.apache.shardingsphere.infra.rule.builder.aware.ResourceAware; +import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration; import org.apache.shardingsphere.sharding.constant.ShardingOrder; import org.apache.shardingsphere.sharding.rule.ShardingRule; -import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration; -import java.util.Collection; +import javax.sql.DataSource; +import java.util.Map; /** * Algorithm provided sharding rule builder. */ -public final class AlgorithmProvidedShardingRuleBuilder implements ShardingSphereRuleBuilder<ShardingRule, AlgorithmProvidedShardingRuleConfiguration> { +@Setter +public final class AlgorithmProvidedShardingRuleBuilder implements ShardingSphereRuleBuilder<ShardingRule, AlgorithmProvidedShardingRuleConfiguration>, ResourceAware { + + private DatabaseType databaseType; + + private Map<String, DataSource> dataSourceMap; @Override - public ShardingRule build(final AlgorithmProvidedShardingRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) { - return new ShardingRule(ruleConfig, dataSourceNames); + public ShardingRule build(final AlgorithmProvidedShardingRuleConfiguration ruleConfig) { + return new ShardingRule(ruleConfig, databaseType, dataSourceMap); } @Override diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilder.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilder.java index b9bb59d..7ef4e26 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilder.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilder.java @@ -17,21 +17,30 @@ package org.apache.shardingsphere.sharding.rule.builder; -import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; +import lombok.Setter; +import org.apache.shardingsphere.infra.database.type.DatabaseType; import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; +import org.apache.shardingsphere.infra.rule.builder.aware.ResourceAware; +import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.constant.ShardingOrder; import org.apache.shardingsphere.sharding.rule.ShardingRule; -import java.util.Collection; +import javax.sql.DataSource; +import java.util.Map; /** * Sharding rule builder. */ -public final class ShardingRuleBuilder implements ShardingSphereRuleBuilder<ShardingRule, ShardingRuleConfiguration> { +@Setter +public final class ShardingRuleBuilder implements ShardingSphereRuleBuilder<ShardingRule, ShardingRuleConfiguration>, ResourceAware { + + private DatabaseType databaseType; + + private Map<String, DataSource> dataSourceMap; @Override - public ShardingRule build(final ShardingRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) { - return new ShardingRule(ruleConfig, dataSourceNames); + public ShardingRule build(final ShardingRuleConfiguration ruleConfig) { + return new ShardingRule(ruleConfig, databaseType, dataSourceMap); } @Override diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/AlgorithmProvidedShardingRuleBuilderTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/AlgorithmProvidedShardingRuleBuilderTest.java index 4578e8b..d9496df 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/AlgorithmProvidedShardingRuleBuilderTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/AlgorithmProvidedShardingRuleBuilderTest.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.rule.builder; +import org.apache.shardingsphere.infra.database.type.DatabaseType; import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry; @@ -24,6 +25,7 @@ import org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShar import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.junit.Test; +import javax.sql.DataSource; import java.util.Collections; import static org.hamcrest.CoreMatchers.instanceOf; @@ -36,10 +38,13 @@ public final class AlgorithmProvidedShardingRuleBuilderTest { ShardingSphereServiceLoader.register(ShardingSphereRuleBuilder.class); } + @SuppressWarnings({"rawtypes", "unchecked"}) @Test public void assertBuild() { AlgorithmProvidedShardingRuleConfiguration ruleConfig = mock(AlgorithmProvidedShardingRuleConfiguration.class); ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig); - assertThat(builder.build(ruleConfig, Collections.singletonList("name")), instanceOf(ShardingRule.class)); + ((AlgorithmProvidedShardingRuleBuilder) builder).setDatabaseType(mock(DatabaseType.class)); + ((AlgorithmProvidedShardingRuleBuilder) builder).setDataSourceMap(Collections.singletonMap("name", mock(DataSource.class))); + assertThat(builder.build(ruleConfig), instanceOf(ShardingRule.class)); } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java index 925bfb2..36b0113 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/test/java/org/apache/shardingsphere/sharding/rule/builder/ShardingRuleBuilderTest.java @@ -17,6 +17,7 @@ package org.apache.shardingsphere.sharding.rule.builder; +import org.apache.shardingsphere.infra.database.type.DatabaseType; import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry; @@ -24,6 +25,7 @@ import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration; import org.apache.shardingsphere.sharding.rule.ShardingRule; import org.junit.Test; +import javax.sql.DataSource; import java.util.Collections; import static org.hamcrest.CoreMatchers.instanceOf; @@ -36,10 +38,13 @@ public final class ShardingRuleBuilderTest { ShardingSphereServiceLoader.register(ShardingSphereRuleBuilder.class); } + @SuppressWarnings({"rawtypes", "unchecked"}) @Test public void assertBuild() { ShardingRuleConfiguration ruleConfig = mock(ShardingRuleConfiguration.class); ShardingSphereRuleBuilder builder = OrderedSPIRegistry.getRegisteredServices(Collections.singletonList(ruleConfig), ShardingSphereRuleBuilder.class).get(ruleConfig); - assertThat(builder.build(ruleConfig, Collections.singletonList("name")), instanceOf(ShardingRule.class)); + ((ShardingRuleBuilder) builder).setDatabaseType(mock(DatabaseType.class)); + ((ShardingRuleBuilder) builder).setDataSourceMap(Collections.singletonMap("name", mock(DataSource.class))); + assertThat(builder.build(ruleConfig), instanceOf(ShardingRule.class)); } } diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/MixSQLRewriterParameterizedTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/MixSQLRewriterParameterizedTest.java index e9cf0f1..d676d11 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/MixSQLRewriterParameterizedTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/MixSQLRewriterParameterizedTest.java @@ -22,12 +22,13 @@ import org.apache.shardingsphere.infra.binder.LogicSQL; import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory; import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext; import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; +import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource; import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData; +import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema; import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData; import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData; -import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema; import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData; import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngine; import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry; @@ -77,9 +78,10 @@ public final class MixSQLRewriterParameterizedTest extends AbstractSQLRewriterPa @Override protected Collection<SQLRewriteUnit> createSQLRewriteUnits() throws IOException { YamlRootRuleConfigurations ruleConfigurations = createRuleConfigurations(); - Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build( - new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(ruleConfigurations.getRules()), ruleConfigurations.getDataSources().keySet()); - SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType()); + String databaseType = null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType(); + Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations( + ruleConfigurations.getRules()), DatabaseTypeRegistry.getTrunkDatabaseType(databaseType), ruleConfigurations.getDataSources()); + SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(databaseType); ShardingSphereSchema schema = mockSchema(); ConfigurationProperties props = new ConfigurationProperties(ruleConfigurations.getProps()); SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance( diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/ShardingSQLRewriterParameterizedTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/ShardingSQLRewriterParameterizedTest.java index b99981c..f7c3751 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/ShardingSQLRewriterParameterizedTest.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-rewrite/src/test/java/org/apache/shardingsphere/sharding/rewrite/parameterized/ShardingSQLRewriterParameterizedTest.java @@ -22,12 +22,13 @@ import org.apache.shardingsphere.infra.binder.LogicSQL; import org.apache.shardingsphere.infra.binder.SQLStatementContextFactory; import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext; import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties; +import org.apache.shardingsphere.infra.database.type.DatabaseTypeRegistry; import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData; import org.apache.shardingsphere.infra.metadata.resource.ShardingSphereResource; import org.apache.shardingsphere.infra.metadata.rule.ShardingSphereRuleMetaData; +import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema; import org.apache.shardingsphere.infra.metadata.schema.model.ColumnMetaData; import org.apache.shardingsphere.infra.metadata.schema.model.IndexMetaData; -import org.apache.shardingsphere.infra.metadata.schema.ShardingSphereSchema; import org.apache.shardingsphere.infra.metadata.schema.model.TableMetaData; import org.apache.shardingsphere.infra.parser.sql.SQLStatementParserEngine; import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry; @@ -77,9 +78,10 @@ public final class ShardingSQLRewriterParameterizedTest extends AbstractSQLRewri @Override protected Collection<SQLRewriteUnit> createSQLRewriteUnits() throws IOException { YamlRootRuleConfigurations yamlRootRuleConfigs = createYamlRootRuleConfigurations(); - Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build( - new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(yamlRootRuleConfigs.getRules()), yamlRootRuleConfigs.getDataSources().keySet()); - SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType()); + String databaseType = null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType(); + Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations( + yamlRootRuleConfigs.getRules()), DatabaseTypeRegistry.getTrunkDatabaseType(databaseType), yamlRootRuleConfigs.getDataSources()); + SQLStatementParserEngine sqlStatementParserEngine = new SQLStatementParserEngine(databaseType); ShardingSphereSchema schema = mockSchema(); ConfigurationProperties props = new ConfigurationProperties(yamlRootRuleConfigs.getProps()); SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance( diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java index 6156c0a..571ab0a 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/ShardingRouteEngineFactory.java @@ -71,7 +71,7 @@ public final class ShardingRouteEngineFactory { * @return new instance of routing engine */ public static ShardingRouteEngine newInstance(final ShardingRule shardingRule, final ShardingSphereMetaData metaData, - final SQLStatementContext sqlStatementContext, final ShardingConditions shardingConditions, final ConfigurationProperties props) { + final SQLStatementContext<?> sqlStatementContext, final ShardingConditions shardingConditions, final ConfigurationProperties props) { SQLStatement sqlStatement = sqlStatementContext.getSqlStatement(); Collection<String> tableNames = sqlStatementContext.getTablesContext().getTableNames(); if (sqlStatement instanceof TCLStatement) { diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngine.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngine.java index 53829bd..7543464 100644 --- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngine.java +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/type/broadcast/ShardingTableBroadcastRoutingEngine.java @@ -44,7 +44,7 @@ public final class ShardingTableBroadcastRoutingEngine implements ShardingRouteE private final ShardingSphereSchema schema; - private final SQLStatementContext sqlStatementContext; + private final SQLStatementContext<?> sqlStatementContext; @Override public void route(final RouteContext routeContext, final ShardingRule shardingRule) { diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/ShardingSphereRuleBuilder.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/ShardingSphereRuleBuilder.java index 7ebf054..28400b4 100644 --- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/ShardingSphereRuleBuilder.java +++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/ShardingSphereRuleBuilder.java @@ -21,8 +21,6 @@ import org.apache.shardingsphere.infra.config.RuleConfiguration; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.spi.ordered.OrderedSPI; -import java.util.Collection; - /** * ShardingSphere rule builder. * @@ -35,8 +33,7 @@ public interface ShardingSphereRuleBuilder<R extends ShardingSphereRule, T exten * Build ShardingSphere rule. * * @param ruleConfig rule configuration - * @param dataSourceNames data source names * @return ShardingSphere rule */ - R build(T ruleConfig, Collection<String> dataSourceNames); + R build(T ruleConfig); } diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/ShardingSphereRulesBuilder.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/ShardingSphereRulesBuilder.java index ee00b205..f00ff58 100644 --- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/ShardingSphereRulesBuilder.java +++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/ShardingSphereRulesBuilder.java @@ -20,11 +20,15 @@ package org.apache.shardingsphere.infra.rule.builder; import lombok.AccessLevel; import lombok.NoArgsConstructor; import org.apache.shardingsphere.infra.config.RuleConfiguration; +import org.apache.shardingsphere.infra.database.type.DatabaseType; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; +import org.apache.shardingsphere.infra.rule.builder.aware.ResourceAware; import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader; import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry; +import javax.sql.DataSource; import java.util.Collection; +import java.util.Map; import java.util.stream.Collectors; /** @@ -41,12 +45,24 @@ public final class ShardingSphereRulesBuilder { * Build rules. * * @param ruleConfigurations rule configurations - * @param dataSourceNames data source names + * @param databaseType database type + * @param dataSourceMap data source map * @return rules */ - @SuppressWarnings("unchecked") - public static Collection<ShardingSphereRule> build(final Collection<RuleConfiguration> ruleConfigurations, final Collection<String> dataSourceNames) { - return OrderedSPIRegistry.getRegisteredServices( - ruleConfigurations, ShardingSphereRuleBuilder.class).entrySet().stream().map(entry -> entry.getValue().build(entry.getKey(), dataSourceNames)).collect(Collectors.toList()); + @SuppressWarnings({"unchecked", "rawtypes"}) + public static Collection<ShardingSphereRule> build(final Collection<RuleConfiguration> ruleConfigurations, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) { + Map<RuleConfiguration, ShardingSphereRuleBuilder> builders = OrderedSPIRegistry.getRegisteredServices(ruleConfigurations, ShardingSphereRuleBuilder.class); + setResources(builders.values(), databaseType, dataSourceMap); + return builders.entrySet().stream().map(entry -> entry.getValue().build(entry.getKey())).collect(Collectors.toList()); + } + + @SuppressWarnings("rawtypes") + private static void setResources(final Collection<ShardingSphereRuleBuilder> builders, final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap) { + for (ShardingSphereRuleBuilder each : builders) { + if (each instanceof ResourceAware) { + ((ResourceAware) each).setDatabaseType(databaseType); + ((ResourceAware) each).setDataSourceMap(dataSourceMap); + } + } } } diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/fixture/FixtureRuleBuilder.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/aware/ResourceAware.java similarity index 55% copy from shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/fixture/FixtureRuleBuilder.java copy to shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/aware/ResourceAware.java index 92b2b01..2d6a8c8 100644 --- a/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/fixture/FixtureRuleBuilder.java +++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/aware/ResourceAware.java @@ -15,26 +15,29 @@ * limitations under the License. */ -package org.apache.shardingsphere.infra.context.fixture; +package org.apache.shardingsphere.infra.rule.builder.aware; -import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; +import org.apache.shardingsphere.infra.database.type.DatabaseType; -import java.util.Collection; +import javax.sql.DataSource; +import java.util.Map; -public final class FixtureRuleBuilder implements ShardingSphereRuleBuilder<FixtureRule, FixtureRuleConfiguration> { - - @Override - public FixtureRule build(final FixtureRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) { - return new FixtureRule(); - } +/** + * Resource aware. + */ +public interface ResourceAware { - @Override - public int getOrder() { - return 0; - } + /** + * Set database type. + * + * @param databaseType database type + */ + void setDatabaseType(DatabaseType databaseType); - @Override - public Class<FixtureRuleConfiguration> getTypeClass() { - return FixtureRuleConfiguration.class; - } + /** + * Set data source map. + * + * @param dataSourceMap data source map + */ + void setDataSourceMap(Map<String, DataSource> dataSourceMap); } diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/ShardingSphereRulesBuilderTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/ShardingSphereRulesBuilderTest.java index 7435c2a..4b83cf3 100644 --- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/ShardingSphereRulesBuilderTest.java +++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/ShardingSphereRulesBuilderTest.java @@ -18,6 +18,7 @@ package org.apache.shardingsphere.infra.rule; import org.apache.shardingsphere.infra.config.RuleConfiguration; +import org.apache.shardingsphere.infra.database.type.DatabaseType; import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRulesBuilder; import org.apache.shardingsphere.infra.rule.fixture.TestRuleConfiguration; import org.apache.shardingsphere.infra.rule.fixture.TestShardingSphereRuleBuilder; @@ -28,13 +29,14 @@ import java.util.Collections; import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; public final class ShardingSphereRulesBuilderTest { @Test public void assertBuild() { RuleConfiguration ruleConfig = new TestRuleConfiguration(); - Collection<ShardingSphereRule> shardingSphereRules = ShardingSphereRulesBuilder.build(Collections.singletonList(ruleConfig), Collections.singletonList("")); - assertThat(shardingSphereRules, is(Collections.singletonList(TestShardingSphereRuleBuilder.getShardingSphereRule()))); + Collection<ShardingSphereRule> shardingSphereRules = ShardingSphereRulesBuilder.build(Collections.singletonList(ruleConfig), mock(DatabaseType.class), Collections.emptyMap()); + assertThat(shardingSphereRules, is(Collections.singletonList(TestShardingSphereRuleBuilder.getRule()))); } } diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestShardingSphereRuleBuilder.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestShardingSphereRuleBuilder.java index 8a991c8..08038ad 100644 --- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestShardingSphereRuleBuilder.java +++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/rule/fixture/TestShardingSphereRuleBuilder.java @@ -22,16 +22,14 @@ import org.apache.shardingsphere.infra.config.RuleConfiguration; import org.apache.shardingsphere.infra.rule.ShardingSphereRule; import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; -import java.util.Collection; - public final class TestShardingSphereRuleBuilder implements ShardingSphereRuleBuilder { @Getter - private static ShardingSphereRule shardingSphereRule = new TestShardingSphereRule(); + private static ShardingSphereRule rule = new TestShardingSphereRule(); @Override - public ShardingSphereRule build(final RuleConfiguration ruleConfig, final Collection dataSourceNames) { - return shardingSphereRule; + public ShardingSphereRule build(final RuleConfiguration ruleConfig) { + return rule; } @Override diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java index ba1eb96..65538b2 100644 --- a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java +++ b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/metadata/MetaDataContextsBuilder.java @@ -98,7 +98,7 @@ public final class MetaDataContextsBuilder { private ShardingSphereMetaData buildMetaData(final String schemaName) throws SQLException { Map<String, DataSource> dataSourceMap = dataSources.get(schemaName); Collection<RuleConfiguration> ruleConfigs = this.ruleConfigs.get(schemaName); - Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(ruleConfigs, dataSourceMap.keySet()); + Collection<ShardingSphereRule> rules = ShardingSphereRulesBuilder.build(ruleConfigs, databaseType, dataSourceMap); ShardingSphereRuleMetaData ruleMetaData = new ShardingSphereRuleMetaData(ruleConfigs, rules); return new ShardingSphereMetaData(schemaName, buildResource(dataSourceMap), ruleMetaData, buildSchema(schemaName, dataSourceMap, rules)); } diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/fixture/FixtureRuleBuilder.java b/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/fixture/FixtureRuleBuilder.java index 92b2b01..887a3c9 100644 --- a/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/fixture/FixtureRuleBuilder.java +++ b/shardingsphere-infra/shardingsphere-infra-context/src/test/java/org/apache/shardingsphere/infra/context/fixture/FixtureRuleBuilder.java @@ -19,12 +19,10 @@ package org.apache.shardingsphere.infra.context.fixture; import org.apache.shardingsphere.infra.rule.builder.ShardingSphereRuleBuilder; -import java.util.Collection; - public final class FixtureRuleBuilder implements ShardingSphereRuleBuilder<FixtureRule, FixtureRuleConfiguration> { @Override - public FixtureRule build(final FixtureRuleConfiguration ruleConfig, final Collection<String> dataSourceNames) { + public FixtureRule build(final FixtureRuleConfiguration ruleConfig) { return new FixtureRule(); }