Search in sources :

Example 41 with SqlToyConfig

use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.

the class PostgreSqlDialect method load.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.dialect.Dialect#load(org.sagacity.sqltoy.
	 * SqlToyContext, java.io.Serializable, java.util.List,
	 * org.sagacity.sqltoy.lock.LockMode, java.sql.Connection)
	 */
@Override
public Serializable load(SqlToyContext sqlToyContext, Serializable entity, List<Class> cascadeTypes, LockMode lockMode, Connection conn, final String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    // 获取loadsql(loadsql 可以通过@loadSql进行改变,所以需要sqltoyContext重新获取)
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(entityMeta.getLoadSql(tableName), SqlType.search);
    String loadSql = sqlToyConfig.getSql();
    if (lockMode != null) {
        switch(lockMode) {
            case UPGRADE_NOWAIT:
                loadSql = loadSql + " for update nowait";
                break;
            case UPGRADE:
                loadSql = loadSql + " for update";
                break;
        }
    }
    return (Serializable) DialectUtils.load(sqlToyContext, sqlToyConfig, loadSql, entityMeta, entity, cascadeTypes, conn);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) Serializable(java.io.Serializable) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 42 with SqlToyConfig

use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.

the class MySqlDialect method load.

/*
	 * (non-Javadoc)
	 * 
	 * @see org.sagacity.sqltoy.dialect.Dialect#load(java.io.Serializable,
	 * java.util.List, java.sql.Connection)
	 */
@Override
public Serializable load(final SqlToyContext sqlToyContext, Serializable entity, List<Class> cascadeTypes, LockMode lockMode, Connection conn, final String tableName) throws Exception {
    EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
    // 获取loadsql(loadsql 可以通过@loadSql进行改变,所以需要sqltoyContext重新获取)
    SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(entityMeta.getLoadSql(tableName), SqlType.search);
    String loadSql = sqlToyConfig.getSql();
    if (lockMode != null) {
        switch(lockMode) {
            case UPGRADE_NOWAIT:
            case UPGRADE:
                loadSql = loadSql + " for update";
                break;
        }
    }
    return (Serializable) DialectUtils.load(sqlToyContext, sqlToyConfig, loadSql, entityMeta, entity, cascadeTypes, conn);
}
Also used : EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) Serializable(java.io.Serializable) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig)

Example 43 with SqlToyConfig

use of org.sagacity.sqltoy.config.model.SqlToyConfig 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)

Example 44 with SqlToyConfig

use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.

the class DialectFactory method getRandomResult.

/**
 * @todo 取随机记录
 * @param sqlToyContext
 * @param queryExecutor
 * @param randomCount
 * @param dataSource
 * @return
 * @throws Exception
 */
public QueryResult getRandomResult(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final Double randomCount, final DataSource dataSource) throws Exception {
    if (queryExecutor.getSql() == null)
        throw new Exception("getRandomResult operate sql is null!");
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "getRandomResult", sqlToyConfig.isShowSql());
        return (QueryResult) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                // 处理sql中的?为统一的:named形式
                SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, true);
                // 判断数据库是否支持取随机记录(只有informix和sybase不支持)
                Long totalCount = SqlToyConstants.randomWithDialect(dbType) ? null : getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
                Long randomCnt;
                // 记录数量大于1表示取随机记录数量
                if (randomCount >= 1) {
                    randomCnt = randomCount.longValue();
                } else // 按比例提取
                {
                    // 提取总记录数
                    if (totalCount == null) {
                        totalCount = getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
                    }
                    if (sqlToyContext.isDebug()) {
                        out.println("getRandomResult按比例提取数据,总记录数=" + totalCount);
                    }
                    randomCnt = new Double(totalCount * randomCount.doubleValue()).longValue();
                    // 如果总记录数不为零,randomCnt最小为1
                    if (totalCount >= 1 && randomCnt < 1)
                        randomCnt = 1L;
                }
                // 总记录数为零(主要针对sybase & informix 数据库)
                if (totalCount != null && totalCount == 0) {
                    this.setResult(new QueryResult());
                    logger.warn("getRandom,total Records is zero,please check sql!sqlId={}", sqlToyConfig.getId());
                    return;
                }
                QueryResult queryResult = getDialectSqlWrapper(dbType).getRandomResult(sqlToyContext, realSqlToyConfig, queryExecutor, totalCount, randomCnt, conn);
                // 存在计算和旋转的数据不能映射到对象(数据类型不一致,如汇总平均以及数据旋转)
                List pivotCategorySet = ResultUtils.getPivotCategory(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dialect);
                ResultUtils.calculate(realSqlToyConfig, queryResult, pivotCategorySet, sqlToyContext.isDebug());
                if (queryExecutor.getResultType() != null) {
                    queryResult.setRows(ResultUtils.wrapQueryResult(queryResult.getRows(), ResultUtils.humpFieldNames(queryExecutor, queryResult.getLabelNames()), (Class) queryExecutor.getResultType()));
                }
                this.setResult(queryResult);
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) List(java.util.List) ArrayList(java.util.ArrayList) BaseException(org.sagacity.sqltoy.exception.BaseException) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler)

