Yes that's right dale.

With this function also, all the columns don't have to be passed in either.
If it finds the column it the "rowData" param it populates it otherwise it
is just left empty.

Using the array function is going to be faster than looping over recordsets
also.

Steve

-----Original Message-----
From: cfaussie@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf
Of Dale Fraser
Sent: Thursday, 15 May 2008 8:09 AM
To: cfaussie@googlegroups.com
Subject: [cfaussie] Re: queryAddRow


Thanks Steve,

I think I understand that.

You are converting each column to an array inserting a row into that array
and adding that column back to a new query, correct?

Regards
Dale Fraser

-----Original Message-----
From: cfaussie@googlegroups.com [mailto:[EMAIL PROTECTED] On Behalf
Of Steve Onnis
Sent: Wednesday, 14 May 2008 6:56 PM
To: cfaussie@googlegroups.com
Subject: [cfaussie] Re: queryAddRow


Here you go

Got bored and always like a challenge :)

/////////////////////////////////////////////////////////
<cfscript>
        function queryInsertRow(query, position, rowData) {
                /*
                query           : the query object
                position        : the position to add the new row
                rowData         : a structure containing the names of the
columns as keys
                */
                
                var columns = listToArray(query.columnList);
                var rowCount = MAX(query.recordCount + 1, position);
                var columnCount = arrayLen(columns);
                var columnStruct = structNew();
                var c = "";
                var q = queryNew("");
                
                // loop over the query columns
                for (c=1; c LTE columnCount; c = c + 1) {
                        // get the values out of the column
                        columnStruct[columns[c]] =
listToArray(evaluate("valueList(query.#columns[c]#)"));
                        
                        // arrayInserAt() fails if the inser position is
greater then the avalable array length
                        // so resize the array and set the array element at
the position
                        if (position GT query.recordCount) {
                                arrayResize(
                                        columnStruct[columns[c]], 
                                        rowCount
                                        );
                                        
                                arraySet(
                                        columnStruct[columns[c]], 
                                        position, 
                                        position, 
                                        IIF(structKeyExists(rowData,
columns[c]), "rowData[columns[c]]", "''")
                                        );
                                }
                        
                        // Use arrayInserAt() to inject the new value   
                        else {
                                arrayInsertAt(
                                        columnStruct[columns[c]], 
                                        position, 
                                        IIF(structKeyExists(rowData,
columns[c]), "rowData[columns[c]]", "''")
                                        );
                                }
                        // add the column back into our new query
                        queryAddColumn(
                                q, 
                                columns[c], 
                                columnStruct[columns[c]]
                                );
                        }
                return q;
                }
                
                
        qry = queryNew("");
        queryAddColumn(qry, "fruit", listToArray("banana,apple,orange"));
        queryAddColumn(qry, "vegies",
listToArray("carrot,cucumber,tomato"));
        
</cfscript>
<cfdump var="#qry#">

<cfscript>
        newRow = structNew();
        newRow["fruit"] = "new fruit";
        
        qry = queryInsertRow(
                qry, 
                2, 
                newRow
                );
</cfscript>

<cfdump var="#qry#">
/////////////////////////////////////////////////////////









--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"cfaussie" group.
To post to this group, send email to cfaussie@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cfaussie?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to