CFLib.org – Common Function Library Project

ArrayOfStructuresToQuery(Array)

Last updated March 19, 2003

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

 
Rated 5 time(s). Average Rating: 4.8

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:

view plain print about
<!--- 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:

view plain print about
<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>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Tayo Akinmade Tayo Akinmade added
arrayTrim
11 day(s) ago

Will Belden Will Belden added
longTime
17 day(s) ago

James Sleeman James Sleeman added
quickSort
27 day(s) ago

Ben Forta Ben Forta added
GetHostAddress
30 day(s) ago

Top Rated

Darwan Leonardo Sitepu EksporSQLData
Rated 5.0, 16 time(s)

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 13 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson