Search in sources :

Example 31 with QueryResult

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

the class Query method findTop.

/**
 * @todo 取前多少条记录
 * @param topSize
 * @return
 */
public List<?> findTop(final double topSize) {
    QueryExecutor queryExecute = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecute, SqlType.search, getDialect());
    QueryResult result = dialectFactory.findTop(sqlToyContext, queryExecute, sqlToyConfig, topSize, getDataSource(sqlToyConfig));
    return result.getRows();
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 32 with QueryResult

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

the class Query method getValue.

/**
 * @todo 获取单值
 * @return
 */
public Object getValue() {
    QueryExecutor queryExecute = new QueryExecutor(sql).names(names).values(values);
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecute, SqlType.search, getDialect());
    QueryResult result = dialectFactory.findByQuery(sqlToyContext, queryExecute, sqlToyConfig, null, getDataSource(sqlToyConfig));
    List rows = result.getRows();
    if (rows != null && rows.size() > 0) {
        return ((List) rows.get(0)).get(0);
    }
    return null;
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) List(java.util.List)

Example 33 with QueryResult

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

the class Query method find.

/**
 * @todo 查询结果集合
 * @return
 */
public List<?> find() {
    QueryExecutor queryExecute = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecute, SqlType.search, getDialect());
    QueryResult result = dialectFactory.findByQuery(sqlToyContext, queryExecute, sqlToyConfig, lockMode, getDataSource(sqlToyConfig));
    return result.getRows();
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 34 with QueryResult

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

the class ResultUtils method processResultSet.

/**
 * @todo 处理sql查询时的结果集,当没有反调或voClass反射处理时以数组方式返回resultSet的数据
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param conn
 * @param rs
 * @param rowCallbackHandler
 * @param updateRowHandler
 * @param decryptHandler
 * @param startColIndex
 * @return
 * @throws Exception
 */
public static QueryResult processResultSet(final SqlToyContext sqlToyContext, final SqlToyConfig sqlToyConfig, Connection conn, ResultSet rs, RowCallbackHandler rowCallbackHandler, UpdateRowHandler updateRowHandler, DecryptHandler decryptHandler, int startColIndex) throws Exception {
    QueryResult result = new QueryResult();
    // 记录行记数器
    int index = 0;
    if (rowCallbackHandler != null) {
        while (rs.next()) {
            rowCallbackHandler.processRow(rs, index);
            index++;
        }
        result.setRows(rowCallbackHandler.getResult());
    } else {
        // 重新组合解密字段(entityMeta中的和sql自定义的合并)
        IgnoreCaseSet decryptColumns = (decryptHandler == null) ? null : decryptHandler.getColumns();
        if (sqlToyConfig.getDecryptColumns() != null) {
            if (decryptColumns == null) {
                decryptColumns = sqlToyConfig.getDecryptColumns();
            } else {
                decryptColumns.addAll(sqlToyConfig.getDecryptColumns());
            }
        }
        DecryptHandler realDecryptHandler = null;
        if (decryptColumns != null && !decryptColumns.isEmpty()) {
            realDecryptHandler = new DecryptHandler(sqlToyContext.getFieldsSecureProvider(), decryptColumns);
        }
        // 取得字段列数,在没有rowCallbackHandler時用数组返回
        int rowCnt = rs.getMetaData().getColumnCount();
        // 类型转成string的列
        Set<String> strTypeCols = getStringColumns(sqlToyConfig);
        boolean hasToStrCols = !strTypeCols.isEmpty();
        String[] labelNames = new String[rowCnt - startColIndex];
        String[] labelTypes = new String[rowCnt - startColIndex];
        HashMap<String, Integer> labelIndexMap = new HashMap<String, Integer>();
        String labeNameLow;
        for (int i = startColIndex; i < rowCnt; i++) {
            labelNames[index] = rs.getMetaData().getColumnLabel(i + 1);
            labeNameLow = labelNames[index].toLowerCase();
            labelIndexMap.put(labeNameLow, index);
            labelTypes[index] = rs.getMetaData().getColumnTypeName(i + 1);
            // 类型因缓存翻译、格式化转为string
            if (hasToStrCols) {
                if (strTypeCols.contains(labeNameLow)) {
                    labelTypes[index] = "VARCHAR";
                }
            }
            index++;
        }
        result.setLabelNames(labelNames);
        result.setLabelTypes(labelTypes);
        // 返回结果为非VO class时才可以应用旋转和汇总合计功能
        try {
            result.setRows(getResultSet(sqlToyConfig, sqlToyContext, conn, rs, updateRowHandler, realDecryptHandler, rowCnt, labelIndexMap, labelNames, startColIndex));
        }// update 2019-09-11 此处增加数组溢出异常是因为经常有开发设置缓存cache-indexs时写错误,为了增加错误提示信息的友好性增加此处理
         catch (Exception oie) {
            logger.error("sql={} 提取结果发生异常:{}!", sqlToyConfig.getId(), oie.getMessage());
            throw oie;
        }
    }
    // 填充记录数
    if (result.getRows() != null) {
        result.setRecordCount(Long.valueOf(result.getRows().size()));
    }
    return result;
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) DecryptHandler(org.sagacity.sqltoy.callback.DecryptHandler) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IgnoreCaseSet(org.sagacity.sqltoy.model.IgnoreCaseSet) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Aggregations

QueryResult (org.sagacity.sqltoy.model.QueryResult)34 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)20 List (java.util.List)14 Connection (java.sql.Connection)13 ArrayList (java.util.ArrayList)13 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)13 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)11 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)9 ResultSet (java.sql.ResultSet)8 BaseException (org.sagacity.sqltoy.exception.BaseException)7 QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)7 QueryExecutor (org.sagacity.sqltoy.model.QueryExecutor)6 CallableStatement (java.sql.CallableStatement)4 PreparedStatement (java.sql.PreparedStatement)4 CallableStatementResultHandler (org.sagacity.sqltoy.callback.CallableStatementResultHandler)4 PreparedStatementResultHandler (org.sagacity.sqltoy.callback.PreparedStatementResultHandler)4 StoreResult (org.sagacity.sqltoy.model.StoreResult)4 Serializable (java.io.Serializable)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2