Eleven Coldfusion-ish tips from the field

I’ve had this running list of Coldfusion tips on my wall for the last few years and it’s time to get these online.  All of the items in this list came from Coldfusion projects over the last few years, but a good portion of these could easily be considered tips for server programmers.  I definitely run into the same items when programming Asp.NET.

There is no rhyme or reason here, just some things I felt need to be repeated.

1. PreserveSingleQuotes()

This one came in really handy on a project requiring large text files to be imported into a MySQL database.  I used Coldfusion to upload and read the files into large INSERT chunks using MySQL’s multi – row INSERT syntax. Code built the VALUES portion of the SQL, then I just fed the data into a function for insertion.

[sql]
INSERT INTO tbl_name (a,b,c) VALUES(1,2,3),(4,5,6),(7,8,9);
[/sql]
[coldfusion]
<cfquery name="insert_data" result="insert_result" DATASOURCE="#request.dsn#" USERNAME="#request.dbuser#" PASSWORD="#request.dbpswd#">
INSERT INTO table
( column1, column2, column3, column4, column5, column6, column7 )
VALUES
#PreserveSingleQuotes( insert_values )#
</cfquery>
[/coldfusion]

2. What if GENERATED_KEY doesn’t work?

If you’re using MySQL and the GENERATED_KEY property of your cfquery objects isn’t populating, you can use LAST_INSERT_ID instead.

[sql]SELECT LAST_INSERT_ID();[/sql]

3. Use ArrayAppend when building strings

Classic performance tuning tip for just about any programming language.  Here I’ll give a Coldfusion example and keep it dead simple.  If you ever have to concatenate strings in code, user ArrayAppend instead.  Here are two loops that do the same thing.  If you run this code, you should notice loop1 takes forever, and loop2 is smoking fast.

slow…..

[coldfusion]
<cfscript>
xx = 100000;
insertString = "";

// do the loop
while( xx > 0 ) {
insertString &= xx & " ";
xx–;
}

WriteOutput( insertString);
</cfscript>
[/coldfusion]

FAST!

[coldfusion]
<cfscript>
xx = 1000000;
insertArray = ArrayNew(1);
// do the loop
while( xx > 0 ) {
ArrayAppend( insertArray, xx & " " );
xx–;
}

WriteOutput( ArrayToList( insertArray, " " ) );
ArrayClear( insertArray );
</cfscript>
[/coldfusion]

4. If CSV, then CHR

This one is simple, if you find yourself creating CSV or any other text file, use special characters when dealing with single and double quotes, etc.

  • chr(9) = Tab
  • chr(34) = ” double quote
  • chr(39) = ‘ single quote

And if you’re not sure of the correct code for the character you’re looking to use, just wrap that character in ASC() and WriteOutput to the page.

5. Use CFMail with GMail

This is a no brainer, but with how difficult sending email can be with other languages, I’m mentioning it here.

Application.cfc

[coldfusion]
<cfscript>
APPLICATION.mail.server = "smtp.gmail.com";
APPLICATION.mail.port = "465";
APPLICATION.mail.ssl = true;
APPLICATION.mail.user = "gmailAccount";
APPLICATION.mail.pswd = "gmailPasssword";
</cfscript>
[/coldfusion]

Emailer.cfm

[coldfusion]
<cfmail to="work@ericfickes.com"
bcc=""
from="web@master.com"
subject="sending mail is easy with Coldfusion"
server="#application.mail.server#"
useSSL="#application.mail.ssl#"
port="#application.mail.port#"
username="#application.mail.user#"
password="#application.mail.pswd#">
#emailBody#
</cfmail>
[/coldfusion]

6. CFSCRIPT doesn’t know NULL?

Another tip from the land of importing and exporting data.  While working with query objects in CFScript, for some reason I could never accurately detect for NULL values.  I tried all sorts of detection schemes and ended up just writing a hacky fail safe.  Please, if you have a better suggestion for *easy* NULL detection in CFSCript, add it in the comments below.

