fix(数据集): 修复SQL数据集参数识别错误

This commit is contained in:
taojinlong 2024-03-07 16:16:18 +08:00
parent a074b87a6e
commit ec5cec2e21

View File

@ -62,10 +62,7 @@ import io.dataease.service.datasource.DatasourceService;
import io.dataease.service.engine.EngineService; import io.dataease.service.engine.EngineService;
import io.dataease.service.sys.SysAuthService; import io.dataease.service.sys.SysAuthService;
import lombok.Data; import lombok.Data;
import net.sf.jsqlparser.expression.Alias; import net.sf.jsqlparser.expression.*;
import net.sf.jsqlparser.expression.BinaryExpression;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.Parenthesis;
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.expression.operators.relational.*;
@ -1185,7 +1182,6 @@ public class DataSetTableService {
binaryExpression = (BinaryExpression) expr; binaryExpression = (BinaryExpression) expr;
} catch (Exception e) { } catch (Exception e) {
} }
if (binaryExpression != null) { if (binaryExpression != null) {
boolean hasSubBinaryExpression = binaryExpression instanceof AndExpression || binaryExpression instanceof OrExpression; boolean hasSubBinaryExpression = binaryExpression instanceof AndExpression || binaryExpression instanceof OrExpression;
if (!hasSubBinaryExpression && !(binaryExpression.getLeftExpression() instanceof BinaryExpression) && !(binaryExpression.getLeftExpression() instanceof InExpression) && (hasVariable(binaryExpression.getLeftExpression().toString()) || hasVariable(binaryExpression.getRightExpression().toString()))) { if (!hasSubBinaryExpression && !(binaryExpression.getLeftExpression() instanceof BinaryExpression) && !(binaryExpression.getLeftExpression() instanceof InExpression) && (hasVariable(binaryExpression.getLeftExpression().toString()) || hasVariable(binaryExpression.getRightExpression().toString()))) {
@ -2832,28 +2828,43 @@ public class DataSetTableService {
private void visitBinaryExpr(BinaryExpression expr, String operator) { private void visitBinaryExpr(BinaryExpression expr, String operator) {
boolean hasSubBinaryExpression = false; boolean hasSubBinaryExpression = false;
try { if(expr.getLeftExpression() instanceof Parenthesis){
BinaryExpression leftBinaryExpression = (BinaryExpression) expr.getLeftExpression(); Parenthesis parenthesis = (Parenthesis)expr.getLeftExpression();
BinaryExpression leftBinaryExpression = (BinaryExpression)parenthesis.getExpression();
hasSubBinaryExpression = leftBinaryExpression instanceof AndExpression || leftBinaryExpression instanceof OrExpression; hasSubBinaryExpression = leftBinaryExpression instanceof AndExpression || leftBinaryExpression instanceof OrExpression;
} catch (Exception e) { }
e.printStackTrace(); if(expr.getLeftExpression() instanceof BinaryExpression){
try {
BinaryExpression leftBinaryExpression = (BinaryExpression) expr.getLeftExpression();
hasSubBinaryExpression = leftBinaryExpression instanceof AndExpression || leftBinaryExpression instanceof OrExpression;
} catch (Exception e) {
e.printStackTrace();
}
} }
if (expr.getLeftExpression() instanceof BinaryExpression && !hasSubBinaryExpression && hasVariable(expr.getLeftExpression().toString())) { if ((expr.getLeftExpression() instanceof BinaryExpression || expr.getLeftExpression() instanceof Parenthesis) && !hasSubBinaryExpression && hasVariable(expr.getLeftExpression().toString())) {
getBuffer().append(SubstitutedSql); getBuffer().append(SubstitutedSql);
} else { } else {
expr.getLeftExpression().accept(this); expr.getLeftExpression().accept(this);
} }
getBuffer().append(" " + operator + " "); getBuffer().append(" " + operator + " ");
hasSubBinaryExpression = false; hasSubBinaryExpression = false;
try { if(expr.getRightExpression() instanceof Parenthesis){
BinaryExpression rightBinaryExpression = (BinaryExpression) expr.getRightExpression(); Parenthesis parenthesis = (Parenthesis)expr.getRightExpression();
BinaryExpression rightBinaryExpression = (BinaryExpression)parenthesis.getExpression();
hasSubBinaryExpression = rightBinaryExpression instanceof AndExpression || rightBinaryExpression instanceof OrExpression; hasSubBinaryExpression = rightBinaryExpression instanceof AndExpression || rightBinaryExpression instanceof OrExpression;
;
} catch (Exception e) {
} }
if (expr.getRightExpression() instanceof BinaryExpression && !hasSubBinaryExpression && hasVariable(expr.getRightExpression().toString())) { if(expr.getRightExpression() instanceof BinaryExpression){
try {
BinaryExpression rightBinaryExpression = (BinaryExpression) expr.getRightExpression();
hasSubBinaryExpression = rightBinaryExpression instanceof AndExpression || rightBinaryExpression instanceof OrExpression;
} catch (Exception e) {
}
}
if ((expr.getRightExpression() instanceof Parenthesis || expr.getRightExpression() instanceof BinaryExpression || expr.getRightExpression() instanceof Function) && !hasSubBinaryExpression && hasVariable(expr.getRightExpression().toString())) {
getBuffer().append(SubstitutedSql); getBuffer().append(SubstitutedSql);
} else { } else {
expr.getRightExpression().accept(this); expr.getRightExpression().accept(this);