From ec5cec2e21435c6723d981f64f8446e1caa63546 Mon Sep 17 00:00:00 2001 From: taojinlong Date: Thu, 7 Mar 2024 16:16:18 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=95=B0=E6=8D=AE=E9=9B=86):=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8DSQL=E6=95=B0=E6=8D=AE=E9=9B=86=E5=8F=82=E6=95=B0?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/dataset/DataSetTableService.java | 41 ++++++++++++------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/core/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java b/core/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java index 02c8943477..7f9ffb7585 100644 --- a/core/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java +++ b/core/backend/src/main/java/io/dataease/service/dataset/DataSetTableService.java @@ -62,10 +62,7 @@ import io.dataease.service.datasource.DatasourceService; import io.dataease.service.engine.EngineService; import io.dataease.service.sys.SysAuthService; import lombok.Data; -import net.sf.jsqlparser.expression.Alias; -import net.sf.jsqlparser.expression.BinaryExpression; -import net.sf.jsqlparser.expression.Expression; -import net.sf.jsqlparser.expression.Parenthesis; +import net.sf.jsqlparser.expression.*; import net.sf.jsqlparser.expression.operators.conditional.AndExpression; import net.sf.jsqlparser.expression.operators.conditional.OrExpression; import net.sf.jsqlparser.expression.operators.relational.*; @@ -1185,7 +1182,6 @@ public class DataSetTableService { binaryExpression = (BinaryExpression) expr; } catch (Exception e) { } - if (binaryExpression != null) { 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()))) { @@ -2832,28 +2828,43 @@ public class DataSetTableService { private void visitBinaryExpr(BinaryExpression expr, String operator) { boolean hasSubBinaryExpression = false; - try { - BinaryExpression leftBinaryExpression = (BinaryExpression) expr.getLeftExpression(); + if(expr.getLeftExpression() instanceof Parenthesis){ + Parenthesis parenthesis = (Parenthesis)expr.getLeftExpression(); + BinaryExpression leftBinaryExpression = (BinaryExpression)parenthesis.getExpression(); 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); } else { expr.getLeftExpression().accept(this); } getBuffer().append(" " + operator + " "); + hasSubBinaryExpression = false; - try { - BinaryExpression rightBinaryExpression = (BinaryExpression) expr.getRightExpression(); + if(expr.getRightExpression() instanceof Parenthesis){ + Parenthesis parenthesis = (Parenthesis)expr.getRightExpression(); + BinaryExpression rightBinaryExpression = (BinaryExpression)parenthesis.getExpression(); 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); } else { expr.getRightExpression().accept(this);