Search in sources :

Example 1 with TableRuleConfiguration

use of io.shardingjdbc.core.api.config.TableRuleConfiguration in project sharding-jdbc by shardingjdbc.

the class ShardingRule method createTableRuleWithDefaultDataSource.

private TableRule createTableRuleWithDefaultDataSource(final String logicTableName) {
    TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
    tableRuleConfig.setLogicTable(logicTableName);
    return new TableRule(tableRuleConfig, Collections.singletonList(defaultDataSourceName));
}
Also used : TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration)

Example 2 with TableRuleConfiguration

use of io.shardingjdbc.core.api.config.TableRuleConfiguration in project sharding-jdbc by shardingjdbc.

the class AbstractShardingJDBCDatabaseAndTableTest method initShardingDataSources.

@Before
public void initShardingDataSources() throws SQLException {
    if (!getShardingDataSources().isEmpty()) {
        return;
    }
    Map<DatabaseType, Map<String, DataSource>> dataSourceMap = createDataSourceMap();
    for (Map.Entry<DatabaseType, Map<String, DataSource>> entry : dataSourceMap.entrySet()) {
        final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
        orderTableRuleConfig.setLogicTable("t_order");
        List<String> orderActualDataNodes = new LinkedList<>();
        for (String dataSourceName : entry.getValue().keySet()) {
            orderActualDataNodes.add(dataSourceName + ".t_order_${0..1}");
        }
        orderTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderActualDataNodes));
        shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
        TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration();
        orderItemTableRuleConfig.setLogicTable("t_order_item");
        List<String> orderItemActualDataNodes = new LinkedList<>();
        for (String dataSourceName : entry.getValue().keySet()) {
            orderItemActualDataNodes.add(dataSourceName + ".t_order_item_${0..1}");
        }
        orderItemTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderItemActualDataNodes));
        orderItemTableRuleConfig.setKeyGeneratorColumnName("item_id");
        orderItemTableRuleConfig.setKeyGenerator(new IncrementKeyGenerator());
        shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);
        TableRuleConfiguration configTableRuleConfig = new TableRuleConfiguration();
        configTableRuleConfig.setLogicTable("t_config");
        shardingRuleConfig.getTableRuleConfigs().add(configTableRuleConfig);
        shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
        shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseOrderShardingAlgorithm(), new RangeOrderShardingAlgorithm()));
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", new PreciseOrderShardingAlgorithm(), new RangeOrderShardingAlgorithm()));
        ShardingRule shardingRule = new ShardingRule(shardingRuleConfig, entry.getValue().keySet());
        getShardingDataSources().put(entry.getKey(), new ShardingDataSource(entry.getValue(), shardingRule));
    }
}
Also used : DatabaseType(io.shardingjdbc.core.constant.DatabaseType) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) IncrementKeyGenerator(io.shardingjdbc.core.fixture.IncrementKeyGenerator) LinkedList(java.util.LinkedList) StandardShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration) PreciseOrderShardingAlgorithm(io.shardingjdbc.core.fixture.PreciseOrderShardingAlgorithm) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) Map(java.util.Map) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration) RangeOrderShardingAlgorithm(io.shardingjdbc.core.fixture.RangeOrderShardingAlgorithm) Before(org.junit.Before)

Example 3 with TableRuleConfiguration

use of io.shardingjdbc.core.api.config.TableRuleConfiguration in project sharding-jdbc by shardingjdbc.

the class SQLBuilderTest method createShardingRule.

private ShardingRule createShardingRule() {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    TableRuleConfiguration tableRuleConfig = createTableRuleConfig();
    shardingRuleConfig.getTableRuleConfigs().add(tableRuleConfig);
    return new ShardingRule(shardingRuleConfig, createDataSourceNames());
}
Also used : ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration)

Example 4 with TableRuleConfiguration

use of io.shardingjdbc.core.api.config.TableRuleConfiguration in project sharding-jdbc by shardingjdbc.

the class SQLBuilderTest method createTableRuleConfig.

private TableRuleConfiguration createTableRuleConfig() {
    TableRuleConfiguration result = new TableRuleConfiguration();
    result.setLogicTable("LOGIC_TABLE");
    result.setActualDataNodes("ds${0..1}.table_${0..2}");
    return result;
}
Also used : TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration)

Example 5 with TableRuleConfiguration

use of io.shardingjdbc.core.api.config.TableRuleConfiguration 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)

Aggregations

TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)65 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)43 Test (org.junit.Test)33 NoneShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration)16 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)14 ComplexShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration)7 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)7 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)6 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)6 Map (java.util.Map)6 SQLShardingRule (io.shardingjdbc.core.integrate.jaxb.SQLShardingRule)5 NoneShardingStrategy (io.shardingjdbc.core.routing.strategy.none.NoneShardingStrategy)5 LinkedList (java.util.LinkedList)5 DataSource (javax.sql.DataSource)5 TestComplexKeysShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm)4 Before (org.junit.Before)4 ComplexKeysModuloDatabaseShardingAlgorithm (io.shardingjdbc.core.integrate.fixture.ComplexKeysModuloDatabaseShardingAlgorithm)3 IncrementKeyGenerator (io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator)3 TestPreciseShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestPreciseShardingAlgorithm)2 IncrementKeyGenerator (io.shardingjdbc.core.fixture.IncrementKeyGenerator)2