Search in sources :

Example 1 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule 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 2 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class UpdateStatementParserTest method parseWithOr.

@Test(expected = SQLParsingUnsupportedException.class)
public void parseWithOr() {
    ShardingRule shardingRule = createShardingRule();
    new SQLParsingEngine(DatabaseType.Oracle, "UPDATE TABLE_XXX SET field1=1 WHERE field1<1 AND (field1 >2 OR field2 =1)", shardingRule).parse();
}
Also used : ShardingRule(io.shardingjdbc.core.rule.ShardingRule) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine) Test(org.junit.Test)

Example 3 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class UpdateStatementParserTest method parseWithSpecialSyntax.

private void parseWithSpecialSyntax(final DatabaseType dbType, final String actualSQL) {
    ShardingRule shardingRule = createShardingRule();
    DMLStatement updateStatement = (DMLStatement) new SQLParsingEngine(dbType, actualSQL, shardingRule).parse();
    assertThat(updateStatement.getTables().find("TABLE_XXX").get().getName(), is("TABLE_XXX"));
    assertFalse(updateStatement.getTables().find("TABLE_XXX").get().getAlias().isPresent());
    Condition condition = updateStatement.getConditions().find(new Column("field1", "TABLE_XXX")).get();
    assertThat(condition.getOperator(), is(ShardingOperator.EQUAL));
    assertThat(((ListShardingValue<? extends Comparable>) condition.getShardingValue(Collections.emptyList())).getValues().iterator().next(), is((Object) 1));
}
Also used : Condition(io.shardingjdbc.core.parsing.parser.context.condition.Condition) Column(io.shardingjdbc.core.parsing.parser.context.condition.Column) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) DMLStatement(io.shardingjdbc.core.parsing.parser.sql.dml.DMLStatement) SQLParsingEngine(io.shardingjdbc.core.parsing.SQLParsingEngine)

Example 4 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule 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 5 with ShardingRule

use of io.shardingjdbc.core.rule.ShardingRule in project sharding-jdbc by shardingjdbc.

the class DatabaseTest method setRouteRuleContext.

@Before
public void setRouteRuleContext() {
    ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
    shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new HintShardingStrategyConfiguration(new OrderDatabaseHintShardingAlgorithm()));
    dataSourceMap = new LinkedHashMap<>(2, 1);
    dataSourceMap.put("ds_0", null);
    dataSourceMap.put("ds_1", null);
    shardingRule = new ShardingRule(shardingRuleConfig, dataSourceMap.keySet());
}
Also used : ShardingRuleConfiguration(io.shardingjdbc.core.api.config.ShardingRuleConfiguration) HintShardingStrategyConfiguration(io.shardingjdbc.core.api.config.strategy.HintShardingStrategyConfiguration) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) OrderDatabaseHintShardingAlgorithm(io.shardingjdbc.core.fixture.OrderDatabaseHintShardingAlgorithm) Before(org.junit.Before)

Aggregations

ShardingRule (io.shardingjdbc.core.rule.ShardingRule)60 Test (org.junit.Test)35 ShardingDataSource (io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource)20 SQLParsingEngine (io.shardingjdbc.core.parsing.SQLParsingEngine)19 ShardingRuleConfiguration (io.shardingjdbc.core.api.config.ShardingRuleConfiguration)16 TableRuleConfiguration (io.shardingjdbc.core.api.config.TableRuleConfiguration)14 DataSource (javax.sql.DataSource)14 BindingTableRule (io.shardingjdbc.core.rule.BindingTableRule)10 StandardShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.StandardShardingStrategyConfiguration)8 DMLStatement (io.shardingjdbc.core.parsing.parser.sql.dml.DMLStatement)8 Map (java.util.Map)8 ComplexShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.ComplexShardingStrategyConfiguration)7 DatabaseType (io.shardingjdbc.core.constant.DatabaseType)7 TableRule (io.shardingjdbc.core.rule.TableRule)6 SQLShardingRule (io.shardingjdbc.core.integrate.jaxb.SQLShardingRule)5 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)5 HashMap (java.util.HashMap)5 Before (org.junit.Before)5 TestComplexKeysShardingAlgorithm (io.shardingjdbc.core.api.algorithm.fixture.TestComplexKeysShardingAlgorithm)4 InlineShardingStrategyConfiguration (io.shardingjdbc.core.api.config.strategy.InlineShardingStrategyConfiguration)4