Search in sources :

Example 1 with Translate

use of org.sagacity.sqltoy.config.model.Translate 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, DecryptHandler decryptHandler, int rowCnt, HashMap<String, Integer> labelIndexMap, String[] labelNames, int startColIndex) throws Exception {
    // 字段连接(多行数据拼接成一个数据,以一行显示)
    LinkModel linkModel = sqlToyConfig.getLinkModel();
    // update 2020-09-13 存在多列link(独立出去编写,避免对单列产生影响)
    if (linkModel != null && linkModel.getColumns().length > 1) {
        return getMoreLinkResultSet(sqlToyConfig, sqlToyContext, decryptHandler, conn, rs, rowCnt, labelIndexMap, labelNames, startColIndex);
    }
    List<List> items = new ArrayList();
    // 判断是否有缓存翻译器定义
    Boolean hasTranslate = (sqlToyConfig.getTranslateMap().isEmpty()) ? false : true;
    HashMap<String, Translate> translateMap = sqlToyConfig.getTranslateMap();
    HashMap<String, HashMap<String, Object[]>> translateCache = null;
    if (hasTranslate) {
        translateCache = sqlToyContext.getTranslateManager().getTranslates(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;
    // 是否判断全部为null的行记录
    boolean ignoreAllEmpty = sqlToyConfig.isIgnoreEmpty();
    // 最大值要大于等于警告阀值
    if (maxThresholds > 1 && maxThresholds <= warnThresholds) {
        maxThresholds = warnThresholds;
    }
    List rowTemp;
    if (linkModel != null) {
        Object identity = null;
        String linkColumn = linkModel.getColumns()[0];
        String linkColumnLow = linkColumn.toLowerCase();
        if (!labelIndexMap.containsKey(linkColumnLow)) {
            throw new DataAccessException("做link操作时,查询结果字段中没有字段:" + linkColumn + ",请检查sql或link 配置的正确性!");
        }
        Set<String> linkSet = new HashSet<String>();
        int linkIndex = labelIndexMap.get(linkColumnLow);
        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;
        String linkStr;
        boolean translateLink = hasTranslate ? translateMap.containsKey(linkColumnLow) : false;
        HashMap<String, Object[]> linkTranslateMap = null;
        int linkTranslateIndex = 1;
        TranslateExtend extend = null;
        if (translateLink) {
            extend = translateMap.get(linkColumnLow).getExtend();
            linkTranslateIndex = extend.index;
            linkTranslateMap = translateCache.get(extend.column);
        }
        Object[] cacheValues;
        // 判断link拼接是否重新开始
        boolean isLastProcess = false;
        boolean doLink = true;
        while (rs.next()) {
            isLastProcess = false;
            linkValue = rs.getObject(linkColumn);
            if (linkValue == null) {
                linkStr = "";
            } else {
                if (translateLink) {
                    cacheValues = linkTranslateMap.get(linkValue.toString());
                    if (cacheValues == null) {
                        linkStr = "[" + linkValue + "]未匹配";
                        logger.debug("translate cache:{},cacheType:{}, 对应的key:{} 没有设置相应的value!", extend.cache, extend.cacheType, linkValue);
                    } else {
                        linkStr = (cacheValues[linkTranslateIndex] == null) ? "" : cacheValues[linkTranslateIndex].toString();
                    }
                } else {
                    linkStr = linkValue.toString();
                }
            }
            identity = (linkModel.getIdColumns() == null) ? "default" : getLinkColumnsId(rs, linkModel.getIdColumns());
            // 不相等
            if (!identity.equals(preIdentity)) {
                if (index != 0) {
                    items.get(items.size() - 1).set(linkIndex, linkBuffer.toString());
                    linkBuffer.delete(0, linkBuffer.length());
                    linkSet.clear();
                }
                linkBuffer.append(linkStr);
                linkSet.add(linkStr);
                if (hasTranslate) {
                    rowTemp = processResultRowWithTranslate(translateMap, translateCache, labelNames, rs, columnSize, decryptHandler, ignoreAllEmpty);
                } else {
                    rowTemp = processResultRow(rs, labelNames, columnSize, decryptHandler, ignoreAllEmpty);
                }
                if (rowTemp != null) {
                    items.add(rowTemp);
                }
                preIdentity = identity;
            } else {
                isLastProcess = true;
                doLink = true;
                if (linkModel.isDistinct() && linkSet.contains(linkStr)) {
                    doLink = false;
                }
                linkSet.add(linkStr);
                if (doLink) {
                    if (linkBuffer.length() > 0) {
                        linkBuffer.append(linkModel.getSign());
                    }
                    linkBuffer.append(hasDecorate ? StringUtil.appendStr(linkStr, linkModel.getDecorateAppendChar(), linkModel.getDecorateSize(), isLeft) : linkStr);
                }
            }
            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();
                }
                rowTemp = processResultRowWithTranslate(translateMap, translateCache, labelNames, rs, columnSize, decryptHandler, ignoreAllEmpty);
                if (rowTemp != null) {
                    items.add(rowTemp);
                }
                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();
                }
                rowTemp = processResultRow(rs, labelNames, columnSize, decryptHandler, ignoreAllEmpty);
                if (rowTemp != null) {
                    items.add(rowTemp);
                }
                index++;
                // 存在超出警告规模级的数据查询
                if (index == warnThresholds) {
                    warnLimit = true;
                }
                // 提取数据超过上限(-1表示不限制)
                if (index == maxThresholds) {
                    maxLimit = true;
                    break;
                }
            }
        }
    }
    // 超出警告阀值
    if (warnLimit) {
        warnLog(sqlToyConfig, index);
    }
    // 超过最大提取数据阀值
    if (maxLimit) {
        logger.error("MaxLargeResult:执行sql提取数据超出最大阀值限制{},sqlId={},具体语句={}", index, sqlToyConfig.getId(), sqlToyConfig.getSql(null));
    }
    return items;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkModel(org.sagacity.sqltoy.config.model.LinkModel) TranslateExtend(org.sagacity.sqltoy.model.inner.TranslateExtend) ReverseList(org.sagacity.sqltoy.plugins.calculator.ReverseList) List(java.util.List) ArrayList(java.util.ArrayList) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) Translate(org.sagacity.sqltoy.config.model.Translate) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) HashSet(java.util.HashSet)

