Skip to content

Instantly share code, notes, and snippets.

@kevindb
Created March 23, 2015 12:11
Show Gist options
  • Save kevindb/3e21baa5c3b159a2120d to your computer and use it in GitHub Desktop.
Save kevindb/3e21baa5c3b159a2120d to your computer and use it in GitHub Desktop.
ColdFusion Query Transformation
/**
* @hint Returns reasonable array of objects from a cfquery
*/
public function queryToArray(
required query query,
string excludeColumns = ""
){
if (server.coldfusion.productName == "ColdFusion Server") {
local.columns = arguments.query.getMetaData().getColumnLabels();
} else if (server.coldfusion.productName == "Lucee") {
local.columns = arguments.query.getColumnNames();
} else {
local.columns = [];
}
local.response = arrayNew(1);
for (local.rowIndex=1; local.rowIndex<=arguments.query.recordCount; local.rowIndex++) {
local.response[local.rowIndex] = structNew();
for (local.columnIndex=1; local.columnIndex<=arrayLen(local.columns); local.columnIndex++) {
local.columnName = local.columns[local.columnIndex];
if(local.columnName != "" && (arguments.excludeColumns == "" || !listFindNoCase(arguments.excludeColumns, local.columnName))) {
local.response[local.rowIndex][local.columnName] = arguments.query[local.columnName][local.rowIndex];
}
}
}
return local.response;
}
/**
* @hint Returns reasonable objects from a cfquery single row
*/
public function queryToStruct(
required query query,
string excludeColumns = "",
numeric row = 1
){
if (server.coldfusion.productName == "ColdFusion Server") {
local.columns = arguments.query.getMetaData().getColumnLabels();
} else if (server.coldfusion.productName == "Lucee") {
local.columns = arguments.query.getColumnNames();
} else {
local.columns = [];
}
for (local.columnIndex=1; local.columnIndex<=arrayLen(local.columns); local.columnIndex++) {
local.columnName = local.columns[local.columnIndex];
if(arguments.excludeColumns == "" || !listFindNoCase(arguments.excludeColumns, local.columnName)) {
local.response[local.columnName] = arguments.query[local.columnName][arguments.row];
}
}
return local.response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment