CFLib.org – Common Function Library Project

queryGetRow(query [, row])

Last updated June 21, 2014

Version: 1 | Requires: ColdFusion 9 | Library: CFMLLib

 
Rated 0 time(s). Average Rating: 0

Description:
Simulate QueryGetRow @ https://wikidocs.adobe.com/wiki/display/coldfusionen/QueryGetRow for CF8+

Return Values:
Returns a struct containing the data from the selected row

Example:

view plain print about
// TestQueryGetRow.cfc
component extends="testbox.system.BaseSpec" {

    function run(){
        include "udfs/queryGetRow.cfm"; // renamed to _queryGetRow() for tests, as I'm running CF11

        describe("Tests for convertBBCodeToHtml()", function(){
            it("handles an empty query", function(){
                expect(
                    queryGetRow(queryNew(""), 0)
                ).toBe({});
            });
            it("handles zeroth row from a one-row query", function(){
                var q = queryNew("col", "varchar", [["data"]]);
                expect(
                    queryGetRow(q, 0)
                ).toBe({col=""});
            });
            it("handles first row from a one-row query", function(){
                var q = queryNew("col", "varchar", [["data"]]);
                expect(
                    queryGetRow(q, 1)
                ).toBe({col="data"});
            });
            it("handles row with multiple columns", function(){
                var q = queryNew("col1,col2", "varchar,varchar", [["data1","data2"]]);
                expect(
                    queryGetRow(q, 1)
                ).toBe({col1="data1",col2="data2"});
            });
        });
    }

}

Parameters:

Name Description Required
query The query from which to extract the row data Yes
row The row to extract. Defaults to currentRow No

Full UDF Source:

view plain print about
<cfscript>
/**
 * This function returns a struct having all the lowercased columns as keys and their corresponding values
 * v0.9 by Henry Ho
 * v1.0 by Adam Cameron (re-write in script)
 * 
 * @param query      The query from which to extract the row data (Required)
 * @param row      The row to extract. Defaults to currentRow (Optional)
 * @return Returns a struct containing the data from the selected row 
 * @author Henry Ho (henryho167@gmail.com) 
 * @version 1, June 21, 2014 
 */

public struct function queryGetRow(required query query, numeric row){
    if (!structKeyExists(arguments, "row")){
        row = query.currentRow;
    }
    
    var struct = {};
    for (var col in listToArray(query.columnList)){
        struct[lcase(col)] = query[col][row];
    }

    return struct;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Troy Pullis Troy Pullis added
firstXDayOfMonth
a while ago

Henry Ho Henry Ho added
arrayMap
a while ago

Henry Ho Henry Ho added
queryGetRow
a while ago

Tony Felice Tony Felice added
getRowFromQuery
a while ago

Top Rated

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

Barney Boisvert indentXml
Rated 5.0, 12 time(s)

Rachel Lehman deAccent
Rated 5.0, 9 time(s)

Markus Schneebeli                                 ListRemoveByStri...
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson