use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler 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;
}
use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler 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();
}
}
use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler 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();
}
}
use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method findPage.
/**
* @todo 分页查询, pageNo为负一表示取全部记录
* @param sqlToyContext
* @param queryExecutor
* @param pageNo
* @param pageSize
* @param dataSource
* @return
* @throws Exception
*/
public QueryResult findPage(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final long pageNo, final Integer pageSize, final DataSource dataSource) throws Exception {
if (queryExecutor.getSql() == null)
throw new Exception("findPage operate sql is null!");
final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
try {
SqlExecuteStat.start(sqlToyConfig.getId(), "findPage", 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);
QueryResult queryResult = null;
Long recordCnt = null;
// 通过查询条件构造唯一的key
String pageQueryKey = PageOptimizeUtils.generateOptimizeKey(sqlToyConfig, queryExecutor);
// 需要进行分页查询优化
if (null != pageQueryKey) {
// 从缓存中提取总记录数
recordCnt = PageOptimizeUtils.getPageTotalCount(sqlToyConfig, pageQueryKey);
// 缓存中没有则重新查询
if (null == recordCnt) {
recordCnt = getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
// 将总记录数登记到缓存
PageOptimizeUtils.registPageTotalCount(sqlToyConfig, pageQueryKey, recordCnt);
}
} else
recordCnt = getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
// pageNo=-1时的提取数据量限制
int limitSize = sqlToyContext.getPageFetchSizeLimit();
// pageNo=-1时,总记录数超出限制则返回空集合
boolean illegal = (pageNo == -1 && (limitSize != -1 && recordCnt > limitSize));
if (recordCnt == 0 || illegal) {
queryResult = new QueryResult();
queryResult.setPageNo(pageNo);
queryResult.setPageSize(pageSize);
queryResult.setRecordCount(new Long(0));
if (illegal)
logger.warn("非法进行分页查询,提取记录总数为:{},sql={}", recordCnt, sqlToyConfig.getSql());
} else {
// 合法的全记录提取,设置页号为1按记录数
if (pageNo == -1) {
// 通过参数处理最终的sql和参数值
SqlToyResult queryParam = SqlConfigParseUtils.processSql(realSqlToyConfig.getSql(), queryExecutor.getParamsName(realSqlToyConfig), queryExecutor.getParamsValue(realSqlToyConfig));
queryResult = getDialectSqlWrapper(dbType).findBySql(sqlToyContext, realSqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), queryExecutor.getRowCallbackHandler(), conn, queryExecutor.getFetchSize(), queryExecutor.getMaxRows());
long totalRecord = (queryResult.getRows() == null) ? 0 : queryResult.getRows().size();
queryResult.setPageNo(1L);
queryResult.setPageSize(new Long(totalRecord).intValue());
queryResult.setRecordCount(totalRecord);
} else {
// 实际开始页(页数据超出总记录,则从第一页重新开始,相反如继续按指定的页查询则记录为空,且实际页号也不存在)
long realStartPage = (pageNo * pageSize >= (recordCnt + pageSize)) ? 1 : pageNo;
queryResult = getDialectSqlWrapper(dbType).findPageBySql(sqlToyContext, realSqlToyConfig, queryExecutor, realStartPage, pageSize, conn);
queryResult.setPageNo(realStartPage);
queryResult.setPageSize(pageSize);
queryResult.setRecordCount(recordCnt);
}
// 存在计算和旋转的数据不能映射到对象(数据类型不一致,如汇总平均以及数据旋转)
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();
}
}
use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method saveAllNotExist.
/**
* @todo 批量保存数据,当已经存在的时候忽视掉
* @param sqlToyContext
* @param entities
* @param batchSize
* @param reflectPropertyHandler
* @param dataSource
* @param autoCommit
* @throws Exception
*/
public Long saveAllNotExist(final SqlToyContext sqlToyContext, final List<?> entities, final int batchSize, final ReflectPropertyHandler reflectPropertyHandler, final DataSource dataSource, final Boolean autoCommit) throws Exception {
if (entities == null || entities.isEmpty())
return new Long(0);
try {
SqlExecuteStat.start(entities.get(0).getClass().getName(), "saveAllNotExist", null);
List<Long> result = ParallelUtils.execute(sqlToyContext, entities, true, dataSource, new ParallelCallbackHandler() {
public List execute(SqlToyContext sqlToyContext, ShardingGroupModel batchModel) throws Exception {
final ShardingModel shardingModel = batchModel.getShardingModel();
Long updateCnt = (Long) DataSourceUtils.processDataSource(sqlToyContext, shardingModel.getDataSource(), new DataSourceCallbackHandler() {
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(getDialectSqlWrapper(dbType).saveAllIgnoreExist(sqlToyContext, batchModel.getEntities(), batchSize, reflectPropertyHandler, conn, autoCommit, shardingModel.getTableName()));
}
});
List<Long> tmp = new ArrayList();
tmp.add(updateCnt);
return tmp;
}
});
long updateTotalCnt = 0;
if (result != null) {
for (Long cnt : result) {
updateTotalCnt = updateTotalCnt + cnt.longValue();
}
}
return new Long(updateTotalCnt);
} catch (Exception e) {
SqlExecuteStat.error(e);
throw e;
} finally {
SqlExecuteStat.destroy();
}
}
Aggregations