Search in sources :

Example 1 with TableTypeEnum

use of com.actiontech.dble.config.model.TableConfig.TableTypeEnum in project dble by actiontech.

the class XMLSchemaLoader method loadTables.

private Map<String, TableConfig> loadTables(Element node, boolean isLowerCaseNames) {
    // supprot [`] BEN GONG in table name
    Map<String, TableConfig> tables = new TableConfigMap();
    NodeList nodeList = node.getElementsByTagName("table");
    for (int i = 0; i < nodeList.getLength(); i++) {
        Element tableElement = (Element) nodeList.item(i);
        // primaryKey used for cache and autoincrement
        String primaryKey = tableElement.hasAttribute("primaryKey") ? tableElement.getAttribute("primaryKey").toUpperCase() : null;
        // if autoIncrement,it will use sequence handler
        boolean autoIncrement = false;
        if (tableElement.hasAttribute("autoIncrement")) {
            autoIncrement = Boolean.parseBoolean(tableElement.getAttribute("autoIncrement"));
        }
        // limit size of the table
        boolean needAddLimit = true;
        if (tableElement.hasAttribute("needAddLimit")) {
            needAddLimit = Boolean.parseBoolean(tableElement.getAttribute("needAddLimit"));
        }
        // table type is global or not
        String tableTypeStr = tableElement.hasAttribute("type") ? tableElement.getAttribute("type") : null;
        TableTypeEnum tableType = TableTypeEnum.TYPE_SHARDING_TABLE;
        if ("global".equalsIgnoreCase(tableTypeStr)) {
            tableType = TableTypeEnum.TYPE_GLOBAL_TABLE;
        }
        // dataNode of table
        TableRuleConfig tableRule = null;
        if (tableElement.hasAttribute("rule")) {
            String ruleName = tableElement.getAttribute("rule");
            tableRule = tableRules.get(ruleName);
            if (tableRule == null) {
                throw new ConfigException("rule " + ruleName + " is not found!");
            }
        }
        boolean ruleRequired = false;
        // ruleRequired?
        if (tableElement.hasAttribute("ruleRequired")) {
            ruleRequired = Boolean.parseBoolean(tableElement.getAttribute("ruleRequired"));
        }
        String dataNode = tableElement.getAttribute("dataNode");
        // distribute function
        String distPrex = "distribute(";
        boolean distTableDns = dataNode.startsWith(distPrex);
        if (distTableDns) {
            dataNode = dataNode.substring(distPrex.length(), dataNode.length() - 1);
        }
        String tableNameElement = tableElement.getAttribute("name");
        if (isLowerCaseNames) {
            tableNameElement = tableNameElement.toLowerCase();
        }
        String[] tableNames = tableNameElement.split(",");
        if (tableNames == null) {
            throw new ConfigException("table name is not found!");
        }
        for (String tableName : tableNames) {
            TableConfig table = new TableConfig(tableName, primaryKey, autoIncrement, needAddLimit, tableType, dataNode, (tableRule != null) ? tableRule.getRule() : null, ruleRequired);
            checkDataNodeExists(table.getDataNodes());
            if (table.getRule() != null) {
                checkRuleSuitTable(table);
            }
            if (distTableDns) {
                distributeDataNodes(table.getDataNodes());
            }
            if (tables.containsKey(table.getName())) {
                throw new ConfigException("table " + tableName + " duplicated!");
            }
            tables.put(table.getName(), table);
        }
        // child table must know its unique father
        if (tableNames.length == 1) {
            TableConfig parentTable = tables.get(tableNames[0]);
            // process child tables
            processChildTables(tables, parentTable, dataNode, tableElement, isLowerCaseNames);
        }
    }
    return tables;
}
Also used : TableTypeEnum(com.actiontech.dble.config.model.TableConfig.TableTypeEnum) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ConfigException(com.actiontech.dble.config.util.ConfigException) TableRuleConfig(com.actiontech.dble.config.model.rule.TableRuleConfig)

Aggregations

TableTypeEnum (com.actiontech.dble.config.model.TableConfig.TableTypeEnum)1 TableRuleConfig (com.actiontech.dble.config.model.rule.TableRuleConfig)1 ConfigException (com.actiontech.dble.config.util.ConfigException)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1