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 0ea15e2cca4dab6beb51f8fe283dd7306d4a3806..84373800e139efeeab042fb69aa153eb0712d9ca 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 { @Override - public EncryptRule build(final AlgorithmProvidedEncryptRuleConfiguration ruleConfig, final Collection 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 31fb9ec25299698966ee7b37eca3808934069b5a..06e87523d3fb7e0ec2fbe6efe78f86c1545b98cb 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 { @Override - public EncryptRule build(final EncryptRuleConfiguration ruleConfig, final Collection 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 9f9c579cc3b98cfe639690dd440a9d9181b4f628..82a8e12dc053c306a2168b187f4e7b7f02a5a325 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 f71ba3629520c1a7a007e256c4b29d96b01dc278..df8d84b37ff14b4b7a9ab01e2555a362a01e29bc 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 533d3659e24b2507e53270df54c661048173d8fd..da154ff2fa962cc16ff46d647cf83fe195fbb970 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 createSQLRewriteUnits() throws IOException { YamlRootRuleConfigurations ruleConfigurations = createRuleConfigurations(); - Collection 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 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 83aa08b70d00a2955ace4754843d6b3330a80dbe..dc5df9726f4c918afb2a5d2ccbde439902ab8414 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 { @Override - public HARule build(final AlgorithmProvidedHARuleConfiguration ruleConfig, final Collection 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 ad11d176e10c8de58112e0091f69cfa631ece91e..74a4504d5033c2e6eea4cb03d7a2fba2729a7883 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 { @Override - public HARule build(final HARuleConfiguration ruleConfig, final Collection 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 f05e17f0f5d1074738dbfc19b1664410a0dcade8..670073f1678d89e0c7425093322dfe9ab125f4ed 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 da55ce1930ea3217f6f363cfffe2f1a80d3a5285..01b107e14986aa482daceb95ddcad67af4ab4c24 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 a834951e543b43edab70f052e18dba144f837e03..d9e33cf33aee855ca0a9f7cdf773d288954fb9fa 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 { @Override - public ReplicaQueryRule build(final AlgorithmProvidedReplicaQueryRuleConfiguration ruleConfig, final Collection 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 8cf20439209221f17f961160539130c6feabe03a..56ab6e3159a97455b158d2820dd4424f63b55bf3 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 { @Override - public ReplicaQueryRule build(final ReplicaQueryRuleConfiguration ruleConfig, final Collection 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 8c7afddac611a632c15fc00e7f162a052cb65b50..2c52bc1cfa451af0cd21ab87179175212740667c 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 80984d36fda23eb1c90e133b75e1043ea2a2b5b0..78b33569938e547b8e5cc9e643d1b32dfe3b8bca 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 3d631f5bb6742d3024b5c26b52833322b6864346..5d046155ff08e47c6ce9a0f64fee8ca5069d8f34 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 { @Override - public ShadowRule build(final ShadowRuleConfiguration ruleConfig, final Collection 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 79c83cb3027f35b149a90a941f2275defb598960..2a2bee263368693b3fb70f346465d3768d7af580 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 0000000000000000000000000000000000000000..272f4260ef79e123a9e73b64f9f5bd1b34102423 --- /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 load(final DatabaseType databaseType, final Map dataSourceMap, final ShardingRule shardingRule) throws SQLException { + Collection excludedTables = getExcludedTables(shardingRule); + Map result = new HashMap<>(); + for (Entry entry : dataSourceMap.entrySet()) { + result.putAll(load(databaseType, entry.getKey(), entry.getValue(), excludedTables)); + } + return result; + } + + private static Map load(final DatabaseType databaseType, + final String dataSourceName, final DataSource dataSource, final Collection excludedTables) throws SQLException { + Collection tables = SchemaMetaDataLoader.loadAllTableNames(dataSource, databaseType); + Map 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 getExcludedTables(final ShardingRule shardingRule) { + Collection 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 cd71219885765b7f7ae9793f22c3d01e7640a97d..647f2e972ae08500040d967687c38de65b2c9561 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 dataSourceNames) { + public ShardingRule(final ShardingRuleConfiguration config, final DatabaseType databaseType, final Map 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 dataSourceNames) { + public ShardingRule(final AlgorithmProvidedShardingRuleConfiguration config, final DatabaseType databaseType, final Map 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-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/SingleTableRule.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/SingleTableRule.java new file mode 100644 index 0000000000000000000000000000000000000000..5ec522def2f0614bee4be6f3296f070b9d024292 --- /dev/null +++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/rule/SingleTableRule.java @@ -0,0 +1,33 @@ +/* + * 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.rule; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +/** + * Single table rule. + */ +@RequiredArgsConstructor +@Getter +public final class SingleTableRule { + + private final String tableName; + + 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 6fe89c87148780df8cd0fa87885caf2250438b5f..71bc0b7375761be3d7e61f90673182f675fe0658 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 { +@Setter +public final class AlgorithmProvidedShardingRuleBuilder implements ShardingSphereRuleBuilder, ResourceAware { + + private DatabaseType databaseType; + + private Map dataSourceMap; @Override - public ShardingRule build(final AlgorithmProvidedShardingRuleConfiguration ruleConfig, final Collection 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 b9bb59dadb5e2daba70ed339ed2396ba7f66830b..7ef4e2630438b94441fd2290e1bb68601984ef2e 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 { +@Setter +public final class ShardingRuleBuilder implements ShardingSphereRuleBuilder, ResourceAware { + + private DatabaseType databaseType; + + private Map dataSourceMap; @Override - public ShardingRule build(final ShardingRuleConfiguration ruleConfig, final Collection 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 4578e8b0de1e0889dd68154ff0d5a00b7b0ef55b..d9496df69faa9fd5bb6b013bc56fa1ef144285d5 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 925bfb27b2dd7c5d3e17647cefb0867097ff9bce..36b0113923e1db1076345bbed96753d0af2325f8 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 e9cf0f1a2c240be2887a11a613aeb7b3b47bc3f8..d676d11b537e2292de617618334b2ef0b3376053 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 createSQLRewriteUnits() throws IOException { YamlRootRuleConfigurations ruleConfigurations = createRuleConfigurations(); - Collection 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 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 b99981c25783c6b6e5ca79bf2955b0e4c246c3e9..f7c37514146718648b4b91f24f220597590ce61a 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 createSQLRewriteUnits() throws IOException { YamlRootRuleConfigurations yamlRootRuleConfigs = createYamlRootRuleConfigurations(); - Collection 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 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 6156c0a5f56f34348150258664fcd72925eaf4ed..571ab0a34f6048d693cfcff36ce4f580e9cf78cd 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 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 53829bd74fbd562709a7b0a1ba38a9e52b557832..7543464f0f65d27f152555e39c3d47a190530c3f 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 7ebf054ef8c14dcf0f2837677026c9be44ea822b..28400b419693115e1d3937179690b811d551acd8 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 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 ee00b205ff7d94b6bf2787cc5f200c0b3a26b76b..f00ff58eafb7b9aafe5a6fd6399341318b58be5c 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 build(final Collection ruleConfigurations, final Collection 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 build(final Collection ruleConfigurations, final DatabaseType databaseType, final Map dataSourceMap) { + Map 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 builders, final DatabaseType databaseType, final Map dataSourceMap) { + for (ShardingSphereRuleBuilder each : builders) { + if (each instanceof ResourceAware) { + ((ResourceAware) each).setDatabaseType(databaseType); + ((ResourceAware) each).setDataSourceMap(dataSourceMap); + } + } } } diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/aware/ResourceAware.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/aware/ResourceAware.java new file mode 100644 index 0000000000000000000000000000000000000000..2d6a8c86d7685bd60077cbdc51e65d1616a1902f --- /dev/null +++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/rule/builder/aware/ResourceAware.java @@ -0,0 +1,43 @@ +/* + * 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.infra.rule.builder.aware; + +import org.apache.shardingsphere.infra.database.type.DatabaseType; + +import javax.sql.DataSource; +import java.util.Map; + +/** + * Resource aware. + */ +public interface ResourceAware { + + /** + * Set database type. + * + * @param databaseType database type + */ + void setDatabaseType(DatabaseType databaseType); + + /** + * Set data source map. + * + * @param dataSourceMap data source map + */ + void setDataSourceMap(Map 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 7435c2a3859d5d195cee241f9985458d86f15454..4b83cf3c51fc760bf351ebc3d4cd3eadd902ef48 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 shardingSphereRules = ShardingSphereRulesBuilder.build(Collections.singletonList(ruleConfig), Collections.singletonList("")); - assertThat(shardingSphereRules, is(Collections.singletonList(TestShardingSphereRuleBuilder.getShardingSphereRule()))); + Collection 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 8a991c81b2cec8759cf23ba3703dcf65ef32d9c6..08038ad1417d0af67dd7c8b4ece5e36db72e8be2 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 ba1eb967ee292fc35f23bf059a835258db286771..65538b2165fdc5f8c84beff44d0492873bed9e76 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 dataSourceMap = dataSources.get(schemaName); Collection ruleConfigs = this.ruleConfigs.get(schemaName); - Collection rules = ShardingSphereRulesBuilder.build(ruleConfigs, dataSourceMap.keySet()); + Collection 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 92b2b017c0f554738481da513dea773979a430fe..887a3c932510234c489cb4a11c8d396a95f66246 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 { @Override - public FixtureRule build(final FixtureRuleConfiguration ruleConfig, final Collection dataSourceNames) { + public FixtureRule build(final FixtureRuleConfiguration ruleConfig) { return new FixtureRule(); }