提交 dd056c11 编写于 作者: T terrymanu

For #673: polish TableRuleConfiguration

上级 57300a63
......@@ -18,6 +18,7 @@
package io.shardingjdbc.core.api.config;
import io.shardingjdbc.core.api.config.strategy.ShardingStrategyConfiguration;
import io.shardingjdbc.core.keygen.KeyGenerator;
import lombok.Getter;
import lombok.Setter;
......@@ -40,7 +41,7 @@ public final class TableRuleConfiguration {
private String keyGeneratorColumnName;
private String keyGeneratorClass;
private KeyGenerator keyGenerator;
private String logicIndex;
}
......@@ -18,11 +18,9 @@
package io.shardingjdbc.core.rule;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import io.shardingjdbc.core.api.config.TableRuleConfiguration;
import io.shardingjdbc.core.exception.ShardingJdbcException;
import io.shardingjdbc.core.keygen.KeyGenerator;
import io.shardingjdbc.core.keygen.KeyGeneratorFactory;
import io.shardingjdbc.core.routing.strategy.ShardingStrategy;
import io.shardingjdbc.core.routing.strategy.ShardingStrategyFactory;
import io.shardingjdbc.core.util.InlineExpressionParser;
......@@ -64,7 +62,7 @@ public final class TableRule {
databaseShardingStrategy = null == tableRuleConfig.getDatabaseShardingStrategyConfig() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getDatabaseShardingStrategyConfig());
tableShardingStrategy = null == tableRuleConfig.getTableShardingStrategyConfig() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getTableShardingStrategyConfig());
generateKeyColumn = tableRuleConfig.getKeyGeneratorColumnName();
keyGenerator = !hasKeyGenerator(tableRuleConfig) ? null : KeyGeneratorFactory.newInstance(tableRuleConfig.getKeyGeneratorClass());
keyGenerator = tableRuleConfig.getKeyGenerator();
logicIndex = null == tableRuleConfig.getLogicIndex() ? null : tableRuleConfig.getLogicIndex().toLowerCase();
Preconditions.checkNotNull(logicTable);
}
......@@ -93,10 +91,6 @@ public final class TableRule {
return result;
}
private boolean hasKeyGenerator(final TableRuleConfiguration tableRuleConfig) {
return !Strings.isNullOrEmpty(generateKeyColumn) && !Strings.isNullOrEmpty(tableRuleConfig.getKeyGeneratorClass());
}
/**
* Get actual data source names.
*
......
......@@ -18,7 +18,9 @@
package io.shardingjdbc.core.yaml.sharding;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import io.shardingjdbc.core.api.config.TableRuleConfiguration;
import io.shardingjdbc.core.keygen.KeyGeneratorFactory;
import lombok.Getter;
import lombok.Setter;
......@@ -61,7 +63,9 @@ public class YamlTableRuleConfiguration {
if (null != tableStrategy) {
result.setTableShardingStrategyConfig(tableStrategy.build());
}
result.setKeyGeneratorClass(keyGeneratorClass);
if (!Strings.isNullOrEmpty(keyGeneratorClass)) {
result.setKeyGenerator(KeyGeneratorFactory.newInstance(keyGeneratorClass));
}
result.setKeyGeneratorColumnName(keyGeneratorColumnName);
result.setLogicIndex(logicIndex);
return result;
......
......@@ -59,7 +59,7 @@ public final class TableRuleTest {
tableRuleConfig.setDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration());
tableRuleConfig.setTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
tableRuleConfig.setKeyGeneratorColumnName("col_1");
tableRuleConfig.setKeyGeneratorClass(IncrementKeyGenerator.class.getName());
tableRuleConfig.setKeyGenerator(new IncrementKeyGenerator());
tableRuleConfig.setLogicIndex("LOGIC_INDEX");
TableRule actual = new TableRule(tableRuleConfig, createDataSourceNames());
assertThat(actual.getLogicTable(), is("logic_table"));
......
......@@ -72,7 +72,7 @@ public final class YamlTableRuleConfigurationTest {
assertThat(actual.getLogicTable(), is("t_order"));
assertThat(actual.getActualDataNodes(), is("ds_${0..1}.t_order_${0..1}"));
assertThat(actual.getKeyGeneratorColumnName(), is("order_id"));
assertThat(actual.getKeyGeneratorClass(), is(DefaultKeyGenerator.class.getName()));
assertThat(actual.getKeyGenerator(), instanceOf(DefaultKeyGenerator.class));
assertThat(actual.getLogicIndex(), is("order_index"));
}
......
......@@ -21,6 +21,7 @@ import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.api.config.TableRuleConfiguration;
import io.shardingjdbc.core.keygen.KeyGeneratorFactory;
import io.shardingjdbc.spring.datasource.SpringShardingDataSource;
import io.shardingjdbc.spring.namespace.constants.ShardingDataSourceBeanDefinitionParserTag;
import org.springframework.beans.factory.config.BeanDefinition;
......@@ -139,7 +140,7 @@ public class ShardingDataSourceBeanDefinitionParser extends AbstractBeanDefiniti
}
String keyGeneratorClass = tableElement.getAttribute(ShardingDataSourceBeanDefinitionParserTag.COLUMN_KEY_GENERATOR_CLASS);
if (!Strings.isNullOrEmpty(keyGeneratorClass)) {
factory.addPropertyValue("keyGeneratorClass", keyGeneratorClass);
factory.addPropertyValue("keyGenerator", KeyGeneratorFactory.newInstance(keyGeneratorClass));
}
String logicIndex = tableElement.getAttribute(ShardingDataSourceBeanDefinitionParserTag.LOGIC_INDEX);
if (!Strings.isNullOrEmpty(logicIndex)) {
......
......@@ -78,7 +78,7 @@ public abstract class AbstractShardingJDBCDatabaseAndTableTest extends AbstractS
}
orderItemTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderItemActualDataNodes));
orderItemTableRuleConfig.setKeyGeneratorColumnName("item_id");
orderItemTableRuleConfig.setKeyGeneratorClass(IncrementKeyGenerator.class.getName());
orderItemTableRuleConfig.setKeyGenerator(new IncrementKeyGenerator());
shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);
TableRuleConfiguration configTableRuleConfig = new TableRuleConfiguration();
configTableRuleConfig.setLogicTable("t_config");
......
......@@ -77,7 +77,7 @@ public abstract class AbstractShardingDatabaseOnlyTest extends AbstractSQLAssert
orderTableRuleConfig.setLogicTable("t_order");
orderTableRuleConfig.setLogicIndex("t_order_index");
orderTableRuleConfig.setKeyGeneratorColumnName("order_id");
orderTableRuleConfig.setKeyGeneratorClass(IncrementKeyGenerator.class.getName());
orderTableRuleConfig.setKeyGenerator(new IncrementKeyGenerator());
shardingRuleConfig.getTableRuleConfigs().add(orderTableRuleConfig);
TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration();
orderItemTableRuleConfig.setLogicTable("t_order_item");
......
......@@ -81,7 +81,7 @@ public abstract class AbstractShardingTableOnlyTest extends AbstractSQLAssertTes
}
orderItemTableRuleConfig.setActualDataNodes(Joiner.on(",").join(orderItemActualDataNodes));
orderItemTableRuleConfig.setLogicTable("t_order_item");
orderItemTableRuleConfig.setKeyGeneratorClass("item_id");
orderItemTableRuleConfig.setKeyGeneratorColumnName("item_id");
shardingRuleConfig.getTableRuleConfigs().add(orderItemTableRuleConfig);
TableRuleConfiguration logTableRuleConfig = new TableRuleConfiguration();
logTableRuleConfig.setLogicIndex("t_log_index");
......
......@@ -21,6 +21,7 @@ import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.api.config.TableRuleConfiguration;
import io.shardingjdbc.core.keygen.KeyGeneratorFactory;
import io.shardingjdbc.orchestration.internal.OrchestrationShardingDataSource;
import io.shardingjdbc.orchestration.spring.namespace.constants.ShardingDataSourceBeanDefinitionParserTag;
import org.springframework.beans.factory.config.BeanDefinition;
......@@ -141,7 +142,7 @@ public class OrchestrationShardingDataSourceBeanDefinitionParser extends Abstrac
}
String keyGeneratorClass = tableElement.getAttribute(ShardingDataSourceBeanDefinitionParserTag.COLUMN_KEY_GENERATOR_CLASS);
if (!Strings.isNullOrEmpty(keyGeneratorClass)) {
factory.addPropertyValue("keyGeneratorClass", keyGeneratorClass);
factory.addPropertyValue("keyGenerator", KeyGeneratorFactory.newInstance(keyGeneratorClass));
}
String logicIndex = tableElement.getAttribute(ShardingDataSourceBeanDefinitionParserTag.LOGIC_INDEX_ATTRIBUTE);
if (!Strings.isNullOrEmpty(logicIndex)) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册