Skip to content

Instantly share code, notes, and snippets.

@neokoenig
Created August 19, 2016 08:21
Show Gist options
  • Save neokoenig/befe5e331ce5a6372627824361bfbbb6 to your computer and use it in GitHub Desktop.
Save neokoenig/befe5e331ce5a6372627824361bfbbb6 to your computer and use it in GitHub Desktop.
<cfif listFirst(server.coldfusion.productVersion) LT "11">
<!---
Backport of QueryExecute in CF11 to CF9 &amp; CF10
@param sql_statement SQL. (Required)
@param queryParams Struct of query param values. (Optional)
@param queryOptions Query options. (Optional)
@return Returns a query.
@author Henry Ho ([email protected])
@version 1, September 22, 2014
--->
<cffunction name="QueryExecute" output="false"
hint="
* result struct is returned to the caller by utilizing URL scope (no prefix needed) *
https://wikidocs.adobe.com/wiki/display/coldfusionen/QueryExecute">
<cfargument name="sql_statement" required="true">
<cfargument name="queryParams" default="#structNew()#">
<cfargument name="queryOptions" default="#structNew()#">
<cfset var parameters = []>
<cfif isArray(queryParams)>
<cfloop array="#queryParams#" index="local.param">
<cfif isSimpleValue(param)>
<cfset arrayAppend(parameters, {value=param})>
<cfelse>
<cfset arrayAppend(parameters, param)>
</cfif>
</cfloop>
<cfelseif isStruct(queryParams)>
<cfloop collection="#queryParams#" item="local.key">
<cfif isSimpleValue(queryParams[key])>
<cfset arrayAppend(parameters, {name=local.key, value=queryParams[key]})>
<cfelse>
<cfset var parameter = {name=key}>
<cfset structAppend(parameter, queryParams[key])>
<cfset arrayAppend(parameters, parameter)>
</cfif>
</cfloop>
<cfelse>
<cfthrow message="unexpected type for queryParams">
</cfif>
<cfif structKeyExists(queryOptions, "result")>
<!--- strip scope, not supported --->
<cfset queryOptions.result = listLast(queryOptions.result, '.')>
</cfif>
<cfset var executeResult = new Query(sql=sql_statement, parameters=parameters, argumentCollection=queryOptions).execute()>
<cfif structKeyExists(queryOptions, "result")>
<!--- workaround for passing result struct value out to the caller by utilizing URL scope (no prefix needed) --->
<cfset URL[queryOptions.result] = executeResult.getPrefix()>
</cfif>
<cfreturn executeResult.getResult()>
</cffunction>
</cfif>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment