Search in sources :

Example 31 with QueryExecutorExtend

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

the class OracleDialectUtils method findPageBySql.

/**
 * @TODO 分页查询
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param queryExecutor
 * @param pageNo
 * @param pageSize
 * @param conn
 * @param dbType
 * @param dialect
 * @param fetchSize
 * @param maxRows
 * @return
 * @throws Exception
 */
public static QueryResult findPageBySql(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, final DecryptHandler decryptHandler, Long pageNo, Integer pageSize, Connection conn, final Integer dbType, final String dialect, final int fetchSize, final int maxRows) throws Exception {
    StringBuilder sql = new StringBuilder();
    boolean isNamed = sqlToyConfig.isNamedParam();
    // 是否有order by,update 2017-5-22
    boolean hasOrderBy = SqlUtil.hasOrderBy(sqlToyConfig.isHasFast() ? sqlToyConfig.getFastSql(dialect) : sqlToyConfig.getSql(dialect), true);
    if (sqlToyConfig.isHasFast()) {
        sql.append(sqlToyConfig.getFastPreSql(dialect));
        if (!sqlToyConfig.isIgnoreBracket()) {
            sql.append(" (");
        }
    }
    // order by 外包裹一层,确保查询结果是按排序
    if (hasOrderBy) {
        sql.append(" select SAG_Paginationtable.* from (");
    }
    sql.append(sqlToyConfig.isHasFast() ? sqlToyConfig.getFastSql(dialect) : sqlToyConfig.getSql(dialect));
    if (hasOrderBy) {
        sql.append(") SAG_Paginationtable ");
    }
    sql.append(" offset ");
    sql.append(isNamed ? ":" + SqlToyConstants.PAGE_FIRST_PARAM_NAME : "?");
    sql.append(" rows fetch next ");
    sql.append(isNamed ? ":" + SqlToyConstants.PAGE_LAST_PARAM_NAME : "?");
    sql.append(" rows only ");
    if (sqlToyConfig.isHasFast()) {
        if (!sqlToyConfig.isIgnoreBracket()) {
            sql.append(") ");
        }
        sql.append(sqlToyConfig.getFastTailSql(dialect));
    }
    SqlToyResult queryParam = DialectUtils.wrapPageSqlParams(sqlToyContext, sqlToyConfig, queryExecutor, sql.toString(), (pageNo - 1) * pageSize, Long.valueOf(pageSize), dialect);
    QueryExecutorExtend extend = queryExecutor.getInnerModel();
    return DialectUtils.findBySql(sqlToyContext, sqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), extend.rowCallbackHandler, decryptHandler, conn, dbType, 0, fetchSize, maxRows);
}
Also used : QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 32 with QueryExecutorExtend

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

the class ElasticSqlPlugin method findTop.

/**
 * @todo 提取符合条件的前多少条记录
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param queryExecutor
 * @param topSize
 * @return
 * @throws Exception
 */
public static List<?> findTop(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Integer topSize) throws Exception {
    QueryExecutorExtend extend = queryExecutor.getInnerModel();
    String realSql = MongoElasticUtils.wrapES(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig)).trim();
    // sql模式
    if (topSize != null) {
        realSql = realSql + " limit " + topSize;
    }
    if (sqlToyContext.isDebug()) {
        if (logger.isDebugEnabled()) {
            logger.debug("findTopByElastic sql=" + realSql);
        } else {
            System.out.println("findTopByElastic sql=" + realSql);
        }
    }
    DataSetResult result = ElasticSearchUtils.executeQuery(sqlToyContext, sqlToyConfig, realSql, (Class) extend.resultType, extend.humpMapLabel);
    return result.getRows();
}
Also used : DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend)

Example 33 with QueryExecutorExtend

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

the class Mongo method findTop.

/**
 * @todo 查询前多少条记录
 * @param topSize
 * @return
 */
