-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdbrow3.cfc
More file actions
2006 lines (1561 loc) · 77.4 KB
/
dbrow3.cfc
File metadata and controls
2006 lines (1561 loc) · 77.4 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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!--- Sample child component extending dbrow:
<cfcomponent extends="dbrow3_mysql"> // choose the correct adapter
<cfscript>
// This is the only required field.
theObject = "User";
// You may supply these fields as necessary. Otherwise init() will use defaults like the
// following
theID = "UserID";
theFieldsToSkip = "";
theTable = "tblUser";
theDatasource = application.datasource;
theNameField = "User_name";
/* RELATIONSHIPS
- Has-many relationships can be defined in the constructor by using
hasMany(). Both one-to-many and many-to-many relationships are supported.
- Belongs-to-many relationships are the same this as has-many, so you can
define them with hasMany().
- Belongs-to-one relationships are currently unsupported.
*/
</cfscript>
</cfcomponent>
--->
<cfcomponent name="dbrow3" hint="represents a database record">
<cfinclude template="mixins/check_remote_method_authorization.cfm">
<!--- Vars supporting logging - leon 9/27/09 --->
<!--- Set to 0 to disable logging. - leon 9/27/09 --->
<cfset dbrow3Logging = 0>
<cfset lastTick = getTickCount()>
<!--- End vars supporting logging - leon 9/27/09 --->
<!--- Flag indicates whether initializeObject() has run - leon 8/31/06 --->
<cfparam name="this.isInitialized" default="0">
<!--- Flag indicates whether init() has run - leon 8/31/06 --->
<cfparam name="this.isInited" default="0">
<!--- Flag indicates whether object has ever been stored in database - leon 3/12/07 --->
<cfparam name="this.isStored" default="0">
<!--- Automatically calling `init()` from the pseudoconstructor
doesn't work - it complains about theObject not being defined. I
think this has something to do with the order in which constructors
are called in child/parent objects. - leon 12/27/07 --->
<!--- This used to have returntype="dbrow3", but that doesn't seem to work
when you have grandchildren in another directory. - leon 7/25/07 --->
<cffunction name="init" access="public"
hint="This is the new 'constructor' method. It's called automatically by load() and new()." output="yes">
<cfif not(isdefined('theObject'))>
<cfthrow message="dbrow3 requires 'theObject' to be set in the constructor">
</cfif>
<cfif not(isdefined('theDatasource') or structKeyExists(application, 'datasource'))>
<cfthrow message="dbrow3 requires 'theDatasource' to be set in the constructor OR that 'application.datasource' exists">
</cfif>
<cfif not(isdefined('theObjectMap') or structKeyExists(application, 'objectMap'))>
<cfthrow message="dbrow3 requires 'theObjectMap' to be set in the constructor OR that 'application.objectMap' exists">
</cfif>
<cfparam name="theID" default="#theObject#ID">
<cfparam name="theFieldsToSkip" default="">
<cfparam name="theTable" default="tbl#theObject#">
<cfparam name="theDatasource" default="#application.datasource#">
<cfparam name="binaryFieldList" default="">
<cfparam name="theNameField" default="#theObject#_name">
<!--- Define the default immutable name field.
Type objects need to define the value they represent - dave 12/2/08 --->
<cfparam name="theImmutableNameField" default="name_immutable">
<cfparam name="theImmutableNameFieldValue" default="">
<!--- The type id field is the id field of this object that refers to the the
foreign key id that defines this objects type. The type obj is an actual
instance of this object - dave 12/2/08 --->
<cfparam name="typeIDField" default="">
<cfparam name="this.typeObj" default="">
<cfparam name="hiddenFieldList" default="">
<cfscript>
/* Define cache timespans. Do not depend on request.timeLong
to be defined. - Jared 2012-02-16 */
this.timeLong = StructKeyExists(request, "timeLong") ? request.timeLong : cacheTimeoutDefault();
this.timeNone = StructKeyExists(request, "timeNone") ? request.timeNone : CreateTimeSpan(0,0,0,0);
</cfscript>
<cfset logIt('init() called on dbrow object #theNameField#. Generating classUniqueID...')>
<!--- classUniqueID holds an alphanumeric string that uniquely identifies this CFC. We use it
in the cache manager method calls to prevent name collisions. - leon 4/5/07 --->
<cfset this.classUniqueID = hash(getMetadata(this).path)>
<cfset logIt('Calling initializeObject()...')>
<cfset initializeObject(theID, theFieldsToSkip, theTable, theObject, theDatasource, theImmutableNameField, theImmutableNameFieldValue, typeIDField, binaryFieldList)>
<cfset this.theNameField = theNameField>
<cfset this.hiddenFieldList = hiddenFieldList>
<cfif isdefined('theObjectMap')>
<cfset this.objectMap = theObjectMap>
<cfelse>
<cfset this.objectMap = application.objectMap>
</cfif>
<!--- Create stMany if it doesn't exist yet. - leon 4/21/08 --->
<cfif not(structKeyExists(this, 'stMany'))>
<!--- Stores information about this-to-many relationships - leon 4/21/08 --->
<cfset this.stMany = structNew()>
</cfif>
<!--- Create stCustomValidation if it doesn't exist yet. - leon 4/21/08 --->
<cfif not(structKeyExists(this, 'stCustomValidation'))>
<!--- Stores custom validation rules - leon 4/21/08 --->
<cfset this.stCustomValidation = structNew()>
<!--- This struct has property names for keys.
The value is always an array of custom rules for that property.
Custom rules are structs with keys (regex, fn, errorText).
- leon 12/11/08 --->
</cfif>
<cfset this.isInited = 1>
<cfreturn this>
</cffunction> <!--- init --->
<cfscript>
/* There are a dozen methods in dbrow3.cfc which are more
concerned with rendering a record (eg. in html) than they are
with core responsibilities like persistence. In dbrow 3.2,
these methods will be delegated to a dbrow_renderer without
changing the behavior of dbrow or its interface (method
signatures). In future versions of dbrow, these methods may be
deprecated, or the delegate (dbrow_renderer) could become a
decorator, or these methods could be removed from the dbrow api
entirely. - Jared 4/28/12 */
public void function initializeRenderer() {
if (NOT StructKeyExists(this, "renderer")) {
this.renderer = CreateObject('component', 'dbrow_renderer').init(this);
}
}
</cfscript>
<cffunction name="initializeObject" returnType="boolean" access="package"
hint="This initializes the object's properties and metadata. It is called by init(), but may
also be manually run in the constructor area of the child component (for legacy apps).">
<cfargument name="theID" type="string" required="yes">
<cfargument name="theFieldsToSkip" type="string" required="yes">
<cfargument name="theTable" type="string" required="yes">
<cfargument name="theObject" type="string" required="yes">
<cfargument name="theDatasource" type="string" required="yes">
<cfargument name="theImmutableNameField" type="string" required="yes">
<cfargument name="theImmutableNameFieldValue" type="string" required="no" default="">
<cfargument name="typeIDField" type="string" required="no" default="">
<cfargument name="binaryFieldList" type="string" required="no" default="">
<cfparam name="this.isInitialized" default="0">
<cfset logIt('Running initializeObject()')>
<!--- This is a workaround for the situation when you have a grandchild object of dbrow's. The
child object's constructor runs first, thus initializing the object with what may not
be the correct information. So, when the grandchild's constructor runs, we've got to clear out
the child's information before we can set up the grandchild's. - leon 6/30/05 --->
<cfif this.isInitialized>
<cfset logIt('Clearing dbrow child info so we can use grandchild''s instead')>
<cfset structclear(this)>
</cfif>
<cfset this.theID = arguments.theID>
<cfset this.theFieldsToSkip = arguments.theFieldsToSkip>
<cfset this.theTable = arguments.theTable>
<cfset this.theObject = arguments.theObject>
<cfset this.datasource = arguments.theDatasource>
<cfset this.theImmutableNameField = arguments.theImmutableNameField>
<cfset this.theImmutableNameFieldValue = arguments.theImmutableNameFieldValue>
<cfset this.typeIDField = arguments.typeIDField >
<cfset this.binaryFieldList = arguments.binaryFieldList>
<cfif not(find("cfcexplorer.cfc", cgi.script_name))>
<!--- Generate metadata structures - leon 2/4/06 --->
<cfset logIt('Setting stColMetaData')>
<cfif not(structKeyExists(this, 'stColMetaData'))>
<cfset this.stColMetaData = getCachedColumnMetaData()>
</cfif>
<cfset logIt('Setting stFKMetaData')>
<cfif not(structKeyExists(this, 'stFKMetaData'))>
<cfset this.stFKMetaData = getCachedForeignKeyMetaData()>
</cfif>
<!--- Done generating metadata - leon 2/4/06 --->
<cfset logIt('Populating stOrigState')>
<cfset this.stOrigState = structNew()>
<cfloop list="#structKeyList(this.stColMetaData)#" index="local.i">
<cfset structInsert(this, i, '')>
<cfset structInsert(this.stOrigState, i, '')>
</cfloop>
<cfset variables.properties = StructSort(this.stColMetaData, 'numeric', 'asc', 'sortorder')>
<cfset this.isInitialized = 1>
<cfset this.isStored = 0>
<cfscript>
/* Set up labels for properties - leon 2/4/06 */
logIt('Populating stLabel');
if ( NOT StructKeyExists(this, 'stLabel') ) {
this.stLabel = StructNew();
}
for( var i in variables.properties ){
if (NOT StructKeyExists(this.stLabel, i) ) {
this.stLabel[i] = REReplace(Replace(i, '_', ' ', 'all'), '((^| ))([a-zA-Z])', '\1\u\3', 'all');
}
}
/* Done setting up labels - leon 2/4/06 */
</cfscript>
</cfif>
<cfreturn true>
</cffunction> <!--- initializeObject --->
<cffunction name="addValidation" returntype="void" output="no" access="public">
<cfargument name="propertyName" type="string" required="yes">
<cfargument name="regex" type="string" required="no" default=""
hint="Regex checks will work client-side (if using formvalidation.js) and server-side.">
<cfargument name="fn" type="any" required="no" default=""
hint="A CF function name or a closure that takes in a property value and
returns a boolean value indicating that the value is OK or not. These
obviously won't work client-side.">
<cfargument name="errorText" type="string" required="no"
hint="Gets prepended with property label.">
<cfscript>
/* Check input - leon 12/11/08 */
if (!(structKeyExists(arguments, 'regex') or structKeyExists(arguments, 'fn'))) {
Throw(message="addValidation() requires either a regex or a fn");
}
if (arguments.KeyExists('function')) {
Throw(message="The `function` argument to `addValidation()` has been renamed to `fn`");
}
var newRule = structNew();
newRule.regex = arguments.regex;
newRule.fn = arguments.fn;
newRule.errorText = arguments.errorText;
if (!structKeyExists(this, 'stCustomValidation')) {
this.stCustomValidation = structNew();
}
if (!structKeyExists(this.stCustomValidation, propertyName)) {
this.stCustomValidation[propertyName] = arrayNew(1);
}
arrayAppend(this.stCustomValidation[propertyName], newRule);
</cfscript>
</cffunction> <!--- addValidation --->
<cffunction name="afterDelete" returnType="void" output="false" access="package"
hint="A stub function that's called at the end of a delete() process">
</cffunction> <!--- afterDelete --->
<cffunction name="afterLoad" returnType="void" output="false" access="package"
hint="A stub function that's called at the end of a load() process">
</cffunction> <!--- afterLoad --->
<cffunction name="afterStore" returnType="void" output="false" access="package"
hint="Called at the end of a store() process. Children should call super.afterStore()">
<cfset var v = structNew()>
<!--- Store data for this-to-many relationships - leon 4/22/08 --->
<cfloop collection="#this.stMany#" item="v.relName">
<cfset v.stRel = this.stMany[v.relName]>
<cfif len(v.stRel.myID)>
<cfset v.myID = v.stRel.myID>
<cfelse>
<cfset v.myID = this.theID>
</cfif>
<!--- Only need to store dirty records. - leon 4/22/08 --->
<cfif v.stRel.dirty>
<cfset v.objForeignObj = createObject('component', '#application.objectMap#.#v.stRel.objectType#').new()>
<cfif len(v.stRel.linkTable)>
<!--- Store related IDs in linking table - leon 4/21/08 --->
<!--- Determine relevant column names in linking table - Jared 5/9/08 --->
<cfset v.stLTInfo = getLinkingTableInfo(v.relName)>
<cfset v.linksToMyID = v.stLTInfo.linksToMyID>
<cfset v.linksToForeignID = v.stLTInfo.linksToForeignID>
<!--- First clear out removed links - leon 4/22/08 --->
<cfquery datasource="#this.datasource#">
delete from #v.stRel.linkTable#
where #v.linksToMyID# = <cfqueryparam value="#this[v.myID]#" cfsqltype="cf_sql_#this.stColMetaData[v.myID].datatype#">
<cfif len(v.stRel.idList)>
and #v.linksToForeignID# not in (#v.stRel.idList#)
</cfif>
<cfif len(v.stRel.linkTableFilterField)>
and #v.stRel.linkTableFilterField# = <cfqueryparam value="#v.stRel.linkTableFilterValue#" cfsqltype="cf_sql_#v.stRel.linkTableFilterSqlType#">
</cfif>
</cfquery>
<!--- Then add missing links, if any - leon 4/22/08 --->
<cfif len(v.stRel.idList)>
<cfquery datasource="#this.datasource#">
insert into #v.stRel.linkTable# (#v.linksToMyID#, #v.linksToForeignID#
<cfif len(v.stRel.linkTableFilterField)>
, #v.stRel.linkTableFilterField#
</cfif>
)
select <cfqueryparam value="#this[v.myID]#" cfsqltype="cf_sql_#this.stColMetaData[v.myID].datatype#">,
#v.objForeignObj.theID#
<cfif len(v.stRel.linkTableFilterField)>
, <cfqueryparam value="#v.stRel.linkTableFilterValue#" cfsqltype="cf_sql_#v.stRel.linkTableFilterSqlType#">
</cfif>
from #v.objForeignObj.theTable# f
where #v.objForeignObj.theID# in (#v.stRel.idList#)
and not(exists(
select #v.linksToMyID# from #v.stRel.linkTable# l
where l.#v.linksToMyID# = <cfqueryparam value="#this[v.myID]#" cfsqltype="cf_sql_#this.stColMetaData[v.myID].datatype#">
and l.#v.linksToForeignID# = f.#v.objForeignObj.theID#
))
</cfquery>
</cfif>
<cfelse>
<!--- Update items with related IDs in foreign table - leon 4/21/08 --->
<!--- This is a little bit weird because we could be setting the foreign key field
in the other table to null if we're removing entities from the relationship. This
will certainly fail in some relationships where the foreign key field is not nullable.
Under these circumstances, the user interface should only allow reassignment, not
removal of an entity from the relationship. - leon 4/22/08 --->
<!--- Load up array of items - leon 4/22/08 --->
<cfset v.objForeignSet = createObject('component', '#application.objectMap#.#v.stRel.objectType#_set')>
<!--- Find the foreign key field in the other table - leon 4/22/08 --->
<cfset v.foreignKeyCol = v.objForeignObj.getForeignKeyCol(this.theID, this.theTable)>
<!--- Remove entities from the relationship where appropriate - leon 4/22/08 --->
<cfset v.rsForeign = v.objForeignSet.getAll(
filterField = v.foreignKeyCol,
filterValue = this[this.theID]
)>
<!--- XXX - maybe we shouldn't convert them all to objects. Maybe we should test
first and then convert where necessary. - leon 4/22/08 --->
<cfset v.arForeign = v.objForeignObj.queryToArray(v.rsForeign)>
<cfloop from="1" to="#arrayLen(v.arForeign)#" index="v.i">
<cfset v.thisObj = v.arForeign[v.i]>
<cfif not(listFindNoCase(v.stRel.idList, v.thisObj[v.thisObj.theID]))>
<cfset v.thisObj[v.foreignKeyCol] = "">
<cfset v.thisObj.store()>
</cfif>
</cfloop>
<!--- Done removing entities - leon 4/22/08 --->
<!--- Add entities to the relationship (if any) - leon 4/22/08 --->
<cfif len(v.stRel.idList)>
<cfset v.rsForeign = v.objForeignSet.getAll(
filterField = v.objForeignObj.theID,
filterValue = v.stRel.idList
)>
<!--- XXX - maybe we shouldn't convert them all to objects. Maybe we should test
first and then convert where necessary. - leon 4/22/08 --->
<cfset v.arForeign = v.objForeignObj.queryToArray(v.rsForeign)>
<cfloop from="1" to="#arrayLen(v.arForeign)#" index="v.i">
<cfset v.thisObj = v.arForeign[v.i]>
<cfif v.thisObj[v.foreignKeyCol] neq this[this.theID]>
<cfset v.thisObj[v.foreignKeyCol] = this[this.theID]>
<cfset v.thisObj.store()>
</cfif>
</cfloop>
</cfif> <!--- len(v.stRel.idList) --->
<!--- Done adding entities - leon 4/22/08 --->
</cfif>
<!--- Now that it's stored, it's no longer dirty. - leon 4/22/08 --->
<cfset v.stRel.dirty = 0>
<!--- It is "loaded", though. - leon 4/22/08 --->
<cfset v.stRel.loaded = 1>
</cfif> <!--- v.stRel.dirty --->
</cfloop>
</cffunction> <!--- afterStore --->
<cffunction name="beforeDelete" returnType="void" output="false" access="package"
hint="A stub function that's called at the beginning of a delete() process">
</cffunction> <!--- beforeDelete --->
<cffunction name="beforeLoad" returnType="void" output="false" access="package"
hint="A stub function that's called at the beginning of a load() process">
</cffunction> <!--- beforeLoad --->
<cffunction name="beforeStore" returnType="void" output="false" access="package"
hint="A stub function that's called at the beginning of a store() process">
</cffunction> <!--- beforeStore --->
<cffunction name="cacheTimeout" returntype="date" output="no" access="public">
<cfargument name="useCache" type="boolean" required="yes">
<cfreturn IIF(arguments.useCache, this.timeLong, this.timeNone)>
</cffunction> <!--- cacheTimeout --->
<!--- Railo/Lucee want "timestamp" as the returntype. ACF wants "date". Just make it "any"
so it works with both. --->
<cffunction name="cacheTimeoutDefault" returntype="any" output="no" access="public"
hint="The default cache timeout. Only used if request.timeLong is
undefined. If a time span other than two hours is required, override
this function in the child class.">
<cfreturn CreateTimeSpan(0,2,0,0)>
</cffunction> <!--- cacheTimeoutDefault --->
<cffunction name="caseSensitiveComparisons" returntype="boolean" output="no" access="public"
hint="Specifies whether the RDBMS uses case-sensitive comparisons for IN, LIKE, and =.
If an adapter returns a true value, dbrow will do extra work to make comparisons
case-insensitive.">
<!--- Most RDBMSs use case-insensitive comparisons, so make that the default here. --->
<cfreturn false>
</cffunction>
<cfscript>
/* Clears this object's properties so that it can be safely
load()ed again with a new ID */
public void function clear(){
if ( NOT this.isInited ) {
init();
}
for (var i in variables.properties) {
StructUpdate(this, i, '');
StructUpdate(this.stOrigState, i, '');
}
this.isStored = 0;
clearThisToManyData();
}
</cfscript>
<cffunction name="clearThisToManyData" returntype="void" output="no" access="public">
<cfset var v = structNew()>
<!--- Clear out this-to-many relationships - leon 4/22/08 --->
<cfloop collection="#this.stMany#" item="v.i">
<cfset this.stMany[v.i].idList = "">
<cfset this.stMany[v.i].loaded = 0>
<cfset this.stMany[v.i].dirty = 0>
</cfloop>
</cffunction> <!--- clearThisToManyData --->
<cffunction name="delete" returnType="boolean" access="remote"
hint="Delete this object's data from the database">
<cfargument name="ID" required="no">
<cfargument name="goto" type="string" required="no" default="#replace(cgi.script_name, '.cfc', '_set.cfc')#?method=list">
<cfargument name="skipRemoteAuthorization" required="no" default="no" hint="When, eg. authorization has already been performed by the controller">
<!--- Check to make sure the user has permissions --->
<cfif not skipRemoteAuthorization and StructKeyExists( url, "method" ) and url.method eq "delete">
<cfset checkRemoteMethodAuthorization() >
</cfif>
<cfif structKeyExists(arguments, 'ID')>
<cfset load(arguments.ID)>
</cfif>
<cfset beforeDelete()>
<cfset IDToDelete = this[this.theID]>
<!--- Tombstoning - Jared 4/23/08 --->
<cfif usesTombstoning()>
<!--- RecordAlreadyDeletedException --->
<cfif isDeleted()>
<cfthrow type="com.singlebrook.dbrow3.RecordAlreadyDeletedException"
message="Attempt to delete() a tombstoned record">
</cfif>
<!--- Set deleted in database ... --->
<cfquery name="delete#theObject#" datasource="#this.datasource#">
update #this.theTable#
set deleted = <cfqueryparam value="1" cfsqltype="cf_sql_#this.stColMetaData['deleted'].datatype#">
where #this.theID# = <cfqueryparam value="#IDToDelete#" cfsqltype="cf_sql_#this.stColMetaData[this.theID].datatype#">
</cfquery>
<!--- ... and in memory --->
<cfset this.deleted = true>
<!--- Normal deletion --->
<cfelse>
<cfquery name="delete#theObject#" datasource="#this.datasource#">
delete
from #this.theTable#
where #this.theID# = <cfqueryparam value="#IDToDelete#" cfsqltype="cf_sql_#this.stColMetaData[this.theID].datatype#">
</cfquery>
</cfif>
<cfset afterDelete()>
<cfif structKeyExists(arguments, 'ID')>
<cflocation url="#goto#" addToken="no">
</cfif>
<cfreturn true>
</cffunction> <!--- delete --->
<cffunction name="drawPropertyValue" returnType="string" output="no" access="public">
<cfargument name="propertyname" type="string" required="yes">
<cfset initializeRenderer()>
<cfreturn this.renderer.drawPropertyValue(arguments.propertyname)>
</cffunction>
<cffunction name="drawForm" returnType="string" output="no" access="public">
<cfset initializeRenderer()>
<cfreturn this.renderer.drawForm(argumentCollection = arguments)>
</cffunction>
<cffunction name="drawFormEnd" returnType="string" output="no" access="public">
<cfset initializeRenderer()>
<cfreturn this.renderer.drawFormEnd()>
</cffunction>
<cffunction name="drawFormField" returnType="string" output="no" access="public">
<cfset initializeRenderer()>
<cfreturn this.renderer.drawFormField(argumentCollection = arguments)>
</cffunction>
<cffunction name="drawFormErrorSummary" returnType="string" output="no" access="public">
<cfset initializeRenderer()>
<cfreturn this.renderer.drawFormErrorSummary(argumentCollection = arguments)>
</cffunction>
<cffunction name="drawFormStart" returnType="string" output="no" access="public">
<cfset initializeRenderer()>
<cfreturn this.renderer.drawFormStart(argumentCollection = arguments)>
</cffunction>
<cffunction name="drawStandardFormField" returnType="string" output="no" access="public">
<cfset initializeRenderer()>
<cfreturn this.renderer.drawStandardFormField(argumentCollection = arguments)>
</cffunction>
<cffunction name="drawErrorField" returnType="string" output="no" access="public">
<cfset initializeRenderer()>
<cfreturn this.renderer.drawErrorField(argumentCollection = arguments)>
</cffunction>
<cffunction name="edit" returnType="void" output="yes" access="remote">
<cfset initializeRenderer()>
#this.renderer.edit(argumentCollection = arguments)#
</cffunction> <!--- edit --->
<cffunction name="elapsed" returntype="numeric" output="no">
<cfset var elapsedTime = getTickCount() - lastTick>
<cfset lastTick = getTickCount()>
<!--- Convert to seconds - leon 9/27/09 --->
<cfset elapsedTime = numberFormat(elapsedTime / 1000, '9999.000')>
<!--- Format for display by prepending spaces to get a fixed width. - leon 9/27/09 --->
<cfset elapsedTime = repeatString(' ', 10 - len(elapsedTime)) & elapsedTime>
<cfreturn elapsedTime>
</cffunction> <!--- elapsed --->
<cffunction name="getCachedColumnMetaData" returnType="struct" output="no"
hint="Returns the column meta data for this object. Uses the cached version if possible.">
<cfset var stResults = "">
<cftry>
<cfset stResults = application.dbrow3cache.getColumnMetaData(this.classUniqueID)>
<cfif stResults.hit>
<cfset stColMetaData = stResults.stMetaData>
<cfelse>
<!--- dbrow3cache miss. Ask the rdbms adapter (eg. dbrow3_pgsql)
for the metadata. Be aware that the adapter probably uses CF query
caching in its getColumnMetaData() method. - Jared 2012-02-16 --->
<cfset stColMetaData = getColumnMetaData()>
<cfset application.dbrow3cache.setColumnMetaData(this.classUniqueID, stColMetaData)>
</cfif>
<cfcatch type="any">
<cfset stColMetaData = getColumnMetaData()>
</cfcatch>
</cftry>
<cfreturn stColMetaData>
</cffunction> <!--- getCachedColumnMetaData --->
<cffunction name="getCachedForeignKeyMetaData" returnType="struct" output="no"
hint="Returns the column meta data for this object. Uses the cached version if possible.">
<cfset var stResults = "">
<cftry>
<cfset stResults = application.dbrow3cache.getForeignKeyMetaData(this.classUniqueID)>
<cfif stResults.hit>
<cfset stFKMetaData = stResults.stMetaData>
<cfelse>
<cfset stFKMetaData = getForeignKeyMetaData()>
<cfset application.dbrow3cache.setForeignKeyMetaData(this.classUniqueID, stFKMetaData)>
</cfif>
<cfcatch type="any">
<cfset stFKMetaData = getForeignKeyMetaData()>
</cfcatch>
</cftry>
<cfreturn stFKMetaData>
</cffunction> <!--- getCachedForeignKeyMetaData --->
<cffunction name="getColDataType" returnType="string" output="no" access="public">
<cfargument name="col" type="string" required="yes">
<cfreturn stColMetaData[arguments.col].dataType>
</cffunction> <!--- getColDataType --->
<cffunction name="getChanges" returnType="struct" output="no"
hint="Returns a struct of arrays, where each inner struct is an old/new value pair for a property">
<cfset stChanges = request.structCompare(this.stOrigState, this)>
<cfreturn stChanges>
</cffunction> <!--- getChanges --->
<cffunction name="getDefaultTabIndex" returnType="numeric" output="no" access="public">
<cfset initializeRenderer()>
<cfreturn this.renderer.getDefaultTabIndex()>
</cffunction>
<cffunction name="getError" returntype="struct" output="no" access="public"
hint="Pulls an error for a particular field out of an error array. Error is a struct with keys (propertyName, propertyLabel, errorText).
Struct values will be empty if there was no error.">
<cfargument name="arErrors" type="array" required="yes" hint="Array of validation errors. See getErrorArray().">
<cfargument name="propertyName" type="string" required="yes" hint="The property you're checking.">
<cfset var v = structNew()>
<cfloop from="1" to="#arrayLen(arErrors)#" index="v.i">
<cfif arErrors[v.i].propertyName eq arguments.propertyName>
<cfreturn arErrors[v.i]>
</cfif>
</cfloop>
<cfreturn newError('', '', '')>
</cffunction> <!--- getError --->
<cffunction name="getErrorArray" returntype="array" output="no" access="public"
hint="Returns an array of errors. Each error is a hash with keys (propertyName, propertyLabel, errorText).
We return an array in order to preserve the ordering of fields.">
<cfset var v = structNew()>
<cfset var stMD = "">
<cfset v.arErrors = arrayNew(1)>
<cfloop array="#variables.properties#" index="v.thisProp">
<cfif v.thisProp neq this.theID and not(listFindNoCase(this.theFieldsToSkip, v.thisProp))>
<cfset stMD = this.stColMetaData[v.thisProp]>
<!--- Check not null constraint - leon 12/9/08 --->
<cfif stMD.notNull and NOT isBinary(this[v.thisProp])
and IsSimpleValue(this[v.thisProp]) and this[v.thisProp] eq "">
<cfset arrayAppend(v.arErrors, newError(v.thisProp, getLabel(v.thisProp), 'cannot be blank'))>
</cfif>
<!--- Check maximum length - leon 12/9/08 --->
<cfif val(stMD.maxLen) and (len(this[v.thisProp]) gt stMD.maxLen)>
<cfset arrayAppend(v.arErrors, newError(v.thisProp, getLabel(v.thisProp), 'cannot contain more than #stMD.maxLen# characters'))>
</cfif>
<!--- These checks don't get run on blank values - leon 12/9/08 --->
<cfif NOT IsSimpleValue(this[v.thisProp]) OR len(this[v.thisProp])>
<!--- Check datatype - leon 12/11/08 --->
<cfswitch expression="#stMD.datatype#">
<cfcase value="float,decimal" delimiters=",">
<cfif not(isNumeric(this[v.thisProp]))>
<cfset arrayAppend(v.arErrors, newError(v.thisProp, getLabel(v.thisProp), 'must be a number'))>
</cfif>
</cfcase>
<cfcase value="integer,bigint" delimiters=",">
<cfif not REFind('^-?\d+$', this[v.thisProp])>
<cfset arrayAppend(v.arErrors, newError(v.thisProp, getLabel(v.thisProp), 'must be an integer'))>
</cfif>
</cfcase>
<cfcase value="date,timestamp" delimiters=",">
<cfif not(isDate(this[v.thisProp]))>
<cfset arrayAppend(v.arErrors, newError(v.thisProp, getLabel(v.thisProp), 'must be a valid #stMD.datatype#'))>
</cfif>
</cfcase>
<cfcase value="time">
<cfif not(REFindNoCase('^((([0]?[1-9]|1[0-2])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?( )?(AM|am|aM|Am|PM|pm|pM|Pm))|(([0]?[0-9]|1[0-9]|2[0-3])(:|\.)[0-5][0-9]((:|\.)[0-5][0-9])?))$', this[v.thisProp]))>
<cfset arrayAppend(v.arErrors, newError(v.thisProp, getLabel(v.thisProp), 'must be a valid time'))>
</cfif>
</cfcase>
<cfcase value="varchar,nvarchar" delimiters=",">
<cfif v.thisProp EQ "email" AND NOT IsValid('email', this[v.thisProp])>
<cfset arrayAppend(v.arErrors, newError(v.thisProp, getLabel(v.thisProp), 'must be a valid email address'))>
</cfif>
</cfcase>
</cfswitch>
<!--- Done checking datatype - leon 12/11/08 --->
<!--- Check custom rules - leon 12/11/08 --->
<cfif structKeyExists(this.stCustomValidation, v.thisProp)>
<cfset v.arRules = this.stCustomValidation[v.thisProp]>
<cfloop from="1" to="#arrayLen(v.arRules)#" index="v.i">
<cfset v.stRule = v.arRules[v.i]>
<cfif len(v.stRule.regex)>
<cfif not(REFind(v.stRule.regex, this[v.thisProp]))>
<cfset arrayAppend(v.arErrors, newError(v.thisProp, getLabel(v.thisProp), v.stRule.errorText))>
</cfif>
</cfif>
<cfscript>
var funcRef = v.stRule.fn;
var passed = true;
if (isCustomFunction(funcRef) or isClosure(funcRef)) {
passed = funcRef(this[v.thisProp]);
} else if (Len(funcRef)) {
passed = Evaluate('#funcRef#("#this[v.thisProp].replace('"', '""', 'all')#")');
}
if (!passed) {
ArrayAppend(v.arErrors, newError(v.thisProp, getLabel(v.thisProp), v.stRule.errorText));
}
</cfscript>
</cfloop>
</cfif>
</cfif> <!--- len(this[v.thisProp]) --->
</cfif> <!--- v.thisProp neq this.theID and not(listFindNoCase(theFieldsToSkip, v.thisProp)) --->
</cfloop> <!--- variables.properties --->
<cfreturn v.arErrors>
</cffunction> <!--- getErrorArray --->
<cffunction name="getErrorStruct" returntype="struct" output="no" access="public"
hint="Returns a struct of validation errors with the property names as keys.
Each error is a struct with keys (propertyName, propertyLabel, errorText).
Currently limited to one error per property.">
<cfset var arErrors = getErrorArray()>
<cfset var stErrors = structNew()>
<cfset var i = "">
<cfloop from="1" to="#arrayLen(arErrors)#" index="local.i">
<cfif not(structKeyExists(stErrors, arErrors[i].propertyName))>
<cfset structInsert(stErrors, arErrors[i].propertyName, arErrors[i])>
</cfif>
</cfloop>
<cfreturn stErrors>
</cffunction> <!--- getErrorStruct --->
<cffunction name="getForeignKeyCol" returntype="string" output="no" access="public">
<cfargument name="refersToCol" type="string" required="yes"
hint="The name of the column the foreign key col refers to">
<cfargument name="refersToTable" type="string" required="yes"
hint="The name of the table containing the foreign key">
<cfset var v = structNew()>
<cfloop collection="#this.stFKMetaData#" item="v.i">
<cfset v.stFK = this.stFKMetaData[v.i]>
<cfif v.stFK.foreignColumn eq refersToCol and v.stFK.foreignTable eq refersToTable>
<cfreturn v.i>
</cfif>
</cfloop>
<cfthrow message="Could not find foreign key col referring to #refersToTable# (#refersToCol#)"
detail="Is this a many-to-many relationship? Perhaps you forgot to specify linkTable in your call to hasMany()">
</cffunction> <!--- getForeignKeyCol --->
<cffunction name="getLabel" returntype="string" access="public" output="no">
<cfargument name="propertyname" type="string" required="yes">
<cfset initializeRenderer()>
<cfreturn this.renderer.getLabel(arguments.propertyname)>
</cffunction> <!--- getLabel --->
<cffunction name="getLinkingTableInfo" returntype="struct" access="public" output="no"
hint="Returns a structure with information about the important columns in a linking table">
<cfargument name="relName" type="string" required="yes">
<cfset var v = StructNew()>
<cfset v.stInfo = StructNew()>
<!--- Has the given relationship been defined? - Jared 5/9/08 --->
<cfif not StructKeyExists(this.stMany, arguments.relName)>
<cfthrow message="There is no relationship with the name '#arguments.relName#'. Define relationships with hasMany()"
detail="#StructKeyList(this.stMany)#">
</cfif>
<!--- The relationship - Jared 5/9/08 --->
<cfset v.stRel = this.stMany[arguments.relName]>
<!--- Either the relevant columns already been defined - Jared 5/9/08 --->
<cfif len(v.stRel.linksToMyID)>
<cfset v.stInfo.linksToMyID = v.stRel.linksToMyID>
<cfelse>
<!--- First figure out which column in linking table links to my ID field. - leon 4/21/08 --->
<cfset v.stInfo.linksToMyID = lookupColLinkingTo(v.stRel.linkTable, this.theID, this.theTable)>
</cfif>
<cfif len(v.stRel.linksToForeignID)>
<cfset v.stInfo.linksToForeignID = v.stRel.linksToForeignID>
<cfelse>
<!--- Then figure out which column in linking table links to foreign object's ID field. - leon 4/21/08 --->
<cfset v.objForeignObj = createObject('component', '#application.objectMap#.#v.stRel.objectType#').new()>
<cfset v.stArgs = StructNew()>
<cfset v.stArgs.linkTable = v.stRel.linkTable>
<cfset v.stArgs.linksToColumn = v.objForeignObj.theID>
<cfset v.stArgs.linksToTable = v.objForeignObj.theTable>
<cfset v.stInfo.linksToForeignID = lookupColLinkingTo(argumentCollection = v.stArgs)>
</cfif>
<cfreturn v.stInfo>
</cffunction> <!--- getLinkingTableInfo --->
<cffunction name="getIDColumn" returntype="string" output="no" access="public">
<cfreturn this.theID>
</cffunction> <!--- getIDColumn --->
<cffunction name="getManyRelatedArray" returntype="array" output="no" access="public">
<cfargument name="relName" type="variableName" required="yes"
hint="The name of the relationship given in the initial hasMany()">
<cfargument name="filterField" type="string" required="no">
<cfargument name="filterValue" type="string" required="no">
<cfargument name="filterSet" type="struct" required="no">
<!--- Same arguments as getManyRelatedRS() - leon 5/8/08 --->
<cfset var v = structNew()>
<cfset v.stRel = this.stMany[relName]>
<cfset v.objForeign = createObject('component', '#application.objectMap#.#v.stRel.objectType#').new()>
<cfset v.rsRelated = getManyRelatedRS(argumentCollection = arguments)>
<cfreturn v.objForeign.queryToArray(v.rsRelated)>
</cffunction> <!--- getManyRelatedArray --->
<cffunction name="getManyRelatedIDs" returntype="string" output="no" access="public"
hint="Returns a list of IDs of the items in the named relationship">
<cfargument name="relName" type="variableName" required="yes"
hint="The name of the relationship given in the initial hasMany()">
<cfargument name="reload" type="boolean" required="no" default="false"
hint="Reload the IDs even if they are loaded or dirty">
<cfargument name="bUseCache" type="boolean" required="no" default="no">
<cfset var v = structNew()>
<cfif not(structKeyExists(this.stMany, arguments.relName))>
<cfthrow message="#theObject#.stMany has no information about the relationship #relName#">
</cfif>
<cfset v.cacheTime = cacheTimeout(arguments.bUseCache)>
<cfset v.stRel = this.stMany[arguments.relName]>
<!--- If reloading but the relation is dirty, throw an error - dave 5/19/08 --->
<cfif v.stRel.dirty and arguments.reload >
<cfthrow message="Attempting to reload relation when the relation is already modified">
</cfif>
<!--- Don't load links from the database if they've already been loaded or have been modified
since the last store(), unless reloading. - leon 4/22/08 --->
<cfif not(v.stRel.loaded or v.stRel.dirty) or arguments.reload >
<!--- Links have not been loaded yet. Load 'em up. - leon 4/21/08 --->
<cfif len(v.stRel.myID)>
<cfset v.myID = v.stRel.myID>
<cfelse>
<cfset v.myID = this.theID>
</cfif>
<cfset v.objForeignObj = createObject('component', '#application.objectMap#.#v.stRel.objectType#').new()>
<cfif this.isStored>
<cfif not(len(this[v.myID]))>
<cfthrow message="Can't call getManyRelatedIDs on an object that is stored but has no data in the relavant ID field (#v.myID#). If the ID is the primary key, maybe you should add getID=1 when you store().">
</cfif>
<cfif len(v.stRel.linkTable)>
<!--- Load related IDs from linking table - leon 4/21/08 --->
<!--- Determine relevant column names in linking table - Jared 5/9/08 --->
<cfset v.stLTInfo = getLinkingTableInfo(arguments.relName)>
<cfset v.linksToMyID = v.stLTInfo.linksToMyID>
<cfset v.linksToForeignID = v.stLTInfo.linksToForeignID>
<cfquery name="v.rsForeign" datasource="#this.datasource#" cachedwithin="#v.cacheTime#">
select #v.linksToForeignID# as ID
from #v.stRel.linkTable#
where #v.linksToMyID# = <cfqueryparam value="#this[v.myID]#" cfsqltype="cf_sql_#this.stColMetaData[v.myID].datatype#">
<cfif len(v.stRel.linkTableFilterField)>
and #v.stRel.linkTableFilterField# = <cfqueryparam value="#v.stRel.linkTableFilterValue#" cfsqltype="cf_sql_#v.stRel.linkTableFilterSqlType#">
</cfif>
</cfquery>
<cfset v.idList = valueList(v.rsForeign.ID)>
<cfelse>
<!--- Load related IDs from foreign table - leon 4/21/08 --->
<cfset v.objForeignSet = createObject('component', '#application.objectMap#.#v.stRel.objectType#_set')>
<!--- Identify the foreign key column --->
<cfif Len(v.stRel.linksToMyID)>
<cfset v.fkField = v.stRel.linksToMyID>
<cfelse>
<cfset v.fkField = v.objForeignObj.getForeignKeyCol(v.myID, this.theTable)>
</cfif>
<cfset v.rsForeign = v.objForeignSet.getAll(
filterField = v.fkField,
filterValue = this[v.myID],
IDOnly = 1,
bUseCache = bUseCache
)>
<cfset v.idList = "">
<cfloop query="v.rsForeign">
<cfset v.idList = listAppend(v.idList, evaluate(v.objForeignObj.theID))>
</cfloop>
</cfif>
<cfelse>
<!--- Not stored, so can't be linked to anything. - leon 6/10/08 --->
<cfset v.idList = "">
</cfif>
<cfset v.stRel.idList = v.idList>
<cfset v.stRel.loaded = 1>
<cfset v.stRel.dirty = 0>
</cfif> <!--- not(v.stRel.loaded) --->
<cfreturn v.stRel.idList>
</cffunction> <!--- getManyRelatedIDs --->
<cffunction name="getManyRelatedRS" returntype="query" output="no" access="public">
<cfargument name="relName" type="variableName" required="yes"
hint="The name of the relationship given in the initial hasMany()">
<cfargument name="filterField" type="string" required="no">
<cfargument name="filterValue" type="string" required="no">
<cfargument name="filterSet" type="struct" required="no">
<cfargument name="bUseCache" type="boolean" required="no" default="no">
<cfset var v = structNew()>