Search in sources :

Example 1 with LockMode

use of org.sagacity.sqltoy.model.LockMode in project sagacity-sqltoy by chenrenfei.

the class DialectFactory method loadAll.

/**
 * @todo 批量加载集合(自4.13.1 版本已经自动将超大规模集合拆分执行),规避了jpa等框架的缺陷
 * @param sqlToyContext
 * @param entities
 * @param cascadeTypes
 * @param lockMode
 * @param dataSource
 * @return
 */
public <T extends Serializable> List<T> loadAll(final SqlToyContext sqlToyContext, final List<T> entities, final Class[] cascadeTypes, final LockMode lockMode, final DataSource dataSource) {
    if (entities == null || entities.isEmpty()) {
        logger.warn("loadAll entities is null or empty,please check!");
        return entities;
    }
    try {
        SqlExecuteStat.start(BeanUtil.getEntityClass(entities.get(0).getClass()).getName(), "loadAll:[" + entities.size() + "]条记录!", null);
        // 一般in的最大数量是1000
        int batchSize = SqlToyConstants.getLoadAllBatchSize();
        // 对可能存在的配置参数定义错误进行校正,最大控制在1000内
        if (batchSize > 1000 || batchSize < 1) {
            batchSize = 1000;
        }
        int totalSize = entities.size();
        int batch = (totalSize + batchSize - 1) / batchSize;
        List result = new ArrayList();
        List batchEntities;
        for (int i = 0; i < batch; i++) {
            // 切取单个批次的记录
            batchEntities = entities.subList(i * batchSize, (i == batch - 1) ? totalSize : (i + 1) * batchSize);
            // 分库分表并行执行,并返回结果
            result.addAll(ParallelUtils.execute(sqlToyContext, batchEntities, false, dataSource, (context, batchModel) -> {
                ShardingModel shardingModel = batchModel.getShardingModel();
                return (List) DataSourceUtils.processDataSource(context, shardingModel.getDataSource(), new DataSourceCallbackHandler() {

                    @Override
                    public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                        this.setResult(getDialectSqlWrapper(dbType).loadAll(context, batchModel.getEntities(), (cascadeTypes == null) ? null : CollectionUtil.arrayToList(cascadeTypes), lockMode, conn, dbType, dialect, shardingModel.getTableName(), getFetchSize(-1), -1));
                    }
                });
            }));
        }
        SqlExecuteStat.debug("执行结果", "查询结果记录:{} 条!", result.size());
        return result;
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw new DataAccessException(e);
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : SqlServerDialect(org.sagacity.sqltoy.dialect.impl.SqlServerDialect) EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) PageOptimizeUtils(org.sagacity.sqltoy.dialect.utils.PageOptimizeUtils) ResultUtils(org.sagacity.sqltoy.utils.ResultUtils) Connection(java.sql.Connection) GuassDBDialect(org.sagacity.sqltoy.dialect.impl.GuassDBDialect) SqlParamsModel(org.sagacity.sqltoy.config.model.SqlParamsModel) LoggerFactory(org.slf4j.LoggerFactory) SqlUtil(org.sagacity.sqltoy.utils.SqlUtil) PostgreSqlDialect(org.sagacity.sqltoy.dialect.impl.PostgreSqlDialect) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) TreeTableModel(org.sagacity.sqltoy.model.TreeTableModel) OceanBaseDialect(org.sagacity.sqltoy.dialect.impl.OceanBaseDialect) Map(java.util.Map) CollectionUtil(org.sagacity.sqltoy.utils.CollectionUtil) KingbaseDialect(org.sagacity.sqltoy.dialect.impl.KingbaseDialect) Oracle11gDialect(org.sagacity.sqltoy.dialect.impl.Oracle11gDialect) DecryptHandler(org.sagacity.sqltoy.callback.DecryptHandler) SqlUtilsExt(org.sagacity.sqltoy.utils.SqlUtilsExt) SqlConfigParseUtils(org.sagacity.sqltoy.config.SqlConfigParseUtils) QueryResult(org.sagacity.sqltoy.model.QueryResult) SqlWithAnalysis(org.sagacity.sqltoy.config.model.SqlWithAnalysis) PageOptimize(org.sagacity.sqltoy.config.model.PageOptimize) SqliteDialect(org.sagacity.sqltoy.dialect.impl.SqliteDialect) Executors(java.util.concurrent.Executors) Serializable(java.io.Serializable) QueryExecutor(org.sagacity.sqltoy.model.QueryExecutor) List(java.util.List) StoreResult(org.sagacity.sqltoy.model.StoreResult) Type(java.lang.reflect.Type) DB2Dialect(org.sagacity.sqltoy.dialect.impl.DB2Dialect) ParallelUtils(org.sagacity.sqltoy.utils.ParallelUtils) Pattern(java.util.regex.Pattern) QueryExecutorExtend(org.sagacity.sqltoy.model.inner.QueryExecutorExtend) DefaultDialect(org.sagacity.sqltoy.dialect.impl.DefaultDialect) UpdateRowHandler(org.sagacity.sqltoy.callback.UpdateRowHandler) DMDialect(org.sagacity.sqltoy.dialect.impl.DMDialect) HashMap(java.util.HashMap) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) SqlExecuteStat(org.sagacity.sqltoy.SqlExecuteStat) ArrayList(java.util.ArrayList) QueryExecutorBuilder(org.sagacity.sqltoy.utils.QueryExecutorBuilder) FieldMeta(org.sagacity.sqltoy.config.model.FieldMeta) ShardingUtils(org.sagacity.sqltoy.plugins.sharding.ShardingUtils) SqlExecuteTrace(org.sagacity.sqltoy.config.model.SqlExecuteTrace) DataSource(javax.sql.DataSource) FieldsSecureProvider(org.sagacity.sqltoy.plugins.secure.FieldsSecureProvider) ShardingModel(org.sagacity.sqltoy.config.model.ShardingModel) TableMeta(org.sagacity.sqltoy.model.TableMeta) ExecutorService(java.util.concurrent.ExecutorService) MySqlDialect(org.sagacity.sqltoy.dialect.impl.MySqlDialect) Logger(org.slf4j.Logger) ReflectPropsHandler(org.sagacity.sqltoy.callback.ReflectPropsHandler) SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) StringUtil(org.sagacity.sqltoy.utils.StringUtil) ImpalaDialect(org.sagacity.sqltoy.dialect.impl.ImpalaDialect) OracleDialect(org.sagacity.sqltoy.dialect.impl.OracleDialect) SqlToyContext(org.sagacity.sqltoy.SqlToyContext) DBType(org.sagacity.sqltoy.utils.DataSourceUtils.DBType) DataSourceUtils(org.sagacity.sqltoy.utils.DataSourceUtils) TimeUnit(java.util.concurrent.TimeUnit) DialectUtils(org.sagacity.sqltoy.dialect.utils.DialectUtils) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult) UniqueExecutor(org.sagacity.sqltoy.model.UniqueExecutor) InsertRowCallbackHandler(org.sagacity.sqltoy.callback.InsertRowCallbackHandler) BeanUtil(org.sagacity.sqltoy.utils.BeanUtil) LockMode(org.sagacity.sqltoy.model.LockMode) TidbDialect(org.sagacity.sqltoy.dialect.impl.TidbDialect) ColumnMeta(org.sagacity.sqltoy.model.ColumnMeta) SqlToyConstants(org.sagacity.sqltoy.SqlToyConstants) ClickHouseDialect(org.sagacity.sqltoy.dialect.impl.ClickHouseDialect) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) List(java.util.List) ArrayList(java.util.ArrayList) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) DataAccessException(org.sagacity.sqltoy.exception.DataAccessException) ShardingModel(org.sagacity.sqltoy.config.model.ShardingModel)

Aggregations

Serializable (java.io.Serializable)1 Type (java.lang.reflect.Type)1 Connection (java.sql.Connection)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 TimeUnit (java.util.concurrent.TimeUnit)1 Pattern (java.util.regex.Pattern)1 DataSource (javax.sql.DataSource)1 SqlExecuteStat (org.sagacity.sqltoy.SqlExecuteStat)1 SqlToyConstants (org.sagacity.sqltoy.SqlToyConstants)1 SqlToyContext (org.sagacity.sqltoy.SqlToyContext)1 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)1 DecryptHandler (org.sagacity.sqltoy.callback.DecryptHandler)1 InsertRowCallbackHandler (org.sagacity.sqltoy.callback.InsertRowCallbackHandler)1 ReflectPropsHandler (org.sagacity.sqltoy.callback.ReflectPropsHandler)1 UpdateRowHandler (org.sagacity.sqltoy.callback.UpdateRowHandler)1