ArrayOfStructuresToQuery(Array)
Last updated March 19, 2003
Version: 2 | Requires: ColdFusion 5 | 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:
<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:
<cfscript>
/**
* 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 (rbils@amkor.comdcrawford@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;
}
</cfscript>
Search CFLib.org
Latest Additions
Tayo Akinmade added
arrayTrim
11 day(s) ago
Will Belden added
longTime
17 day(s) ago
James Sleeman added
quickSort
27 day(s) ago
Ben Forta added
GetHostAddress
30 day(s) ago
Top Rated
EksporSQLData
Rated 5.0, 16 time(s)
backupDatabase
Rated 5.0, 13 time(s)
indentXml
Rated 5.0, 10 time(s)
generateSsccAsn
Rated 5.0, 4 time(s)