use of com.dangdang.ddframe.rdb.sharding.hint.ShardingKey in project sharding-jdbc by dangdangdotcom.
the class HintManagerTest method assertAddTableShardingValueForIn.
@Test
public void assertAddTableShardingValueForIn() {
try (HintManager hintManager = HintManager.getInstance()) {
hintManager.addTableShardingValue("logicTable", "shardingColumn", Condition.BinaryOperator.IN, 1, 3, 5);
ShardingKey shardingKey = new ShardingKey("logicTable", "shardingColumn");
assertTrue(HintManagerHolder.getTableShardingValue(shardingKey).isPresent());
assertThat(hintManager.getTableShardingValue(shardingKey).getColumnName(), is("shardingColumn"));
assertThat(hintManager.getTableShardingValue(shardingKey).getType(), is(ShardingValue.ShardingValueType.LIST));
assertThat(hintManager.getTableShardingValue(shardingKey).getValues().size(), is(3));
}
}
use of com.dangdang.ddframe.rdb.sharding.hint.ShardingKey in project sharding-jdbc by dangdangdotcom.
the class HintManagerTest method assertAddDatabaseShardingValueForIn.
@Test
public void assertAddDatabaseShardingValueForIn() {
try (HintManager hintManager = HintManager.getInstance()) {
hintManager.addDatabaseShardingValue("logicTable", "shardingColumn", Condition.BinaryOperator.IN, 1, 3, 5);
ShardingKey shardingKey = new ShardingKey("logicTable", "shardingColumn");
assertTrue(HintManagerHolder.getDatabaseShardingValue(shardingKey).isPresent());
assertThat(hintManager.getDatabaseShardingValue(shardingKey).getColumnName(), is("shardingColumn"));
assertThat(hintManager.getDatabaseShardingValue(shardingKey).getType(), is(ShardingValue.ShardingValueType.LIST));
assertThat(hintManager.getDatabaseShardingValue(shardingKey).getValues().size(), is(3));
}
}
use of com.dangdang.ddframe.rdb.sharding.hint.ShardingKey in project sharding-jdbc by dangdangdotcom.
the class HintManagerTest method assertAddDatabaseShardingValueForEquals.
@Test
public void assertAddDatabaseShardingValueForEquals() {
try (HintManager hintManager = HintManager.getInstance()) {
hintManager.addDatabaseShardingValue("logicTable", "shardingColumn", 1);
ShardingKey shardingKey = new ShardingKey("logicTable", "shardingColumn");
assertTrue(HintManagerHolder.getDatabaseShardingValue(shardingKey).isPresent());
assertThat(hintManager.getDatabaseShardingValue(shardingKey).getColumnName(), is("shardingColumn"));
assertThat(hintManager.getDatabaseShardingValue(shardingKey).getType(), is(ShardingValue.ShardingValueType.SINGLE));
assertThat(hintManager.getDatabaseShardingValue(shardingKey).getValue(), is((Comparable) 1));
}
}
use of com.dangdang.ddframe.rdb.sharding.hint.ShardingKey in project sharding-jdbc by dangdangdotcom.
the class DatabaseRouter method route.
/**
* 根据Hint路由到库.
*
* @return 库路由结果
*/
public DatabaseRoutingResult route() {
Optional<ShardingValue<?>> shardingValueOptional = HintManagerHolder.getDatabaseShardingValue(new ShardingKey(HintManagerHolder.DB_TABLE_NAME, HintManagerHolder.DB_COLUMN_NAME));
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()));
Preconditions.checkState(!routedResult.isEmpty(), "no database route info");
log.debug("After database sharding only result: {}", routedResult);
return new DatabaseRoutingResult(routedResult);
}
use of com.dangdang.ddframe.rdb.sharding.hint.ShardingKey in project sharding-jdbc by dangdangdotcom.
the class HintManager method addTableShardingValue.
/**
* 添加分表分片值.
*
* @param logicTable 逻辑表名称
* @param shardingColumn 分片键
* @param binaryOperator 分片操作符
* @param values 分片值
*/
public void addTableShardingValue(final String logicTable, final String shardingColumn, final Condition.BinaryOperator binaryOperator, final Comparable<?>... values) {
shardingHint = true;
tableShardingValues.put(new ShardingKey(logicTable, shardingColumn), getShardingValue(logicTable, shardingColumn, binaryOperator, values));
}
Aggregations