邪恶八进制信息安全团队技术讨论组's Archiver

金州 2006-1-31 08:47

[转载]ColdFusion中的RecordSet Component

<P>信息来源:Wayne_Deng的专栏</P>
<P>CF中对数据的操作都是通过CFQuery来进行的,纯粹从便利的角度上来讲远远比不上asp中的recordset object。不过在coldfusion MX里面也有对象了嘛,我们也来自己封装一个简单的recordset Component!</P>
<P>下面是应用示例,熟悉recordset object的不用多说也明白吧?操作起来确实方便。<BR>
<TABLE style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#fdfddf>
<P><FONT style="FONT-WEIGHT: bold; COLOR: #990000">以下是代码:</FONT><BR><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" color=#990000><cfscript><BR>rs=createObject("component","CFNews.model.db.recordset");<BR>rs.getByTable("article","article_source"); //打开recordsource为article_source中名为article的table</FONT></STRONG></P>
<P><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" color=#990000>writeoutput(rs.getField("title") & "<br>");<BR>rs.MoveNext();<BR>writeoutput(rs.getField("title") & "<br>");<BR>rs.MoveLast();<BR>writeoutput(rs.getField("title") & "<br>");<BR>//添加一个新的记录<BR>rs.addNew();<BR>rs.setField("title","insert test");<BR>rs.setField("content","This is a test");<BR>rs.setField("artileID",8);<BR>rs.setField("img","<img src='aa'>");<BR>rs.update();</FONT></STRONG></P>
<P><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" color=#990000>//修改第2个记录<BR>rs.rownum=2;<BR></FONT></STRONG><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" color=#990000>rs.setField("title","update test");<BR>rs.update();<BR></cfscript></FONT></STRONG></P></TD></TR></TBODY></TABLE></P>
<P></P>
<P>下面是recordset.cfc<BR>
<TABLE style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#fdfddf><FONT size=1>CFNews.model.db.RecordSet<BR></FONT><B><FONT size=+1>Component RecordSet (Recordset class) </B></FONT><BR><BR>
<TABLE>
<TBODY>
<TR>
<TD>hierarchy:</TD>
<TD><A href="http://127.0.0.1/CFIDE/componentutils/cfcexplorer.cfc?method=getcfcinhtml&name=WEB-INF.cftags.component"><FONT color=#0000ff>WEB-INF.cftags.component</FONT></A><BR>CFNews.model.db.RecordSet<BR></TD></TR>
<TR>
<TD>path:</TD>
<TD>C:\Inetpub\wwwroot\CFNews\model\db\RecordSet.cfc</TD></TR>
<TR>
<TD>properties:</TD>
<TD></TD></TR>
<TR>
<TD>methods:</TD>
<TD><A href="http://127.0.0.1/CFIDE/componentutils/componentdetail.cfm?component=CFNews.model.db.RecordSet#method_addNew"><FONT color=#800080>addNew</FONT></A>, <A href="http://127.0.0.1/CFIDE/componentutils/componentdetail.cfm?component=CFNews.model.db.RecordSet#method_delete"><FONT color=#800080>delete</FONT></A>, <A href="http://127.0.0.1/CFIDE/componentutils/componentdetail.cfm?component=CFNews.model.db.RecordSet#method_edit"><FONT color=#800080>edit</FONT></A>, <A href="http://127.0.0.1/CFIDE/componentutils/componentdetail.cfm?component=CFNews.model.db.RecordSet#method_getBySQL"><FONT color=#800080>getBySQL</FONT></A>, <A href="http://127.0.0.1/CFIDE/componentutils/componentdetail.cfm?component=CFNews.model.db.RecordSet#method_getByTable"><FONT color=#800080>getByTable</FONT></A>, <A href="http://127.0.0.1/CFIDE/componentutils/componentdetail.cfm?component=CFNews.model.db.RecordSet#method_getField"><FONT color=#800080>getField</FONT></A>, <A href="http://127.0.0.1/CFIDE/componentutils/componentdetail.cfm?component=CFNews.model.db.RecordSet#method_moveFirst"><FONT color=#800080>moveFirst</FONT></A>, <A href="http://127.0.0.1/CFIDE/componentutils/componentdetail.cfm?component=CFNews.model.db.RecordSet#method_moveLast"><FONT color=#800080>moveLast</FONT></A>, <A href="http://127.0.0.1/CFIDE/componentutils/componentdetail.cfm?component=CFNews.model.db.RecordSet#method_moveNext"><FONT color=#800080>moveNext</FONT></A>, <A href="http://127.0.0.1/CFIDE/componentutils/componentdetail.cfm?component=CFNews.model.db.RecordSet#method_movePrev"><FONT color=#800080>movePrev</FONT></A>, <A href="http://127.0.0.1/CFIDE/componentutils/componentdetail.cfm?component=CFNews.model.db.RecordSet#method_setField"><FONT color=#800080>setField</FONT></A>, <A href="http://127.0.0.1/CFIDE/componentutils/componentdetail.cfm?component=CFNews.model.db.RecordSet#method_update"><FONT color=#800080>update</FONT></A></TD></TR></TBODY></TABLE><FONT size=-2>* - private method</FONT> <BR><BR>
<TABLE>
<TBODY>
<TR>
<TH align=left><A name=method_addNew>addNew</A> </TH></TR>
<TR>
<TD><FONT color=#000099><FONT face=新宋体><CODE><I>public</I> <B>addNew</B> ( ) </I></CODE><BR><BR></FONT></FONT>Output: supressed<BR><BR></TD></TR>
<TR>
<TH align=left><A name=method_delete>delete</A> </TH></TR>
<TR>
<TD><FONT color=#000099><FONT face=新宋体><CODE><I>public</I> <B>delete</B> ( ) </I></CODE><BR><BR></FONT></FONT>Output: supressed<BR><BR></TD></TR>
<TR>
<TH align=left><A name=method_edit>edit</A> </TH></TR>
<TR>
<TD><FONT color=#000099><FONT face=新宋体><CODE><I>public</I> <B>edit</B> ( ) </I></CODE><BR><BR></FONT></FONT>Output: supressed<BR><BR></TD></TR>
<TR>
<TH align=left><A name=method_getBySQL>getBySQL</A> </TH></TR>
<TR>
<TD><FONT color=#000099><FONT face=新宋体><CODE><I>public</I> <B>getBySQL</B> ( <I>required string </I>sql, <I>required string </I>recordSource ) </I></CODE><BR><BR></FONT></FONT>get a Query object by a sql sentence<BR><BR>Output: supressed<BR>Parameters:<BR> <B>sql:</B> string, required, sql <BR> <B>recordSource:</B> string, required, recordSource <BR><BR></TD></TR>
<TR>
<TH align=left><A name=method_getByTable>getByTable</A> </TH></TR>
<TR>
<TD><FONT color=#000099><FONT face=新宋体><CODE><I>public</I> <B>getByTable</B> ( <I>required string </I>tableName, <I>required string </I>recordSource ) </I></CODE><BR><BR></FONT></FONT>get a Query object by a table name<BR><BR>Output: supressed<BR>Parameters:<BR> <B>tableName:</B> string, required, tableName <BR> <B>recordSource:</B> string, required, recordSource <BR><BR></TD></TR>
<TR>
<TH align=left><A name=method_getField>getField</A> </TH></TR>
<TR>
<TD><FONT color=#000099><FONT face=新宋体><CODE><I>public</I> <B>getField</B> ( <I>required string </I>fieldName ) </I></CODE><BR><BR></FONT></FONT>get field value by field name<BR><BR>Output: supressed<BR>Parameters:<BR> <B>fieldName:</B> string, required, fieldName <BR><BR></TD></TR>
<TR>
<TH align=left><A name=method_moveFirst>moveFirst</A> </TH></TR>
<TR>
<TD><FONT color=#000099><FONT face=新宋体><CODE><I>public</I> <B>moveFirst</B> ( ) </I></CODE><BR><BR></FONT></FONT>Output: supressed<BR><BR></TD></TR>
<TR>
<TH align=left><A name=method_moveLast>moveLast</A> </TH></TR>
<TR>
<TD><FONT color=#000099><FONT face=新宋体><CODE><I>public</I> <B>moveLast</B> ( ) </I></CODE><BR><BR></FONT></FONT>Output: supressed<BR><BR></TD></TR>
<TR>
<TH align=left><A name=method_moveNext>moveNext</A> </TH></TR>
<TR>
<TD><FONT color=#000099><FONT face=新宋体><CODE><I>public</I> <B>moveNext</B> ( ) </I></CODE><BR><BR></FONT></FONT>Output: supressed<BR><BR></TD></TR>
<TR>
<TH align=left><A name=method_movePrev>movePrev</A> </TH></TR>
<TR>
<TD><FONT color=#000099><FONT face=新宋体><CODE><I>public</I> <B>movePrev</B> ( ) </I></CODE><BR><BR></FONT></FONT>Output: supressed<BR><BR></TD></TR>
<TR>
<TH align=left><A name=method_setField>setField</A> </TH></TR>
<TR>
<TD><FONT color=#000099><FONT face=新宋体><CODE><I>public</I> <B>setField</B> ( <I>required string </I>fieldName, <I>required string </I>value, <I>string </I>fieldType ) </I></CODE><BR><BR></FONT></FONT>Output: supressed<BR>Parameters:<BR> <B>fieldName:</B> string, required, fieldName <BR> <B>value:</B> string, required, value <BR> <B>fieldType:</B> string, optional, fieldType <BR><BR></TD></TR>
<TR>
<TH align=left><A name=method_update>update</A> </TH></TR>
<TR>
<TD><FONT color=#000099><FONT face=新宋体><CODE><I>public</I> <B>update</B> ( <I>string </I>IDName ) </I></CODE><BR><BR></FONT></FONT>Output: supressed<BR>Parameters:<BR> <B>IDName:</B> string, optional, IDName <BR></TD></TR></TBODY></TABLE><BR></TD></TR></TBODY></TABLE></P>
<P>
<TABLE style="BORDER-RIGHT: #cccccc 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: #cccccc 1px dotted; BORDER-LEFT: #cccccc 1px dotted; BORDER-BOTTOM: #cccccc 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD style="WORD-WRAP: break-word" bgColor=#fdfddf>
<P><FONT style="FONT-WEIGHT: bold; COLOR: #990000">以下是代码:</FONT><BR><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" color=#990000><cfcomponent displayname="Recordset class"><BR><cfset this.fields=structNew()><BR><cfset this.tableName=""><BR><cfset this.recordSource=""></FONT></STRONG></P>
<P><STRONG><FONT style="BACKGROUND-COLOR: #ffffff" color=#990000><cffunction name="getByTable" access="public" output="false" hint="get a Query object by a table name"><BR><cfargument name="tableName" type="string" required="yes"><BR><cfargument name="recordSource" type="string" required="yes"><BR><cfset this.tableName=arguments.tableName><BR><cfset this.recordSource=arguments.recordSource><BR><cfquery datasource="#this.recordSource#" name="recordSetSelect"><BR>select * from #this.tableName#<BR></cfquery><BR><cfset this.recordSet=recordSetSelect><BR><cfset this.mode="select"><BR><cfset this.rownum=1><BR></cffunction><BR><cffunction name="getBySQL" access="public" output="false" hint="get a Query object by a sql sentence"><BR><cfargument name="sql" type="string" required="yes"><BR><cfargument name="recordSource" type="string" required="yes"><BR><cfset this.sql=arguments.sql><BR><cfset this.recordSource=arguments.recordSource><BR><cfquery datasource="#this.recordSource#" name="recordSetSelect"><BR>#PreserveSingleQuotes(this.sql)#<BR></cfquery><BR><cfset this.recordSet=recordSetSelect><BR><cfset this.mode="select"><BR><cfset this.rownum=1><BR></cffunction><BR><cffunction name="getField" access="public" output="false" hint="get field value by field name"><BR><cfargument name="fieldName" type="string" required="yes"><BR><cfset value=evaluate("this.recordSet.#arguments.fieldName#[#this.rownum#]")><BR><cfreturn value><BR></cffunction><BR><cffunction name="setField" access="public" output="false"><BR><cfargument name="fieldName" type="string" required="yes"><BR><cfargument name="value" type="string" required="yes"><BR><cfargument name="fieldType" type="string" required="no"><BR><cfif not isdefined("this.fields.#arguments.fieldName#")><cfset tmp=Evaluate("this.fields.#arguments.fieldName#=structNew()")></cfif><BR><cfscript>StructInsert(Evaluate("this.fields.#arguments.fieldName#"),"value",arguments.value,"yes");</cfscript><BR><cfif not isdefined("arguments.fieldType")><BR><cfif isDate(arguments.value)><BR><cfset tmp=Evaluate("this.fields.#arguments.fieldName#.fieldType='date'")><BR><cfelseif isNumeric(arguments.value)><BR><cfset tmp=Evaluate("this.fields.#arguments.fieldName#.fieldType='numeric'")><BR><cfelse><BR><cfset tmp=Evaluate("this.fields.#arguments.fieldName#.fieldType='varchar'")><BR></cfif><BR><cfelse><BR><cfset tmp=Evaluate("this.fields.#arguments.fieldName#.fieldType='#arguments.fieldType#'")><BR></cfif><BR></cffunction><BR><cffunction name="moveFirst" access="public" output="false"><BR><cfset this.rownum=1><BR></cffunction><BR><cffunction name="moveLast" access="public" output="false"><BR><cfset this.rownum=this.recordSet.recordcount><BR></cffunction><BR><cffunction name="moveNext" access="public" output="false"><BR><cfset this.rownum=this.rownum+1><BR><cfif this.rownum gt this.recordSet.recordcount><cfset this.rownum=this.recordSet.recordcount></cfif><BR></cffunction><BR><cffunction name="movePrev" access="public" output="false"><BR><cfset this.rownum=this.rownum-1><BR><cfif this.rownum lt 1><cfset this.rownum=1></cfif><BR></cffunction><BR><cffunction name="addNew" access="public" output="false"><BR><cfset this.mode="addnew"><BR></cffunction><BR><cffunction name="edit" access="public" output="false"><BR><cfset this.mode="edit"><BR></cffunction><BR><cffunction name="delete" access="public" output="false"><BR><cfif not isdefined("arguments.IDName")><cfset arguments.IDName=ListContainsNoCase(this.recordSet.ColumnList,"id",",")></cfif><BR><cfif arguments.IDName eq ""><cfset arguments.IDName=ListFirst(this.recordSet.ColumnList,",")></cfif><BR><cfquery datasource="#this.recordSource#" name="recordSetDelete"><BR>delete from #this.tableName#<BR>where <cfset fv=evaluate("this.recordSet.#arguments.IDName#[#this.rownum#]")><BR><cfif isNumeric(fv)><BR>#arguments.IDName#=<cfqueryparam cfsqltype="cf_sql_numeric" value="#fv#"><BR><cfelse><BR>#arguments.IDName#=<cfqueryparam cfsqltype="cf_sql_varchar" value="#fv#"><BR></cfif><BR></cfquery><BR></cffunction><BR><cffunction name="update" access="public" output="false"><BR><cfargument name="IDName" type="string" required="no"><BR><cfset fieldsList=StructKeyList(this.fields,",")><BR><cfset i=1><BR><cfif this.mode eq "addnew"><BR><cfquery datasource="#this.recordSource#" name="recordSetInsert"><BR>insert into <BR>#this.tableName# (#fieldsList#)<BR>values(<BR><cfloop index="f" list="#fieldsList#" delimiters=","><BR><cfset fv=evaluate("this.fields.#f#.value")><cfset ftype=evaluate("this.fields.#f#.fieldType")><BR><cfif i eq 1><cfset comma=""><cfelse><cfset comma=","></cfif><cfset i=2><BR>#comma#<cfqueryparam cfsqltype="cf_sql_#ftype#" value="#fv#"><BR></cfloop>)<BR></cfquery><BR><cfelseif this.mode eq "edit"><BR><cfif not isdefined("arguments.IDName")><cfset arguments.IDName=ListContainsNoCase(this.recordSet.ColumnList,"id",",")></cfif><BR><cfif arguments.IDName eq ""><cfset arguments.IDName=ListFirst(this.recordSet.ColumnList,",")></cfif><BR><cfquery datasource="#this.recordSource#" name="recordSetUpdate"><BR>update <BR>#this.tableName#<BR>set<BR><cfloop index="f" list="#fieldsList#" delimiters=","><BR><cfset fv=evaluate("this.fields.#f#.value")><cfset ftype=evaluate("this.fields.#f#.fieldType")><BR><cfif i eq 1><cfset comma=""><cfelse><cfset comma=","></cfif><cfset i=2><BR>#comma##f#=<cfqueryparam cfsqltype="cf_sql_#ftype#" value="#fv#"><BR></cfloop><BR>where <cfset fv=evaluate("this.recordSet.#arguments.IDName#[#this.rownum#]")><BR><cfif isNumeric(fv)><BR>#arguments.IDName#=<cfqueryparam cfsqltype="cf_sql_numeric" value="#fv#"><BR><cfelse><BR>#arguments.IDName#=<cfqueryparam cfsqltype="cf_sql_varchar" value="#fv#"><BR></cfif><BR></cfquery><BR></cfif><BR></cffunction><BR></cfcomponent></FONT></STRONG></P></TD></TR></TBODY></TABLE></P>

页: [1]
© 1999-2008 EvilOctal Security Team