Example 2 with Translate

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

the class QueryExecutor method translates.

/**
 * @TODO 对sql语句指定缓存翻译
 * @param translates
 * @return
 */
public QueryExecutor 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.column)) {
                throw new IllegalArgumentException("给查询增加的缓存翻译时未定义具体的cacheName=[" + extend.cache + "] 或 对应的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 3 with Translate

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

the class SqlToyDaoSupport method findEntityBase.

/**
 * @TODO 提供findEntity的基础实现,供对外接口包装,额外开放了resultClass的自定义功能
 * @param entityClass
 * @param page        如分页查询则需指定,非分页则传null
 * @param entityQuery
 * @param resultClass 指定返回结果类型
 * @param isCount
 * @return
 */
private Object findEntityBase(Class entityClass, Page page, EntityQuery entityQuery, Class resultClass, boolean isCount) {
    EntityMeta entityMeta = getEntityMeta(entityClass);
    EntityQueryExtend innerModel = entityQuery.getInnerModel();
    String translateFields = "";
    // 将缓存翻译对应的查询补充到select column 上,形成select keyColumn as viewColumn 模式
    if (!innerModel.translates.isEmpty()) {
        Iterator<Translate> iter = innerModel.translates.values().iterator();
        String keyColumn;
        TranslateExtend extend;
        while (iter.hasNext()) {
            extend = iter.next().getExtend();
            // 将java模式的字段名称转化为数据库字段名称
            keyColumn = entityMeta.getColumnName(extend.keyColumn);
            if (keyColumn == null) {
                keyColumn = extend.keyColumn;
            }
            // 保留字处理
            keyColumn = ReservedWordsUtil.convertWord(keyColumn, null);
            translateFields = translateFields.concat(",").concat(keyColumn).concat(" as ").concat(extend.column);
        }
    }
    // 将notSelect构造成select,形成统一处理机制
    String[] selectFieldAry = null;
    Set<String> notSelect = innerModel.notSelectFields;
    if (notSelect != null) {
        List<String> selectFields = new ArrayList<String>();
        for (String field : entityMeta.getFieldsArray()) {
            if (!notSelect.contains(field.toLowerCase())) {
                selectFields.add(field);
            }
        }
        if (selectFields.size() > 0) {
            selectFieldAry = new String[selectFields.size()];
            selectFields.toArray(selectFieldAry);
        }
    } else {
        selectFieldAry = innerModel.fields;
    }
    // 指定的查询字段
    String fields = "";
    if (selectFieldAry != null && selectFieldAry.length > 0) {
        int index = 0;
        String colName;
        HashSet<String> cols = new HashSet<String>();
        boolean notAllPureField = false;
        for (String field : selectFieldAry) {
            // 去除重复字段
            if (!cols.contains(field)) {
                colName = entityMeta.getColumnName(field);
                // 非表字段对应pojo的属性名称
                if (colName == null) {
                    colName = field;
                    // 非字段名称
                    if (!entityMeta.getColumnFieldMap().containsKey(colName.toLowerCase())) {
                        notAllPureField = true;
                    } else {
                        // 保留字处理
                        colName = ReservedWordsUtil.convertWord(colName, null);
                    }
                } else {
                    // 保留字处理
                    colName = ReservedWordsUtil.convertWord(colName, null);
                }
                if (index > 0) {
                    fields = fields.concat(",");
                }
                fields = fields.concat(colName);
                index++;
                cols.add(field);
            }
        }
        // select 字段中可能存在max(field)或field as xxx等非字段形式
        if (notAllPureField) {
            fields = SqlUtil.convertFieldsToColumns(entityMeta, fields);
        }
    } else {
        fields = entityMeta.getAllColumnNames();
    }
    String sql = "select ".concat((innerModel.distinct) ? " distinct " : "").concat(fields).concat(translateFields).concat(" from ").concat(entityMeta.getSchemaTable(null, null));
    // where条件
    String where = "";
    // 动态组织where 后面的条件语句,此功能并不建议使用,where 一般需要指定明确条件
    if (StringUtil.isBlank(innerModel.where)) {
        if (innerModel.values != null && innerModel.values.length > 0) {
            where = SqlUtil.wrapWhere(entityMeta);
        }
    } else {
        where = SqlUtil.convertFieldsToColumns(entityMeta, innerModel.where);
    }
    if (StringUtil.isNotBlank(where)) {
        sql = sql.concat(" where ").concat(where);
    }
    // 分组和having
    if (StringUtil.isNotBlank(innerModel.groupBy)) {
        sql = sql.concat(" group by ").concat(SqlUtil.convertFieldsToColumns(entityMeta, innerModel.groupBy));
        if (StringUtil.isNotBlank(innerModel.having)) {
            sql = sql.concat(" having ").concat(SqlUtil.convertFieldsToColumns(entityMeta, innerModel.having));
        }
    }
    // 处理order by 排序
    if (!innerModel.orderBy.isEmpty()) {
        sql = sql.concat(" order by ");
        Iterator<Entry<String, String>> iter = innerModel.orderBy.entrySet().iterator();
        Entry<String, String> entry;
        String columnName;
        int index = 0;
        while (iter.hasNext()) {
            entry = iter.next();
            columnName = entityMeta.getColumnName(entry.getKey());
            if (columnName == null) {
                columnName = entry.getKey();
            }
            // 保留字处理
            columnName = ReservedWordsUtil.convertWord(columnName, null);
            if (index > 0) {
                sql = sql.concat(",");
            }
            // entry.getValue() is order way,like: desc or " "
            sql = sql.concat(columnName).concat(entry.getValue());
            index++;
        }
    }
    QueryExecutor queryExecutor;
    Class resultType = (resultClass == null) ? entityClass : resultClass;
    // :named 模式(named模式参数值必须存在)
    if (SqlConfigParseUtils.hasNamedParam(where) && StringUtil.isBlank(innerModel.names)) {
        queryExecutor = new QueryExecutor(sql, (innerModel.values == null || innerModel.values.length == 0) ? null : (Serializable) innerModel.values[0]).resultType(resultType).dataSource(getDataSource(innerModel.dataSource)).fetchSize(innerModel.fetchSize).maxRows(innerModel.maxRows);
    } else {
        queryExecutor = new QueryExecutor(sql).names(innerModel.names).values(innerModel.values).resultType(resultType).dataSource(getDataSource(innerModel.dataSource)).fetchSize(innerModel.fetchSize).maxRows(innerModel.maxRows);
    }
    // 设置是否空白转null
    queryExecutor.getInnerModel().blankToNull = innerModel.blankToNull;
    // 设置额外的缓存翻译
    if (!innerModel.translates.isEmpty()) {
        queryExecutor.getInnerModel().translates.putAll(innerModel.translates);
    }
    // 设置额外的参数条件过滤
    if (!innerModel.paramFilters.isEmpty()) {
        queryExecutor.getInnerModel().paramFilters.addAll(innerModel.paramFilters);
    }
    // 设置安全脱敏
    if (!innerModel.secureMask.isEmpty()) {
        queryExecutor.getInnerModel().secureMask.putAll(innerModel.secureMask);
    }
    // 设置分页优化
    queryExecutor.getInnerModel().pageOptimize = innerModel.pageOptimize;
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor, SqlType.search, getDialect(queryExecutor.getInnerModel().dataSource));
    // 加密字段,查询时解密
    if (entityMeta.getSecureColumns() != null) {
        sqlToyConfig.setDecryptColumns(entityMeta.getSecureColumns());
    }
    // 分库分表策略
    setEntitySharding(queryExecutor, entityMeta);
    if (innerModel.dbSharding != null) {
        queryExecutor.getInnerModel().dbSharding = innerModel.dbSharding;
    }
    if (innerModel.tableSharding != null) {
        ShardingStrategyConfig shardingConfig = innerModel.tableSharding;
        // 补充表名称
        shardingConfig.setTables(new String[] { entityMeta.getTableName() });
        List<ShardingStrategyConfig> tableShardings = new ArrayList<ShardingStrategyConfig>();
        tableShardings.add(shardingConfig);
        queryExecutor.getInnerModel().tableShardings = tableShardings;
    }
    DataSource realDataSource = getDataSource(queryExecutor.getInnerModel().dataSource, sqlToyConfig);
    // 取count数量
    if (isCount) {
        return dialectFactory.getCountBySql(sqlToyContext, queryExecutor, sqlToyConfig, realDataSource);
    }
    // 非分页
    if (page == null) {
        // 取top
        if (innerModel.pickType == 0) {
            return dialectFactory.findTop(sqlToyContext, queryExecutor, sqlToyConfig, innerModel.pickSize, realDataSource).getRows();
        } else // 取随机记录
        if (innerModel.pickType == 1) {
            return dialectFactory.getRandomResult(sqlToyContext, queryExecutor, sqlToyConfig, innerModel.pickSize, realDataSource).getRows();
        } else {
            return dialectFactory.findByQuery(sqlToyContext, queryExecutor, sqlToyConfig, innerModel.lockMode, realDataSource).getRows();
        }
    }
    // 跳过总记录数形式的分页
    if (page.getSkipQueryCount()) {
        return dialectFactory.findSkipTotalCountPage(sqlToyContext, queryExecutor, sqlToyConfig, page.getPageNo(), page.getPageSize(), realDataSource).getPageResult();
    }
    return dialectFactory.findPage(sqlToyContext, queryExecutor, sqlToyConfig, page.getPageNo(), page.getPageSize(), realDataSource).getPageResult();
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) ArrayList(java.util.ArrayList) DataSource(javax.sql.DataSource) Entry(java.util.Map.Entry) ParallQueryExecutor(org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) ShardingStrategyConfig(org.sagacity.sqltoy.config.model.ShardingStrategyConfig) TranslateExtend(org.sagacity.sqltoy.model.inner.TranslateExtend) EntityQueryExtend(org.sagacity.sqltoy.model.inner.EntityQueryExtend) Translate(org.sagacity.sqltoy.config.model.Translate) HashSet(java.util.HashSet)

Example 4 with Translate

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

the class ResultUtils method getMoreLinkResultSet.

/**
 * @TODO 实现多列link
 * @param sqlToyConfig
 * @param sqlToyContext
 * @param decryptHandler
 * @param conn
 * @param rs
 * @param rowCnt
 * @param labelIndexMap
 * @param labelNames
 * @param startColIndex
 * @return
 * @throws Exception
 */
private static List getMoreLinkResultSet(SqlToyConfig sqlToyConfig, SqlToyContext sqlToyContext, DecryptHandler decryptHandler, Connection conn, ResultSet rs, int rowCnt, HashMap<String, Integer> labelIndexMap, String[] labelNames, int startColIndex) throws Exception {
    // 字段连接(多行数据拼接成一个数据,以一行显示)
    LinkModel linkModel = sqlToyConfig.getLinkModel();
    List<List> items = new ArrayList();
    // 判断是否有缓存翻译器定义
    Boolean hasTranslate = (sqlToyConfig.getTranslateMap().isEmpty()) ? false : true;
    HashMap<String, Translate> translateMap = sqlToyConfig.getTranslateMap();
    HashMap<String, HashMap<String, Object[]>> translateCache = null;
    if (hasTranslate) {
        translateCache = sqlToyContext.getTranslateManager().getTranslates(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;
    // 是否判断全部为null的行记录
    boolean ignoreAllEmpty = sqlToyConfig.isIgnoreEmpty();
    // 最大值要大于等于警告阀值
    if (maxThresholds > 1 && maxThresholds <= warnThresholds) {
        maxThresholds = warnThresholds;
    }
    int linkCols = linkModel.getColumns().length;
    String[] linkColumns = linkModel.getColumns();
    int[] linkIndexs = new int[linkCols];
    StringBuilder[] linkBuffers = new StringBuilder[linkCols];
    Set<String>[] linkSets = linkModel.isDistinct() ? new HashSet[linkCols] : null;
    boolean[] translateLinks = new boolean[linkCols];
    TranslateExtend[] transExtends = new TranslateExtend[linkCols];
    String linkColumnLow;
    for (int i = 0; i < linkCols; i++) {
        linkBuffers[i] = new StringBuilder();
        linkColumnLow = linkColumns[i].toLowerCase();
        if (!labelIndexMap.containsKey(linkColumnLow)) {
            throw new DataAccessException("做link操作时,查询结果字段中没有字段:" + linkColumnLow + ",请检查sql或link 配置的正确性!");
        }
        linkIndexs[i] = labelIndexMap.get(linkColumnLow);
        if (hasTranslate) {
            translateLinks[i] = translateMap.containsKey(linkColumnLow);
            if (translateLinks[i]) {
                transExtends[i] = translateMap.get(linkColumnLow).getExtend();
            }
        }
        if (linkModel.isDistinct()) {
            linkSets[i] = new HashSet<String>();
        }
    }
    // link是否有修饰器
    boolean hasDecorate = (linkModel.getDecorateAppendChar() == null) ? false : true;
    boolean isLeft = true;
    if (hasDecorate) {
        isLeft = linkModel.getDecorateAlign().equals("left") ? true : false;
    }
    Object preIdentity = null;
    Object[] linkValues = new Object[linkCols];
    String[] linkStrs = new String[linkCols];
    TranslateExtend extend = null;
    Object[] cacheValues;
    List rowTemp;
    Object identity = null;
    // 判断link拼接是否重新开始
    boolean isLastProcess = false;
    boolean doLink = false;
    while (rs.next()) {
        isLastProcess = false;
        // 对多个link字段取值并进行翻译转义
        for (int i = 0; i < linkCols; i++) {
            linkValues[i] = rs.getObject(linkColumns[i]);
            if (linkValues[i] == null) {
                linkStrs[i] = "";
            } else {
                if (translateLinks[i]) {
                    extend = transExtends[i];
                    cacheValues = translateCache.get(extend.column).get(linkValues[i].toString());
                    if (cacheValues == null) {
                        linkStrs[i] = "[" + linkValues[i] + "]未匹配";
                        logger.debug("translate cache:{},cacheType:{}, 对应的key:{} 没有设置相应的value!", extend.cache, extend.cacheType, linkValues[i]);
                    } else {
                        linkStrs[i] = (cacheValues[extend.index] == null) ? "" : cacheValues[extend.index].toString();
                    }
                } else {
                    linkStrs[i] = linkValues[i].toString();
                }
            }
        }
        // 取分组列的值
        identity = (linkModel.getIdColumns() == null) ? "default" : getLinkColumnsId(rs, linkModel.getIdColumns());
        // 不相等
        if (!identity.equals(preIdentity)) {
            // 不相等时先对最后一条记录修改,写入拼接后的字符串
            if (index != 0) {
                rowTemp = items.get(items.size() - 1);
                for (int i = 0; i < linkCols; i++) {
                    rowTemp.set(linkIndexs[i], linkBuffers[i].toString());
                    linkBuffers[i].delete(0, linkBuffers[i].length());
                    // 清除
                    if (linkModel.isDistinct()) {
                        linkSets[i].clear();
                    }
                }
            }
            // 再写入新的拼接串
            for (int i = 0; i < linkCols; i++) {
                linkBuffers[i].append(linkStrs[i]);
                if (linkModel.isDistinct()) {
                    linkSets[i].add(linkStrs[i]);
                }
            }
            // 提取result中的数据(identity相等时不需要提取)
            if (hasTranslate) {
                rowTemp = processResultRowWithTranslate(translateMap, translateCache, labelNames, rs, columnSize, decryptHandler, ignoreAllEmpty);
            } else {
                rowTemp = processResultRow(rs, labelNames, columnSize, decryptHandler, ignoreAllEmpty);
            }
            if (rowTemp != null) {
                items.add(rowTemp);
            }
            preIdentity = identity;
        } else {
            isLastProcess = true;
            // identity相同,表示还在同一组内,直接拼接link字符
            for (int i = 0; i < linkCols; i++) {
                doLink = true;
                // 判断是否已经重复
                if (linkModel.isDistinct()) {
                    if (linkSets[i].contains(linkStrs[i])) {
                        doLink = false;
                    }
                    linkSets[i].add(linkStrs[i]);
                }
                if (doLink) {
                    if (linkBuffers[i].length() > 0) {
                        linkBuffers[i].append(linkModel.getSign());
                    }
                    linkBuffers[i].append(hasDecorate ? StringUtil.appendStr(linkStrs[i], linkModel.getDecorateAppendChar(), linkModel.getDecorateSize(), isLeft) : linkStrs[i]);
                }
            }
        }
        index++;
        // 存在超出25000条数据的查询
        if (index == warnThresholds) {
            warnLimit = true;
        }
        // 提取数据超过上限(-1表示不限制)
        if (index == maxThresholds) {
            maxLimit = true;
            break;
        }
    }
    // 数据集合不为空,对最后一条记录写入循环值
    if (isLastProcess) {
        rowTemp = items.get(items.size() - 1);
        for (int i = 0; i < linkCols; i++) {
            rowTemp.set(linkIndexs[i], linkBuffers[i].toString());
        }
    }
    // 超出警告阀值
    if (warnLimit) {
        warnLog(sqlToyConfig, index);
    }
    // 超过最大提取数据阀值
    if (maxLimit) {
        logger.error("MaxLargeResult:执行sql提取数据超出最大阀值限制{},sqlId={},具体语句={}", index, sqlToyConfig.getId(), sqlToyConfig.getSql(null));
    }
    return items;
}
Also used : ResultSet(java.sql.ResultSet) Set(java.util.Set) HashSet(java.util.HashSet) IgnoreCaseSet(org.sagacity.sqltoy.model.IgnoreCaseSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) LinkModel(org.sagacity.sqltoy.config.model.LinkModel) ReverseList(org.sagacity.sqltoy.plugins.calculator.ReverseList) List(java.util.List) ArrayList(java.util.ArrayList) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) Translate(org.sagacity.sqltoy.config.model.Translate) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) TranslateExtend(org.sagacity.sqltoy.model.inner.TranslateExtend)

Example 5 with Translate

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

the class ResultUtils method wrapResultTranslate.

/**
 * @TODO 对返回POJO(或DTO)含@Translate 配置的结果进行缓存翻译处理,通过key属性的值翻译成名称反射到当前名称属性上
 * @param sqlToyContext
 * @param result
 * @param resultType
 */
public static void wrapResultTranslate(SqlToyContext sqlToyContext, Object result, Class resultType) {
    HashMap<String, Translate> translateConfig = TranslateConfigParse.getClassTranslates(resultType);
    if (result == null || translateConfig == null || translateConfig.isEmpty()) {
        return;
    }
    // 获取缓存数据
    HashMap<String, HashMap<String, Object[]>> cacheDatas = sqlToyContext.getTranslateManager().getTranslates(translateConfig);
    List voList;
    if (result instanceof List) {
        voList = (List) result;
    } else {
        voList = new ArrayList();
        voList.add(result);
    }
    if (voList.isEmpty()) {
        return;
    }
    Object item;
    String field = null;
    TranslateExtend trans;
    Object srcFieldValue;
    Object fieldValue;
    HashMap<String, Object[]> cacheData;
    try {
        for (int i = 0; i < voList.size(); i++) {
            item = voList.get(i);
            for (Map.Entry<String, Translate> entry : translateConfig.entrySet()) {
                field = entry.getKey();
                trans = entry.getValue().getExtend();
                // column是field小写后的值
                cacheData = cacheDatas.get(trans.column);
                srcFieldValue = BeanUtil.getProperty(item, trans.keyColumn);
                fieldValue = BeanUtil.getProperty(item, field);
                if (srcFieldValue != null && !srcFieldValue.toString().equals("") && fieldValue == null) {
                    BeanUtil.setProperty(item, field, translateKey(trans, cacheData, srcFieldValue));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        logger.error("针对类:{} 的属性:{} 进行缓存翻译发生异常!{}", resultType.getName(), field, e.getMessage());
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) TranslateExtend(org.sagacity.sqltoy.model.inner.TranslateExtend) ReverseList(org.sagacity.sqltoy.plugins.calculator.ReverseList) List(java.util.List) ArrayList(java.util.ArrayList) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) Translate(org.sagacity.sqltoy.config.model.Translate) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) IgnoreKeyCaseMap(org.sagacity.sqltoy.model.IgnoreKeyCaseMap)

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