CFLib.org – Common Function Library Project

queryGetRow(query[, row])

Last updated June 21, 2014
Download UDF

author

Henry Ho

Version: 1 | Requires: CF9 | Library: CFMLLib

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:

// 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:

/**
 * 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;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

CF Ninja added
calcIRR
a month ago

Stephen Withington added
getWeekOfMonth
a month ago

Adam Cameron added
createPrimeNumbe...
2 months ago

Ray Ford added
timeZoneNow
3 months ago

Henry Ho added
queryExecute
3 months ago

Created by Raymond Camden / Design by Justin Johnson