Recent Posts

Jul 23 2010

Implicit Structure behaviour change in CF9

The introduction of ColdFusion 8 saw the addition of implicit structure creation where by you could easily create structures by using {} to create the structure key/value pairs.

For example
<cfset struct = {firstname = "Steve", lastname = "Onnis"} />

Now you notice that example ColdFusion 8 does not allow the key to be quoted.  This means the the following

<cfset struct = {"firstname" = "Steve", "lastname" = "Onnis"} />

generates this error

ColdFusion was looking at the following text:
{

This means that you cannot have a structure created in this way that has keys with spaces in them.

ColdFusion 9 however seems to have changed the manner in which this behaves.  ColdFusion 9 implicit structures now allow you to use quoted values for structure keys when creating structures implicitly.

For example
<cfset struct = {"First Name" = "Steve", "Last Name" = "Onnis"} />

Not sure if this is on purpose as the ColdFusion 9 docs don't mention any change in the behaviour but it is certainly something to look out for.

 

0 comments - Posted by Steve Onnis at 4:23 PM - Categories: Coldfusion

Jul 23 2010

Start ColdFusion Builder and Flash Builder independently

Installing ColdFusion Builder and Flash Builder on my new laptop I wanted to be able to run them separately if I wanted to.  For instance if I just wanted to have a shortcut that launched Flash Builder that's pretty easy as the default perspective for Flash Builder is the Flash Builder perspective.  But what if when I clicked on the shortcut I wanted it to open the ColdFusion Builder perspective?

Well thanks to some pointers in the right direction from Charlie Arehart regarding the start up switches for eclipse I came to the following solution. Now I installed my software to D:\Adobe so the following examples show my install path. I also installed Flash Builder as my base and installed ColdFusion Builder as the plug-in.  If you want to set it up you will need to change the install path to reflect your own setup.

The -perspective switch tells the start-up process which perspective to use, either Flash Builder or ColdFusion Builder. The issue is though that when you start up say Flash Builder and then you click on your other shortcut to open ColdFusion Builder, the Flash Builder instance is already using the workspace so when you open up ColdFusion Builder you will get a message saying jus that and that you need to choose another workspace.  To get around this we can use the -data switch to tell the shortcut what workspace to use. 

Launch to Flash Builder
"D:\Adobe\Adobe Flash Builder 4\FlashBuilder.exe" -perspective com.adobe.flexbuilder.standalone.product -data @user.home/Adobe Flash Builder 4

Launch to ColdFusion Builder
"D:\Adobe\Adobe Flash Builder 4\FlashBuilder.exe" -perspective com.adobe.ide.coldfusion.perspective.CFML -data @user.home/ColdFusion Builder

 

0 comments - Posted by Steve Onnis at 12:58 AM - Categories: Coldfusion | ColdFusion Builder | Flash Builder | General

Jul 22 2010

Extracting a row from a query

I had a friend of mine, Dale Fraser ask me if there was a way to return a row from a query recordset as a structure.  There are many query functions in ColdFusion but sadly there is not one that will just give you a single row from a recordset.  We had a look at queryConvertForGrid() but it just gave back a query object.  What we were looking for was a plain structure object.

Now the simplest way to acheive this would be to create a UDF and loop over each column and create a new structure to return, maybe something like this:

<cffunction name="queryRowToStruct" access="private" returntype="struct">
    <cfargument name="query" type="query" required="true" />
    <cfargument name="row" type="numeric" default="1" />
  
    <cfset var local = {} />
    <cfset local.result = {} />
  
    <cfloop index="local.i" list="#query.columnList#">
        <cfset local.result[local.i] = arguments.query[local.i][arguments.row] />
    </cfloop>
 
    <cfreturn local.result />
</cffunction>

Then i got to thinking, what if we can acheive the same result just using ColdFusion functions without having to do a loop.  I thought what if we could somehow use the new implicit structure creation with {} to create a string and use Evaluate() to create our new sctructure.  Well the string part was easy enough using regular expressions but where it fell over was trying to evaluate a string with {} in it.

Then i thought, "Hey! JSON! I can use the JSON functions to decode a JSON string!". So rather than try to evaluate the string and try to create my structure that way i could just create my JSON style string and use DESerialJSON() to create my structure.  On a side note, I really cant understand why they called the function that.  Array functions start with "array", structure functions start with "struct", spreadheet functions start with "spreadsheet", date functions start with "date", query functions start with "query" and so on. Why not call the JSON functions JSONEncode() and JSONDecode()? Instead they called them SerialJSON() and DESerialJSON() ....go figure!

So here we have one line of nested functions to extract a single row from a query and return it as a structure object instead of a query object.

<cfscript>
    q = queryNew("");
    queryAddColumn(q, "col1", listToArray("a,b,c,d,e,f"));
    queryAddColumn(q, "col2", listToArray("g,h,i,j,k,l"));
 
    function queryRowToStruct(query, row) {
        return DESerializeJson("{#evaluate(DE("#REReplaceNoCase(q.columnList, '\b([^,]*)\b', '"\1" : "##IIF(row GT q.recordCount, DE(''''), ''query.\1[1]'')##"', 'ALL')#"))#}");
        }
</cfscript>
<cfdump var="#queryRowToStruct(q, 2)#">

The process ends up returning a string that looks like this:

{"COL1" : "a","COL2" : "g"}

This is just a JSON string so we can just decode it with DESerializeJson() to create out ColdFusion structure.  The key here i think is the use of the regular expression and using the backreference to inject into our JSON string.

I think this is pretty!  What do you think?

3 comments - Posted by Steve Onnis at 11:59 PM - Categories: Coldfusion | General | Regular Expressions

Previous Posts

Jul 21

CFSETTING in Application.cfc

0 comments - Posted by Steve Onnis at 11:02 AM - Categories: Coldfusion | General |

Jul 7

Flex and ColdFusion Objects

1 comments - Posted by Steve Onnis at 11:17 AM - Categories: Coldfusion | Coldfusion Server | Flex |

Jul 7

Oracle 10g Lite dates and Coldfusion 8/9

0 comments - Posted by Steve Onnis at 11:10 AM - Categories: Coldfusion | Coldfusion Server | Oracle |

Jul 7

Migrating ColdFusion Standard Servers

0 comments - Posted by Steve Onnis at 10:52 AM - Categories: Coldfusion | Coldfusion Server | General |