/
snapshotService.cfc
437 lines (381 loc) · 18.7 KB
/
snapshotService.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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
<cfcomponent hint="I manage the task of creating snapshots of table content">
<cffunction name="init" access="public" output="false" returntype="any">
<cfargument name="datasource" type="string" required="true" />
<cfargument name="nameDelimiter" type="string" required="true">
<cfargument name="JSONSerializer" type="string" required="true" />
<cfargument name="JSONDeserializer" type="string" required="true" />
<cfargument name="tableFilterType" type="string" required="true" hint="Possible values: prefix, suffix, or empty string" />
<cfargument name="tableFilterValue" type="string" required="true" />
<cfargument name="snapshotsFolder" type="string" required="true" />
<cfargument name="backupsFolder" type="string" required="true" />
<cfargument name="loopLimit" type="numeric" required="true" />
<cfargument name="insertThreadLimit" type="numeric" required="true" />
<cfset var qryDatabaseInfo= "" />
<cfset variables.datasource= arguments.datasource>
<cfset variables.nameDelimiter= arguments.nameDelimiter>
<cftry>
<cfset variables.JSONSerializer= CreateObject("component",arguments.JSONSerializer)>
<cfset variables.JSONDeserializer= CreateObject("component",arguments.JSONDeserializer)>
<cfcatch type="any">
<cfthrow message="One or more of the specified JSON library CFCs was not found.">
</cfcatch>
</cftry>
<cfset variables.tableFilterType= arguments.tableFilterType>
<cfset variables.tableFilterValue= arguments.tableFilterValue>
<cfif arguments.datasource NEQ "">
<cfdbinfo type="version" name="qryDatabaseInfo" datasource="#arguments.datasource#">
<cftry>
<cfset variables.dbaseLibrary= CreateObject("component",Replace(qryDatabaseInfo.database_productname," ","","ALL"))>
<cfcatch type="any">
<cfthrow message= "There is no database library file named #qryDatabaseInfo.database_productname#.cfc to handle database type-specific functions.">
</cfcatch>
</cftry>
</cfif>
<cfset variables.baseFolder= getDirectoryFromPath(getCurrentTemplatePath())>
<cfset variables.snapshotsFolder= variables.baseFolder & arguments.snapshotsFolder />
<cfset variables.backupsFolder= variables.baseFolder & arguments.backupsFolder />
<cfset variables.loopLimit= arguments.loopLimit />
<cfset variables.insertThreadLimit= arguments.insertThreadLimit />
<cfset variables.tableHasError= false>
<cfset variables.threadErrorArray= ArrayNew(1)>
<cfset variables.tableMetadata= StructNew()>
<cfreturn this />
</cffunction>
<cffunction name="createBackupSnapshot" access="public" output="false" returntype="void">
<cfargument name="datasource" type="string" required="true" />
<cfargument name="tableName" type="any" required="true" />
<cfset var tstamp= Now()>
<cfset var qry= "">
<cfset var jsonString= "">
<cfset var snapshotName= "">
<cfquery name="qry" datasource="#arguments.datasource#">
select * from #arguments.tableName#
</cfquery>
<cfset jsonString= variables.JSONSerializer.serializeToJSON(qry,true,true)>
<cfset snapshotName= arguments.tableName & variables.nameDelimiter & "autobackup" & variables.nameDelimiter & DateFormat(tstamp,'yyyy-mm-dd') & variables.nameDelimiter & TimeFormat(tstamp,'TT-hh-mm-ss') & ".txt">
<cffile action="write" file="#variables.backupsFolder#/#snapshotName#" output="#jsonString#">
</cffunction>
<cffunction name="deleteSnapshots" access="public" output="false" returntype="void" description="I delete the selected snapshot files">
<cfargument name="fileList" type="string" required="true" />
<cfargument name="type" type="string" required="false" default="snapshots" />
<cfset var f= "">
<cfif arguments.type EQ "backups">
<cfloop index="f" list="#arguments.fileList#" delimiters=",">
<cffile action="delete" file="#variables.backupsFolder#/#f#">
</cfloop>
<cfelse>
<cfloop index="f" list="#arguments.fileList#" delimiters=",">
<cffile action="delete" file="#variables.snapshotsFolder#/#f#">
</cfloop>
</cfif>
</cffunction>
<cffunction name="returnHiddenColumnHTML" access="public" returntype="string" output="false">
<cfargument name="colStruct" type="struct" required="true" />
<cfset var hiddenColumnHTML= "">
<cfset var colCounter= 1>
<cfset var f= "">
<cfsavecontent variable="hiddenColumnHTML">
<cfoutput>
<table border="0" cellpadding="3" cellspacing="2" width="100%">
<tbody>
<cfloop item="f" collection="#colStruct#">
<cfif colCounter EQ 1 or (colCounter mod 7 EQ 1)>
<tr>
</cfif>
<td>
<input class="showBox" type="checkbox" value="#(colCounter-1)#" checked="true">#f#
</td>
<cfif colCounter mod 7 EQ 0>
<tr />
</cfif>
<cfset colCounter= colCounter+1>
</cfloop>
</tbody>
</table>
</cfoutput>
</cfsavecontent>
<cfreturn hiddenColumnHTML />
</cffunction>
<cffunction name="getSnapshotHTMLTable" access="public" output="false" returntype="string">
<cfargument name="snapshotFile" type="string" required="true" />
<cfargument name="type" type="string" required="false" default="snapshots">
<cfset var fileContent= "">
<cfset var buildStruct= "">
<cfset var htmlTable= "">
<cfset var r= 0>
<cfset var c= "">
<cfset var colCounter= 0>
<cfif arguments.type EQ "backups">
<cffile action="read" file="#variables.backupsFolder#/#arguments.snapshotFile#" variable="fileContent">
<cfelse>
<cffile action="read" file="#variables.snapshotsFolder#/#arguments.snapshotFile#" variable="fileContent">
</cfif>
<cfset buildStruct= variables.JSONDeserializer.deserializeFromJSON(fileContent,true)>
<cfsavecontent variable="htmlTable">
<cfoutput>
<div id="columnControls"><a id="colControlsLink" class="hideControls" href="">Hide column controls</a>
<div id="hiddenCols">
<p>Check the columns you want displayed in the table, then click "Update Columns"</p>
<p><a id="selAll" href="##">Select all</a> | <a id="deselAll" href="##">De-select all</a></p>
#returnHiddenColumnHTML(buildStruct.data)#
<p>
<input type="button" name="updateCols" id="updateCols" value="Update Columns"/>
</p>
</div>
</div>
<table name="snapDataTable" id="snapDataTable" class="dataTablesTable" border="1" cellpadding="2" cellspacing="2">
<thead>
<tr>
<cfloop collection="#buildStruct.data#" item="c">
<th align="left">
<p class="chead">#c#</p>
<input type="text" class="search_init" title="Search #c#" alt="#colCounter#" value="Search #c#"/>
</th>
<cfset colCounter= colCounter+1>
</cfloop>
</tr>
</thead>
<tbody>
<cfloop index="r" from="1" to="#buildStruct.rowcount#">
<tr>
<cfloop collection="#buildStruct.data#" item="c">
<td>#buildStruct.data[c][r]#</td>
</cfloop>
</tr>
</cfloop>
</tbody>
</table>
</cfoutput>
</cfsavecontent>
<cfreturn htmlTable />
</cffunction>
<cffunction name="getSnapshotList" access="public" output="false" returntype="query">
<cfargument name="type" type="string" required="false" default="snapshots"/>
<cfset var qry= "">
<cfif arguments.type EQ "backups">
<cfdirectory action="list" directory="#variables.backupsFolder#" name="qry">
<cfelse>
<cfdirectory action="list" directory="#variables.snapshotsFolder#" name="qry">
</cfif>
<cfreturn qry />
</cffunction>
<cffunction name="getTableList" access="public" output="false" returntype="query">
<cfargument name="datasource" type="string" required="false" default="#variables.datasource#" />
<cfargument name="tableFilterType" type="string" required="false" default="#variables.tableFilterType#" />
<cfargument name="tableFilterValue" type="string" required="false" default="#variables.tableFilterValue#" />
<cfset var qry= "">
<cfif arguments.tableFilterType EQ "">
<cfdbinfo type="tables" name="qry" datasource="#arguments.datasource#">
<cfelse>
<cfif arguments.tableFilterType EQ "prefix">
<cfdbinfo type="tables" name="qry" datasource="#arguments.datasource#" pattern="#arguments.tableFilterValue#%">
<cfelseif arguments.tableFilterType EQ "suffix">
<cfdbinfo type="tables" name="qry" datasource="#arguments.datasource#" pattern="%#arguments.tableFilterValue#">
<cfelse>
<cfdbinfo type="tables" name="qry" datasource="#arguments.datasource#">
</cfif>
</cfif>
<cfreturn qry />
</cffunction>
<cffunction name="loadSnapshots" access="public" output="true" returntype="array">
<cfargument name="snapshotList" type="string" required="true" />
<cfargument name="datasource" type="string" required="false" default="#variables.datasource#" />
<cfargument name="type" type="string" required="false" default="snapshots" />
<cfset var f= "">
<cfset var tName= "">
<cfset var tNameList= "">
<cfset var fullTableName= "">
<cfset var metaQry= "">
<cfset var deleteList= "">
<cfset var loopCounter= 0>
<cfset var finishList= "">
<cfset var fileContent= "">
<cfset var buildStruct= "">
<cfset var colCount= 0>
<cfset var n= 1>
<cfset var r= 1>
<cfset var colCounter= 0>
<cfset var c= "">
<cfset var insertParams= StructNew()>
<cfset var fieldValue= "">
<cfset var fieldInsertSQL= "">
<cfset var t= "">
<cfset var threadName= "">
<cfset var threadList= "">
<cfset ArrayClear(variables.threadErrorArray)>
<cfoutput>
<cfloop index="f" list="#arguments.snapshotList#" delimiters=",">
<cfset tName= ListGetAt(f,1,variables.nameDelimiter)>
<cfset tNameList= ListAppend(tNameList,tName)>
<cfset fullTableName= arguments.datasource & "_" & tName>
<!---Gather the metadata (the column names, data types, and nullable status) of the selected tables--->
<cfif Not StructKeyExists(variables.tableMetadata,fullTableName)>
<cfset variables.tableMetadata[fullTableName]= StructNew()>
<cfset variables.tableMetadata[fullTableName].name= tName>
<cfset variables.tableMetadata[fullTableName].columnData= StructNew()>
<cfdbinfo type="Columns" name="metaQry" table="#tName#" datasource="#arguments.datasource#">
<cfloop query="metaQry">
<cfset variables.tableMetadata[fullTableName].columnData[metaQry.column_name]= StructNew()>
<cfset variables.tableMetadata[fullTableName].columnData[metaQry.column_name].dataType= metaQry.type_name>
<cfset variables.tableMetadata[fullTableName].columnData[metaQry.column_name].nullable= metaQry.is_nullable>
</cfloop>
</cfif>
<cfset createBackupSnapshot(arguments.datasource,tName)>
</cfloop>
<!---Use cftransaction to ensure that all selected tables have their table deleted, not just the ones that didn't encounter something like a constraint error--->
<cftransaction>
<cfloop condition="ListLen(deleteList) NEQ ListLen(arguments.snapshotList) AND loopCounter NEQ variables.loopLimit">
<cfset loopCounter= loopCounter+1>
<cfloop index="tName" list="#tNameList#" delimiters=",">
<cfif ListFindNoCase(deleteList,tName) EQ false>
<cftry>
<cfquery datasource="#arguments.datasource#">
delete from #tName#
</cfquery>
<cfset deleteList= ListAppend(deleteList,tName)>
<cfcatch type="database">
<cfcontinue>
</cfcatch>
</cftry>
</cfif>
</cfloop>
</cfloop>
<cfif loopCounter GTE variables.loopLimit>
<cftransaction action="rollback" />
<cfthrow message="Unable to delete data from tables to prep for snapshot reload prior to reaching the loop limit (#loopLimit#), so all deletes were rolled back. The most likely cause is that one or more tables could not be deleted due to constraint logic. But if you don't think that is the case, alter the appropriate cfcatch block in snapshotService.loadSnapshots() to surface the error." />
<cfelse>
<cfset loopCounter= 0>
</cfif>
</cftransaction>
<cfloop condition="ListLen(finishList) NEQ ListLen(arguments.snapshotList) AND loopCounter NEQ variables.loopLimit">
<cfset loopCounter= loopCounter+1>
<cfloop index="f" list="#arguments.snapshotList#" delimiters=",">
<!---Reset table error status to false when processing the next table in the loop--->
<cfset variables.tableHasError= false>
<!---Restart record count--->
<cfset r= 1>
<cfset tName= ListGetAt(f,1,variables.nameDelimiter)>
<cfset fullTableName= arguments.datasource & "_" & tName>
<cfif ListFindNoCase(finishList,tName) EQ false>
<cfif arguments.type EQ "backups">
<cffile action="read" file="#variables.backupsFolder#/#f#" variable="fileContent">
<cfelse>
<cffile action="read" file="#variables.snapshotsFolder#/#f#" variable="fileContent">
</cfif>
<cfset buildStruct= variables.JSONDeserializer.deserializeFromJSON(fileContent,true)>
<cfset colCount= StructCount(buildStruct.data)>
<!---Can build your own database-specific insert routine--->
<cfif StructKeyExists(variables.dbaseLibrary,"insertData")>
<cftry>
<cfset variables.dbaseLibrary.insertData(arguments.datasource,tName,buildStruct,variables.tableMetadata[fullTableName])>
<cfset finishList= ListAppend(finishList,tName)>
<cfcatch type="database">
<cfcontinue>
</cfcatch>
</cftry>
<cfelse>
<!---Build the field name list--->
<cfset var fieldInsertSQL= "">
<cfloop collection="#buildStruct.data#" item="c">
<cfset fieldInsertSQL= ListAppend(fieldInsertSQL,c)>
</cfloop>
<cftry>
<cfloop condition="r LTE buildStruct.rowcount">
<cfloop index="t" from="1" to="#variables.insertThreadLimit#">
<cfif r LTE buildStruct.rowcount>
<!---Thread names must be unique within the entire request--->
<cfset threadName= "#tName#_#n#">
<cfset threadList= ListAppend(threadList,threadName)>
<cfthread name="#threadName#"
threadIndex="#r#"
threadDS="#arguments.datasource#"
threadTName="#tName#"
threadFieldInsertSQL= "#fieldInsertSQL#"
threadBuildStruct="#buildStruct#"
threadColCount= "#colCount#"
threadFullTableName="#fullTableName#">
<cftry>
<cfquery datasource="#threadDS#">
insert into #threadTName# (
#threadFieldInsertSQL#
) VALUES (
<cfset colCounter= 1>
<cfloop index="c" list="#threadFieldInsertSQL#" delimiters=",">
<cfset fieldValue= threadBuildStruct.data[c][threadIndex]>
<!---The determination of the values for the cfqueryparameter statement is database-specific--->
<cfset insertParams= variables.dbaseLibrary.returnInsertParams(variables.tableMetadata[threadFullTableName].columnData[c].dataType,variables.tableMetadata[threadFullTableName].columnData[c].nullable,fieldValue)>
<cfif structKeyExists(insertParams,"scale")>
<cfqueryparam value="#insertParams.value#" cfsqltype="#insertParams.sqlType#" null="#insertParams.isNull#" scale="#insertParams.scale#" />
<cfelse>
<cfqueryparam value="#insertParams.value#" cfsqltype="#insertParams.sqlType#" null="#insertParams.isNull#" />
</cfif>
<cfif colCounter NEQ threadColCount>,</cfif>
<cfset colCounter= colCounter+1>
</cfloop>
)
</cfquery>
<cfcatch type="any">
<cfset errorInfo= StructNew()>
<cfset errorInfo.table_JSONRecordNumber= "#threadTName#_#threadIndex#">
<cfset errorInfo.message= cfcatch.message>
<cfset errorInfo.detail= cfcatch.detail>
<cfset errorInfo._cfcatchData= cfcatch>
<cfset ArrayAppend(variables.threadErrorArray,errorInfo)>
<cfset variables.tableHasError= true>
</cfcatch>
</cftry>
</cfthread>
<cfset r= r+1>
<cfset n= n+1>
</cfif>
</cfloop>
<cfthread action="join" name="#threadList#" />
<cfset threadList= "">
<cfif variables.tableHasError>
<!---Cannot risk a partial reload where some insert threads succeeded: clear table again--->
<cfquery datasource="#arguments.datasource#">
delete from #tName#
</cfquery>
<cfset variables.tableHasError= false>
<cfthrow type="insertThreadError">
</cfif>
</cfloop>
<cfset finishList= ListAppend(finishList,tName)>
<cfcatch type="insertThreadError">
<cfcontinue>
</cfcatch>
</cftry>
</cfif>
</cfif>
</cfloop> <!---table list--->
</cfloop>
<cfif loopCounter GTE variables.loopLimit>
<cfthrow message="Unable to reload snapshot data prior to reaching the loop limit (#loopLimit#). The most likely cause is that one or more snapshots could not be loaded due to constraint logic (related records not being present in the existing data). But if you don't think that is the case, alter the appropriate cfcatch block in snapshotService.loadSnapshots() to surface the error." />
</cfif>
</cfoutput>
<!---Return any errors recorded by the threads--->
<cfreturn variables.threadErrorArray />
</cffunction>
<cffunction name="takeTableSnapshots" access="public" output="false" returntype="void">
<cfargument name="tableList" type="any" required="true" />
<cfargument name="formData" type="struct" required="true" />
<cfargument name="datasource" type="string" required="false" default="#variables.datasource#" />
<cfset var t= 1>
<cfset var qry= "">
<cfset var jsonString= "">
<cfset var snapshotName= "">
<cfset var tstamp= Now()>
<cfloop index="t" from="1" to="#ListLen(arguments.tableList)#">
<cfquery name="qry" datasource="#arguments.datasource#">
select * from #ListGetAt(arguments.tableList,t)#
</cfquery>
<cfset jsonString= variables.JSONSerializer.serializeToJSON(qry,true,true)>
<cfif arguments.formData[ListGetAt(arguments.tableList,t)] NEQ "">
<cfset snapshotName= ListGetAt(arguments.tableList,t) & variables.nameDelimiter & arguments.formData[ListGetAt(arguments.tableList,t)] & variables.nameDelimiter & DateFormat(tstamp,'yyyy-mm-dd') & variables.nameDelimiter & TimeFormat(tstamp,'TT-hh-mm-ss') & ".txt">
<cfelse>
<cfset snapshotName= ListGetAt(arguments.tableList,t) & variables.nameDelimiter & DateFormat(tstamp,'yyyy-mm-dd') & variables.nameDelimiter & TimeFormat(tstamp,'TT-hh-mm-ss') & ".txt">
</cfif>
<cffile action="write" file="#variables.snapshotsFolder#/#snapshotName#" output="#jsonString#">
</cfloop>
</cffunction>
</cfcomponent>