utils.cfc

[coldfusion]
<!— Simple value getter with try / catch to get around NULL values
This function originated in a script where we always needed a " " even if
the value from the database was null.
—>
<cffunction name="qryGetString" access="public" returntype="string">

<cfargument name="data" type="string">

<cftry>
<cfscript>
return #data# & " ";
</cfscript>

<cfcatch type="Any">
<cfreturn " "/>
</cfcatch>
</cftry>

</cffunction>
[/coldfusion]

Exporter.cfm

[coldfusion]
// largeish loop
tab = chr(9);
for( xx = 1; xx <= queryObj.RecordCount; xx++ )
{
// start the row
this_row = utils_cfc.qryGetString( queryObj.FirstName[xx] ) & tab &
utils_cfc.qryGetString( queryObj.MiddleName[xx] ) & tab &
utils_cfc.qryGetString( queryObj.LastName[xx] ) & tab &
utils_cfc.qryGetString( queryObj.Suffix[xx] ) & tab &
utils_cfc.qryGetString( queryObj.MedicalTitle[xx] ) & tab &
utils_cfc.qryGetString( queryObj.email[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_phone[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_fax[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_name[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_address1[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_address2[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_city[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_state[xx] ) & tab &
utils_cfc.qryGetString( queryObj.practice_zipcode[xx] ) & tab &
utils_cfc.qryGetString( queryObj.hospital_affiliation[xx] ) & tab;

// do stuff with the data
}
[/coldfusion]

7. How I find list items

Ever notice the different behavior in the Coldfusion ListFind commands?  I ended up writing my own ListHasValue function in order to find exact pattern matching in a list.  I had a list of role ids in a list, and just couldn’t get the built in functions to tell me when my id was in the list without also matching on other ids.  This one makes sense when you run some code.

The top of this sample as my custom ListHasValue() command, and the lower half does three simple loops counting from 1 to 100, and using ListFind, ListContains, and ListHasValue for number checking against the same list.

[coldfusion]
<!—
Use this to do an exact pattern check for a value in a list.
This is useful inside of a loop checking numbers against a number list.

EX :

list = "91, 92"

if you loop from 1 – 100, ListFind and related CF functions will match
on 1, 2, and 9. Not just 91 and 92

Use this when you’re looping and you ONLY want to match on 91, or 92

—>
<cffunction name="ListHasValue" access="public" returntype="boolean">

<cfargument name="list" required="yes" type="string">
<cfargument name="value" required="yes" type="any">

<cfscript>
// clean up to be safe
list = trim( toString( list ) );

// check to see if we have a *possible* match
position = ListContains( list, value ) ;

if( position > 0 )
{
// NOTE : KEEP THE TRIM AND TOSTRING
found_value = trim( toString( ListGetAt( list, position ) ) );

if( Compare( value, found_value ) == 0 )
{
return true;
}
}

// no match for you!
return false;
</cfscript>

</cffunction>

<cfscript>
list = "1, 11, 21, 31, 41, 51, 61, 71, 81, 91, 99";
xx = 1;

WriteOutput( "List = " & list & "<hr>");

while( xx < 100 ) {
if( ListContains( list, xx ) > 0 )
{
WriteOutput( "ListContains found " & xx & "<br>" );
}
xx++;
}

WriteOutput("<hr />");

xx = 1;
while( xx < 100 ) {
// ListFind
if( ListFind( list, xx ) > 0 )
{
WriteOutput( "ListFind found " & xx & "<br>" );
}
xx++;
}

WriteOutput("<hr />");

xx = 1;
while( xx < 100 ) {
// Eric’s ListHasValue
if( ListHasValue( list, xx ) )
{
WriteOutput( "ListHasValue found " & xx & "<br>" );
}
xx++;
}
</cfscript>
[/coldfusion]

If you run this code on your Coldfusion server, you should notice the following results.  ListContains matches single digits from the loop that do not really exist in the list.  ListFind only finds the number 1?  And finally, my function does exactly what I needed it to do.  Tell me when a specific number exists in a list.

CF ListFind function comparison

Coldfusion ListFind functions don't always behave how I want them to


8. Make PDFs faster

This could easily be it’s own topic, but I’ll say one thing about making PDFs faster with CFDocument.  Only put final content between <cfdocument> and </cfdocument>.  That is, if you have any processing code, cfqueries, cfloops, inside of your cfdocument tag, your cfml page is running slower than it needs to be.  Here’s a simple example of one of my cfml pages that has only final content in the cfdocument tags.

The key to this example is moving all of my content creation code into an external file, then including at the top of my page. I always do a check for my main PDF_BODY variable, and then spit out my PDF document.

[coldfusion]
<cfinclude template="code/export_pdf_codefile.cfm">

<cfif PDF_BODY NEQ "">

<cfdocument name="provider_profile"
format="PDF"
pagetype="A4"
mimetype="application/pdf"
orientation="portrait"
margintop="0"
marginbottom="0.2"
marginleft="0.2"
marginright="0.2"
>
<cfoutput>#PDF_BODY#</cfoutput>
</cfdocument>

<!— send directly to client —>
<cfheader name="Content-Disposition" value="attachment; filename=#filename#">
<cfcontent type="application/pdf" variable="#provider_profile#">

<cfelse>
No PDF content found
</cfif>
[/coldfusion]

9. Use parameterized queries

This is a tip for all server side programmers whether you use Coldfusion, ASP, JSP, PBJ.  Use parameterized queries when doing any database interaction.  It’s too easy not to use, and you get protection from SQL Injection, as well as enforcing proper data types when speaking to your database.  This is something all server programmers should do regardless of your language, the sample below is for Coldfusion.

BAD

[coldfusion]
<cfquery name="tblInsert" datasource="myDb">
INSERT INTO myTable
( col1, col2, col3, col4 )
VALUES
( ‘#Form.field1#’, ‘#Form.field2#’, ‘#Form.field3#’ )
</cfquery>
[/coldfusion]

GOOD

[coldfusion]
<cfset val1 = Form.field1>
<cfset val2 = Form.field1>
<cfset val3 = Form.field1>

<cfquery name="tblInsert" datasource="myDb">
INSERT INTO myTable
( col1, col2, col3, col4 )
VALUES
(
<cfqueryparam cfsqltype="cf_sql_varchar" value="#val1#" />,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#val2#" />,
<cfqueryparam cfsqltype="cf_sql_varchar" value="#val3#" />
)
</cfquery>
[/coldfusion]

10. Where’d the time go?

For the good programmers already using parameterized queries, ever insert a timestamp into your database and find out the date is correct, but the time is always 12:00:00?  Take a closer look at the cfsqltype in your cfqueryparam, I had this exact problem and here’s what happened.

Using cf_sql_date does not include the full date and timestamp, just the date.

[coldfusion]
<cfqueryparam cfsqltype="cf_sql_date" value="#paymentDate#" />
[/coldfusion]

 

Using cf_sql_timestamp includes the full date and timestamp I was looking for.

[coldfusion]
<cfqueryparam cfsqltype="cf_sql_timestamp" value="#paymentDate#" />
[/coldfusion]

11. Stored Procedures are a little different

This last one isn’t much of a tip, but more of a reminder to myself.  I do so much database work that stored procedures are just queries to me, but not so to Coldfusion and the CFQuery tag.  If you want to get data from a stored procedure, you need to use the CFStoredproc tag.  Here’s a sample of passing one argument into a stored procedure, and how to get the resulting data.

[coldfusion]
<cfstoredproc datasource="myDb" procedure="GetDataFaster">

<cfprocparam type="in" cfsqltype="cf_sql_integer" value="#inputVar#" />

<!— specify sproc result here, cfstoredproc res != returned recordset —>
<cfprocresult name = sprocResult>

</cfstoredproc>

<cfreturn #sprocResult.ColumnFromQuery#>
[/coldfusion]