CFML - query row to structure
Asked Answered
B

5

8

I would like to handle a row from a query by a function, where I pass the row as a structure.

ideally...

<cfloop query="myquery">
 #myfunction(#row#)#
</cfloop>

I could set it up like this too...

<cfloop query="myquery">
 #myfunction(#col1#,#col2#,#col3#,#col4#)#
</cfloop>

but I don't want to. I haven't been able to finda simple way of extracting a row, But I thought I'd ask.

Bluff answered 17/6, 2011 at 17:28 Comment(0)
P
13

Adobe ColdFusion 11 introduced QueryGetRow which converts a row from a query into a struct.

Poteen answered 15/8, 2017 at 21:25 Comment(0)
B
14

found a little more elegant looking solution, for single row

<cfscript>
    function GetQueryRow(query, rowNumber) {
        var i = 0;
        var rowData = StructNew();
        var cols    = ListToArray(query.columnList);
        for (i = 1; i lte ArrayLen(cols); i = i + 1) {
            rowData[cols[i]] = query[cols[i]][rowNumber];
        }
        return rowData;
    }
</cfscript>
Bluff answered 24/6, 2011 at 1:51 Comment(0)
P
13

Adobe ColdFusion 11 introduced QueryGetRow which converts a row from a query into a struct.

Poteen answered 15/8, 2017 at 21:25 Comment(0)
L
3

Ben Nadel has posted a blog post about this where he gives an example UDF that converts a query to a struct, and it accepts an optional row argument that allows you to turn an single row in that query to a struct. Take a look here.

Loutish answered 17/6, 2011 at 17:41 Comment(2)
I came across this earlier, I thought cfml may have made some progress in this area over the past 5 years. Seems like the should be available without any hacks.Bluff
I'm not sure how useful it would be in the core language. I don't recall ever needing a query row as a struct. Considering how easy it is to quickly loop over a single row and populate a struct, and the fact that UDFs exist to do it, it seems to me it would be an unnecessary addition to the language.Loutish
B
3

This is the Class from Ben's site without the comments

<--- --------------------------------------------------------------------------------------- ----

    Blog Entry:
    Ask Ben: Converting A Query To A Struct

    Author:
    Ben Nadel / Kinky Solutions

    Link:
    http://www.bennadel.com/index.cfm?event=blog.view&id=149

    Date Posted:
    Jul 19, 2006 at 7:32 AM

---- --------------------------------------------------------------------------------------- --->


<cffunction name="QueryToStruct" access="public" returntype="any" output="false"
    hint="Converts an entire query or the given record to a struct. This might return a structure (single record) or an array of structures.">
    <cfargument name="Query" type="query" required="true" />
    <cfargument name="Row" type="numeric" required="false" default="0" />

    <cfscript>
        var LOCAL = StructNew();
        if (ARGUMENTS.Row){
            LOCAL.FromIndex = ARGUMENTS.Row;
            LOCAL.ToIndex = ARGUMENTS.Row;
        } else {
            LOCAL.FromIndex = 1;
            LOCAL.ToIndex = ARGUMENTS.Query.RecordCount;
        }
        LOCAL.Columns = ListToArray( ARGUMENTS.Query.ColumnList );
        LOCAL.ColumnCount = ArrayLen( LOCAL.Columns );
        LOCAL.DataArray = ArrayNew( 1 );
        for (LOCAL.RowIndex = LOCAL.FromIndex ; LOCAL.RowIndex LTE LOCAL.ToIndex ; LOCAL.RowIndex = (LOCAL.RowIndex + 1)){
            ArrayAppend( LOCAL.DataArray, StructNew() );
            LOCAL.DataArrayIndex = ArrayLen( LOCAL.DataArray );
            for (LOCAL.ColumnIndex = 1 ; LOCAL.ColumnIndex LTE LOCAL.ColumnCount ; LOCAL.ColumnIndex = (LOCAL.ColumnIndex + 1)){
                LOCAL.ColumnName = LOCAL.Columns[ LOCAL.ColumnIndex ];
                LOCAL.DataArray[ LOCAL.DataArrayIndex ][ LOCAL.ColumnName ] = ARGUMENTS.Query[ LOCAL.ColumnName ][ LOCAL.RowIndex ];
            }
        }
        if (ARGUMENTS.Row){
            return( LOCAL.DataArray[ 1 ] );
        } else {
            return( LOCAL.DataArray );
        }
    </cfscript>
</cffunction>

usage...

<!--- Convert the entire query to an array of structures. --->
<cfset arrGirls = QueryToStruct( qGirls ) />

<!--- Convert the second record to a structure. --->
<cfset objGirl = QueryToStruct( qGirls, 2 ) />
Bluff answered 18/6, 2011 at 17:33 Comment(0)
D
0

Another solution would be:

function QueryToStruct(query){
    var cols    = ListToArray(query.columnList);
    var salida  = query.map(function(v=0,i,a){
                       return {'#cols[1]#':v};
                  });  
    return ValueArray(salida,'#cols[1]#');
}
Durstin answered 14/11, 2017 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.