views:

107

answers:

3

I'm currently looking at a terrible legacy ColdFusion app written with very few stored procedures and lots of nasty inline SQL statements (it has a similarly bad database too).

Does anyone know of any app which could be used to search the files of the app picking out any SQL statements and listing the tables/stored procedures which are referenced?

+1  A: 

In short: no. You'd have to do alot of tricky parsing to make sure you get all the SQL. And because you can glob SQL together from lots of strings, you'll almost always miss some of it.

The best you're likely to do will be a case insensitive grep for "SELECT|INSERT|UPDATE|DELETE" and then manually pulling out the table names.

Depending on how the code is structured, you might be able to get the table names by regexing the SQL from clause. But that's not foolproof. Alot of people use string concatenation to build SQL statements. This is bad because it can introduce SQL injection attacks, and it also make this particular problem harder.

nont
At least with ColdFusion you can probably grep pretty easily for <cfquery> ... of course if they manipulated the SQL outside of the tag that doesnt help so much.
ryber
And <cfstoredproc> (although, I note, there will be many fewer of those).
Al Everett
Checked and `<cfstoredproc>` does not occur. Not once!
mdresser
+2  A: 

Dreamweaver will allow you to search the code of the entire site. If the site is setup properly including the RDS password and provide a data source it can tell you a lot of information. I've only set it up once so I can't remember exactly what information it gives you, I think maybe just the DB structure. Application window > databases. Even if it isn't set up properly just searching for "cfquery" will quickly find all your queries.

You could also write a CF script using CFDirectory/CFFile to loop the .cfm files and parse everything between cfquery and /cfquery tags.

CFBuilder may have some features like that but I'm not to familiar with it yet.

edit I've heard that CFBuilder can't natively find all your cfqueries that don't have cfqueryparam but you can use CF to extend CFB to do so. I imagine you could find/write something for CFB to help you with your problem.

another edit

I know it isn't indexing the contents of the query, but you can use regex to search using the editor as well. searching for <cfquery.+(select|insert|update|delete) checking the regex box should find the queries that aren't using cfstoredProc (be sure to uncheck the match case option if there is one). I know Dreamweaver and Eclipse can both search for Regex.

HTH

Travis
I dig the down vote, but if something is incorrect or not helpful it should be stated why. Anonymous down voting is cowardly.
Travis
I've used Dreamweaver for this purpose quite extensively. Just ctrl-F and search for "<cfquery" or "<cfstoredproc" or "<cftransaction". This is much faster than writing custom code to search for specific code. People can use other code editors or grep if they don't like Dreamweaver for some reason, but it'll do the job nicely.
Dan Sorensen
+1  A: 

As mentioned above I would try a grep with a regex looking for

"<cfquery*" "</cfquery>" and "<cfstoredproc*" "</cfstoredproc>"

In addition if you have tests that have good code coverage or even just feel like the app is fully exercised in production you could try turning on "Log Database Calls" in Admin - > Datasources or maybe even at the JDBC driver level, just monitor performance to make sure it does not slow the site down unacceptably.

kevink