You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2020/09/27 12:44:41 UTC
[shardingsphere] branch master updated: Remove
RouteContext.sqlStatementContext and parameters (#7631)
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 3085c45 Remove RouteContext.sqlStatementContext and parameters (#7631)
3085c45 is described below
commit 3085c453db58257e63b55f2c0f5c30826f4baf0e
Author: Liang Zhang <te...@163.com>
AuthorDate: Sun Sep 27 20:44:16 2020 +0800
Remove RouteContext.sqlStatementContext and parameters (#7631)
* Move DefaultRouteStageContext to impl package
* Add sqlStatementContext and param to RouteDecorator
* Remove RouteContext.sqlStatementContext and parameters
---
.../engine/ConsensusReplicationRouteDecorator.java | 5 +-
.../EncryptSQLRewriterParameterizedTest.java | 25 ++++----
.../PrimaryReplicaReplicationRouteDecorator.java | 11 ++--
...rimaryReplicaReplicationRouteDecoratorTest.java | 70 +++++++++-------------
.../shadow/route/engine/ShadowRouteDecorator.java | 43 ++++++-------
.../route/engine/ShadowRouteDecoratorTest.java | 70 ++++++++++------------
.../MixSQLRewriterParameterizedTest.java | 31 +++++-----
.../ShardingSQLRewriterParameterizedTest.java | 30 ++++++----
.../route/engine/ShardingRouteDecorator.java | 9 ++-
.../validator/ShardingStatementValidator.java | 9 ++-
.../ShardingCreateTableStatementValidator.java | 14 ++---
.../impl/ShardingDeleteStatementValidator.java | 6 +-
.../impl/ShardingInsertStatementValidator.java | 7 +--
.../impl/ShardingUpdateStatementValidator.java | 10 ++--
.../engine/type/standard/AbstractSQLRouteTest.java | 6 +-
.../ShardingCreateTableStatementValidatorTest.java | 7 +--
.../impl/ShardingDeleteStatementValidatorTest.java | 6 +-
.../impl/ShardingInsertStatementValidatorTest.java | 22 +++----
.../impl/ShardingUpdateStatementValidatorTest.java | 21 ++-----
.../infra/context/kernel/KernelProcessor.java | 9 ++-
.../infra/rewrite/SQLRewriteEntry.java | 10 ++--
.../infra/rewrite/SQLRewriteEntryTest.java | 8 +--
.../shardingsphere/infra/route/DataNodeRouter.java | 17 +++---
.../infra/route/context/RouteContext.java | 14 +----
.../{ => impl}/DefaultRouteStageContext.java | 4 +-
.../infra/route/decorator/RouteDecorator.java | 11 +++-
.../UnconfiguredSchemaRouteDecorator.java | 6 +-
.../infra/route/DataNodeRouterTest.java | 6 +-
.../fixture/decorator/RouteDecoratorFixture.java | 7 ++-
.../decorator/RouteFailureDecoratorFixture.java | 6 +-
30 files changed, 236 insertions(+), 264 deletions(-)
diff --git a/shardingsphere-features/shardingsphere-consensus-replication/shardingsphere-consensus-replication-route/src/main/java/org/apache/shardingsphere/replication/consensus/route/engine/ConsensusReplicationRouteDecorator.java b/shardingsphere-features/shardingsphere-consensus-replication/shardingsphere-consensus-replication-route/src/main/java/org/apache/shardingsphere/replication/consensus/route/engine/ConsensusReplicationRouteDecorator.java
index bd5961d..b29781a 100644
--- a/shardingsphere-features/shardingsphere-consensus-replication/shardingsphere-consensus-replication-route/src/main/java/org/apache/shardingsphere/replication/consensus/route/engine/ConsensusReplicationRouteDecorator.java
+++ b/shardingsphere-features/shardingsphere-consensus-replication/shardingsphere-consensus-replication-route/src/main/java/org/apache/shardingsphere/replication/consensus/route/engine/ConsensusReplicationRouteDecorator.java
@@ -30,6 +30,7 @@ import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext
import java.util.Collection;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
import java.util.Optional;
@@ -39,10 +40,10 @@ import java.util.Optional;
public final class ConsensusReplicationRouteDecorator implements RouteDecorator<ConsensusReplicationRule> {
@Override
- public void decorate(final RouteContext routeContext, final ShardingSphereMetaData metaData, final ConsensusReplicationRule consensusReplicationRule, final ConfigurationProperties props) {
+ public void decorate(final RouteContext routeContext, final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters,
+ final ShardingSphereMetaData metaData, final ConsensusReplicationRule consensusReplicationRule, final ConfigurationProperties props) {
Map<String, ConsensusReplicationGroup> replicaGroups = new HashMap<>();
String schemaName = metaData.getSchemaName();
- SQLStatementContext<?> sqlStatementContext = routeContext.getSqlStatementContext();
if (routeContext.getRouteResult().getRouteUnits().isEmpty()) {
ConsensusReplicationTableRule replicaRoutingRule = consensusReplicationRule.getReplicaTableRules().iterator().next();
ConsensusReplicationGroup replicaGroup = new ConsensusReplicationGroup(replicaRoutingRule.getPhysicsTable(), replicaRoutingRule.getReplicaGroupId(), replicaRoutingRule.getReplicaPeers(),
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 9d0910b..6e6224f 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
@@ -18,18 +18,10 @@
package org.apache.shardingsphere.encrypt.rewrite.parameterized;
import com.google.common.base.Preconditions;
-import org.apache.shardingsphere.sql.parser.engine.StandardSQLParserEngine;
-import org.apache.shardingsphere.sql.parser.engine.SQLParserEngineFactory;
-import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.datasource.DataSourceMetaDatas;
import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRulesBuilder;
-import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
-import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
-import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry;
import org.apache.shardingsphere.infra.rewrite.engine.result.GenericSQLRewriteResult;
import org.apache.shardingsphere.infra.rewrite.engine.result.RouteSQLRewriteResult;
@@ -40,6 +32,16 @@ import org.apache.shardingsphere.infra.rewrite.parameterized.engine.parameter.SQ
import org.apache.shardingsphere.infra.rewrite.parameterized.engine.parameter.SQLRewriteEngineTestParametersBuilder;
import org.apache.shardingsphere.infra.route.DataNodeRouter;
import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRulesBuilder;
+import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
+import org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory;
+import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sql.parser.engine.SQLParserEngineFactory;
+import org.apache.shardingsphere.sql.parser.engine.StandardSQLParserEngine;
import org.junit.runners.Parameterized.Parameters;
import java.io.File;
@@ -73,10 +75,11 @@ public final class EncryptSQLRewriterParameterizedTest extends AbstractSQLRewrit
StandardSQLParserEngine standardSqlParserEngine = SQLParserEngineFactory.getSQLParserEngine(null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType());
ShardingSphereMetaData metaData = createShardingSphereMetaData();
ConfigurationProperties props = new ConfigurationProperties(ruleConfigurations.getProps());
- RouteContext routeContext = new DataNodeRouter(metaData, props, rules).route(
- standardSqlParserEngine.parse(getTestParameters().getInputSQL(), false), getTestParameters().getInputSQL(), getTestParameters().getInputParameters());
+ SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(
+ metaData.getRuleSchemaMetaData().getConfiguredSchemaMetaData(), getTestParameters().getInputParameters(), standardSqlParserEngine.parse(getTestParameters().getInputSQL(), false));
+ RouteContext routeContext = new DataNodeRouter(metaData, props, rules).route(sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters());
SQLRewriteResult sqlRewriteResult = new SQLRewriteEntry(metaData.getRuleSchemaMetaData().getConfiguredSchemaMetaData(),
- props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), routeContext);
+ props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), sqlStatementContext, routeContext);
return sqlRewriteResult instanceof GenericSQLRewriteResult
? Collections.singletonList(((GenericSQLRewriteResult) sqlRewriteResult).getSqlRewriteUnit()) : (((RouteSQLRewriteResult) sqlRewriteResult).getSqlRewriteUnits()).values();
}
diff --git a/shardingsphere-features/shardingsphere-primary-replica-replication/shardingsphere-primary-replica-replication-route/src/main/java/org/apache/shardingsphere/replication/primaryreplica/route/engine/PrimaryReplicaReplicationRouteDecorator.java b/shardingsphere-features/shardingsphere-primary-replica-replication/shardingsphere-primary-replica-replication-route/src/main/java/org/apache/shardingsphere/replication/primaryreplica/route/engine/PrimaryReplicaReplicationRouteDecorator.java
index ab6ddde..ebe1a6f 100644
--- a/shardingsphere-features/shardingsphere-primary-replica-replication/shardingsphere-primary-replica-replication-route/src/main/java/org/apache/shardingsphere/replication/primaryreplica/route/engine/PrimaryReplicaReplicationRouteDecorator.java
+++ b/shardingsphere-features/shardingsphere-primary-replica-replication/shardingsphere-primary-replica-replication-route/src/main/java/org/apache/shardingsphere/replication/primaryreplica/route/engine/PrimaryReplicaReplicationRouteDecorator.java
@@ -20,19 +20,21 @@ package org.apache.shardingsphere.replication.primaryreplica.route.engine;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.database.DefaultSchema;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.DefaultRouteStageContext;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
+import org.apache.shardingsphere.infra.route.context.impl.DefaultRouteStageContext;
import org.apache.shardingsphere.infra.route.decorator.RouteDecorator;
import org.apache.shardingsphere.replication.primaryreplica.constant.PrimaryReplicaReplicationOrder;
import org.apache.shardingsphere.replication.primaryreplica.route.engine.impl.PrimaryReplicaReplicationDataSourceRouter;
import org.apache.shardingsphere.replication.primaryreplica.rule.PrimaryReplicaReplicationDataSourceRule;
import org.apache.shardingsphere.replication.primaryreplica.rule.PrimaryReplicaReplicationRule;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
+import java.util.List;
import java.util.Optional;
/**
@@ -41,9 +43,10 @@ import java.util.Optional;
public final class PrimaryReplicaReplicationRouteDecorator implements RouteDecorator<PrimaryReplicaReplicationRule> {
@Override
- public void decorate(final RouteContext routeContext, final ShardingSphereMetaData metaData, final PrimaryReplicaReplicationRule rule, final ConfigurationProperties props) {
+ public void decorate(final RouteContext routeContext, final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters,
+ final ShardingSphereMetaData metaData, final PrimaryReplicaReplicationRule rule, final ConfigurationProperties props) {
if (routeContext.getRouteResult().getRouteUnits().isEmpty()) {
- String dataSourceName = new PrimaryReplicaReplicationDataSourceRouter(rule.getSingleDataSourceRule()).route(routeContext.getSqlStatementContext().getSqlStatement());
+ String dataSourceName = new PrimaryReplicaReplicationDataSourceRouter(rule.getSingleDataSourceRule()).route(sqlStatementContext.getSqlStatement());
routeContext.getRouteResult().getRouteUnits().add(new RouteUnit(new RouteMapper(DefaultSchema.LOGIC_NAME, dataSourceName), Collections.emptyList()));
routeContext.addNextRouteStageContext(getTypeClass(), new DefaultRouteStageContext());
return;
@@ -55,7 +58,7 @@ public final class PrimaryReplicaReplicationRouteDecorator implements RouteDecor
Optional<PrimaryReplicaReplicationDataSourceRule> dataSourceRule = rule.findDataSourceRule(dataSourceName);
if (dataSourceRule.isPresent() && dataSourceRule.get().getName().equalsIgnoreCase(each.getDataSourceMapper().getActualName())) {
toBeRemoved.add(each);
- String actualDataSourceName = new PrimaryReplicaReplicationDataSourceRouter(dataSourceRule.get()).route(routeContext.getSqlStatementContext().getSqlStatement());
+ String actualDataSourceName = new PrimaryReplicaReplicationDataSourceRouter(dataSourceRule.get()).route(sqlStatementContext.getSqlStatement());
toBeAdded.add(new RouteUnit(new RouteMapper(each.getDataSourceMapper().getLogicName(), actualDataSourceName), each.getTableMappers()));
}
}
diff --git a/shardingsphere-features/shardingsphere-primary-replica-replication/shardingsphere-primary-replica-replication-route/src/test/java/org/apache/shardingsphere/replication/primaryreplica/route/engine/PrimaryReplicaReplicationRouteDecoratorTest.java b/shardingsphere-features/shardingsphere-primary-replica-replication/shardingsphere-primary-replica-replication-route/src/test/java/org/apache/shardingsphere/replication/primaryreplica/route/engine/PrimaryReplicaReplicationRouteDecorat [...]
index 89fee39..5c3408b 100644
--- a/shardingsphere-features/shardingsphere-primary-replica-replication/shardingsphere-primary-replica-replication-route/src/test/java/org/apache/shardingsphere/replication/primaryreplica/route/engine/PrimaryReplicaReplicationRouteDecoratorTest.java
+++ b/shardingsphere-features/shardingsphere-primary-replica-replication/shardingsphere-primary-replica-replication-route/src/test/java/org/apache/shardingsphere/replication/primaryreplica/route/engine/PrimaryReplicaReplicationRouteDecoratorTest.java
@@ -68,14 +68,8 @@ public final class PrimaryReplicaReplicationRouteDecoratorTest {
@Mock
private SQLStatementContext<SQLStatement> sqlStatementContext;
- @Mock
- private InsertStatement insertStatement;
-
- @Mock
- private MySQLSelectStatement selectStatement;
-
private PrimaryReplicaReplicationRouteDecorator routeDecorator;
-
+
static {
ShardingSphereServiceLoader.register(RouteDecorator.class);
}
@@ -94,8 +88,8 @@ public final class PrimaryReplicaReplicationRouteDecoratorTest {
@Test
public void assertDecorateToPrimary() {
- RouteContext actual = mockSQLRouteContext(insertStatement);
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
+ RouteContext actual = mockRouteContext();
+ routeDecorator.decorate(actual, mock(SQLStatementContext.class), Collections.emptyList(), mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
Iterator<String> routedDataSourceNames = actual.getRouteResult().getActualDataSourceNames().iterator();
assertThat(routedDataSourceNames.next(), is(NON_PRIMARY_REPLICA_DATASOURCE_NAME));
assertThat(routedDataSourceNames.next(), is(PRIMARY_DATASOURCE));
@@ -103,17 +97,19 @@ public final class PrimaryReplicaReplicationRouteDecoratorTest {
@Test
public void assertDecorateToPrimaryWithoutRouteUnits() {
- RouteContext actual = mockSQLRouteContextWithoutRouteUnits(insertStatement);
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
+ RouteContext actual = new RouteContext();
+ routeDecorator.decorate(actual, mock(SQLStatementContext.class), Collections.emptyList(), mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
Iterator<String> routedDataSourceNames = actual.getRouteResult().getActualDataSourceNames().iterator();
assertThat(routedDataSourceNames.next(), is(PRIMARY_DATASOURCE));
}
@Test
public void assertDecorateToReplica() {
- RouteContext actual = mockSQLRouteContext(selectStatement);
+ RouteContext actual = mockRouteContext();
+ MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class);
+ when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement);
when(selectStatement.getLock()).thenReturn(Optional.empty());
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
+ routeDecorator.decorate(actual, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
Iterator<String> routedDataSourceNames = actual.getRouteResult().getActualDataSourceNames().iterator();
assertThat(routedDataSourceNames.next(), is(NON_PRIMARY_REPLICA_DATASOURCE_NAME));
assertThat(routedDataSourceNames.next(), is(REPLICA_DATASOURCE));
@@ -121,18 +117,22 @@ public final class PrimaryReplicaReplicationRouteDecoratorTest {
@Test
public void assertDecorateToReplicaWithoutRouteUnits() {
- RouteContext actual = mockSQLRouteContextWithoutRouteUnits(selectStatement);
+ RouteContext actual = new RouteContext();
+ MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class);
+ when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement);
when(selectStatement.getLock()).thenReturn(Optional.empty());
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
+ routeDecorator.decorate(actual, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
Iterator<String> routedDataSourceNames = actual.getRouteResult().getActualDataSourceNames().iterator();
assertThat(routedDataSourceNames.next(), is(REPLICA_DATASOURCE));
}
@Test
public void assertLockDecorateToPrimary() {
- RouteContext actual = mockSQLRouteContext(selectStatement);
+ RouteContext actual = mockRouteContext();
+ MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class);
+ when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement);
when(selectStatement.getLock()).thenReturn(Optional.of(mock(LockSegment.class)));
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
+ routeDecorator.decorate(actual, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
Iterator<String> routedDataSourceNames = actual.getRouteResult().getActualDataSourceNames().iterator();
assertThat(routedDataSourceNames.next(), is(NON_PRIMARY_REPLICA_DATASOURCE_NAME));
assertThat(routedDataSourceNames.next(), is(PRIMARY_DATASOURCE));
@@ -140,43 +140,31 @@ public final class PrimaryReplicaReplicationRouteDecoratorTest {
@Test
public void assertLockDecorateToPrimaryWithoutRouteUnits() {
- RouteContext actual = mockSQLRouteContextWithoutRouteUnits(selectStatement);
+ RouteContext actual = new RouteContext();
+ MySQLSelectStatement selectStatement = mock(MySQLSelectStatement.class);
+ when(sqlStatementContext.getSqlStatement()).thenReturn(selectStatement);
when(selectStatement.getLock()).thenReturn(Optional.of(mock(LockSegment.class)));
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
+ routeDecorator.decorate(actual, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
Iterator<String> routedDataSourceNames = actual.getRouteResult().getActualDataSourceNames().iterator();
assertThat(routedDataSourceNames.next(), is(PRIMARY_DATASOURCE));
}
@Test
public void assertDecorateToPrimaryWithoutRouteUnitsAndWithParameters() {
- RouteContext actual = mockSQLRouteContextWithoutRouteUnitsAndWithParameters(insertStatement);
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
+ RouteContext actual = new RouteContext();
+ when(sqlStatementContext.getSqlStatement()).thenReturn(mock(InsertStatement.class));
+ routeDecorator.decorate(actual, sqlStatementContext, Collections.singletonList("true"), mock(ShardingSphereMetaData.class), rule, new ConfigurationProperties(new Properties()));
Iterator<String> routedDataSourceNames = actual.getRouteResult().getActualDataSourceNames().iterator();
assertThat(routedDataSourceNames.next(), is(PRIMARY_DATASOURCE));
- assertThat(actual.getParameters().get(0), is("true"));
- }
-
- private RouteContext mockSQLRouteContext(final SQLStatement sqlStatement) {
- when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement);
- RouteContext result = new RouteContext(sqlStatementContext, Collections.emptyList());
- mockRouteResult(result.getRouteResult());
- result.addNextRouteStageContext(null, null);
- return result;
}
- private void mockRouteResult(final RouteResult routeResult) {
+ private RouteContext mockRouteContext() {
+ RouteContext result = new RouteContext();
+ RouteResult routeResult = result.getRouteResult();
RouteUnit routeUnit = new RouteUnit(new RouteMapper(DATASOURCE_NAME, DATASOURCE_NAME), Collections.singletonList(new RouteMapper("table", "table_0")));
routeResult.getRouteUnits().add(routeUnit);
routeResult.getRouteUnits().add(new RouteUnit(new RouteMapper(NON_PRIMARY_REPLICA_DATASOURCE_NAME, NON_PRIMARY_REPLICA_DATASOURCE_NAME), Collections.emptyList()));
- }
-
- private RouteContext mockSQLRouteContextWithoutRouteUnits(final SQLStatement sqlStatement) {
- when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement);
- return new RouteContext(sqlStatementContext, Collections.emptyList());
- }
-
- private RouteContext mockSQLRouteContextWithoutRouteUnitsAndWithParameters(final SQLStatement sqlStatement) {
- when(sqlStatementContext.getSqlStatement()).thenReturn(sqlStatement);
- return new RouteContext(sqlStatementContext, Collections.singletonList("true"));
+ result.addNextRouteStageContext(null, null);
+ return result;
}
}
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecorator.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecorator.java
index 2b8be16..93e3d11 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecorator.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/main/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecorator.java
@@ -17,22 +17,21 @@
package org.apache.shardingsphere.shadow.route.engine;
-import org.apache.shardingsphere.infra.route.context.DefaultRouteStageContext;
-import org.apache.shardingsphere.shadow.constant.ShadowOrder;
-import org.apache.shardingsphere.shadow.route.engine.judge.ShadowDataSourceJudgeEngine;
-import org.apache.shardingsphere.shadow.rule.ShadowRule;
-import org.apache.shardingsphere.shadow.route.engine.judge.impl.PreparedShadowDataSourceJudgeEngine;
-import org.apache.shardingsphere.shadow.route.engine.judge.impl.SimpleShadowDataSourceJudgeEngine;
-import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
+import org.apache.shardingsphere.infra.route.context.impl.DefaultRouteStageContext;
import org.apache.shardingsphere.infra.route.decorator.RouteDecorator;
+import org.apache.shardingsphere.shadow.constant.ShadowOrder;
+import org.apache.shardingsphere.shadow.route.engine.judge.ShadowDataSourceJudgeEngine;
+import org.apache.shardingsphere.shadow.route.engine.judge.impl.PreparedShadowDataSourceJudgeEngine;
+import org.apache.shardingsphere.shadow.route.engine.judge.impl.SimpleShadowDataSourceJudgeEngine;
+import org.apache.shardingsphere.shadow.rule.ShadowRule;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
import java.util.Collection;
import java.util.Collections;
@@ -45,19 +44,18 @@ import java.util.List;
public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
@Override
- public void decorate(final RouteContext routeContext, final ShardingSphereMetaData metaData, final ShadowRule shadowRule, final ConfigurationProperties props) {
+ public void decorate(final RouteContext routeContext, final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters,
+ final ShardingSphereMetaData metaData, final ShadowRule shadowRule, final ConfigurationProperties props) {
if (routeContext.getRouteResult().getRouteUnits().isEmpty()) {
- decorateRouteContext(routeContext, shadowRule);
+ decorateRouteContext(routeContext, sqlStatementContext, parameters, shadowRule);
return;
}
- decorateRouteContextWithRouteResult(routeContext, shadowRule);
+ decorateRouteContextWithRouteResult(routeContext, sqlStatementContext, parameters, shadowRule);
}
- private void decorateRouteContext(final RouteContext routeContext, final ShadowRule shadowRule) {
- SQLStatementContext<?> sqlStatementContext = routeContext.getSqlStatementContext();
- SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
+ private void decorateRouteContext(final RouteContext routeContext, final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters, final ShadowRule shadowRule) {
RouteResult routeResult = routeContext.getRouteResult();
- if (!(sqlStatement instanceof DMLStatement)) {
+ if (!(sqlStatementContext.getSqlStatement() instanceof DMLStatement)) {
shadowRule.getShadowMappings().forEach((key, value) -> {
routeResult.getRouteUnits().add(new RouteUnit(new RouteMapper(key, key), Collections.emptyList()));
routeResult.getRouteUnits().add(new RouteUnit(new RouteMapper(value, value), Collections.emptyList()));
@@ -65,7 +63,7 @@ public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
routeContext.addNextRouteStageContext(getTypeClass(), new DefaultRouteStageContext());
return;
}
- if (isShadow(routeContext, shadowRule)) {
+ if (isShadow(sqlStatementContext, parameters, shadowRule)) {
shadowRule.getShadowMappings().values().forEach(each -> routeResult.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())));
} else {
shadowRule.getShadowMappings().keySet().forEach(each -> routeResult.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList())));
@@ -73,10 +71,9 @@ public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
routeContext.addNextRouteStageContext(getTypeClass(), new DefaultRouteStageContext());
}
- private void decorateRouteContextWithRouteResult(final RouteContext routeContext, final ShadowRule shadowRule) {
- SQLStatement sqlStatement = routeContext.getSqlStatementContext().getSqlStatement();
+ private void decorateRouteContextWithRouteResult(final RouteContext routeContext, final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters, final ShadowRule shadowRule) {
Collection<RouteUnit> toBeAdded = new LinkedList<>();
- if (!(sqlStatement instanceof DMLStatement)) {
+ if (!(sqlStatementContext.getSqlStatement() instanceof DMLStatement)) {
for (RouteUnit each : routeContext.getRouteResult().getRouteUnits()) {
String shadowDataSourceName = shadowRule.getShadowMappings().get(each.getDataSourceMapper().getActualName());
toBeAdded.add(new RouteUnit(new RouteMapper(each.getDataSourceMapper().getLogicName(), shadowDataSourceName), each.getTableMappers()));
@@ -85,7 +82,7 @@ public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
return;
}
Collection<RouteUnit> toBeRemoved = new LinkedList<>();
- if (isShadow(routeContext, shadowRule)) {
+ if (isShadow(sqlStatementContext, parameters, shadowRule)) {
for (RouteUnit each : routeContext.getRouteResult().getRouteUnits()) {
toBeRemoved.add(each);
String shadowDataSourceName = shadowRule.getShadowMappings().get(each.getDataSourceMapper().getActualName());
@@ -96,9 +93,7 @@ public final class ShadowRouteDecorator implements RouteDecorator<ShadowRule> {
routeContext.getRouteResult().getRouteUnits().addAll(toBeAdded);
}
- private boolean isShadow(final RouteContext routeContext, final ShadowRule shadowRule) {
- List<Object> parameters = routeContext.getParameters();
- SQLStatementContext<?> sqlStatementContext = routeContext.getSqlStatementContext();
+ private boolean isShadow(final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters, final ShadowRule shadowRule) {
ShadowDataSourceJudgeEngine shadowDataSourceRouter = parameters.isEmpty() ? new SimpleShadowDataSourceJudgeEngine(shadowRule, sqlStatementContext)
: new PreparedShadowDataSourceJudgeEngine(shadowRule, sqlStatementContext, parameters);
return shadowDataSourceRouter.isShadow();
diff --git a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/test/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecoratorTest.java b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/test/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecoratorTest.java
index 29b3cfb..ed617ed 100644
--- a/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/test/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecoratorTest.java
+++ b/shardingsphere-features/shardingsphere-shadow/shardingsphere-shadow-route/src/test/java/org/apache/shardingsphere/shadow/route/engine/ShadowRouteDecoratorTest.java
@@ -32,7 +32,6 @@ import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml.MySQ
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
-import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Collection;
@@ -55,18 +54,6 @@ public final class ShadowRouteDecoratorTest {
private static final String SHADOW_DATASOURCE = "shadow_ds";
- @Mock
- private InsertStatementContext sqlStatementContext;
-
- @Mock
- private MySQLInsertStatement insertStatement;
-
- @Mock
- private CreateTableStatementContext createTableStatementContext;
-
- @Mock
- private MySQLCreateTableStatement createTableStatement;
-
private ShadowRouteDecorator routeDecorator;
private ShadowRule shadowRule;
@@ -80,24 +67,24 @@ public final class ShadowRouteDecoratorTest {
@Test
public void assertDecorateToShadowWithOutRouteUnit() {
- RouteContext actual = mockSQLRouteContextForShadow();
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
+ RouteContext actual = new RouteContext();
+ routeDecorator.decorate(actual, mockSQLStatementContextForShadow(), Collections.emptyList(), mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
Iterator<String> routedDataSourceNames = actual.getRouteResult().getActualDataSourceNames().iterator();
assertThat(routedDataSourceNames.next(), is(SHADOW_DATASOURCE));
}
@Test
public void assertDecorateToActualWithOutRouteUnit() {
- RouteContext actual = mockSQLRouteContext();
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
+ RouteContext actual = new RouteContext();
+ routeDecorator.decorate(actual, mockSQLStatementContext(), Collections.emptyList(), mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
Iterator<String> routedDataSourceNames = actual.getRouteResult().getActualDataSourceNames().iterator();
assertThat(routedDataSourceNames.next(), is(ACTUAL_DATASOURCE));
}
@Test
public void assertNonDMLStatementWithOutRouteUnit() {
- RouteContext actual = mockNonDMLSQLRouteContext();
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
+ RouteContext actual = new RouteContext();
+ routeDecorator.decorate(actual, mockNonDMLSQLStatementContext(), Collections.emptyList(), mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
assertThat(actual.getRouteResult().getRouteUnits().size(), is(2));
assertTrue(actual.getRouteResult().getActualDataSourceNames().contains(SHADOW_DATASOURCE));
assertTrue(actual.getRouteResult().getActualDataSourceNames().contains(ACTUAL_DATASOURCE));
@@ -105,27 +92,27 @@ public final class ShadowRouteDecoratorTest {
@Test
public void assertDecorateToShadowWithRouteUnit() {
- RouteContext actual = mockSQLRouteContextForShadow();
+ RouteContext actual = new RouteContext();
actual.getRouteResult().getRouteUnits().add(mockRouteUnit());
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
+ routeDecorator.decorate(actual, mockSQLStatementContextForShadow(), Collections.emptyList(), mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
assertThat(actual.getRouteResult().getRouteUnits().size(), is(1));
assertTrue(actual.getRouteResult().getActualDataSourceNames().contains(SHADOW_DATASOURCE));
}
@Test
public void assertDecorateToActualWithRouteUnit() {
- RouteContext actual = mockSQLRouteContext();
+ RouteContext actual = new RouteContext();
actual.getRouteResult().getRouteUnits().add(mockRouteUnit());
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
+ routeDecorator.decorate(actual, mockSQLStatementContext(), Collections.emptyList(), mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
Iterator<String> routedDataSourceNames = actual.getRouteResult().getActualDataSourceNames().iterator();
assertThat(routedDataSourceNames.next(), is(ACTUAL_DATASOURCE));
}
@Test
public void assertNonDMLStatementWithRouteUnit() {
- RouteContext actual = mockNonDMLSQLRouteContext();
+ RouteContext actual = new RouteContext();
actual.getRouteResult().getRouteUnits().add(mockRouteUnit());
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
+ routeDecorator.decorate(actual, mockNonDMLSQLStatementContext(), Collections.emptyList(), mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
assertThat(actual.getRouteResult().getRouteUnits().size(), is(2));
assertTrue(actual.getRouteResult().getActualDataSourceNames().contains(SHADOW_DATASOURCE));
assertTrue(actual.getRouteResult().getActualDataSourceNames().contains(ACTUAL_DATASOURCE));
@@ -133,9 +120,9 @@ public final class ShadowRouteDecoratorTest {
@Test
public void assertTableMapperWithRouteUnit() {
- RouteContext actual = mockSQLRouteContextForShadow();
+ RouteContext actual = new RouteContext();
actual.getRouteResult().getRouteUnits().add(mockRouteUnit());
- routeDecorator.decorate(actual, mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
+ routeDecorator.decorate(actual, mockSQLStatementContextForShadow(), Collections.emptyList(), mock(ShardingSphereMetaData.class), shadowRule, new ConfigurationProperties(new Properties()));
assertThat(actual.getRouteResult().getRouteUnits().size(), is(1));
assertTrue(actual.getRouteResult().getActualDataSourceNames().contains(SHADOW_DATASOURCE));
Collection<RouteMapper> tableMappers = actual.getRouteResult().getRouteUnits().iterator().next().getTableMappers();
@@ -144,26 +131,29 @@ public final class ShadowRouteDecoratorTest {
assertThat(tableMappers.iterator().next().getLogicName(), is("table"));
}
- private RouteContext mockSQLRouteContextForShadow() {
- when(sqlStatementContext.getSqlStatement()).thenReturn(insertStatement);
- when(sqlStatementContext.getDescendingColumnNames()).thenReturn(Collections.singletonList(SHADOW_COLUMN).iterator());
- when(sqlStatementContext.getColumnNames()).thenReturn(Collections.singletonList(SHADOW_COLUMN));
+ private InsertStatementContext mockSQLStatementContextForShadow() {
+ InsertStatementContext result = mock(InsertStatementContext.class);
+ when(result.getSqlStatement()).thenReturn(mock(MySQLInsertStatement.class));
+ when(result.getDescendingColumnNames()).thenReturn(Collections.singletonList(SHADOW_COLUMN).iterator());
+ when(result.getColumnNames()).thenReturn(Collections.singletonList(SHADOW_COLUMN));
InsertValueContext insertValueContext = mock(InsertValueContext.class);
when(insertValueContext.getValue(0)).thenReturn(true);
- when(sqlStatementContext.getInsertValueContexts()).thenReturn(Collections.singletonList(insertValueContext));
- return new RouteContext(sqlStatementContext, Collections.emptyList());
+ when(result.getInsertValueContexts()).thenReturn(Collections.singletonList(insertValueContext));
+ return result;
}
- private RouteContext mockSQLRouteContext() {
- when(sqlStatementContext.getSqlStatement()).thenReturn(insertStatement);
- return new RouteContext(sqlStatementContext, Collections.emptyList());
+ private InsertStatementContext mockSQLStatementContext() {
+ InsertStatementContext result = mock(InsertStatementContext.class);
+ when(result.getSqlStatement()).thenReturn(mock(MySQLInsertStatement.class));
+ return result;
}
- private RouteContext mockNonDMLSQLRouteContext() {
- when(createTableStatementContext.getSqlStatement()).thenReturn(createTableStatement);
- return new RouteContext(createTableStatementContext, Collections.emptyList());
+ private CreateTableStatementContext mockNonDMLSQLStatementContext() {
+ CreateTableStatementContext result = mock(CreateTableStatementContext.class);
+ when(result.getSqlStatement()).thenReturn(mock(MySQLCreateTableStatement.class));
+ return result;
}
-
+
private RouteUnit mockRouteUnit() {
return new RouteUnit(new RouteMapper(ACTUAL_DATASOURCE, ACTUAL_DATASOURCE), Collections.singletonList(new RouteMapper("table", "table_0")));
}
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 25e50c4..2d1d951 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
@@ -18,21 +18,10 @@
package org.apache.shardingsphere.sharding.rewrite.parameterized;
import com.google.common.base.Preconditions;
-import org.apache.shardingsphere.sql.parser.engine.StandardSQLParserEngine;
-import org.apache.shardingsphere.sql.parser.engine.SQLParserEngineFactory;
-import org.apache.shardingsphere.sql.parser.binder.metadata.column.ColumnMetaData;
-import org.apache.shardingsphere.sql.parser.binder.metadata.index.IndexMetaData;
-import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
-import org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaData;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.datasource.DataSourceMetaDatas;
import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRulesBuilder;
-import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
-import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
-import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry;
import org.apache.shardingsphere.infra.rewrite.engine.result.GenericSQLRewriteResult;
import org.apache.shardingsphere.infra.rewrite.engine.result.RouteSQLRewriteResult;
@@ -43,6 +32,19 @@ import org.apache.shardingsphere.infra.rewrite.parameterized.engine.parameter.SQ
import org.apache.shardingsphere.infra.rewrite.parameterized.engine.parameter.SQLRewriteEngineTestParametersBuilder;
import org.apache.shardingsphere.infra.route.DataNodeRouter;
import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRulesBuilder;
+import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
+import org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory;
+import org.apache.shardingsphere.sql.parser.binder.metadata.column.ColumnMetaData;
+import org.apache.shardingsphere.sql.parser.binder.metadata.index.IndexMetaData;
+import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
+import org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaData;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sql.parser.engine.SQLParserEngineFactory;
+import org.apache.shardingsphere.sql.parser.engine.StandardSQLParserEngine;
import org.junit.runners.Parameterized.Parameters;
import java.io.File;
@@ -80,10 +82,11 @@ public final class MixSQLRewriterParameterizedTest extends AbstractSQLRewriterPa
StandardSQLParserEngine standardSqlParserEngine = SQLParserEngineFactory.getSQLParserEngine(null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType());
ShardingSphereMetaData metaData = createShardingSphereMetaData();
ConfigurationProperties props = new ConfigurationProperties(ruleConfigurations.getProps());
- RouteContext routeContext = new DataNodeRouter(metaData, props, rules).route(
- standardSqlParserEngine.parse(getTestParameters().getInputSQL(), false), getTestParameters().getInputSQL(), getTestParameters().getInputParameters());
+ SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(
+ metaData.getRuleSchemaMetaData().getConfiguredSchemaMetaData(), getTestParameters().getInputParameters(), standardSqlParserEngine.parse(getTestParameters().getInputSQL(), false));
+ RouteContext routeContext = new DataNodeRouter(metaData, props, rules).route(sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters());
SQLRewriteResult sqlRewriteResult = new SQLRewriteEntry(metaData.getRuleSchemaMetaData().getConfiguredSchemaMetaData(),
- props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), routeContext);
+ props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), sqlStatementContext, routeContext);
return sqlRewriteResult instanceof GenericSQLRewriteResult
? Collections.singletonList(((GenericSQLRewriteResult) sqlRewriteResult).getSqlRewriteUnit()) : (((RouteSQLRewriteResult) sqlRewriteResult).getSqlRewriteUnits()).values();
}
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 7991254..a438ff8 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
@@ -18,21 +18,10 @@
package org.apache.shardingsphere.sharding.rewrite.parameterized;
import com.google.common.base.Preconditions;
-import org.apache.shardingsphere.sql.parser.engine.StandardSQLParserEngine;
-import org.apache.shardingsphere.sql.parser.engine.SQLParserEngineFactory;
-import org.apache.shardingsphere.sql.parser.binder.metadata.column.ColumnMetaData;
-import org.apache.shardingsphere.sql.parser.binder.metadata.index.IndexMetaData;
-import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
-import org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaData;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.datasource.DataSourceMetaDatas;
import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.infra.rule.ShardingSphereRulesBuilder;
-import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
-import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
-import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
import org.apache.shardingsphere.infra.rewrite.SQLRewriteEntry;
import org.apache.shardingsphere.infra.rewrite.engine.result.GenericSQLRewriteResult;
import org.apache.shardingsphere.infra.rewrite.engine.result.RouteSQLRewriteResult;
@@ -43,6 +32,19 @@ import org.apache.shardingsphere.infra.rewrite.parameterized.engine.parameter.SQ
import org.apache.shardingsphere.infra.rewrite.parameterized.engine.parameter.SQLRewriteEngineTestParametersBuilder;
import org.apache.shardingsphere.infra.route.DataNodeRouter;
import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRulesBuilder;
+import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
+import org.apache.shardingsphere.infra.yaml.engine.YamlEngine;
+import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapperEngine;
+import org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory;
+import org.apache.shardingsphere.sql.parser.binder.metadata.column.ColumnMetaData;
+import org.apache.shardingsphere.sql.parser.binder.metadata.index.IndexMetaData;
+import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
+import org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaData;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.sql.parser.engine.SQLParserEngineFactory;
+import org.apache.shardingsphere.sql.parser.engine.StandardSQLParserEngine;
import org.junit.runners.Parameterized.Parameters;
import java.io.File;
@@ -80,10 +82,12 @@ public final class ShardingSQLRewriterParameterizedTest extends AbstractSQLRewri
StandardSQLParserEngine standardSqlParserEngine = SQLParserEngineFactory.getSQLParserEngine(null == getTestParameters().getDatabaseType() ? "SQL92" : getTestParameters().getDatabaseType());
ShardingSphereMetaData metaData = createShardingSphereMetaData();
ConfigurationProperties props = new ConfigurationProperties(yamlRootRuleConfigs.getProps());
+ SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(
+ metaData.getRuleSchemaMetaData().getConfiguredSchemaMetaData(), getTestParameters().getInputParameters(), standardSqlParserEngine.parse(getTestParameters().getInputSQL(), false));
RouteContext routeContext = new DataNodeRouter(metaData, props, rules).route(
- standardSqlParserEngine.parse(getTestParameters().getInputSQL(), false), getTestParameters().getInputSQL(), getTestParameters().getInputParameters());
+ sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters());
SQLRewriteResult sqlRewriteResult = new SQLRewriteEntry(metaData.getRuleSchemaMetaData().getConfiguredSchemaMetaData(),
- props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), routeContext);
+ props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), sqlStatementContext, routeContext);
return sqlRewriteResult instanceof GenericSQLRewriteResult
? Collections.singletonList(((GenericSQLRewriteResult) sqlRewriteResult).getSqlRewriteUnit()) : (((RouteSQLRewriteResult) sqlRewriteResult).getSqlRewriteUnits()).values();
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingRouteDecorator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingRouteDecorator.java
index 52d6eb7..7cccc93 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingRouteDecorator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/ShardingRouteDecorator.java
@@ -21,7 +21,7 @@ import com.google.common.base.Preconditions;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.DefaultRouteStageContext;
+import org.apache.shardingsphere.infra.route.context.impl.DefaultRouteStageContext;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.infra.route.decorator.RouteDecorator;
@@ -59,12 +59,11 @@ public final class ShardingRouteDecorator implements RouteDecorator<ShardingRule
@SuppressWarnings({"rawtypes", "unchecked"})
@Override
- public void decorate(final RouteContext routeContext, final ShardingSphereMetaData metaData, final ShardingRule shardingRule, final ConfigurationProperties props) {
- SQLStatementContext<?> sqlStatementContext = routeContext.getSqlStatementContext();
- List<Object> parameters = routeContext.getParameters();
+ public void decorate(final RouteContext routeContext, final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters,
+ final ShardingSphereMetaData metaData, final ShardingRule shardingRule, final ConfigurationProperties props) {
SQLStatement sqlStatement = sqlStatementContext.getSqlStatement();
Optional<ShardingStatementValidator> shardingStatementValidator = ShardingStatementValidatorFactory.newInstance(sqlStatement);
- shardingStatementValidator.ifPresent(validator -> validator.preValidate(shardingRule, routeContext, metaData));
+ shardingStatementValidator.ifPresent(validator -> validator.preValidate(shardingRule, sqlStatementContext, parameters, metaData));
ShardingConditions shardingConditions = getShardingConditions(parameters, sqlStatementContext, metaData.getRuleSchemaMetaData().getConfiguredSchemaMetaData(), shardingRule);
boolean needMergeShardingValues = isNeedMergeShardingValues(sqlStatementContext, shardingRule);
if (sqlStatement instanceof DMLStatement && needMergeShardingValues) {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
index de4cb2e..df66157 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/ShardingStatementValidator.java
@@ -18,11 +18,13 @@
package org.apache.shardingsphere.sharding.route.engine.validator;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import java.util.List;
+
/**
* Sharding statement validator.
*
@@ -34,10 +36,11 @@ public interface ShardingStatementValidator<T extends SQLStatement> {
* Validate whether sharding operation is supported before route.
*
* @param shardingRule sharding rule
- * @param routeContext route context
+ * @param sqlStatementContext SQL statement context
+ * @param parameters SQL parameters
* @param metaData meta data
*/
- void preValidate(ShardingRule shardingRule, RouteContext routeContext, ShardingSphereMetaData metaData);
+ void preValidate(ShardingRule shardingRule, SQLStatementContext<T> sqlStatementContext, List<Object> parameters, ShardingSphereMetaData metaData);
/**
* Validate whether sharding operation is supported after route.
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingCreateTableStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingCreateTableStatementValidator.java
index c25c45d..25e94e0 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingCreateTableStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingCreateTableStatementValidator.java
@@ -18,26 +18,26 @@
package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.sharding.route.engine.exception.TableExistsException;
import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.binder.statement.ddl.CreateTableStatementContext;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.CreateTableStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.ddl.CreateTableStatementHandler;
+import java.util.List;
+
/**
* Sharding create table statement validator.
*/
public final class ShardingCreateTableStatementValidator implements ShardingStatementValidator<CreateTableStatement> {
@Override
- public void preValidate(final ShardingRule shardingRule, final RouteContext routeContext, final ShardingSphereMetaData metaData) {
- CreateTableStatementContext sqlStatementContext = (CreateTableStatementContext) routeContext.getSqlStatementContext();
- CreateTableStatement createTableStatement = sqlStatementContext.getSqlStatement();
- String tableName = createTableStatement.getTable().getTableName().getIdentifier().getValue();
- if (!CreateTableStatementHandler.containsNotExistClause(createTableStatement) && metaData.getRuleSchemaMetaData().getAllTableNames().contains(tableName)) {
+ public void preValidate(final ShardingRule shardingRule,
+ final SQLStatementContext<CreateTableStatement> sqlStatementContext, final List<Object> parameters, final ShardingSphereMetaData metaData) {
+ String tableName = sqlStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue();
+ if (!CreateTableStatementHandler.containsNotExistClause(sqlStatementContext.getSqlStatement()) && metaData.getRuleSchemaMetaData().getAllTableNames().contains(tableName)) {
throw new TableExistsException(tableName);
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidator.java
index e078b11..126f962 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidator.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
@@ -28,14 +27,15 @@ import org.apache.shardingsphere.sql.parser.binder.type.TableAvailable;
import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DeleteStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.DeleteStatementHandler;
+import java.util.List;
+
/**
* Sharding delete statement validator.
*/
public final class ShardingDeleteStatementValidator implements ShardingStatementValidator<DeleteStatement> {
@Override
- public void preValidate(final ShardingRule shardingRule, final RouteContext routeContext, final ShardingSphereMetaData metaData) {
- SQLStatementContext<?> sqlStatementContext = routeContext.getSqlStatementContext();
+ public void preValidate(final ShardingRule shardingRule, final SQLStatementContext<DeleteStatement> sqlStatementContext, final List<Object> parameters, final ShardingSphereMetaData metaData) {
if (1 != ((TableAvailable) sqlStatementContext).getAllTables().size()) {
throw new ShardingSphereException("Cannot support Multiple-Table for '%s'.", sqlStatementContext.getSqlStatement());
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java
index 2c35ef4..fcf59c3 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidator.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
@@ -35,6 +34,7 @@ import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.InsertState
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.InsertStatementHandler;
import java.util.Collection;
+import java.util.List;
import java.util.Optional;
/**
@@ -43,12 +43,11 @@ import java.util.Optional;
public final class ShardingInsertStatementValidator implements ShardingStatementValidator<InsertStatement> {
@Override
- public void preValidate(final ShardingRule shardingRule, final RouteContext routeContext, final ShardingSphereMetaData metaData) {
- SQLStatementContext<?> sqlStatementContext = routeContext.getSqlStatementContext();
+ public void preValidate(final ShardingRule shardingRule, final SQLStatementContext<InsertStatement> sqlStatementContext, final List<Object> parameters, final ShardingSphereMetaData metaData) {
if (null == ((InsertStatementContext) sqlStatementContext).getInsertSelectContext() && 1 != ((TableAvailable) sqlStatementContext).getTablesContext().getTables().size()) {
throw new ShardingSphereException("Cannot support Multiple-Table for '%s'.", sqlStatementContext.getSqlStatement());
}
- InsertStatement sqlStatement = (InsertStatement) sqlStatementContext.getSqlStatement();
+ InsertStatement sqlStatement = sqlStatementContext.getSqlStatement();
Optional<OnDuplicateKeyColumnsSegment> onDuplicateKeyColumnsSegment = InsertStatementHandler.getOnDuplicateKeyColumnsSegment(sqlStatement);
String tableName = sqlStatement.getTable().getTableName().getIdentifier().getValue();
if (onDuplicateKeyColumnsSegment.isPresent() && isUpdateShardingKey(shardingRule, onDuplicateKeyColumnsSegment.get(), tableName)) {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidator.java
index d56daa7..684d429 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/main/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidator.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteResult;
import org.apache.shardingsphere.sharding.route.engine.validator.ShardingStatementValidator;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
@@ -46,21 +45,20 @@ import java.util.Optional;
public final class ShardingUpdateStatementValidator implements ShardingStatementValidator<UpdateStatement> {
@Override
- public void preValidate(final ShardingRule shardingRule, final RouteContext routeContext, final ShardingSphereMetaData metaData) {
- SQLStatementContext<?> sqlStatementContext = routeContext.getSqlStatementContext();
+ public void preValidate(final ShardingRule shardingRule, final SQLStatementContext<UpdateStatement> sqlStatementContext, final List<Object> parameters, final ShardingSphereMetaData metaData) {
if (1 != ((TableAvailable) sqlStatementContext).getAllTables().size()) {
throw new ShardingSphereException("Cannot support Multiple-Table for '%s'.", sqlStatementContext.getSqlStatement());
}
- UpdateStatement sqlStatement = (UpdateStatement) sqlStatementContext.getSqlStatement();
+ UpdateStatement sqlStatement = sqlStatementContext.getSqlStatement();
String tableName = sqlStatementContext.getTablesContext().getTables().iterator().next().getTableName().getIdentifier().getValue();
for (AssignmentSegment each : sqlStatement.getSetAssignment().getAssignments()) {
String shardingColumn = each.getColumn().getIdentifier().getValue();
if (shardingRule.isShardingColumn(shardingColumn, tableName)) {
- Optional<Object> shardingColumnSetAssignmentValue = getShardingColumnSetAssignmentValue(each, routeContext.getParameters());
+ Optional<Object> shardingColumnSetAssignmentValue = getShardingColumnSetAssignmentValue(each, parameters);
Optional<Object> shardingValue = Optional.empty();
Optional<WhereSegment> whereSegmentOptional = sqlStatement.getWhere();
if (whereSegmentOptional.isPresent()) {
- shardingValue = getShardingValue(whereSegmentOptional.get(), routeContext.getParameters(), shardingColumn);
+ shardingValue = getShardingValue(whereSegmentOptional.get(), parameters, shardingColumn);
}
if (shardingColumnSetAssignmentValue.isPresent() && shardingValue.isPresent() && shardingColumnSetAssignmentValue.get().equals(shardingValue.get())) {
continue;
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java
index 1f5603e..8794e7b 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/type/standard/AbstractSQLRouteTest.java
@@ -27,9 +27,11 @@ import org.apache.shardingsphere.infra.route.DataNodeRouter;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.sharding.route.fixture.AbstractRoutingEngineTest;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
+import org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory;
import org.apache.shardingsphere.sql.parser.binder.metadata.column.ColumnMetaData;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import org.apache.shardingsphere.sql.parser.binder.metadata.table.TableMetaData;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.engine.SQLParserEngineFactory;
import org.apache.shardingsphere.sql.parser.engine.StandardSQLParserEngine;
@@ -52,7 +54,9 @@ public abstract class AbstractSQLRouteTest extends AbstractRoutingEngineTest {
ShardingSphereMetaData metaData = new ShardingSphereMetaData(buildDataSourceMetas(), buildRuleSchemaMetaData(), "sharding_db");
ConfigurationProperties props = new ConfigurationProperties(new Properties());
StandardSQLParserEngine standardSqlParserEngine = SQLParserEngineFactory.getSQLParserEngine("MySQL");
- RouteContext result = new DataNodeRouter(metaData, props, Collections.singletonList(shardingRule)).route(standardSqlParserEngine.parse(sql, false), sql, parameters);
+ SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(
+ metaData.getRuleSchemaMetaData().getConfiguredSchemaMetaData(), parameters, standardSqlParserEngine.parse(sql, false));
+ RouteContext result = new DataNodeRouter(metaData, props, Collections.singletonList(shardingRule)).route(sqlStatementContext, sql, parameters);
assertThat(result.getRouteResult().getRouteUnits().size(), is(1));
return result;
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingCreateTableStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingCreateTableStatementValidatorTest.java
index 71368d7..9d49c91 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingCreateTableStatementValidatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingCreateTableStatementValidatorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.schema.RuleSchemaMetaData;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.sharding.route.engine.exception.TableExistsException;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
@@ -84,12 +83,11 @@ public final class ShardingCreateTableStatementValidatorTest {
private void assertValidateCreateTable(final CreateTableStatement sqlStatement) {
SQLStatementContext<CreateTableStatement> sqlStatementContext = new CreateTableStatementContext(sqlStatement);
- RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList());
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
RuleSchemaMetaData ruleSchemaMetaData = mock(RuleSchemaMetaData.class);
when(ruleSchemaMetaData.getAllTableNames()).thenReturn(Collections.singleton("t_order"));
when(metaData.getRuleSchemaMetaData()).thenReturn(ruleSchemaMetaData);
- new ShardingCreateTableStatementValidator().preValidate(shardingRule, routeContext, metaData);
+ new ShardingCreateTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), metaData);
}
@Test
@@ -110,11 +108,10 @@ public final class ShardingCreateTableStatementValidatorTest {
private void assertValidateCreateTableIfNotExists(final CreateTableStatement sqlStatement) {
SQLStatementContext<CreateTableStatement> sqlStatementContext = new CreateTableStatementContext(sqlStatement);
- RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList());
ShardingSphereMetaData metaData = mock(ShardingSphereMetaData.class);
RuleSchemaMetaData ruleSchemaMetaData = mock(RuleSchemaMetaData.class);
when(ruleSchemaMetaData.getAllTableNames()).thenReturn(Collections.singleton("t_order"));
when(metaData.getRuleSchemaMetaData()).thenReturn(ruleSchemaMetaData);
- new ShardingCreateTableStatementValidator().preValidate(shardingRule, routeContext, metaData);
+ new ShardingCreateTableStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), metaData);
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidatorTest.java
index ae3df1d..4c6a0c5 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingDeleteStatementValidatorTest.java
@@ -19,9 +19,7 @@ package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
-import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.dml.DeleteStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.DeleteMultiTableSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.generic.table.SimpleTableSegment;
@@ -74,8 +72,6 @@ public final class ShardingDeleteStatementValidatorTest {
tableSegment.getActualDeleteTables().add(new SimpleTableSegment(0, 0, new IdentifierValue("user")));
tableSegment.getActualDeleteTables().add(new SimpleTableSegment(0, 0, new IdentifierValue("order")));
sqlStatement.setTableSegment(tableSegment);
- SQLStatementContext<DeleteStatement> sqlStatementContext = new DeleteStatementContext(sqlStatement);
- RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList());
- new ShardingDeleteStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingDeleteStatementValidator().preValidate(shardingRule, new DeleteStatementContext(sqlStatement), Collections.emptyList(), mock(ShardingSphereMetaData.class));
}
}
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java
index e6a6868..8960f51 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingInsertStatementValidatorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import org.apache.shardingsphere.sql.parser.binder.segment.table.TablesContext;
@@ -62,24 +61,21 @@ public final class ShardingInsertStatementValidatorTest {
public void assertValidateInsertModifyMultiTables() {
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertStatement());
sqlStatementContext.getTablesContext().getTables().addAll(createMultiTablesContext().getTables());
- RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList());
- new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereMetaData.class));
}
@Test
public void assertValidateOnDuplicateKeyWithoutShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(false);
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertStatement());
- RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList());
- new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereMetaData.class));
}
@Test(expected = ShardingSphereException.class)
public void assertValidateOnDuplicateKeyWithShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertStatement());
- RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList());
- new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereMetaData.class));
}
@Test(expected = ShardingSphereException.class)
@@ -88,8 +84,7 @@ public final class ShardingInsertStatementValidatorTest {
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(false);
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTables().addAll(createSingleTablesContext().getTables());
- RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList());
- new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereMetaData.class));
}
@Test
@@ -98,8 +93,7 @@ public final class ShardingInsertStatementValidatorTest {
when(shardingRule.isGenerateKeyColumn("id", "user")).thenReturn(true);
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTables().addAll(createSingleTablesContext().getTables());
- RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList());
- new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereMetaData.class));
}
@Test(expected = ShardingSphereException.class)
@@ -110,8 +104,7 @@ public final class ShardingInsertStatementValidatorTest {
when(shardingRule.isAllBindingTables(multiTablesContext.getTableNames())).thenReturn(false);
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTables().addAll(multiTablesContext.getTables());
- RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList());
- new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereMetaData.class));
}
@Test
@@ -122,8 +115,7 @@ public final class ShardingInsertStatementValidatorTest {
when(shardingRule.isAllBindingTables(multiTablesContext.getTableNames())).thenReturn(true);
SQLStatementContext<InsertStatement> sqlStatementContext = new InsertStatementContext(new SchemaMetaData(Collections.emptyMap()), Collections.singletonList(1), createInsertSelectStatement());
sqlStatementContext.getTablesContext().getTables().addAll(multiTablesContext.getTables());
- RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList());
- new ShardingInsertStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingInsertStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereMetaData.class));
}
private InsertStatement createInsertStatement() {
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidatorTest.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidatorTest.java
index f147826..9388907 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidatorTest.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-route/src/test/java/org/apache/shardingsphere/sharding/route/engine/validator/impl/ShardingUpdateStatementValidatorTest.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.sharding.route.engine.validator.impl;
import org.apache.shardingsphere.infra.exception.ShardingSphereException;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.sharding.rule.ShardingRule;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.binder.statement.dml.UpdateStatementContext;
@@ -61,48 +60,40 @@ public final class ShardingUpdateStatementValidatorTest {
joinTableSegment.setLeft(new SimpleTableSegment(0, 0, new IdentifierValue("user")));
joinTableSegment.setRight(new SimpleTableSegment(0, 0, new IdentifierValue("order")));
sqlStatementContext.getSqlStatement().setTableSegment(joinTableSegment);
- RouteContext routeContext = new RouteContext(sqlStatementContext, Collections.emptyList());
- new ShardingUpdateStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingUpdateStatementValidator().preValidate(shardingRule, sqlStatementContext, Collections.emptyList(), mock(ShardingSphereMetaData.class));
}
@Test
public void assertValidateUpdateWithoutShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(false);
- RouteContext routeContext = new RouteContext(new UpdateStatementContext(createUpdateStatement()), Collections.emptyList());
- new ShardingUpdateStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingUpdateStatementValidator().preValidate(shardingRule, new UpdateStatementContext(createUpdateStatement()), Collections.emptyList(), mock(ShardingSphereMetaData.class));
}
@Test(expected = ShardingSphereException.class)
public void assertValidateUpdateWithShardingKey() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
- RouteContext routeContext = new RouteContext(new UpdateStatementContext(createUpdateStatement()), Collections.emptyList());
- new ShardingUpdateStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingUpdateStatementValidator().preValidate(shardingRule, new UpdateStatementContext(createUpdateStatement()), Collections.emptyList(), mock(ShardingSphereMetaData.class));
}
@Test
public void assertValidateUpdateWithoutShardingKeyAndParameters() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(false);
List<Object> parameters = Arrays.asList(1, 1);
- RouteContext routeContext = new RouteContext(new UpdateStatementContext(createUpdateStatement()), parameters);
- new ShardingUpdateStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingUpdateStatementValidator().preValidate(shardingRule, new UpdateStatementContext(createUpdateStatement()), parameters, mock(ShardingSphereMetaData.class));
}
@Test
public void assertValidateUpdateWithShardingKeyAndShardingParameterEquals() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
List<Object> parameters = Arrays.asList(1, 1);
- SQLStatementContext<UpdateStatement> updateStatementContext = new UpdateStatementContext(createUpdateStatementAndParameters(1));
- RouteContext routeContext = new RouteContext(updateStatementContext, parameters);
- new ShardingUpdateStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingUpdateStatementValidator().preValidate(shardingRule, new UpdateStatementContext(createUpdateStatementAndParameters(1)), parameters, mock(ShardingSphereMetaData.class));
}
@Test(expected = ShardingSphereException.class)
public void assertValidateUpdateWithShardingKeyAndShardingParameterNotEquals() {
when(shardingRule.isShardingColumn("id", "user")).thenReturn(true);
List<Object> parameters = Arrays.asList(1, 1);
- SQLStatementContext<UpdateStatement> updateStatementContext = new UpdateStatementContext(createUpdateStatementAndParameters(2));
- RouteContext routeContext = new RouteContext(updateStatementContext, parameters);
- new ShardingUpdateStatementValidator().preValidate(shardingRule, routeContext, mock(ShardingSphereMetaData.class));
+ new ShardingUpdateStatementValidator().preValidate(shardingRule, new UpdateStatementContext(createUpdateStatementAndParameters(2)), parameters, mock(ShardingSphereMetaData.class));
}
private UpdateStatement createUpdateStatement() {
diff --git a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java
index 75129ba..a3cc3da 100644
--- a/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java
+++ b/shardingsphere-infra/shardingsphere-infra-context/src/main/java/org/apache/shardingsphere/infra/context/kernel/KernelProcessor.java
@@ -27,6 +27,8 @@ import org.apache.shardingsphere.infra.rewrite.engine.result.SQLRewriteResult;
import org.apache.shardingsphere.infra.route.DataNodeRouter;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory;
+import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import java.util.Collection;
@@ -46,10 +48,11 @@ public final class KernelProcessor {
public ExecutionContext generateExecutionContext(final LogicSQLContext logicSQLContext, final ConfigurationProperties props) {
Collection<ShardingSphereRule> rules = logicSQLContext.getSchemaContext().getSchema().getRules();
DataNodeRouter router = new DataNodeRouter(logicSQLContext.getSchemaContext().getSchema().getMetaData(), props, rules);
- RouteContext routeContext = router.route(logicSQLContext.getSqlStatement(), logicSQLContext.getSql(), logicSQLContext.getParameters());
+ SchemaMetaData schemaMetaData = logicSQLContext.getSchemaContext().getSchema().getMetaData().getRuleSchemaMetaData().getSchemaMetaData();
+ SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(schemaMetaData, logicSQLContext.getParameters(), logicSQLContext.getSqlStatement());
+ RouteContext routeContext = router.route(sqlStatementContext, logicSQLContext.getSql(), logicSQLContext.getParameters());
SQLRewriteEntry rewriteEntry = new SQLRewriteEntry(logicSQLContext.getSchemaContext().getSchema().getMetaData().getRuleSchemaMetaData().getConfiguredSchemaMetaData(), props, rules);
- SQLRewriteResult rewriteResult = rewriteEntry.rewrite(logicSQLContext.getSql(), logicSQLContext.getParameters(), routeContext);
- SQLStatementContext<?> sqlStatementContext = routeContext.getSqlStatementContext();
+ SQLRewriteResult rewriteResult = rewriteEntry.rewrite(logicSQLContext.getSql(), logicSQLContext.getParameters(), sqlStatementContext, routeContext);
Collection<ExecutionUnit> executionUnits = ExecutionContextBuilder.build(logicSQLContext.getSchemaContext().getSchema().getMetaData(), rewriteResult, sqlStatementContext);
return new ExecutionContext(sqlStatementContext, executionUnits, routeContext);
}
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java
index 1a8400c..cc85ddd 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/main/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntry.java
@@ -29,6 +29,7 @@ import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.order.OrderedSPIRegistry;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import java.util.Collection;
import java.util.List;
@@ -61,17 +62,18 @@ public final class SQLRewriteEntry {
*
* @param sql SQL
* @param parameters SQL parameters
+ * @param sqlStatementContext SQL statement context
* @param routeContext route context
* @return route unit and SQL rewrite result map
*/
- public SQLRewriteResult rewrite(final String sql, final List<Object> parameters, final RouteContext routeContext) {
- SQLRewriteContext sqlRewriteContext = createSQLRewriteContext(sql, parameters, routeContext);
+ public SQLRewriteResult rewrite(final String sql, final List<Object> parameters, final SQLStatementContext<?> sqlStatementContext, final RouteContext routeContext) {
+ SQLRewriteContext sqlRewriteContext = createSQLRewriteContext(sql, parameters, sqlStatementContext, routeContext);
RouteResult routeResult = routeContext.getRouteResult();
return routeResult.getRouteUnits().isEmpty() ? new GenericSQLRewriteEngine().rewrite(sqlRewriteContext) : new RouteSQLRewriteEngine().rewrite(sqlRewriteContext, routeResult);
}
- private SQLRewriteContext createSQLRewriteContext(final String sql, final List<Object> parameters, final RouteContext routeContext) {
- SQLRewriteContext result = new SQLRewriteContext(metaData, routeContext.getSqlStatementContext(), sql, parameters);
+ private SQLRewriteContext createSQLRewriteContext(final String sql, final List<Object> parameters, final SQLStatementContext<?> sqlStatementContext, final RouteContext routeContext) {
+ SQLRewriteContext result = new SQLRewriteContext(metaData, sqlStatementContext, sql, parameters);
decorate(decorators, result, routeContext);
result.generateSQLTokens();
return result;
diff --git a/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntryTest.java b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntryTest.java
index 16907f4..8896ae3 100644
--- a/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntryTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-rewrite/shardingsphere-infra-rewrite-engine/src/test/java/org/apache/shardingsphere/infra/rewrite/SQLRewriteEntryTest.java
@@ -48,8 +48,8 @@ public final class SQLRewriteEntryTest {
@Test
public void assertRewriteForGenericSQLRewriteResult() {
SQLRewriteEntry sqlRewriteEntry = new SQLRewriteEntry(metaData, props, Collections.emptyList());
- RouteContext routeContext = new RouteContext(mock(SQLStatementContext.class), Collections.singletonList(1));
- GenericSQLRewriteResult sqlRewriteResult = (GenericSQLRewriteResult) sqlRewriteEntry.rewrite("SELECT ?", Collections.singletonList(1), routeContext);
+ RouteContext routeContext = new RouteContext();
+ GenericSQLRewriteResult sqlRewriteResult = (GenericSQLRewriteResult) sqlRewriteEntry.rewrite("SELECT ?", Collections.singletonList(1), mock(SQLStatementContext.class), routeContext);
assertThat(sqlRewriteResult.getSqlRewriteUnit().getSql(), is("SELECT ?"));
assertThat(sqlRewriteResult.getSqlRewriteUnit().getParameters(), is(Collections.singletonList(1)));
}
@@ -57,10 +57,10 @@ public final class SQLRewriteEntryTest {
@Test
public void assertRewriteForRouteSQLRewriteResult() {
SQLRewriteEntry sqlRewriteEntry = new SQLRewriteEntry(metaData, props, Collections.emptyList());
- RouteContext routeContext = new RouteContext(mock(SQLStatementContext.class), Collections.singletonList(1));
+ RouteContext routeContext = new RouteContext();
routeContext.getRouteResult().getRouteUnits().addAll(Arrays.asList(mock(RouteUnit.class), mock(RouteUnit.class)));
routeContext.addNextRouteStageContext(null, null);
- RouteSQLRewriteResult sqlRewriteResult = (RouteSQLRewriteResult) sqlRewriteEntry.rewrite("SELECT ?", Collections.singletonList(1), routeContext);
+ RouteSQLRewriteResult sqlRewriteResult = (RouteSQLRewriteResult) sqlRewriteEntry.rewrite("SELECT ?", Collections.singletonList(1), mock(SQLStatementContext.class), routeContext);
assertThat(sqlRewriteResult.getSqlRewriteUnits().size(), is(2));
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/DataNodeRouter.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/DataNodeRouter.java
index 65ac303..b757487 100644
--- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/DataNodeRouter.java
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/DataNodeRouter.java
@@ -26,9 +26,7 @@ import org.apache.shardingsphere.infra.route.hook.SPIRoutingHook;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.spi.order.OrderedSPIRegistry;
-import org.apache.shardingsphere.sql.parser.binder.SQLStatementContextFactory;
import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import java.util.Collection;
import java.util.List;
@@ -63,15 +61,15 @@ public final class DataNodeRouter {
/**
* Route SQL.
*
- * @param sqlStatement SQL statement
+ * @param sqlStatementContext SQL statement context
* @param sql SQL
* @param parameters SQL parameters
* @return route context
*/
- public RouteContext route(final SQLStatement sqlStatement, final String sql, final List<Object> parameters) {
+ public RouteContext route(final SQLStatementContext<?> sqlStatementContext, final String sql, final List<Object> parameters) {
routingHook.start(sql);
try {
- RouteContext result = doRoute(sqlStatement, parameters);
+ RouteContext result = doRoute(sqlStatementContext, parameters);
routingHook.finishSuccess(result, metaData.getRuleSchemaMetaData().getConfiguredSchemaMetaData());
return result;
// CHECKSTYLE:OFF
@@ -83,13 +81,12 @@ public final class DataNodeRouter {
}
@SuppressWarnings({"unchecked", "rawtypes"})
- private RouteContext doRoute(final SQLStatement sqlStatement, final List<Object> parameters) {
- SQLStatementContext<?> sqlStatementContext = SQLStatementContextFactory.newInstance(metaData.getRuleSchemaMetaData().getSchemaMetaData(), parameters, sqlStatement);
- RouteContext result = new RouteContext(sqlStatementContext, parameters);
+ private RouteContext doRoute(final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters) {
+ RouteContext result = new RouteContext();
for (Entry<ShardingSphereRule, RouteDecorator> entry : decorators.entrySet()) {
- entry.getValue().decorate(result, metaData, entry.getKey(), props);
+ entry.getValue().decorate(result, sqlStatementContext, parameters, metaData, entry.getKey(), props);
}
- new UnconfiguredSchemaRouteDecorator().decorate(result, metaData);
+ new UnconfiguredSchemaRouteDecorator().decorate(result, sqlStatementContext, metaData);
return result;
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteContext.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteContext.java
index d62c93f..d5fa66e 100644
--- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/RouteContext.java
@@ -19,10 +19,8 @@ package org.apache.shardingsphere.infra.route.context;
import lombok.Getter;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
-import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import java.util.LinkedHashMap;
-import java.util.List;
import java.util.Map;
/**
@@ -31,20 +29,10 @@ import java.util.Map;
@Getter
public final class RouteContext {
- private final SQLStatementContext<?> sqlStatementContext;
-
- private final List<Object> parameters;
-
- private final RouteResult routeResult;
+ private final RouteResult routeResult = new RouteResult();
private final Map<Class<? extends ShardingSphereRule>, RouteStageContext> routeStageContexts = new LinkedHashMap<>();
- public RouteContext(final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters) {
- this.sqlStatementContext = sqlStatementContext;
- this.parameters = parameters;
- routeResult = new RouteResult();
- }
-
/**
* Add next route stage context.
*
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/DefaultRouteStageContext.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/impl/DefaultRouteStageContext.java
similarity index 87%
rename from shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/DefaultRouteStageContext.java
rename to shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/impl/DefaultRouteStageContext.java
index 8ae1531..73dc8fe 100644
--- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/DefaultRouteStageContext.java
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/context/impl/DefaultRouteStageContext.java
@@ -15,7 +15,9 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.infra.route.context;
+package org.apache.shardingsphere.infra.route.context.impl;
+
+import org.apache.shardingsphere.infra.route.context.RouteStageContext;
/**
* Default route stage context.
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/decorator/RouteDecorator.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/decorator/RouteDecorator.java
index ae5efbf..c942c37 100644
--- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/decorator/RouteDecorator.java
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/decorator/RouteDecorator.java
@@ -17,11 +17,14 @@
package org.apache.shardingsphere.infra.route.decorator;
-import org.apache.shardingsphere.infra.route.context.RouteContext;
-import org.apache.shardingsphere.infra.spi.order.OrderedSPI;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.spi.order.OrderedSPI;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+
+import java.util.List;
/**
* Route decorator.
@@ -34,9 +37,11 @@ public interface RouteDecorator<T extends ShardingSphereRule> extends OrderedSPI
* Decorate route context.
*
* @param routeContext route context
+ * @param sqlStatementContext SQL statement context
+ * @param parameters SQL parameters
* @param metaData meta data of ShardingSphere
* @param rule rule
* @param props configuration properties
*/
- void decorate(RouteContext routeContext, ShardingSphereMetaData metaData, T rule, ConfigurationProperties props);
+ void decorate(RouteContext routeContext, SQLStatementContext<?> sqlStatementContext, List<Object> parameters, ShardingSphereMetaData metaData, T rule, ConfigurationProperties props);
}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/decorator/UnconfiguredSchemaRouteDecorator.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/decorator/UnconfiguredSchemaRouteDecorator.java
index 587fd63..844df35 100644
--- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/decorator/UnconfiguredSchemaRouteDecorator.java
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/decorator/UnconfiguredSchemaRouteDecorator.java
@@ -21,6 +21,7 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement;
@@ -35,10 +36,11 @@ public final class UnconfiguredSchemaRouteDecorator {
* Decorate route context.
*
* @param routeContext route context
+ * @param sqlStatementContext SQL statement context
* @param metaData meta data of ShardingSphere
*/
- public void decorate(final RouteContext routeContext, final ShardingSphereMetaData metaData) {
- if (isNeedUnconfiguredSchema(routeContext.getSqlStatementContext().getSqlStatement())) {
+ public void decorate(final RouteContext routeContext, final SQLStatementContext<?> sqlStatementContext, final ShardingSphereMetaData metaData) {
+ if (isNeedUnconfiguredSchema(sqlStatementContext.getSqlStatement())) {
for (String each : metaData.getRuleSchemaMetaData().getUnconfiguredSchemaMetaDataMap().keySet()) {
routeContext.getRouteResult().getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList()));
}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/DataNodeRouterTest.java b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/DataNodeRouterTest.java
index b777cfc..37513f8 100644
--- a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/DataNodeRouterTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/DataNodeRouterTest.java
@@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.route.fixture.rule.RouteFailureRuleFixtur
import org.apache.shardingsphere.infra.route.fixture.rule.RouteRuleFixture;
import org.apache.shardingsphere.infra.route.hook.SPIRoutingHook;
import org.apache.shardingsphere.sql.parser.binder.metadata.schema.SchemaMetaData;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -65,7 +65,7 @@ public final class DataNodeRouterTest {
public void assertRouteSuccess() {
DataNodeRouter router = new DataNodeRouter(metaData, props, Collections.singletonList(new RouteRuleFixture()));
setSPIRoutingHook(router);
- RouteContext actual = router.route(mock(SQLStatement.class), "SELECT 1", Collections.emptyList());
+ RouteContext actual = router.route(mock(SQLStatementContext.class), "SELECT 1", Collections.emptyList());
assertThat(actual.getRouteResult().getRouteUnits().size(), is(1));
RouteUnit routeUnit = actual.getRouteResult().getRouteUnits().iterator().next();
assertThat(routeUnit.getDataSourceMapper().getLogicName(), is("ds"));
@@ -80,7 +80,7 @@ public final class DataNodeRouterTest {
DataNodeRouter router = new DataNodeRouter(metaData, props, Collections.singletonList(new RouteFailureRuleFixture()));
setSPIRoutingHook(router);
try {
- router.route(mock(SQLStatement.class), "SELECT 1", Collections.emptyList());
+ router.route(mock(SQLStatementContext.class), "SELECT 1", Collections.emptyList());
} catch (final UnsupportedOperationException ex) {
verify(routingHook).start("SELECT 1");
verify(routingHook).finishFailure(ex);
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/decorator/RouteDecoratorFixture.java b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/decorator/RouteDecoratorFixture.java
index 7f82c8e..785e6f7 100644
--- a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/decorator/RouteDecoratorFixture.java
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/decorator/RouteDecoratorFixture.java
@@ -19,19 +19,22 @@ package org.apache.shardingsphere.infra.route.fixture.decorator;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
-import org.apache.shardingsphere.infra.route.context.DefaultRouteStageContext;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.context.RouteMapper;
import org.apache.shardingsphere.infra.route.context.RouteUnit;
+import org.apache.shardingsphere.infra.route.context.impl.DefaultRouteStageContext;
import org.apache.shardingsphere.infra.route.decorator.RouteDecorator;
import org.apache.shardingsphere.infra.route.fixture.rule.RouteRuleFixture;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
import java.util.Collections;
+import java.util.List;
public final class RouteDecoratorFixture implements RouteDecorator<RouteRuleFixture> {
@Override
- public void decorate(final RouteContext routeContext, final ShardingSphereMetaData metaData, final RouteRuleFixture rule, final ConfigurationProperties props) {
+ public void decorate(final RouteContext routeContext, final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters,
+ final ShardingSphereMetaData metaData, final RouteRuleFixture rule, final ConfigurationProperties props) {
routeContext.getRouteResult().getRouteUnits().add(new RouteUnit(new RouteMapper("ds", "ds_0"), Collections.emptyList()));
routeContext.addNextRouteStageContext(getTypeClass(), new DefaultRouteStageContext());
}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/decorator/RouteFailureDecoratorFixture.java b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/decorator/RouteFailureDecoratorFixture.java
index 0938a2e..dab6b7d 100644
--- a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/decorator/RouteFailureDecoratorFixture.java
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/fixture/decorator/RouteFailureDecoratorFixture.java
@@ -22,11 +22,15 @@ import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.route.context.RouteContext;
import org.apache.shardingsphere.infra.route.decorator.RouteDecorator;
import org.apache.shardingsphere.infra.route.fixture.rule.RouteFailureRuleFixture;
+import org.apache.shardingsphere.sql.parser.binder.statement.SQLStatementContext;
+
+import java.util.List;
public final class RouteFailureDecoratorFixture implements RouteDecorator<RouteFailureRuleFixture> {
@Override
- public void decorate(final RouteContext routeContext, final ShardingSphereMetaData metaData, final RouteFailureRuleFixture rule, final ConfigurationProperties props) {
+ public void decorate(final RouteContext routeContext, final SQLStatementContext<?> sqlStatementContext, final List<Object> parameters,
+ final ShardingSphereMetaData metaData, final RouteFailureRuleFixture rule, final ConfigurationProperties props) {
throw new UnsupportedOperationException("Route failure.");
}