Permalink
Browse files

added backticks around field- and tablenames

  • Loading branch information...
1 parent 3506a49 commit 4932f1e62c38372caf21038a31574fd622d7b053 @seybsen committed Apr 1, 2012
Showing with 23 additions and 15 deletions.
  1. +23 −15 databasereplacr.cfc
View
@@ -1,50 +1,58 @@
-<CFCOMPONENT output="false">
+<CFCOMPONENT output="false" accessors="true">
<CFPROPERTY name="datasource" type="string" hint="I'm the datasource in which to search and replace" />
+ <CFFUNCTION name="init" output="false" returntype="component">
+ <CFARGUMENT name="datasource" type="string" required="yes" />
+
+ <CFSET THIS.setDatasource(arguments.datasource) />
+
+ <CFRETURN THIS />
+ </CFFUNCTION>
+
<CFFUNCTION name="ReplaceInDB" access="public" returntype="any" output="false" hint="I replace all occurencies of substring in all text columns with given replacestring">
<CFARGUMENT name="substring" type="string" required="yes" default="foo" hint="I'm gettin replaced">
<CFARGUMENT name="replacestring" type="string" required="yes" default="bar" hint="I'm the new string">
+
+ <CFSET var result = arraynew(1) />
- <cfset var erg = arraynew(1) />
-
- <cfset var qTables = getTableNames() />
+ <CFSET var qTables = getTableNames() />
<CFLOOP query="qTables">
- <cfset var lFields = arraytolist(getFields(qTables["table_name"][qTables.currentrow])) />
+ <CFSET var lFields = arraytolist(getFields(qTables["table_name"][qTables.currentrow])) />
<CFIF listlen(lFields)>
- <CFQUERY result="qReplace" datasource="#this.datasource#">
- UPDATE #qTables["table_name"][qTables.currentrow]#
+ <CFQUERY result="qReplace" datasource="#THIS.getDatasource()#">
+ UPDATE `#qTables["table_name"][qTables.currentrow]#`
SET
<CFLOOP from="1" to="#listlen(lFields)#" index="idx">
- #listGetAt(lFields,idx)# = REPLACE(#listGetAt(lFields,idx)#, <CFQUERYPARAM value="#arguments.substring#" cfsqltype="cf_sql_varchar" />,<CFQUERYPARAM value="#arguments.replacestring#" cfsqltype="cf_sql_varchar" />)<CFIF listlen(lFields) NEQ idx>,</CFIF>
+ `#listGetAt(lFields,idx)#` = REPLACE(`#listGetAt(lFields,idx)#`, <CFQUERYPARAM value="#arguments.substring#" cfsqltype="cf_sql_varchar" />,<CFQUERYPARAM value="#arguments.replacestring#" cfsqltype="cf_sql_varchar" />)<CFIF listlen(lFields) NEQ idx>,</CFIF>
</CFLOOP>
WHERE 2 = 1
<CFLOOP from="1" to="#listlen(lFields)#" index="idx">
- OR #listGetAt(lFields,idx)# LIKE <CFQUERYPARAM value="%#arguments.substring#%" cfsqltype="cf_sql_varchar" />
+ OR `#listGetAt(lFields,idx)#` LIKE <CFQUERYPARAM value="%#arguments.substring#%" cfsqltype="cf_sql_varchar" />
</CFLOOP>
</CFQUERY>
- <cfset arrayappend(erg, {"affected_rows"=qReplace.recordCount, "statement"=qReplace.sql}) />
+ <CFSET arrayappend(result, {"affected_rows"=qReplace.recordCount, "statement"=qReplace.sql}) />
</CFIF>
</CFLOOP>
- <CFRETURN erg />
+ <CFRETURN result />
</CFFUNCTION>
<CFFUNCTION name="getTableNames" access="private" output="false" returntype="query" hint="I get all Tablenames from given database">
- <CFDBINFO type="tables" name="qTables" datasource="#this.datasource#" />
+ <CFDBINFO type="tables" name="qTables" datasource="#THIS.getDatasource()#" />
<CFRETURN qTables />
</CFFUNCTION>
<CFFUNCTION name="getTextFieldsInTable" access="private" output="false" returntype="array" hint="I get all text fields from table as array from describe-query">
<CFARGUMENT name="fields" type="query" required="yes">
- <cfset var aFields = arraynew(1) />
+ <CFSET var aFields = arraynew(1) />
<CFLOOP query="fields">
<CFIF listfindnocase("char,text",right(fields.type_name,4))>
- <cfset arrayappend(aFields,fields.column_name) />
+ <CFSET arrayappend(aFields,fields.column_name) />
</CFIF>
</CFLOOP>
@@ -54,7 +62,7 @@
<CFFUNCTION name="getFields" access="private" output="false" returntype="array" hint="I DESCRIBE a table">
<CFARGUMENT name="table" type="string" required="yes">
- <CFDBINFO name="qFields" datasource="#this.datasource#" type="columns" table="#arguments.table#" />
+ <CFDBINFO name="qFields" datasource="#THIS.getDatasource()#" type="columns" table="#arguments.table#" />
<CFRETURN getTextFieldsInTable(qFields) />
</CFFUNCTION>

0 comments on commit 4932f1e

Please sign in to comment.