提交 8b786703 编写于 作者: T terrymanu

refactor Router parameters

上级 353a7ae5
...@@ -106,13 +106,13 @@ public final class ParsingSQLRouter implements SQLRouter { ...@@ -106,13 +106,13 @@ public final class ParsingSQLRouter implements SQLRouter {
private RoutingResult route(final List<Object> parameters, final SQLStatement sqlStatement) { private RoutingResult route(final List<Object> parameters, final SQLStatement sqlStatement) {
Collection<String> tableNames = sqlStatement.getTables().getTableNames(); Collection<String> tableNames = sqlStatement.getTables().getTableNames();
if (1 == tableNames.size()) { if (1 == tableNames.size()) {
return new SingleTableRouter(shardingRule, parameters, tableNames.iterator().next(), sqlStatement, sqlStatement.getType()).route(); return new SingleTableRouter(shardingRule, parameters, tableNames.iterator().next(), sqlStatement).route();
} }
if (shardingRule.isAllBindingTables(tableNames)) { if (shardingRule.isAllBindingTables(tableNames)) {
return new BindingTablesRouter(shardingRule, parameters, tableNames, sqlStatement, sqlStatement.getType()).route(); return new BindingTablesRouter(shardingRule, parameters, tableNames, sqlStatement).route();
} }
// TODO 可配置是否执行笛卡尔积 // TODO 可配置是否执行笛卡尔积
return new MixedTablesRouter(shardingRule, parameters, tableNames, sqlStatement, sqlStatement.getType()).route(); return new MixedTablesRouter(shardingRule, parameters, tableNames, sqlStatement).route();
} }
private void logSQLRouteResult(final SQLRouteResult routeResult, final List<Object> parameters) { private void logSQLRouteResult(final SQLRouteResult routeResult, final List<Object> parameters) {
......
...@@ -19,7 +19,6 @@ package com.dangdang.ddframe.rdb.sharding.routing.type.binding; ...@@ -19,7 +19,6 @@ package com.dangdang.ddframe.rdb.sharding.routing.type.binding;
import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule; import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.constant.SQLType;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.statement.SQLStatement; import com.dangdang.ddframe.rdb.sharding.parsing.parser.statement.SQLStatement;
import com.dangdang.ddframe.rdb.sharding.routing.type.single.SingleTableRouter; import com.dangdang.ddframe.rdb.sharding.routing.type.single.SingleTableRouter;
import com.google.common.base.Optional; import com.google.common.base.Optional;
...@@ -47,14 +46,11 @@ public final class BindingTablesRouter { ...@@ -47,14 +46,11 @@ public final class BindingTablesRouter {
private final BindingTableRule bindingTableRule; private final BindingTableRule bindingTableRule;
private final SQLType sqlType; public BindingTablesRouter(final ShardingRule shardingRule, final List<Object> parameters, final Collection<String> logicTables, final SQLStatement sqlStatement) {
public BindingTablesRouter(final ShardingRule shardingRule, final List<Object> parameters, final Collection<String> logicTables, final SQLStatement sqlStatement, final SQLType sqlType) {
this.shardingRule = shardingRule; this.shardingRule = shardingRule;
this.parameters = parameters; this.parameters = parameters;
this.logicTables = logicTables; this.logicTables = logicTables;
this.sqlStatement = sqlStatement; this.sqlStatement = sqlStatement;
this.sqlType = sqlType;
Optional<BindingTableRule> optionalBindingTableRule = shardingRule.findBindingTableRule(logicTables.iterator().next()); Optional<BindingTableRule> optionalBindingTableRule = shardingRule.findBindingTableRule(logicTables.iterator().next());
Preconditions.checkState(optionalBindingTableRule.isPresent()); Preconditions.checkState(optionalBindingTableRule.isPresent());
bindingTableRule = optionalBindingTableRule.get(); bindingTableRule = optionalBindingTableRule.get();
...@@ -69,7 +65,7 @@ public final class BindingTablesRouter { ...@@ -69,7 +65,7 @@ public final class BindingTablesRouter {
BindingRoutingResult result = null; BindingRoutingResult result = null;
for (final String each : logicTables) { for (final String each : logicTables) {
if (null == result) { if (null == result) {
result = new BindingRoutingResult(new SingleTableRouter(shardingRule, parameters, each, sqlStatement, sqlType).route()); result = new BindingRoutingResult(new SingleTableRouter(shardingRule, parameters, each, sqlStatement).route());
} else { } else {
result.bind(bindingTableRule, each); result.bind(bindingTableRule, each);
} }
......
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
package com.dangdang.ddframe.rdb.sharding.routing.type.mixed; package com.dangdang.ddframe.rdb.sharding.routing.type.mixed;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule; import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.constant.SQLType;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.statement.SQLStatement; import com.dangdang.ddframe.rdb.sharding.parsing.parser.statement.SQLStatement;
import com.dangdang.ddframe.rdb.sharding.routing.RoutingResult; import com.dangdang.ddframe.rdb.sharding.routing.RoutingResult;
import com.dangdang.ddframe.rdb.sharding.routing.type.binding.BindingTablesRouter; import com.dangdang.ddframe.rdb.sharding.routing.type.binding.BindingTablesRouter;
...@@ -49,8 +48,6 @@ public final class MixedTablesRouter { ...@@ -49,8 +48,6 @@ public final class MixedTablesRouter {
private final SQLStatement sqlStatement; private final SQLStatement sqlStatement;
private final SQLType sqlType;
/** /**
* 路由. * 路由.
* *
...@@ -62,11 +59,11 @@ public final class MixedTablesRouter { ...@@ -62,11 +59,11 @@ public final class MixedTablesRouter {
Collection<String> remainingTables = new ArrayList<>(logicTables); Collection<String> remainingTables = new ArrayList<>(logicTables);
Collection<SingleRoutingResult> result = new ArrayList<>(logicTables.size()); Collection<SingleRoutingResult> result = new ArrayList<>(logicTables.size());
if (1 < bindingTables.size()) { if (1 < bindingTables.size()) {
result.add(new BindingTablesRouter(shardingRule, parameters, bindingTables, sqlStatement, sqlType).route()); result.add(new BindingTablesRouter(shardingRule, parameters, bindingTables, sqlStatement).route());
remainingTables.removeAll(bindingTables); remainingTables.removeAll(bindingTables);
} }
for (String each : remainingTables) { for (String each : remainingTables) {
SingleRoutingResult routingResult = new SingleTableRouter(shardingRule, parameters, each, sqlStatement, sqlType).route(); SingleRoutingResult routingResult = new SingleTableRouter(shardingRule, parameters, each, sqlStatement).route();
if (null != routingResult) { if (null != routingResult) {
result.add(routingResult); result.add(routingResult);
} }
......
...@@ -26,7 +26,6 @@ import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingS ...@@ -26,7 +26,6 @@ import com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingS
import com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm; import com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm; import com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm;
import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy; import com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy;
import com.dangdang.ddframe.rdb.sharding.constant.SQLType;
import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder; import com.dangdang.ddframe.rdb.sharding.hint.HintManagerHolder;
import com.dangdang.ddframe.rdb.sharding.hint.ShardingKey; import com.dangdang.ddframe.rdb.sharding.hint.ShardingKey;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.Column; import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.Column;
...@@ -63,14 +62,11 @@ public final class SingleTableRouter { ...@@ -63,14 +62,11 @@ public final class SingleTableRouter {
private final TableRule tableRule; private final TableRule tableRule;
private final SQLType sqlType; public SingleTableRouter(final ShardingRule shardingRule, final List<Object> parameters, final String logicTable, final SQLStatement sqlStatement) {
public SingleTableRouter(final ShardingRule shardingRule, final List<Object> parameters, final String logicTable, final SQLStatement sqlStatement, final SQLType sqlType) {
this.shardingRule = shardingRule; this.shardingRule = shardingRule;
this.parameters = parameters; this.parameters = parameters;
this.logicTable = logicTable; this.logicTable = logicTable;
this.sqlStatement = sqlStatement; this.sqlStatement = sqlStatement;
this.sqlType = sqlType;
Optional<TableRule> tableRuleOptional = shardingRule.tryFindTableRule(logicTable); Optional<TableRule> tableRuleOptional = shardingRule.tryFindTableRule(logicTable);
if (tableRuleOptional.isPresent()) { if (tableRuleOptional.isPresent()) {
tableRule = tableRuleOptional.get(); tableRule = tableRuleOptional.get();
...@@ -110,7 +106,7 @@ public final class SingleTableRouter { ...@@ -110,7 +106,7 @@ public final class SingleTableRouter {
shardingValues = getShardingValues(strategy.getShardingColumns()); shardingValues = getShardingValues(strategy.getShardingColumns());
} }
logBeforeRoute("database", logicTable, tableRule.getActualDatasourceNames(), strategy.getShardingColumns(), shardingValues); logBeforeRoute("database", logicTable, tableRule.getActualDatasourceNames(), strategy.getShardingColumns(), shardingValues);
Collection<String> result = new HashSet<>(strategy.doStaticSharding(sqlType, tableRule.getActualDatasourceNames(), shardingValues)); Collection<String> result = new HashSet<>(strategy.doStaticSharding(sqlStatement.getType(), tableRule.getActualDatasourceNames(), shardingValues));
logAfterRoute("database", logicTable, result); logAfterRoute("database", logicTable, result);
Preconditions.checkState(!result.isEmpty(), "no database route info"); Preconditions.checkState(!result.isEmpty(), "no database route info");
return result; return result;
...@@ -129,7 +125,7 @@ public final class SingleTableRouter { ...@@ -129,7 +125,7 @@ public final class SingleTableRouter {
if (tableRule.isDynamic()) { if (tableRule.isDynamic()) {
result = new HashSet<>(strategy.doDynamicSharding(shardingValues)); result = new HashSet<>(strategy.doDynamicSharding(shardingValues));
} else { } else {
result = new HashSet<>(strategy.doStaticSharding(sqlType, tableRule.getActualTableNames(routedDataSources), shardingValues)); result = new HashSet<>(strategy.doStaticSharding(sqlStatement.getType(), tableRule.getActualTableNames(routedDataSources), shardingValues));
} }
logAfterRoute("table", logicTable, result); logAfterRoute("table", logicTable, result);
Preconditions.checkState(!result.isEmpty(), "no table route info"); Preconditions.checkState(!result.isEmpty(), "no table route info");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册