CFLib.org – Common Function Library Project

ArrayOfStructuresToQuery(Array)

Last updated March 19, 2003

author

David Crawford

Version: 2 | Requires: CF5 | Library: DataManipulationLib

Description:
Converts an array of structures to a CF Query Object. Mirror Image of Nathan Dintenfass' QueryToArrayOfStructures function.

Return Values:
Returns a query object.

Example:

<!--- create an array calledMyArray --->
<CFSET Stooges = ArrayNew(1)>
<!--- create a structure as the first array element --->
<CFSET Stooges[1] = StructNew()>
<CFSET Stooges[1].Name = "Larry">
<CFSET Stooges[1].Age = "34">
<!--- create a structure as the second array element --->
<CFSET Stooges[2] = StructNew()>
<CFSET Stooges[2].Name = "Moe">
<CFSET Stooges[2].Age = "38">
<!--- create a structure as the third array element --->
<CFSET Stooges[3] = StructNew()>
<CFSET Stooges[3].Name = "Curly">
<CFSET Stooges[3].Age = "33">

<CFSET StoogesQuery=ArrayOfStructuresToQuery(Stooges)>
<B>Array of Structures:</B>
<CFDUMP Var="#Stooges#">
<P>
<B>Query Object:</B>
<CFDUMP VAR="#StoogesQuery#">

Parameters:

Name Description Required
Array The array of structures to be converted to a query object. Assumes each array element contains structure with same Yes

Full UDF Source:

/**
 * Converts an array of structures to a CF Query Object.
 * 6-19-02: Minor revision by Rob Brooks-Bilson (rbils@amkor.com)
 * 
 * Update to handle empty array passed in. Mod by Nathan Dintenfass. Also no longer using list func.
 * 
 * @param Array 	 The array of structures to be converted to a query object.  Assumes each array element contains structure with same  (Required)
 * @return Returns a query object. 
 * @author David Crawford (dcrawford@acteksoft.com) 
 * @version 2, March 19, 2003 
 */
function arrayOfStructuresToQuery(theArray){
	var colNames = "";
	var theQuery = queryNew("");
	var i=0;
	var j=0;
	//if there's nothing in the array, return the empty query
	if(NOT arrayLen(theArray))
		return theQuery;
	//get the column names into an array =	
	colNames = structKeyArray(theArray[1]);
	//build the query based on the colNames
	theQuery = queryNew(arrayToList(colNames));
	//add the right number of rows to the query
	queryAddRow(theQuery, arrayLen(theArray));
	//for each element in the array, loop through the columns, populating the query
	for(i=1; i LTE arrayLen(theArray); i=i+1){
		for(j=1; j LTE arrayLen(colNames); j=j+1){
			querySetCell(theQuery, colNames[j], theArray[i][colNames[j]], i);
		}
	}
	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