Search in sources :

Example 1 with Page

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

the class Elastic method findPage.

/**
 * @todo 分页查询
 * @param pageModel
 * @return
 */
public Page findPage(Page pageModel) {
    QueryExecutor queryExecutor = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql, SqlType.search, "");
    NoSqlConfigModel noSqlConfig = sqlToyConfig.getNoSqlConfigModel();
    if (noSqlConfig == null) {
        throw new IllegalArgumentException(ERROR_MESSAGE);
    }
    Page pageResult = null;
    try {
        if (noSqlConfig.isSqlMode()) {
            ElasticEndpoint esConfig = sqlToyContext.getElasticEndpoint(noSqlConfig.getEndpoint());
            if (esConfig.isNativeSql()) {
                throw new UnsupportedOperationException("elastic native sql pagination is not support!");
            }
            pageResult = ElasticSqlPlugin.findPage(sqlToyContext, sqlToyConfig, pageModel, queryExecutor);
        } else {
            pageResult = ElasticSearchPlugin.findPage(sqlToyContext, sqlToyConfig, pageModel, queryExecutor);
        }
        if (pageResult.getRecordCount() == 0 && sqlToyContext.isPageOverToFirst()) {
            pageResult.setPageNo(1L);
        }
        return pageResult;
    } 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) Page(org.sagacity.sqltoy.model.Page) ElasticEndpoint(org.sagacity.sqltoy.config.model.ElasticEndpoint) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Example 2 with Page

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

the class Mongo method findPage.

/**
 * @todo 分页查询
 * @param mongoTemplate
 * @param sqlToyConfig
 * @param pageModel
 * @param mql
 * @param resultClass
 * @param humpMapLabel
 * @return
 * @throws Exception
 */
private Page findPage(MongoTemplate mongoTemplate, SqlToyConfig sqlToyConfig, Page pageModel, String mql, Class resultClass, boolean humpMapLabel) throws Exception {
    Page result = new Page();
    result.setPageNo(pageModel.getPageNo());
    result.setPageSize(pageModel.getPageSize());
    BasicQuery query = new BasicQuery(mql);
    result.setRecordCount(mongoTemplate.count(query, sqlToyConfig.getNoSqlConfigModel().getCollection()));
    if (result.getRecordCount() == 0) {
        if (sqlToyContext.isPageOverToFirst()) {
            result.setPageNo(1L);
        }
        return result;
    }
    // 设置分页
    if (result.getPageNo() == -1) {
        query.skip(0).limit(Long.valueOf(result.getRecordCount()).intValue());
    } else {
        boolean isOverPage = (pageModel.getPageNo() * pageModel.getPageSize() >= (result.getRecordCount() + pageModel.getPageSize()));
        if (isOverPage && !sqlToyContext.isPageOverToFirst()) {
            return result;
        }
        long realStartPage = isOverPage ? 1 : pageModel.getPageNo();
        result.setPageNo(realStartPage);
        query.skip((realStartPage - 1) * pageModel.getPageSize()).limit(pageModel.getPageSize());
    }
    if (sqlToyContext.isDebug()) {
        if (logger.isDebugEnabled()) {
            logger.debug("findPageByMongo script=" + query.getQueryObject());
        } else {
            System.out.println("findPageByMongo script=" + query.getQueryObject());
        }
    }
    List<Document> rs = mongoTemplate.find(query, Document.class, sqlToyConfig.getNoSqlConfigModel().getCollection());
    if (rs == null || rs.isEmpty()) {
        return result;
    }
    result.setRows(extractFieldValues(sqlToyConfig, rs.iterator(), resultClass, humpMapLabel));
    return result;
}
Also used : BasicQuery(org.springframework.data.mongodb.core.query.BasicQuery) Page(org.sagacity.sqltoy.model.Page) Document(org.bson.Document)

Example 3 with Page

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

the class ElasticSqlPlugin 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 {
    QueryExecutorExtend extend = queryExecutor.getInnerModel();
    String realSql = MongoElasticUtils.wrapES(sqlToyConfig, extend.getParamsName(sqlToyConfig), extend.getParamsValue(sqlToyContext, sqlToyConfig)).trim();
    // sql模式
    realSql = realSql + " limit " + (pageModel.getPageNo() - 1) * pageModel.getPageSize() + "," + pageModel.getPageSize();
    if (sqlToyContext.isDebug()) {
        if (logger.isDebugEnabled()) {
            logger.debug("findPageByElastic sql=" + realSql);
        } else {
            System.out.println("findPageByElastic sql=" + realSql);
        }
    }
    Page page = new Page();
    page.setPageNo(pageModel.getPageNo());
    page.setPageSize(pageModel.getPageSize());
    DataSetResult result = ElasticSearchUtils.executeQuery(sqlToyContext, sqlToyConfig, realSql, (Class) extend.resultType, extend.humpMapLabel);
    page.setRows(result.getRows());
    page.setRecordCount(result.getRecordCount());
    return page;
}
Also used : Page(org.sagacity.sqltoy.model.Page) DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend)

Example 4 with Page

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

the class Query method findPage.

/**
 * @TODO 进行分页查询
 * @param page
 * @return
 */
public Page<?> findPage(final Page page) {
    QueryExecutor queryExecute = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecute, SqlType.search, getDialect());
    if (page.getSkipQueryCount()) {
        return (Page<?>) dialectFactory.findSkipTotalCountPage(sqlToyContext, queryExecute, sqlToyConfig, page.getPageNo(), page.getPageSize(), getDataSource(sqlToyConfig)).getPageResult();
    }
    return (Page<?>) dialectFactory.findPage(sqlToyContext, queryExecute, sqlToyConfig, page.getPageNo(), page.getPageSize(), getDataSource(sqlToyConfig)).getPageResult();
}
Also used : QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Page(org.sagacity.sqltoy.model.Page)

Example 5 with Page

use of org.sagacity.sqltoy.model.Page 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

Page (org.sagacity.sqltoy.model.Page)6 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)2 QueryExecutor (org.sagacity.sqltoy.model.QueryExecutor)2 DataSetResult (org.sagacity.sqltoy.model.inner.DataSetResult)2 QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)2 JSONObject (com.alibaba.fastjson.JSONObject)1 Document (org.bson.Document)1 ElasticEndpoint (org.sagacity.sqltoy.config.model.ElasticEndpoint)1 NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)1 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)1 BasicQuery (org.springframework.data.mongodb.core.query.BasicQuery)1