Search in sources :

Example 16 with QueryResult

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

the class DialectFactory method getRandomResult.

/**
 * @todo 取随机记录
 * @param sqlToyContext
 * @param queryExecutor
 * @param randomCount
 * @param dataSource
 * @return
 * @throws Exception
 */
public QueryResult getRandomResult(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final Double randomCount, final DataSource dataSource) throws Exception {
    if (queryExecutor.getSql() == null)
        throw new Exception("getRandomResult operate sql is null!");
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "getRandomResult", sqlToyConfig.isShowSql());
        return (QueryResult) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                // 处理sql中的?为统一的:named形式
                SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, true);
                // 判断数据库是否支持取随机记录(只有informix和sybase不支持)
                Long totalCount = SqlToyConstants.randomWithDialect(dbType) ? null : getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
                Long randomCnt;
                // 记录数量大于1表示取随机记录数量
                if (randomCount >= 1) {
                    randomCnt = randomCount.longValue();
                } else // 按比例提取
                {
                    // 提取总记录数
                    if (totalCount == null) {
                        totalCount = getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
                    }
                    if (sqlToyContext.isDebug()) {
                        out.println("getRandomResult按比例提取数据,总记录数=" + totalCount);
                    }
                    randomCnt = new Double(totalCount * randomCount.doubleValue()).longValue();
                    // 如果总记录数不为零,randomCnt最小为1
                    if (totalCount >= 1 && randomCnt < 1)
                        randomCnt = 1L;
                }
                // 总记录数为零(主要针对sybase & informix 数据库)
                if (totalCount != null && totalCount == 0) {
                    this.setResult(new QueryResult());
                    logger.warn("getRandom,total Records is zero,please check sql!sqlId={}", sqlToyConfig.getId());
                    return;
                }
                QueryResult queryResult = getDialectSqlWrapper(dbType).getRandomResult(sqlToyContext, realSqlToyConfig, queryExecutor, totalCount, randomCnt, conn);
                // 存在计算和旋转的数据不能映射到对象(数据类型不一致,如汇总平均以及数据旋转)
                List pivotCategorySet = ResultUtils.getPivotCategory(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dialect);
                ResultUtils.calculate(realSqlToyConfig, queryResult, pivotCategorySet, sqlToyContext.isDebug());
                if (queryExecutor.getResultType() != null) {
                    queryResult.setRows(ResultUtils.wrapQueryResult(queryResult.getRows(), ResultUtils.humpFieldNames(queryExecutor, queryResult.getLabelNames()), (Class) queryExecutor.getResultType()));
                }
                this.setResult(queryResult);
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) List(java.util.List) ArrayList(java.util.ArrayList) BaseException(org.sagacity.sqltoy.exception.BaseException) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler)

Example 17 with QueryResult

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

the class DialectFactory method findTop.

/**
 * @todo 取符合条件的前多少条记录
 * @param sqlToyContext
 * @param queryExecutor
 * @param topSize
 * @param dataSource
 * @return
 * @throws Exception
 */
public QueryResult findTop(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final double topSize, final DataSource dataSource) throws Exception {
    if (queryExecutor.getSql() == null)
        throw new Exception("findTop operate sql is null!");
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "findTop", sqlToyConfig.isShowSql());
        return (QueryResult) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                // 处理sql中的?为统一的:named形式
                SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, false);
                Integer realTopSize;
                if (topSize < 1) {
                    Long totalCount = getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
                    if (sqlToyContext.isDebug()) {
                        out.println("findTopByQuery按比例提取数据,总记录数=" + totalCount);
                    }
                    realTopSize = new Double(topSize * totalCount.longValue()).intValue();
                } else
                    realTopSize = new Double(topSize).intValue();
                if (realTopSize == 0) {
                    this.setResult(new QueryResult());
                    return;
                }
                QueryResult queryResult = getDialectSqlWrapper(dbType).findTopBySql(sqlToyContext, realSqlToyConfig, queryExecutor, realTopSize, conn);
                // 存在计算和旋转的数据不能映射到对象(数据类型不一致,如汇总平均以及数据旋转)
                List pivotCategorySet = ResultUtils.getPivotCategory(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dialect);
                ResultUtils.calculate(realSqlToyConfig, queryResult, pivotCategorySet, sqlToyContext.isDebug());
                // 结果映射成对象
                if (queryExecutor.getResultType() != null) {
                    queryResult.setRows(ResultUtils.wrapQueryResult(queryResult.getRows(), ResultUtils.humpFieldNames(queryExecutor, queryResult.getLabelNames()), (Class) queryExecutor.getResultType()));
                }
                this.setResult(queryResult);
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) List(java.util.List) ArrayList(java.util.ArrayList) BaseException(org.sagacity.sqltoy.exception.BaseException) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler)

