Search in sources :

Example 1 with TestComplexKeysShardingAlgorithm

use of io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm 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 TestComplexKeysShardingAlgorithm

use of io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm in project sharding-jdbc by shardingjdbc.

the class ShardingStrategyTest method assertDoShardingForMultipleKeys.

@Test
public void assertDoShardingForMultipleKeys() {
    ComplexShardingStrategy strategy = new ComplexShardingStrategy(new ComplexShardingStrategyConfiguration("column", new TestComplexKeysShardingAlgorithm()));
    assertThat(strategy.doSharding(targets, Collections.<ShardingValue>singletonList(new PreciseShardingValue<>("logicTable", "column", "1"))), is((Collection<String>) Sets.newHashSet("1", "2", "3")));
}
Also used : ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) Collection(java.util.Collection) TestComplexKeysShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm) PreciseShardingValue(io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue) ShardingValue(io.shardingjdbc.core.api.algorithm.sharding.ShardingValue) RangeShardingValue(io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue) ComplexShardingStrategy(io.shardingjdbc.core.routing.strategy.complex.ComplexShardingStrategy) Test(org.junit.Test)

Example 3 with TestComplexKeysShardingAlgorithm

use of io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm in project sharding-jdbc by shardingjdbc.

the class ShowCreateTableMergedResultTest method setUp.

@Before
public void setUp() throws SQLException {
    TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
    tableRuleConfig.setLogicTable("table");
    tableRuleConfig.setActualDataNodes("ds.table_${0..2}");
    tableRuleConfig.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration("field1, field2, field3", new TestComplexKeysShardingAlgorithm()));
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
    shardingRule = new ShardingRule(shardingRuleConfig, Lists.newArrayList("ds"));
    resultSet = mock(ResultSet.class);
    ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class);
    when(resultSet.getMetaData()).thenReturn(resultSetMetaData);
    when(resultSetMetaData.getColumnCount()).thenReturn(2);
    List<ResultSet> resultSets = Lists.newArrayList(resultSet);
    for (ResultSet each : resultSets) {
        when(each.next()).thenReturn(true, false);
    }
    queryResults = new ArrayList<>(resultSets.size());
    for (ResultSet each : resultSets) {
        queryResults.add(new TestQueryResult(each));
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) TestQueryResult(io.shardingjdbc.core.merger.fixture.TestQueryResult) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) ResultSet(java.sql.ResultSet) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) TestComplexKeysShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) Before(org.junit.Before)

Example 4 with TestComplexKeysShardingAlgorithm

use of io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm in project sharding-jdbc by shardingjdbc.

the class ShowTablesMergedResultTest method setUp.

@Before
public void setUp() throws SQLException {
    TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
    tableRuleConfig.setLogicTable("table");
    tableRuleConfig.setActualDataNodes("ds.table_${0..2}");
    tableRuleConfig.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration("field1, field2, field3", new TestComplexKeysShardingAlgorithm()));
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
    shardingRule = new ShardingRule(shardingRuleConfig, Lists.newArrayList("ds"));
    resultSet = mock(ResultSet.class);
    ResultSetMetaData resultSetMetaData = mock(ResultSetMetaData.class);
    when(resultSet.getMetaData()).thenReturn(resultSetMetaData);
    when(resultSetMetaData.getColumnCount()).thenReturn(1);
    List<ResultSet> resultSets = Lists.newArrayList(resultSet);
    for (ResultSet each : resultSets) {
        when(each.next()).thenReturn(true, false);
    }
    queryResults = new ArrayList<>(resultSets.size());
    for (ResultSet each : resultSets) {
        queryResults.add(new TestQueryResult(each));
    }
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) TestQueryResult(io.shardingjdbc.core.merger.fixture.TestQueryResult) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) ResultSet(java.sql.ResultSet) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) TestComplexKeysShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) Before(org.junit.Before)

Example 5 with TestComplexKeysShardingAlgorithm

use of io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm in project sharding-jdbc by shardingjdbc.

the class AbstractStatementParserTest method createShardingRule.

protected final ShardingRule createShardingRule() {
    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);
    }
    TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
    tableRuleConfig.setLogicTable("TABLE_XXX");
    tableRuleConfig.setActualDataNodes("ds.table_${0..2}");
    tableRuleConfig.setTableShardingStrategyConfig(new ComplexShardingStrategyConfiguration("field1, field2, field3, field4, field5, field6, field7", new TestComplexKeysShardingAlgorithm()));
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
    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) DatabaseMetaData(java.sql.DatabaseMetaData) TestComplexKeysShardingAlgorithm(io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) DataSource(javax.sql.DataSource)

Aggregations

TestComplexKeysShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm)5 ComplexShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration)5 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)4 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)4 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)4 TestQueryResult (io.shardingjdbc.core.merger.fixture.TestQueryResult)2 Connection (java.sql.Connection)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 ResultSet (java.sql.ResultSet)2 ResultSetMetaData (java.sql.ResultSetMetaData)2 SQLException (java.sql.SQLException)2 DataSource (javax.sql.DataSource)2 Before (org.junit.Before)2 PreciseShardingValue (io.shardingjdbc.core.api.algorithm.sharding.PreciseShardingValue)1 RangeShardingValue (io.shardingjdbc.core.api.algorithm.sharding.RangeShardingValue)1 ShardingValue (io.shardingjdbc.core.api.algorithm.sharding.ShardingValue)1 IncrementKeyGenerator (io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator)1 ComplexShardingStrategy (io.shardingjdbc.core.routing.strategy.complex.ComplexShardingStrategy)1 Collection (java.util.Collection)1 Test (org.junit.Test)1