Search in sources :

Example 21 with DataSourceCallbackHandler

use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.

the class DialectFactory method updateAll.

/**
 * @todo 批量修改对象
 * @param sqlToyContext
 * @param entities
 * @param batchSize
 * @param forceUpdateFields
 * @param reflectPropertyHandler
 * @param dataSource
 * @param autoCommit
 * @throws Exception
 */
public Long updateAll(final SqlToyContext sqlToyContext, final List<?> entities, final int batchSize, final String[] forceUpdateFields, 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, false, 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).updateAll(sqlToyContext, batchModel.getEntities(), batchSize, forceUpdateFields, 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();
    }
}
Also used : ParallelCallbackHandler(org.sagacity.sqltoy.callback.ParallelCallbackHandler) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ShardingGroupModel(org.sagacity.sqltoy.model.ShardingGroupModel) BaseException(org.sagacity.sqltoy.exception.BaseException) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) List(java.util.List) ArrayList(java.util.ArrayList) SqlToyContext(org.sagacity.sqltoy.SqlToyContext) ShardingModel(org.sagacity.sqltoy.model.ShardingModel)

Example 22 with DataSourceCallbackHandler

use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.

the class DialectFactory method batchUpdate.

/**
 * @todo 批量执行sql修改或删除操作
 * @param sqlToyContext
 * @param sqlOrNamedSql
 * @param dataSet
 * @param batchSize
 * @param reflectPropertyHandler
 * @param insertCallhandler
 * @param autoCommit
 * @param dataSource
 * @return
 * @throws Exception
 */
public Long batchUpdate(final SqlToyContext sqlToyContext, final String sqlOrNamedSql, final List dataSet, final int batchSize, final ReflectPropertyHandler reflectPropertyHandler, final InsertRowCallbackHandler insertCallhandler, final Boolean autoCommit, final DataSource dataSource) throws Exception {
    try {
        final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sqlOrNamedSql, SqlType.update);
        SqlExecuteStat.start(sqlToyConfig.getId(), "batchUpdate", sqlToyConfig.isShowSql());
        return (Long) DataSourceUtils.processDataSource(sqlToyContext, dataSource, new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                String realSql = sqlToyContext.convertFunctions(sqlToyConfig.getSql(), dialect);
                Integer[] fieldTypes = null;
                List values = dataSet;
                // sql中存在:named参数模式,通过sql提取参数名称
                if (sqlToyConfig.getParamsName() != null) {
                    // 替换sql中:name为?并提取参数名称归集成数组
                    SqlParamsModel sqlParamsModel = SqlConfigParseUtils.processNamedParamsQuery(realSql);
                    values = BeanUtil.reflectBeansToList(dataSet, sqlParamsModel.getParamsName(), reflectPropertyHandler, false, 0);
                    fieldTypes = BeanUtil.matchMethodsType(dataSet.get(0).getClass(), sqlParamsModel.getParamsName());
                    realSql = sqlParamsModel.getSql();
                }
                SqlExecuteStat.showSql(realSql, null);
                this.setResult(SqlUtil.batchUpdateByJdbc(realSql, values, batchSize, insertCallhandler, fieldTypes, autoCommit, conn));
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) SqlParamsModel(org.sagacity.sqltoy.config.model.SqlParamsModel) List(java.util.List) ArrayList(java.util.ArrayList) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) BaseException(org.sagacity.sqltoy.exception.BaseException)

Example 23 with DataSourceCallbackHandler

use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.

the class DialectFactory method deleteAll.

/**
 * @todo 批量删除对象
 * @param sqlToyContext
 * @param entities
 * @param batchSize
 * @param dataSource
 * @param autoCommit
 * @throws Exception
 */
public Long deleteAll(final SqlToyContext sqlToyContext, final List<?> entities, final int batchSize, 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, false, 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).deleteAll(sqlToyContext, batchModel.getEntities(), batchSize, 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();
    }
}
Also used : ParallelCallbackHandler(org.sagacity.sqltoy.callback.ParallelCallbackHandler) Connection(java.sql.Connection) ArrayList(java.util.ArrayList) ShardingGroupModel(org.sagacity.sqltoy.model.ShardingGroupModel) BaseException(org.sagacity.sqltoy.exception.BaseException) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) List(java.util.List) ArrayList(java.util.ArrayList) SqlToyContext(org.sagacity.sqltoy.SqlToyContext) ShardingModel(org.sagacity.sqltoy.model.ShardingModel)

Example 24 with DataSourceCallbackHandler

use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.

the class DialectFactory method updateFetchRandom.

@Deprecated
public QueryResult updateFetchRandom(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final Integer random, final UpdateRowHandler updateRowHandler, final DataSource dataSource) throws Exception {
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "updateFetchRandom", 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).updateFetchRandom(sqlToyContext, realSqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), random, 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();
    }
}
Also used : QueryResult(org.sagacity.sqltoy.model.QueryResult) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) BaseException(org.sagacity.sqltoy.exception.BaseException) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 25 with DataSourceCallbackHandler

use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler in project sagacity-sqltoy by chenrenfei.

the class DialectFactory method findByQuery.

/**
 * @todo 查询符合条件的数据集合
 * @param sqlToyContext
 * @param queryExecutor
 * @param dataSource
 * @return
 * @throws Exception
 */
public QueryResult findByQuery(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final DataSource dataSource) throws Exception {
    if (queryExecutor.getSql() == null)
        throw new Exception("findByQuery operate sql is null!");
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "query", 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);
                // 通过参数处理最终的sql和参数值
                SqlToyResult queryParam = SqlConfigParseUtils.processSql(realSqlToyConfig.getSql(), queryExecutor.getParamsName(realSqlToyConfig), queryExecutor.getParamsValue(realSqlToyConfig));
                QueryResult queryResult = getDialectSqlWrapper(dbType).findBySql(sqlToyContext, realSqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), queryExecutor.getRowCallbackHandler(), conn, queryExecutor.getFetchSize(), queryExecutor.getMaxRows());
                // 存在计算和旋转的数据不能映射到对象(数据类型不一致,如汇总平均以及数据旋转)
                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) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Aggregations

Connection (java.sql.Connection)40 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)40 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)24 ArrayList (java.util.ArrayList)23 SqlToyConfig (org.sagacity.sqltoy.config.model.SqlToyConfig)20 List (java.util.List)19 BaseException (org.sagacity.sqltoy.exception.BaseException)15 QueryResult (org.sagacity.sqltoy.model.QueryResult)14 ShardingModel (org.sagacity.sqltoy.config.model.ShardingModel)13 SqlToyResult (org.sagacity.sqltoy.config.model.SqlToyResult)12 QueryExecutorExtend (org.sagacity.sqltoy.model.inner.QueryExecutorExtend)9 SqlToyContext (org.sagacity.sqltoy.SqlToyContext)6 ParallelCallbackHandler (org.sagacity.sqltoy.callback.ParallelCallbackHandler)5 ShardingGroupModel (org.sagacity.sqltoy.model.ShardingGroupModel)5 ShardingModel (org.sagacity.sqltoy.model.ShardingModel)5 Serializable (java.io.Serializable)3 SqlParamsModel (org.sagacity.sqltoy.config.model.SqlParamsModel)3 Type (java.lang.reflect.Type)2 HashMap (java.util.HashMap)2 DataSource (javax.sql.DataSource)2