Created
August 16, 2018 11:55
-
-
Save halityurttas/1501998e117a8aa6f8728e6f26ee1064 to your computer and use it in GitHub Desktop.
Coldfusion Query Iterator Pattern
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
<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> |
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
<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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.