Search in sources :

Example 1 with IncrementKeyGenerator

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"));
}
Also used : SQLException(java.sql.SQLException) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) Connection(java.sql.Connection) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) IncrementKeyGenerator(io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator) DatabaseMetaData(java.sql.DatabaseMetaData) TestComplexKeysShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) DataSource(javax.sql.DataSource)

Example 2 with IncrementKeyGenerator

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));
}
Also used : ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) IncrementKeyGenerator(io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) Test(org.junit.Test)

Example 3 with IncrementKeyGenerator

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"));
}
Also used : NoneShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration) IncrementKeyGenerator(io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) Test(org.junit.Test)

Aggregations

TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)3 IncrementKeyGenerator (io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator)3 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)2 Test (org.junit.Test)2 TestComplexKeysShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm)1 ComplexShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration)1 NoneShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration)1 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)1 Connection (java.sql.Connection)1 DatabaseMetaData (java.sql.DatabaseMetaData)1 SQLException (java.sql.SQLException)1 DataSource (javax.sql.DataSource)1