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.
views:
70answers:
4Not 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.
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.
Have you considered having the SQL for the prepared statements in a property file (or language resource bundle) and read them from there?
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.