//object.cfc
<cfcomponent displayname="Object">
<!--- struct to contain object/db mapping --->
<!--- dbMap.objectAttribute.column = 'dbColumnName'; --->
<!--- dbMap.objectAttribute.type = '[number|string|date]'; --->
<!--- 建立一个对象和数据间的影射 --->
<cfset dbMap = structNew()>
<!--- 构造一个对象根据ID --->
<!--- constructor() --->
<cffunction name="init" returntype="void" output="false">
<cfargument name="objId" type="numeric" required="yes"/>
<cfset var _get = "">
<cfset var _column = "">
<cfset var _value = "">
<cfquery name="_get" datasource="#request.dsn#">
SELECT * FROM #this.tableName# WHERE #this.pk# = #arguments.objId#
</cfquery>
<cfloop collection="#dbMap#" item="attr">
<cfset _column = dbMap[attr].column>
<cfset _value = _get[_column]>
<cfif dbMap[attr].type IS "date">
<cfset _value = _get[_column]>
<cfset "this.#attr#" = dateFormat(_value, 'mm/dd/yyyy')>
<cfelse>
<cfset "this.#attr#" = trim(_value)>
</cfif>
</cfloop>
</cffunction>
<!--- 根据传过来的ID DELETE记录 --->
<!--- delete() --->
<cffunction name="delete" returntype="void" output="false">
<cfargument name="pk" required="true" type="numeric" />
<cfloop collection="#dbMap#" item="attr">
<cfset _column = evaluate("dbMap." & attr & ".column")>
<cfset _type ="cf_sql_"&evaluate("dbMap." & attr & ".type")>
<cfif _column IS this.pk>
<cfquery name="delete" datasource="#request.dsn#">
DELETE FROM #this.tableName# WHERE #this.pk# =<cfqueryparam value="#arguments.pk#" cfsqltype="#_type#">
</cfquery>
</cfif>
</cfloop>
</cffunction>
<!--- 获取所有的记录 --->
<!--- getAll() --->
<cffunction name="getAll" returntype="query" output="false">
<cfset var _getAll = "">
<cfquery name="_getAll" datasource="#request.dsn#">
SELECT * FROM #this.tableName#
<cfif len(this.orderby)>
ORDER BY #this.orderBy#
</cfif>
<cfif len(this.groupby)>
group by #this.groupby#
</cfif>
</cfquery>
<cfreturn _getAll />
</cffunction>
<cffunction name="getBy" returntype="query" output="false">
<cfargument name="_getby" required="yes">
<cfargument name="_value" required="yes">
<cfloop collection="#dbMap#" item="attr">
<cfset _column = evaluate("dbMap." & attr & ".column")>
<cfset _type ="cf_sql_"&evaluate("dbMap." & attr & ".type")>
<cfif _column IS _getBy>
<cfquery name="_getAll" datasource="#request.dsn#">
SELECT * FROM #this.tableName# where #_getby#=<cfqueryparam value="#_value#" cfsqltype="#_type#">
<cfif len(this.orderby)>
ORDER BY #this.orderBy#
</cfif>
<cfif len(this.groupby)>
group by #this.groupby#
</cfif>
</cfquery>
</cfif>
</cfloop>
<cfreturn _getAll />
</cffunction>
<!--- 获取最大的ID --->
<!--- getSequenceCurrent() --->
<cffunction name="getSequenceCurrent" returntype="numeric">
<cfset var _Max = "">
<cfquery name="_Max" datasource="#request.dsn#">
SELECT LAST_INSERT_ID() AS id FROM #this.tableName#
</cfquery>
<cfreturn _Max.id />
</cffunction>
<!--- 插入记录 --->
<!--- insertRecord() --->
<cffunction name="insertRecord" returntype="void">
<cfset var _column = "">
<cfset var _columns = "">
<cfset var _value = "">
<cfset var _values = "">
<cfset var _type = "">
<cfloop collection="#dbMap#" item="attr">
<cfset _column = evaluate("dbMap." & attr & ".column")>
<cfset _value = evaluate("this." & attr)>
<cfset _type = evaluate("dbMap." & attr & ".type")>
<cfif _column NEQ this.pk>
<cfswitch expression="#_type#">
<cfcase value="date">
<cfif isDate(_value)>
<cfset _value = dateFormat(_value)>
<cfset _value = "'" & _value & "'">
<cfelse>
<cfset _value = 'null'>
</cfif>
</cfcase>
<cfdefaultcase>
<cfset _value = replace(_value, "'", "''", "ALL")>
<cfset _value = "'" & _value & "'">
</cfdefaultcase>
</cfswitch>
<cfset _columns = listAppend(_columns, _column)>
<cfset _values = listAppend(_values, _value)>
</cfif>
</cfloop>
<cfquery name="insert" datasource="#request.dsn#">
INSERT INTO #this.tableName# (#_columns#) valueS (#preserveSingleQuotes(_values)#)
</cfquery>
</cffunction>
<!--- passport() --->
<cffunction name="passport" returntype="string" output="false">
<cfargument name="privilege" type="string" required="yes" />
<cfreturn StructFind(session.passport, privilege) />
</cffunction>
<!--- save() --->
<cffunction name="save" returntype="void">
<cfif evaluate("this." & this.objId) EQ 0>
<cftransaction>
<cfset nothing = insertRecord()>
<cfset "this.#this.objId#" = getSequenceCurrent()>
</cftransaction>
<cfelse>
<cfset nothing = updateRecord()>
</cfif>
</cffunction>
<!--- set() --->
<!--- loops thru in struct and finds matching varnames in this. --->
<cffunction name="set" returntype="void" output="false">
<cfargument name="obj" type="struct" required="yes" />
<cfloop collection="#arguments.obj#" item="attr">
<cfif StructKeyExists(this, attr)>
<cfset "this.#attr#" = arguments.obj[attr]>
</cfif>
</cfloop>
</cffunction>
<cffunction name="get" returntype="any" output="false">
<cfargument name="attr" type="string" required="yes">
<cfif structKeyExists(this,attr)>
<cfreturn "#evaluate('this.'&attr)#">
</cfif>
</cffunction>
<cffunction name="checkSame">
<cfquery name="check" datasource="#request.dsn#">
<cfoutput>
select #this.pk#
from #this.tableName#
where #arguments[2]#='#arguments[3]#'
and #this.pk#!=#arguments[1]#
</cfoutput>
</cfquery>
<cfreturn check.recordcount>
</cffunction>
<!--- updateRecord() --->
<!--- overwrites Object.updateRecord() --->
<cffunction name="updateRecord" returntype="void">
<cfset var _column = "">
<cfset var _value = "">
<cfset var _type = "">
<cfset var _phrase = "">
<cfset var _sql = "">
<cfset var _pk = "">
<cfloop collection="#dbMap#" item="attr">
<cfset _column = evaluate("dbMap." & attr & ".column")>
<cfset _value = evaluate("this." & attr)>
<cfset _type = evaluate("dbMap." & attr & ".type")>
<cfif _column IS this.pk>
<cfset _pk = _value>
<cfelse>
<cfswitch expression="#_type#">
<cfcase value="date">
<cfif isDate(_value)>
<cfset _value = dateFormat(_value)>
<cfset _value = "'" & _value & "'">
<cfelse>
<cfset _value = 'null'>
</cfif>
</cfcase>
<cfdefaultcase>
<cfset _value = replace(_value, "'", "''", "ALL")>
<cfset _value = "'" & _value & "'">
</cfdefaultcase>
</cfswitch>
</cfif>
<cfset _phrase = _column & " = " & _value>
<cfset _sql = listAppend(_sql, _phrase)>
</cfloop>
<cfquery name="update" datasource="#request.dsn#">
UPDATE #this.tableName# SET #preserveSingleQuotes(_sql)# WHERE #this.pk#
= #_pk#
</cfquery>
</cffunction>
<cffunction name="showMsg">
<cfoutput>
<script language="javascript">
alert("#arguments[1]#");
window.location="#arguments[2]#";
</script>
</cfoutput>
</cffunction>
<cffunction name="include">
<cfargument name="template">
<cfinclude template="#template#">
</cffunction>
</cfcomponent>