Search in sources :

Example 11 with QueryExecutorExtend

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

the class ShardingUtils method getShardingDataSource.

/**
 * @todo 根据条件决定获得对应的数据库
 * @param sqlToyContext
 * @param sqlToyConfig
 * @param queryExecutor
 * @param dataSource
 * @return
 * @throws Exception
 */
public static DataSource getShardingDataSource(SqlToyContext sqlToyContext, SqlToyConfig sqlToyConfig, QueryExecutor queryExecutor, DataSource dataSource) throws Exception {
    // 获取sharding DataSource
    // 优先以直接指定的dataSource为基准
    DataSource shardingDataSource = dataSource;
    QueryExecutorExtend extend = queryExecutor.getInnerModel();
    ShardingStrategyConfig shardingConfig = null;
    if (null != sqlToyConfig.getDataSourceSharding()) {
        shardingConfig = sqlToyConfig.getDataSourceSharding();
    }
    if (null != extend.dbSharding) {
        shardingConfig = extend.dbSharding;
    }
    // 如果没有sharding策略,则返回dataSource,否则以sharding的结果dataSource为基准
    if (null == shardingConfig) {
        return shardingDataSource;
    }
    String[] paramNames = extend.getDataSourceShardingParamsName(sqlToyConfig);
    Object[] paramValues = extend.getDataSourceShardingParamsValue(sqlToyConfig);
    IgnoreCaseLinkedMap<String, Object> valueMap = hashParams(paramNames, paramValues);
    DataSource result = getShardingDataSource(sqlToyContext, sqlToyConfig, shardingConfig, valueMap);
    if (result != null) {
        return result;
    }
    return shardingDataSource;
}
Also used : ShardingStrategyConfig(org.sagacity.sqltoy.config.model.ShardingStrategyConfig) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) DataSource(javax.sql.DataSource)

Example 12 with QueryExecutorExtend

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

the class SqlToyDaoSupport method loadByQuery.

/**
 * TODO 通过构造QueyExecutor 提供更加灵活的参数传递方式,包括DataSource 比如:
 * <li>1、new QueryExecutor(sql,entity).dataSource(dataSource)</li>
 * <li>2、new
 * QueryExecutor(sql).names(paramNames).values(paramValues).resultType(resultType);
 * </li>
 *
 * @param queryExecutor
 * @return
 */
protected Object loadByQuery(final QueryExecutor queryExecutor) {
    QueryExecutorExtend extend = queryExecutor.getInnerModel();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor, SqlType.search, getDialect(extend.dataSource));
    QueryResult result = dialectFactory.findByQuery(sqlToyContext, queryExecutor, sqlToyConfig, null, this.getDataSource(extend.dataSource, sqlToyConfig));
    List rows = result.getRows();
    if (rows == null || rows.isEmpty()) {
        return null;
    }
    if (rows.size() == 1) {
        return rows.get(0);
    }
    throw new IllegalArgumentException("loadByQuery查询出:" + rows.size() + " 条记录,不符合load查询预期!");
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) ParallQueryResult(org.sagacity.sqltoy.model.ParallQueryResult) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) List(java.util.List) ArrayList(java.util.ArrayList) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend)

Example 13 with QueryExecutorExtend

use of org.sagacity.sqltoy.model.inner.QueryExecutorExtend 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 14 with QueryExecutorExtend

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

the class ElasticSearchPlugin 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 {
    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);
        if (topSize != null) {
            jsonQuery.fluentRemove("from");
            jsonQuery.fluentRemove("FROM");
            jsonQuery.fluentRemove("size");
            jsonQuery.fluentRemove("SIZE");
            jsonQuery.fluentPut("from", 0);
            jsonQuery.fluentPut("size", topSize);
        }
    } catch (Exception e) {
        logger.error("解析es原生json错误,请检查json串格式是否正确!错误信息:{},json={}", e.getMessage(), realMql);
        throw e;
    }
    DataSetResult result = executeQuery(sqlToyContext, sqlToyConfig, jsonQuery, (Class) extend.resultType, extend.humpMapLabel);
    return result.getRows();
}
Also used : JSONObject(com.alibaba.fastjson.JSONObject) DataSetResult(org.sagacity.sqltoy.model.inner.DataSetResult) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend)

Example 15 with QueryExecutorExtend

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

