Skip to content

Instantly share code, notes, and snippets.

@halityurttas
Created August 16, 2018 11:55
Show Gist options
  • Save halityurttas/1501998e117a8aa6f8728e6f26ee1064 to your computer and use it in GitHub Desktop.
Save halityurttas/1501998e117a8aa6f8728e6f26ee1064 to your computer and use it in GitHub Desktop.
Coldfusion Query Iterator Pattern
<cfcomponent>
<cfproperty name="query" type="query">
<cfproperty name="current" type="number">
<cfproperty name="recordcount" type="number">
<cfproperty name="columns" type="array">
<cffunction name="init" access="public" returntype="any">
<cfargument name="query" type="query">
<cfset this.query=arguments.query>
<cfset this.recordcount=arguments.query.recordcount>
<cfset this.columns=listToArray(this.query.columnlist)>
<cfset this.current=1>
<cfreturn this>
</cffunction>
<cffunction name="Get" access="public" returntype="any">
<cfargument name="field" type="any">
<cfscript>
if (isNumeric(field)) {
return this.query[this.columns[arguments.field]][this.current];
} else {
return this.query[arguments.field][this.current];
}
</cfscript>
</cffunction>
<cffunction name="GetRow" access="public" returntype="struct">
<cfscript>
row = {};
for (col in this.columns) {
row[col] = this.query[col][this.current];
}
</cfscript>
<cfreturn row>
</cffunction>
<cffunction name="GetRowArray" access="public" returntype="array">
<cfscript>
row = [];
for (col in this.columns)
{
row[arrayFind(this.columns, col)] = this.query[col][this.current];
}
</cfscript>
<cfreturn row>
</cffunction>
<cffunction name="hasIter" access="public" returntype="boolean">
<cfscript>
return this.current <= this.recordcount;
</cfscript>
</cffunction>
<cffunction name="Next" access="public" returntype="any">
<cfscript>
if (this.hasIter())
{
this.current++;
}
</cfscript>
</cffunction>
<cffunction name="Prev" access="public" returntype="any">
<cfscript>
if (this.current > 1)
{
this.current--;
}
</cfscript>
</cffunction>
<cffunction name="First" access="public" returntype="any">
<cfscript>
this.current = 1;
</cfscript>
</cffunction>
<cffunction name="Last" access="public" returntype="any">
<cfscript>
this.current = this.recordcount;
</cfscript>
</cffunction>
</cfcomponent>
<cfquery name="qu1" datasource="#dsn#">
SELECT 1 as id, 'halit' as title
UNION ALL
SELECT 2 as id, 'velit' as title
</cfquery>
<cfset queryiter = createObject("component", "queryiterator").init(qu1)>
<cfloop condition="#queryiter.hasiter()#">
<cfdump var="#queryiter.getrowarray()#">
<cfset queryiter.next()>
</cfloop>
@halityurttas
Copy link
Author

Iterating and recordset pattern. Get field by name or index on the current record, Get the current record as column named struct or column indexed array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment