Search in sources :

Example 11 with SqlToyResult

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());
}
Also used : SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 12 with SqlToyResult

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;
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) Serializable(java.io.Serializable) List(java.util.List) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) IOException(java.io.IOException) OneToManyModel(org.sagacity.sqltoy.config.model.OneToManyModel) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 13 with SqlToyResult

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());
}
Also used : SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 14 with SqlToyResult

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;
}
Also used : PivotModel(org.sagacity.sqltoy.config.model.PivotModel) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) ArrayList(java.util.ArrayList) List(java.util.List) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 15 with SqlToyResult

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;
}
Also used : PivotModel(org.sagacity.sqltoy.config.model.PivotModel) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) ArrayList(java.util.ArrayList) ReverseList(org.sagacity.sqltoy.plugins.calculator.ReverseList) List(java.util.List) ArrayList(java.util.ArrayList) UnpivotList(org.sagacity.sqltoy.plugins.calculator.UnpivotList) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Aggregations

SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)74 QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)19 List (java.util.List)16 ArrayList (java.util.ArrayList)15 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)12 Connection (java.sql.Connection)11 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)11 QueryResult (org.sagacity.sqltoy.model.QueryResult)11 Test (org.junit.jupiter.api.Test)9 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)8 BaseException (org.sagacity.sqltoy.exception.BaseException)6 SqlWithAnalysis (org.sagacity.sqltoy.config.model.SqlWithAnalysis)5 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)5 Type (java.lang.reflect.Type)4 HashMap (java.util.HashMap)4 OneToManyModel (org.sagacity.sqltoy.config.model.OneToManyModel)4 TableCascadeModel (org.sagacity.sqltoy.config.model.TableCascadeModel)4 DBType (org.sagacity.sqltoy.utils.DataSourceUtils.DBType)3 IOException (java.io.IOException)2 Serializable (java.io.Serializable)2