public List<?> findTop(final Float topSize) {
    QueryExecutor queryExecutor = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql, SqlType.search, "");
    NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
    if (noSqlModel == null || noSqlModel.getCollection() == null || noSqlModel.getFields() == null) {
        throw new IllegalArgumentException(ERROR_MESSAGE);
    }
    try {
        QueryExecutorExtend extend = queryExecutor.getInnerModel();
        // 最后的执行语句
        String realMql = MongoElasticUtils.wrapMql(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig));
        return findTop(getMongoTemplate(), sqlToyConfig, topSize, realMql, (Class) extend.resultType, extend.humpMapLabel);
    } catch (Exception e) {
        e.printStackTrace();
        throw new DataAccessException(e);
    }
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Example 34 with QueryExecutorExtend

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

the class Mongo method findPage.

/**
 * @todo 分页查询
 * @param page
 * @return
 */
public Page findPage(Page page) {
    QueryExecutor queryExecutor = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql, SqlType.search, "");
    NoSqlConfigModel noSqlModel = sqlToyConfig.getNoSqlConfigModel();
    if (noSqlModel == null || noSqlModel.getCollection() == null || noSqlModel.getFields() == null) {
        throw new IllegalArgumentException(ERROR_MESSAGE);
    }
    try {
        QueryExecutorExtend extend = queryExecutor.getInnerModel();
        // 最后的执行语句
        String realMql = MongoElasticUtils.wrapMql(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig));
        return findPage(getMongoTemplate(), sqlToyConfig, page, realMql, (Class) extend.resultType, extend.humpMapLabel);
    } catch (Exception e) {
        e.printStackTrace();
        throw new DataAccessException(e);
    }
}
Also used : NoSqlConfigModel(org.sagacity.sqltoy.config.model.NoSqlConfigModel) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Example 35 with QueryExecutorExtend

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

the class ElasticSearchPlugin method findPage.

/**
 * @todo 基于es的分页查询
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param pageModel
 * @param queryExecutor
 * @return
 * @throws Exception
 */
public static Page findPage(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, Page pageModel, QueryExecutor queryExecutor) throws Exception {
    String realMql = "";
    JSONObject jsonQuery = null;
    QueryExecutorExtend extend = queryExecutor.getInnerModel();
    try {
        realMql = MongoElasticUtils.wrapES(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig)).trim();
        jsonQuery = JSON.parseObject(realMql);
        jsonQuery.fluentRemove("from");
        jsonQuery.fluentRemove("FROM");
        jsonQuery.fluentRemove("size");
        jsonQuery.fluentRemove("SIZE");
        jsonQuery.fluentPut("from", (pageModel.getPageNo() - 1) * pageModel.getPageSize());
        jsonQuery.fluentPut("size", pageModel.getPageSize());
    } catch (Exception e) {
        logger.error("分页解析es原生json错误,请检查json串格式是否正确!错误信息:{},json={}", e.getMessage(), realMql);
        throw e;
    }
    Page page = new Page();
    page.setPageNo(pageModel.getPageNo());
    page.setPageSize(pageModel.getPageSize());
    DataSetResult result = executeQuery(sqlToyContext, sqlToyConfig, jsonQuery, (Class) extend.resultType, extend.humpMapLabel);
    page.setRows(result.getRows());
    page.setRecordCount(result.getRecordCount());
    return page;
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) Page(org.sagacity.sqltoy.model.Page) DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend)

Aggregations

QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)36 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)19 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)15 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)11 Connection (java.sql.Connection)8 List (java.util.List)8 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)8 ArrayList (java.util.ArrayList)7 QueryResult (org.sagacity.sqltoy.model.QueryResult)7 DataSetResult (org.sagacity.sqltoy.model.inner.DataSetResult)4 NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)3 QueryExecutor (org.sagacity.sqltoy.model.QueryExecutor)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ShardingStrategyConfig (org.sagacity.sqltoy.config.model.ShardingStrategyConfig)2 SqlWithAnalysis (org.sagacity.sqltoy.config.model.SqlWithAnalysis)2 Page (org.sagacity.sqltoy.model.Page)2 DataSource (javax.sql.DataSource)1 PageOptimize (org.sagacity.sqltoy.config.model.PageOptimize)1 PivotModel (org.sagacity.sqltoy.config.model.PivotModel)1 SqlParamsModel (org.sagacity.sqltoy.config.model.SqlParamsModel)1