Search in sources :

Example 6 with ShardingModel

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

the class DialectFactory method update.

/**
 * @todo 修改单个对象
 * @param sqlToyContext
 * @param entity
 * @param uniqueFields             唯一性索引字段
 * @param forceUpdateFields
 * @param cascade
 * @param forceCascadeClass
 * @param subTableForceUpdateProps
 * @param dataSource
 * @return
 */
public Long update(final SqlToyContext sqlToyContext, final Serializable entity, final String[] forceUpdateFields, final boolean cascade, final Class[] forceCascadeClass, final HashMap<Class, String[]> subTableForceUpdateProps, final DataSource dataSource) {
    if (entity == null) {
        logger.warn("update entity is null,please check!");
        return 0L;
    }
    try {
        SqlExecuteStat.start(BeanUtil.getEntityClass(entity.getClass()).getName(), "update", 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).update(sqlToyContext, entity, forceUpdateFields, cascade, forceCascadeClass, subTableForceUpdateProps, conn, dbType, dialect, shardingModel.getTableName()));
            }
        });
        SqlExecuteStat.debug("执行结果", "update操作影响记录量:{} 条!", 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 7 with ShardingModel

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

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

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

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

Aggregations

ShardingModel (org.sagacity.sqltoy.config.model.ShardingModel)15 Connection (java.sql.Connection)13 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)13 DataAccessException (org.sagacity.sqltoy.exception.DataAccessException)13 ArrayList (java.util.ArrayList)7 Serializable (java.io.Serializable)3 HashMap (java.util.HashMap)2 List (java.util.List)2 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)2 ShardingConfig (org.sagacity.sqltoy.config.model.ShardingConfig)2 ShardingDBModel (org.sagacity.sqltoy.config.model.ShardingDBModel)2 ShardingStrategyConfig (org.sagacity.sqltoy.config.model.ShardingStrategyConfig)2 Type (java.lang.reflect.Type)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