ShardingRuleMockBuilder.java 4.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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>
 */

18
package io.shardingjdbc.core.api.fixture;
19

20 21 22 23
import io.shardingjdbc.core.api.config.ShardingRuleConfiguration;
import io.shardingjdbc.core.api.config.TableRuleConfiguration;
import io.shardingjdbc.core.rule.ShardingRule;
import io.shardingjdbc.core.keygen.fixture.IncrementKeyGenerator;
24
import com.google.common.base.Function;
25
import com.google.common.base.Joiner;
26 27 28 29 30 31
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import org.mockito.Mockito;

import javax.sql.DataSource;
32
import java.sql.SQLException;
33
import java.util.Collection;
34 35
import java.util.HashMap;
import java.util.HashSet;
36
import java.util.LinkedList;
37
import java.util.List;
38 39
import java.util.Map;
import java.util.Set;
40

41 42
@Deprecated
// TODO refactor
43 44
public class ShardingRuleMockBuilder {
    
T
terrymanu 已提交
45
    private final List<TableRuleConfiguration> tableRuleConfigs = new LinkedList<>();
46 47
    
    private final List<String> shardingColumns = new LinkedList<>();
48
    
49
    private final Map<String, String> generateKeyColumnsMap = new HashMap<>();
50
    
51
    private final Set<String> bindTables = new HashSet<>();
52
    
T
terrymanu 已提交
53
    public ShardingRuleMockBuilder addTableRuleConfig(final TableRuleConfiguration tableRuleConfig) {
54
        tableRuleConfigs.add(tableRuleConfig);
55 56 57
        return this;
    }
    
58
    public ShardingRuleMockBuilder addShardingColumns(final String shardingColumnName) {
59
        shardingColumns.add(shardingColumnName);
60 61 62
        return this;
    }
    
63
    public ShardingRuleMockBuilder addGenerateKeyColumn(final String tableName, final String columnName) {
T
terrymanu 已提交
64
        generateKeyColumnsMap.put(tableName, columnName);
65 66 67
        return this;
    }
    
68 69 70 71 72
    public ShardingRuleMockBuilder addBindingTable(final String bindingTableName) {
        bindTables.add(bindingTableName);
        return this;
    }
    
73
    public ShardingRule build() throws SQLException {
T
terrymanu 已提交
74
        Collection<TableRuleConfiguration> tableRuleConfigs = Lists.newArrayList(Iterators.transform(generateKeyColumnsMap.keySet().iterator(), new Function<String, TableRuleConfiguration>() {
75
            
76
            @Override
T
terrymanu 已提交
77 78
            public TableRuleConfiguration apply(final String input) {
                TableRuleConfiguration result = new TableRuleConfiguration();
79
                result.setLogicTable(input);
80
                result.setActualDataNodes("db0." + input + ",db1." + input);
81
                result.setKeyGeneratorColumnName(generateKeyColumnsMap.get(input));
82
                return result;
83 84
            }
        }));
85 86
        tableRuleConfigs.addAll(this.tableRuleConfigs);
        if (tableRuleConfigs.isEmpty()) {
T
terrymanu 已提交
87
            TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
88
            tableRuleConfig.setLogicTable("mock");
89
            tableRuleConfig.setActualDataNodes("mock");
90
            tableRuleConfigs.add(tableRuleConfig);
91
        }
T
terrymanu 已提交
92
        ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
93
        for (String each : bindTables) {
94 95 96
            if (existInTableRuleConfig(each)) {
                continue;
            }
T
terrymanu 已提交
97
            TableRuleConfiguration tableRuleConfig = new TableRuleConfiguration();
98 99 100
            tableRuleConfig.setLogicTable(each);
            tableRuleConfigs.add(tableRuleConfig);
        }
T
terrymanu 已提交
101
        for (TableRuleConfiguration each : tableRuleConfigs) {
102
            shardingRuleConfig.getTableRuleConfigs().add(each);
103
            bindTables.add(each.getLogicTable());
104
        }
105
        shardingRuleConfig.getBindingTableGroups().add(Joiner.on(",").join(bindTables));
106
        shardingRuleConfig.setDefaultKeyGeneratorClass(IncrementKeyGenerator.class.getName());
T
terrymanu 已提交
107
        return shardingRuleConfig.build(ImmutableMap.of("db0", Mockito.mock(DataSource.class), "db1", Mockito.mock(DataSource.class)));
108 109 110
    }
    
    private boolean existInTableRuleConfig(final String logicTableName) {
T
terrymanu 已提交
111
        for (TableRuleConfiguration tableRuleConfig : tableRuleConfigs) {
112 113 114
            if (tableRuleConfig.getLogicTable().equals(logicTableName)) {
                return true;
            }
115
        }
116
        return false;
117 118
    }
}