the class DialectFactory method findByQuery.

/**
 * @todo 查询符合条件的数据集合
 * @param sqlToyContext
 * @param queryExecutor
 * @param sqlToyConfig
 * @param lockMode
 * @param dataSource
 * @return
 */
public QueryResult findByQuery(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final SqlToyConfig sqlToyConfig, final LockMode lockMode, final DataSource dataSource) {
    final QueryExecutorExtend extend = queryExecutor.getInnerModel();
    // 合法校验
    if (StringUtil.isBlank(extend.sql)) {
        throw new IllegalArgumentException("findByQuery operate sql is null!");
    }
    try {
        Long startTime = System.currentTimeMillis();
        // 规整查询参数名称和参数名称对应的值
        QueryExecutorBuilder.initQueryExecutor(sqlToyContext, extend, sqlToyConfig, false);
        SqlExecuteStat.start(sqlToyConfig.getId(), "findByQuery", sqlToyConfig.isShowSql());
        QueryResult result = (QueryResult) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {

            @Override
            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                // 处理sql中的?为统一的:named形式,并进行sharding table替换
                SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, false);
                // 通过参数处理最终的sql和参数值
                SqlToyResult queryParam = SqlConfigParseUtils.processSql(realSqlToyConfig.getSql(dialect), extend.getParamsName(realSqlToyConfig), extend.getParamsValue(sqlToyContext, realSqlToyConfig), dialect);
                QueryResult queryResult = getDialectSqlWrapper(dbType).findBySql(sqlToyContext, realSqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), extend.rowCallbackHandler, wrapDecryptHandler(sqlToyContext, extend.resultType), conn, lockMode, dbType, dialect, getFetchSize(extend.fetchSize), extend.maxRows);
                if (queryResult.getRows() != null && !queryResult.getRows().isEmpty()) {
                    // 存在计算和旋转的数据不能映射到对象(数据类型不一致,如汇总平均以及数据旋转)
                    List pivotCategorySet = ResultUtils.getPivotCategory(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
                    // 对查询结果进行计算处理:字段脱敏、格式化、数据旋转、同步环比、分组汇总等
                    boolean changedCols = ResultUtils.calculate(sqlToyContext.getDesensitizeProvider(), realSqlToyConfig, queryResult, pivotCategorySet, extend);
                    // 将结果映射对象单独出来为了解耦,性能影响其实可以忽略,上万条也是1毫秒级
                    if (extend.resultType != null) {
                        queryResult.setRows(ResultUtils.wrapQueryResult(sqlToyContext, queryResult.getRows(), queryResult.getLabelNames(), (Class) extend.resultType, changedCols, extend.humpMapLabel, extend.hiberarchy, extend.hiberarchyClasses, extend.fieldsMap));
                    }
                }
                SqlExecuteStat.debug("查询结果", "共查询出记录数={}条!", queryResult.getRecordCount());
                this.setResult(queryResult);
            }
        });
        result.setExecuteTime(System.currentTimeMillis() - startTime);
        return result;
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw new DataAccessException(e);
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult) QueryResult(org.sagacity.sqltoy.model.QueryResult) List(java.util.List) ArrayList(java.util.ArrayList) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException)

Aggregations

QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)36 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)19 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)15 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)11 Connection (java.sql.Connection)8 List (java.util.List)8 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)8 ArrayList (java.util.ArrayList)7 QueryResult (org.sagacity.sqltoy.model.QueryResult)7 DataSetResult (org.sagacity.sqltoy.model.inner.DataSetResult)4 NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)3 QueryExecutor (org.sagacity.sqltoy.model.QueryExecutor)3 JSONObject (com.alibaba.fastjson.JSONObject)2 ShardingStrategyConfig (org.sagacity.sqltoy.config.model.ShardingStrategyConfig)2 SqlWithAnalysis (org.sagacity.sqltoy.config.model.SqlWithAnalysis)2 Page (org.sagacity.sqltoy.model.Page)2 DataSource (javax.sql.DataSource)1 PageOptimize (org.sagacity.sqltoy.config.model.PageOptimize)1 PivotModel (org.sagacity.sqltoy.config.model.PivotModel)1 SqlParamsModel (org.sagacity.sqltoy.config.model.SqlParamsModel)1