首先搭好java环境,把druid导入lib中, 我是基于vsc开发的,配好java环境以后把官方的druid-1.2.8.jar放到开发目录/lib 下,vsc就能自动将其加入依赖库中,
以下代码可以测试MySqlStatementParser的主要功能:(代码源于官方测试代码MySqlPerfTest.java) (在开发目录下的test.java)
import java.util.List;
import java.io.IOException;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
import com.alibaba.druid.util.Utils;
public class test {
public static void main(String[] args) {
String sql;
try {
sql = Utils.readFromResource("test_in.txt");
}catch (IOException ex) {
throw new IllegalStateException("read error", ex);
} {}
StringBuilder out = new StringBuilder();
MySqlOutputVisitor visitor = new MySqlOutputVisitor(out);
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
for (SQLStatement statement : statementList) {
statement.accept(visitor);
visitor.println();
}
System.out.println(out.toString());
}
}
通过调试我们可以发现,变量statement 里面存储的就是一颗AST树,这样就可以很方便的帮助我们定位不同语句所对应的对象是什么。
整个程序的逻辑就是"MySQL Compound Statements" --> “AST” --> “MySQL Compound Statements”, 其中"MySQL Compound Statements" --> “AST” 是通过MySqlStatementParser() 实现的, “AST” --> “MySQL Compound Statements” 是通过MySqlOutputVisitor 来实现的,
可以发现MySqlOutputVisitor 里是通过visit() 函数解析AST中的每一个对象来进行转换的, 我们思路就是通过继承MySqlOutputVisitor 类,重载里面的visit() 函数,实现 “AST” --> “pgplsql”, 这样只需要将上面的程序中的MySqlOutputVisitor(out) 改为你继承重写了的OutputVisitor(out) ,即可完成转化。
我们要主要看的就是MySqlOutputVisitor 这个对象里的所有visit() 函数,理解不同的visit() 是在干嘛,并将其重载,使得其输出变为pgplsql语言,
这里我简单写了个MySql2PgplSqlOutputVisitor 的示例,里面简单的重载了几个visit()函数
import com.alibaba.druid.DbType;
import com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.ast.expr.*;
import com.alibaba.druid.sql.ast.statement.*;
import com.alibaba.druid.sql.dialect.mysql.ast.*;
import com.alibaba.druid.sql.dialect.mysql.ast.clause.*;
import com.alibaba.druid.sql.dialect.mysql.ast.clause.ConditionValue.ConditionType;
import com.alibaba.druid.sql.dialect.mysql.ast.clause.MySqlCaseStatement.MySqlWhenStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.expr.*;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.*;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement.TableSpaceOption;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateUserStatement.UserSpecification;
import com.alibaba.druid.sql.visitor.ExportParameterVisitorUtils;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.druid.sql.visitor.VisitorFeature;
import com.alibaba.druid.util.FnvHash;
import java.io.IOException;
import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlOutputVisitor;
public class MySql2PgplSqlOutputVisitor extends MySqlOutputVisitor {
public MySql2PgplSqlOutputVisitor(Appendable appender) {
super(appender);
}
public MySql2PgplSqlOutputVisitor(Appendable appender, boolean parameterized) {
super(appender, parameterized);
try {
configFromProperty();
} catch (AccessControlException e) {
}
}
@Override
public boolean visit(SQLAssignItem x) {
x.getTarget().accept(this);
print0(" := ");
x.getValue().accept(this);
return false;
}
@Override
public boolean visit(SQLSetStatement x) {
SQLSetStatement.Option option = x.getOption();
if (option != null) {
print(option.name());
print(' ');
}
if (option == SQLSetStatement.Option.PASSWORD) {
print0("FOR ");
}
printAndAccept(x.getItems(), ", ");
if (x.getHints() != null && x.getHints().size() > 0) {
print(' ');
printAndAccept(x.getHints(), " ");
}
return false;
}
@Override
public boolean visit(SQLWhileStatement x) {
String label = x.getLabelName();
if (label != null && label.length() != 0) {
print0(x.getLabelName());
print0(": ");
}
print0(ucase ? "WHILE " : "while ");
x.getCondition().accept(this);
print0(ucase ? " LOOP" : " loop");
println();
for (int i = 0, size = x.getStatements().size(); i < size; ++i) {
SQLStatement item = x.getStatements().get(i);
item.accept(this);
if (i != size - 1) {
println();
}
}
println();
print0(ucase ? "END LOOP" : "end loop");
if (label != null && label.length() != 0) {
print(' ');
print0(label);
}
return false;
}
}
|