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

SaaS表达式中租户列补充

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