use of org.sagacity.sqltoy.model.inner.EntityQueryExtend in project sagacity-sqltoy by chenrenfei.
the class SqlToyDaoSupport method findEntityBase.
/**
* @TODO 提供findEntity的基础实现,供对外接口包装,额外开放了resultClass的自定义功能
* @param entityClass
* @param page 如分页查询则需指定,非分页则传null
* @param entityQuery
* @param resultClass 指定返回结果类型
* @param isCount
* @return
*/
private Object findEntityBase(Class entityClass, Page page, EntityQuery entityQuery, Class resultClass, boolean isCount) {
EntityMeta entityMeta = getEntityMeta(entityClass);
EntityQueryExtend innerModel = entityQuery.getInnerModel();
String translateFields = "";
// 将缓存翻译对应的查询补充到select column 上,形成select keyColumn as viewColumn 模式
if (!innerModel.translates.isEmpty()) {
Iterator<Translate> iter = innerModel.translates.values().iterator();
String keyColumn;
TranslateExtend extend;
while (iter.hasNext()) {
extend = iter.next().getExtend();
// 将java模式的字段名称转化为数据库字段名称
keyColumn = entityMeta.getColumnName(extend.keyColumn);
if (keyColumn == null) {
keyColumn = extend.keyColumn;
}
// 保留字处理
keyColumn = ReservedWordsUtil.convertWord(keyColumn, null);
translateFields = translateFields.concat(",").concat(keyColumn).concat(" as ").concat(extend.column);
}
}
// 将notSelect构造成select,形成统一处理机制
String[] selectFieldAry = null;
Set<String> notSelect = innerModel.notSelectFields;
if (notSelect != null) {
List<String> selectFields = new ArrayList<String>();
for (String field : entityMeta.getFieldsArray()) {
if (!notSelect.contains(field.toLowerCase())) {
selectFields.add(field);
}
}
if (selectFields.size() > 0) {
selectFieldAry = new String[selectFields.size()];
selectFields.toArray(selectFieldAry);
}
} else {
selectFieldAry = innerModel.fields;
}
// 指定的查询字段
String fields = "";
if (selectFieldAry != null && selectFieldAry.length > 0) {
int index = 0;
String colName;
HashSet<String> cols = new HashSet<String>();
boolean notAllPureField = false;
for (String field : selectFieldAry) {
// 去除重复字段
if (!cols.contains(field)) {
colName = entityMeta.getColumnName(field);
// 非表字段对应pojo的属性名称
if (colName == null) {
colName = field;
// 非字段名称
if (!entityMeta.getColumnFieldMap().containsKey(colName.toLowerCase())) {
notAllPureField = true;
} else {
// 保留字处理
colName = ReservedWordsUtil.convertWord(colName, null);
}
} else {
// 保留字处理
colName = ReservedWordsUtil.convertWord(colName, null);
}
if (index > 0) {
fields = fields.concat(",");
}
fields = fields.concat(colName);
index++;
cols.add(field);
}
}
// select 字段中可能存在max(field)或field as xxx等非字段形式
if (notAllPureField) {
fields = SqlUtil.convertFieldsToColumns(entityMeta, fields);
}
} else {
fields = entityMeta.getAllColumnNames();
}
String sql = "select ".concat((innerModel.distinct) ? " distinct " : "").concat(fields).concat(translateFields).concat(" from ").concat(entityMeta.getSchemaTable(null, null));
// where条件
String where = "";
// 动态组织where 后面的条件语句,此功能并不建议使用,where 一般需要指定明确条件
if (StringUtil.isBlank(innerModel.where)) {
if (innerModel.values != null && innerModel.values.length > 0) {
where = SqlUtil.wrapWhere(entityMeta);
}
} else {
where = SqlUtil.convertFieldsToColumns(entityMeta, innerModel.where);
}
if (StringUtil.isNotBlank(where)) {
sql = sql.concat(" where ").concat(where);
}
// 分组和having
if (StringUtil.isNotBlank(innerModel.groupBy)) {
sql = sql.concat(" group by ").concat(SqlUtil.convertFieldsToColumns(entityMeta, innerModel.groupBy));
if (StringUtil.isNotBlank(innerModel.having)) {
sql = sql.concat(" having ").concat(SqlUtil.convertFieldsToColumns(entityMeta, innerModel.having));
}
}
// 处理order by 排序
if (!innerModel.orderBy.isEmpty()) {
sql = sql.concat(" order by ");
Iterator<Entry<String, String>> iter = innerModel.orderBy.entrySet().iterator();
Entry<String, String> entry;
String columnName;
int index = 0;
while (iter.hasNext()) {
entry = iter.next();
columnName = entityMeta.getColumnName(entry.getKey());
if (columnName == null) {
columnName = entry.getKey();
}
// 保留字处理
columnName = ReservedWordsUtil.convertWord(columnName, null);
if (index > 0) {
sql = sql.concat(",");
}
// entry.getValue() is order way,like: desc or " "
sql = sql.concat(columnName).concat(entry.getValue());
index++;
}
}
QueryExecutor queryExecutor;
Class resultType = (resultClass == null) ? entityClass : resultClass;
// :named 模式(named模式参数值必须存在)
if (SqlConfigParseUtils.hasNamedParam(where) && StringUtil.isBlank(innerModel.names)) {
queryExecutor = new QueryExecutor(sql, (innerModel.values == null || innerModel.values.length == 0) ? null : (Serializable) innerModel.values[0]).resultType(resultType).dataSource(getDataSource(innerModel.dataSource)).fetchSize(innerModel.fetchSize).maxRows(innerModel.maxRows);
} else {
queryExecutor = new QueryExecutor(sql).names(innerModel.names).values(innerModel.values).resultType(resultType).dataSource(getDataSource(innerModel.dataSource)).fetchSize(innerModel.fetchSize).maxRows(innerModel.maxRows);
}
// 设置是否空白转null
queryExecutor.getInnerModel().blankToNull = innerModel.blankToNull;
// 设置额外的缓存翻译
if (!innerModel.translates.isEmpty()) {
queryExecutor.getInnerModel().translates.putAll(innerModel.translates);
}
// 设置额外的参数条件过滤
if (!innerModel.paramFilters.isEmpty()) {
queryExecutor.getInnerModel().paramFilters.addAll(innerModel.paramFilters);
}
// 设置安全脱敏
if (!innerModel.secureMask.isEmpty()) {
queryExecutor.getInnerModel().secureMask.putAll(innerModel.secureMask);
}
// 设置分页优化
queryExecutor.getInnerModel().pageOptimize = innerModel.pageOptimize;
SqlToyConfig sqlToyConfig = sqlToyContext.getSqlToyConfig(queryExecutor, SqlType.search, getDialect(queryExecutor.getInnerModel().dataSource));
// 加密字段,查询时解密
if (entityMeta.getSecureColumns() != null) {
sqlToyConfig.setDecryptColumns(entityMeta.getSecureColumns());
}
// 分库分表策略
setEntitySharding(queryExecutor, entityMeta);
if (innerModel.dbSharding != null) {
queryExecutor.getInnerModel().dbSharding = innerModel.dbSharding;
}
if (innerModel.tableSharding != null) {
ShardingStrategyConfig shardingConfig = innerModel.tableSharding;
// 补充表名称
shardingConfig.setTables(new String[] { entityMeta.getTableName() });
List<ShardingStrategyConfig> tableShardings = new ArrayList<ShardingStrategyConfig>();
tableShardings.add(shardingConfig);
queryExecutor.getInnerModel().tableShardings = tableShardings;
}
DataSource realDataSource = getDataSource(queryExecutor.getInnerModel().dataSource, sqlToyConfig);
// 取count数量
if (isCount) {
return dialectFactory.getCountBySql(sqlToyContext, queryExecutor, sqlToyConfig, realDataSource);
}
// 非分页
if (page == null) {
// 取top
if (innerModel.pickType == 0) {
return dialectFactory.findTop(sqlToyContext, queryExecutor, sqlToyConfig, innerModel.pickSize, realDataSource).getRows();
} else // 取随机记录
if (innerModel.pickType == 1) {
return dialectFactory.getRandomResult(sqlToyContext, queryExecutor, sqlToyConfig, innerModel.pickSize, realDataSource).getRows();
} else {
return dialectFactory.findByQuery(sqlToyContext, queryExecutor, sqlToyConfig, innerModel.lockMode, realDataSource).getRows();
}
}
// 跳过总记录数形式的分页
if (page.getSkipQueryCount()) {
return dialectFactory.findSkipTotalCountPage(sqlToyContext, queryExecutor, sqlToyConfig, page.getPageNo(), page.getPageSize(), realDataSource).getPageResult();
}
return dialectFactory.findPage(sqlToyContext, queryExecutor, sqlToyConfig, page.getPageNo(), page.getPageSize(), realDataSource).getPageResult();
}
use of org.sagacity.sqltoy.model.inner.EntityQueryExtend in project sagacity-sqltoy by chenrenfei.
the class SqlToyDaoSupport method deleteByQuery.
/**
* @TODO 提供单表简易查询进行删除操作(删除操作filters过滤无效)
* @param entityClass
* @param entityQuery
* @return
*/
protected Long deleteByQuery(Class entityClass, EntityQuery entityQuery) {
EntityQueryExtend innerModel = entityQuery.getInnerModel();
if (null == entityClass || null == entityQuery || StringUtil.isBlank(innerModel.where) || StringUtil.isBlank(innerModel.values)) {
throw new IllegalArgumentException("deleteByQuery entityClass、where、value 值不能为空!");
}
// 做一个必要提示
if (!innerModel.paramFilters.isEmpty()) {
logger.warn("删除操作设置动态条件过滤是无效的,数据删除查询条件必须是精准的!");
}
EntityMeta entityMeta = getEntityMeta(entityClass);
String where = SqlUtil.convertFieldsToColumns(entityMeta, innerModel.where);
String sql = "delete from ".concat(entityMeta.getSchemaTable(null, null)).concat(" where ").concat(where);
SqlToyConfig sqlToyConfig = getSqlToyConfig(sql, SqlType.update);
QueryExecutor queryExecutor = null;
// :named 模式
if (SqlConfigParseUtils.hasNamedParam(where) && StringUtil.isBlank(innerModel.names)) {
queryExecutor = new QueryExecutor(sql, (Serializable) innerModel.values[0]);
} else {
queryExecutor = new QueryExecutor(sql).names(innerModel.names).values(innerModel.values);
}
if (innerModel.paramFilters != null && innerModel.paramFilters.size() > 0) {
queryExecutor.getInnerModel().paramFilters.addAll(innerModel.paramFilters);
}
// 分库分表策略
setEntitySharding(queryExecutor, entityMeta);
return dialectFactory.executeSql(sqlToyContext, sqlToyConfig, queryExecutor, null, null, getDataSource(innerModel.dataSource));
}
Aggregations