While updating my cfquery-to-json repoistory to use the latest ColdFusiuon 2016 functions, I was amazed at how much less code it takes to return the same data structures.
/**
* Convert n query object to an array of structs.
* @param required query data Query Object
* @return array
*/
public array function arrayOfStructs( required query data ) {
var results = [];
var temp = {};
var q = arguments.data;
var rc = q.recordCount;
var fields = listToArray(arguments.data.getColumnNames());
var fc = arrayLen(fields);
var x = 0;
var y = 0;
var fieldName = "";
for ( x = 1; x LTE rc; x++ ){
temp = {};
for ( y = 1; y LTE fc; y++ ) {
fieldName = fields[y];
temp[fieldName] = q[fieldName][x];
}
arrayAppend( results, temp );
}
return results;
}
public array function arrayOfStructs(required query data) {
var results = [];
var record = {};
var columns = arguments.data.getColumnNames();
for (var x in arguments.data) {
record = {};
for (var y in columns) {
record[y] = x[y];
}
arrayAppend(results, record);
}
return results;
}
The query member function each()
references the contents of each row as a struct
.
public array function arrayOfStructs(required query data, boolean lowerCase = false) {
var results = [];
arguments.data.each(function(row) {
arrayAppend(results, arguments.row);
});
return results;
}