use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method updateFetch.
/**
* @todo 查询锁定记录,并进行修改
* @param sqlToyContext
* @param queryExecutor
* @param updateRowHandler
* @param dataSource
* @return
* @throws Exception
*/
public QueryResult updateFetch(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final UpdateRowHandler updateRowHandler, final DataSource dataSource) throws Exception {
final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
try {
SqlExecuteStat.start(sqlToyConfig.getId(), "updateFetch", 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);
SqlToyResult queryParam = SqlConfigParseUtils.processSql(realSqlToyConfig.getSql(), queryExecutor.getParamsName(realSqlToyConfig), queryExecutor.getParamsValue(realSqlToyConfig));
QueryResult queryResult = getDialectSqlWrapper(dbType).updateFetch(sqlToyContext, realSqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), updateRowHandler, conn);
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.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.
the class OracleDialectUtils method load.
/**
* @todo 加载单个对象
* @param sqlToyContext
* @param entity
* @param cascadeTypes
* @param lockMode
* @param conn
* @param tableName
* @return
* @throws Exception
*/
public static Serializable load(final SqlToyContext sqlToyContext, Serializable entity, List<Class> cascadeTypes, LockMode lockMode, Connection conn, 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);
}
use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.
the class SybaseIQDialect method load.
/*
* sybase iq 没有所谓的行锁
*
* @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);
return (Serializable) DialectUtils.load(sqlToyContext, sqlToyConfig, sqlToyConfig.getSql(), entityMeta, entity, cascadeTypes, conn);
}
use of org.sagacity.sqltoy.config.model.SqlToyConfig in project sagacity-sqltoy by chenrenfei.
the class SqlToyContext method getSqlToyConfig.
/**
* @todo 获取sql对应的配置模型
* @param sqlKey
* @param type
* @return
*/
public SqlToyConfig getSqlToyConfig(String sqlKey, SqlType type) {
SqlToyConfig result = scriptLoader.getSqlConfig(sqlKey);
if (null == result) {
// 判断是否是sqlId,非在xml中定义id的sql
if (!SqlConfigParseUtils.isNamedQuery(sqlKey)) {
result = SqlConfigParseUtils.parseSqlToyConfig(sqlKey, (null == scriptLoader) ? null : scriptLoader.getDialect(), type, (null == scriptLoader) ? null : scriptLoader.getFunctionConverts());
// 设置默认空白查询条件过滤filter,便于直接传递sql语句情况下查询条件的处理
ParamFilterModel[] filters = new ParamFilterModel[1];
filters[0] = new ParamFilterModel("blank", new String[] { "*" });
result.setFilters(filters);
} else {
// 这一步理论上不应该执行
result = new SqlToyConfig();
result.setSql(sqlKey);
}
}
return result;
}
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 sqlToyConfig
* @param randomCount
* @param dataSource
* @return
*/
public QueryResult getRandomResult(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final SqlToyConfig sqlToyConfig, final Double randomCount, final DataSource dataSource) {
final QueryExecutorExtend extend = queryExecutor.getInnerModel();
if (extend.sql == null) {
throw new IllegalArgumentException("getRandomResult operate sql is null!");
}
try {
Long startTime = System.currentTimeMillis();
// 规整查询参数名称和参数名称对应的值
QueryExecutorBuilder.initQueryExecutor(sqlToyContext, extend, sqlToyConfig, (randomCount < 1) ? true : false);
SqlExecuteStat.start(sqlToyConfig.getId(), "getRandomResult", 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, (randomCount < 1) ? true : false);
// 判断数据库是否支持取随机记录(只有informix和sybase不支持)
Long totalCount = null;
Long randomCnt;
// 记录数量大于1表示取随机记录数量
if (randomCount >= 1) {
randomCnt = randomCount.longValue();
} else // 按比例提取
{
// 提取总记录数
if (totalCount == null) {
totalCount = getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect);
}
randomCnt = Double.valueOf(totalCount * randomCount.doubleValue()).longValue();
SqlExecuteStat.debug("过程提示", "按比例提取总记录数:{}条,需取随机记录:{}条!", totalCount, randomCnt);
// 如果总记录数不为零,randomCnt最小为1
if (totalCount >= 1 && randomCnt < 1) {
randomCnt = 1L;
}
}
QueryResult queryResult;
// 总记录数为零
if (totalCount != null && totalCount == 0) {
queryResult = new QueryResult();
queryResult.setRows(new ArrayList());
this.setResult(queryResult);
logger.warn("getRandom,total Records is zero,please check sql!sqlId={}", sqlToyConfig.getIdOrSql());
return;
}
queryResult = getDialectSqlWrapper(dbType).getRandomResult(sqlToyContext, realSqlToyConfig, queryExecutor, wrapDecryptHandler(sqlToyContext, extend.resultType), totalCount, randomCnt, conn, 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();
}
}
Aggregations