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);
}
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();
}
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);
}
}
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);
}
}
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;
}
Aggregations