Search in sources :

Example 1 with TranslateModel

use of org.sagacity.sqltoy.config.model.TranslateModel 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, TranslateModel> translateMap, HashMap<String, HashMap<String, Object[]>> translateCaches, String[] labelNames, ResultSet rs, int size) throws Exception {
    List rowData = new ArrayList();
    Object fieldValue;
    TranslateModel 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 : TranslateModel(org.sagacity.sqltoy.config.model.TranslateModel) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with TranslateModel

use of org.sagacity.sqltoy.config.model.TranslateModel 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, TranslateModel> 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;
    TranslateModel 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 : TranslateModel(org.sagacity.sqltoy.config.model.TranslateModel) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with TranslateModel

use of org.sagacity.sqltoy.config.model.TranslateModel 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, TranslateModel> translates) throws Exception {
    HashMap<String, HashMap<String, Object[]>> result = new HashMap<String, HashMap<String, Object[]>>();
    TranslateModel translate;
    String cacheName;
    HashMap<String, Object[]> cache;
    TranslateCacheModel cacheModel;
    for (Map.Entry<String, TranslateModel> entry : translates.entrySet()) {
        translate = entry.getValue();
        if (translateMap.containsKey(translate.getCache())) {
            cacheModel = translateMap.get(translate.getCache());
            cacheName = cacheModel.getCacheName();
            cache = getCache(sqlToyContext, conn, cacheName, 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数据!", cacheName, translate.getDictType(), translate.getColumn());
            }
        }
    }
    return result;
}
Also used : TranslateCacheModel(org.sagacity.sqltoy.config.model.TranslateCacheModel) TranslateModel(org.sagacity.sqltoy.config.model.TranslateModel) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

TranslateModel (org.sagacity.sqltoy.config.model.TranslateModel)3 HashMap (java.util.HashMap)2 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 TranslateCacheModel (org.sagacity.sqltoy.config.model.TranslateCacheModel)1