Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL parser library for Java [closed]

Tags:

java

sql

parsing

Is there an open-source Java library for parsing SQL statements?

If possible, it should be customizable or flexible enough to also be able to parse (or at least ignore) vendor-specific syntax (such as Oracle tablespace definitions or MySQL's LIMIT clause).

If not, strict adherence to the SQL standard is also fine.

Update: I need this for two things:

  • providing an SQL interface to a non-SQL database (mapping to internal API calls)
  • rewriting SQL before it goes to the actual database (e.g. Oracle)
like image 901
Thilo Avatar asked Mar 19 '09 00:03

Thilo


7 Answers

ANTLR3 has an ANSI SQL grammar available. You can use that to create your own parser.

ANTLR4 has a SQL grammar.

like image 143
duffymo Avatar answered Oct 02 '22 05:10

duffymo


  • JSqlParser
  • Trino's parser is written using ANTLR4 and has its own immutable AST classes that are built from the parser. The AST has a visitor and pretty printer.
like image 30
David Phillips Avatar answered Oct 02 '22 05:10

David Phillips


Parser

If you need a parser there should be a parser in the code base of Apache Derby.

Dealing with vendor-specific SQL

You may want to look at the .native() method on the jdbc Connection object which you can pass it vendor neutral queries that will get postprocessed into vendor specific queries.

like image 39
Elijah Avatar answered Oct 02 '22 04:10

Elijah


General SQL Parser for Java is not open source, but is exactly what you are looking after.

like image 36
James Avatar answered Oct 02 '22 03:10

James


Try Zql

like image 38
jagamot Avatar answered Oct 02 '22 03:10

jagamot


Hibernate uses ANTLR for sql and hql parsing.

JSqlParser is also a good option.although it has some bugs(or some features not implemented) while parsing oracle pl/sql. see its forum for detail.

so if you're parsing oracle pl/sql, ANTLR is recommended.

like image 26
Han Zheng Avatar answered Oct 02 '22 04:10

Han Zheng


What you want to do with the parsed SQL? I can recommend a few Java implementation of Lex/Yacc (BYACC/J, Java Cup) that you can use an existing SQL grammar with.

If you want to actually do something with the resulting parsed grammar, I might suggest looking at Derby, an open source SQL database written in Java.

like image 21
Thomas Jones-Low Avatar answered Oct 02 '22 03:10

Thomas Jones-Low