Search in sources :

Example 1 with RuleExpression

use of com.wplatform.ddal.dispatch.rule.RuleExpression in project jdbc-shards by wplatform.

the class XmlConfigParser method newTableConfig.

/**
     * @param scConfig
     * @param template
     * @param tableNode
     * @return
     */
private TableConfig newTableConfig(SchemaConfig scConfig, Map<String, String> template, XNode tableNode) {
    TableConfig config = new TableConfig();
    String router = null;
    boolean validation = scConfig.isValidation();
    String scanLevel = "none";
    String shard = null;
    if (template != null) {
        router = template.get("router");
        String s = template.get("validation");
        if (!StringUtils.isNullOrEmpty(s)) {
            validation = Boolean.parseBoolean(s);
        }
        scanLevel = template.get("scanLevel");
        shard = template.get("shard");
    }
    String tableName = tableNode.getStringAttribute("name");
    String routerChild = tableNode.getStringAttribute("router", router);
    validation = tableNode.getBooleanAttribute("validation", validation);
    scanLevel = tableNode.getStringAttribute("scanLevel", scanLevel);
    shard = tableNode.getStringAttribute("shard", shard);
    if (StringUtils.isNullOrEmpty(shard)) {
        shard = scConfig.getShard();
    }
    if (StringUtils.isNullOrEmpty(tableName)) {
        throw new ParsingException("table attribute 'name' is required.");
    }
    if (!StringUtils.isNullOrEmpty(router) && !StringUtils.equals(router, routerChild)) {
        throw new ParsingException("table's attribute 'router' can't override tableGroup's attribute 'router'.");
    }
    if (StringUtils.isNullOrEmpty(router) && StringUtils.isNullOrEmpty(shard)) {
        throw new ParsingException("attribute 'shard' must be null if attribute 'router' was assigned.");
    }
    router = routerChild;
    config.setName(tableName);
    config.setValidation(validation);
    setTableScanLevel(config, scanLevel);
    List<String> nodes = New.arrayList();
    if (!StringUtils.isNullOrEmpty(shard)) {
        for (String string : shard.split(",")) {
            if (!string.trim().isEmpty() && !nodes.contains(string)) {
                nodes.add(string);
            }
        }
        TableNode[] tableNodes = new TableNode[nodes.size()];
        for (int i = 0; i < nodes.size(); i++) {
            tableNodes[i] = new TableNode(nodes.get(i), tableName);
        }
        config.setShards(tableNodes);
    }
    if (!StringUtils.isNullOrEmpty(router)) {
        TableRouter rawRouter = configuration.getTemporaryTableRouters().get(router);
        if (rawRouter == null) {
            throw new ParsingException("The table router '" + router + "' is not found.");
        }
        TableRouter tableRouter = new TableRouter(configuration);
        List<TableNode> partition = rawRouter.getPartition();
        List<TableNode> inited = New.arrayList(partition.size());
        for (TableNode item : partition) {
            String shardName = item.getShardName();
            String name = item.getObjectName();
            String suffix = item.getSuffix();
            if (StringUtils.isNullOrEmpty(name)) {
                name = tableName;
            }
            TableNode initedNode = new TableNode(shardName, name, suffix);
            inited.add(initedNode);
        }
        tableRouter.setId(rawRouter.getId());
        tableRouter.setPartition(inited);
        RuleExpression rawExpression = rawRouter.getRuleExpression();
        RuleExpression expression = new RuleExpression(tableRouter);
        expression.setExpression(rawExpression.getExpression());
        expression.setRuleColumns(rawExpression.getRuleColumns());
        tableRouter.setRuleExpression(expression);
        config.setTableRouter(tableRouter);
        config.setShards(null);
    }
    config.setSchemaConfig(scConfig);
    return config;
}
Also used : TableRouter(com.wplatform.ddal.dispatch.rule.TableRouter) RuleExpression(com.wplatform.ddal.dispatch.rule.RuleExpression) TableNode(com.wplatform.ddal.dispatch.rule.TableNode) TableConfig(com.wplatform.ddal.config.TableConfig)

Example 2 with RuleExpression

use of com.wplatform.ddal.dispatch.rule.RuleExpression in project jdbc-shards by wplatform.

the class XmlRuleConfigParser method parseRuleExpression.

// 解析<rule>标签的内容
public RuleExpression parseRuleExpression(XNode xNode) {
    String stringBody = getStringBody(xNode);
    String text = stringBody.replaceAll("\\s", " ");
    final List<RuleColumn> ruleColumns = new ArrayList<RuleColumn>();
    GenericTokenParser parser = new GenericTokenParser("${", "}", new TokenHandler() {

        @Override
        public String handleToken(String content) {
            content = content.replaceAll("\\s", "");
            String name = null;
            String required = null;
            String type = null;
            if (content.contains(",")) {
                String[] properties = content.split(",");
                name = properties[0];
                for (int j = 1; j < properties.length; j++) {
                    String propety = properties[j];
                    if (propety.contains("required=")) {
                        required = propety.split("required=")[1];
                    }
                    if (propety.contains("type=")) {
                        type = propety.split("type=")[1];
                    }
                }
            } else {
                name = content;
            }
            ruleColumns.add(newRuleColumn(name, required, type));
            return name;
        }
    });
    String expression = parser.parse(text);
    RuleExpression rule = new RuleExpression(null);
    rule.setExpression(expression);
    rule.setRuleColumns(ruleColumns);
    return rule;
}
Also used : RuleExpression(com.wplatform.ddal.dispatch.rule.RuleExpression) RuleColumn(com.wplatform.ddal.dispatch.rule.RuleColumn) ArrayList(java.util.ArrayList)

Aggregations

RuleExpression (com.wplatform.ddal.dispatch.rule.RuleExpression)2 TableConfig (com.wplatform.ddal.config.TableConfig)1 RuleColumn (com.wplatform.ddal.dispatch.rule.RuleColumn)1 TableNode (com.wplatform.ddal.dispatch.rule.TableNode)1 TableRouter (com.wplatform.ddal.dispatch.rule.TableRouter)1 ArrayList (java.util.ArrayList)1