Use SQLRouteExecutor to refactor SQL route engine (#8009)
* For code format
* Use SQLRouteExecutor to refactor SQL route engine
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 3542feb..05f17c7 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
@@ -82,7 +82,7 @@
getTestParameters().getInputParameters(), sqlStatementParserEngine.parse(getTestParameters().getInputSQL(), false));
LogicSQL logicSQL = new LogicSQL(sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters());
ShardingSphereSchema schema = new ShardingSphereSchema("sharding_db", Collections.emptyList(), rules, Collections.emptyMap(), metaData);
- RouteContext routeContext = new SQLRouteEngine(props, rules).route(logicSQL, schema);
+ RouteContext routeContext = new SQLRouteEngine(rules, props).route(logicSQL, schema);
SQLRewriteResult sqlRewriteResult = new SQLRewriteEntry(metaData.getSchemaMetaData().getConfiguredSchemaMetaData(),
props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), sqlStatementContext, routeContext);
return sqlRewriteResult instanceof GenericSQLRewriteResult
diff --git a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableAddressingMetaDataDecorator.java b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableAddressingMetaDataDecorator.java
index aa80389..9a8fc45 100644
--- a/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableAddressingMetaDataDecorator.java
+++ b/shardingsphere-features/shardingsphere-sharding/shardingsphere-sharding-common/src/main/java/org/apache/shardingsphere/sharding/metadata/ShardingTableAddressingMetaDataDecorator.java
@@ -38,10 +38,13 @@
}
private void decorate(final TableRule tableRule, final TableAddressingMetaData metaData) {
+ boolean found = false;
for (String each : tableRule.getActualDataNodes().stream().map(DataNode::getTableName).collect(Collectors.toSet())) {
- metaData.getTableDataSourceNamesMapper().remove(each);
+ found = null != metaData.getTableDataSourceNamesMapper().remove(each) || found;
}
- metaData.getTableDataSourceNamesMapper().put(tableRule.getLogicTable(), new LinkedList<>(tableRule.getActualDatasourceNames()));
+ if (found) {
+ metaData.getTableDataSourceNamesMapper().put(tableRule.getLogicTable(), new LinkedList<>(tableRule.getActualDatasourceNames()));
+ }
}
@Override
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 2e09c66..53dc922 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
@@ -89,7 +89,7 @@
getTestParameters().getInputParameters(), sqlStatementParserEngine.parse(getTestParameters().getInputSQL(), false));
LogicSQL logicSQL = new LogicSQL(sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters());
ShardingSphereSchema schema = new ShardingSphereSchema("sharding_db", Collections.emptyList(), rules, Collections.emptyMap(), metaData);
- RouteContext routeContext = new SQLRouteEngine(props, rules).route(logicSQL, schema);
+ RouteContext routeContext = new SQLRouteEngine(rules, props).route(logicSQL, schema);
SQLRewriteResult sqlRewriteResult = new SQLRewriteEntry(metaData.getSchemaMetaData().getConfiguredSchemaMetaData(),
props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), sqlStatementContext, routeContext);
return sqlRewriteResult instanceof GenericSQLRewriteResult
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 a75b8d2..f3505a8 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
@@ -89,7 +89,7 @@
getTestParameters().getInputParameters(), sqlStatementParserEngine.parse(getTestParameters().getInputSQL(), false));
LogicSQL logicSQL = new LogicSQL(sqlStatementContext, getTestParameters().getInputSQL(), getTestParameters().getInputParameters());
ShardingSphereSchema schema = new ShardingSphereSchema("sharding_db", Collections.emptyList(), rules, Collections.emptyMap(), metaData);
- RouteContext routeContext = new SQLRouteEngine(props, rules).route(logicSQL, schema);
+ RouteContext routeContext = new SQLRouteEngine(rules, props).route(logicSQL, schema);
SQLRewriteResult sqlRewriteResult = new SQLRewriteEntry(metaData.getSchemaMetaData().getConfiguredSchemaMetaData(),
props, rules).rewrite(getTestParameters().getInputSQL(), getTestParameters().getInputParameters(), sqlStatementContext, routeContext);
return sqlRewriteResult instanceof GenericSQLRewriteResult
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 5a6076c..84990e4 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
@@ -62,7 +62,7 @@
metaData.getSchemaMetaData().getConfiguredSchemaMetaData(), parameters, sqlStatementParserEngine.parse(sql, false));
LogicSQL logicSQL = new LogicSQL(sqlStatementContext, sql, parameters);
ShardingSphereSchema schema = new ShardingSphereSchema("sharding_db", Collections.emptyList(), Collections.singleton(shardingRule), Collections.emptyMap(), metaData);
- RouteContext result = new SQLRouteEngine(props, Collections.singletonList(shardingRule)).route(logicSQL, schema);
+ RouteContext result = new SQLRouteEngine(Collections.singletonList(shardingRule), props).route(logicSQL, schema);
assertThat(result.getRouteUnits().size(), is(1));
return result;
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/model/logic/LogicSchemaMetaDataLoader.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/model/logic/LogicSchemaMetaDataLoader.java
index 1600e15..858ff63 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/model/logic/LogicSchemaMetaDataLoader.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/model/logic/LogicSchemaMetaDataLoader.java
@@ -136,8 +136,8 @@
return load(databaseType, dataSourceMap, tableName, props);
}
- private Map<String, Collection<String>> loadUnConfiguredSchemaMetaData(final DatabaseType databaseType, final Map<String, DataSource> dataSourceMap,
- final Collection<String> excludedTableNames) throws SQLException {
+ private Map<String, Collection<String>> loadUnConfiguredSchemaMetaData(final DatabaseType databaseType,
+ final Map<String, DataSource> dataSourceMap, final Collection<String> excludedTableNames) throws SQLException {
Map<String, Collection<String>> result = new HashMap<>(dataSourceMap.size(), 1);
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
Collection<String> tableNames = PhysicalSchemaMetaDataLoader.loadTableNames(entry.getValue(), databaseType, excludedTableNames);
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 96c81ff..ddcc910 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
@@ -47,7 +47,7 @@
*/
public ExecutionContext generateExecutionContext(final LogicSQL logicSQL, final ShardingSphereSchema schema, final ConfigurationProperties props) {
Collection<ShardingSphereRule> rules = schema.getRules();
- SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(props, rules);
+ SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(rules, props);
SQLStatementContext<?> sqlStatementContext = logicSQL.getSqlStatementContext();
RouteContext routeContext = sqlRouteEngine.route(logicSQL, schema);
SQLRewriteEntry rewriteEntry = new SQLRewriteEntry(schema.getMetaData().getSchemaMetaData().getConfiguredSchemaMetaData(), props, rules);
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/UnconfiguredSchemaSQLRouter.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/UnconfiguredSchemaSQLRouter.java
deleted file mode 100644
index 9f27624..0000000
--- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/UnconfiguredSchemaSQLRouter.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.shardingsphere.infra.route;
-
-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.schema.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.binder.LogicSQL;
-import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
-import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement;
-
-import java.util.Collections;
-
-/**
- * Unconfigured schema SQL router.
- */
-public final class UnconfiguredSchemaSQLRouter {
-
- /**
- * Decorate route context.
- *
- * @param routeContext route context
- * @param logicSQL logic SQL
- * @param schema ShardingSphere schema
- */
- public void decorate(final RouteContext routeContext, final LogicSQL logicSQL, final ShardingSphereSchema schema) {
- if (isNeedUnconfiguredSchema(logicSQL.getSqlStatementContext().getSqlStatement())) {
- for (String each : schema.getMetaData().getSchemaMetaData().getUnconfiguredSchemaMetaDataMap().keySet()) {
- routeContext.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList()));
- }
- }
- }
-
- // TODO use dynamic config to judge UnconfiguredSchema
- private boolean isNeedUnconfiguredSchema(final SQLStatement sqlStatement) {
- return sqlStatement instanceof MySQLShowTablesStatement;
- }
-}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java
index 4d9e8e3..a5a489c 100644
--- a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngine.java
@@ -17,42 +17,31 @@
package org.apache.shardingsphere.infra.route.engine;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.infra.binder.LogicSQL;
import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
-import org.apache.shardingsphere.infra.route.SQLRouter;
-import org.apache.shardingsphere.infra.route.UnconfiguredSchemaSQLRouter;
import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.infra.route.engine.impl.AllSQLRouteExecutor;
+import org.apache.shardingsphere.infra.route.engine.impl.PartialSQLRouteExecutor;
import org.apache.shardingsphere.infra.route.hook.SPIRoutingHook;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.infra.schema.ShardingSphereSchema;
-import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
-import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry;
-import org.apache.shardingsphere.infra.binder.LogicSQL;
+import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
+import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dal.MySQLShowTablesStatement;
import java.util.Collection;
-import java.util.Map;
-import java.util.Map.Entry;
/**
* SQL route engine.
*/
+@RequiredArgsConstructor
public final class SQLRouteEngine {
- static {
- ShardingSphereServiceLoader.register(SQLRouter.class);
- }
+ private final Collection<ShardingSphereRule> rules;
private final ConfigurationProperties props;
- @SuppressWarnings("rawtypes")
- private final Map<ShardingSphereRule, SQLRouter> routers;
-
- private final SPIRoutingHook routingHook;
-
- public SQLRouteEngine(final ConfigurationProperties props, final Collection<ShardingSphereRule> rules) {
- this.props = props;
- routers = OrderedSPIRegistry.getRegisteredServices(rules, SQLRouter.class);
- routingHook = new SPIRoutingHook();
- }
+ private final SPIRoutingHook routingHook = new SPIRoutingHook();
/**
* Route SQL.
@@ -64,7 +53,8 @@
public RouteContext route(final LogicSQL logicSQL, final ShardingSphereSchema schema) {
routingHook.start(logicSQL.getSql());
try {
- RouteContext result = doRoute(logicSQL, schema);
+ SQLRouteExecutor executor = isNeedAllSchemas(logicSQL.getSqlStatementContext().getSqlStatement()) ? new AllSQLRouteExecutor() : new PartialSQLRouteExecutor(rules, props);
+ RouteContext result = executor.route(logicSQL, schema);
routingHook.finishSuccess(result, schema.getMetaData().getSchemaMetaData().getConfiguredSchemaMetaData());
return result;
// CHECKSTYLE:OFF
@@ -75,17 +65,8 @@
}
}
- @SuppressWarnings({"unchecked", "rawtypes"})
- private RouteContext doRoute(final LogicSQL logicSQL, final ShardingSphereSchema schema) {
- RouteContext result = new RouteContext();
- for (Entry<ShardingSphereRule, SQLRouter> entry : routers.entrySet()) {
- if (result.getRouteUnits().isEmpty()) {
- result = entry.getValue().createRouteContext(logicSQL, schema, entry.getKey(), props);
- } else {
- entry.getValue().decorateRouteContext(result, logicSQL, schema, entry.getKey(), props);
- }
- }
- new UnconfiguredSchemaSQLRouter().decorate(result, logicSQL, schema);
- return result;
+ // TODO use dynamic config to judge UnconfiguredSchema
+ private boolean isNeedAllSchemas(final SQLStatement sqlStatement) {
+ return sqlStatement instanceof MySQLShowTablesStatement;
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java
new file mode 100644
index 0000000..8ebdb48
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/SQLRouteExecutor.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.route.engine;
+
+import org.apache.shardingsphere.infra.binder.LogicSQL;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.infra.schema.ShardingSphereSchema;
+
+/**
+ * SQL route executor.
+ */
+public interface SQLRouteExecutor {
+
+ /**
+ * Route.
+ *
+ * @param logicSQL logic SQL
+ * @param schema ShardingSphere schema
+ * @return route context
+ */
+ RouteContext route(LogicSQL logicSQL, ShardingSphereSchema schema);
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java
new file mode 100644
index 0000000..0c50771
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/AllSQLRouteExecutor.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.route.engine.impl;
+
+import org.apache.shardingsphere.infra.binder.LogicSQL;
+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.engine.SQLRouteExecutor;
+import org.apache.shardingsphere.infra.schema.ShardingSphereSchema;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashSet;
+
+/**
+ * All SQL route executor.
+ */
+public final class AllSQLRouteExecutor implements SQLRouteExecutor {
+
+ @Override
+ public RouteContext route(final LogicSQL logicSQL, final ShardingSphereSchema schema) {
+ RouteContext result = new RouteContext();
+ for (String each : getAllDataSourceNames(schema)) {
+ result.getRouteUnits().add(new RouteUnit(new RouteMapper(each, each), Collections.emptyList()));
+ }
+ return result;
+ }
+
+ private Collection<String> getAllDataSourceNames(final ShardingSphereSchema schema) {
+ Collection<String> result = new LinkedHashSet<>();
+ for (Collection<String> each : schema.getMetaData().getTableAddressingMetaData().getTableDataSourceNamesMapper().values()) {
+ result.addAll(each);
+ }
+ return result;
+ }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java
new file mode 100644
index 0000000..d5daa1a
--- /dev/null
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/main/java/org/apache/shardingsphere/infra/route/engine/impl/PartialSQLRouteExecutor.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.route.engine.impl;
+
+import org.apache.shardingsphere.infra.binder.LogicSQL;
+import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
+import org.apache.shardingsphere.infra.route.SQLRouter;
+import org.apache.shardingsphere.infra.route.context.RouteContext;
+import org.apache.shardingsphere.infra.route.engine.SQLRouteExecutor;
+import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
+import org.apache.shardingsphere.infra.schema.ShardingSphereSchema;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.spi.ordered.OrderedSPIRegistry;
+
+import java.util.Collection;
+import java.util.Map;
+import java.util.Map.Entry;
+
+/**
+ * Partial SQL route executor.
+ */
+public final class PartialSQLRouteExecutor implements SQLRouteExecutor {
+
+ static {
+ ShardingSphereServiceLoader.register(SQLRouter.class);
+ }
+
+ private final ConfigurationProperties props;
+
+ @SuppressWarnings("rawtypes")
+ private final Map<ShardingSphereRule, SQLRouter> routers;
+
+ public PartialSQLRouteExecutor(final Collection<ShardingSphereRule> rules, final ConfigurationProperties props) {
+ this.props = props;
+ routers = OrderedSPIRegistry.getRegisteredServices(rules, SQLRouter.class);
+ }
+
+ @Override
+ @SuppressWarnings({"unchecked", "rawtypes"})
+ public RouteContext route(final LogicSQL logicSQL, final ShardingSphereSchema schema) {
+ RouteContext result = new RouteContext();
+ for (Entry<ShardingSphereRule, SQLRouter> entry : routers.entrySet()) {
+ if (result.getRouteUnits().isEmpty()) {
+ result = entry.getValue().createRouteContext(logicSQL, schema, entry.getKey(), props);
+ } else {
+ entry.getValue().decorateRouteContext(result, logicSQL, schema, entry.getKey(), props);
+ }
+ }
+ return result;
+ }
+}
diff --git a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java
index 25dbd01..b11b10b 100644
--- a/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-route/src/test/java/org/apache/shardingsphere/infra/route/engine/SQLRouteEngineTest.java
@@ -67,7 +67,7 @@
public void assertRouteSuccess() {
LogicSQL logicSQL = new LogicSQL(mock(SQLStatementContext.class), "SELECT 1", Collections.emptyList());
ShardingSphereSchema schema = new ShardingSphereSchema("logic_schema", Collections.emptyList(), Collections.singleton(new RouteRuleFixture()), Collections.emptyMap(), metaData);
- SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(props, Collections.singleton(new RouteRuleFixture()));
+ SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(Collections.singleton(new RouteRuleFixture()), props);
setSPIRoutingHook(sqlRouteEngine);
RouteContext actual = sqlRouteEngine.route(logicSQL, schema);
assertThat(actual.getRouteUnits().size(), is(1));
@@ -83,7 +83,7 @@
public void assertRouteFailure() {
LogicSQL logicSQL = new LogicSQL(mock(SQLStatementContext.class), "SELECT 1", Collections.emptyList());
ShardingSphereSchema schema = new ShardingSphereSchema("logic_schema", Collections.emptyList(), Collections.singleton(new RouteRuleFixture()), Collections.emptyMap(), metaData);
- SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(props, Collections.singleton(new RouteFailureRuleFixture()));
+ SQLRouteEngine sqlRouteEngine = new SQLRouteEngine(Collections.singleton(new RouteFailureRuleFixture()), props);
setSPIRoutingHook(sqlRouteEngine);
try {
sqlRouteEngine.route(logicSQL, schema);