Category: Coldfusion

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

Jul 21 2010

CFSETTING in Application.cfc

With the introduction of the Application.cfc the <CFSETTING> tag could no longer be just put at the top of your Application file.  To use it you would need to include it in your OnRequest method of your Application.cfc.

Now a new problem has arisen.  With ColdFusion 9 you can now create your ColdFuson components using purely CFSCRIPT.  Many tags now have a CFSCRIPT equivilent which is great but one tag seems to be missing from the CFSCRIPT equivilents, the <CFSETTING> tag.  So the question is if there is no way to apply cfsetting contraints via CFSCRIPT, how would you go about applying a global setting through your Application.cfc?

The only way you can really do it is to include an external cfm file into your request function using the CFSCRIPT include like this

component
    hint = "That Application CFC"
    {
    this.name = "applicationName";

    public void function onRequestStart(targetPage) {
        include "settings.cfm";
        }
    }

The "settings.cfm" file would then contain your <CFSETTING> tag with the required attributes.

 

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

Jul 7 2010

Flex and ColdFusion Objects

Recently ran into something very strange. I was returning a structure to Flex from a ColdFusion Component and FLEX was receiving the structure keys as all UPPERCASE.

Take the following component.

<cfcomponent>
 <cffunction name="helloWorld" access="remote">
  <cfset var result = structNew() />
  <cfset result.msg = "Hello World!" />
  <cfreturn result />
 </cffunction>
</cfcomponent>

When called from Flex, the result will come back as

result.MSG

I guess ColdFusion doesn't see the case being used in the struct.key assinment.  If you want to retain the case of your structure keys you need to assign them like this..

<cfcomponent>
 <cffunction name="helloWorld" access="remote">
  <cfset var result = structNew() />
  <cfset result["msg"] = "Hello World!" />
  <cfreturn result />
 </cffunction>
</cfcomponent>

Creating your structures this way will retain the case of your keys and Flex will receive them in the way you have named them.

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

Jul 7 2010

Oracle 10g Lite dates and Coldfusion 8/9

If you are running Oracle 10g databases and Coldfusion Standard 8 or 9 you may experience a problem with ColdFusion returning dates with a 00:00 time value.   This is a driver issue.  The driver that comes with the Oracle 10g database is for JVM 1.4 and 1.5.  ColdFusion 8 actually ships with JVM 1.6.  What you need to do it download and install the newer driver for the Oracle 11g database. I don't believe there is a lot of difference but the main one is that it is for the JVM 1.6 platform.  Installing the updated driver will correct the date/time issues.

If you are using ColdFusion Enterprise and you are using the packaged Oracle drivers you don't need to worry about this as they are correct drivers.

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

Jul 7 2010

Migrating ColdFusion Standard Servers

We all know that you can export ColdFusion Server settings from one server and import them into another server, but this functionality is only available on Enterprise edition servers which makes migrating Standard servers a bit of a pain.

As you may know, the Developer edition of ColdFusion server has all the functionality of the Enterprise edition so why not just revert your server back to Developer edition so you can export your settings.  Sounds easy enough, just log into the admin and enter "Developer" into the serial number field and update. But no....this is actually an invalid serial number. So what we need to do is manualy modify the file that contains your ColdFusion server license called "license.properties".  This file can be found in the "<CFINSTALL>\lib" directory, so if you have a default install for ColdFusion 8 it would be in "C:\Coldfusion8\lib\license.properties".

The file will look something like this:

#CF License File
#Mon Jul 05 23:36:03 EST 2010
appserver=0V\=LUFWP]J(Z^V)[G?G5'C@  \n
code=-4579634782920590441
sn=1234567890123456
listen=true
user=
trial_sn=
installlanguage=en
installtype=standalone
company=
previous_sn=
vendor=0V\=LUFWP]J(Z^V)[G?G5'C@  \n

What you need to do is change the "sn" value from your serial number to "Developer", restart your ColdFusion server and presto! you can now export your ColdFusion server settings.  Make sure you save your serial number so you can change it back once you are done.

This same method applies for Coldfusion7/8 and 9.

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