This is an automated email from the ASF dual-hosted git repository.

sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 8f84b039705 Fix sonar issue in MySQLStatementVisitor (#27306)
8f84b039705 is described below

commit 8f84b039705aabc502be9616d2d6cf6ac24aaf5a
Author: Zhengqiang Duan <[email protected]>
AuthorDate: Wed Jul 19 19:43:30 2023 +0800

    Fix sonar issue in MySQLStatementVisitor (#27306)
---
 .../visitor/statement/MySQLStatementVisitor.java   | 45 ++++++++--------------
 1 file changed, 15 insertions(+), 30 deletions(-)

diff --git 
a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
 
b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
index 3392f8a3cdb..ec689174684 100644
--- 
a/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
+++ 
b/parser/sql/dialect/mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/statement/MySQLStatementVisitor.java
@@ -1619,7 +1619,7 @@ public abstract class MySQLStatementVisitor extends 
MySQLStatementBaseVisitor<AS
             return createShorthandProjection(ctx.qualifiedShorthand());
         }
         AliasSegment alias = null == ctx.alias() ? null : (AliasSegment) 
visit(ctx.alias());
-        ASTNode exprProjection = visit(ctx.expr());
+        ExpressionSegment exprProjection = (ExpressionSegment) 
visit(ctx.expr());
         if (exprProjection instanceof ColumnSegment) {
             ColumnProjectionSegment result = new 
ColumnProjectionSegment((ColumnSegment) exprProjection);
             result.setAlias(alias);
@@ -1659,7 +1659,7 @@ public abstract class MySQLStatementVisitor extends 
MySQLStatementBaseVisitor<AS
         return new AliasSegment(ctx.start.getStartIndex(), 
ctx.stop.getStopIndex(), new IdentifierValue(ctx.textOrIdentifier().getText()));
     }
     
-    private ASTNode createProjection(final ProjectionContext ctx, final 
AliasSegment alias, final ASTNode projection) {
+    private ASTNode createProjection(final ProjectionContext ctx, final 
AliasSegment alias, final ExpressionSegment projection) {
         if (projection instanceof AggregationProjectionSegment) {
             ((AggregationProjectionSegment) projection).setAlias(alias);
             return projection;
@@ -1682,7 +1682,7 @@ public abstract class MySQLStatementVisitor extends 
MySQLStatementBaseVisitor<AS
         }
         // FIXME :For DISTINCT()
         if (projection instanceof ColumnSegment) {
-            ExpressionProjectionSegment result = new 
ExpressionProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), 
getOriginalText(ctx), (ColumnSegment) projection);
+            ExpressionProjectionSegment result = new 
ExpressionProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), 
getOriginalText(ctx), projection);
             result.setAlias(alias);
             return result;
         }
@@ -1694,9 +1694,9 @@ public abstract class MySQLStatementVisitor extends 
MySQLStatementBaseVisitor<AS
             return result;
         }
         if (projection instanceof BinaryOperationExpression) {
-            int startIndex = ((BinaryOperationExpression) 
projection).getStartIndex();
-            int stopIndex = null != alias ? alias.getStopIndex() : 
((BinaryOperationExpression) projection).getStopIndex();
-            ExpressionProjectionSegment result = new 
ExpressionProjectionSegment(startIndex, stopIndex, ((BinaryOperationExpression) 
projection).getText(), (BinaryOperationExpression) projection);
+            int startIndex = projection.getStartIndex();
+            int stopIndex = null != alias ? alias.getStopIndex() : 
projection.getStopIndex();
+            ExpressionProjectionSegment result = new 
ExpressionProjectionSegment(startIndex, stopIndex, projection.getText(), 
projection);
             result.setAlias(alias);
             return result;
         }
@@ -1705,30 +1705,9 @@ public abstract class MySQLStatementVisitor extends 
MySQLStatementBaseVisitor<AS
             result.setAlias(alias);
             return projection;
         }
-        if (projection instanceof CaseWhenExpression) {
-            ExpressionProjectionSegment result = new 
ExpressionProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), 
getOriginalText(ctx.expr()), (CaseWhenExpression) projection);
-            result.setAlias(alias);
-            return result;
-        }
-        if (projection instanceof VariableSegment) {
-            ExpressionProjectionSegment result = new 
ExpressionProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), 
getOriginalText(ctx.expr()), (VariableSegment) projection);
-            result.setAlias(alias);
-            return result;
-        }
-        if (projection instanceof BetweenExpression) {
-            ExpressionProjectionSegment result = new 
ExpressionProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), 
getOriginalText(ctx.expr()), (BetweenExpression) projection);
-            result.setAlias(alias);
-            return result;
-        }
-        if (projection instanceof InExpression) {
-            ExpressionProjectionSegment result = new 
ExpressionProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), 
getOriginalText(ctx.expr()), (InExpression) projection);
-            result.setAlias(alias);
-            return result;
-        }
-        if (projection instanceof CollateExpression) {
-            ExpressionProjectionSegment result = new 
ExpressionProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), 
getOriginalText(ctx.expr()), (CollateExpression) projection);
-            result.setAlias(alias);
-            return result;
+        if (projection instanceof CaseWhenExpression || projection instanceof 
VariableSegment || projection instanceof BetweenExpression || projection 
instanceof InExpression
+                || projection instanceof CollateExpression) {
+            return createExpressionProjectionSegment(ctx, alias, projection);
         }
         LiteralExpressionSegment column = (LiteralExpressionSegment) 
projection;
         ExpressionProjectionSegment result = null == alias
@@ -1738,6 +1717,12 @@ public abstract class MySQLStatementVisitor extends 
MySQLStatementBaseVisitor<AS
         return result;
     }
     
+    private ExpressionProjectionSegment 
createExpressionProjectionSegment(final ProjectionContext ctx, final 
AliasSegment alias, final ExpressionSegment projection) {
+        ExpressionProjectionSegment result = new 
ExpressionProjectionSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex(), 
getOriginalText(ctx.expr()), projection);
+        result.setAlias(alias);
+        return result;
+    }
+    
     @Override
     public ASTNode visitFromClause(final FromClauseContext ctx) {
         return visit(ctx.tableReferences());

Reply via email to