Example 18 with QueryResult

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

the class DialectFactory method findPage.

/**
 * @todo 分页查询, pageNo为负一表示取全部记录
 * @param sqlToyContext
 * @param queryExecutor
 * @param pageNo
 * @param pageSize
 * @param dataSource
 * @return
 * @throws Exception
 */
public QueryResult findPage(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final long pageNo, final Integer pageSize, final DataSource dataSource) throws Exception {
    if (queryExecutor.getSql() == null)
        throw new Exception("findPage operate sql is null!");
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "findPage", sqlToyConfig.isShowSql());
        return (QueryResult) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                // 处理sql中的?为统一的:named形式
                SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, true);
                QueryResult queryResult = null;
                Long recordCnt = null;
                // 通过查询条件构造唯一的key
                String pageQueryKey = PageOptimizeUtils.generateOptimizeKey(sqlToyConfig, queryExecutor);
                // 需要进行分页查询优化
                if (null != pageQueryKey) {
                    // 从缓存中提取总记录数
                    recordCnt = PageOptimizeUtils.getPageTotalCount(sqlToyConfig, pageQueryKey);
                    // 缓存中没有则重新查询
                    if (null == recordCnt) {
                        recordCnt = getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
                        // 将总记录数登记到缓存
                        PageOptimizeUtils.registPageTotalCount(sqlToyConfig, pageQueryKey, recordCnt);
                    }
                } else
                    recordCnt = getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
                // pageNo=-1时的提取数据量限制
                int limitSize = sqlToyContext.getPageFetchSizeLimit();
                // pageNo=-1时,总记录数超出限制则返回空集合
                boolean illegal = (pageNo == -1 && (limitSize != -1 && recordCnt > limitSize));
                if (recordCnt == 0 || illegal) {
                    queryResult = new QueryResult();
                    queryResult.setPageNo(pageNo);
                    queryResult.setPageSize(pageSize);
                    queryResult.setRecordCount(new Long(0));
                    if (illegal)
                        logger.warn("非法进行分页查询,提取记录总数为:{},sql={}", recordCnt, sqlToyConfig.getSql());
                } else {
                    // 合法的全记录提取,设置页号为1按记录数
                    if (pageNo == -1) {
                        // 通过参数处理最终的sql和参数值
                        SqlToyResult queryParam = SqlConfigParseUtils.processSql(realSqlToyConfig.getSql(), queryExecutor.getParamsName(realSqlToyConfig), queryExecutor.getParamsValue(realSqlToyConfig));
                        queryResult = getDialectSqlWrapper(dbType).findBySql(sqlToyContext, realSqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), queryExecutor.getRowCallbackHandler(), conn, queryExecutor.getFetchSize(), queryExecutor.getMaxRows());
                        long totalRecord = (queryResult.getRows() == null) ? 0 : queryResult.getRows().size();
                        queryResult.setPageNo(1L);
                        queryResult.setPageSize(new Long(totalRecord).intValue());
                        queryResult.setRecordCount(totalRecord);
                    } else {
                        // 实际开始页(页数据超出总记录,则从第一页重新开始,相反如继续按指定的页查询则记录为空,且实际页号也不存在)
                        long realStartPage = (pageNo * pageSize >= (recordCnt + pageSize)) ? 1 : pageNo;
                        queryResult = getDialectSqlWrapper(dbType).findPageBySql(sqlToyContext, realSqlToyConfig, queryExecutor, realStartPage, pageSize, conn);
                        queryResult.setPageNo(realStartPage);
                        queryResult.setPageSize(pageSize);
                        queryResult.setRecordCount(recordCnt);
                    }
                    // 存在计算和旋转的数据不能映射到对象(数据类型不一致,如汇总平均以及数据旋转)
                    List pivotCategorySet = ResultUtils.getPivotCategory(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dialect);
                    ResultUtils.calculate(realSqlToyConfig, queryResult, pivotCategorySet, sqlToyContext.isDebug());
                    // 结果映射成对象
                    if (queryExecutor.getResultType() != null) {
                        queryResult.setRows(ResultUtils.wrapQueryResult(queryResult.getRows(), ResultUtils.humpFieldNames(queryExecutor, queryResult.getLabelNames()), (Class) queryExecutor.getResultType()));
                    }
                }
                this.setResult(queryResult);
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) BaseException(org.sagacity.sqltoy.exception.BaseException) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult) QueryResult(org.sagacity.sqltoy.model.QueryResult) List(java.util.List) ArrayList(java.util.ArrayList)

