CFLib.org – Common Function Library Project

StructOfArraysToQuery(theStruct)

Last updated March 27, 2002
Download UDF

author

Casey Broich                                      Casey Broich

Version: 1 | Requires: ColdFusion 5 | Library: DataManipulationLib

 
Rated 0 time(s). Average Rating: 0

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:

<cfscript>
/**
* 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); }
</cfscript>

Search CFLib.org


Latest Additions

Shawn Porter Shawn Porter added
DeMoronize
3 hour(s) ago

Chris Carey Chris Carey added
readPropertiesFi...
1 day(s) ago

Randy Johnson Randy Johnson added
lastDayofWeek
3 day(s) ago

Frank Marion Frank Marion added
sitemapPing
7 day(s) ago

Top Rated

James Sleeman                                     QuickSort
Rated 5.0, 3 time(s)

Barney Boisvert indentXml
Rated 5.0, 3 time(s)

Nathan Dintenfass                                 queryColumnsToSt...
Rated 5.0, 3 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 3 time(s)

Created by Raymond Camden / Design by Justin Johnson