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;
}
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");
}
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");
}
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");
}
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);
}
Aggregations