Search in sources :

Example 1 with SqlToyContext

use of org.sagacity.sqltoy.SqlToyContext 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 2 with SqlToyContext

use of org.sagacity.sqltoy.SqlToyContext 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 3 with SqlToyContext

use of org.sagacity.sqltoy.SqlToyContext in project sagacity-sqltoy by chenrenfei.

the class MapperUtilsTest method testVOToPO.

@Test
public void testVOToPO() {
    StaffInfoVO staffInfoVO = new StaffInfoVO();
    staffInfoVO.setStaffId("S2007");
    staffInfoVO.setStaffCode("S2007");
    staffInfoVO.setPostType("MASTER");
    staffInfoVO.setStaffName("测试员工9");
    staffInfoVO.setSexType("M");
    staffInfoVO.setEmail("test3@aliyun.com");
    staffInfoVO.setEntryDate(LocalDate.now());
    staffInfoVO.setStatus(1);
    staffInfoVO.setOrganId("100007");
    // staffInfoVO.setPhoto(FileUtil.readAsBytes("classpath:/mock/staff_photo.jpg"));
    staffInfoVO.setCountry("86");
    SqlToyContext context = new SqlToyContext();
    try {
        context.initialize();
        StaffInfo staffInfo = MapperUtils.map(context, staffInfoVO, StaffInfo.class);
        System.err.println(JSON.toJSONString(staffInfo));
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
Also used : StaffInfoVO(org.sagacity.sqltoy.demo.vo.StaffInfoVO) SqlToyContext(org.sagacity.sqltoy.SqlToyContext) StaffInfo(org.sagacity.sqltoy.demo.domain.StaffInfo) Test(org.junit.jupiter.api.Test)

Example 4 with SqlToyContext

use of org.sagacity.sqltoy.SqlToyContext in project sagacity-sqltoy by chenrenfei.

the class DialectFactory method saveAllNotExist.

/**
 * @todo 批量保存数据,当已经存在的时候忽视掉
 * @param sqlToyContext
 * @param entities
 * @param batchSize
 * @param reflectPropertyHandler
 * @param dataSource
 * @param autoCommit
 * @throws Exception
 */
public Long saveAllNotExist(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 {
        SqlExecuteStat.start(entities.get(0).getClass().getName(), "saveAllNotExist", 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).saveAllIgnoreExist(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 5 with SqlToyContext

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

Aggregations

SqlToyContext (org.sagacity.sqltoy.SqlToyContext)9 ArrayList (java.util.ArrayList)7 Connection (java.sql.Connection)6 List (java.util.List)6 DataSourceCallbackHandler (org.sagacity.sqltoy.callback.DataSourceCallbackHandler)6 ParallelCallbackHandler (org.sagacity.sqltoy.callback.ParallelCallbackHandler)5 BaseException (org.sagacity.sqltoy.exception.BaseException)5 ShardingGroupModel (org.sagacity.sqltoy.model.ShardingGroupModel)5 ShardingModel (org.sagacity.sqltoy.model.ShardingModel)5 Test (org.junit.jupiter.api.Test)2 StaffInfo (org.sagacity.sqltoy.demo.domain.StaffInfo)2 StaffInfoVO (org.sagacity.sqltoy.demo.vo.StaffInfoVO)2 FieldsSecureProvider (org.sagacity.sqltoy.plugins.secure.FieldsSecureProvider)2 Serializable (java.io.Serializable)1 Type (java.lang.reflect.Type)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutorService (java.util.concurrent.ExecutorService)1 Executors (java.util.concurrent.Executors)1 TimeUnit (java.util.concurrent.TimeUnit)1