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 2020/10/07 13:56:45 UTC

[GitHub] [shardingsphere] strongduanmu commented on a change in pull request #7704: add create function and procedure validator

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



##########
File path: shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/impl/MySQLDDLVisitor.java
##########
@@ -461,92 +483,256 @@ public ASTNode visitKeyParts_(final KeyParts_Context ctx) {
         }
         return result;
     }
-
+    
     @Override
     public ASTNode visitCreateProcedure(final CreateProcedureContext ctx) {
-        return new MySQLCreateProcedureStatement();
+        MySQLCreateProcedureStatement result = new MySQLCreateProcedureStatement();
+        result.setRoutineBody((RoutineBodySegment) visit(ctx.routineBody()));
+        return result;
     }
-
+    
     @Override
     public ASTNode visitAlterProcedure(final AlterProcedureContext ctx) {
         return new MySQLAlterProcedureStatement();
     }
-
+    
     @Override
     public ASTNode visitDropProcedure(final DropProcedureContext ctx) {
         return new MySQLDropProcedureStatement();
     }
-
+    
     @Override
     public ASTNode visitCreateFunction(final CreateFunctionContext ctx) {
-        return new MySQLCreateFunctionStatement();
+        MySQLCreateFunctionStatement result = new MySQLCreateFunctionStatement();
+        result.setRoutineBody((RoutineBodySegment) visit(ctx.routineBody()));
+        return result;
     }
-
+    
+    @SuppressWarnings("unchecked")
+    @Override
+    public ASTNode visitRoutineBody(final RoutineBodyContext ctx) {
+        RoutineBodySegment result = new RoutineBodySegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex());
+        CollectionValue<ValidStatementSegment> validStatements;
+        if (null != ctx.simpleStatement()) {
+            validStatements = (CollectionValue<ValidStatementSegment>) visit(ctx.simpleStatement());
+        } else {
+            validStatements = (CollectionValue<ValidStatementSegment>) visit(ctx.compoundStatement());
+        }
+        result.getValidStatements().addAll(validStatements.getValue());
+        return result;
+    }
+    
+    @Override
+    public ASTNode visitSimpleStatement(final SimpleStatementContext ctx) {
+        return visit(ctx.validStatement());
+    }
+    
+    @Override
+    public ASTNode visitCompoundStatement(final CompoundStatementContext ctx) {
+        return visit(ctx.beginStatement());
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Override
+    public ASTNode visitBeginStatement(final BeginStatementContext ctx) {
+        CollectionValue<ValidStatementSegment> result = new CollectionValue<>();
+        for (ValidStatementContext each : ctx.validStatement()) {
+            result.combine((CollectionValue<ValidStatementSegment>) visit(each));
+        }
+        return result;
+    }
+    
+    @SuppressWarnings("unchecked")
+    @Override
+    public ASTNode visitValidStatement(final ValidStatementContext ctx) {
+        CollectionValue<ValidStatementSegment> result = new CollectionValue<>();
+        ValidStatementSegment validStatement = new ValidStatementSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex());
+        if (null != ctx.createTable()) {
+            validStatement.setCreateTable((MySQLCreateTableStatement) visit(ctx.createTable()));
+            result.getValue().add(validStatement);
+        } else if (null != ctx.alterTable()) {
+            validStatement.setAlterTable((MySQLAlterTableStatement) visit(ctx.alterTable()));
+            result.getValue().add(validStatement);
+        } else if (null != ctx.dropTable()) {
+            validStatement.setDropTable((MySQLDropTableStatement) visit(ctx.dropTable()));
+            result.getValue().add(validStatement);
+        } else if (null != ctx.truncateTable()) {
+            validStatement.setTruncate((MySQLTruncateStatement) visit(ctx.truncateTable()));
+            result.getValue().add(validStatement);
+        }
+        if (null != ctx.validDMLStatement()) {
+            result.combine((CollectionValue<ValidStatementSegment>) visit(ctx.validDMLStatement()));
+        }
+        if (null != ctx.beginStatement()) {
+            result.combine((CollectionValue<ValidStatementSegment>) visit(ctx.beginStatement()));
+        }
+        if (null != ctx.flowControlStatement()) {
+            result.combine((CollectionValue<ValidStatementSegment>) visit(ctx.flowControlStatement()));
+        }
+        return result;
+    }
+    
+    @Override
+    public ASTNode visitValidDMLStatement(final ValidDMLStatementContext ctx) {
+        CollectionValue<ValidStatementSegment> result = new CollectionValue<>();
+        ValidStatementSegment validStatement = new ValidStatementSegment(ctx.start.getStartIndex(), ctx.stop.getStopIndex());
+        if (null != ctx.insert()) {
+            validStatement.setInsert((MySQLInsertStatement) createParseTreeVisitor(ctx.insert().getClass()).visit(ctx.insert()));
+        } else if (null != ctx.replace()) {
+            validStatement.setReplace((MySQLInsertStatement) createParseTreeVisitor(ctx.replace().getClass()).visit(ctx.replace()));
+        } else if (null != ctx.update()) {
+            validStatement.setUpdate((MySQLUpdateStatement) createParseTreeVisitor(ctx.update().getClass()).visit(ctx.update()));
+        } else if (null != ctx.delete()) {
+            validStatement.setDelete((MySQLDeleteStatement) createParseTreeVisitor(ctx.delete().getClass()).visit(ctx.delete()));
+        } else if (null != ctx.select()) {
+            validStatement.setSelect((MySQLSelectStatement) createParseTreeVisitor(ctx.select().getClass()).visit(ctx.select()));
+        }
+        result.getValue().add(validStatement);
+        return result;
+    }
+    
+    @SuppressWarnings("rawtypes")
+    private ParseTreeVisitor createParseTreeVisitor(final Class<? extends ParseTree> parseTreeClass) {

Review comment:
       > if `MySQLDMLVisitor` is necessary, do you think moving the content of `MySQLDMLVisitor` to `MySQLVisitor` will be better than `createParseTreeVisitor`?
   
   @tristaZero The stored routine supports a variety of DML and DAL statements, and migrating the implementation of these statements to the `MySQLVisitor` would cause the `MySQLVisitor` to become bloated. 
   In addition, the `MySQLVisitor` seems to be more suited to processing some generic syntax fragments than the entire statement.




----------------------------------------------------------------
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.

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