如何使用plsql-parser(ANTLR) [英] How to use plsql-parser (ANTLR)
本文介绍了如何使用plsql-parser(ANTLR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在自动化测试中检查PL/SQL查询语法,它看起来像 https://github.com/porcelli/plsql-parser 可能对此有用.我不容易发现如何安装和使用它.
I'd like to check PL/SQL query syntax in automated tests, and it looks like https://github.com/porcelli/plsql-parser might be useful for that. I am not easily finding out how I would install and use it though.
请注意,这是针对Ruby项目的,但是我在Java方面相当称职.我希望有某种方法可以通过控制台运行检查,传递SQL并获取所有错误信息,包括行/列.
Note that this is for a Ruby project, but I'm reasonably competent in Java. I'm hoping there's some way I can run the checking via console, pass in the SQL, and get back any error info, including line/column.
谢谢.
推荐答案
- 下载3.5.1版的ANTLR工具
- 从此处下载源代码(因为此版本已移植到3.5 .1)
- 使用该工具编译Poarsers/no-ast子目录中的源代码
- 第一次编译PLSQLLexer.g
- 第二次从no-ast子目录编译PLSQLParser.g
- 使用此示例代码作为示例:
- Download the ANTLR tool ver 3.5.1
- Download the source codes from here (as this version was ported to 3.5.1)
- Use the tool to compile the sources from poarsers/no-ast subdir
- 1st compile PLSQLLexer.g
- 2nd compile PLSQLParser.g from no-ast subdir
- the use this sample code as an example:
import org.antlr.runtime.ANTLRNoCaseFileStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.RecognitionException;
import br.com.porcelli.parser.plsql.PLSQLLexer;
import br.com.porcelli.parser.plsql.PLSQLParser;
public static void parse(String file) {
try {
PLSQLLexer lex = new PLSQLLexer(new ANTLRNoCaseFileStream(file));
CommonTokenStream tokens = new CommonTokenStream(lex);
PLSQLParser parser = new PLSQLParser(tokens);
/*start_rule_return AST =*/ parser.data_manipulation_language_statements();
System.err.println(file +": " + parser.getNumberOfSyntaxErrors());
if(parser.getNumberOfSyntaxErrors() != 0)
{
//System.exit(1);
}
} catch (RecognitionException e) {
System.err.println(e.toString());
} catch (IOException e) {
System.err.println(e.toString());
} catch (java.lang.OutOfMemoryError e) {
System.err.println(file + ":");
System.err.println(e.toString());
} catch (java.lang.ArrayIndexOutOfBoundsException e) {
System.err.println(file + ":");
System.err.println(e.toString());
}
}
这篇关于如何使用plsql-parser(ANTLR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文