SQL parser library for Java

102,551

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

Share:
102,551
hansvb
Author by

hansvb

Updated on April 24, 2022

Comments

  • hansvb
    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
      anton1980 over 11 years
      how is this question not constructive? what's wrong with you, Stackoverflow?
    • Viktor Vix Jančík
      Viktor Vix Jančík about 10 years
      This question should be reopened. Also, found foundationdb.github.io/sql-parser
    • Toby Speight
      Toby Speight about 8 years
      This is off-topic - requests for libraries, tools, tutorials and other off-site resources are not considered on-topic for StackOverflow.
    • Peter Chaula
      Peter Chaula over 7 years
      I think this should be asked on another SE site
    • Casualet
      Casualet over 7 years
      I think this question is constructive!!
    • btiernay
      btiernay over 7 years
      Just 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/aliba‌​ba/…
  • hansvb
    hansvb over 15 years
    That native() method looks interesting. Are there any examples as to how it can be used? What kind of conversions are possible there?
  • Lluis Martinez
    Lluis Martinez over 14 years
    Is the SQL parser used in Derby available as an independent JAR ?
  • Thomas Jones-Low
    Thomas Jones-Low over 14 years
    I don't know. I've never looked at the source for Derby.
  • IAdapter
    IAdapter about 14 years
    Why use ANTLR when you can implement your own parser generator?
  • duffymo
    duffymo about 14 years
    Why generate your own parser generator when you can use ANTLR?
  • Han Zheng
    Han Zheng about 13 years
    btw, anltr is under BSD.
  • Thomas Jones-Low
    Thomas Jones-Low over 12 years
    I 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
    Abhishek over 12 years
    Any 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
    duffymo over 12 years
    You 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
    Admin over 11 years
    The best sql parser for Java +1
  • duffymo
    duffymo over 11 years
    We're up to ANTLR 4 now. Perhaps the old grammars don't run on the new version.
  • zato
    zato over 11 years
    zql 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
    CrazyPyro over 10 years
  • kutschkem
    kutschkem about 10 years
    Careful though, the available grammars are not necessarily under the same license as ANTLR
  • Gerold Broser
    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
    Gerold Broser almost 10 years
    @Thilo OJDBC's OracleConnectionWrapper seems like supporting it.
  • quarks
    quarks about 9 years
    I 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
    David Phillips about 9 years
    Statement 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
    zinking about 9 years
    surprised no one mention github.com/porcelli/plsql-parser.git , this is the most comprehensive sql parser I've seen
  • MockedMan.Object
    MockedMan.Object about 9 years
    Would it be possible to get the query tree structure from a given query using Presto?
  • David Phillips
    David Phillips about 9 years
    I'm not sure what you're asking. Can you create a new question with more detail?
  • Lluis Martinez
    Lluis Martinez about 6 years
    I came back again after 10 years :-)
  • Nathan Adams
    Nathan Adams almost 3 years
    ANTLR is great and nice to use but very slow, there's probably faster ones out there
  • duffymo
    duffymo almost 3 years
    Such as? A recommendation or faster code from you would be more helpful.
  • Lluis Martinez
    Lluis Martinez about 2 years
    Presto's parser link is dead