Example 19 with QueryResult

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

the class DialectFactory method updateFetchRandom.

@Deprecated
public QueryResult updateFetchRandom(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final Integer random, final UpdateRowHandler updateRowHandler, final DataSource dataSource) throws Exception {
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "updateFetchRandom", sqlToyConfig.isShowSql());
        return (QueryResult) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                // 处理sql中的?为统一的:named形式
                SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, false);
                SqlToyResult queryParam = SqlConfigParseUtils.processSql(realSqlToyConfig.getSql(), queryExecutor.getParamsName(realSqlToyConfig), queryExecutor.getParamsValue(realSqlToyConfig));
                QueryResult queryResult = getDialectSqlWrapper(dbType).updateFetchRandom(sqlToyContext, realSqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), random, updateRowHandler, conn);
                if (queryExecutor.getResultType() != null) {
                    queryResult.setRows(ResultUtils.wrapQueryResult(queryResult.getRows(), ResultUtils.humpFieldNames(queryExecutor, queryResult.getLabelNames()), (Class) queryExecutor.getResultType()));
                }
                this.setResult(queryResult);
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) BaseException(org.sagacity.sqltoy.exception.BaseException) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 20 with QueryResult

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

the class DialectFactory method findByQuery.

/**
 * @todo 查询符合条件的数据集合
 * @param sqlToyContext
 * @param queryExecutor
 * @param dataSource
 * @return
 * @throws Exception
 */
public QueryResult findByQuery(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final DataSource dataSource) throws Exception {
    if (queryExecutor.getSql() == null)
        throw new Exception("findByQuery operate sql is null!");
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "query", sqlToyConfig.isShowSql());
        return (QueryResult) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                // 处理sql中的?为统一的:named形式
                SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, false);
                // 通过参数处理最终的sql和参数值
                SqlToyResult queryParam = SqlConfigParseUtils.processSql(realSqlToyConfig.getSql(), queryExecutor.getParamsName(realSqlToyConfig), queryExecutor.getParamsValue(realSqlToyConfig));
                QueryResult queryResult = getDialectSqlWrapper(dbType).findBySql(sqlToyContext, realSqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), queryExecutor.getRowCallbackHandler(), conn, queryExecutor.getFetchSize(), queryExecutor.getMaxRows());
                // 存在计算和旋转的数据不能映射到对象(数据类型不一致,如汇总平均以及数据旋转)
                List pivotCategorySet = ResultUtils.getPivotCategory(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dialect);
                ResultUtils.calculate(realSqlToyConfig, queryResult, pivotCategorySet, sqlToyContext.isDebug());
                // 结果映射成对象
                if (queryExecutor.getResultType() != null) {
                    queryResult.setRows(ResultUtils.wrapQueryResult(queryResult.getRows(), ResultUtils.humpFieldNames(queryExecutor, queryResult.getLabelNames()), (Class) queryExecutor.getResultType()));
                }
                this.setResult(queryResult);
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) List(java.util.List) ArrayList(java.util.ArrayList) BaseException(org.sagacity.sqltoy.exception.BaseException) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

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