While we're looking at different approaches for iterators, I figured I'd share the sample general purpose iterator.cfc I made for my "CFCs In Depth" class I used to teach. It is used in the class mostly to show off some of the things you can do with methods (in this case, dynamically assign public methods based on the type of variable you will be iterating over), but might be useful in this conversation. It doesn't have all the methods of John's query iterator, but things like "count()" and "last()" and "first()" would be easy to add.




John Farrar wrote:
OK...here it is... completely untested! This is a rough draft for community suggestions. This is the CFC guru spot... so let me know what else should be done with this object guys. I can create a project for this on CFOpen if anyone is interested in it.

Thanks,

John Farrar


----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to cfcdev@cfczone.org with the words 'unsubscribe cfcdev' as the subject of the email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting (www.cfxhosting.com).

An archive of the CFCDev list is available at
www.mail-archive.com/cfcdev@cfczone.org


------------------------------------------------------------------------

<cfcomponent displayname="QueryIterator" output="false">
<!--- This CFC is the property of SOSensible. You may use it free of charge under standard GNU Open Source Lisc. practices. You may use it for personal or commercial and modify it
to your needs at no charge. The only exceptions are that you may not remove this notice and
you must not hold our company or employees liable in any way for it's performance. If you enhance
this component we request that you send a copy of your enhancements to us at [EMAIL PROTECTED]
for our review so that the spirit of this open source object will continue to grow for the community at large.
www.sosensible.com --->
<cfset variables.query = queryNew("empty","VarChar")>
<cfset variables.currentRow = 0>
<cfset variables.recordCount = 0>
<cfset variables.columnList = "">

<cffunction name="init" access="public" returntype="any" output="false">
<cfargument name="query" type="query" required="yes">
<cfset var local = structNew()>

<cfscript>
variables.query = duplicate(arguments.query);
variables.columnList = arguments.query.columnList;
variables.currentRow = 1;
variables.recordCount = arguments.query.recordCount;
</cfscript>

<cfreturn this>
</cffunction>

<cffunction name="getColumnList" access="public" returntype="numeric" output="false">
<cfreturn variables.columnList>
</cffunction>

<cffunction name="getCurrentRecord" access="public" returntype="query" output="false">
<cfargument name="column" type="variablename" required="yes">
<cfset var local = structNew()>

<cfreturn duplicate(variables.query[arguments.column][variables.currentRow])>
</cffunction>

<cffunction name="getCurrentRow" access="public" returntype="numeric" output="false">
<cfreturn variables.currentRow>
</cffunction>

<cffunction name="getRecordCount" access="public" returntype="numeric" output="false">
<cfreturn variables.recordCount>
</cffunction>

<cffunction name="hasNext" access="public" returntype="boolean" output="false">
<cfreturn (variables.currentRow lt variables.query.recordCount)>
</cffunction>

<cffunction name="hasPrevious" access="public" returntype="boolean" output="false">
<cfreturn (variables.currentRow gt 1)>
</cffunction>

<cffunction name="isFirst" access="public" returntype="boolean" output="false">
<cfreturn (variables.currentRow EQ 1)>
</cffunction>

<cffunction name="isLast" access="public" returntype="boolean" output="false">
<cfreturn (variables.currentRow EQ variables.query.recordCount)>
</cffunction>

<cffunction name="moveFirst" access="public" returntype="boolean" output="false">
<cfset var local = structNew()>

<cfif variables.recordCount GTE 1>
<cfset variables.currentRow = 1>
<cfset local.myReturn = TRUE>
<cfset setAttributes()>
<cfelse>
<cfset local.myReturn = FALSE>
</cfif>

<cfreturn local.myReturn>
</cffunction>

<cffunction name="moveLast" access="public" returntype="boolean" output="false">
<cfset var local = structNew()>

<cfif variables.recordCount GTE 1>
<cfset variables.currentRow = variables.recordCount>
<cfset local.myReturn = TRUE>
<cfset setAttributes()>
<cfelse>
<cfset local.myReturn = FALSE>
</cfif>

<cfreturn local.myReturn>
</cffunction>

<cffunction name="moveNext" access="public" returntype="boolean" output="false">
<cfset var local = structNew()>

<cfif hasNext()>
<cfset variables.currentRow = variables.currentRow + 1>
<cfset local.myReturn = TRUE>
<cfset setAttributes()>
<cfelse>
<cfset local.myReturn = FALSE>
</cfif>

<cfreturn local.myReturn>
</cffunction>

<cffunction name="movePrevious" access="public" returntype="boolean" output="false">
<cfset var local = structNew()>

<cfif hasPrevious()>
<cfset variables.currentRow = variables.currentRow - 1>
<cfset local.myReturn = TRUE>
<cfset setAttributes()>
<cfelse>
<cfset local.myReturn = FALSE>
</cfif>

<cfreturn local.myReturn>
</cffunction>

<cffunction name="setAttributes" access="private" returntype="boolean" output="No">
<cfset var local = structNew()>
<cfset local.myReturn = TRUE>
<cftry>
<cfloop index="columnName" list="#variables.columnList#">
<cfset "this.#columnName#" = variables.query[columnName]>
</cfloop>
<cfcatch>
<cfset local.myReturn = FALSE>
</cfcatch>
</cftry>

<cfreturn local.myReturn>
</cffunction>
</cfcomponent>

----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to 
cfcdev@cfczone.org with the words 'unsubscribe cfcdev' as the subject of the 
email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting 
(www.cfxhosting.com).

An archive of the CFCDev list is available at
www.mail-archive.com/cfcdev@cfczone.org


----------------------------------------------------------
You are subscribed to cfcdev. To unsubscribe, send an email to 
cfcdev@cfczone.org with the words 'unsubscribe cfcdev' as the subject of the 
email.

CFCDev is run by CFCZone (www.cfczone.org) and supported by CFXHosting 
(www.cfxhosting.com).

An archive of the CFCDev list is available at
www.mail-archive.com/cfcdev@cfczone.org

Attachment: iterator.cfm
Description: application/cfm


Attachment: iterator.cfc
Description: application/cfc

Reply via email to