提交 9b758419 编写于 作者: T terrymanu

sqlStatementType => sqlType

上级 f92ec7e4
......@@ -55,18 +55,18 @@ public final class MasterSlaveDataSource extends AbstractDataSourceAdapter {
private final SlaveLoadBalanceStrategy slaveLoadBalanceStrategy = new RoundRobinSlaveLoadBalanceStrategy();
static boolean isDML(final SQLType sqlStatementType) {
return SQLType.SELECT != sqlStatementType || DML_FLAG.get() || HintManagerHolder.isMasterRouteOnly();
static boolean isDML(final SQLType sqlType) {
return SQLType.SELECT != sqlType || DML_FLAG.get() || HintManagerHolder.isMasterRouteOnly();
}
/**
* 获取主或从节点的数据源名称.
*
* @param sqlStatementType SQL类型
* @param sqlType SQL类型
* @return 主或从节点的数据源
*/
public DataSource getDataSource(final SQLType sqlStatementType) {
if (isDML(sqlStatementType)) {
public DataSource getDataSource(final SQLType sqlType) {
if (isDML(sqlType)) {
DML_FLAG.set(true);
return masterDataSource;
}
......
......@@ -57,11 +57,11 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
* 根据数据源名称获取相应的数据库连接.
*
* @param dataSourceName 数据源名称
* @param sqlStatementType SQL语句类型
* @param sqlType SQL语句类型
* @return 数据库连接
*/
public Connection getConnection(final String dataSourceName, final SQLType sqlStatementType) throws SQLException {
Connection result = getConnectionInternal(dataSourceName, sqlStatementType);
public Connection getConnection(final String dataSourceName, final SQLType sqlType) throws SQLException {
Connection result = getConnectionInternal(dataSourceName, sqlType);
replayMethodsInvocation(result);
return result;
}
......@@ -91,8 +91,8 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
return getConnection(shardingContext.getShardingRule().getDataSourceRule().getDataSourceNames().iterator().next(), SQLType.SELECT).getMetaData();
}
private Connection getConnectionInternal(final String dataSourceName, final SQLType sqlStatementType) throws SQLException {
Optional<Connection> connectionOptional = fetchCachedConnectionBySqlStatementType(dataSourceName, sqlStatementType);
private Connection getConnectionInternal(final String dataSourceName, final SQLType sqlType) throws SQLException {
Optional<Connection> connectionOptional = fetchCachedConnectionBySQLType(dataSourceName, sqlType);
if (connectionOptional.isPresent()) {
return connectionOptional.get();
}
......@@ -101,8 +101,8 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
Preconditions.checkState(null != dataSource, "Missing the rule of %s in DataSourceRule", dataSourceName);
String realDataSourceName = dataSourceName;
if (dataSource instanceof MasterSlaveDataSource) {
dataSource = ((MasterSlaveDataSource) dataSource).getDataSource(sqlStatementType);
realDataSourceName = getRealDataSourceName(dataSourceName, sqlStatementType);
dataSource = ((MasterSlaveDataSource) dataSource).getDataSource(sqlType);
realDataSourceName = getRealDataSourceName(dataSourceName, sqlType);
}
Connection result = dataSource.getConnection();
MetricsContext.stop(metricsContext);
......@@ -110,16 +110,16 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
return result;
}
private String getRealDataSourceName(final String dataSourceName, final SQLType sqlStatementType) {
private String getRealDataSourceName(final String dataSourceName, final SQLType sqlType) {
String slaveDataSourceName = getSlaveDataSourceName(dataSourceName);
if (!MasterSlaveDataSource.isDML(sqlStatementType)) {
if (!MasterSlaveDataSource.isDML(sqlType)) {
return slaveDataSourceName;
}
closeConnection(connectionMap.remove(slaveDataSourceName));
return getMasterDataSourceName(dataSourceName);
}
private Optional<Connection> fetchCachedConnectionBySqlStatementType(final String dataSourceName, final SQLType sqlStatementType) {
private Optional<Connection> fetchCachedConnectionBySQLType(final String dataSourceName, final SQLType sqlType) {
if (connectionMap.containsKey(dataSourceName)) {
return Optional.of(connectionMap.get(dataSourceName));
}
......@@ -127,7 +127,7 @@ public final class ShardingConnection extends AbstractConnectionAdapter {
if (connectionMap.containsKey(masterDataSourceName)) {
return Optional.of(connectionMap.get(masterDataSourceName));
}
if (MasterSlaveDataSource.isDML(sqlStatementType)) {
if (MasterSlaveDataSource.isDML(sqlType)) {
return Optional.absent();
}
String slaveDataSourceName = getSlaveDataSourceName(dataSourceName);
......
......@@ -17,17 +17,17 @@
package com.dangdang.ddframe.rdb.sharding.router.binding;
import java.util.Collection;
import com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.ConditionContext;
import com.dangdang.ddframe.rdb.sharding.constant.SQLType;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.ConditionContext;
import com.dangdang.ddframe.rdb.sharding.router.single.SingleTableRouter;
import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import java.util.Collection;
/**
* Binding库表路由类.
*
......@@ -44,13 +44,13 @@ public class BindingTablesRouter {
private final BindingTableRule bindingTableRule;
private final SQLType sqlStatementType;
private final SQLType sqlType;
public BindingTablesRouter(final ShardingRule shardingRule, final Collection<String> logicTables, final ConditionContext conditionContext, final SQLType sqlStatementType) {
public BindingTablesRouter(final ShardingRule shardingRule, final Collection<String> logicTables, final ConditionContext conditionContext, final SQLType sqlType) {
this.shardingRule = shardingRule;
this.logicTables = logicTables;
this.conditionContext = conditionContext;
this.sqlStatementType = sqlStatementType;
this.sqlType = sqlType;
Optional<BindingTableRule> optionalBindingTableRule = shardingRule.findBindingTableRule(logicTables.iterator().next());
Preconditions.checkState(optionalBindingTableRule.isPresent());
bindingTableRule = optionalBindingTableRule.get();
......@@ -65,7 +65,7 @@ public class BindingTablesRouter {
BindingRoutingResult result = null;
for (final String each : logicTables) {
if (null == result) {
result = new BindingRoutingResult(new SingleTableRouter(shardingRule, each, conditionContext, sqlStatementType).route());
result = new BindingRoutingResult(new SingleTableRouter(shardingRule, each, conditionContext, sqlType).route());
} else {
result.bind(bindingTableRule, each);
}
......
......@@ -44,7 +44,7 @@ public class DatabaseRouter {
private final DatabaseShardingStrategy databaseShardingStrategy;
private final SQLType sqlStatementType;
private final SQLType sqlType;
/**
* 根据Hint路由到库.
......@@ -56,7 +56,7 @@ public class DatabaseRouter {
Preconditions.checkState(shardingValueOptional.isPresent());
log.debug("Before database sharding only db:{} sharding values: {}", dataSourceRule.getDataSourceNames(), shardingValueOptional.get());
Collection<String> routedResult = databaseShardingStrategy
.doStaticSharding(sqlStatementType, dataSourceRule.getDataSourceNames(), Collections.<ShardingValue<?>>singleton(shardingValueOptional.get()));
.doStaticSharding(sqlType, dataSourceRule.getDataSourceNames(), Collections.<ShardingValue<?>>singleton(shardingValueOptional.get()));
Preconditions.checkState(!routedResult.isEmpty(), "no database route info");
log.debug("After database sharding only result: {}", routedResult);
return new DatabaseRoutingResult(routedResult);
......
......@@ -17,12 +17,9 @@
package com.dangdang.ddframe.rdb.sharding.router.mixed;
import java.util.ArrayList;
import java.util.Collection;
import com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.ConditionContext;
import com.dangdang.ddframe.rdb.sharding.constant.SQLType;
import com.dangdang.ddframe.rdb.sharding.parsing.parser.context.ConditionContext;
import com.dangdang.ddframe.rdb.sharding.router.RoutingResult;
import com.dangdang.ddframe.rdb.sharding.router.binding.BindingTablesRouter;
import com.dangdang.ddframe.rdb.sharding.router.single.SingleRoutingResult;
......@@ -30,6 +27,9 @@ import com.dangdang.ddframe.rdb.sharding.router.single.SingleTableRouter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.Collection;
/**
* 混合多库表路由类.
*
......@@ -46,7 +46,7 @@ public class MixedTablesRouter {
private final ConditionContext conditionContext;
private final SQLType sqlStatementType;
private final SQLType sqlType;
/**
* 路由.
......@@ -59,11 +59,11 @@ public class MixedTablesRouter {
Collection<String> remainingTables = new ArrayList<>(logicTables);
Collection<SingleRoutingResult> result = new ArrayList<>(logicTables.size());
if (1 < bindingTables.size()) {
result.add(new BindingTablesRouter(shardingRule, bindingTables, conditionContext, sqlStatementType).route());
result.add(new BindingTablesRouter(shardingRule, bindingTables, conditionContext, sqlType).route());
remainingTables.removeAll(bindingTables);
}
for (String each : remainingTables) {
SingleRoutingResult routingResult = new SingleTableRouter(shardingRule, each, conditionContext, sqlStatementType).route();
SingleRoutingResult routingResult = new SingleTableRouter(shardingRule, each, conditionContext, sqlType).route();
if (null != routingResult) {
result.add(routingResult);
}
......
......@@ -59,13 +59,13 @@ public final class SingleTableRouter {
private final TableRule tableRule;
private final SQLType sqlStatementType;
private final SQLType sqlType;
public SingleTableRouter(final ShardingRule shardingRule, final String logicTable, final ConditionContext conditionContext, final SQLType sqlStatementType) {
public SingleTableRouter(final ShardingRule shardingRule, final String logicTable, final ConditionContext conditionContext, final SQLType sqlType) {
this.shardingRule = shardingRule;
this.logicTable = logicTable;
this.conditionContext = conditionContext;
this.sqlStatementType = sqlStatementType;
this.sqlType = sqlType;
Optional<TableRule> tableRuleOptional = shardingRule.tryFindTableRule(logicTable);
if (tableRuleOptional.isPresent()) {
tableRule = tableRuleOptional.get();
......@@ -105,7 +105,7 @@ public final class SingleTableRouter {
shardingValues = getShardingValues(strategy.getShardingColumns());
}
logBeforeRoute("database", logicTable, tableRule.getActualDatasourceNames(), strategy.getShardingColumns(), shardingValues);
Collection<String> result = new HashSet<>(strategy.doStaticSharding(sqlStatementType, tableRule.getActualDatasourceNames(), shardingValues));
Collection<String> result = new HashSet<>(strategy.doStaticSharding(sqlType, tableRule.getActualDatasourceNames(), shardingValues));
logAfterRoute("database", logicTable, result);
Preconditions.checkState(!result.isEmpty(), "no database route info");
return result;
......@@ -124,7 +124,7 @@ public final class SingleTableRouter {
if (tableRule.isDynamic()) {
result = new HashSet<>(strategy.doDynamicSharding(shardingValues));
} else {
result = new HashSet<>(strategy.doStaticSharding(sqlStatementType, tableRule.getActualTableNames(routedDataSources), shardingValues));
result = new HashSet<>(strategy.doStaticSharding(sqlType, tableRule.getActualTableNames(routedDataSources), shardingValues));
}
logAfterRoute("table", logicTable, result);
Preconditions.checkState(!result.isEmpty(), "no table route info");
......
......@@ -51,14 +51,14 @@ public class ShardingStrategy {
/**
* 计算静态分片.
*
* @param sqlStatementType SQL语句的类型
* @param sqlType SQL语句的类型
* @param availableTargetNames 所有的可用分片资源集合
* @param shardingValues 分片值集合
* @return 分库后指向的数据源名称集合
*/
public Collection<String> doStaticSharding(final SQLType sqlStatementType, final Collection<String> availableTargetNames, final Collection<ShardingValue<?>> shardingValues) {
public Collection<String> doStaticSharding(final SQLType sqlType, final Collection<String> availableTargetNames, final Collection<ShardingValue<?>> shardingValues) {
if (shardingValues.isEmpty()) {
Preconditions.checkState(!isInsertMultiple(sqlStatementType, availableTargetNames), "INSERT statement should contain sharding value.");
Preconditions.checkState(!isInsertMultiple(sqlType, availableTargetNames), "INSERT statement should contain sharding value.");
return availableTargetNames;
}
return doSharding(shardingValues, availableTargetNames);
......@@ -101,7 +101,7 @@ public class ShardingStrategy {
throw new UnsupportedOperationException(shardingAlgorithm.getClass().getName());
}
private boolean isInsertMultiple(final SQLType sqlStatementType, final Collection<String> availableTargetNames) {
return SQLType.INSERT.equals(sqlStatementType) && availableTargetNames.size() > 1;
private boolean isInsertMultiple(final SQLType sqlType, final Collection<String> availableTargetNames) {
return SQLType.INSERT.equals(sqlType) && availableTargetNames.size() > 1;
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册