Example 45 with SqlToyConfig

use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.

the class DialectFactory method findTop.

/**
 * @todo 取符合条件的前多少条记录
 * @param sqlToyContext
 * @param queryExecutor
 * @param topSize
 * @param dataSource
 * @return
 * @throws Exception
 */
public QueryResult findTop(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final double topSize, final DataSource dataSource) throws Exception {
    if (queryExecutor.getSql() == null)
        throw new Exception("findTop operate sql is null!");
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "findTop", sqlToyConfig.isShowSql());
        return (QueryResult) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, queryExecutor, dataSource), new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                // 处理sql中的?为统一的:named形式
                SqlToyConfig realSqlToyConfig = DialectUtils.getUnifyParamsNamedConfig(sqlToyContext, sqlToyConfig, queryExecutor, dialect, false);
                Integer realTopSize;
                if (topSize < 1) {
                    Long totalCount = getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
                    if (sqlToyContext.isDebug()) {
                        out.println("findTopByQuery按比例提取数据,总记录数=" + totalCount);
                    }
                    realTopSize = new Double(topSize * totalCount.longValue()).intValue();
                } else
                    realTopSize = new Double(topSize).intValue();
                if (realTopSize == 0) {
                    this.setResult(new QueryResult());
                    return;
                }
                QueryResult queryResult = getDialectSqlWrapper(dbType).findTopBySql(sqlToyContext, realSqlToyConfig, queryExecutor, realTopSize, conn);
                // 存在计算和旋转的数据不能映射到对象(数据类型不一致,如汇总平均以及数据旋转)
                List pivotCategorySet = ResultUtils.getPivotCategory(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dialect);
                ResultUtils.calculate(realSqlToyConfig, queryResult, pivotCategorySet, sqlToyContext.isDebug());
                // 结果映射成对象
                if (queryExecutor.getResultType() != null) {
                    queryResult.setRows(ResultUtils.wrapQueryResult(queryResult.getRows(), ResultUtils.humpFieldNames(queryExecutor, queryResult.getLabelNames()), (Class) queryExecutor.getResultType()));
                }
                this.setResult(queryResult);
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) List(java.util.List) ArrayList(java.util.ArrayList) BaseException(org.sagacity.sqltoy.exception.BaseException) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler)

Aggregations

SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)82 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)22 Serializable (java.io.Serializable)20 QueryExecutor (org.sagacity.sqltoy.model.QueryExecutor)20 QueryResult (org.sagacity.sqltoy.model.QueryResult)20 Connection (java.sql.Connection)19 ArrayList (java.util.ArrayList)19 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)19 List (java.util.List)16 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)16 QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)15 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)12 BaseException (org.sagacity.sqltoy.exception.BaseException)10 NoSqlConfigModel (org.sagacity.sqltoy.config.model.NoSqlConfigModel)5 DataSource (javax.sql.DataSource)4 Test (org.junit.jupiter.api.Test)4 ParallQueryExecutor (org.sagacity.sqltoy.dialect.executor.ParallQueryExecutor)4 QueryExecutor (org.sagacity.sqltoy.executor.QueryExecutor)4 InputStream (java.io.InputStream)3 Document (org.w3c.dom.Document)3