As you may be aware that when we dump cfquery, it dumps the cfqueryparam values into SQLPARAMETERS of the dump. It's been alright to replace a couple of parameters manually. However, lately I've started working on a project where there are tens(or way more sometimes) of cfqueryparams being used in building the complex db queries.
is there a way to actually print a query with the cfqueryparam values replaced and display a valid sql statement on the screen?
Thanks in advance!
Has there been any plugin or a piece of code that I'm unable to find or it is never written?
I'm not aware of any built in functions or code snippets that do this, but you could write something that works with most simple parameter types. The reason for saying "most* is that there are a few complications
The SQLParameters array doesn't include the original cfsqltypes. So it's not possible to definitively differentiate strings (which must be quoted) from numeric values (which are not).
One way to work around that deficiency is to use the internal debugging service, which does include the cfsqltypes in its output. An obvious disadvantage is that it requires debugging be enabled, and utilizes an undocumented internal class. However, since this kind of task isn't something you'd normally be running in production anyway, it's an acceptable limitation IMO.
Unfortunately, none of the available options include cfqueryparam's
null
attribute. Since Adobe chose to treat nulls as an empty string in the parameter list, there's no way to determine when a parameter value isnull
and when it's actually an empty string""
. Sadly there's not much you can do it about that. Probably the best you can do is choose whatever default works for your application:null
or empty string.Syntax and supported data types vary by vendor, so any code will likely need to be adjusted for each dbms. Especially for less common or complex types like
refcursor
orblob
.There's also the issue of handling single quotes embedded within string parameter values. Using replace() to escape single quotes should do the trick, but there may be edge cases.
You didn't mention your dbms, but here's a VERY rough starter example for SQL Server. You'll have to decide how/if to handle less common types like
refcursor and
blob` (currently returns "{{unhandled_type_(typeName)}}".Demo:
Original Query
Output
UDF's