Created
September 5, 2010 21:35
-
-
Save seancorfield/566343 to your computer and use it in GitHub Desktop.
This file contains 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
<cffunction name="executeSQL" returntype="any" access="public" output="false"> | |
<cfargument name="SQL" type="string" required="true" /> | |
<cfargument name="parameters" default="#structNew()#" /> | |
<cfargument name="types" default="#structNew()#" /> | |
<cfargument name="cachedWithin" required="false" /> | |
<cfset var query = 0 /> | |
<cfset var result = 0 /> | |
<cfset var generatedSQL = '/model/generatedSQL/' /> | |
<cfparam name="variables.datasource" default="#variables.defaultDSN#" /> | |
<cfset var attrs = { name = 'query', datasource = variables.datasource, result = 'result' } /> | |
<cfif variables.datasource is variables.defaultDSN and left( arguments.SQL, 7 ) is 'select '> | |
<cfset attrs.datasource = variables.readonlyDSN /> | |
</cfif> | |
<cfif structKeyExists( arguments, 'cachedWithin' )> | |
<cfset attrs.cachedWithin = arguments.cachedWithin /> | |
</cfif> | |
<cfif structCount( arguments.parameters )> | |
<!--- substitute :name with <cfqueryparam> with specified type (default: integer) ---> | |
<!--- we need to write the query to disk first time and we maintain a map of previously processed queries ---> | |
<cfset var qryHash = '' /> | |
<cfif structKeyExists( arguments, 'cachedWithin' )> | |
<cfset qryHash = hash( arguments.SQL & arguments.cachedWithin ) /> | |
<cfelse> | |
<cfset qryHash = hash( arguments.SQL ) /> | |
</cfif> | |
<cfif !structKeyExists( variables.qryMap, qryHash )> | |
<cfset var param = 0 /> | |
<cfloop item="param" collection="#arguments.parameters#"> | |
<cfset var type = 'integer' /> | |
<cfif structKeyExists( arguments.types, param )> | |
<cfset type = arguments.types[ param ] /> | |
</cfif> | |
<cfset isList = '' /> | |
<cfif listFirst( type, ':' ) is 'list'> | |
<cfset type = listLast( type, ':' ) /> | |
<cfset isList = ' list="true"' /> | |
</cfif> | |
<cfset arguments.SQL = replaceNoCase( | |
arguments.SQL, ':' & param, | |
'<cfqueryparam cfsqltype="cf_sql_#type#" value="##arguments.parameters.#param###"#isList# />', | |
'all' ) /> | |
</cfloop> | |
<cfset var qryText = | |
'<cfquery attributeCollection="##attrs##"> | |
#arguments.SQL# | |
</cfquery>' /> | |
<cffile action="write" file="#expandPath( generatedSQL & 'qry_' & qryHash & '.cfm' )#" output="#qryText#" /> | |
<cfset variables.qryMap[ qryHash ] = true /> | |
</cfif> | |
<cfinclude template="#generatedSQL#qry_#qryHash#.cfm" /> | |
<cfelse> | |
<cfquery attributeCollection="#attrs#"> | |
#preservesinglequotes( SQL )# | |
</cfquery> | |
</cfif> | |
<cfreturn query/> | |
</cffunction> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment