CFLib.org – Common Function Library Project

StructOfArraysToQuery(theStruct)

Last updated March 27, 2002

author

Casey Broich

Version: 1 | Requires: CF5 | Library: DataManipulationLib

Description:
Converts a structure of arrays to a CF Query.

Return Values:
Returns a query object.

Example:

<!--- create structure with column names as keys --->

   <cfset stcolums = structNew()>
   <cfset stcolums.ContentID = ArrayNew(1)>
   <cfset stcolums.title = ArrayNew(1)>
   <cfset stcolums.created = ArrayNew(1)>

   <!--- add an array to each key column in the above structure  --->

   <cfloop from=1 to=5 index="row">
       <cfset title = mid('abcdefghijklmonpqrstuvwxyz',RandRange(1,26),1)>
       <cfset tmp = arrayappend(stcolums.title, "title " & row)>
       <cfset tmp = arrayappend(stcolums.created, DateFormat(DateAdd('d',RandRange(1,30),now()),'dd mmm yyyy') )>
       <cfset tmp = arrayappend(stcolums.ContentID, createuuid() )>
   </cfloop>


   <cfdump var="#stcolums#"> <!--- output from above --->

   <cfset myQuery = StructOfArraysToQuery(stcolums)> <!--- Pass structure to function --->

   <cfdump var="#myQuery#"> <!--- query result --->

Parameters:

Name Description Required
theStruct The structure of arrays you want converted to a query. Yes

Full UDF Source:

/**
 * Converts a structure of arrays to a CF Query.
 * 
 * @param theStruct 	 The structure of arrays you want converted to a query. 
 * @return Returns a query object. 
 * @author Casey Broich (cab@pagex.com) 
 * @version 1, March 27, 2002 
 */
function StructOfArraysToQuery(thestruct){
   var fieldlist = structkeylist(thestruct);
   var numrows   = arraylen( thestruct[listfirst(fieldlist)] );
   var thequery  = querynew(fieldlist);
   var fieldname="";
   var thevalue="";
   var row=1;
   var col=1;
   for(row=1; row lte numrows; row = row + 1)
   {
      queryaddrow(thequery);
      for(col=1; col lte listlen(fieldlist); col = col + 1)
      {
	 fieldname = listgetat(fieldlist,col);
	 thevalue  = thestruct[fieldname][row];
	 querysetcell(thequery,fieldname,thevalue);
      }
   }
return(thequery); }
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson