Search in sources :

Example 6 with QueryExecutor

use of org.sagacity.sqltoy.executor.QueryExecutor in project sagacity-sqltoy by chenrenfei.

the class Elastic method findPage.

/**
 * @todo 分页查询
 * @param pageModel
 * @return
 * @throws Exception
 */
public PaginationModel findPage(PaginationModel pageModel) throws Exception {
    QueryExecutor queryExecutor = build();
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sql);
    if (sqlToyConfig.getNoSqlConfigModel() == null)
        throw new Exception(ERROR_MESSAGE);
    if (sqlToyConfig.getNoSqlConfigModel().isSqlMode())
        return ElasticSqlPlugin.findPage(sqlToyContext, sqlToyConfig, pageModel, queryExecutor);
    return ElasticSearchPlugin.findPage(sqlToyContext, sqlToyConfig, pageModel, queryExecutor);
}
Also used : QueryExecutor(org.sagacity.sqltoy.executor.QueryExecutor) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 7 with QueryExecutor

use of org.sagacity.sqltoy.executor.QueryExecutor in project sagacity-sqltoy by chenrenfei.

the class TranslateManager method getCache.

/**
 * @todo 根据sqltoy sql.xml中的翻译设置获取对应的缓存
 * @param sqlToyContext
 * @param conn
 * @param cacheName
 * @param cacheType
 *            一般为null,不为空时一般用于数据字典等同于dictType
 * @return
 * @throws Exception
 */
public HashMap<String, Object[]> getCache(final SqlToyContext sqlToyContext, Connection conn, String cacheName, String cacheType) throws Exception {
    // 获取缓存翻译的管理器
    TranslateCacheModel cacheModel = translateMap.get(cacheName);
    if (cacheModel == null) {
        // cacheName);
        return null;
    }
    TranslateCacheManager manager = getTranslateCacheManager(cacheModel.getTranslateCacheManager());
    HashMap<String, Object[]> result = manager.getCache(cacheName, cacheType);
    if (result == null || result.isEmpty()) {
        final Object[] args = StringUtil.isBlank(cacheType) ? null : new Object[] { cacheType };
        // sql 查询模式
        if (StringUtil.isNotBlank(cacheModel.getSql())) {
            final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(cacheModel.getSql(), SqlType.search);
            // 避免sql中有:name 参数名称
            String realSql = SqlConfigParseUtils.processNamedParamsQuery(sqlToyConfig.getSql()).getSql();
            String dataSourceName = cacheModel.getDataSource();
            if (dataSourceName == null)
                dataSourceName = sqlToyConfig.getDataSource();
            // 设置默认数据库
            if (null == conn && StringUtil.isBlank(dataSourceName))
                dataSourceName = getDefaultDataSource();
            List cacheResult = null;
            // 缓存sql来源于不同数据库
            if (sqlToyConfig.getDataSourceShardingStragety() != null || StringUtil.isNotBlank(dataSourceName)) {
                DataSource dataBase = null;
                if (StringUtil.isNotBlank(dataSourceName))
                    dataBase = sqlToyContext.getDataSource(dataSourceName);
                else {
                    // 考虑存在分库策略,update 2017-12-8
                    dataBase = ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, new QueryExecutor(cacheModel.getSql(), null, args), null);
                }
                cacheResult = (List) DataSourceUtils.processDataSource(sqlToyContext, dataBase, new DataSourceCallbackHandler() {

                    @Override
                    public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                        this.setResult(DialectUtils.findBySql(sqlToyContext, sqlToyConfig, sqlToyContext.convertFunctions(realSql, dialect), args, null, conn, 0, -1, -1).getRows());
                    }
                });
            } else {
                cacheResult = DialectUtils.findBySql(sqlToyContext, sqlToyConfig, sqlToyContext.convertFunctions(realSql, DataSourceUtils.getDialect(DataSourceUtils.getDbType(conn))), args, null, conn, 0, -1, -1).getRows();
            }
            result = new HashMap<String, Object[]>();
            int cacheIndex = cacheModel.getKeyIndex();
            List row;
            for (int i = 0, n = cacheResult.size(); i < n; i++) {
                row = (List) cacheResult.get(i);
                Object[] rowAry = new Object[row.size()];
                for (int j = 0, t = rowAry.length; j < t; j++) {
                    rowAry[j] = row.get(j);
                }
                result.put(rowAry[cacheIndex].toString(), rowAry);
            }
        } else {
            // 通过spring 调用具体的bean 方法获取数据,必须返回的是HashMap结果
            try {
                result = (HashMap<String, Object[]>) sqlToyContext.getServiceData(cacheModel.getService(), cacheModel.getServiceMethod(), args);
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("缓存翻译通过接口服务:{}.{} 返回的结果必须是HashMap<key, Object[]{key,name1,..,nameX}> 类型的数据格式!", cacheModel.getService(), cacheModel.getServiceMethod());
            }
        }
        // 放入缓存
        if (result != null && !result.isEmpty()) {
            manager.put(cacheModel.getCacheConfig(), cacheName, cacheType, result);
        }
    }
    return result;
}
Also used : TranslateCacheModel(org.sagacity.sqltoy.config.model.TranslateCacheModel) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) TranslateCacheManager(org.sagacity.sqltoy.cache.TranslateCacheManager) Connection(java.sql.Connection) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) DocumentException(org.dom4j.DocumentException) DataSource(javax.sql.DataSource) QueryExecutor(org.sagacity.sqltoy.executor.QueryExecutor) List(java.util.List)

Aggregations

QueryExecutor (org.sagacity.sqltoy.executor.QueryExecutor)7 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)4 Connection (java.sql.Connection)2 List (java.util.List)2 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)2 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)2 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 DataSource (javax.sql.DataSource)1 DocumentException (org.dom4j.DocumentException)1 TranslateCacheManager (org.sagacity.sqltoy.cache.TranslateCacheManager)1 UpdateRowHandler (org.sagacity.sqltoy.callback.UpdateRowHandler)1 NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)1 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)1 TranslateCacheModel (org.sagacity.sqltoy.config.model.TranslateCacheModel)1 BaseException (org.sagacity.sqltoy.exception.BaseException)1 MongoTemplate (org.springframework.data.mongodb.core.MongoTemplate)1