use of io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator in project sharding-jdbc by shardingjdbc.
the class InsertStatementParserTest method createShardingRuleWithGenerateKeyColumns.
private ShardingRule createShardingRuleWithGenerateKeyColumns() {
DataSource dataSource = mock(DataSource.class);
Connection connection = mock(Connection.class);
DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class);
try {
when(dataSource.getConnection()).thenReturn(connection);
when(connection.getMetaData()).thenReturn(databaseMetaData);
when(databaseMetaData.getDatabaseProductName()).thenReturn("H2");
} catch (final SQLException ex) {
throw new RuntimeException(ex);
}
final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
tableRuleConfig.setLogicTable("TABLE_XXX");
tableRuleConfig.setActualDataNodes("ds.table_${0..2}");
tableRuleConfig.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration("field1", new TestComplexKeysShardingAlgorithm()));
tableRuleConfig.setKeyGeneratorColumnName("field2");
shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
shardingRuleConfig.setDefaultKeyGenerator(new IncrementKeyGenerator());
return new ShardingRule(shardingRuleConfig, Lists.newArrayList("ds"));
}
use of io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator in project sharding-jdbc by shardingjdbc.
the class ShardingRuleTest method assertGenerateKeyWithKeyGenerator.
@Test
public void assertGenerateKeyWithKeyGenerator() {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
TableRuleConfiguration tableRuleConfig = createTableRuleConfig();
tableRuleConfig.setKeyGenerator(new IncrementKeyGenerator());
shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
assertThat(new ShardingRule(shardingRuleConfig, createDataSourceNames()).generateKey("logic_table"), instanceOf(Integer.class));
}
use of io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator in project sharding-jdbc by shardingjdbc.
the class TableRuleTest method assertCreateFullTableRule.
@Test
public void assertCreateFullTableRule() {
TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
tableRuleConfig.setLogicTable("LOGIC_TABLE");
tableRuleConfig.setActualDataNodes("ds${0..1}.table_${0..2}");
tableRuleConfig.setDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration());
tableRuleConfig.setTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
tableRuleConfig.setKeyGeneratorColumnName("col_1");
tableRuleConfig.setKeyGenerator(new IncrementKeyGenerator());
tableRuleConfig.setLogicIndex("LOGIC_INDEX");
TableRule actual = new TableRule(tableRuleConfig, createDataSourceNames());
assertThat(actual.getLogicTable(), is("logic_table"));
assertThat(actual.getActualDataNodes().size(), is(6));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds0", "table_0")));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds0", "table_1")));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds0", "table_2")));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds1", "table_0")));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds1", "table_1")));
assertTrue(actual.getActualDataNodes().contains(new DataNode("ds1", "table_2")));
assertNotNull(actual.getDatabaseShardingStrategy());
assertNotNull(actual.getTableShardingStrategy());
assertThat(actual.getGenerateKeyColumn(), is("col_1"));
assertThat(actual.getKeyGenerator(), instanceOf(IncrementKeyGenerator.class));
assertThat(actual.getLogicIndex(), is("logic_index"));
}
Aggregations