Last active
December 24, 2015 23:49
-
-
Save Macagare/6883387 to your computer and use it in GitHub Desktop.
cfscript version of query to map conversion made for railo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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