提交 707abaf3 编写于 作者: xignzi006's avatar xignzi006 🇨🇳

SaaS表达式中租户列补充

上级 2a8773f2
......@@ -6,10 +6,14 @@ package ${pub.getPKGCodeName()}.core.util.config;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.extension.plugins.tenant.TenantSqlParser;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Function;
import net.sf.jsqlparser.expression.*;
import net.sf.jsqlparser.expression.operators.arithmetic.Addition;
import net.sf.jsqlparser.expression.operators.arithmetic.Division;
import net.sf.jsqlparser.expression.operators.arithmetic.Multiplication;
import net.sf.jsqlparser.expression.operators.arithmetic.Subtraction;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.relational.*;
import net.sf.jsqlparser.schema.Column;
import net.sf.jsqlparser.schema.Table;
import net.sf.jsqlparser.statement.select.*;
......@@ -24,7 +28,9 @@ public class SaaSTenantSqlParser extends TenantSqlParser {
* @param plainSelect ignore
* @param addColumn 是否添加租户列,insert into select语句中需要
*/
@Override
protected void processPlainSelect(PlainSelect plainSelect, boolean addColumn) {
//处理selectItem表达式
processSelectItem(plainSelect);
FromItem fromItem = plainSelect.getFromItem();
if (fromItem instanceof Table) {
......@@ -32,10 +38,11 @@ public class SaaSTenantSqlParser extends TenantSqlParser {
if (!this.getTenantHandler().doTableFilter(fromTable.getName())) {
plainSelect.setWhere(builderExpression(plainSelect.getWhere(), fromTable));
if (addColumn) {
if (fromItem.getAlias() != null)
if (fromItem.getAlias() != null) {
plainSelect.getSelectItems().add(new SelectExpressionItem(new Column(fromItem.getAlias().getName() + StringPool.DOT + this.getTenantHandler().getTenantIdColumn())));
else
}else {
plainSelect.getSelectItems().add(new SelectExpressionItem(new Column(this.getTenantHandler().getTenantIdColumn())));
}
}
}
} else {
......@@ -48,27 +55,90 @@ public class SaaSTenantSqlParser extends TenantSqlParser {
processFromItem(j.getRightItem());
});
}
processWhere(plainSelect.getWhere());
//处理where表达式
processExcepression(plainSelect.getWhere());
}
/**
* where 条件中包含functionselect 添加租户id
* exception 表达式处理
*/
protected void processWhere(Expression where) {
if (where == null)
protected void processExcepression(Expression exception) {
if (exception == null) {
return;
if (where instanceof AndExpression) {
processWhere(((AndExpression) where).getLeftExpression());
processWhere(((AndExpression) where).getRightExpression());
} else if (where instanceof OrExpression) {
processWhere(((OrExpression) where).getLeftExpression());
processWhere(((OrExpression) where).getRightExpression());
} else if (where instanceof Function) {
for (Expression e : ((Function) where).getParameters().getExpressions()) {
if (e instanceof SubSelect) {
this.processSelectBody(((SubSelect) e).getSelectBody());
}else if (exception instanceof AndExpression) {
processExcepression(((AndExpression) exception).getLeftExpression());
processExcepression(((AndExpression) exception).getRightExpression());
} else if (exception instanceof OrExpression) {
processExcepression(((OrExpression) exception).getLeftExpression());
processExcepression(((OrExpression) exception).getRightExpression());
} else if (exception instanceof Parenthesis) {
processExcepression(((Parenthesis) exception).getExpression());
} else if (exception instanceof NotExpression) {
processExcepression(((NotExpression) exception).getExpression());
} else if (exception instanceof InExpression) {
processExcepression(((InExpression) exception).getLeftExpression());
if(((InExpression) exception).getRightItemsList() instanceof SubSelect) {
this.processSelectBody(((SubSelect) ((InExpression) exception).getRightItemsList()).getSelectBody());
}
} else if (exception instanceof EqualsTo) {
processExcepression(((EqualsTo) exception).getLeftExpression());
processExcepression(((EqualsTo) exception).getRightExpression());
} else if (exception instanceof ExistsExpression) {
processExcepression(((ExistsExpression) exception).getRightExpression());
} else if (exception instanceof GreaterThan) {
processExcepression(((GreaterThan) exception).getLeftExpression());
processExcepression(((GreaterThan) exception).getRightExpression());
} else if (exception instanceof GreaterThanEquals) {
processExcepression(((GreaterThanEquals) exception).getLeftExpression());
processExcepression(((GreaterThanEquals) exception).getRightExpression());
} else if (exception instanceof MinorThan) {
processExcepression(((MinorThan) exception).getLeftExpression());
processExcepression(((MinorThan) exception).getRightExpression());
} else if (exception instanceof MinorThanEquals) {
processExcepression(((MinorThanEquals) exception).getLeftExpression());
processExcepression(((MinorThanEquals) exception).getRightExpression());
} else if (exception instanceof NotEqualsTo) {
processExcepression(((NotEqualsTo) exception).getLeftExpression());
processExcepression(((NotEqualsTo) exception).getRightExpression());
} else if (exception instanceof IsBooleanExpression) {
processExcepression(((IsBooleanExpression) exception).getLeftExpression());
} else if (exception instanceof IsNullExpression) {
processExcepression(((IsNullExpression) exception).getLeftExpression());
} else if (exception instanceof LikeExpression) {
processExcepression(((LikeExpression) exception).getLeftExpression());
processExcepression(((LikeExpression) exception).getRightExpression());
} else if (exception instanceof Between) {
processExcepression(((Between) exception).getLeftExpression());
processExcepression(((Between) exception).getBetweenExpressionStart());
processExcepression(((Between) exception).getBetweenExpressionEnd());
} else if (exception instanceof Function) {
if(null != ((Function) exception).getParameters()) {
for (Expression e : ((Function) exception).getParameters().getExpressions()) {
processExcepression(e);
}
}
} else if (exception instanceof CaseExpression) {
CaseExpression caseExpression = (CaseExpression) exception;
processExcepression(caseExpression.getElseExpression());
processExcepression(caseExpression.getSwitchExpression());
for (WhenClause whenClause : caseExpression.getWhenClauses()) {
processExcepression(whenClause.getWhenExpression());
processExcepression(whenClause.getThenExpression());
}
}else if(exception instanceof Subtraction){
processExcepression(((Subtraction) exception).getLeftExpression());
processExcepression(((Subtraction) exception).getRightExpression());
}else if(exception instanceof Multiplication){
processExcepression(((Multiplication) exception).getLeftExpression());
processExcepression(((Multiplication) exception).getRightExpression());
}else if(exception instanceof Addition){
processExcepression(((Addition) exception).getLeftExpression());
processExcepression(((Addition) exception).getRightExpression());
}else if(exception instanceof Division){
processExcepression(((Division) exception).getLeftExpression());
processExcepression(((Division) exception).getRightExpression());
} else if (exception instanceof SubSelect) {
this.processSelectBody(((SubSelect) exception).getSelectBody());
}
}
......@@ -79,18 +149,10 @@ public class SaaSTenantSqlParser extends TenantSqlParser {
List<SelectItem> selectItems = plainSelect.getSelectItems();
for (SelectItem selectItem : selectItems) {
if (selectItem instanceof SelectExpressionItem) {
if(((SelectExpressionItem) selectItem).getExpression() instanceof SubSelect){
this.processSelectBody(((SubSelect) ((SelectExpressionItem) selectItem).getExpression()).getSelectBody());
}
} else if (selectItem instanceof Function) {
for (Expression e : ((Function) selectItem).getParameters().getExpressions()) {
if (e instanceof SubSelect) {
this.processSelectBody(((SubSelect) e).getSelectBody());
}
}
Expression selectExcepression = ((SelectExpressionItem) selectItem).getExpression();
processExcepression(selectExcepression);
}
}
}
}
</#if>
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册