What is itjava
JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes.sql
The generated hierarchy can be navigated using the Visitor Patternui
jsqlparser结构简单,继承也不复杂,而且setter和getter方法很全面,因此处理起来很容易。spa
How it worksblog
It is built using JavaCC. The core JavaCC grammar for SQL has been taken from Guido Draheim's site and has been changed in order to produce a hierarchy of Java classes.继承
Alternatives to JSqlParser?ci
General SQL Parser looks pretty good, with extended SQL syntax (like PL/SQL and T-SQL) and java + .NET APIs. The tool is commercial (license available online), with a free download option.get
How it can be usedit
The method CCJSqlParserManager.parse returns a class implementing Statement which can be used to navigate the structure representing the SQL statement.io
To avoid using the instanceof operator as in:
if (statement instanceof Insert) {
// insert case
}
else if (statement instanceof Update) {
// update case
}
the hierarchy can be accessed using proper visitors:
void visit(Insert statement) {
// insert case
}
void visit(Update statement) {
// update case
}