提交 64e267fd 编写于 作者: T terrymanu

For #660: use yaml config file to instead of ShardingRuleMockBuilder on rewrite

上级 a642bf29
/*
* Copyright 1999-2015 dangdang.com.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* </p>
*/
package io.shardingjdbc.core.api.fixture;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.api.config.TableRuleConfiguration;
import io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator;
import io.shardingjdbc.core.rule.ShardingRule;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
@Deprecated
// TODO refactor
public class ShardingRuleMockBuilder {
private final List<TableRuleConfiguration> tableRuleConfigs = new LinkedList<>();
private final List<String> shardingColumns = new LinkedList<>();
private final Map<String, String> generateKeyColumnsMap = new HashMap<>();
private final Set<String> bindTables = new HashSet<>();
public ShardingRuleMockBuilder addTableRuleConfig(final TableRuleConfiguration tableRuleConfig) {
tableRuleConfigs.add(tableRuleConfig);
return this;
}
public ShardingRuleMockBuilder addShardingColumns(final String shardingColumnName) {
shardingColumns.add(shardingColumnName);
return this;
}
public ShardingRuleMockBuilder addGenerateKeyColumn(final String tableName, final String columnName) {
generateKeyColumnsMap.put(tableName, columnName);
return this;
}
public ShardingRuleMockBuilder addBindingTable(final String bindingTableName) {
bindTables.add(bindingTableName);
return this;
}
public ShardingRule build() {
Collection<TableRuleConfiguration> tableRuleConfigs = Lists.newArrayList(Iterators.transform(generateKeyColumnsMap.keySet().iterator(), new Function<String, TableRuleConfiguration>() {
@Override
public TableRuleConfiguration apply(final String input) {
TableRuleConfiguration result = new TableRuleConfiguration();
result.setLogicTable(input);
result.setActualDataNodes("db0." + input + ",db1." + input);
result.setKeyGeneratorColumnName(generateKeyColumnsMap.get(input));
return result;
}
}));
tableRuleConfigs.addAll(this.tableRuleConfigs);
if (tableRuleConfigs.isEmpty()) {
TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
tableRuleConfig.setLogicTable("mock");
tableRuleConfig.setActualDataNodes("mock");
tableRuleConfigs.add(tableRuleConfig);
}
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
for (String each : bindTables) {
if (existInTableRuleConfig(each)) {
continue;
}
TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
tableRuleConfig.setLogicTable(each);
tableRuleConfigs.add(tableRuleConfig);
}
for (TableRuleConfiguration each : tableRuleConfigs) {
shardingRuleConfig.getTableRuleConfigs().add(each);
bindTables.add(each.getLogicTable());
}
shardingRuleConfig.getBindingTableGroups().add(Joiner.on(",").join(bindTables));
shardingRuleConfig.setDefaultKeyGenerator(new IncrementKeyGenerator());
return new ShardingRule(shardingRuleConfig, Lists.newArrayList("db0", "db1"));
}
private boolean existInTableRuleConfig(final String logicTableName) {
for (TableRuleConfiguration tableRuleConfig : tableRuleConfigs) {
if (tableRuleConfig.getLogicTable().equals(logicTableName)) {
return true;
}
}
return false;
}
}
......@@ -18,9 +18,9 @@
package io.shardingjdbc.core.rewrite;
import com.google.common.base.Optional;
import io.shardingjdbc.core.api.fixture.ShardingRuleMockBuilder;
import io.shardingjdbc.core.constant.DatabaseType;
import io.shardingjdbc.core.constant.OrderType;
import io.shardingjdbc.core.parsing.SQLParsingEngineTest;
import io.shardingjdbc.core.parsing.parser.context.OrderItem;
import io.shardingjdbc.core.parsing.parser.context.limit.Limit;
import io.shardingjdbc.core.parsing.parser.context.limit.LimitValue;
......@@ -34,9 +34,12 @@ import io.shardingjdbc.core.parsing.parser.token.TableToken;
import io.shardingjdbc.core.routing.type.TableUnit;
import io.shardingjdbc.core.routing.type.complex.CartesianTableReference;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
......@@ -54,8 +57,9 @@ public final class SQLRewriteEngineTest {
private Map<String, String> tableTokens;
@Before
public void setUp() {
shardingRule = new ShardingRuleMockBuilder().addGenerateKeyColumn("table_x", "id").addBindingTable("table_y").build();
public void setUp() throws IOException {
YamlShardingConfiguration yamlShardingConfig = YamlShardingConfiguration.unmarshal(new File(SQLParsingEngineTest.class.getClassLoader().getResource("yaml/rewrite-rule.yaml").getFile()));
shardingRule = yamlShardingConfig.getShardingRule(yamlShardingConfig.getDataSources().keySet());
selectStatement = new SelectStatement();
tableTokens = new HashMap<>(1, 1);
tableTokens.put("table_x", "table_1");
......
dataSources:
db0: !!org.apache.commons.dbcp2.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db0;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
db1: !!org.apache.commons.dbcp2.BasicDataSource
driverClassName: org.h2.Driver
url: jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
username: sa
password:
shardingRule:
tables:
table_x:
actualDataNodes: db${0..1}.table_x
keyGeneratorColumnName: id
table_y:
actualDataNodes: db${0..1}.table_y
bindingTables:
- table_x, table_y
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册