fix: SQL 条件表达式缺少括号问题

This commit is contained in:
gaibu 2022-12-13 10:37:41 +08:00
parent 99338182b2
commit 453603b6ad
3 changed files with 51 additions and 3 deletions

View File

@ -7,6 +7,7 @@ import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils; import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.datapermission.core.rule.DataPermissionRule; import cn.iocoder.yudao.framework.datapermission.core.rule.DataPermissionRule;
import cn.iocoder.yudao.framework.expression.OrExpressionX;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils; import cn.iocoder.yudao.framework.mybatis.core.util.MyBatisUtils;
import cn.iocoder.yudao.framework.security.core.LoginUser; import cn.iocoder.yudao.framework.security.core.LoginUser;
@ -20,7 +21,6 @@ import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.Expression; import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue; import net.sf.jsqlparser.expression.LongValue;
import net.sf.jsqlparser.expression.NullValue; import net.sf.jsqlparser.expression.NullValue;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo; import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList; import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.InExpression; import net.sf.jsqlparser.expression.operators.relational.InExpression;
@ -143,8 +143,8 @@ public class DeptDataPermissionRule implements DataPermissionRule {
if (userExpression == null) { if (userExpression == null) {
return deptExpression; return deptExpression;
} }
// 目前如果有指定部门 + 可查看自己采用 OR 条件WHERE dept_id IN ? OR user_id = ? // 目前如果有指定部门 + 可查看自己采用 OR 条件WHERE (dept_id IN ? OR user_id = ?)
return new OrExpression(deptExpression, userExpression); return new OrExpressionX(deptExpression, userExpression);
} }
private Expression buildDeptExpression(String tableName, Alias tableAlias, Set<Long> deptIds) { private Expression buildDeptExpression(String tableName, Alias tableAlias, Set<Long> deptIds) {

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.framework.expression;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
/**
* AndExpression 的扩展类(会在原有表达式两端加上括号)
*/
public class AndExpressionX extends AndExpression {
public AndExpressionX() {
}
public AndExpressionX(Expression leftExpression, Expression rightExpression) {
this.setLeftExpression(leftExpression);
this.setRightExpression(rightExpression);
}
@Override
public String toString() {
return "(" + this.getLeftExpression() + " " + this.getStringExpression() + " " + this.getRightExpression() + ")";
}
}

View File

@ -0,0 +1,24 @@
package cn.iocoder.yudao.framework.expression;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
/**
* OrExpression 的扩展类(会在原有表达式两端加上括号)
*/
public class OrExpressionX extends OrExpression {
public OrExpressionX() {
}
public OrExpressionX(Expression leftExpression, Expression rightExpression) {
this.setLeftExpression(leftExpression);
this.setRightExpression(rightExpression);
}
@Override
public String toString() {
return "(" + this.getLeftExpression() + " " + this.getStringExpression() + " " + this.getRightExpression() + ")";
}
}