Search in sources :

Example 1 with AutoIncrementColumnConfig

use of com.dangdang.ddframe.rdb.sharding.config.common.api.config.AutoIncrementColumnConfig in project sharding-jdbc by dangdangdotcom.

the class ShardingJdbcDataSourceBeanDefinitionParser method parseTableRuleConfig.

private BeanDefinition parseTableRuleConfig(final Element tableElement) {
    BeanDefinitionBuilder factory = BeanDefinitionBuilder.rootBeanDefinition(TableRuleConfig.class);
    String dynamic = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DYNAMIC_TABLE_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(dynamic)) {
        factory.addPropertyValue("dynamic", dynamic);
    }
    String actualTables = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.ACTUAL_TABLES_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(actualTables)) {
        factory.addPropertyValue("actualTables", actualTables);
    }
    String dataSourceNames = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DATA_SOURCE_NAMES_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(dataSourceNames)) {
        factory.addPropertyValue("dataSourceNames", dataSourceNames);
    }
    String databaseStrategy = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.DATABASE_STRATEGY_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(databaseStrategy)) {
        factory.addPropertyReference("databaseStrategy", databaseStrategy);
    }
    String tableStrategy = tableElement.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.TABLE_STRATEGY_ATTRIBUTE);
    if (!Strings.isNullOrEmpty(tableStrategy)) {
        factory.addPropertyReference("tableStrategy", tableStrategy);
    }
    List<Element> autoIncrementColumns = DomUtils.getChildElementsByTagName(tableElement, ShardingJdbcDataSourceBeanDefinitionParserTag.AUTO_INCREMENT_COLUMN);
    if (null == autoIncrementColumns || autoIncrementColumns.isEmpty()) {
        return factory.getBeanDefinition();
    }
    factory.addPropertyValue("autoIncrementColumns", Lists.transform(autoIncrementColumns, new Function<Element, AutoIncrementColumnConfig>() {

        @Override
        public AutoIncrementColumnConfig apply(final Element input) {
            AutoIncrementColumnConfig result = new AutoIncrementColumnConfig();
            result.setColumnName(input.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.COLUMN_NAME));
            result.setColumnIdGeneratorClass(input.getAttribute(ShardingJdbcDataSourceBeanDefinitionParserTag.COLUMN_ID_GENERATOR_CLASS));
            return result;
        }
    }));
    return factory.getBeanDefinition();
}
Also used : Function(com.google.common.base.Function) BeanDefinitionBuilder(org.springframework.beans.factory.support.BeanDefinitionBuilder) AutoIncrementColumnConfig(com.dangdang.ddframe.rdb.sharding.config.common.api.config.AutoIncrementColumnConfig) Element(org.w3c.dom.Element)

Example 2 with AutoIncrementColumnConfig

use of com.dangdang.ddframe.rdb.sharding.config.common.api.config.AutoIncrementColumnConfig in project sharding-jdbc by dangdangdotcom.

the class ShardingRuleBuilderTest method createTableRuleConfig.

private TableRuleConfig createTableRuleConfig(final String logicTable) {
    TableRuleConfig result = new TableRuleConfig();
    result.setActualTables(logicTable + "_${[0, 1]}");
    result.setDataSourceNames("ds_${0..1}");
    result.setDatabaseStrategy(getDatabaseStrategyConfig(SingleAlgorithm.class.getName()));
    result.setTableStrategy(getTableStrategyConfigForExpression());
    Map<String, String> autoIncrementColumnMap = new HashMap<>();
    autoIncrementColumnMap.put("order_id", null);
    AutoIncrementColumnConfig orderIdConfig = new AutoIncrementColumnConfig();
    orderIdConfig.setColumnName("order_id");
    autoIncrementColumnMap.put("order_item_id", "com.dangdang.ddframe.rdb.sharding.config.common.fixture.DecrementIdGenerator");
    AutoIncrementColumnConfig orderItemIdConfig = new AutoIncrementColumnConfig();
    orderItemIdConfig.setColumnName("order_item_id");
    orderItemIdConfig.setColumnIdGeneratorClass(DecrementIdGenerator.class.getName());
    result.setAutoIncrementColumns(Arrays.asList(orderIdConfig, orderItemIdConfig));
    return result;
}
Also used : AutoIncrementColumnConfig(com.dangdang.ddframe.rdb.sharding.config.common.api.config.AutoIncrementColumnConfig) HashMap(java.util.HashMap) DecrementIdGenerator(com.dangdang.ddframe.rdb.sharding.config.common.fixture.DecrementIdGenerator) BindingTableRuleConfig(com.dangdang.ddframe.rdb.sharding.config.common.api.config.BindingTableRuleConfig) TableRuleConfig(com.dangdang.ddframe.rdb.sharding.config.common.api.config.TableRuleConfig)

Aggregations

AutoIncrementColumnConfig (com.dangdang.ddframe.rdb.sharding.config.common.api.config.AutoIncrementColumnConfig)2 BindingTableRuleConfig (com.dangdang.ddframe.rdb.sharding.config.common.api.config.BindingTableRuleConfig)1 TableRuleConfig (com.dangdang.ddframe.rdb.sharding.config.common.api.config.TableRuleConfig)1 DecrementIdGenerator (com.dangdang.ddframe.rdb.sharding.config.common.fixture.DecrementIdGenerator)1 Function (com.google.common.base.Function)1 HashMap (java.util.HashMap)1 BeanDefinitionBuilder (org.springframework.beans.factory.support.BeanDefinitionBuilder)1 Element (org.w3c.dom.Element)1