Skip to content

Instantly share code, notes, and snippets.

@Macagare
Last active December 24, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Macagare/6883387 to your computer and use it in GitHub Desktop.
Save Macagare/6883387 to your computer and use it in GitHub Desktop.
cfscript version of query to map conversion made for railo
/**
* converts query to sturct map
*
* @param query source query to convert
* @param string key key for the main subkeys, when empty first column taken
* @return struct converted map
*
*/
struct function toMap( required query source, required string key, string ignore="" ) {
var map = {};
var qRow = 1;
var colList = arguments.source.columnList;
var rowKey = "";
var colName = "";
if ( not len(arguments.key) ) arguments.key = listFirst(colList);
for ( qRow = 1; qRow lte arguments.source.recordCount; qRow = qRow + 1 ) {
rowKey = lcase( arguments.source[arguments.key][qRow] );
if ( lcase(rowKey) neq lcase(arguments.key) and not structKeyExists( map, rowKey ) ) {
map[ rowKey ] = {};
for (iCol = 1; iCol lte listLen(colList); iCol = iCol + 1 ) {
colName = lcase( listGetAt(colList,iCol) );
if ( not listFindNoCase(arguments.ignore,colName) ) {
map[ rowKey ][ colName ] = arguments.source[ colName ][qRow];
}
}
}
}
return map;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment