Search in sources :

Example 11 with ShardingDataSource

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

the class ShardingTablesOnlyForStatementWithDMLTest method assertUpdate.

@Test
public void assertUpdate() throws SQLException, DatabaseUnitException {
    ShardingDataSource shardingDataSource = getShardingDataSource();
    String sql = "UPDATE `t_order` SET `status` = '%s' WHERE `order_id` = %s AND `user_id` = %s";
    for (int i = 10; i < 12; i++) {
        for (int j = 0; j < 10; j++) {
            try (Connection connection = shardingDataSource.getConnection();
                Statement stmt = connection.createStatement()) {
                assertThat(stmt.executeUpdate(String.format(sql, "updated", i * 100 + j, i)), is(1));
            }
        }
    }
    assertDataSet("update", "updated");
}
Also used : ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test) AbstractShardingTablesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.tbl.AbstractShardingTablesOnlyDBUnitTest)

Example 12 with ShardingDataSource

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

the class ShardingTablesOnlyForStatementWithDMLTest method assertDelete.

@Test
public void assertDelete() throws SQLException, DatabaseUnitException {
    ShardingDataSource shardingDataSource = getShardingDataSource();
    String sql = "DELETE `t_order` WHERE `order_id` = %s AND `user_id` = %s AND `status` = '%s'";
    for (int i = 10; i < 12; i++) {
        for (int j = 0; j < 10; j++) {
            try (Connection connection = shardingDataSource.getConnection();
                Statement stmt = connection.createStatement()) {
                assertThat(stmt.executeUpdate(String.format(sql, i * 100 + j, i, "init")), is(1));
            }
        }
    }
    assertDataSet("delete", "init");
}
Also used : ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) Statement(java.sql.Statement) Connection(java.sql.Connection) Test(org.junit.Test) AbstractShardingTablesOnlyDBUnitTest(com.dangdang.ddframe.rdb.integrate.tbl.AbstractShardingTablesOnlyDBUnitTest)

Example 13 with ShardingDataSource

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

the class AbstractSoftTransactionIntegrationTest method prepareEnv.

private void prepareEnv() throws SQLException {
    DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap());
    TableRule tableRule = TableRule.builder("transaction_test").dataSourceRule(dataSourceRule).build();
    ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Lists.newArrayList(tableRule)).build();
    shardingDataSource = new ShardingDataSource(shardingRule);
    createTable(shardingDataSource);
    transactionDataSource = createTransactionLogDataSource();
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)

Example 14 with ShardingDataSource

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

the class AbstractRoutingDatabaseOnlyTest method initDataSource.

protected ShardingDataSource initDataSource() {
    DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap("dataSource_%s"));
    ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).databaseShardingStrategy(new DatabaseShardingStrategy(new NoneKeyModuloDatabaseShardingAlgorithm())).build();
    return new ShardingDataSource(shardingRule);
}
Also used : 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) NoneKeyModuloDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.NoneKeyModuloDatabaseShardingAlgorithm) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)

Example 15 with ShardingDataSource

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

the class AbstractShardingDataBasesOnlyHintDBUnitTest method initDataSource.

ShardingDataSource initDataSource() {
    DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap("dataSource_%s"));
    TableRule orderTableRule = TableRule.builder("t_order").dataSourceRule(dataSourceRule).build();
    TableRule orderItemTableRule = TableRule.builder("t_order_item").dataSourceRule(dataSourceRule).build();
    ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Lists.newArrayList(orderTableRule, orderItemTableRule)).bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule)))).databaseShardingStrategy(new DatabaseShardingStrategy(Collections.singletonList("user_id"), new MultipleKeysModuloDatabaseShardingAlgorithm())).tableShardingStrategy(new TableShardingStrategy(Collections.singletonList("order_id"), new NoneTableShardingAlgorithm())).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) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) MultipleKeysModuloDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.MultipleKeysModuloDatabaseShardingAlgorithm) NoneTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm)

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