JSQLParser /JSqlParser
JSqlParser parses an SQL statement and translate it into a hierarchy of Java classes. The generated hierarchy can be navigated using the Visitor Pattern
★ 5,970⑂ 1,446👁 210▤ 38
5,970
stars
+0+-1+6
1,446
forks
210
watchers
–
prs
–
issues
38
releases
–
discussions
README
JSqlParser
Turn any SQL statement into a traversable tree of Java objects -- and back again.
An RDBMS-agnostic SQL parser for the JVM:one grammar, twelve dialects, no native extensions.
Website · Samples · Syntax · Change Log · Contributing
---
What it does
Give it SQL. Get an AST you can walk, rewrite, and print back out.
sql
SELECT 1 FROM dual WHERE a = b
text
SQL Text
└─Statements: statement.select.PlainSelect
├─selectItems: statement.select.SelectItem
│ └─LongValue: 1
├─Table: dual
└─where: expression.operators.relational.EqualsTo
├─Column: a
└─Column: b
java
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());
The tree is traversable with the Visitor pattern, and the same object model works in reverse:
build statements from Java with a fluent API
and render them as SQL text.
Install
Use the stable Manticore builds. They are released continuously from the current development
line and carry all of the performance and grammar work described below. The upstream
com.github.jsqlparser release on Maven Central is considerably older.
xml
com.manticore-projects.jsqlformatter
jsqlparser
5.3.218,)
gradle
implementation("com.manticore-projects.jsqlformatter:jsqlparser:+")
Upstream release and snapshots
xml
com.github.jsqlparser
jsqlparser
5.3
Snapshot coordinates and repository setup are on the
[build dependencies page.
Performance
11× faster than 5.3, and the fastest parser on real-world SQL of any of the parsers
tested, in any language — 19× ahead of sqlglot[c] on JSqlParser's own SELECT test suite. And the community kept sending performance improvements since. Thank you everyone!
text
Benchmark (version) Mode Cnt Score Error Units
JSQLParserBenchmark.parseSQLStatements latest avgt 15 21.138 ± 0.557 ms/op
BigQuery · Snowflake · DuckDB · Redshift · Oracle · MS SQL Server · Sybase
PostgreSQL · MySQL · MariaDB · DB2 · H2 · HSQLDB · Derby · SQLite
| | Statements |
|---|---|
| Queries | SELECT · WITH … · Piped SQL |
| ksqlDB windows | JOIN WITHIN, window GRACE PERIOD, and EMIT CHANGES/FINAL |
| ClickHouse column selection | COLUMNS('regexp') select items with chained APPLY, EXCEPT, and REPLACE transformers |
| DML | INSERT · UPDATE · UPSERT · MERGE · DELETE · TRUNCATE TABLE |
| Oracle RETURNING | Named output binds (INTO :name), JDBC outp