Search in sources :

Example 1 with SqlTranslate

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

the class MongoElasticUtils method translate.

/**
 * @todo 对结果集合进行缓存翻译
 * @param translateCache
 * @param translateMap
 * @param dataSet
 * @param dataMap
 * @param fields
 */
private static void translate(HashMap<String, HashMap<String, Object[]>> translateCache, HashMap<String, SqlTranslate> translateMap, List<List> dataSet, Map dataMap, String[] fields) {
    if (translateMap == null || translateMap.isEmpty())
        return;
    if ((dataSet == null || dataSet.isEmpty()) && (dataMap == null || dataMap.isEmpty()))
        return;
    int[] cacheMapIndex = new int[translateMap.size()];
    int[] realIndex = new int[translateMap.size()];
    String[] lables = new String[translateMap.size()];
    String field;
    int index = 0;
    SqlTranslate translateModel;
    HashMap<String, Integer> map = new HashMap<String, Integer>();
    for (int i = 0; i < fields.length; i++) map.put(fields[i].toLowerCase(), i);
    for (int i = 0; i < fields.length; i++) {
        field = fields[i].toLowerCase();
        if (translateMap.containsKey(field)) {
            translateModel = translateMap.get(field);
            cacheMapIndex[index] = i;
            // alias是对应有效列
            realIndex[index] = map.get(translateModel.getAlias());
            // 实际对应的列
            lables[index] = field;
            index++;
        }
    }
    Object value;
    HashMap<String, Object[]> keyValues;
    int cacheIndex;
    Object[] translateAry;
    if (dataSet != null) {
        int size = dataSet.size();
        int colIndex;
        for (int i = 0; i < cacheMapIndex.length; i++) {
            colIndex = cacheMapIndex[i];
            keyValues = translateCache.get(lables[i]);
            translateModel = translateMap.get(lables[i]);
            cacheIndex = translateModel.getIndex();
            for (int j = 0; j < size; j++) {
                value = dataSet.get(j).get(realIndex[i]);
                if (value != null) {
                    translateAry = keyValues.get(value.toString());
                    if (null != translateAry)
                        dataSet.get(j).set(colIndex, keyValues.get(value.toString())[cacheIndex]);
                }
            }
        }
    } else {
        for (int i = 0; i < cacheMapIndex.length; i++) {
            keyValues = translateCache.get(lables[i]);
            translateModel = translateMap.get(lables[i]);
            cacheIndex = translateModel.getIndex();
            value = dataMap.get(translateModel.getAlias());
            if (value != null) {
                translateAry = keyValues.get(value.toString());
                if (null != translateAry)
                    dataMap.put(lables[i], keyValues.get(value.toString())[cacheIndex]);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SqlTranslate(org.sagacity.sqltoy.config.model.SqlTranslate)

Example 2 with SqlTranslate

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

the class SqlXMLConfigParse method parseTranslate.

/**
 * @todo 解析翻译器
 * @param translates
 * @return
 */
public static HashMap<String, SqlTranslate> parseTranslate(List<Element> translates) {
    if (translates != null && !translates.isEmpty()) {
        // 翻译器
        HashMap<String, SqlTranslate> translateMap = new HashMap<String, SqlTranslate>();
        String cacheType;
        String cacheName;
        String[] columns;
        Integer[] cacheIndexs;
        String[] cacheIndexStr;
        String uncachedTemplate;
        // 为mongo和elastic模式提供备用
        String[] aliasNames;
        for (Element translate : translates) {
            cacheName = translate.attributeValue("cache");
            // 具体的缓存子分类,如数据字典类别
            if (translate.attribute("cache-type") != null)
                cacheType = translate.attributeValue("cache-type");
            else
                cacheType = null;
            columns = trimParams(translate.attributeValue("columns").toLowerCase().split(","));
            aliasNames = null;
            uncachedTemplate = null;
            if (translate.attribute("undefine-template") != null)
                uncachedTemplate = translate.attributeValue("undefine-template");
            else if (translate.attribute("uncached-template") != null)
                uncachedTemplate = translate.attributeValue("uncached-template");
            else if (translate.attribute("uncached") != null)
                uncachedTemplate = translate.attributeValue("uncached");
            // 使用alias时只能针对单列处理
            if (translate.attribute("alias-name") != null)
                aliasNames = trimParams(translate.attributeValue("alias-name").toLowerCase().split(","));
            // 翻译key对应value的在缓存数组中对应的列
            cacheIndexs = null;
            if (translate.attribute("cache-indexs") != null) {
                cacheIndexStr = trimParams(translate.attributeValue("cache-indexs").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++) {
                    SqlTranslate translateModel = new SqlTranslate();
                    translateModel.setCache(cacheName);
                    translateModel.setColumn(columns[i]);
                    translateModel.setAlias(aliasNames == null ? columns[i] : aliasNames[i]);
                    translateModel.setDictType(cacheType);
                    if (uncachedTemplate != null) {
                        if (uncachedTemplate.trim().equals(""))
                            translateModel.setUncached(null);
                        else
                            translateModel.setUncached(uncachedTemplate);
                    }
                    if (cacheIndexs != null) {
                        if (i < cacheIndexs.length - 1)
                            translateModel.setIndex(cacheIndexs[i]);
                        else
                            translateModel.setIndex(cacheIndexs[cacheIndexs.length - 1]);
                    }
                    translateMap.put(translateModel.getColumn(), translateModel);
                }
            } else
                logger.warn("cache translate columns must mapped with cache-indexs!");
        }
        return translateMap;
    }
    return null;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Element(org.dom4j.Element) SqlTranslate(org.sagacity.sqltoy.config.model.SqlTranslate)

Example 3 with SqlTranslate

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

the class ResultUtils method processResultRowWithTranslate.

/**
 * @todo 存在缓存翻译的结果处理
 * @param translateMap
 * @param translateCaches
 * @param labelNames
 * @param rs
 * @param size
 * @return
 * @throws Exception
 */
private static List processResultRowWithTranslate(HashMap<String, SqlTranslate> translateMap, HashMap<String, HashMap<String, Object[]>> translateCaches, String[] labelNames, ResultSet rs, int size) throws Exception {
    List rowData = new ArrayList();
    Object fieldValue;
    SqlTranslate translate;
    String label;
    Object[] cacheValues;
    String keyIndex;
    boolean isDebug = logger.isDebugEnabled();
    // String uncachedKeyResult = SqlToyConstants.UNCACHED_KEY_RESULT;
    for (int i = 0; i < size; i++) {
        label = labelNames[i];
        fieldValue = rs.getObject(label);
        label = label.toLowerCase();
        keyIndex = Integer.toString(i);
        if (null != fieldValue) {
            if (fieldValue instanceof java.sql.Clob)
                fieldValue = SqlUtil.clobToString((java.sql.Clob) fieldValue);
            if (translateMap.containsKey(label) || translateMap.containsKey(keyIndex)) {
                translate = translateMap.get(label);
                if (translate == null)
                    translate = translateMap.get(keyIndex);
                cacheValues = translateCaches.get(translate.getColumn()).get(fieldValue.toString());
                if (cacheValues == null) {
                    if (translate.getUncached() != null)
                        fieldValue = translate.getUncached().replace("${value}", fieldValue.toString());
                    else
                        fieldValue = fieldValue.toString();
                    if (isDebug)
                        logger.debug("translate cache:{} 对应的key:{}没有设置相应的value!", translate.getCache(), fieldValue);
                } else
                    fieldValue = cacheValues[translate.getIndex()];
            }
        }
        rowData.add(fieldValue);
    }
    return rowData;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) SqlTranslate(org.sagacity.sqltoy.config.model.SqlTranslate)

Example 4 with SqlTranslate

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

the class TranslateManager method getTranslates.

/**
 * @todo 根据sqltoy sql.xml中的翻译设置获取对应的缓存(多个translate对应的多个缓存结果)
 * @param sqlToyContext
 * @param conn
 * @param translates
 * @return
 * @throws Exception
 */
public HashMap<String, HashMap<String, Object[]>> getTranslates(SqlToyContext sqlToyContext, Connection conn, HashMap<String, SqlTranslate> translates) throws Exception {
    HashMap<String, HashMap<String, Object[]>> result = new HashMap<String, HashMap<String, Object[]>>();
    SqlTranslate translate;
    HashMap<String, Object[]> cache;
    TranslateConfigModel cacheModel;
    for (Map.Entry<String, SqlTranslate> entry : translates.entrySet()) {
        translate = entry.getValue();
        if (translateMap.containsKey(translate.getCache())) {
            cacheModel = translateMap.get(translate.getCache());
            cache = getCacheData(sqlToyContext, cacheModel, translate.getDictType());
            if (cache != null)
                result.put(translate.getColumn(), cache);
            else {
                result.put(translate.getColumn(), new HashMap<String, Object[]>());
                logger.warn("sqltoy translate:cacheName={},cache-type={},column={}配置不正确,未获取对应cache数据!", cacheModel.getCache(), translate.getDictType(), translate.getColumn());
            }
        } else {
            logger.error("cacheName:{} 没有配置,请检查sqltoy-translate.xml文件!", translate.getCache());
        }
    }
    return result;
}
Also used : TranslateConfigModel(org.sagacity.sqltoy.translate.model.TranslateConfigModel) HashMap(java.util.HashMap) SqlTranslate(org.sagacity.sqltoy.config.model.SqlTranslate) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with SqlTranslate

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

the class ResultUtils method getResultSet.

private static List getResultSet(SqlToyConfig sqlToyConfig, SqlToyContext sqlToyContext, Connection conn, ResultSet rs, UpdateRowHandler updateRowHandler, int rowCnt, HashMap<String, Integer> labelIndexMap, String[] labelNames, int startColIndex) throws Exception {
    // 字段连接(多行数据拼接成一个数据,以一行显示)
    LinkModel linkModel = (sqlToyConfig == null) ? null : sqlToyConfig.getLinkModel();
    List<List> items = new ArrayList();
    boolean isDebug = logger.isDebugEnabled();
    // 判断是否有缓存翻译器定义
    Boolean hasTranslate = (sqlToyConfig.getTranslateMap() == null) ? false : true;
    HashMap<String, SqlTranslate> translateMap = hasTranslate ? sqlToyConfig.getTranslateMap() : null;
    HashMap<String, HashMap<String, Object[]>> translateCache = null;
    if (hasTranslate) {
        translateCache = sqlToyContext.getTranslateManager().getTranslates(sqlToyContext, conn, translateMap);
        if (translateCache == null || translateCache.isEmpty()) {
            hasTranslate = false;
            logger.debug("请正确配置TranslateManager!");
        }
    }
    // link 目前只支持单个字段运算
    int columnSize = labelNames.length;
    int index = 0;
    // 警告阀值
    int warnThresholds = SqlToyConstants.getWarnThresholds();
    boolean warnLimit = false;
    // 最大阀值
    long maxThresholds = SqlToyConstants.getMaxThresholds();
    boolean maxLimit = false;
    // 最大值要大于等于警告阀值
    if (maxThresholds > 1 && maxThresholds <= warnThresholds)
        maxThresholds = warnThresholds;
    if (linkModel != null) {
        Object identity = null;
        int linkIndex = labelIndexMap.get(linkModel.getColumn().toLowerCase());
        StringBuilder linkBuffer = new StringBuilder();
        boolean hasDecorate = (linkModel.getDecorateAppendChar() == null) ? false : true;
        boolean isLeft = true;
        if (hasDecorate) {
            isLeft = linkModel.getDecorateAlign().equals("left") ? true : false;
        }
        Object preIdentity = null;
        Object linkValue;
        Object linkStr;
        boolean translateLink = hasTranslate ? translateMap.containsKey(linkModel.getColumn().toLowerCase()) : false;
        HashMap<String, Object[]> linkTranslateMap = null;
        int linkTranslateIndex = 1;
        SqlTranslate translateModel = null;
        if (translateLink) {
            translateModel = translateMap.get(linkModel.getColumn().toLowerCase());
            linkTranslateIndex = translateModel.getIndex();
            linkTranslateMap = translateCache.get(translateModel.getColumn());
        }
        Object[] cacheValues;
        // 判断link拼接是否重新开始
        boolean isLastProcess = false;
        while (rs.next()) {
            isLastProcess = false;
            linkValue = rs.getObject(linkModel.getColumn());
            if (linkValue == null)
                linkStr = "";
            else if (translateLink) {
                cacheValues = linkTranslateMap.get(linkValue.toString());
                if (cacheValues == null) {
                    linkStr = "";
                    if (isDebug)
                        logger.debug("translate cache:" + translateModel.getCache() + " 对应的key:" + linkValue + " 没有设置相应的value!");
                } else
                    linkStr = cacheValues[linkTranslateIndex];
            } else
                linkStr = linkValue.toString();
            identity = (linkModel.getIdColumn() == null) ? "default" : rs.getObject(linkModel.getIdColumn());
            // 不相等
            if (!identity.equals(preIdentity)) {
                if (index != 0) {
                    items.get(items.size() - 1).set(linkIndex, linkBuffer.toString());
                    linkBuffer.delete(0, linkBuffer.length());
                }
                linkBuffer.append(linkStr);
                if (hasTranslate)
                    items.add(processResultRowWithTranslate(translateMap, translateCache, labelNames, rs, columnSize));
                else
                    items.add(processResultRow(rs, startColIndex, rowCnt));
                preIdentity = identity;
            } else {
                if (linkBuffer.length() > 0)
                    linkBuffer.append(linkModel.getSign());
                linkBuffer.append(hasDecorate ? StringUtil.appendStr(linkStr.toString(), linkModel.getDecorateAppendChar(), linkModel.getDecorateSize(), isLeft) : linkStr);
                isLastProcess = true;
            }
            index++;
            // 存在超出25000条数据的查询
            if (index == warnThresholds) {
                warnLimit = true;
            }
            // 提取数据超过上限(-1表示不限制)
            if (index == maxThresholds) {
                maxLimit = true;
                break;
            }
        }
        if (isLastProcess) {
            items.get(items.size() - 1).set(linkIndex, linkBuffer.toString());
        }
    } else {
        // 修改操作不支持link操作
        boolean isUpdate = false;
        if (updateRowHandler != null)
            isUpdate = true;
        // 循环通过java reflection将rs中的值映射到VO中
        if (hasTranslate) {
            while (rs.next()) {
                // 先修改后再获取最终值
                if (isUpdate) {
                    updateRowHandler.updateRow(rs, index);
                    rs.updateRow();
                }
                items.add(processResultRowWithTranslate(translateMap, translateCache, labelNames, rs, columnSize));
                index++;
                // 存在超出25000条数据的查询(具体数据规模可以通过参数进行定义)
                if (index == warnThresholds) {
                    warnLimit = true;
                }
                // 超出最大提取数据阀值,直接终止数据提取
                if (index == maxThresholds) {
                    maxLimit = true;
                    break;
                }
            }
        } else {
            while (rs.next()) {
                if (isUpdate) {
                    updateRowHandler.updateRow(rs, index);
                    rs.updateRow();
                }
                items.add(processResultRow(rs, startColIndex, rowCnt));
                index++;
                // 存在超出警告规模级的数据查询
                if (index == warnThresholds) {
                    warnLimit = true;
                }
                // 提取数据超过上限(-1表示不限制)
                if (index == maxThresholds) {
                    maxLimit = true;
                    break;
                }
            }
        }
    }
    // 超出警告阀值
    if (warnLimit)
        warnLog(sqlToyConfig, index);
    // 超过最大提取数据阀值
    if (maxLimit)
        logger.error("Max Large Result:执行sql提取数据超出最大阀值限制{},sqlId={},具体语句={}", index, sqlToyConfig.getId(), sqlToyConfig.getSql());
    return items;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkModel(org.sagacity.sqltoy.config.model.LinkModel) ArrayList(java.util.ArrayList) List(java.util.List) SqlTranslate(org.sagacity.sqltoy.config.model.SqlTranslate)

Aggregations

SqlTranslate (org.sagacity.sqltoy.config.model.SqlTranslate)5 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 List (java.util.List)2 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Element (org.dom4j.Element)1 LinkModel (org.sagacity.sqltoy.config.model.LinkModel)1 TranslateConfigModel (org.sagacity.sqltoy.translate.model.TranslateConfigModel)1