Skip to content

Instantly share code, notes, and snippets.

@evagoras
Created June 18, 2013 00:00
Show Gist options
  • Save evagoras/5801584 to your computer and use it in GitHub Desktop.
Save evagoras/5801584 to your computer and use it in GitHub Desktop.
Method to convert a Query to a CSV
<!---
Method to convert query to csv
(sourced from http://www.bennadel.com/blog/1231-Converting-A-ColdFusion-Query-To-CSV-Using-QueryToCSV-.htm)
Change: argument "Fields" not mandatory, taking by default all of the columns in the query.
--->
<cffunction
name="QueryToCSV"
access="public"
returntype="string"
output="false"
hint="I take a query and convert it to a comma separated value string.">
<!--- Define arguments. --->
<cfargument
name="Query"
type="query"
required="true"
hint="I am the query being converted to CSV."
/>
<cfargument
name="Fields"
type="string"
required="false"
default=""
hint="I am the list of query fields to be used when creating the CSV value."
/>
<cfargument
name="CreateHeaderRow"
type="boolean"
required="false"
default="true"
hint="I flag whether or not to create a row of header values."
/>
<cfargument
name="Delimiter"
type="string"
required="false"
default=","
hint="I am the field delimiter in the CSV value."
/>
<!--- Define the local scope. --->
<cfset var LOCAL = {} />
<!---
First, we want to set up a column index so that we can
iterate over the column names faster than if we used a
standard list loop on the passed-in list.
--->
<cfset LOCAL.ColumnNames = [] />
<!---
If no fields were passed in the method,
then use all of the columns in the query.
--->
<cfif NOT len( ARGUMENTS.Fields )>
<!--- GetColumnNames() returns all of the query columns in the correct order. --->
<cfset LOCAL.aFields = ARGUMENTS.Query.GetColumnNames() />
<cfloop from="1" to="#arrayLen(LOCAL.aFields)#" index="position">
<cfset ARGUMENTS.Fields = listAppend( ARGUMENTS.Fields, LOCAL.aFields[position] ) />
</cfloop>
</cfif>
<!---
Loop over column names and index them numerically. We
are working with an array since I believe its loop times
are faster than that of a list.
--->
<cfloop
index="LOCAL.ColumnName"
list="#ARGUMENTS.Fields#"
delimiters=",">
<!--- Store the current column name. --->
<cfset ArrayAppend(
LOCAL.ColumnNames,
Trim( LOCAL.ColumnName )
) />
</cfloop>
<!--- Store the column count. --->
<cfset LOCAL.ColumnCount = ArrayLen( LOCAL.ColumnNames ) />
<!---
Now that we have our index in place, let's create
a string buffer to help us build the CSV value more
effiently.
--->
<cfset LOCAL.Buffer = CreateObject( "java", "java.lang.StringBuffer" ).Init() />
<!--- Create a short hand for the new line characters. --->
<cfset LOCAL.NewLine = (Chr( 13 ) & Chr( 10 )) />
<!--- Check to see if we need to add a header row. --->
<cfif ARGUMENTS.CreateHeaderRow>
<!--- Create array to hold row data. --->
<cfset LOCAL.RowData = [] />
<!--- Loop over the column names. --->
<cfloop
index="LOCAL.ColumnIndex"
from="1"
to="#LOCAL.ColumnCount#"
step="1">
<!--- Add the field name to the row data. --->
<cfset LOCAL.RowData[ LOCAL.ColumnIndex ] = """#LOCAL.ColumnNames[ LOCAL.ColumnIndex ]#""" />
</cfloop>
<!--- Append the row data to the string buffer. --->
<cfset LOCAL.Buffer.Append(
JavaCast(
"string",
(
ArrayToList(
LOCAL.RowData,
ARGUMENTS.Delimiter
) &
LOCAL.NewLine
))
) />
</cfif>
<!---
Now that we have dealt with any header value, let's
convert the query body to CSV. When doing this, we are
going to qualify each field value. This is done be
default since it will be much faster than actually
checking to see if a field needs to be qualified.
--->
<!--- Loop over the query. --->
<cfloop query="ARGUMENTS.Query">
<!--- Create array to hold row data. --->
<cfset LOCAL.RowData = [] />
<!--- Loop over the columns. --->
<cfloop
index="LOCAL.ColumnIndex"
from="1"
to="#LOCAL.ColumnCount#"
step="1">
<!--- Add the field to the row data. --->
<cfset LOCAL.RowData[ LOCAL.ColumnIndex ] = """#Replace( ARGUMENTS.Query[ LOCAL.ColumnNames[ LOCAL.ColumnIndex ] ][ ARGUMENTS.Query.CurrentRow ], """", """""", "all" )#""" />
</cfloop>
<!--- Append the row data to the string buffer. --->
<cfset LOCAL.Buffer.Append(
JavaCast(
"string",
(
ArrayToList(
LOCAL.RowData,
ARGUMENTS.Delimiter
) &
LOCAL.NewLine
))
) />
</cfloop>
<!--- Return the CSV value. --->
<cfreturn LOCAL.Buffer.ToString() />
</cffunction>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment