From 7ae968a16133dc5baf89a22b68294646b2c664c3 Mon Sep 17 00:00:00 2001 From: dumpweed Date: Fri, 12 Jul 2013 11:13:26 -0700 Subject: [PATCH] Refactored to use cfscript functions --- _parameterizeQueries.cfm | 632 +++++++++++++++++++++------------------ 1 file changed, 335 insertions(+), 297 deletions(-) diff --git a/_parameterizeQueries.cfm b/_parameterizeQueries.cfm index 48537e4..62d9ce2 100644 --- a/_parameterizeQueries.cfm +++ b/_parameterizeQueries.cfm @@ -1,298 +1,336 @@ - - - - - - - - - - - - - - - - - - - Queries - - - + + + + function getTypeStr(theParam) { + // Put you heuristic here + if (theParam contains "now()" or theParam contains "date") { + return "CF_SQL_TIMESTAMP"; + } + return ""; + } + + function buildNewParam(theParam) { + return buildCfqueryparam(theParam, buildTypeAttr(theParam)); + } + + function buildTypeAttr(theParam) { + var typeStr = getTypeStr(theParam); + + var typeAttr = ""; + if (typeStr neq "") { + typeAttr = " cfsqltype=""#typeStr#"""; + } + return typeAttr; + } + + function buildCfqueryparam(theParam, typeAttr) { + return ""; + } + + function buildNewParamForDisplay(theParam, newParam) { + return buildCfqueryparamForDisplay(newParam, theParam, buildTypeAttr(theParam)); + } + + function buildCfqueryparamForDisplay(newParam, theParam, typeAttr) { + return newParam & "<cfqueryparam value=""#theParam#""#typeAttr#>"; + } + + function rewriteQuery(SQL, pattern) { + var theParam = ""; + var newParam = ""; + var prefix = ""; + var startIdx = 1; + var st = reFind(pattern,SQL,startIdx,true); + while (st.pos[1]) { + prefix = mid(SQL, startIdx, st.pos[1] - startIdx); + theParam = mid(SQL, st.pos[2], st.len[2]); + if (left(theParam,1) IS "'") { + theParam=mid(theParam,2,len(theParam)-2); + } + if (prefix does not contain "<cf") { + SQL=removechars(SQL,st.pos[2],st.len[2]); + newParam = buildNewParam(theParam); + SQL=insert(newParam,SQL,st.pos[2]-1); + startIdx = st.pos[1]+len(newParam); + } else { + startIdx = st.pos[2] + st.len[2]; + } + st = reFind(pattern, SQL, startIdx, true); + } + return SQL; + } + + function rewriteQueryForDisplay(SQL, pattern) { + var theParam = ""; + var newParam = ""; + var data = structNew(); + var Fixable=false; + var prefix = ""; + var startIdx = 1; + var st = reFind(pattern,SQL,startIdx,true); + while (st.pos[1]) { + prefix = mid(SQL, startIdx, st.pos[1] - startIdx); + theParam = mid(SQL, st.pos[2], st.len[2]); + if (left(theParam,1) IS "'") { + theParam=mid(theParam,2,len(theParam)-2); + } + if (prefix does not contain "<cf") { + Fixable=true; + newParam = "" & theParam; + newParam = buildNewParamForDisplay(theParam, newParam); + SQL=removechars(SQL,st.pos[2],st.len[2]); + SQL=insert(newParam,SQL,st.pos[2]-1); + startIdx = st.pos[1]+len(newParam); + } else { + startIdx = st.pos[2] + st.len[2]; + } + st = reFind(pattern,SQL,startIdx,true); + } + data.Fixable= Fixable; + data.SQL = SQL; + return data; + } + + function getPattern(queryType) { + var pattern = ""; + if (queryType is "insert") { + pattern = "([']?##[^##]+##[']?)"; + } else { + pattern = "=[[:space:]]*([']?##[^##]+##[']?)"; + } + return pattern; + } + + + + + + + + + + + + + + + + + + + + + + + + Queries + + + - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - ",TheFile,curpos)> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Files in #CurDir#:
NameInfo
#Dir.Name# - #Dir.Size# bytes, #NumLines# Lines - - -
- ")> -
#htmlcodeformat(StartTag)#
- - - ",TheFile,curPos)+10> - -
#htmlcodeformat(StartTag & sql)#
- - - ",TheFile,curPos)+10> - - - - - - - - - - - - - - - - - "> - - "> - - - - - - - - - - - - - "> - - "> - - - - - - - - - Parameterized!
-
#htmlcodeformat(StartTag & SQL)#
-
- - - - - - - - - - - - - - - - - - - <CFQUERYPARAM Value=""#theParam#"" cfsqltype=""CF_SQL_TIMESTAMP"">"> - - <CFQUERYPARAM Value=""#theParam#"">"> - - - - - - - - - - - - - - - - <CFQUERYPARAM Value=""#theParam#"" cfsqltype=""CF_SQL_TIMESTAMP"">"> - - <CFQUERYPARAM Value=""#theParam#"">"> - - - - - - - - - CHECKED>Parameterize Me:
-
#htmlcodeformat(StartTag)##SQL#
- - -
- -
File "#CurDir#\#Dir.Name#" written. Old version saved as ".old"
File "#CurDir#\#Dir.Name#.new" written.
- Error parsing query: -
#htmlcodeformat(StartTag & SQL)#
- #cfcatch.message#
- #cfcatch.detail# -
Totals:#numberFormat(TotalSize)##numberFormat(TotalLines)#
- - - - - - - - - - - - - - - - - - - - - - - - - - -
 SizeLines
Grand Totals:#numberFormat(TotalSize)##numberFormat(TotalLines)#
- - - - -
-
+
+ + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + ",TheFile,curpos)> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Files in #CurDir#:
NameInfo
#Dir.Name# + #Dir.Size# bytes, #NumLines# Lines + + +
+ ")> +
#htmlcodeformat(StartTag)#
+ + + ",TheFile,curPos)+10> + +
#htmlcodeformat(StartTag & sql)#
+ + + ",TheFile,curPos)+10> + + + + + + + ; + + Parameterized!
+
#htmlcodeformat(StartTag & SQL)#
+
+ + + + + + + + + + + CHECKED>Parameterize Me:
+
#htmlcodeformat(StartTag)##SQL#
+ + +
+ +
File "#CurDir#\#Dir.Name#" written. Old version saved as ".old"
File "#CurDir#\#Dir.Name#.new" written.
+ Error parsing query: +
#htmlcodeformat(StartTag & SQL)#
+ #cfcatch.message#
+ #cfcatch.detail# +
Totals:#numberFormat(TotalSize)##numberFormat(TotalLines)#
+ + + + + + + + + + + + + + + + + + + + + + + + + + +
 SizeLines
Grand Totals:#numberFormat(TotalSize)##numberFormat(TotalLines)#
+ + + + +
+
\ No newline at end of file