-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path_parameterizeQueries.cfm
More file actions
298 lines (276 loc) · 10.9 KB
/
_parameterizeQueries.cfm
File metadata and controls
298 lines (276 loc) · 10.9 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!---
_ParameterizeQueries.cfm v1.5 (20080721)
Written by Daryl Banttari dbanttari@gmail.com
RELEASED TO THE PUBLIC DOMAIN. But feel free to credit me with original authorship if you release it with modifications.
Purpose:
Seek out unparamaterized queries in ColdFusion templates and, at user's option,
parameterize them.
Use:
Place _ParameterizeQueries.cfm in a document directory and load.
Template will start from its current directory and proceed to read all .cfm documents in that
directory, find and report all <CFQUERY>s found, and, if it looks like there's a spot that
<CFQUERYPARAM> can be used, give you the option to parameterize the query.
If "beRecursive" is set to True (just after these comments), it will recursively
search all subdirectories, too.
If "overwriteInPlace" is set to True (just after these comments), it will replace the files
in place, and save a copy of the "before" file as ".old". If false, changes will be saved in
files with ".new" appended.
To parameterize, click the "Parameterize!" button at the bottom, and all selected queries
will be parameterized, and the resulting template saved.
Be sure to test the changes before placing the new code into production!!!
Templates beginning with an underscore character ("_") will be skipped.
If working recursively, directories starting with a period (".") will be skipped.
Do NOT leave this on production servers..!
Legal:
Furnished without warranty of ANY KIND including merchantability
or fitness for any particular purpose. Use at your own exclusive risk.
--->
<!--- set to True to work on directories recursively --->
<CFSET beRecursive=true>
<!--- set to True to overwrite files (saving old ones as .old), False to create new ".new" files. --->
<CFSET overwriteInPlace=true>
<!--- default the checkbox to CHECKED --->
<CFSET isDefaultChecked=true>
<!--- don't edit below this line (unless you don't mind breaking stuff!) --->
<CFSET crlf = "
">
<CFIF isDefined("Attributes.CurDir")>
<CFSET CurDir = Attributes.CurDir>
<CFELSE>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Queries</title>
</head>
<body>
<CFSET CurDir="#GetDirectoryFromPath(CGI.Path_Translated)#">
</CFIF>
<CFDIRECTORY Action="List"
Directory="#CurDir#"
Name="Dir"
Filter="*.cfm">
<FORM Action="_parameterizeQueries.cfm" Method="POST">
<CFPARAM Name="ffFixMe" default="">
<TABLE Border=1>
<CFOUTPUT>
<TR>
<TH Colspan=2 bgColor="ffff99"><font face="arial">Files in #CurDir#:</font></TH>
</TR>
</CFOUTPUT>
<TR>
<TH bgColor="eeeeee"><font face="arial">Name</font></TH>
<TH bgColor="eeeeee"><font face="arial">Info</font></TH>
</TR>
<CFSET TotalLines=0>
<CFSET TotalSize=0>
<cfset sql = "">
<CFLOOP Query="Dir">
<CFIF left(Dir.Name,1) IS NOT "_">
<CFFILE Action="Read" File="#CurDir#\#Dir.Name#" Variable="TheFile">
<cfset theOriginalFile = theFile>
<cfset rewrite=false>
<CFOUTPUT>
<TR>
<TD><font face="arial">#Dir.Name#</font></TD>
<TD><CFSET NumLines = ListLen(TheFile,crlf)>
<font face="arial">#Dir.Size# bytes, #NumLines# Lines</font>
<CFSET TotalSize = TotalSize + Dir.Size>
<CFSET TotalLines = TotalLines + Numlines>
</TD>
</TR>
</CFOUTPUT>
<cftry>
<CFSET curpos = findNoCase("<CFQUERY",TheFile)>
<CFLOOP Condition="#curpos#">
<CFSET endOfTagPos = findNoCase(">",TheFile,curpos)>
<CFSET StartTag = mid(TheFile,curpos,endOfTagPos-curpos+1)>
<CFOUTPUT><TR><TD></TD><TD></CFOUTPUT>
<CFIF (StartTag CONTAINS "SQL=") or (right(StartTag,2) IS "/>")>
<CFOUTPUT><pre>#htmlcodeformat(StartTag)#</pre></CFOUTPUT>
<CFSET curpos = endOfTagPos-curpos+1>
<CFELSEIF (startTag contains "cachedWithin")>
<CFSET endTagPos = findNoCase("</CFQUERY>",TheFile,curPos)+10>
<CFSET SQL = mid(TheFile, EndOfTagPos+1, endTagPos-EndOfTagPos-1)>
<CFOUTPUT><pre><font color="red">#htmlcodeformat(StartTag & sql)#</font></pre></CFOUTPUT>
<CFSET curpos = endOfTagPos>
<CFELSE>
<CFSET endTagPos = findNoCase("</CFQUERY>",TheFile,curPos)+10>
<CFSET SQL = mid(TheFile, EndOfTagPos+1, endTagPos-EndOfTagPos-1)>
<cfset queryType = getToken(trim(sql), 1)>
<CFSET SQLHash = hash(SQL)>
<CFIF listFind(ffFixMe,SQLHash)>
<!--- actually fix the sql --->
<cfset rewrite=true>
<CFIF SQL does not contain "CFQUERYPARAM">
<cfif queryType is "insert">
<CFSET st = reFind("([']?##[^##]+##[']?)",SQL,1,true)>
<CFLOOP Condition="#st.pos[1]#">
<CFSET theParam = mid(SQL, st.pos[2], st.len[2])>
<CFIF left(theParam,1) IS "'">
<CFSET theParam=mid(theParam,2,len(theParam)-2)>
</CFIF>
<CFSET SQL=removechars(SQL,st.pos[2],st.len[2])>
<cfif theParam contains "now()" or theParam contains "date">
<cfset newParam = "<CFQUERYPARAM Value=""#theParam#"" cfsqltype=""CF_SQL_TIMESTAMP"">">
<cfelse>
<cfset newParam = "<CFQUERYPARAM Value=""#theParam#"">">
</cfif>
<CFSET SQL=insert(newParam,SQL,st.pos[2]-1)>
<CFSET st = reFind("([']?##[^##]+##[']?)", SQL, st.pos[1]+len(newParam), true)>
</CFLOOP>
<cfelse>
<CFSET st = reFind("=[[:space:]]*([']?##[^##]+##[']?)",SQL,1,true)>
<CFLOOP Condition="#st.pos[1]#">
<CFSET theParam = mid(SQL, st.pos[2], st.len[2])>
<CFIF left(theParam,1) IS "'">
<CFSET theParam=mid(theParam,2,len(theParam)-2)>
</CFIF>
<cfif theParam contains "now()" or theParam contains "date">
<cfset newParam = "<CFQUERYPARAM Value=""#theParam#"" cfsqltype=""CF_SQL_TIMESTAMP"">">
<cfelse>
<cfset newParam = "<CFQUERYPARAM Value=""#theParam#"">">
</cfif>
<CFSET SQL=removechars(SQL,st.pos[2],st.len[2])>
<CFSET SQL=insert(newParam, SQL, st.pos[2]-1)>
<CFSET st = reFind("\=[[:space:]]*([']?##[^##]+##[']?)", SQL, st.pos[1]+len(newParam), true)>
</CFLOOP>
</cfif>
</CFIF>
<CFOUTPUT>
<strong>Parameterized!</strong><br>
<pre>#htmlcodeformat(StartTag & SQL)#</pre>
</CFOUTPUT>
<CFSET TheFile = removeChars(TheFile, EndOfTagPos+1, endTagPos-EndOfTagPos-1)>
<CFSET TheFile = insert(SQL, TheFile, EndOfTagPos)>
<CFSET Curpos = EndOfTagPos+1+len(SQL)>
<CFELSE>
<CFSET SQL = htmlCodeFormat(SQL)>
<CFSET SQL = htmlCodeFormat(mid(TheFile, EndOfTagPos+1, endTagPos-EndOfTagPos-1))>
<CFSET Fixable=false>
<CFIF SQL does not contain "CFQUERYPARAM">
<cfif queryType is "insert">
<CFSET st = reFind("([']?##[^##]+##[']?)",SQL,1,true)>
<CFLOOP Condition="#st.pos[1]#">
<CFSET Fixable=true>
<CFSET theParam = mid(SQL, st.pos[2], st.len[2])>
<cfset newParam = "<strike>" & theParam>
<CFIF left(theParam,1) IS "'">
<CFSET theParam=mid(theParam,2,len(theParam)-2)>
</CFIF>
<cfif theParam contains "now()" or theParam contains "date">
<cfset newParam = newParam & "</strike><b><CFQUERYPARAM Value=""#theParam#"" cfsqltype=""CF_SQL_TIMESTAMP""></b>">
<cfelse>
<cfset newParam = newParam & "</strike><b><CFQUERYPARAM Value=""#theParam#""></b>">
</cfif>
<CFSET SQL=removechars(SQL,st.pos[2],st.len[2])>
<CFSET SQL=insert(newParam,SQL,st.pos[2]-1)>
<CFSET st = reFind("([']?##[^##]+##[']?)",SQL,st.pos[1]+len(newParam),true)>
</CFLOOP>
<cfelse>
<CFSET st = reFind("=[[:space:]]*([']?##[^##]+##[']?)",SQL,1,true)>
<CFLOOP Condition="#st.pos[1]#">
<CFSET Fixable=true>
<CFSET theParam = mid(SQL, st.pos[2], st.len[2])>
<cfset newParam = "<strike>" & theParam>
<CFIF left(theParam,1) IS "'">
<CFSET theParam=mid(theParam,2,len(theParam)-2)>
</CFIF>
<cfif theParam contains "now()" or theParam contains "date">
<cfset newParam = newParam & "</strike><b><CFQUERYPARAM Value=""#theParam#"" cfsqltype=""CF_SQL_TIMESTAMP""></b>">
<cfelse>
<cfset newParam = newParam & "</strike><b><CFQUERYPARAM Value=""#theParam#""></b>">
</cfif>
<CFSET SQL=removechars(SQL,st.pos[2],st.len[2])>
<CFSET SQL=insert(newParam, SQL, st.pos[2]-1)>
<CFSET st = reFind("\=[[:space:]]*([']?##[^##]+##[']?)",SQL,st.pos[1]+st.len[1]+50+len(theParam),true)>
</CFLOOP>
</cfif>
</CFIF>
<CFOUTPUT>
<CFIF Fixable><INPUT Type="Checkbox" Name="ffFixMe" Value="#SQLHash#" <cfif isDefaultChecked>CHECKED</cfif>>Parameterize Me:<br></CFIF>
<pre>#htmlcodeformat(StartTag)##SQL#</pre>
</CFOUTPUT>
<CFSET curpos = endTagPos>
</CFIF>
</CFIF>
<CFOUTPUT></TD></TR></CFOUTPUT>
<CFSET curpos = findNoCase("<CFQUERY",TheFile,curpos)>
</CFLOOP>
<CFIF ReWrite>
<cfif overwriteInPlace>
<CFFILE Action="Write"
File="#CurDir#\#Dir.Name#.old"
OUTPUT="#TheOriginalFile#"
ADDNEWLINE="No"
>
<CFFILE Action="Write"
File="#CurDir#\#Dir.Name#"
OUTPUT="#TheFile#"
ADDNEWLINE="No"
>
<CFOUTPUT><TR><TD></TD><TD>File "#CurDir#\#Dir.Name#" written. Old version saved as ".old"</TD></TR></CFOUTPUT>
<cfelse>
<CFFILE Action="Write"
File="#CurDir#\#Dir.Name#.new"
OUTPUT="#TheFile#"
ADDNEWLINE="No"
>
<CFOUTPUT><TR><TD></TD><TD>File "#CurDir#\#Dir.Name#.new" written.</TD></TR></CFOUTPUT>
</cfif>
</CFIF>
<cfcatch type="any">
<cfoutput>
<TR><TD></TD><TD>
<strong>Error parsing query:</strong>
<pre>#htmlcodeformat(StartTag & SQL)#</pre>
#cfcatch.message#<br>
#cfcatch.detail#
</TD></TR>
<CFSET curpos = endTagPos>
</cfoutput>
</cfcatch>
</cftry>
</CFIF>
<CFFLUSH>
</CFLOOP>
<CFOUTPUT>
<TR>
<TD bgColor="eeeeee"><font face="arial"><b>Totals:</b></font></TD>
<TD bgColor="eeeeee" Align="Right"><font face="arial">#numberFormat(TotalSize)#</font></TD>
<TD bgColor="eeeeee" Align="Right"><font face="arial">#numberFormat(TotalLines)#</font></TD>
</TR>
</CFOUTPUT>
</TABLE>
<CFIF beRecursive>
<CFDIRECTORY Action="List"
Directory="#CurDir#"
Name="Dir">
<CFLOOP Query="Dir">
<CFIF Dir.Type IS "Dir" AND left(Dir.Name,1) IS NOT ".">
<CFMODULE template="_parameterizeQueries.cfm" CurDir="#CurDir#\#Dir.Name#">
</CFIF>
</CFLOOP>
</CFIF>
<CFIF isDefined("Attributes.CurDir")>
<CFSET Caller.TotalSize = Caller.TotalSize + TotalSize>
<CFSET Caller.TotalLines = Caller.TotalLines + TotalLines>
<CFELSE>
<CFOUTPUT>
<TABLE>
<TR>
<TH bgColor="eeeeee"> </TH>
<TH bgColor="eeeeee"><font face="arial">Size</font></TH>
<TH bgColor="eeeeee"><font face="arial">Lines</font></TH>
</TR>
<TR>
<TD bgColor="eeeeee"><font face="arial"><b>Grand Totals:</b></font></TD>
<TD bgColor="eeeeee" Align="Right"><font face="arial">#numberFormat(TotalSize)#</font></TD>
<TD bgColor="eeeeee" Align="Right"><font face="arial">#numberFormat(TotalLines)#</font></TD>
</TR>
</TABLE>
<INPUT Type="Submit" Value="Parameterize Selected">
</FORM>
</body>
</html>
</CFOUTPUT>
</CFIF>