Search in sources :

Example 1 with ShardingKey

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));
    }
}
Also used : ShardingKey(com.dangdang.ddframe.rdb.sharding.hint.ShardingKey) Test(org.junit.Test)

Example 2 with ShardingKey

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));
    }
}
Also used : ShardingKey(com.dangdang.ddframe.rdb.sharding.hint.ShardingKey) Test(org.junit.Test)

Example 3 with ShardingKey

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));
    }
}
Also used : ShardingKey(com.dangdang.ddframe.rdb.sharding.hint.ShardingKey) Test(org.junit.Test)

Example 4 with ShardingKey

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);
}
Also used : ShardingKey(com.dangdang.ddframe.rdb.sharding.hint.ShardingKey) ShardingValue(com.dangdang.ddframe.rdb.sharding.api.ShardingValue)

Example 5 with ShardingKey

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));
}
Also used : ShardingKey(com.dangdang.ddframe.rdb.sharding.hint.ShardingKey)

Aggregations

ShardingKey (com.dangdang.ddframe.rdb.sharding.hint.ShardingKey)9 Test (org.junit.Test)6 ShardingValue (com.dangdang.ddframe.rdb.sharding.api.ShardingValue)1