use of org.sagacity.sqltoy.config.model.SqlToyResult in project sagacity-sqltoy by chenrenfei.
the class SybaseIQDialect method findPageBySql.
/**
* @todo sybase iq15.4之后通过set option public.reserved_keywords='limit'支持limit分页
* @param sqlToyContext
* @param sqlToyConfig
* @param queryExecutor
* @param pageNo
* @param pageSize
* @param conn
* @return
* @throws Exception
*/
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());
}
use of org.sagacity.sqltoy.config.model.SqlToyResult in project sagacity-sqltoy by chenrenfei.
the class DialectUtils method load.
/**
* @todo 加载获取单笔数据库记录
* @param sqlToyContext
* @param sqlToyConfig
* @param sql
* @param entityMeta
* @param entity
* @param cascadeTypes
* @param conn
* @return
* @throws Exception
*/
public static Serializable load(final SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, String sql, EntityMeta entityMeta, Serializable entity, List<Class> cascadeTypes, Connection conn) throws Exception {
Object[] pkValues = BeanUtil.reflectBeanToAry(entity, entityMeta.getIdArray(), null, null);
for (int i = 0; i < pkValues.length; i++) if (null == pkValues[i])
throw new Exception("load method must assign value for pk,null pk field is:" + entityMeta.getIdArray()[i]);
SqlToyResult sqlToyResult = SqlConfigParseUtils.processSql(sql, entityMeta.getIdArray(), pkValues);
// 显示sql
SqlExecuteStat.showSql(sqlToyResult.getSql(), sqlToyResult.getParamsValue());
QueryResult queryResult = findBySql(sqlToyContext, sqlToyConfig, sqlToyResult.getSql(), sqlToyResult.getParamsValue(), null, conn, 0, -1, -1);
List rows = queryResult.getRows();
Serializable result = null;
if (rows != null && rows.size() > 0) {
rows = BeanUtil.reflectListToBean(rows, ResultUtils.humpFieldNames(queryResult.getLabelNames()), entity.getClass());
result = (Serializable) rows.get(0);
}
if (result == null)
return null;
// 存在主表对应子表
if (null != cascadeTypes && !cascadeTypes.isEmpty() && !entityMeta.getOneToManys().isEmpty()) {
List pkRefDetails;
for (OneToManyModel oneToMany : entityMeta.getOneToManys()) {
// 判定是否要加载
if (cascadeTypes.contains(oneToMany.getMappedType())) {
sqlToyResult = SqlConfigParseUtils.processSql(oneToMany.getLoadSubTableSql(), oneToMany.getMappedFields(), pkValues);
if (sqlToyContext.isDebug())
out.println("auto load sub table dataSet sql:".concat(sqlToyResult.getSql()));
pkRefDetails = SqlUtil.findByJdbcQuery(sqlToyResult.getSql(), sqlToyResult.getParamsValue(), oneToMany.getMappedType(), null, conn);
if (null != pkRefDetails)
BeanUtils.setProperty(result, oneToMany.getProperty(), pkRefDetails);
}
}
}
return result;
}
use of org.sagacity.sqltoy.config.model.SqlToyResult in project sagacity-sqltoy by chenrenfei.
the class PostgreSqlDialectUtils method findTopBySql.
/**
* @todo 实现top记录查询
* @param sqlToyContext
* @param sqlToyConfig
* @param queryExecutor
* @param topSize
* @param conn
* @return
* @throws Exception
*/
public static 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 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 ResultUtils method getPivotCategory.
/**
* @todo 提取数据旋转对应的sql查询结果
* @param sqlToyContext
* @param sqlToyConfig
* @param queryExecutor
* @param conn
* @param dialect
* @return
* @throws Exception
*/
public static List getPivotCategory(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Connection conn, String dialect) throws Exception {
if (sqlToyConfig.getResultProcessor() != null && !sqlToyConfig.getResultProcessor().isEmpty()) {
List resultProcessors = sqlToyConfig.getResultProcessor();
Object processor;
for (int i = 0; i < resultProcessors.size(); i++) {
processor = resultProcessors.get(i);
// 数据旋转只能存在一个
if (processor instanceof PivotModel) {
PivotModel pivotModel = (PivotModel) processor;
if (pivotModel.getCategorySql() != null) {
SqlToyConfig pivotSqlConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyContext.getSqlToyConfig(pivotModel.getCategorySql(), SqlType.search), queryExecutor, dialect, false);
SqlToyResult pivotSqlToyResult = SqlConfigParseUtils.processSql(pivotSqlConfig.getSql(), queryExecutor.getParamsName(pivotSqlConfig), queryExecutor.getParamsValue(pivotSqlConfig));
List pivotCategory = SqlUtil.findByJdbcQuery(pivotSqlToyResult.getSql(), pivotSqlToyResult.getParamsValue(), null, null, conn);
// 行转列返回
return CollectionUtil.convertColToRow(pivotCategory, null);
}
}
}
}
return null;
}
use of org.sagacity.sqltoy.config.model.SqlToyResult in project sagacity-sqltoy by chenrenfei.
the class ResultUtils method getPivotCategory.
/**
* @todo 提取数据旋转对应的sql查询结果
* @param sqlToyContext
* @param sqlToyConfig
* @param queryExecutor
* @param conn
* @param dbType
* @param dialect
* @return
* @throws Exception
*/
public static List getPivotCategory(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, Connection conn, final Integer dbType, String dialect) throws Exception {
List resultProcessors = new ArrayList();
QueryExecutorExtend extend = queryExecutor.getInnerModel();
if (!sqlToyConfig.getResultProcessor().isEmpty()) {
resultProcessors.addAll(sqlToyConfig.getResultProcessor());
}
// QueryExecutor中扩展的计算
if (extend != null && !extend.calculators.isEmpty()) {
resultProcessors.addAll(extend.calculators);
}
Object processor;
for (int i = 0; i < resultProcessors.size(); i++) {
processor = resultProcessors.get(i);
// 数据旋转只能存在一个
if (processor instanceof PivotModel) {
PivotModel pivotModel = (PivotModel) processor;
if (pivotModel.getCategorySql() != null) {
SqlToyConfig pivotSqlConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyContext.getSqlToyConfig(pivotModel.getCategorySql(), SqlType.search, ""), queryExecutor, dialect, false);
SqlToyResult pivotSqlToyResult = SqlConfigParseUtils.processSql(pivotSqlConfig.getSql(dialect), extend.getParamsName(pivotSqlConfig), extend.getParamsValue(sqlToyContext, pivotSqlConfig), dialect);
List pivotCategory = SqlUtil.findByJdbcQuery(sqlToyContext.getTypeHandler(), pivotSqlToyResult.getSql(), pivotSqlToyResult.getParamsValue(), null, null, null, conn, dbType, sqlToyConfig.isIgnoreEmpty(), null, SqlToyConstants.FETCH_SIZE, -1);
// 行转列返回
return CollectionUtil.convertColToRow(pivotCategory, null);
}
}
}
return null;
}
Aggregations