Search in sources :

Example 6 with ShardingModel

use of org.sagacity.sqltoy.model.ShardingModel 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 7 with ShardingModel

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

Aggregations

ShardingModel (org.sagacity.sqltoy.model.ShardingModel)7 ArrayList (java.util.ArrayList)6 List (java.util.List)6 ShardingGroupModel (org.sagacity.sqltoy.model.ShardingGroupModel)6 Connection (java.sql.Connection)5 SqlToyContext (org.sagacity.sqltoy.SqlToyContext)5 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)5 ParallelCallbackHandler (org.sagacity.sqltoy.callback.ParallelCallbackHandler)5 BaseException (org.sagacity.sqltoy.exception.BaseException)5 QueryShardingModel (org.sagacity.sqltoy.config.model.QueryShardingModel)2 ShardingConfig (org.sagacity.sqltoy.config.model.ShardingConfig)2 ShardingStrategyConfig (org.sagacity.sqltoy.config.model.ShardingStrategyConfig)2 ShardingDBModel (org.sagacity.sqltoy.model.ShardingDBModel)2 ShardingStrategy (org.sagacity.sqltoy.plugin.ShardingStrategy)2 HashMap (java.util.HashMap)1 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)1