Search in sources :

Example 6 with Translate

use of org.sagacity.sqltoy.config.model.Translate in project sagacity-sqltoy by chenrenfei.

the class EntityQuery method translates.

/**
 * @TODO 对sql语句指定缓存翻译
 * @param translates
 * @return
 */
public EntityQuery translates(Translate... translates) {
    if (translates != null && translates.length > 0) {
        TranslateExtend extend;
        for (Translate trans : translates) {
            extend = trans.getExtend();
            if (StringUtil.isBlank(extend.cache) || StringUtil.isBlank(extend.keyColumn) || StringUtil.isBlank(extend.column)) {
                throw new IllegalArgumentException("针对EntityQuery设置缓存翻译必须要明确:cacheName=[" + extend.cache + "]、keyColumn=[" + extend.keyColumn + "](作为key的字段列)、 column=[" + extend.column + "](翻译结果映射的列)!");
            }
            innerModel.translates.put(extend.column, trans);
        }
    }
    return this;
}
Also used : TranslateExtend(org.sagacity.sqltoy.model.inner.TranslateExtend) Translate(org.sagacity.sqltoy.config.model.Translate)

Example 7 with Translate

use of org.sagacity.sqltoy.config.model.Translate in project sagacity-sqltoy by chenrenfei.

the class SqlXMLConfigParse method parseTranslate.

/**
 * @todo 解析翻译器
 * @param sqlToyConfig
 * @param translates
 */
