Improve mysql Statemen definition and Visitor
Background
Now, the Statement definition of mysql is not perfect yet. We need to refine the Statement definition and construct reasonable statements through visitor. For example, some statements are empty
// shardingsphere/shardingsphere-sql-parser/shardingsphere-sql-parser-statement/src/main/java/org/apache/shardingsphere/sql/parser/sql/dialect/statement/mysql/dml/MySQLCallStatement.java
package org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.dml;
/**
* MySQL call statement.
*/
public final class MySQLCallStatement extends CallStatement implements MySQLStatement {
}
// shardingsphere/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/impl/MySQLDMLVisitor.java
package org.apache.shardingsphere.sql.parser.mysql.visitor.impl
/**
* DML visitor for MySQL.
*/
public final class MySQLDMLVisitor extends MySQLVisitor implements DMLVisitor {
@Override
public ASTNode visitCall(final CallContext ctx) {
return new MySQLCallStatement();
}
@Override
public ASTNode visitDoStatement(final DoStatementContext ctx) {
return new MySQLDoStatement();
}
}
Tasks
- Refine statement definition.
- Implementation corresponding method in Visitor to builds a reasonable Statement.
Solutions
- Think about a reasonable Statement definition
- Complete the specify Statement Class
- Implementation corresponding method in Visitor(eg:shardingsphere/shardingsphere-sql-parser/shardingsphere-sql-parser-dialect/shardingsphere-sql-parser-mysql/src/main/java/org/apache/shardingsphere/sql/parser/mysql/visitor/impl/MySQLDMLVisitor.java)
- Add unit test for it.
Note
We can discuss the reasonableness of the Statement definition and then implement it.
Subtask
- [x] MySQL
CallStatement - [x] MySQL
AlterTableStatemen - [x] MySQL
CreateTableStatement - [x] MySQL
DropTableStatement
Please assgin to me, i want to have a try
Please assgin to me, i want to have a try
@wenweibin OK, we can discuss it first and then implement it to avoid unnecessary work.
@wenweibin We can start with the example above(MySQLCallStatement,MySQLDoStatement) or the statement you are interested in.
- Considering that the synax of MySQL CALL Statement is
CALL sp_name([parameter[,...]]),in my opinionMySQLCallStatementclass should add two properties:procedureNameandparameters,the codes are as below:
@ToString
@AllArgsConstructor
@Getter
public final class MySQLCallStatement extends CallStatement implements MySQLStatement {
private String procedureName;
private List<ExpressionSegment> parameters;
}
- The Synax of MySQL DO_Statement_ is
DO expr [, expr] ...,so i thinkMySQLDoStatementclass should add one propertie:expressionSegments` ,the codes are as below:
@ToString
@AllArgsConstructor
@Getter
public final class MySQLDoStatement extends DoStatement implements MySQLStatement {
private List<ExpressionSegment> expressionSegments;
}
- Correspondingly,the
MySQLDMLVisitorclass is refactored as below:
public final class MySQLDMLVisitor extends MySQLVisitor implements DMLVisitor {
@Override
public ASTNode visitCall(final CallContext ctx) {
String procedureName = ctx.identifier().getText();
List<ExpressionSegment> parameters = new ArrayList<>();
ctx.expr().forEach(each -> parameters.add((ExpressionSegment) visit(each)));
return new MySQLCallStatement(procedureName,parameters);
}
@Override
public ASTNode visitDoStatement(final DoStatementContext ctx) {
List<ExpressionSegment> expressions = new ArrayList<>();
ctx.expr().forEach(each -> expressions.add((ExpressionSegment) visit(each)));
return new MySQLDoStatement(expressions);
}
}
Can you give some suggestions?@jingshanglu
@wenweibin I think your design is ok.
Which project should unit tests be placed? shardingsphere-sql-parser-test or shardingsphere-sql-parser-mysql?
If they are placed in the shardingsphere-sql-parser-test directory, should these refactored codes migrate to CallStatement and DoStatement?
Hope to get your advices. @jingshanglu
Which project should unit tests be placed?
shardingsphere-sql-parser-testorshardingsphere-sql-parser-mysql? If they are placed in theshardingsphere-sql-parser-testdirectory, should these refactored codes migrate toCallStatementandDoStatement? Hope to get your advices. @jingshanglu
For the logic of the test framework, please refer to #7880 . Test sql and expected result should place in shardingsphere/shardingsphere-sql-parser/shardingsphere-sql-parser-test/src/main/resources/, and then add the assert code on project shardingsphere-sql-parser-test. @wenweibin
@wenweibin Hello, i add some subtask about table,we can discuss the definition first.
Firstlly, I'm going to start with MySQLDropTableStatement of subtask 4.According the synax of dropTable in DDLStatement.g4, I think the refactor is like below:
- Revise
MySQLDropTableStatementclass
/**
* MySQL drop table statement.
*/
@ToString
@Getter
@Setter
public final class MySQLDropTableStatement extends DropTableStatement implements MySQLStatement {
private boolean temporary;
private boolean isExisted;
private Restrict restrict;
}
- Add
Restrictenum
/**
* Restrict type enum.
*/
public enum Restrict {
RESTRICT, CASCADE
}
- Revise
MySQLDDLStatementSQLVisitorvisitDropTable method
@Override
public ASTNode visitDropTable(final DropTableContext ctx) {
MySQLDropTableStatement result = new MySQLDropTableStatement();
result.setTemporary(Objects.nonNull(ctx.TEMPORARY()));
result.setExisted(Objects.nonNull(ctx.existClause()));
if (null != ctx.restrict()) {
result.setRestrict(Objects.nonNull(ctx.restrict()) ? Restrict.RESTRICT : Restrict.CASCADE);
}
result.getTables().addAll(((CollectionValue<SimpleTableSegment>) visit(ctx.tableNames())).getValue());
return result;
}
Can you give some suggestions?@jingshanglu
@wenweibin I agree with your design.