Skip to content

Instantly share code, notes, and snippets.

@howellcc
Last active August 29, 2015 14:19
Show Gist options
  • Save howellcc/7b405f1b42283e2f36ea to your computer and use it in GitHub Desktop.
Save howellcc/7b405f1b42283e2f36ea to your computer and use it in GitHub Desktop.
QueryToStruct translated to cfscript
//Credit: Ben Nadel: http://www.bennadel.com/blog/149-ask-ben-converting-a-query-to-a-struct.htm
private any function queryToStruct(required query query,boolean row=false ) {
local.struct = 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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment