use of org.sagacity.sqltoy.config.model.SqlToyResult in project sagacity-sqltoy by chenrenfei.
the class PostgreSqlDialectUtils method findPageBySql.
/**
* @todo 分頁查詢
* @param sqlToyContext
* @param sqlToyConfig
* @param queryExecutor
* @param pageNo
* @param pageSize
* @param conn
* @return
* @throws Exception
*/
public static QueryResult findPageBySql(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Long pageNo, Integer pageSize, Connection conn) throws Exception {
StringBuilder sql = new StringBuilder();
boolean isNamed = sqlToyConfig.isNamedParam();
if (sqlToyConfig.isHasFast()) {
sql.append(sqlToyConfig.getFastPreSql());
sql.append(" (").append(sqlToyConfig.getFastSql());
} else
sql.append(sqlToyConfig.getSql());
sql.append(" limit ");
sql.append(isNamed ? ":" + SqlToyConstants.PAGE_FIRST_PARAM_NAME : "?");
sql.append(" offset ");
sql.append(isNamed ? ":" + SqlToyConstants.PAGE_LAST_PARAM_NAME : "?");
if (sqlToyConfig.isHasFast()) {
sql.append(") ").append(sqlToyConfig.getFastTailSql());
}
SqlToyResult queryParam = DialectUtils.wrapPageSqlParams(sqlToyContext, sqlToyConfig, queryExecutor, sql.toString(), new Long(pageSize), (pageNo - 1) * pageSize);
return DialectUtils.findBySql(sqlToyContext, sqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), queryExecutor.getRowCallbackHandler(), conn, 0, queryExecutor.getFetchSize(), queryExecutor.getMaxRows());
}
use of org.sagacity.sqltoy.config.model.SqlToyResult in project sagacity-sqltoy by chenrenfei.
the class PostgreSqlDialectUtils method getRandomResult.
/**
* @todo 提供随机记录查询
* @param sqlToyContext
* @param sqlToyConfig
* @param queryExecutor
* @param totalCount
* @param randomCount
* @param conn
* @return
* @throws Exception
*/
public static QueryResult getRandomResult(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Long totalCount, Long randomCount, Connection conn) throws Exception {
String innerSql = sqlToyConfig.isHasFast() ? sqlToyConfig.getFastSql() : sqlToyConfig.getSql();
// sql中是否存在排序或union
boolean hasOrderOrUnion = DialectUtils.hasOrderByOrUnion(innerSql);
StringBuilder sql = new StringBuilder();
if (sqlToyConfig.isHasFast())
sql.append(sqlToyConfig.getFastPreSql()).append(" (");
// 存在order 或union 则在sql外包裹一层
if (hasOrderOrUnion)
sql.append("select sag_random_table.* from (");
sql.append(innerSql);
if (hasOrderOrUnion)
sql.append(") sag_random_table ");
sql.append(" order by random() limit ");
sql.append(randomCount);
if (sqlToyConfig.isHasFast())
sql.append(") ").append(sqlToyConfig.getFastTailSql());
SqlToyResult queryParam = DialectUtils.wrapPageSqlParams(sqlToyContext, sqlToyConfig, queryExecutor, sql.toString(), null, null);
return DialectUtils.findBySql(sqlToyContext, sqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), queryExecutor.getRowCallbackHandler(), conn, 0, queryExecutor.getFetchSize(), queryExecutor.getMaxRows());
}
use of org.sagacity.sqltoy.config.model.SqlToyResult in project sagacity-sqltoy by chenrenfei.
the class SqlServerDialectUtils method getRandomResult.
/**
* @todo 取随机记录
* @param sqlToyContext
* @param sqlToyConfig
* @param queryExecutor
* @param totalCount
* @param randomCount
* @param conn
* @return
* @throws Exception
*/
public static QueryResult getRandomResult(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Long totalCount, Long randomCount, Connection conn) throws Exception {
// sqlserver 不支持内部order by
String innerSql = sqlToyConfig.isHasFast() ? sqlToyConfig.getFastSql() : sqlToyConfig.getSql();
// sql中是否存在排序或union
boolean hasOrderOrUnion = DialectUtils.hasOrderByOrUnion(innerSql);
StringBuilder sql = new StringBuilder();
if (sqlToyConfig.isHasFast())
sql.append(sqlToyConfig.getFastPreSql()).append(" (");
// 存在order 或union 则在sql外包裹一层
if (hasOrderOrUnion) {
sql.append("select top " + randomCount);
sql.append(" sag_random_table.* from (");
sql.append(innerSql);
} else
sql.append(innerSql.replaceFirst("(?i)select ", "select top " + randomCount + " "));
if (hasOrderOrUnion)
sql.append(") sag_random_table ");
sql.append(" order by NEWID() ");
if (sqlToyConfig.isHasFast())
sql.append(") ").append(sqlToyConfig.getFastTailSql());
SqlToyResult queryParam = SqlConfigParseUtils.processSql(sql.toString(), queryExecutor.getParamsName(sqlToyConfig), queryExecutor.getParamsValue(sqlToyConfig));
return DialectUtils.findBySql(sqlToyContext, sqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), queryExecutor.getRowCallbackHandler(), conn, 0, queryExecutor.getFetchSize(), queryExecutor.getMaxRows());
}
use of org.sagacity.sqltoy.config.model.SqlToyResult in project sagacity-sqltoy by chenrenfei.
the class DB2Dialect method findTopBySql.
/*
* (non-Javadoc)
*
* @see org.sagacity.sqltoy.dialect.Dialect#findTopBySql(org.sagacity.sqltoy.
* SqlToyContext, org.sagacity.sqltoy.config.model.SqlToyConfig,
* org.sagacity.sqltoy.executor.QueryExecutor, double, java.sql.Connection)
*/
@Override
public QueryResult findTopBySql(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, double topSize, Connection conn) throws Exception {
StringBuilder sql = new StringBuilder();
if (sqlToyConfig.isHasFast()) {
sql.append(sqlToyConfig.getFastPreSql());
sql.append(" (").append(sqlToyConfig.getFastSql());
} else
sql.append(sqlToyConfig.getSql());
sql.append(" limit ");
sql.append(new Double(topSize).intValue());
if (sqlToyConfig.isHasFast()) {
sql.append(") ").append(sqlToyConfig.getFastTailSql());
}
SqlToyResult queryParam = DialectUtils.wrapPageSqlParams(sqlToyContext, sqlToyConfig, queryExecutor, sql.toString(), null, null);
return findBySql(sqlToyContext, sqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), queryExecutor.getRowCallbackHandler(), conn, queryExecutor.getFetchSize(), queryExecutor.getMaxRows());
}
use of org.sagacity.sqltoy.config.model.SqlToyResult in project sagacity-sqltoy by chenrenfei.
the class DB2Dialect method findPageBySql.
/*
* (non-Javadoc)
*
* @see org.sagacity.sqltoy.dialect.DialectSqlWrapper#findPageBySql(org.sagacity
* .sqltoy.SqlToyContext, org.sagacity.sqltoy.config.model.SqlToyConfig,
* org.sagacity.sqltoy.executor.QueryExecutor,
* org.sagacity.core.database.callback.RowCallbackHandler, java.lang.Long,
* java.lang.Integer, java.sql.Connection)
*/
@Override
public QueryResult findPageBySql(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Long pageNo, Integer pageSize, Connection conn) throws Exception {
StringBuilder sql = new StringBuilder();
boolean isNamed = sqlToyConfig.isNamedParam();
if (sqlToyConfig.isHasFast()) {
sql.append(sqlToyConfig.getFastPreSql());
sql.append(" (").append(sqlToyConfig.getFastSql());
} else
sql.append(sqlToyConfig.getSql());
sql.append(" limit ");
sql.append(isNamed ? ":" + SqlToyConstants.PAGE_FIRST_PARAM_NAME : "?");
sql.append(" offset ");
sql.append(isNamed ? ":" + SqlToyConstants.PAGE_LAST_PARAM_NAME : "?");
if (sqlToyConfig.isHasFast()) {
sql.append(") ").append(sqlToyConfig.getFastTailSql());
}
SqlToyResult queryParam = DialectUtils.wrapPageSqlParams(sqlToyContext, sqlToyConfig, queryExecutor, sql.toString(), new Long(pageSize), (pageNo - 1) * pageSize);
return findBySql(sqlToyContext, sqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), queryExecutor.getRowCallbackHandler(), conn, queryExecutor.getFetchSize(), queryExecutor.getMaxRows());
}
Aggregations