SQL parser library for Java
Solution 1
ANTLR3 has an ANSI SQL grammar available. You can use that to create your own parser.
ANTLR4 has a SQL grammar.
Solution 2
- 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.
Solution 3
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.
Solution 4
General SQL Parser for Java is not open source, but is exactly what you are looking after.
Solution 5
Try Zql
hansvb
Updated on April 24, 2022Comments
-
hansvb about 2 years
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)
-
anton1980 over 11 yearshow is this question not constructive? what's wrong with you, Stackoverflow?
-
Viktor Vix Jančík about 10 yearsThis question should be reopened. Also, found foundationdb.github.io/sql-parser
-
Toby Speight about 8 yearsThis is off-topic - requests for libraries, tools, tutorials and other off-site resources are not considered on-topic for StackOverflow.
-
Peter Chaula over 7 yearsI think this should be asked on another SE site
-
Casualet over 7 yearsI think this question is constructive!!
-
btiernay over 7 yearsJust in case anyone needs a more feature rich parser that is open source, supports multiple dialects, AST / visitors, checkout Alibaba's Druid: github.com/alibaba/druid/tree/master/src/main/java/com/alibaba/…
-
hansvb over 15 yearsThat native() method looks interesting. Are there any examples as to how it can be used? What kind of conversions are possible there?
-
Lluis Martinez over 14 yearsIs the SQL parser used in Derby available as an independent JAR ?
-
Thomas Jones-Low over 14 yearsI don't know. I've never looked at the source for Derby.
-
IAdapter about 14 yearsWhy use ANTLR when you can implement your own parser generator?
-
duffymo about 14 yearsWhy generate your own parser generator when you can use ANTLR?
-
Han Zheng about 13 yearsbtw, anltr is under BSD.
-
Thomas Jones-Low over 12 yearsI now know the answer to this question: No, the SQL parser for Derby isn't a separate project. You will need pull it apart to use it for something other than Derby.
-
Abhishek over 12 yearsAny links to how one can use Antlr SQL Grammar to parse these queries? I looked at the grammar of some PL/SQL Parsers as well as Lexers and Parsers but was unable to fathom how to use one. Would appreciate any links.
-
duffymo over 12 yearsYou feed the grammar to ANTLR, which spits out the lexer/parser classes that you'll then compile and run. The best source I can think of is the ANTLR reference: amazon.com/…
-
Admin over 11 yearsThe best sql parser for Java +1
-
duffymo over 11 yearsWe're up to ANTLR 4 now. Perhaps the old grammars don't run on the new version.
-
zato over 11 yearszql is good for basic queries but when you try to parse a query that contains join statement, it blows up. so i don't suggest it
-
CrazyPyro over 10 years@SHC see also stackoverflow.com/a/14465585/268066
-
kutschkem about 10 yearsCareful though, the available grammars are not necessarily under the same license as ANTLR
-
Gerold Broser almost 10 years@Thilo e.g. SQL Server 2014, nativeSQL Method (SQLServerConnection): "This method is not currently supported by the Microsoft JDBC Driver for SQL Server."
-
Gerold Broser almost 10 years@Thilo OJDBC's OracleConnectionWrapper seems like supporting it.
-
quarks about 9 yearsI have a question for Presto, If I have Statement statement = SQL_PARSER.createStatement(query); How can I get the Query body, i.e. the Select, From, Where, etc values?
-
David Phillips about 9 yearsStatement is a base class. A SELECT statement will be of type Query. It contains a QueryBody that has the subclass QuerySpecification. The structure is more complex than you might expect in order to support UNION, TABLE, VALUES, set operations, etc. You can create a visitor by extending AstVisitor or DefaultTraversalVisitor. Look at SqlFormatter for an example of how to walk the tree.
-
zinking about 9 yearssurprised no one mention github.com/porcelli/plsql-parser.git , this is the most comprehensive sql parser I've seen
-
MockedMan.Object about 9 yearsWould it be possible to get the query tree structure from a given query using Presto?
-
David Phillips about 9 yearsI'm not sure what you're asking. Can you create a new question with more detail?
-
Lluis Martinez about 6 yearsI came back again after 10 years :-)
-
Nathan Adams almost 3 yearsANTLR is great and nice to use but very slow, there's probably faster ones out there
-
duffymo almost 3 yearsSuch as? A recommendation or faster code from you would be more helpful.
-
Lluis Martinez about 2 yearsPresto's parser link is dead