use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method saveOrUpdateAll.
/**
* @todo 批量保存或修改数据
* @param sqlToyContext
* @param entities
* @param batchSize
* @param forceUpdateProps
* @param reflectPropertyHandler
* @param dataSource
* @param autoCommit
* @throws Exception
*/
public Long saveOrUpdateAll(final SqlToyContext sqlToyContext, final List<?> entities, final int batchSize, final String[] forceUpdateProps, 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(), "saveOrUpdateAll", 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).saveOrUpdateAll(sqlToyContext, batchModel.getEntities(), batchSize, reflectPropertyHandler, forceUpdateProps, 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();
}
}
use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method executeSql.
/**
* @todo 执行sql修改性质的操作语句
* @param sqlToyContext
* @param sqlOrNamedSql
* @param paramsNamed
* @param paramsValue
* @param autoCommit
* @param dataSource
* @return
* @throws Exception
*/
public Long executeSql(final SqlToyContext sqlToyContext, final String sqlOrNamedSql, final String[] paramsNamed, final Object[] paramsValue, final Boolean autoCommit, final DataSource dataSource) throws Exception {
final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sqlOrNamedSql, SqlType.update);
try {
SqlExecuteStat.start(sqlToyConfig.getId(), "update", sqlToyConfig.isShowSql());
return (Long) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, new QueryExecutor(sqlOrNamedSql, paramsNamed, paramsValue), dataSource), new DataSourceCallbackHandler() {
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
SqlToyResult queryParam = SqlConfigParseUtils.processSql(sqlToyContext.convertFunctions(sqlToyConfig.getSql(), dialect), paramsNamed, paramsValue);
String executeSql = queryParam.getSql();
// 替换sharding table
executeSql = ShardingUtils.replaceShardingTables(sqlToyContext, executeSql, sqlToyConfig, paramsNamed, paramsValue);
// debug 显示sql
SqlExecuteStat.showSql(executeSql, queryParam.getParamsValue());
this.setResult(DialectUtils.executeSql(executeSql, queryParam.getParamsValue(), null, conn, autoCommit));
}
});
} 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 saveAll.
/**
* @todo 批量保存
* @param sqlToyContext
* @param entities
* @param batchSize
* @param reflectPropertyHandler
* @param dataSource
* @param autoCommit
* @throws Exception
*/
public Long saveAll(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 {
// 分库分表并行执行
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).saveAll(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();
}
}
use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method updateFetchTop.
@Deprecated
public QueryResult updateFetchTop(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final Integer topSize, final UpdateRowHandler updateRowHandler, final DataSource dataSource) throws Exception {
final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
try {
SqlExecuteStat.start(sqlToyConfig.getId(), "updateFetchTop", 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).updateFetchTop(sqlToyContext, sqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), topSize, 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.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method getCountBySql.
/**
* @todo 查询符合条件的记录数量
* @param sqlToyContext
* @param queryExecutor
* @param dataSource
* @return
* @throws Exception
*/
public Long getCountBySql(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final DataSource dataSource) throws Exception {
if (queryExecutor.getSql() == null)
throw new Exception("getCountBySql operate sql is null!");
final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
try {
SqlExecuteStat.start(sqlToyConfig.getId(), "count", sqlToyConfig.isShowSql());
return (Long) 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);
this.setResult(getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect));
}
});
} catch (Exception e) {
SqlExecuteStat.error(e);
throw e;
} finally {
SqlExecuteStat.destroy();
}
}
Aggregations