Awesome
JSqlParser 5.0 Website <img src="src/site/sphinx/_images/logo-no-background.svg" alt="drawing" width="200" align="right"/>
Summary
Please visit the WebSite. JSqlParser is a RDBMS agnostic SQL statement parser. It translates SQL statements into a traversable hierarchy of Java classes (see Samples):
SELECT 1 FROM dual WHERE a = b
SQL Text
└─Statements: statement.select.PlainSelect
├─selectItems: statement.select.SelectItem
│ └─LongValue: 1
├─Table: dual
└─where: expression.operators.relational.EqualsTo
├─Column: a
└─Column: b
String sqlStr = "select 1 from dual where a=b";
PlainSelect select = (PlainSelect) CCJSqlParserUtil.parse(sqlStr);
SelectItem selectItem =
select.getSelectItems().get(0);
Assertions.assertEquals(
new LongValue(1)
, selectItem.getExpression());
Table table = (Table) select.getFromItem();
Assertions.assertEquals("dual", table.getName());
EqualsTo equalsTo = (EqualsTo) select.getWhere();
Column a = (Column) equalsTo.getLeftExpression();
Column b = (Column) equalsTo.getRightExpression();
Assertions.assertEquals("a", a.getColumnName());
Assertions.assertEquals("b", b.getColumnName());
}
JSQLParser-4.9 was the last JDK8 compatible version. The recent JSQLParser-5.0 depends on JDK11 and introduces API breaking changes to the AST Visitors. Please see the Migration Guide for the details.
Supported Grammar and Syntax
JSqlParser aims to support the SQL standard as well as all major RDBMS. Any missing syntax or features can be added on demand.
RDBMS | Statements |
---|---|
Oracle<br>MS SQL Server and Sybase<br>Postgres<br>MySQL and MariaDB<br>DB2<br>H2 and HSQLDB and Derby<br>SQLite | SELECT <br>INSERT , UPDATE , UPSERT , MERGE <br>DELETE , TRUNCATE TABLE <br>CREATE ... , ALTER .... , DROP ... <br>WITH ... |
Salesforce SOQL | INCLUDES , EXCLUDES |
JSqlParser can also be used to create SQL Statements from Java Code with a fluent API (see Samples).
Sister Projects
If you like JSqlParser then please check out its related projects:
-
JSQLFormatter for pretty printing and formatting SQL Text
-
JSQLTranspiler for dialect specific rewriting, SQL Column resolution and Lineage
Alternatives to JSqlParser?
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.
Alternatively the dual-licensed JOOQ provides a hand-written Parser supporting a lot of RDBMS, translation between dialects, SQL transformation, can be used as a JDBC proxy for translation and transformation purposes.
Documentation
License
JSqlParser is dual licensed under LGPL V2.1 or Apache Software License, Version 2.0.