【fix sonar】QueryGenerator文件
This commit is contained in:
+38
-28
@@ -12,6 +12,7 @@ import java.util.regex.Matcher;
|
|||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
import lombok.val;
|
||||||
import org.apache.commons.beanutils.PropertyUtils;
|
import org.apache.commons.beanutils.PropertyUtils;
|
||||||
import com.jero.common.constant.CommonConstant;
|
import com.jero.common.constant.CommonConstant;
|
||||||
import com.jero.common.constant.DataBaseConstant;
|
import com.jero.common.constant.DataBaseConstant;
|
||||||
@@ -22,6 +23,8 @@ import com.jero.common.util.CommonUtils;
|
|||||||
import com.jero.common.util.DateUtils;
|
import com.jero.common.util.DateUtils;
|
||||||
import com.jero.common.util.SqlInjectionUtil;
|
import com.jero.common.util.SqlInjectionUtil;
|
||||||
import com.jero.common.util.oConvertUtils;
|
import com.jero.common.util.oConvertUtils;
|
||||||
|
import org.apache.commons.collections4.CollectionUtils;
|
||||||
|
import org.apache.poi.ss.formula.functions.T;
|
||||||
import org.springframework.util.NumberUtils;
|
import org.springframework.util.NumberUtils;
|
||||||
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
@@ -33,6 +36,11 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class QueryGenerator {
|
public class QueryGenerator {
|
||||||
|
|
||||||
|
private QueryGenerator(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
public static final String SQL_RULES_COLUMN = "SQL_RULES_COLUMN";
|
public static final String SQL_RULES_COLUMN = "SQL_RULES_COLUMN";
|
||||||
|
|
||||||
private static final String BEGIN = "_begin";
|
private static final String BEGIN = "_begin";
|
||||||
@@ -66,7 +74,7 @@ public class QueryGenerator {
|
|||||||
public static final String LIKE_MYSQL_SPECIAL_STRS = "_,%";
|
public static final String LIKE_MYSQL_SPECIAL_STRS = "_,%";
|
||||||
|
|
||||||
/**时间格式化 */
|
/**时间格式化 */
|
||||||
private static final ThreadLocal<SimpleDateFormat> local = new ThreadLocal<SimpleDateFormat>();
|
private static final ThreadLocal<SimpleDateFormat> local = new ThreadLocal<>();
|
||||||
private static SimpleDateFormat getTime(){
|
private static SimpleDateFormat getTime(){
|
||||||
SimpleDateFormat time = local.get();
|
SimpleDateFormat time = local.get();
|
||||||
if(time == null){
|
if(time == null){
|
||||||
@@ -76,6 +84,10 @@ public class QueryGenerator {
|
|||||||
return time;
|
return time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void unload() {
|
||||||
|
local.remove();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取查询条件构造器QueryWrapper实例 通用查询条件已被封装完成
|
* 获取查询条件构造器QueryWrapper实例 通用查询条件已被封装完成
|
||||||
* @param searchObj 查询实体
|
* @param searchObj 查询实体
|
||||||
@@ -84,7 +96,7 @@ public class QueryGenerator {
|
|||||||
*/
|
*/
|
||||||
public static <T> QueryWrapper<T> initQueryWrapper(T searchObj,Map<String, String[]> parameterMap){
|
public static <T> QueryWrapper<T> initQueryWrapper(T searchObj,Map<String, String[]> parameterMap){
|
||||||
long start = System.currentTimeMillis();
|
long start = System.currentTimeMillis();
|
||||||
QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
|
QueryWrapper<T> queryWrapper = new QueryWrapper<>();
|
||||||
installMplus(queryWrapper, searchObj, parameterMap);
|
installMplus(queryWrapper, searchObj, parameterMap);
|
||||||
log.debug("---查询条件构造器初始化完成,耗时:"+(System.currentTimeMillis()-start)+"毫秒----");
|
log.debug("---查询条件构造器初始化完成,耗时:"+(System.currentTimeMillis()-start)+"毫秒----");
|
||||||
return queryWrapper;
|
return queryWrapper;
|
||||||
@@ -121,7 +133,7 @@ public class QueryGenerator {
|
|||||||
String name, type, column;
|
String name, type, column;
|
||||||
// update-begin--Author:taoyan Date:20200923 for:issues/1671 如果字段加注解了@TableField(exist = false),不走DB查询-------
|
// update-begin--Author:taoyan Date:20200923 for:issues/1671 如果字段加注解了@TableField(exist = false),不走DB查询-------
|
||||||
//定义实体字段和数据库字段名称的映射 高级查询中 只能获取实体字段 如果设置TableField注解 那么查询条件会出问题
|
//定义实体字段和数据库字段名称的映射 高级查询中 只能获取实体字段 如果设置TableField注解 那么查询条件会出问题
|
||||||
Map<String,String> fieldColumnMap = new HashMap<String,String>();
|
Map<String,String> fieldColumnMap = new HashMap<>();
|
||||||
for (int i = 0; i < origDescriptors.length; i++) {
|
for (int i = 0; i < origDescriptors.length; i++) {
|
||||||
//aliasName = origDescriptors[i].getName(); mybatis 不存在实体属性 不用处理别名的情况
|
//aliasName = origDescriptors[i].getName(); mybatis 不存在实体属性 不用处理别名的情况
|
||||||
name = origDescriptors[i].getName();
|
name = origDescriptors[i].getName();
|
||||||
@@ -156,7 +168,6 @@ public class QueryGenerator {
|
|||||||
for (int k=1;k<vals.length;k++) {
|
for (int k=1;k<vals.length;k++) {
|
||||||
j = j.or().like(field,vals[k]);
|
j = j.or().like(field,vals[k]);
|
||||||
}
|
}
|
||||||
//return j;
|
|
||||||
});
|
});
|
||||||
}else {
|
}else {
|
||||||
queryWrapper.and(j -> j.like(field,vals[0]));
|
queryWrapper.and(j -> j.like(field,vals[0]));
|
||||||
@@ -226,7 +237,7 @@ public class QueryGenerator {
|
|||||||
}
|
}
|
||||||
if (oConvertUtils.isNotEmpty(column) && oConvertUtils.isNotEmpty(order)) {
|
if (oConvertUtils.isNotEmpty(column) && oConvertUtils.isNotEmpty(order)) {
|
||||||
//字典字段,去掉字典翻译文本后缀
|
//字典字段,去掉字典翻译文本后缀
|
||||||
if(column.endsWith(CommonConstant.DICT_TEXT_SUFFIX)) {
|
if(column != null && column.endsWith(CommonConstant.DICT_TEXT_SUFFIX)) {
|
||||||
column = column.substring(0, column.lastIndexOf(CommonConstant.DICT_TEXT_SUFFIX));
|
column = column.substring(0, column.lastIndexOf(CommonConstant.DICT_TEXT_SUFFIX));
|
||||||
}
|
}
|
||||||
//SQL注入check
|
//SQL注入check
|
||||||
@@ -236,7 +247,7 @@ public class QueryGenerator {
|
|||||||
// 排序规则修改
|
// 排序规则修改
|
||||||
// 将现有排序 _ 前端传递排序条件{....,column: 'column1,column2',order: 'desc'} 翻译成sql "column1,column2 desc"
|
// 将现有排序 _ 前端传递排序条件{....,column: 'column1,column2',order: 'desc'} 翻译成sql "column1,column2 desc"
|
||||||
// 修改为 _ 前端传递排序条件{....,column: 'column1,column2',order: 'desc'} 翻译成sql "column1 desc,column2 desc"
|
// 修改为 _ 前端传递排序条件{....,column: 'column1,column2',order: 'desc'} 翻译成sql "column1 desc,column2 desc"
|
||||||
if (order.toUpperCase().indexOf(ORDER_TYPE_ASC)>=0) {
|
if (order != null && order.toUpperCase().contains(ORDER_TYPE_ASC)) {
|
||||||
String columnStr = oConvertUtils.camelToUnderline(column);
|
String columnStr = oConvertUtils.camelToUnderline(column);
|
||||||
String[] columnArray = columnStr.split(",");
|
String[] columnArray = columnStr.split(",");
|
||||||
queryWrapper.orderByAsc(Arrays.asList(columnArray));
|
queryWrapper.orderByAsc(Arrays.asList(columnArray));
|
||||||
@@ -264,7 +275,7 @@ public class QueryGenerator {
|
|||||||
try {
|
try {
|
||||||
superQueryParams = URLDecoder.decode(superQueryParams, "UTF-8");
|
superQueryParams = URLDecoder.decode(superQueryParams, "UTF-8");
|
||||||
List<QueryCondition> conditions = JSON.parseArray(superQueryParams, QueryCondition.class);
|
List<QueryCondition> conditions = JSON.parseArray(superQueryParams, QueryCondition.class);
|
||||||
if (conditions == null || conditions.size() == 0) {
|
if (conditions == null || conditions.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
log.info("---高级查询参数-->" + conditions.toString());
|
log.info("---高级查询参数-->" + conditions.toString());
|
||||||
@@ -293,7 +304,6 @@ public class QueryGenerator {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//return andWrapper;
|
|
||||||
});
|
});
|
||||||
} catch (UnsupportedEncodingException e) {
|
} catch (UnsupportedEncodingException e) {
|
||||||
log.error("--高级查询参数转码失败:" + superQueryParams, e);
|
log.error("--高级查询参数转码失败:" + superQueryParams, e);
|
||||||
@@ -303,7 +313,6 @@ public class QueryGenerator {
|
|||||||
}
|
}
|
||||||
// update-end--Author:sunjianlei Date:20200325 for:高级查询的条件要用括号括起来,防止和用户的其他条件冲突 -------
|
// update-end--Author:sunjianlei Date:20200325 for:高级查询的条件要用括号括起来,防止和用户的其他条件冲突 -------
|
||||||
}
|
}
|
||||||
//log.info(" superQuery getCustomSqlSegment: "+ queryWrapper.getCustomSqlSegment());
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 根据所传的值 转化成对应的比较方式
|
* 根据所传的值 转化成对应的比较方式
|
||||||
@@ -359,7 +368,7 @@ public class QueryGenerator {
|
|||||||
rule = QueryRuleEnum.NE;
|
rule = QueryRuleEnum.NE;
|
||||||
}
|
}
|
||||||
// step 6 xx+xx+xx 这种情况适用于如果想要用逗号作精确查询 但是系统默认逗号走in 所以可以用++替换【此逻辑作废】
|
// step 6 xx+xx+xx 这种情况适用于如果想要用逗号作精确查询 但是系统默认逗号走in 所以可以用++替换【此逻辑作废】
|
||||||
if(rule == null && val.indexOf(QUERY_COMMA_ESCAPE)>0){
|
if(rule == null && val.contains(QUERY_COMMA_ESCAPE)){
|
||||||
rule = QueryRuleEnum.EQ_WITH_ADD;
|
rule = QueryRuleEnum.EQ_WITH_ADD;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -403,7 +412,7 @@ public class QueryGenerator {
|
|||||||
} else if (rule == QueryRuleEnum.IN) {
|
} else if (rule == QueryRuleEnum.IN) {
|
||||||
value = val.split(",");
|
value = val.split(",");
|
||||||
} else if (rule == QueryRuleEnum.EQ_WITH_ADD) {
|
} else if (rule == QueryRuleEnum.EQ_WITH_ADD) {
|
||||||
value = val.replaceAll("\\+\\+", COMMA);
|
value = val.replace("\\+\\+", COMMA);
|
||||||
}else {
|
}else {
|
||||||
//update-begin--Author:scott Date:20190724 for:initQueryWrapper组装sql查询条件错误 #284-------------------
|
//update-begin--Author:scott Date:20190724 for:initQueryWrapper组装sql查询条件错误 #284-------------------
|
||||||
if(val.startsWith(rule.getValue())){
|
if(val.startsWith(rule.getValue())){
|
||||||
@@ -551,7 +560,7 @@ public class QueryGenerator {
|
|||||||
private static boolean judgedIsUselessField(String name) {
|
private static boolean judgedIsUselessField(String name) {
|
||||||
return "class".equals(name) || "ids".equals(name)
|
return "class".equals(name) || "ids".equals(name)
|
||||||
|| "page".equals(name) || "rows".equals(name)
|
|| "page".equals(name) || "rows".equals(name)
|
||||||
|| "sort".equals(name) || "order".equals(name);
|
|| "sort".equals(name) || ORDER_TYPE.equals(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -561,9 +570,9 @@ public class QueryGenerator {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static Map<String, SysPermissionDataRuleModel> getRuleMap() {
|
public static Map<String, SysPermissionDataRuleModel> getRuleMap() {
|
||||||
Map<String, SysPermissionDataRuleModel> ruleMap = new HashMap<String, SysPermissionDataRuleModel>();
|
Map<String, SysPermissionDataRuleModel> ruleMap = new HashMap<>();
|
||||||
List<SysPermissionDataRuleModel> list = JeroDataAutorUtils.loadDataSearchConditon();
|
List<SysPermissionDataRuleModel> list = JeroDataAutorUtils.loadDataSearchConditon();
|
||||||
if(list != null&&list.size()>0){
|
if(list != null&&!list.isEmpty()){
|
||||||
if(list.get(0)==null){
|
if(list.get(0)==null){
|
||||||
return ruleMap;
|
return ruleMap;
|
||||||
}
|
}
|
||||||
@@ -583,11 +592,11 @@ public class QueryGenerator {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static Map<String, SysPermissionDataRuleModel> getRuleMap(List<SysPermissionDataRuleModel> list) {
|
public static Map<String, SysPermissionDataRuleModel> getRuleMap(List<SysPermissionDataRuleModel> list) {
|
||||||
Map<String, SysPermissionDataRuleModel> ruleMap = new HashMap<String, SysPermissionDataRuleModel>();
|
Map<String, SysPermissionDataRuleModel> ruleMap = new HashMap<>();
|
||||||
if(list==null){
|
if(list==null){
|
||||||
list = JeroDataAutorUtils.loadDataSearchConditon();
|
list = JeroDataAutorUtils.loadDataSearchConditon();
|
||||||
}
|
}
|
||||||
if(list != null&&list.size()>0){
|
if(list != null&&!list.isEmpty()){
|
||||||
if(list.get(0)==null){
|
if(list.get(0)==null){
|
||||||
return ruleMap;
|
return ruleMap;
|
||||||
}
|
}
|
||||||
@@ -672,7 +681,7 @@ public class QueryGenerator {
|
|||||||
if(oConvertUtils.isEmpty(sql)){
|
if(oConvertUtils.isEmpty(sql)){
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
Set<String> varParams = new HashSet<String>();
|
Set<String> varParams = new HashSet<>();
|
||||||
String regex = "\\#\\{\\w+\\}";
|
String regex = "\\#\\{\\w+\\}";
|
||||||
|
|
||||||
Pattern p = Pattern.compile(regex);
|
Pattern p = Pattern.compile(regex);
|
||||||
@@ -725,6 +734,7 @@ public class QueryGenerator {
|
|||||||
*/
|
*/
|
||||||
public static String getSingleSqlByRule(QueryRuleEnum rule,String field,Object value,boolean isString, String dataBaseType) {
|
public static String getSingleSqlByRule(QueryRuleEnum rule,String field,Object value,boolean isString, String dataBaseType) {
|
||||||
String res = "";
|
String res = "";
|
||||||
|
String like = " like ";
|
||||||
switch (rule) {
|
switch (rule) {
|
||||||
case GT:
|
case GT:
|
||||||
res =field+rule.getValue()+getFieldConditionValue(value, isString, dataBaseType);
|
res =field+rule.getValue()+getFieldConditionValue(value, isString, dataBaseType);
|
||||||
@@ -751,13 +761,13 @@ public class QueryGenerator {
|
|||||||
res = field + " in "+getInConditionValue(value, isString);
|
res = field + " in "+getInConditionValue(value, isString);
|
||||||
break;
|
break;
|
||||||
case LIKE:
|
case LIKE:
|
||||||
res = field + " like "+getLikeConditionValue(value);
|
res = field + like+getLikeConditionValue(value);
|
||||||
break;
|
break;
|
||||||
case LEFT_LIKE:
|
case LEFT_LIKE:
|
||||||
res = field + " like "+getLikeConditionValue(value);
|
res = field + like+getLikeConditionValue(value);
|
||||||
break;
|
break;
|
||||||
case RIGHT_LIKE:
|
case RIGHT_LIKE:
|
||||||
res = field + " like "+getLikeConditionValue(value);
|
res = field + like+getLikeConditionValue(value);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
res = field+" = "+getFieldConditionValue(value, isString, dataBaseType);
|
res = field+" = "+getFieldConditionValue(value, isString, dataBaseType);
|
||||||
@@ -798,8 +808,8 @@ public class QueryGenerator {
|
|||||||
str = str.substring(1);
|
str = str.substring(1);
|
||||||
}else if(str.startsWith("<")) {
|
}else if(str.startsWith("<")) {
|
||||||
str = str.substring(1);
|
str = str.substring(1);
|
||||||
}else if(str.indexOf(QUERY_COMMA_ESCAPE)>0) {
|
}else if(str.contains(QUERY_COMMA_ESCAPE)) {
|
||||||
str = str.replaceAll("\\+\\+", COMMA);
|
str = str.replace("\\+\\+", COMMA);
|
||||||
}
|
}
|
||||||
if(dataBaseType==null){
|
if(dataBaseType==null){
|
||||||
dataBaseType = getDbType();
|
dataBaseType = getDbType();
|
||||||
@@ -822,12 +832,12 @@ public class QueryGenerator {
|
|||||||
private static String getInConditionValue(Object value,boolean isString) {
|
private static String getInConditionValue(Object value,boolean isString) {
|
||||||
if(isString) {
|
if(isString) {
|
||||||
String temp[] = value.toString().split(",");
|
String temp[] = value.toString().split(",");
|
||||||
String res="";
|
StringBuilder res= new StringBuilder();
|
||||||
for (String string : temp) {
|
for (String string : temp) {
|
||||||
if(DataBaseConstant.DB_TYPE_SQLSERVER.equals(getDbType())){
|
if(DataBaseConstant.DB_TYPE_SQLSERVER.equals(getDbType())){
|
||||||
res+=",N'"+string+"'";
|
res.append(",N'").append(string).append("'");
|
||||||
}else{
|
}else{
|
||||||
res+=",'"+string+"'";
|
res.append(",'").append(string).append("'");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return "("+res.substring(1)+")";
|
return "("+res.substring(1)+")";
|
||||||
@@ -888,7 +898,7 @@ public class QueryGenerator {
|
|||||||
*/
|
*/
|
||||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||||
public static String installAuthJdbc(Class<?> clazz) {
|
public static String installAuthJdbc(Class<?> clazz) {
|
||||||
StringBuffer sb = new StringBuffer();
|
StringBuilder sb = new StringBuilder();
|
||||||
//权限查询
|
//权限查询
|
||||||
Map<String,SysPermissionDataRuleModel> ruleMap = getRuleMap();
|
Map<String,SysPermissionDataRuleModel> ruleMap = getRuleMap();
|
||||||
PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(clazz);
|
PropertyDescriptor origDescriptors[] = PropertyUtils.getPropertyDescriptors(clazz);
|
||||||
@@ -1019,7 +1029,7 @@ public class QueryGenerator {
|
|||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
private static List<Field> getClassFields(Class<?> clazz) {
|
private static List<Field> getClassFields(Class<?> clazz) {
|
||||||
List<Field> list = new ArrayList<Field>();
|
List<Field> list = new ArrayList<>();
|
||||||
Field[] fields;
|
Field[] fields;
|
||||||
do{
|
do{
|
||||||
fields = clazz.getDeclaredFields();
|
fields = clazz.getDeclaredFields();
|
||||||
@@ -1051,7 +1061,7 @@ public class QueryGenerator {
|
|||||||
if (field == null) {
|
if (field == null) {
|
||||||
List<Field> allFields = getClassFields(clazz);
|
List<Field> allFields = getClassFields(clazz);
|
||||||
List<Field> searchFields = allFields.stream().filter(a -> a.getName().equals(name)).collect(Collectors.toList());
|
List<Field> searchFields = allFields.stream().filter(a -> a.getName().equals(name)).collect(Collectors.toList());
|
||||||
if(searchFields!=null && searchFields.size()>0){
|
if(!CollectionUtils.isEmpty(searchFields)){
|
||||||
field = searchFields.get(0);
|
field = searchFields.get(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user