use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class OracleDialectUtils method loadAll.
/**
* @todo oracle loadAll 实现
* @param sqlToyContext
* @param entities
* @param cascadeTypes
* @param lockMode
* @param conn
* @return
* @throws Exception
*/
public static List<?> loadAll(final SqlToyContext sqlToyContext, List<?> entities, List<Class> cascadeTypes, LockMode lockMode, Connection conn) throws Exception {
if (null == entities || entities.isEmpty())
return null;
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
// 判断是否存在主键
if (null == entityMeta.getIdArray())
throw new Exception(entities.get(0).getClass().getName() + " Entity Object hasn't primary key,cann't use load method!");
StringBuilder loadSql = new StringBuilder();
loadSql.append("select * from ");
loadSql.append(entityMeta.getSchemaTable());
loadSql.append(" where ");
String field;
// 用in 的方式加载全量数据(在实际应用过程中应该注意in () 形式有1000个参数的限制)
for (int i = 0, n = entityMeta.getIdArray().length; i < n; i++) {
field = entityMeta.getIdArray()[i];
if (i > 0)
loadSql.append(" and ");
loadSql.append(entityMeta.getColumnName(field));
loadSql.append(" in (:").append(field).append(") ");
}
// 是否锁记录
if (lockMode != null) {
switch(lockMode) {
case UPGRADE_NOWAIT:
loadSql.append(" for update nowait ");
break;
case UPGRADE:
loadSql.append(" for update ");
break;
}
}
return DialectUtils.loadAll(sqlToyContext, loadSql.toString(), entities, cascadeTypes, conn);
}
use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class ParallelUtils method execute.
/**
* @todo 将集合进行根据sharding字段的值提取sharding策略并按照策略将集合分组,然后并行执行
* @param sqlToyContext
* @param entities
* @param dataSource
* @param handler
* @return
* @throws Exception
*/
public static List execute(final SqlToyContext sqlToyContext, List entities, boolean wrapIdValue, DataSource dataSource, ParallelCallbackHandler handler) throws Exception {
// 获取对象的媒体信息
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
// 主键值需要提前按照主键策略赋予(sequence 和assign模式的不会实际执行赋值)
if (wrapIdValue) {
ShardingUtils.assignPKs(sqlToyContext, entityMeta, entities);
}
// 将批量集合数据按sharding策略处理后的库和表组成的key分组
Collection<ShardingGroupModel> shardingGroups = ShardingUtils.groupShardings(sqlToyContext, entities, entityMeta, dataSource);
// 单分组直接执行
if (shardingGroups.size() == 1) {
return handler.execute(sqlToyContext, shardingGroups.iterator().next());
}
// 开始多线程并行执行
ShardingConfig shardingConfig = entityMeta.getShardingConfig();
List results = new ArrayList();
// 并行线程数量
int threads = shardingGroups.size();
// 如果额外策略配置了线程数量,则按照指定的线程数量执行
if (threads > shardingConfig.getMaxConcurrents() && shardingConfig.getMaxConcurrents() > 1)
threads = shardingConfig.getMaxConcurrents();
ExecutorService pool = Executors.newFixedThreadPool(threads);
List<Future<ShardingResult>> futureResult = new ArrayList<Future<ShardingResult>>();
for (final ShardingGroupModel group : shardingGroups) {
Future<ShardingResult> future = pool.submit(new DialectExecutor(sqlToyContext, group, handler));
futureResult.add(future);
}
pool.shutdown();
// 设置最大等待时长
if (shardingConfig.getMaxWaitSeconds() > 0)
pool.awaitTermination(shardingConfig.getMaxWaitSeconds(), TimeUnit.SECONDS);
// 提取各个线程返回的结果进行合并
try {
for (Future<ShardingResult> future : futureResult) {
ShardingResult item = future.get();
if (item != null && item.getRows() != null && !item.getRows().isEmpty())
results.addAll(item.getRows());
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
pool.shutdownNow();
}
return results;
}
use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class SqlToyDaoSupport method findByCriteria.
/**
* @todo 根据链式条件构造查询语句(不建议使用,并没有写sql直观)
* @param criteria
* @return
* @throws Exception
*/
protected List findByCriteria(final Criteria criteria) throws Exception {
EntityMeta entityMeta = this.getEntityMeta(criteria.getEntity().getClass());
// 构造条件查询
StringBuilder queryStr = new StringBuilder(entityMeta.getLoadAllSql());
// 增加空白增强容错性
queryStr.append(" ").append(criteria.wrapSqlFragments(entityMeta));
return findByQuery(new QueryExecutor(queryStr.toString(), null, criteria.getParamValues().toArray()).resultType(criteria.getEntity().getClass())).getRows();
}
use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class SqlToyDaoSupport method findPageByCriteria.
protected PaginationModel findPageByCriteria(PaginationModel pageModel, Criteria criteria) throws Exception {
EntityMeta entityMeta = this.getEntityMeta(criteria.getEntity().getClass());
// 构造条件查询
StringBuilder queryStr = new StringBuilder(entityMeta.getLoadAllSql());
// 增加一个空格增强容错
queryStr.append(" ").append(criteria.wrapSqlFragments(entityMeta));
return findPageByQuery(pageModel, new QueryExecutor(queryStr.toString(), null, criteria.getParamValues().toArray()).resultType(criteria.getEntity().getClass())).getPageResult();
}
use of org.sagacity.sqltoy.config.model.EntityMeta in project sagacity-sqltoy by chenrenfei.
the class ShardingUtils method getSharding.
/**
* @todo 单个对象sharding策略处理,适用于load、save、update、delete单对象操作
* @param sqlToyContext
* @param entity
* @param wrapIdValue
* @param dataSource
* @return
* @throws Exception
*/
public static ShardingModel getSharding(SqlToyContext sqlToyContext, Serializable entity, boolean wrapIdValue, DataSource dataSource) throws Exception {
ShardingModel shardingModel = new ShardingModel();
shardingModel.setDataSource(dataSource);
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entity.getClass());
// 主键值需要提前按照主键策略赋予(sequence 和assign模式的不会实际执行赋值)
if (wrapIdValue)
assignPK(sqlToyContext, entityMeta, entity);
ShardingConfig shardingConfig = entityMeta.getShardingConfig();
if (shardingConfig == null)
return shardingModel;
ShardingStrategy shardingStrategy;
ShardingStrategyConfig strategyConfig;
// 分库策略处理
if (shardingConfig.getShardingDBStrategy() != null) {
strategyConfig = shardingConfig.getShardingDBStrategy();
shardingStrategy = sqlToyContext.getShardingStrategy(strategyConfig.getName());
if (shardingStrategy == null)
throw new Exception("POJO 对象:" + entity.getClass().getName() + " Sharding DB Strategy:" + strategyConfig.getName() + " 未定义,请检查!");
IgnoreCaseLinkedMap<String, Object> valueMap = hashParams(strategyConfig.getAliasNames(), BeanUtil.reflectBeanToAry(entity, strategyConfig.getFields(), null, null));
ShardingDBModel dbModel = shardingStrategy.getShardingDB(sqlToyContext, entity.getClass(), entityMeta.getSchemaTable(), strategyConfig.getDecisionType(), valueMap);
shardingModel.setDataSourceName(dbModel.getDataSourceName());
if (dbModel.getDataSource() == null)
shardingModel.setDataSource(sqlToyContext.getDataSource(dbModel.getDataSourceName()));
else
shardingModel.setDataSource(dbModel.getDataSource());
}
// 分表策略
if (shardingConfig.getShardingTableStrategy() != null) {
strategyConfig = shardingConfig.getShardingTableStrategy();
shardingStrategy = sqlToyContext.getShardingStrategy(strategyConfig.getName());
if (shardingStrategy == null)
throw new Exception("POJO 对象:" + entity.getClass().getName() + " Sharding Table Strategy:" + strategyConfig.getName() + " 未定义,请检查!");
IgnoreCaseLinkedMap<String, Object> valueMap = hashParams(strategyConfig.getAliasNames(), BeanUtil.reflectBeanToAry(entity, strategyConfig.getFields(), null, null));
String tableName = shardingStrategy.getShardingTable(sqlToyContext, entity.getClass(), entityMeta.getSchemaTable(), strategyConfig.getDecisionType(), valueMap);
if (StringUtil.isNotBlank(tableName))
shardingModel.setTableName(tableName);
}
return shardingModel;
}
Aggregations