When you need to create a query object, it can be a bit complicated to create one with several columns and rows. Take a look at the following example:
<cfset qry = queryNew("Firstname,Name,Zip,Country")>
<cfset queryAddRow(3)>
<cfset querySetCell(qry, "Firstname", "Gert", 1)>
<cfset querySetCell(qry, "Name", "Franz", 1)>
<cfset querySetCell(qry, "Country", "Switzerland", 1)>
<cfset querySetCell(qry, "Firstname", "Mark", 2)>
<cfset querySetCell(qry, "Name", "Drew", 2)>
<cfset querySetCell(qry, "Country", "United Kingdom", 2)>
<cfset querySetCell(qry, "Firstname", "Todd", 3)>
<cfset querySetCell(qry, "Name", "Rafferty", 3)>
<cfset querySetCell(qry, "Country", "USA", 3)>
Now compare that to the following notation available in Railo:
<cfset qry = query( firstname:["Gert","Mark","Todd"], name:["Franz","Drew","Rafferty"], country:["Switzerland","United Kingdom","USA"])>
7 responses so far ↓
1 Marco Spescha // Apr 2, 2012 at 4:30 PM
For me a notation like this would make much more sense:
query([{firstname:"Gert",name:"Franz"},{"firstname:"Mark",name:"Drew"}]);
So you can have a function getNameStruct() returning a struct, that you can append to an array and convert it to a query.
a query is kind of an array of structs, isn't it?
2 Michael Offner // Apr 2, 2012 at 5:21 PM
Your example makes more "noise". you have to write the column name again and again.
a query is a struct of columns (in simple terms) ;-)
see this example:
#query.column[2]#
in this example i get the column from the query and from the column the row.
3 Scott Stroz // Apr 2, 2012 at 5:31 PM
4 Nando // Apr 4, 2012 at 11:10 AM
What about:
qry = queryNew("Firstname,Name,Country")
querySetRow(qry, "Gert", "Franz", "Switzerland")
5 Nando // Apr 4, 2012 at 12:02 PM
qry = queryNew("Firstname,Name,Zip,Country");
querySetRow(qry, "Gert", "Franz", null, "Switzerland");
In looping constructs where I've used QuerySetCell(), I've only set values by row. I'm not sure I'd run across cases very often where setting values by column would make sense, but maybe that's just my own limitation as a programmer!
6 Kyle // Apr 6, 2012 at 8:41 PM
7 Christian Ready // Apr 6, 2012 at 9:05 PM
Leave a Comment