Using The ColdFusion Query Object As A Complex Object Iterator

Posted August 23, 2006 at 2:31 PM

Tags: ColdFusion

Let me preface this by saying this is probably not useful, but kind of sweet anyway. Did you know that you can store complex data inside of a query object? It's true; you can pretty much store any kind of data inside of a ColdFusion query object. To demonstrate that, let's try to use the query object as an iterator of complex data structures. This, in and of itself, could be kind of cool, because the query object has some methods that are nice for iteration.

First, let's build a query from scratch with a single column that houses structures:

 Launch code in new window » Download code as text file »

  • <!---
  • Create a query with only data column to hold
  • out complex objects.
  • --->
  • <cfset qGirls = QueryNew( "data" ) />
  •  
  • <!--- Add rows to query. --->
  • <cfset QueryAddRow( qGirls, 3 ) />
  •  
  • <!--- Set row data. --->
  • <cfset qGirls[ "data" ][ 1 ] = StructNew() />
  • <cfset qGirls[ "data" ][ 1 ].Name ="Thomas, Ashley" />
  • <cfset qGirls[ "data" ][ 1 ].IsSexy = true />
  • <cfset qGirls[ "data" ][ 1 ].HowSexy = "Very" />
  •  
  • <!--- Set row data. --->
  • <cfset qGirls[ "data" ][ 2 ] = StructNew() />
  • <cfset qGirls[ "data" ][ 2 ].Name ="Fisakis, Anne" />
  • <cfset qGirls[ "data" ][ 2 ].IsSexy = true />
  • <cfset qGirls[ "data" ][ 2 ].HowSexy = "Very" />
  •  
  • <!--- Set row data. --->
  • <cfset qGirls[ "data" ][ 3 ] = StructNew() />
  • <cfset qGirls[ "data" ][ 3 ].Name ="Cox, Christina" />
  • <cfset qGirls[ "data" ][ 3 ].IsSexy = true />
  • <cfset qGirls[ "data" ][ 3 ].HowSexy = "Very" />

As you see, for each query row, I am setting the value of [data] to a StructNew() and then treating the value as a struct. If you dump this out, you will see that everything is fine and dandy:


 
 
 

 
CFDump: Using ColdFusion Query As A Complex Object Iterator  
 
 
 

Now, let's loop over it and output the data values:

 Launch code in new window » Download code as text file »

  • <!--- Loop over girls. --->
  • <cfloop query="qGirls">
  •  
  • <!--- Get current girl. --->
  • <cfset objGirl = qGirls.data />
  •  
  • <!--- Output data. --->
  • #objGirl.Name#<br />
  • #objGirl.IsSexy#<br />
  • #objGirl.HowSexy#<br />
  •  
  • <!--- Check for break. --->
  • <cfif NOT qGirls.IsLast()>
  • <br />
  • </cfif>
  •  
  • </cfloop>

Works as you would *hope* it to. Well, that's not 100% true. There are some caveats here. The biggest one is that you CANNOT reference the struct directly as in:

 Launch code in new window » Download code as text file »

  • <cfset strName = qGirls.data.Name />

This throws the error:

Element DATA.NAME is undefined in QGIRLS

That is lame as that is what you would want to do naturally. To overcome this, you have two options: you can either do what I did above, by creating a intermediary "objGirl" structure, then referencing that, or you can use structure notation as in:

 Launch code in new window » Download code as text file »

  • <cfset strName = qGirls[ "data" ][ 1 ].Name />

So that is a not-great caveat, but notice, on the other hand, that we can leverage other great query features like IsFirst() and IsLast(). No more comparing the current index to the array length (or however else you would do this sort of loop). Plus, you can use things like StartRow and EndRow:

 Launch code in new window » Download code as text file »

  • <!--- Loop over first 2 rows. --->
  • <cfloop query="qGirls" startrow="1" endrow="2">
  • ....
  • </cfloop>

So, like I said, maybe not useful, but a kind of down-and-dirty way to use the query object. Plus, I am sure I am not alone on this, but isn't the query loop much nice to look at than an index loop?

Download Code Snippet ZIP File

Post Comment  |  Ask Ben  |  Print Page




Reader Comments

Aug 23, 2006 at 10:39 PM // reply »
153 Comments

Scary timing. I was just yesterday coding up a plugin-management CFC base-class/factory that uses objects inside of queries.

And yeah, cfloop with a query is so much sexier than without.


Aug 23, 2006 at 10:48 PM // reply »
74 Comments

Yeah, Rick, that's what i'm talking about! Sexy!


Aug 26, 2006 at 9:30 PM // reply »
1 Comments

Actually Ben your post was extremely helpful to me. I don't know if you use the report builder, but in that you are allowed to pass information from only one query into the report (not including any additional parameters passed in when the report is called). Anyway, before I read your post I had been imbedding subreports within subreports to get an output that was passable, but not great. Now I can pass all kinds of information along in the query by imbedding them in structures. Thanks!


Aug 27, 2006 at 11:39 AM // reply »
74 Comments

Cameron,

I have to say, I have only studies the report builder, but never actually used it. I am glad that this helps though. I am aware that you can pass queries into reports at run time, but passing a query of structs is wild :) Rock on.


Dec 15, 2008 at 3:42 PM // reply »
1 Comments

You player, you!


Post Comment  |  Ask Ben

Recent Blog Comments
Feb 8, 2010 at 6:25 PM
Muscle: Confessions Of An Unlikely Bodybuilder By Samuel Wilson Fussell
Hello Ben, Talk about synchronicity!! I just saw a copy of this book last night. Was intrigued with it and decided to see what the author was up to these days. Did a google search and arrived here se ... read »
Feb 8, 2010 at 4:47 PM
How To Create GStrings In Javascript By Extending Core Data Types
@Garrett, Very interesting. I'll have to give that a look. I don't think I have seen that before. ... read »
Feb 8, 2010 at 4:43 PM
How To Create GStrings In Javascript By Extending Core Data Types
Sick stuff Ben! Ironically there is an interesting project called fusebox that takes on the idea of "safely" extending JavaScript. http://github.com/jdalton/fusebox ... read »
Feb 8, 2010 at 4:19 PM
Converting An IP Address To An Integer Using MySQL (Thanks Julian Halliwell)
@Rob Yes, as I said it's CF9 that seems to return byte arrays from this and certain other MySQL functions such as GREATEST(). To run Ben's code as is you just need to enable multiple queries in you ... read »
Feb 8, 2010 at 4:04 PM
Creating Microsoft Excel Documents With ColdFusion And XML
@Ben, Thanks for the tip regarding your XML article and using BufferedInputStream instead of FileInputStream. I wish excel files were written in plain text just like your XML example. Unfortunately ... read »
Feb 8, 2010 at 3:51 PM
How To Create GStrings In Javascript By Extending Core Data Types
@Robert, Thanks my man. This is the first time I have ever tried to extend a core Javascript object. Technically, I have extended the "Object" class (pretty much how all prototypal inheritance work ... read »
Feb 8, 2010 at 3:37 PM
How To Create GStrings In Javascript By Extending Core Data Types
This is some impressive solution. I've to try it myself to understand the concept better and say more about it, but nice job! PS: Wicked pictures! ... read »
Feb 8, 2010 at 3:20 PM
Converting An IP Address To An Integer Using MySQL (Thanks Julian Halliwell)
@Julian, I book marked this post so I could make a very similar post. A few years ago I experimented quite a bit with the INET functions and BaseN conversions in MySQL after finding coldfusion lim ... read »