Search in sources :

Example 31 with ShardingRuleConfiguration

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

the class ShardingDataSourceFactoryTest method createShardingRuleConfig.

private ShardingRuleConfiguration createShardingRuleConfig() {
    ShardingRuleConfiguration result = new ShardingRuleConfiguration();
    TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
    tableRuleConfig.setLogicTable("logicTable");
    tableRuleConfig.setActualDataNodes("ds.table_${0..2}");
    result.getTableRuleConfigs().add(tableRuleConfig);
    return result;
}
Also used : ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration)

Example 32 with ShardingRuleConfiguration

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

the class ShardingDataSourceFactoryTest method assertCreateDataSourceWithShardingRuleAndConfigMapAndProperties.

@Test
public void assertCreateDataSourceWithShardingRuleAndConfigMapAndProperties() throws SQLException, NoSuchFieldException, IllegalAccessException {
    ShardingRuleConfiguration shardingRuleConfig = createShardingRuleConfig();
    Properties props = new Properties();
    Map<String, Object> configMap = new ConcurrentHashMap<>();
    configMap.put("key1", "value1");
    DataSource dataSource = ShardingDataSourceFactory.createDataSource(getDataSourceMap(), shardingRuleConfig, configMap, props);
    assertNotNull(getShardingRule(dataSource));
    assertThat(ConfigMapContext.getInstance().getShardingConfig(), is(configMap));
    assertThat(getShardingProperties(dataSource), is(props));
}
Also used : ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) Properties(java.util.Properties) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 33 with ShardingRuleConfiguration

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

the class AbstractRoutingDatabaseOnlyWithHintTest method getShardingRule.

@Override
protected ShardingRule getShardingRule(final Map.Entry<DatabaseType, Map<String, DataSource>> dataSourceEntry) {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new HintShardingStrategyConfiguration(new HintModuloDatabaseShardingAlgorithm()));
    return new ShardingRule(shardingRuleConfig, dataSourceEntry.getValue().keySet());
}
Also used : HintModuloDatabaseShardingAlgorithm(io.shardingjdbc.core.integrate.fixture.HintModuloDatabaseShardingAlgorithm) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) HintShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.HintShardingStrategyConfiguration) ShardingRule(io.shardingjdbc.core.rule.ShardingRule)

Example 34 with ShardingRuleConfiguration

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

the class AbstractShardingDatabaseOnlyWithHintTest method getShardingRule.

@Override
protected ShardingRule getShardingRule(final Map.Entry<DatabaseType, Map<String, DataSource>> dataSourceEntry) {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
    orderTableRuleConfig.setLogicTable("t_order");
    shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
    TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration();
    orderItemTableRuleConfig.setLogicTable("t_order_item");
    shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);
    shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new ComplexShardingStrategyConfiguration("user_id", new ComplexKeysModuloDatabaseShardingAlgorithm()));
    shardingRuleConfig.setDefaultTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
    return new ShardingRule(shardingRuleConfig, dataSourceEntry.getValue().keySet());
}
Also used : NoneShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) ComplexKeysModuloDatabaseShardingAlgorithm(io.shardingjdbc.core.integrate.fixture.ComplexKeysModuloDatabaseShardingAlgorithm) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration)

Example 35 with ShardingRuleConfiguration

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

the class AbstractShardingDatabaseOnlyTest method getDataSources.

@Override
protected Map<DatabaseType, ShardingDataSource> getDataSources() throws SQLException {
    if (!getShardingDataSources().isEmpty()) {
        return getShardingDataSources();
    }
    Map<DatabaseType, Map<String, DataSource>> dataSourceMap = createDataSourceMap();
    for (Map.Entry<DatabaseType, Map<String, DataSource>> each : dataSourceMap.entrySet()) {
        final ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
        TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
        orderTableRuleConfig.setLogicTable("t_order");
        orderTableRuleConfig.setLogicIndex("t_order_index");
        orderTableRuleConfig.setKeyGeneratorColumnName("order_id");
        orderTableRuleConfig.setKeyGenerator(new IncrementKeyGenerator());
        shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
        TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration();
        orderItemTableRuleConfig.setLogicTable("t_order_item");
        TableRuleConfiguration logTableRuleConfig = new TableRuleConfiguration();
        logTableRuleConfig.setLogicIndex("t_log_index");
        logTableRuleConfig.setLogicTable("t_log");
        TableRuleConfiguration tempLogTableRuleConfig = new TableRuleConfiguration();
        tempLogTableRuleConfig.setLogicTable("t_temp_log");
        shardingRuleConfig.getTableRuleConfigs().add(logTableRuleConfig);
        shardingRuleConfig.getTableRuleConfigs().add(tempLogTableRuleConfig);
        shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);
        shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
        shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new ComplexShardingStrategyConfiguration("user_id", new ComplexKeysModuloDatabaseShardingAlgorithm()));
        shardingRuleConfig.setDefaultTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
        getShardingDataSources().put(each.getKey(), new ShardingDataSource(each.getValue(), new ShardingRule(shardingRuleConfig, each.getValue().keySet())));
    }
    return getShardingDataSources();
}
Also used : DatabaseType(io.shardingjdbc.core.constant.DatabaseType) NoneShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.NoneShardingStrategyConfiguration) ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) ShardingDataSource(io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource) ComplexShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration) ComplexKeysModuloDatabaseShardingAlgorithm(io.shardingjdbc.core.integrate.fixture.ComplexKeysModuloDatabaseShardingAlgorithm) SQLShardingRule(io.shardingjdbc.core.integrate.jaxb.SQLShardingRule) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) IncrementKeyGenerator(io.shardingjdbc.core.fixture.IncrementKeyGenerator) Map(java.util.Map) TableRuleConfiguration(io.shardingjdbc.core.api.config.TableRuleConfiguration)

Aggregations

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