use of org.sagacity.sqltoy.config.model.ShardingModel in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method saveAll.
/**
* @todo 批量保存
* @param sqlToyContext
* @param entities
* @param batchSize
* @param reflectPropsHandler
* @param dataSource
* @param autoCommit
*/
public Long saveAll(final SqlToyContext sqlToyContext, final List<?> entities, final int batchSize, final ReflectPropsHandler reflectPropsHandler, final DataSource dataSource, final Boolean autoCommit) {
if (entities == null || entities.isEmpty()) {
logger.warn("saveAll entities is null or empty,please check!");
return 0L;
}
try {
SqlExecuteStat.start(BeanUtil.getEntityClass(entities.get(0).getClass()).getName(), "saveAll:[" + entities.size() + "]条记录!", null);
// 分库分表并行执行
List<Long> result = ParallelUtils.execute(sqlToyContext, entities, true, dataSource, (context, batchModel) -> {
ShardingModel shardingModel = batchModel.getShardingModel();
Long updateCnt = (Long) DataSourceUtils.processDataSource(context, shardingModel.getDataSource(), new DataSourceCallbackHandler() {
@Override
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(getDialectSqlWrapper(dbType).saveAll(context, batchModel.getEntities(), batchSize, reflectPropsHandler, conn, dbType, dialect, 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();
}
}
SqlExecuteStat.debug("执行结果", "批量保存记录量:{}条!", updateTotalCnt);
return Long.valueOf(updateTotalCnt);
} catch (Exception e) {
SqlExecuteStat.error(e);
throw new DataAccessException(e);
} finally {
SqlExecuteStat.destroy();
}
}
use of org.sagacity.sqltoy.config.model.ShardingModel in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method saveAllIgnoreExist.
/**
* @todo 批量保存数据,当已经存在的时候忽视掉
* @param sqlToyContext
* @param entities
* @param batchSize
* @param reflectPropsHandler
* @param dataSource
* @param autoCommit
*/
public Long saveAllIgnoreExist(final SqlToyContext sqlToyContext, final List<?> entities, final int batchSize, final ReflectPropsHandler reflectPropsHandler, final DataSource dataSource, final Boolean autoCommit) {
if (entities == null || entities.isEmpty()) {
logger.warn("saveAllIgnoreExist entities is null or empty,please check!");
return 0L;
}
try {
SqlExecuteStat.start(BeanUtil.getEntityClass(entities.get(0).getClass()).getName(), "saveAllNotExist:[" + entities.size() + "]条记录!", null);
List<Long> result = ParallelUtils.execute(sqlToyContext, entities, true, dataSource, (context, batchModel) -> {
ShardingModel shardingModel = batchModel.getShardingModel();
Long updateCnt = (Long) DataSourceUtils.processDataSource(context, shardingModel.getDataSource(), new DataSourceCallbackHandler() {
@Override
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(getDialectSqlWrapper(dbType).saveAllIgnoreExist(context, batchModel.getEntities(), batchSize, reflectPropsHandler, conn, dbType, dialect, 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();
}
}
SqlExecuteStat.debug("执行结果", "实际影响记录数量:{} 条!", updateTotalCnt);
return Long.valueOf(updateTotalCnt);
} catch (Exception e) {
SqlExecuteStat.error(e);
throw new DataAccessException(e);
} finally {
SqlExecuteStat.destroy();
}
}
use of org.sagacity.sqltoy.config.model.ShardingModel in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method updateAll.
/**
* @todo 批量修改对象
* @param sqlToyContext
* @param entities
* @param batchSize
* @param uniqueFields 唯一性索引字段
* @param forceUpdateFields
* @param reflectPropsHandler
* @param dataSource
* @param autoCommit
* @return
*/
public Long updateAll(final SqlToyContext sqlToyContext, final List<?> entities, final int batchSize, final String[] uniqueFields, final String[] forceUpdateFields, final ReflectPropsHandler reflectPropsHandler, final DataSource dataSource, final Boolean autoCommit) {
if (entities == null || entities.isEmpty()) {
logger.warn("updateAll entities is null or empty,please check!");
return 0L;
}
try {
SqlExecuteStat.start(BeanUtil.getEntityClass(entities.get(0).getClass()).getName(), "updateAll:[" + entities.size() + "]条记录!", null);
// 分库分表并行执行
List<Long> result = ParallelUtils.execute(sqlToyContext, entities, false, dataSource, (context, batchModel) -> {
ShardingModel shardingModel = batchModel.getShardingModel();
Long updateCnt = (Long) DataSourceUtils.processDataSource(context, shardingModel.getDataSource(), new DataSourceCallbackHandler() {
@Override
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(getDialectSqlWrapper(dbType).updateAll(context, batchModel.getEntities(), batchSize, uniqueFields, forceUpdateFields, reflectPropsHandler, conn, dbType, dialect, 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();
}
}
SqlExecuteStat.debug("执行结果", "批量更新影响记录量:{} 条!", updateTotalCnt);
return Long.valueOf(updateTotalCnt);
} catch (Exception e) {
SqlExecuteStat.error(e);
throw new DataAccessException(e);
} finally {
SqlExecuteStat.destroy();
}
}
use of org.sagacity.sqltoy.config.model.ShardingModel in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method saveOrUpdate.
// mysql、postgresql、sqlite等类似的on duplicate key update
// 存在缺陷,所以改为先update后save;但oracle、mssql、db2等可以用merge实现一次交互完成新增和修改
/**
* @todo 保存或修改单个对象(数据库单条记录)
* @param sqlToyContext
* @param entity
* @param forceUpdateProps
* @param dataSource
* @return
*/
public Long saveOrUpdate(final SqlToyContext sqlToyContext, final Serializable entity, final String[] forceUpdateProps, final DataSource dataSource) {
if (entity == null) {
logger.warn("saveOrUpdate entity is null,please check!");
return 0L;
}
// 主键值为空,直接调用save操作
if (DialectUtils.isEmptyPK(sqlToyContext, entity)) {
logger.debug("主键字段对应值存在null,因此saveOrUpdate转执行save操作!");
save(sqlToyContext, entity, dataSource);
return 1L;
}
try {
final ShardingModel shardingModel = ShardingUtils.getSharding(sqlToyContext, entity, true, dataSource);
SqlExecuteStat.start(entity.getClass().getName(), "saveOrUpdate", null);
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).saveOrUpdate(sqlToyContext, entity, forceUpdateProps, conn, dbType, dialect, null, shardingModel.getTableName()));
}
});
SqlExecuteStat.debug("执行结果", "实际影响记录数量:{} 条!", updateTotalCnt);
return updateTotalCnt;
} catch (Exception e) {
SqlExecuteStat.error(e);
throw new DataAccessException(e);
} finally {
SqlExecuteStat.destroy();
}
}
use of org.sagacity.sqltoy.config.model.ShardingModel in project sagacity-sqltoy by chenrenfei.
the class DialectFactory method isUnique.
/**
* @todo 判定数据是否重复 true 表示唯一不重复;false 表示不唯一,即数据库中已经存在
* @param sqlToyContext
* @param uniqueExecutor
* @param dataSource
* @return
*/
public boolean isUnique(final SqlToyContext sqlToyContext, final UniqueExecutor uniqueExecutor, final DataSource dataSource) {
if (uniqueExecutor.getEntity() == null) {
throw new IllegalArgumentException("unique judge entity object is null,please check!");
}
try {
SqlExecuteStat.start(BeanUtil.getEntityClass(uniqueExecutor.getEntity().getClass()).getName(), "isUnique", null);
final ShardingModel shardingModel = ShardingUtils.getSharding(sqlToyContext, uniqueExecutor.getEntity(), false, dataSource);
Boolean isUnique = (Boolean) DataSourceUtils.processDataSource(sqlToyContext, shardingModel.getDataSource(), new DataSourceCallbackHandler() {
@Override
public void doConnection(Connection conn, Integer dbType, String dialect) throws Exception {
this.setResult(getDialectSqlWrapper(dbType).isUnique(sqlToyContext, uniqueExecutor.getEntity(), uniqueExecutor.getUniqueFields(), conn, dbType, shardingModel.getTableName()));
}
});
SqlExecuteStat.debug("查询结果", "唯一性验证返回结果={}!", isUnique);
return isUnique;
} catch (Exception e) {
SqlExecuteStat.error(e);
throw new DataAccessException(e);
} finally {
SqlExecuteStat.destroy();
}
}
Aggregations