From 49cd5ea860bc155cd7414bc927da42beb1c7e9bf Mon Sep 17 00:00:00 2001 From: tristaZero Date: Sat, 8 Feb 2020 18:42:24 +0800 Subject: [PATCH] adjust tables list and add judgement for expr. --- .../sql/parser/MySQLVisitor.java | 17 +- .../sql/parser/visitor/MySQLDMLVisitor.java | 6 +- .../src/test/resources/visitor/dml/select.xml | 262 ++++++++++++++++++ 3 files changed, 277 insertions(+), 8 deletions(-) diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/MySQLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/MySQLVisitor.java index 6d12d7e8c9d0f1..1ae16c17e09c64 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/MySQLVisitor.java +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/MySQLVisitor.java @@ -235,11 +235,7 @@ public final ASTNode visitBooleanPrimary(final BooleanPrimaryContext ctx) { return new SubquerySegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.subquery().getText()); } if (null != ctx.comparisonOperator() || null != ctx.SAFE_EQ_()) { - ASTNode leftValue = visit(ctx.booleanPrimary()); - ASTNode rightValue = visit(ctx.predicate()); - PredicateRightValue predicateRightValue = rightValue instanceof ColumnSegment - ? (ColumnSegment) rightValue : new PredicateCompareRightValue(ctx.comparisonOperator().getText(), (ExpressionSegment) rightValue); - return new PredicateSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (ColumnSegment) leftValue, predicateRightValue); + return createCompareSegment(ctx); } if (null != ctx.predicate()) { return visit(ctx.predicate()); @@ -248,6 +244,17 @@ public final ASTNode visitBooleanPrimary(final BooleanPrimaryContext ctx) { return new CommonExpressionSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), ctx.getText()); } + private ASTNode createCompareSegment(final BooleanPrimaryContext ctx) { + ASTNode leftValue = visit(ctx.booleanPrimary()); + ASTNode rightValue = visit(ctx.predicate()); + if (!(leftValue instanceof ColumnSegment)) { + return leftValue; + } + PredicateRightValue predicateRightValue = rightValue instanceof ColumnSegment + ? (ColumnSegment) rightValue : new PredicateCompareRightValue(ctx.comparisonOperator().getText(), (ExpressionSegment) rightValue); + return new PredicateSegment(ctx.getStart().getStartIndex(), ctx.getStop().getStopIndex(), (ColumnSegment) leftValue, predicateRightValue); + } + @Override public final ASTNode visitPredicate(final PredicateContext ctx) { if (null != ctx.subquery()) { diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/visitor/MySQLDMLVisitor.java b/shardingsphere-sql-parser/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/visitor/MySQLDMLVisitor.java index f5b5353dc072d0..8d3c62c4c0391b 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/visitor/MySQLDMLVisitor.java +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/visitor/MySQLDMLVisitor.java @@ -361,14 +361,14 @@ public ASTNode visitEscapedTableReference(final EscapedTableReferenceContext ctx @Override public ASTNode visitTableReference(final TableReferenceContext ctx) { CollectionValue result = new CollectionValue<>(); + if (null != ctx.tableFactor()) { + result.getValues().add((TableSegment) visit(ctx.tableFactor())); + } if (null != ctx.joinedTable()) { for (JoinedTableContext each : ctx.joinedTable()) { result.getValues().add((TableSegment) visit(each)); } } - if (null != ctx.tableFactor()) { - result.getValues().add((TableSegment) visit(ctx.tableFactor())); - } return result; } diff --git a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/visitor/dml/select.xml b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/visitor/dml/select.xml index 4de604a06f6819..aed29edc6857ca 100644 --- a/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/visitor/dml/select.xml +++ b/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/test/resources/visitor/dml/select.xml @@ -413,4 +413,266 @@ + + + + + + + + + + + + + + + + + + +