-
Notifications
You must be signed in to change notification settings - Fork 0
/
query_to_csv.cfc
124 lines (110 loc) · 5.23 KB
/
query_to_csv.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<cfcomponent>
<!--- Credits to Ben Nadel: http://www.bennadel.com/blog/1231-converting-a-coldfusion-query-to-csv-using-querytocsv.htm --->
<cffunction name="query_to_csv" output="yes" returntype="Any">
<!---Function Arguments--->
<cfargument name="Query" required="yes" type="query" default="" hint="The query you want output as a spreadsheet"/>
<cfargument name="Fields" type="string" required="true" hint="The list of query fields to be used when creating the CSV"/>
<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."/>
<cfargument name="QuoteFields" type="boolean" required="false" default="true" hint="Choose to surround each field with quotes or not."/>
<cfargument name="Extension" type="string" required="false" default="csv" hint="Extension of the file generated. The default is csv."/>
<cfargument name="report_name" required="yes" type="string" default="" hint="The name of this report">
<cfset variables.report_name = arguments.report_name>
<cfif ARGUMENTS.QuoteFields>
<cfset quotes = """">
<cfelse>
<cfset quotes = ''>
</cfif>
<cfset var LOCAL = {} />
<cfset LOCAL.ColumnNames = {} />
<cfloop
index="LOCAL.ColumnName"
list="#ARGUMENTS.Fields#"
delimiters=",">
<!--- Store the current column name. --->
<cfset LOCAL.ColumnNames[ StructCount( LOCAL.ColumnNames ) + 1 ] = Trim( LOCAL.ColumnName ) />
</cfloop>
<cfset LOCAL.ColumnCount = StructCount( LOCAL.ColumnNames ) />
<cfquery name="batch_dir_query" datasource="soniswebp" maxrows="1">
select batch_dir from webopts
</cfquery>
<CFIF right(trim(batch_dir_query.batch_dir),1) EQ '/' OR right(trim(batch_dir_query.batch_dir),1) EQ '\'>
<CFSET batch_dir = '#trim(batch_dir_query.batch_dir)#'>
<CFELSE>
<CFSET batch_dir = '#trim(batch_dir_query.batch_dir)#\'>
</CFIF>
<cfset login_id = trim(#session.login_id#)>
<cfset LOCAL.Buffer = CreateObject( "java", "java.lang.StringBuffer" ).Init() />
<cfset LOCAL.NewLine = (Chr( 13 ) & Chr( 10 )) />
<cfif ARGUMENTS.CreateHeaderRow>
<!--- Loop over the column names. --->
<cfloop
index="LOCAL.ColumnIndex"
from="1"
to="#LOCAL.ColumnCount#"
step="1">
<!--- Append the field name. --->
<cfset LOCAL.Buffer.Append(
JavaCast(
"string",
"#quotes##LOCAL.ColumnNames[ LOCAL.ColumnIndex ]##quotes#"
)
) />
<cfif (LOCAL.ColumnIndex LT LOCAL.ColumnCount)>
<!--- Field delimiter. --->
<cfset LOCAL.Buffer.Append(
JavaCast( "string", ARGUMENTS.Delimiter )
) />
<cfelse>
<cfset LOCAL.Buffer.Append(
JavaCast( "string", LOCAL.NewLine )
) />
</cfif>
</cfloop>
</cfif>
<!--- Loop over the query. --->
<cfloop query="ARGUMENTS.Query">
<!--- Loop over the columns. --->
<cfloop
index="LOCAL.ColumnIndex"
from="1"
to="#LOCAL.ColumnCount#"
step="1">
<!--- Append the field value. --->
<cfset LOCAL.Buffer.Append(
JavaCast(
"string",
"#quotes##ARGUMENTS.Query[ LOCAL.ColumnNames[ LOCAL.ColumnIndex ] ][ ARGUMENTS.Query.CurrentRow ]##quotes#"
)
) />
<!---
Check to see which delimiter we need to add:
field or line.
--->
<cfif (LOCAL.ColumnIndex LT LOCAL.ColumnCount)>
<!--- Field delimiter. --->
<cfset LOCAL.Buffer.Append(
JavaCast( "string", ARGUMENTS.Delimiter )
) />
<cfelse>
<!--- Line delimiter. --->
<cfset LOCAL.Buffer.Append(
JavaCast( "string", LOCAL.NewLine )
) />
</cfif>
</cfloop>
</cfloop>
<!--- Write the CSV value to a file and prevent race conditions. --->
<cfquery name="batch_dir_query" datasource="soniswebp" maxrows="1">
select batch_dir from webopts
</cfquery>
<cfset batch_dir = trim(#batch_dir_query.batch_dir#) & "\">
<cfset login_id = trim(#session.login_id#)>
<cfset file_name = #login_id# & "_" & #ReReplace(report_name, "[[:space:]]","","ALL")# & ".#ARGUMENTS.Extension#">
<cfset save_path = #batch_dir# & #login_id# & "_" & #ReReplace(report_name, "[[:space:]]","","ALL")# & ".#ARGUMENTS.Extension#">
<cffile action = "write" file = #save_path# output = #LOCAL.Buffer.ToString()#>
<!--- Stream the file to browser/delete it --->
<cfheader name="Content-Disposition" value="attachment;filename=#file_name#">
<cfcontent type="application/octet-stream" file="#save_path#" deletefile="Yes">
</cffunction>
</cfcomponent>