Skip to content

Instantly share code, notes, and snippets.

@aliaspooryorik
Last active September 12, 2018 23:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aliaspooryorik/94cab7ba26b3902ba12fec66153a5854 to your computer and use it in GitHub Desktop.
Save aliaspooryorik/94cab7ba26b3902ba12fec66153a5854 to your computer and use it in GitHub Desktop.
Iterator based on the IBO pattern
component {
function init(required query data){
setQuery(arguments.data);
return this;
}
/* ---------------------------- PUBLIC ---------------------------- */
/* --- Iterator methods ---------------------------- */
// returns true if not at the end of the records
boolean function hasNext(){
if (variables.recordcount > variables.cursor){
variables.cursor++;
return true;
}
return false;
}
array function getProperties(){
return variables.properties;
}
// returns the value for current cursor position
any function get(key){
return variables.recordset[ variables.cursor ][ key ];
}
numeric function getCursorPosition(){
return variables.cursor;
}
void function setCursorPosition(required numeric row){
variables.cursor = row;
}
void function resetCursorPosition(){
setCursorPosition(0);
}
query function getRawData(){
return variables.query;
}
numeric function getRecordcount(){
return variables.recordcount;
}
// returns an array of structs
array function getRecordset(){
return variables.recordset;
}
string function getAsJSON(){
return SerializeJSON(variables.recordset);
}
boolean function hasRecords(){
return variables.recordcount > 0;
}
void function sortBy(required string properties){
var keys = listToArray(properties);
arraySort(variables.recordset, function(prev, next) {
var diff = 0;
for (var key in keys) {
if (!isPropertyText(key)) {
diff = prev[key] - next[key];
if (diff != 0) {
diff = diff > 0 ? 1 : -1;
}
} else {
diff = compare(prev[key], next[key]);
}
if (diff!=0) {
break;
}
}
return diff;
});
}
/* ---------------------------- DYNAMIC ---------------------------- */
// Allow for get*key*() style calls without needing to create getters
any function onMissingMethod(missingMethodName, missingMethodArguments){
var prefix = Left(arguments.missingMethodName, 3);
if ("get" == prefix){
var key = Right(arguments.missingMethodName, len(arguments.missingMethodName) - 3);
return get(key);
}
else{
throw "method '#arguments.missingMethodName#' not found";
}
}
/* ---------------------------- PRIVATE ---------------------------- */
private struct function getPropertyMetaData(required string key){
var position = ArrayFind(variables.propertyMetadata, function(it) {
return it.name == key;
});
return variables.propertyMetadata[position];
}
private boolean function isPropertyText(required string key){
var fieldMetaData = getPropertyMetaData(key);
if (structKeyExists(fieldMetaData, "typeName")) {
return fieldMetaData.typeName == "VarChar";
}
// have to sort alphabetically as no query meta data
// it's likely this is a dynamically added column
return true;
}
private void function setQuery(required query q){
variables.query = arguments.q;
variables.recordset = [];
variables.properties = ListToArray(lcase(variables.query.columnlist));
for (var i=1; i<=variables.query.recordcount; i++){
var row = {};
for (var col in variables.properties){
row[lcase(col)] = variables.query[col][i];
}
arrayAppend(variables.recordset, row);
}
variables.recordcount = variables.query.recordCount;
variables.propertyMetadata = getMetadata(variables.query);
resetCursorPosition();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment