Search in sources :

Example 11 with DataSourceCallbackHandler

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

the class DialectFactory method saveOrUpdateAll.

/**
 * @todo 批量保存或修改数据
 * @param sqlToyContext
 * @param entities
 * @param batchSize
 * @param forceUpdateProps
 * @param reflectPropertyHandler
 * @param dataSource
 * @param autoCommit
 * @throws Exception
 */
public Long saveOrUpdateAll(final SqlToyContext sqlToyContext, final List<?> entities, final int batchSize, final String[] forceUpdateProps, final ReflectPropertyHandler reflectPropertyHandler, final DataSource dataSource, final Boolean autoCommit) throws Exception {
    if (entities == null || entities.isEmpty())
        return new Long(0);
    try {
        SqlExecuteStat.start(entities.get(0).getClass().getName(), "saveOrUpdateAll", null);
        List<Long> result = ParallelUtils.execute(sqlToyContext, entities, true, 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).saveOrUpdateAll(sqlToyContext, batchModel.getEntities(), batchSize, reflectPropertyHandler, forceUpdateProps, 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 12 with DataSourceCallbackHandler

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

the class DialectFactory method executeSql.

/**
 * @todo 执行sql修改性质的操作语句
 * @param sqlToyContext
 * @param sqlOrNamedSql
 * @param paramsNamed
 * @param paramsValue
 * @param autoCommit
 * @param dataSource
 * @return
 * @throws Exception
 */
public Long executeSql(final SqlToyContext sqlToyContext, final String sqlOrNamedSql, final String[] paramsNamed, final Object[] paramsValue, final Boolean autoCommit, final DataSource dataSource) throws Exception {
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(sqlOrNamedSql, SqlType.update);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "update", sqlToyConfig.isShowSql());
        return (Long) DataSourceUtils.processDataSource(sqlToyContext, ShardingUtils.getShardingDataSource(sqlToyContext, sqlToyConfig, new QueryExecutor(sqlOrNamedSql, paramsNamed, paramsValue), dataSource), new DataSourceCallbackHandler() {

            public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
                SqlToyResult queryParam = SqlConfigParseUtils.processSql(sqlToyContext.convertFunctions(sqlToyConfig.getSql(), dialect), paramsNamed, paramsValue);
                String executeSql = queryParam.getSql();
                // 替换sharding table
                executeSql = ShardingUtils.replaceShardingTables(sqlToyContext, executeSql, sqlToyConfig, paramsNamed, paramsValue);
                // debug 显示sql
                SqlExecuteStat.showSql(executeSql, queryParam.getParamsValue());
                this.setResult(DialectUtils.executeSql(executeSql, queryParam.getParamsValue(), null, conn, autoCommit));
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) QueryExecutor(org.sagacity.sqltoy.executor.QueryExecutor) Connection(java.sql.Connection) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler) BaseException(org.sagacity.sqltoy.exception.BaseException) SqlToyResult(org.sagacity.sqltoy.config.model.SqlToyResult)

Example 13 with DataSourceCallbackHandler

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

the class DialectFactory method saveAll.

/**
 * @todo 批量保存
 * @param sqlToyContext
 * @param entities
 * @param batchSize
 * @param reflectPropertyHandler
 * @param dataSource
 * @param autoCommit
 * @throws Exception
 */
public Long saveAll(final SqlToyContext sqlToyContext, final List<?> entities, final int batchSize, 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, true, 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).saveAll(sqlToyContext, batchModel.getEntities(), batchSize, 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 14 with DataSourceCallbackHandler

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

the class DialectFactory method updateFetchTop.

@Deprecated
public QueryResult updateFetchTop(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final Integer topSize, final UpdateRowHandler updateRowHandler, final DataSource dataSource) throws Exception {
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "updateFetchTop", 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).updateFetchTop(sqlToyContext, sqlToyConfig, queryParam.getSql(), queryParam.getParamsValue(), topSize, 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 15 with DataSourceCallbackHandler

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

the class DialectFactory method getCountBySql.

/**
 * @todo 查询符合条件的记录数量
 * @param sqlToyContext
 * @param queryExecutor
 * @param dataSource
 * @return
 * @throws Exception
 */
public Long getCountBySql(final SqlToyContext sqlToyContext, final QueryExecutor queryExecutor, final DataSource dataSource) throws Exception {
    if (queryExecutor.getSql() == null)
        throw new Exception("getCountBySql operate sql is null!");
    final SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor.getSql(), SqlType.search);
    try {
        SqlExecuteStat.start(sqlToyConfig.getId(), "count", sqlToyConfig.isShowSql());
        return (Long) 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);
                this.setResult(getCountBySql(sqlToyContext, realSqlToyConfig, queryExecutor, conn, dbType, dialect));
            }
        });
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        throw e;
    } finally {
        SqlExecuteStat.destroy();
    }
}
Also used : SqlToyConfig(org.sagacity.sqltoy.config.model.SqlToyConfig) Connection(java.sql.Connection) BaseException(org.sagacity.sqltoy.exception.BaseException) DataSourceCallbackHandler(org.sagacity.sqltoy.callback.DataSourceCallbackHandler)

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