All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.mybatisplus.binding.parser.BaseConditionManager Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.mybatisplus.binding.parser;

import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.schema.Column;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zodiac.commons.util.lang.Strings;
import org.zodiac.sdk.toolkit.util.lang.StrUtil;

/**
 * 条件管理器base类。
 * 
 */
public abstract class BaseConditionManager {

    private static Logger log = LoggerFactory.getLogger(BaseConditionManager.class);

    /**
     * 表达式缓存Map
     */
    private static final Map> expressionParseResultMap = new ConcurrentHashMap<>();

    protected BaseConditionManager() {
        super();
    }

    /**
     * 获取解析后的Expression列表。
     * 
     * @param condition 条件
     * @return 结果
     */
    protected static List getExpressionList(String condition) {
        if (StrUtil.isBlank(condition)) {
            return null;
        }
        List expressionList = expressionParseResultMap.get(condition);
        if (expressionList == null) {
            ConditionParser visitor = new ConditionParser();
            try {
                Expression expression = CCJSqlParserUtil.parseCondExpression(condition);
                expression.accept(visitor);
                expressionList = visitor.getExpressList();
                expressionParseResultMap.put(condition, expressionList);
            } catch (Exception e) {
                log.error("关联条件解析异常", e);
            }
        }
        return expressionList;
    }

    /**
     * 提取中间表表对象名。
     * 
     * @param expressionList 表达式列表
     * @return 结果
     */
    protected static String extractMiddleTableName(List expressionList) {
        Set tableNameSet = new HashSet<>();
        for (Expression operator : expressionList) {
            if (operator instanceof EqualsTo) {
                EqualsTo express = (EqualsTo)operator;
                /*全部是列*/
                if (express.getLeftExpression() instanceof Column && express.getRightExpression() instanceof Column) {
                    /*统计左侧列中出现的表名*/
                    String leftColumn = express.getLeftExpression().toString();
                    collectTableName(tableNameSet, leftColumn);
                    /*统计右侧列中出现的表名*/
                    String rightColumn = express.getRightExpression().toString();
                    collectTableName(tableNameSet, rightColumn);
                }
            }
        }
        if (tableNameSet.isEmpty()) {
            return null;
        }
        if (tableNameSet.size() > 1) {
            log.warn("中间表关联条件暂只支持1张中间表!");
        }
        return tableNameSet.iterator().next();
    }

    /**
     * 是否为VO自身属性(以this开头的)。
     * 
     * @param expression 表达式
     * @return 结果
     */
    protected static boolean isCurrentObjColumn(String expression) {
        String tempTableName = StrUtil.subStrBefore(expression, ".");
        /*如果是中间表(非this,self标识的当前表)*/
        return "this".equals(tempTableName) || "self".equals(tempTableName);
    }

    /**
     * 统计表名出现的次数。
     * 
     * @param tableNameSet 表名列表
     * @param columnStr 列名
     */
    private static void collectTableName(Set tableNameSet, String columnStr) {
        if (!columnStr.contains(".")) {
            return;
        }
        /*如果是中间表(非this,self标识的当前表)*/
        if (!isCurrentObjColumn(columnStr)) {
            String tempTableName = StrUtil.subStrBefore(columnStr, ".");
            tableNameSet.add(tempTableName);
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy