You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by GitBox <gi...@apache.org> on 2021/09/08 08:59:25 UTC

[GitHub] [shardingsphere] strongduanmu commented on a change in pull request #12280: Support sql annotation analysis

strongduanmu commented on a change in pull request #12280:
URL: https://github.com/apache/shardingsphere/pull/12280#discussion_r704188487



##########
File path: shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserExecutor.java
##########
@@ -44,6 +48,16 @@ public SQLStatementParserExecutor(final String databaseType) {
      * @return SQL statement
      */
     public SQLStatement parse(final String sql) {
-        return visitorEngine.visit(parserEngine.parse(sql, false));
+        ParserContext parserContext = parserEngine.parse(sql, false);
+        SQLStatement result = visitorEngine.visit(parserContext.getParseTree());
+        handleComments(result, parserContext);
+        return result;
+    }
+    
+    private void handleComments(final SQLStatement sqlStatement, final ParserContext parserContext) {
+        if (sqlStatement instanceof AbstractSQLStatement) {
+            ((AbstractSQLStatement) sqlStatement).setCommentsSegments(parserContext.getHiddenTokens().stream()

Review comment:
       @tuichenchuxin Can we use addAll method here?

##########
File path: shardingsphere-infra/shardingsphere-infra-parser/src/main/java/org/apache/shardingsphere/infra/parser/sql/SQLStatementParserExecutor.java
##########
@@ -44,6 +48,16 @@ public SQLStatementParserExecutor(final String databaseType) {
      * @return SQL statement
      */
     public SQLStatement parse(final String sql) {
-        return visitorEngine.visit(parserEngine.parse(sql, false));
+        ParserContext parserContext = parserEngine.parse(sql, false);
+        SQLStatement result = visitorEngine.visit(parserContext.getParseTree());
+        handleComments(result, parserContext);

Review comment:
       @tuichenchuxin Do you think `appendSQLComments` is better for this method?

##########
File path: shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/ParserContext.java
##########
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.sql.parser.core;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+import org.antlr.v4.runtime.Token;
+import org.antlr.v4.runtime.tree.ParseTree;
+
+import java.util.Collection;
+
+@RequiredArgsConstructor
+@Getter
+public class ParserContext {

Review comment:
       @tuichenchuxin Add final here.

##########
File path: shardingsphere-sql-parser/shardingsphere-sql-parser-engine/src/main/java/org/apache/shardingsphere/sql/parser/core/ParseASTNode.java
##########
@@ -17,18 +17,34 @@
 
 package org.apache.shardingsphere.sql.parser.core;
 
-import lombok.RequiredArgsConstructor;
+import org.antlr.v4.runtime.CommonTokenStream;
+import org.antlr.v4.runtime.Token;
 import org.antlr.v4.runtime.tree.ParseTree;
 import org.apache.shardingsphere.sql.parser.api.visitor.ASTNode;
 
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.stream.Collectors;
+
 /**
  * Parse AST node.
  */
-@RequiredArgsConstructor
 public final class ParseASTNode implements ASTNode {
     
     private final ParseTree parseTree;
     
+    private final Collection<Token> hiddenTokens;
+    
+    public ParseASTNode(final ParseTree parseTree) {
+        this.parseTree = parseTree;
+        this.hiddenTokens = new LinkedList<>();
+    }
+    
+    public ParseASTNode(final ParseTree parseTree, final CommonTokenStream tokenStream) {
+        this.parseTree = parseTree;
+        this.hiddenTokens = tokenStream.getTokens().stream().filter(each -> each.getChannel() == Token.HIDDEN_CHANNEL).collect(Collectors.toList());

Review comment:
       @tuichenchuxin Please place the constant to the left of the expression.

##########
File path: shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/common/segment/generic/CommentsSegment.java
##########
@@ -0,0 +1,35 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.sql.parser.sql.common.segment.generic;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+/**
+ * Comments segment.
+ */
+@RequiredArgsConstructor
+@Getter
+public class CommentsSegment {

Review comment:
       @tuichenchuxin Add final for this class and extend SQLStatement.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@shardingsphere.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org