public static void parseTranslate(SqlToyConfig sqlToyConfig, NodeList translates) {
    if (translates == null || translates.getLength() == 0) {
        return;
    }
    // 翻译器
    HashMap<String, Translate> translateMap = new HashMap<String, Translate>();
    String cacheType;
    String cacheName;
    String[] columns;
    Integer[] cacheIndexs;
    String[] cacheIndexStr;
    String uncachedTemplate;
    // 为mongo和elastic模式提供备用
    String[] aliasNames;
    // 分隔表达式
    String splitRegex = null;
    // 对应split重新连接的字符
    String linkSign = ",";
    boolean hasLink = false;
    Element translate;
    for (int k = 0; k < translates.getLength(); k++) {
        translate = (Element) translates.item(k);
        hasLink = false;
        cacheName = translate.getAttribute("cache");
        // 具体的缓存子分类,如数据字典类别
        if (translate.hasAttribute("cache-type")) {
            cacheType = translate.getAttribute("cache-type");
        } else {
            cacheType = null;
        }
        // 已经小写
        columns = trimParams(translate.getAttribute("columns").toLowerCase().split("\\,"));
        aliasNames = null;
        uncachedTemplate = null;
        if (translate.hasAttribute("undefine-template")) {
            uncachedTemplate = translate.getAttribute("undefine-template");
        } else if (translate.hasAttribute("uncached-template")) {
            uncachedTemplate = translate.getAttribute("uncached-template");
        } else if (translate.hasAttribute("uncached")) {
            uncachedTemplate = translate.getAttribute("uncached");
        }
        if (translate.hasAttribute("split-regex")) {
            splitRegex = translate.getAttribute("split-regex");
            if (translate.hasAttribute("link-sign")) {
                linkSign = translate.getAttribute("link-sign");
                hasLink = true;
            }
            // 正则转化
            if (splitRegex.equals(",") || splitRegex.equals(",")) {
                splitRegex = "\\,";
            } else if (splitRegex.equals(";") || splitRegex.equals(";")) {
                splitRegex = "\\;";
                if (!hasLink) {
                    linkSign = ";";
                }
            } else if (splitRegex.equals("、")) {
                splitRegex = "\\、";
            } else if (splitRegex.equals("->")) {
                splitRegex = "\\-\\>";
                if (!hasLink) {
                    linkSign = "->";
                }
            }
        }
        // 使用alias时只能针对单列处理
        if (translate.hasAttribute("alias-name")) {
            aliasNames = trimParams(translate.getAttribute("alias-name").toLowerCase().split("\\,"));
        } else if (translate.hasAttribute("original-columns")) {
            aliasNames = trimParams(translate.getAttribute("original-columns").toLowerCase().split("\\,"));
        }
        // 翻译key对应value的在缓存数组中对应的列
        cacheIndexs = null;
        if (translate.hasAttribute("cache-indexs")) {
            cacheIndexStr = trimParams(translate.getAttribute("cache-indexs").split("\\,"));
            cacheIndexs = new Integer[cacheIndexStr.length];
            for (int i = 0; i < cacheIndexStr.length; i++) {
                cacheIndexs[i] = Integer.parseInt(cacheIndexStr[i]);
            }
        } else // 兼容参数命名
        if (translate.hasAttribute("cache-indexes")) {
            cacheIndexStr = trimParams(translate.getAttribute("cache-indexes").split("\\,"));
            cacheIndexs = new Integer[cacheIndexStr.length];
            for (int i = 0; i < cacheIndexStr.length; i++) {
                cacheIndexs[i] = Integer.parseInt(cacheIndexStr[i]);
            }
        }
        if (cacheIndexs == null || cacheIndexs.length == columns.length) {
            for (int i = 0; i < columns.length; i++) {
                Translate translateModel = new Translate(cacheName);
                // 小写
                translateModel.setColumn(columns[i]);
                translateModel.setAlias(aliasNames == null ? columns[i] : aliasNames[i]);
                translateModel.setCacheType(cacheType);
                translateModel.setSplitRegex(splitRegex);
                translateModel.setLinkSign(linkSign);
                if (uncachedTemplate != null) {
                    // 统一未匹配中的通配符号为${value}
                    translateModel.setUncached(uncachedTemplate.replaceAll("(?i)\\$?\\{\\s*key\\s*\\}", "\\$\\{value\\}"));
                }
                if (cacheIndexs != null) {
                    if (i < cacheIndexs.length - 1) {
                        translateModel.setIndex(cacheIndexs[i]);
                    } else {
                        translateModel.setIndex(cacheIndexs[cacheIndexs.length - 1]);
                    }
                }
                // column 已经小写
                translateMap.put(translateModel.getExtend().column, translateModel);
            }
        } else if (cacheIndexs != null && cacheIndexs.length != columns.length) {
            logger.warn("sqlId:{} 对应的cache translate columns suggest config with cache-indexs!", sqlToyConfig.getId());
        }
    }
    sqlToyConfig.setTranslateMap(translateMap);
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Element(org.w3c.dom.Element) Translate(org.sagacity.sqltoy.config.model.Translate)

Example 8 with Translate

use of org.sagacity.sqltoy.config.model.Translate in project sagacity-sqltoy by chenrenfei.

the class TranslateManager method getTranslates.

/**
 * @todo 根据sqltoy sql.xml中的翻译设置获取对应的缓存(多个translate对应的多个缓存结果)
 * @param translates
 * @return
 */
public HashMap<String, HashMap<String, Object[]>> getTranslates(HashMap<String, Translate> translates) {
    // 获得当前线程中的sql执行日志,后续缓存获取会覆盖掉日志
    SqlExecuteTrace sqlTrace = SqlExecuteStat.get();
    HashMap<String, HashMap<String, Object[]>> result = new HashMap<String, HashMap<String, Object[]>>();
    HashMap<String, Object[]> cache;
    TranslateConfigModel cacheModel;
    TranslateExtend extend;
    int cacheEltLength;
    for (Map.Entry<String, Translate> entry : translates.entrySet()) {
        extend = entry.getValue().getExtend();
        if (translateMap.containsKey(extend.cache)) {
            cacheModel = translateMap.get(extend.cache);
            cache = getCacheData(cacheModel, extend.cacheType);
            if (cache != null) {
                // update 2022-1-4 增加缓存使用时cache-index 合法性校验
                if (cache.size() > 0) {
                    cacheEltLength = cache.values().iterator().next().length;
                    if (extend.index >= cacheEltLength) {
                        throw new IllegalArgumentException("缓存取值数组越界:cacheName:" + extend.cache + ", column:" + extend.column + ",cache-indexs:(" + extend.index + ">=" + cacheEltLength + ")[缓存内容数组长度],请检查cache-indexs值确保跟缓存数据具体列保持一致!");
                    }
                }
                result.put(extend.column, cache);
            } else {
                result.put(extend.column, new HashMap<String, Object[]>());
                if (logger.isWarnEnabled()) {
                    logger.warn("sqltoy translate:cacheName={},cache-type={},column={}配置不正确,未获取对应cache数据!", cacheModel.getCache(), extend.cacheType, extend.column);
                } else {
                    System.err.println("sqltoy translate:cacheName=" + cacheModel.getCache() + ",cache-type=" + extend.cacheType + ",column=" + extend.column + " 配置不正确,未获取对应cache数据!");
                }
            }
        } else {
            logger.error("cacheName:{} 没有配置,请检查sqltoy-translate.xml文件!", extend.cache);
        }
    }
    // 将调用获取缓存之前的日志放回线程中
    if (sqlTrace != null) {
        SqlExecuteStat.set(sqlTrace);
    }
    return result;
}
Also used : HashMap(java.util.HashMap) TranslateConfigModel(org.sagacity.sqltoy.translate.model.TranslateConfigModel) TranslateExtend(org.sagacity.sqltoy.model.inner.TranslateExtend) HashMap(java.util.HashMap) IgnoreKeyCaseMap(org.sagacity.sqltoy.model.IgnoreKeyCaseMap) Map(java.util.Map) Translate(org.sagacity.sqltoy.config.model.Translate) SqlExecuteTrace(org.sagacity.sqltoy.config.model.SqlExecuteTrace)

Example 9 with Translate

use of org.sagacity.sqltoy.config.model.Translate in project sagacity-sqltoy by chenrenfei.

the class TranslateConfigParse method getClassTranslates.

/**
 * @TODO 获取DTO或POJO中的@translate注解
 * @param classType
 * @return
 */
public static HashMap<String, Translate> getClassTranslates(Class classType) {
    if (classType == null || classType.equals(Map.class) || classType.equals(HashMap.class) || classType.equals(List.class) || classType.equals(ArrayList.class) || classType.equals(Array.class) || BeanUtil.isBaseDataType(classType)) {
        return null;
    }
    String className = classType.getName();
    // 利用Map对类中的缓存翻译配置进行缓存,规避每次都解析
    if (classTranslateConfigMap.containsKey(className)) {
        return classTranslateConfigMap.get(className);
    }
    HashMap<String, Translate> translateConfig = new HashMap<String, Translate>();
    org.sagacity.sqltoy.config.annotation.Translate translate;
    Class classVar = classType;
    while (classVar != null && !classVar.equals(Object.class)) {
        for (Field field : classVar.getDeclaredFields()) {
            translate = field.getAnnotation(org.sagacity.sqltoy.config.annotation.Translate.class);
            // 以子类注解为优先
            if (translate != null && !translateConfig.containsKey(field.getName())) {
                Translate trans = new Translate(translate.cacheName());
                trans.setIndex(translate.cacheIndex());
                if (StringUtil.isNotBlank(translate.cacheType())) {
                    trans.setCacheType(translate.cacheType());
                }
                trans.setKeyColumn(translate.keyField());
                trans.setColumn(field.getName());
                trans.setAlias(field.getName());
                if (StringUtil.isNotBlank(translate.split())) {
                    trans.setSplitRegex(translate.split());
                }
                // 默认是,逗号
                if (StringUtil.isNotBlank(translate.join())) {
                    trans.setLinkSign(translate.join());
                }
                if (translate.uncached() != null && !translate.uncached().equals("")) {
                    trans.setUncached(translate.uncached().trim());
                }
                translateConfig.put(field.getName(), trans);
            }
        }
        // 向父类递归
        classVar = classVar.getSuperclass();
    }
    classTranslateConfigMap.put(className, translateConfig);
    return translateConfig;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) Translate(org.sagacity.sqltoy.config.model.Translate)

Aggregations

Translate (org.sagacity.sqltoy.config.model.Translate)9 TranslateExtend (org.sagacity.sqltoy.model.inner.TranslateExtend)7 HashMap (java.util.HashMap)6 ArrayList (java.util.ArrayList)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 HashSet (java.util.HashSet)3 LinkedHashMap (java.util.LinkedHashMap)3 List (java.util.List)3 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)3 ReverseList (org.sagacity.sqltoy.plugins.calculator.ReverseList)3 UnpivotList (org.sagacity.sqltoy.plugins.calculator.UnpivotList)3 Map (java.util.Map)2 LinkModel (org.sagacity.sqltoy.config.model.LinkModel)2 IgnoreKeyCaseMap (org.sagacity.sqltoy.model.IgnoreKeyCaseMap)2 Field (java.lang.reflect.Field)1 ResultSet (java.sql.ResultSet)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 DataSource (javax.sql.DataSource)1