Search in sources :

Example 16 with ShardingDataSource

use of com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource in project sharding-jdbc by dangdangdotcom.

the class AbstractShardingTablesOnlyDBUnitTest method getShardingDataSource.

protected final ShardingDataSource getShardingDataSource() {
    if (null != shardingDataSource && !isShutdown) {
        return shardingDataSource;
    }
    isShutdown = false;
    DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap("dataSource_%s"));
    TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList("t_order_0", "t_order_1", "t_order_2", "t_order_3", "t_order_4", "t_order_5", "t_order_6", "t_order_7", "t_order_8", "t_order_9")).dataSourceRule(dataSourceRule).build();
    TableRule orderItemTableRule = TableRule.builder("t_order_item").actualTables(Arrays.asList("t_order_item_0", "t_order_item_1", "t_order_item_2", "t_order_item_3", "t_order_item_4", "t_order_item_5", "t_order_item_6", "t_order_item_7", "t_order_item_8", "t_order_item_9")).dataSourceRule(dataSourceRule).build();
    ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Arrays.asList(orderTableRule, orderItemTableRule)).bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule)))).databaseShardingStrategy(new DatabaseShardingStrategy("user_id", new NoneDatabaseShardingAlgorithm())).tableShardingStrategy(new TableShardingStrategy("order_id", new SingleKeyModuloTableShardingAlgorithm())).build();
    shardingDataSource = new ShardingDataSource(shardingRule);
    return shardingDataSource;
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) DatabaseShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) NoneDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) SingleKeyModuloTableShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloTableShardingAlgorithm)

Example 17 with ShardingDataSource

use of com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource in project sharding-jdbc by dangdangdotcom.

the class ShardingTablesOnlyForPStatementWithDMLTest method assertDeleteWithoutShardingValue.

@Test
public void assertDeleteWithoutShardingValue() throws SQLException, DatabaseUnitException {
    ShardingDataSource shardingDataSource = getShardingDataSource();
    String sql = "DELETE `t_order` WHERE `status` = ?";
    try (Connection connection = shardingDataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
        preparedStatement.setString(1, "init");
        assertThat(preparedStatement.executeUpdate(), is(20));
    }
    assertDataSet("delete", "init");
}
Also used : ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test) AbstractShardingTablesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.tbl.AbstractShardingTablesOnlyDBUnitTest)

Example 18 with ShardingDataSource

use of com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource in project sharding-jdbc by dangdangdotcom.

the class ShardingTablesOnlyForPStatementWithDMLTest method assertUpdateWithoutShardingValue.

@Test
public void assertUpdateWithoutShardingValue() throws SQLException, DatabaseUnitException {
    ShardingDataSource shardingDataSource = getShardingDataSource();
    String sql = "UPDATE `t_order` SET `status` = ? WHERE `status` = ?";
    try (Connection connection = shardingDataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
        preparedStatement.setString(1, "updated");
        preparedStatement.setString(2, "init");
        assertThat(preparedStatement.executeUpdate(), is(20));
    }
    assertDataSet("update", "updated");
}
Also used : ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test) AbstractShardingTablesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.tbl.AbstractShardingTablesOnlyDBUnitTest)

Example 19 with ShardingDataSource

use of com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource in project sharding-jdbc by dangdangdotcom.

the class ShardingTablesOnlyForPStatementWithDMLTest method assertUpdateWithAlias.

@Test
public void assertUpdateWithAlias() throws SQLException, DatabaseUnitException {
    ShardingDataSource shardingDataSource = getShardingDataSource();
    String sql = "UPDATE `t_order` as o SET o.`status` = ? WHERE o.`order_id` = ? AND o.`user_id` = ?";
    try (Connection connection = shardingDataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
        for (int i = 10; i < 12; i++) {
            for (int j = 0; j < 10; j++) {
                preparedStatement.setString(1, "updated");
                preparedStatement.setInt(2, i * 100 + j);
                preparedStatement.setInt(3, i);
                assertThat(preparedStatement.executeUpdate(), is(1));
            }
        }
    }
    assertDataSet("update", "updated");
}
Also used : ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) Test(org.junit.Test) AbstractShardingTablesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.tbl.AbstractShardingTablesOnlyDBUnitTest)

Example 20 with ShardingDataSource

use of com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource in project sharding-jdbc by dangdangdotcom.

the class Main method getShardingDataSource.

private static ShardingDataSource getShardingDataSource() {
    DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap());
    TableRule orderTableRule = TableRule.builder("t_order").actualTables(Arrays.asList("t_order_0", "t_order_1")).dataSourceRule(dataSourceRule).build();
    TableRule orderItemTableRule = TableRule.builder("t_order_item").actualTables(Arrays.asList("t_order_item_0", "t_order_item_1")).dataSourceRule(dataSourceRule).build();
    ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Arrays.asList(orderTableRule, orderItemTableRule)).bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule)))).databaseShardingStrategy(new DatabaseShardingStrategy("user_id", new ModuloDatabaseShardingAlgorithm())).tableShardingStrategy(new TableShardingStrategy("order_id", new ModuloTableShardingAlgorithm())).build();
    return new ShardingDataSource(shardingRule);
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) DatabaseShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) ModuloDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.example.jdbc.algorithm.ModuloDatabaseShardingAlgorithm) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) ModuloTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.example.jdbc.algorithm.ModuloTableShardingAlgorithm)

Aggregations

ShardingDataSource (com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource)21 DataSourceRule (com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule)11 ShardingRule (com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)11 TableRule (com.dangdang.ddframe.rdb.sharding.api.rule.TableRule)10 DatabaseShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy)10 AbstractShardingTablesOnlyDBUnitTest (com.dangdang.ddframe.rdb.integrate.tbl.AbstractShardingTablesOnlyDBUnitTest)9 BindingTableRule (com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule)9 TableShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy)9 Connection (java.sql.Connection)9 Test (org.junit.Test)9 PreparedStatement (java.sql.PreparedStatement)5 Statement (java.sql.Statement)4 MultipleKeysModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.MultipleKeysModuloDatabaseShardingAlgorithm)3 SingleKeyModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloDatabaseShardingAlgorithm)3 SingleKeyModuloTableShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloTableShardingAlgorithm)3 NoneTableShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm)3 NoneKeyModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.NoneKeyModuloDatabaseShardingAlgorithm)1 SingleKeyDynamicModuloTableShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyDynamicModuloTableShardingAlgorithm)1 NoneDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm)1 ModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.example.jdbc.algorithm.ModuloDatabaseShardingAlgorithm)1