Search in sources :

Example 31 with DataSourceCallbackHandler

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

the class DialectFactory method load.

/**
 * @todo 加载单个对象
 * @param sqlToyContext
 * @param entity
 * @param cascadeTypes
 * @param lockMode
 * @param dataSource
 * @return
 */
public <T extends Serializable> T load(final SqlToyContext sqlToyContext, final T entity, final Class[] cascadeTypes, final LockMode lockMode, final DataSource dataSource) {
    if (entity == null) {
        logger.warn("load entity is null,please check!");
        return null;
    }
    try {
        // 单记录操作返回对应的库和表配置
        final ShardingModel shardingModel = ShardingUtils.getSharding(sqlToyContext, entity, false, dataSource);
        SqlExecuteStat.start(BeanUtil.getEntityClass(entity.getClass()).getName(), "load", null);
        return (T) DataSourceUtils.processDataSource(sqlToyContext, shardingModel.getDataSource(), new DataSourceCallbackHandler() {

            @Override
            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                this.setResult(getDialectSqlWrapper(dbType).load(sqlToyContext, entity, (cascadeTypes == null) ? null : CollectionUtil.arrayToList(cascadeTypes), lockMode, conn, dbType, dialect, shardingModel.getTableName()));
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw new DataAccessException(e);
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : Connection(java.sql.Connection) 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)

Example 32 with DataSourceCallbackHandler

use of org.sagacity.sqltoy.callback.DataSourceCallbackHandler 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)

Example 33 with DataSourceCallbackHandler

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

the class DialectFactory method delete.

/**
 * @todo 删除单个对象
 * @param sqlToyContext
 * @param entity
 * @param dataSource
 * @return
 */
public Long delete(final SqlToyContext sqlToyContext, final Serializable entity, final DataSource dataSource) {
    if (entity == null) {
        logger.warn("delete entity is null,please check!");
        return 0L;
    }
    try {
        SqlExecuteStat.start(BeanUtil.getEntityClass(entity.getClass()).getName(), "delete", null);
        // 获取分库分表策略结果
        final ShardingModel shardingModel = ShardingUtils.getSharding(sqlToyContext, entity, false, dataSource);
        Long updateTotalCnt = (Long) DataSourceUtils.processDataSource(sqlToyContext, shardingModel.getDataSource(), new DataSourceCallbackHandler() {

            @Override
            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                this.setResult(getDialectSqlWrapper(dbType).delete(sqlToyContext, entity, conn, dbType, dialect, shardingModel.getTableName()));
            }
        });
        SqlExecuteStat.debug("执行结果", "删除操作影响记录量:{} 条!", updateTotalCnt);
        return updateTotalCnt;
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw new DataAccessException(e);
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : Connection(java.sql.Connection) 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)

Example 34 with DataSourceCallbackHandler

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

the class DialectFactory method save.

/**
 * @todo 保存单个对象记录
 * @param sqlToyContext
 * @param entity
 * @param dataSource
 * @return
 */
public Serializable save(final SqlToyContext sqlToyContext, final Serializable entity, final DataSource dataSource) {
    if (entity == null) {
        logger.warn("save entity is null,please check!");
        return null;
    }
    try {
        SqlExecuteStat.start(BeanUtil.getEntityClass(entity.getClass()).getName(), "save", null);
        final ShardingModel shardingModel = ShardingUtils.getSharding(sqlToyContext, entity, true, dataSource);
        Serializable result = (Serializable) DataSourceUtils.processDataSource(sqlToyContext, shardingModel.getDataSource(), new DataSourceCallbackHandler() {

            @Override
            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                this.setResult(getDialectSqlWrapper(dbType).save(sqlToyContext, entity, conn, dbType, dialect, shardingModel.getTableName()));
            }
        });
        SqlExecuteStat.debug("执行结果", "单对象保存返回主键值:{}", result);
        return result;
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw new DataAccessException(e);
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : Serializable(java.io.Serializable) Connection(java.sql.Connection) 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)

Example 35 with DataSourceCallbackHandler

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

the class DialectFactory method updateSaveFetch.

/**
 * @TODO 适用于库存台账、客户资金账强事务高并发场景,一次数据库交互实现:1、锁查询;2、记录存在则修改;3、记录不存在则执行insert;4、返回修改或插入的记录信息
 * @param sqlToyContext
 * @param entity
 * @param updateRowHandler
 * @param uniqueProps      空则表示根据主键查询
 * @param dataSource
 * @return
 */
public Serializable updateSaveFetch(final SqlToyContext sqlToyContext, final Serializable entity, final UpdateRowHandler updateRowHandler, final String[] uniqueProps, final DataSource dataSource) {
    if (entity == null || updateRowHandler == null) {
        logger.warn("updateSaveFetch entity or updateRowHandler is null,please check!");
        return null;
    }
    try {
        SqlExecuteStat.start(BeanUtil.getEntityClass(entity.getClass()).getName(), "updateSaveFetch", null);
        final ShardingModel shardingModel = ShardingUtils.getSharding(sqlToyContext, entity, false, dataSource);
        Serializable result = (Serializable) DataSourceUtils.processDataSource(sqlToyContext, shardingModel.getDataSource(), new DataSourceCallbackHandler() {

            @Override
            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                this.setResult(getDialectSqlWrapper(dbType).updateSaveFetch(sqlToyContext, entity, updateRowHandler, uniqueProps, conn, dbType, dialect, shardingModel.getTableName()));
            }
        });
        return result;
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw new DataAccessException(e);
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : Serializable(java.io.Serializable) Connection(java.sql.Connection) 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

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