Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL parser library for Java - Retrieve the list of table names present in a SQL statement

Tags:

java

sql

parsing

I am looking for a SQL Library that will parse an SQL statement and return some sort of Object representation of the SQL statement. My main objective is actually to be able to parse the SQL statement and retrieve the list of table names present in the SQL statement (including subqueries, joins and unions).

I am looking for a free library with a license business friendly (e.g. Apache license). I am looking for a library and not for an SQL Grammar. I do not want to build my own parser.

The best I could find so far was JSQLParser, and the example they give is actually pretty close to what I am looking for. However it fails parsing too many good queries (DB2 Database) and I'm hoping to find a more reliable library.

like image 430
Mario Duarte Avatar asked Apr 06 '11 20:04

Mario Duarte


1 Answers

I doubt you'll find anything prewritten that you can just use. The problem is that ISO/ANSI SQL is a very complicated grammar — something like more than 600 production rules IIRC.

Terence Parr's ANTLR parser generator (Java, but can generate parsers in any one of a number of target languages) has several SQL grammars available, including a couple for PL/SQL, one for a SQL Server SELECT statement, one for mySQL, and one for ISO SQL.

No idea how complete/correct/up-to-date they are.

http://www.antlr.org/grammar/list

like image 94
Nicholas Carey Avatar answered Sep 28 '22 23:09

Nicholas Carey