3

I am in need of a java API to parse a SQL statement.

For example, I have a SQL query

SELECT
  PRODUCTS.PROD_ID,
  PRODUCTS.PROD_NAME,
  PRODUCTS.PROD_CATEGORY,
  PRODUCTS.PROD_TOTAL_ID
FROM
  PRODUCTS

I need to retrieve the table name "Products" and the column names as "Prog_ID" etc.

Please provide any links to tutorials or code snippets if possible.

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
user727272
  • 475
  • 2
  • 9
  • 20
  • 1
    possible duplicate of [SQL parser library for Java](http://stackoverflow.com/questions/660609/sql-parser-library-for-java) – codingbadger Jun 15 '11 at 12:51

4 Answers4

4

ANTLR is a parser generator that has a SQL grammar, but that might be more than you bargained for.

It would be simpler to write one on your own, as long as your requirements didn't get too crazy. How general do you need to be?

Gábor Lipták
  • 9,646
  • 2
  • 59
  • 113
duffymo
  • 305,152
  • 44
  • 369
  • 561
  • 5
    Been there done that! Parsing SQL is a bit of a nightmare -- you need to parse a specific dialect (ORACLE,SQLServer etc.). The vast number of keywords, the inconsistent syntax, the overloading of keywords means you need a massive and complex grammar. – James Anderson May 04 '11 at 10:42
  • I have a very simple requirement, the query posted above is the only format i ll be using. – user727272 May 04 '11 at 12:36
  • @user727272, in that case, you can construct the ANTLR grammar to suit only that format, and parse it. I would suggest getting a copy of Terrence Parr's book - [The Definitive ANTLR Reference](http://pragprog.com/titles/tpantlr/the-definitive-antlr-reference), if you are finding the tutorials on the ANTLR site to be difficult. – Vineet Reynolds Jun 15 '11 at 06:45
  • 1
    parsing SQL is beyond a nightmare... see the post i just made: http://stackoverflow.com/questions/12863532/java-api-for-parsing-recognition-but-not-executing-sql-against-a-db – amphibient Oct 12 '12 at 17:13
  • No wonder - you tried to mangage vendor-specific SQL with regex? Your situation sounds like a case study for "do not do this." – duffymo Oct 12 '12 at 17:17
3

If you have a connection to the database, you can do that using a prepared statement:

String sql = "....";
PreparedStatement pstmt = connection.prepareStatement(sql);
ResultSetMetaData meta = pstmt.getMetaData();

Then you can use ResultSetMetaData.getColumnCount() and getColumnName(int) to retrieve the column names. If your JDBC driver supports it, you can even get the underlying table name using getTableName(int)

Note that not all drivers support getting the metadata before actually executing the statement, you need to test that with the one you are using.

2

Ended up using ZQL Parsing libraries from http://zql.sourceforge.net/.

If you are having simple queries , that should do the job easily

user727272
  • 475
  • 2
  • 9
  • 20
1

Regex matching is only work for simple query, SQL has a complex recursive grammer, and, there will always be some sub select, group by, or literal that will break your regex based parser.

Here is a demo illustrates how to achieve what you need by using general sql parser:

James Wang
  • 453
  • 4
  • 5