Search in sources :

Example 21 with TableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule in project sharding-jdbc by dangdangdotcom.

the class MySQLInsertVisitor method supplyAutoIncrementColumn.

private void supplyAutoIncrementColumn(final Collection<String> autoIncrementColumns, final String tableName, final List<SQLExpr> columns, final List<SQLExpr> values) {
    boolean isPreparedStatement = !getParameters().isEmpty();
    GeneratedKeyContext generatedKeyContext = getParseContext().getParsedResult().getGeneratedKeyContext();
    if (isPreparedStatement) {
        generatedKeyContext.getColumns().addAll(autoIncrementColumns);
    }
    TableRule tableRule = getParseContext().getShardingRule().findTableRule(tableName);
    for (String each : autoIncrementColumns) {
        SQLExpr sqlExpr;
        Object id = tableRule.generateId(each);
        generatedKeyContext.putValue(each, id);
        if (isPreparedStatement) {
            sqlExpr = new SQLVariantRefExpr("?");
            getParameters().add(id);
            ((SQLVariantRefExpr) sqlExpr).setIndex(getParametersSize() - 1);
        } else {
            sqlExpr = (id instanceof Number) ? new SQLNumberExpr((Number) id) : new SQLCharExpr((String) id);
        }
        getParseContext().addCondition(each, tableName, BinaryOperator.EQUAL, sqlExpr, getDatabaseType(), getParameters());
        columns.add(new SQLIdentifierExpr(each));
        values.add(sqlExpr);
    }
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) GeneratedKeyContext(com.dangdang.ddframe.rdb.sharding.parser.result.GeneratedKeyContext) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr)

Example 22 with TableRule

use of com.dangdang.ddframe.rdb.sharding.api.rule.TableRule in project sharding-jdbc by dangdangdotcom.

the class AbstractShardingDataBasesOnlyDBUnitTest method getShardingDataSource.

protected final ShardingDataSource getShardingDataSource() {
    if (null != shardingDataSource && !isShutdown) {
        return shardingDataSource;
    }
    isShutdown = false;
    DataSourceRule dataSourceRule = new DataSourceRule(createDataSourceMap("dataSource_%s"));
    TableRule orderTableRule = TableRule.builder("t_order").dataSourceRule(dataSourceRule).autoIncrementColumns("user_id").tableIdGenerator(IncrementIdGenerator.class).build();
    TableRule orderItemTableRule = TableRule.builder("t_order_item").dataSourceRule(dataSourceRule).build();
    ShardingRule shardingRule = ShardingRule.builder().dataSourceRule(dataSourceRule).tableRules(Arrays.asList(orderTableRule, orderItemTableRule)).bindingTableRules(Collections.singletonList(new BindingTableRule(Arrays.asList(orderTableRule, orderItemTableRule)))).databaseShardingStrategy(new DatabaseShardingStrategy(Collections.singletonList("user_id"), new MultipleKeysModuloDatabaseShardingAlgorithm())).tableShardingStrategy(new TableShardingStrategy(Collections.singletonList("order_id"), new NoneTableShardingAlgorithm())).build();
    shardingDataSource = new ShardingDataSource(shardingRule);
    return shardingDataSource;
}
Also used : TableRule(com.dangdang.ddframe.rdb.sharding.api.rule.TableRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) DatabaseShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy) ShardingDataSource(com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource) DataSourceRule(com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule) BindingTableRule(com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule) ShardingRule(com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule) TableShardingStrategy(com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy) IncrementIdGenerator(com.dangdang.ddframe.rdb.sharding.id.generator.fixture.IncrementIdGenerator) MultipleKeysModuloDatabaseShardingAlgorithm(com.dangdang.ddframe.rdb.integrate.fixture.MultipleKeysModuloDatabaseShardingAlgorithm) NoneTableShardingAlgorithm(com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm)

Aggregations

TableRule (com.dangdang.ddframe.rdb.sharding.api.rule.TableRule)22 DataSourceRule (com.dangdang.ddframe.rdb.sharding.api.rule.DataSourceRule)16 ShardingRule (com.dangdang.ddframe.rdb.sharding.api.rule.ShardingRule)16 BindingTableRule (com.dangdang.ddframe.rdb.sharding.api.rule.BindingTableRule)14 DatabaseShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.database.DatabaseShardingStrategy)13 TableShardingStrategy (com.dangdang.ddframe.rdb.sharding.api.strategy.table.TableShardingStrategy)12 ShardingDataSource (com.dangdang.ddframe.rdb.sharding.jdbc.ShardingDataSource)12 HashMap (java.util.HashMap)6 DataSource (javax.sql.DataSource)6 MultipleKeysModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.MultipleKeysModuloDatabaseShardingAlgorithm)3 SingleKeyModuloDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloDatabaseShardingAlgorithm)3 SingleKeyModuloTableShardingAlgorithm (com.dangdang.ddframe.rdb.integrate.fixture.SingleKeyModuloTableShardingAlgorithm)3 NoneTableShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.table.NoneTableShardingAlgorithm)3 Test (org.junit.Test)3 NoneDatabaseShardingAlgorithm (com.dangdang.ddframe.rdb.sharding.api.strategy.database.NoneDatabaseShardingAlgorithm)2 YamlShardingDataSource (com.dangdang.ddframe.rdb.sharding.config.yaml.api.YamlShardingDataSource)2 IncrementIdGenerator (com.dangdang.ddframe.rdb.sharding.id.generator.fixture.IncrementIdGenerator)2 GeneratedKeyContext (com.dangdang.ddframe.rdb.sharding.parser.result.GeneratedKeyContext)2 BasicDataSource (org.apache.commons.dbcp.BasicDataSource)2 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1