tags:

views:

70

answers:

4

Hi is there any tool available in Java world that will parse/read a source file and pull SQL statements out in to a text file. This is a complex task given that you can write SQL statements in different fashion within the source (ex: using + sign or using .append()) or even conditional building of SQL.

+1  A: 

Not that I've ever come across. The few times that people have had to do something like this, they have ended up writing a program to do it, because it's not really something you can generalise.

Derek Clarkson
+2  A: 

We've been considering doing this. This requires:

1) A full Java parser

2) Full control and data flow analysis, so that you can track how values propagate through the code

3) Recognition of SQL (JDBC) calls, tracking data flows for the SQL query back to their origins, and symbolically intrepreting the set of operations along the data flow paths to determine the apparant SQL operation.

The DMS Software Reengineering Toolkit is a generic engine for parsing langauges. DMS has a full Java front end (meets condition 1) that computes local control and global data data flow analysis (condition 2). DMS is configurable to extract code properties, so it could be configured we think to do 3. Still not a trivial task.

But alas, the requested tool is not available yet.

Ira Baxter
+1  A: 

Have you considered having the SQL for the prepared statements in a property file (or language resource bundle) and read them from there?

Thorbjørn Ravn Andersen
Hi - yes, and that is what I was trying to achieve.Please go through the question again(Guess it is clear enough) - I am looking for a tool that will pull out (externalize) SQLs from java source code that was written by someone a few years ago.
ring bearer
Your best bet is a package that will allow internationalization of existing code, to extract all strings and label them. I have no personal experience with such a tool
Thorbjørn Ravn Andersen
A: 

Check www.antlr.org, they have grammars for PL/SQL, MySQL and Java 1.6. If you only interested in SQL, probably SQL grammar could be something you can build upon. There could be easier ways about it thought, like reading pseudo SQL from JDBC driver log/wire/from database server log will produce more consistent syntax.

pavel