Dynamic Data Types For ColdFusion Query of Queries

SQL/Queries , ColdFusion , Meld FormBuilder Add comments

An important part of the Meld FormBuilder application I'm working on is the Meld DataProvider, a utility which in this case provides lists for multi-choice items for dropdowns, radioboxes, etc.  I want these lists to hold typed and validated data, and to be able to sort on the secondary columns that can be added beyond the "label", but also want to store all the values in a single database table.  The problem that arises is that we also need to be able to sort results on any of the columns, which because of their generic nature are all type "VARCHAR".

As always, the robust swiss army knife that is ColdFusion comes to the rescue.  The solution is in the ability to CAST datatypes on query columns in a Query of Queries (QoQ) query, regardless of whether it is a manually created query or one drawn from a database.  Take for instance the following dataset:

label value1 value2 value3 value4
Alice 10/10/2009 98 23.17 10:23
Bob 04/11/2002 23 80.23 8:25 PM
Judy 03/05/2010 4 7.77 23:03

 

In this example, all of the database table columns are 'varchar' data types.  In this case I am ordering the query on the label column.  If, however, I do an 'ORDER BY' on the value1 column, I get the following:

label value1 value2 value3 value4
Judy 03/05/2010 4 7.77 23:03
Bob 04/11/2002 23 80.23 8:25 PM
Alice 10/10/2009 98 23.17 10:23

 

This is obviously not the result I desired, as the dates are sorted as strings.  In order to have the dates sort properly, I use the following:

... which produces the following results:

label value1 value2 value3 value4 sortcolumn
Bob {ts '2002-04-11 00:00:00'} 23 80.23 {ts '1899-12-30 20:25:00'} {ts '2002-04-11 00:00:00'}
Alice {ts '2009-10-10 00:00:00'} 98 23.17 {t '10:23:00'} {ts '2009-10-10 00:00:00'}
Judy {ts '2010-03-05 00:00:00'} 4 7.77 {t '23:03:00'} {ts '2010-03-05 00:00:00'}

 

Now the columns are sorted correctly.  You may also notice that for some reason ColdFusion has also data typed the value4 column as well.  This aberration in the metadata applied to the table isn't really that desirable (what unrequested change in your data set ever is?) but for now I'm focusing upon the desired results, which the CAST function in my QoQ has achieved.

For a more flexible approach, you can use the following QoQ function:

This function will return data-typed sorts on your string-columned table.  You could round this out by including all of the supported data types (ColdFusion currently supports BINARY, BIGINIT, BIT, DATE, DECIMAL, DOUBLE, INTEGER, TIME, TIMESTAMP, and VARCHAR data type casting).

One significant caveat of this is that none of your sorts are going to be using original indexes (which you wouldn't want anyway, since they'd be string-sorted indexes).  A second is that you will have to strictly validate the content going into these columns, as even a single non-castable value in your column will cause the whole thing to go pear-shaped.

The main advantage is that you don't have to develop any custom sorting techniques, and that you can easily build upon or modify the results using any of the other wonderful QoQ functions.  It also maintains your data as a ColdFusion query, so you aren't suddenly restricted to manipulating your data via arrays or structures.

 

2 responses to “Dynamic Data Types For ColdFusion Query of Queries”

  1. martin Says:
    the undesired cast looks like a bug in the cfml engine
  2. Adam Cameron Says:
    Agreed: looks like a bug to me. You might want to bring it to Adobe's attention:
    http://cfbugs.adobe.com/cfbugreport/flexbugui/cfbugtracker/main.html

    (If you raise it and report back with the number, I'll vote for it because QoQ needs all the enhancement it can get!)

    --
    Adam

Leave a Reply

Leave this field empty:

Powered by Mango Blog. Design and Icons by N.Design Studio
Clicky Web Analytics