sql parser support mysql xa statement

This commit is contained in:
shaojin.wensj
2022-10-23 00:12:40 +08:00
parent 6fca469001
commit 0229a76ad7
6 changed files with 114 additions and 1 deletions
@@ -0,0 +1,71 @@
package com.alibaba.druid.sql.dialect.mysql.ast.statement;
import com.alibaba.druid.DruidRuntimeException;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
public class MySqlXAStatement extends MySqlStatementImpl {
private XAType type;
private SQLExpr id;
public XAType getType() {
return type;
}
public void setType(XAType type) {
this.type = type;
}
public SQLExpr getId() {
return id;
}
public void setId(SQLExpr x) {
if (x != null) {
x.setParent(this);
}
this.id = x;
}
public void accept0(MySqlASTVisitor v) {
if (v.visit(this)) {
acceptChild(v, id);
}
v.endVisit(this);
}
public enum XAType {
START,
BEGIN,
END,
PREPARE,
COMMIT,
ROLLBACK,
RECOVER;
public static XAType of(String typeStr) {
if (typeStr == null || typeStr.isEmpty()) {
return null;
}
switch (typeStr.toUpperCase()) {
case "START":
return XAType.START;
case "BEGIN":
return XAType.BEGIN;
case "END":
return XAType.END;
case "PREPARE":
return XAType.PREPARE;
case "COMMIT":
return XAType.COMMIT;
case "ROLLBACK":
return XAType.ROLLBACK;
case "RECOVER":
return XAType.RECOVER;
default:
throw new DruidRuntimeException("not support xa type " + typeStr);
}
}
}
}
@@ -1264,6 +1264,24 @@ public class MySqlStatementParser extends SQLStatementParser {
return true;
}
if (lexer.identifierEquals("XA")) {
lexer.nextToken();
MySqlXAStatement stmt = new MySqlXAStatement();
String typeStr = lexer.stringVal();
stmt.setType(
MySqlXAStatement.XAType.of(typeStr)
);
lexer.nextToken();
if (lexer.token() != EOF && lexer.token() != SEMI) {
SQLExpr xid = exprParser.expr();
stmt.setId(xid);
}
statementList.add(stmt);
return true;
}
if (lexer.token() == Token.EXPLAIN) {
SQLStatement stmt = this.parseExplain();
statementList.add(stmt);
@@ -1267,4 +1267,11 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
default void endVisit(MysqlAlterTableAlterCheck x) {
}
default boolean visit(MySqlXAStatement x) {
return true;
}
default void endVisit(MySqlXAStatement x) {
}
} //
@@ -5553,6 +5553,16 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
return false;
}
public void endVisit(MysqlAlterTableAlterCheck x) {
public boolean visit(MySqlXAStatement x) {
print0(ucase ? "XA " : "xa ");
MySqlXAStatement.XAType type = x.getType();
print0(ucase ? type.name() : type.name().toLowerCase());
SQLExpr id = x.getId();
if (id != null) {
print(' ');
printExpr(id);
}
return false;
}
} //
@@ -126,6 +126,10 @@ public class MySqlResourceTest extends TestCase {
exec_test("bvt/parser/mysql-33.txt");
}
public void test_34() throws Exception {
exec_test("bvt/parser/mysql-34.txt");
}
public void exec_test(String resource) throws Exception {
// System.out.println(resource);
InputStream is = null;
@@ -0,0 +1,3 @@
XA PREPARE xid
---------------------------
XA PREPARE xid