Thursday, January 24, 2013

Debugging SQL in ColdFusion 9 & 10

There may be times you would like to see the actual SQL statement that is being sent to the database for execution, especially when you are trying to diagnose a problem. When dynamically creating SQL statements and using CFQUERYPARAM within the SQL, the resulting SQL will have question marks as place holders of parameter values attached to the end of the SQL statement. In order to see both the SQL statement and the parameters use "queryName.getMetaData().getExtendedMetaData()". See the example below:



<cfquery name="myQuery" datasource="myDatabase">
   INSERT INTO Contacts (name, birthdate)
   VALUES(
       '#form.name#',
       <CFQUERYPARAM VALUE="#form.birthDate#" null="#NOT IsNumericDate(form.birthDate)#");
</cfquery>

<cfoutput>
   <cfdump var="#myQuery.getMetaData().getExtendedMetaData()#"/>
</cfoutput>

Tuesday, January 8, 2013

ColdFusion 10's CGI PATH_INFO Variable is Empty

A couple years ago I wrote a web app using ColdFusion 8 development server. I had wrote a method that I used to store the page name and query string of the current page into a session variable called "returnPage". I recently upgraded my developent server to ColdFusion 10 and I noticed my "returnPage" session variable was never being populated with any text. Doing some investigating, I discovered that the CGI variable named PATH_INFO (which in ColdFusion 8 stored the page name) is an empty string in ColdFusion 10. Doing a quick Google search, appears other's have ran into this same issue and reported it as a bug with ColdFusion 10. For the workaround, I chose to use ColdFusion's function GetFileFromPath() and passed in the CGI variable CF_TEMPLATE_PATH. Now the session variable "returnPage" contains the desired page name. Below is the old code and modified code:

Old Code:


<cfcomponent>

   <cffunction name="setReturnPage">

      <!--- This function sets the session variable returnPage to the current URL address --->

      <cfif cgi.QUERY_STRING IS NOT "">
         <cfset session.returnPage = "#cgi.PATH_INFO#" & "?" & "#cgi.QUERY_STRING#" >
      <cfelse>
         <cfset session.returnPage = "#cgi.PATH_INFO#">
      </cfif>

   </cffunction>

</cfcomponent>
Updated Code:

<cfcomponent>

   <cffunction name="setReturnPage">

      <!--- This function sets the session variable returnPage to the current URL address --->

      <cfif cgi.QUERY_STRING IS NOT "">
         <cfset session.returnPage = "#GetFileFromPath(cgi.CF_TEMPLATE_PATH)#" & "?" & "#cgi.QUERY_STRING#" >
      <cfelse>
         <cfset session.returnPage = "#GetFileFromPath(CF_TEMPLATE_PATH)#">
      </cfif>

   </cffunction>

</cfcomponent>