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:
ANTLR3 has an ANSI SQL grammar available. You can use that to create your own parser.
ANTLR4 has a SQL grammar.
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.
General SQL Parser for Java is not open source, but is exactly what you are looking after.
Try Zql
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With