File: Record.pm

package info (click to toggle)
libdbix-searchbuilder-perl 1.82-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 776 kB
  • sloc: perl: 10,608; makefile: 2
file content (1427 lines) | stat: -rwxr-xr-x 36,697 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
package DBIx::SearchBuilder::Record;

use strict;
use warnings;

use vars qw($AUTOLOAD);
use Class::ReturnValue;
use Encode qw();

use DBIx::SearchBuilder::Util qw/ sorted_values /;

=head1 NAME

DBIx::SearchBuilder::Record - Superclass for records loaded by SearchBuilder

=head1 SYNOPSIS

  package MyRecord;
  use base qw/DBIx::SearchBuilder::Record/;

  sub _Init {
      my $self       = shift;
      my $DBIxHandle = shift;  # A DBIx::SearchBuilder::Handle::foo object for your database

      $self->_Handle($DBIxHandle);
      $self->Table("Users");
  }

  # Tell Record what the primary keys are
  sub _PrimaryKeys {
      return ['id'];
  }

  # Preferred and most efficient way to specify fields attributes in a derived
  # class, used by the autoloader to construct Attrib and SetAttrib methods.

  # read: calling $Object->Foo will return the value of this record's Foo column
  # write: calling $Object->SetFoo with a single value will set Foo's value in
  #        both the loaded object and the database
  sub _ClassAccessible {
      {
          Tofu => { 'read' => 1, 'write' => 1 },
          Maz  => { 'auto' => 1, },
          Roo => { 'read' => 1, 'auto' => 1, 'public' => 1, },
      };
  }

  # A subroutine to check a user's password without returning the current value
  # For security purposes, we didn't expose the Password method above
  sub IsPassword {
      my $self = shift;
      my $try  = shift;

      # note two __s in __Value.  Subclasses may muck with _Value, but
      # they should never touch __Value

      if ( $try eq $self->__Value('Password') ) {
          return (1);
      }
      else {
          return (undef);
      }
  }

  # Override DBIx::SearchBuilder::Create to do some checking on create
  sub Create {
      my $self   = shift;
      my %fields = (
          UserId   => undef,
          Password => 'default',    #Set a default password
          @_
      );

      # Make sure a userid is specified
      unless ( $fields{'UserId'} ) {
          die "No userid specified.";
      }

      # Get DBIx::SearchBuilder::Record->Create to do the real work
      return (
          $self->SUPER::Create(
              UserId   => $fields{'UserId'},
              Password => $fields{'Password'},
              Created  => time
          )
      );
  }

=head1 DESCRIPTION

DBIx::SearchBuilder::Record is designed to work with DBIx::SearchBuilder.


=head2 What is it trying to do.

DBIx::SearchBuilder::Record abstracts the agony of writing the common and generally
simple SQL statements needed to serialize and De-serialize an object to the
database.  In a traditional system, you would define various methods on
your object 'create', 'find', 'modify', and 'delete' being the most common.
In each method you would have a SQL statement like:

  select * from table where value='blah';

If you wanted to control what data a user could modify, you would have to
do some special magic to make accessors do the right thing. Etc.  The
problem with this approach is that in a majority of the cases, the SQL is
incredibly simple and the code from one method/object to the next was
basically the same.

<trumpets>

Enter, DBIx::SearchBuilder::Record.

With Record, you can in the simple case, remove all of that code and
replace it by defining two methods and inheriting some code.  It's pretty
simple, and incredibly powerful.  For more complex cases, you can
do more complicated things by overriding certain methods.  Let's stick with
the simple case for now.

The two methods in question are L</_Init> and L</_ClassAccessible>. All they
really do are define some values and send you on your way.  As you might 
have guessed the '_' means that these are private methods.
They will get called by your record object's constructor.

=over 4

=item '_Init'

Defines what table we are talking about, and set a variable to store
the database handle.

=item '_ClassAccessible

Defines what operations may be performed on various data selected
from the database.  For example you can define fields to be mutable,
or immutable, there are a few other options but I don't understand
what they do at this time.

=back

And really, that's it.  So let's have some sample code.

=head2 An Annotated Example

The example code below makes the following assumptions:

=over 4

=item *

The database is 'postgres',

=item *

The host is 'reason',

=item *

The login name is 'mhat',

=item *

The database is called 'example',

=item *

The table is called 'simple',

=item *

The table looks like so:

      id     integer     not NULL,   primary_key(id),
      foo    varchar(10),
      bar    varchar(10)

=back

First, let's define our record class in a new module named "Simple.pm".

  000: package Simple;
  001: use DBIx::SearchBuilder::Record;
  002: @ISA = (DBIx::SearchBuilder::Record);

This should be pretty obvious, name the package, import ::Record and then
define ourself as a subclass of ::Record.

  003:
  004: sub _Init {
  005:   my $this   = shift;
  006:   my $handle = shift;
  007:
  008:   $this->_Handle($handle);
  009:   $this->Table("Simple");
  010:
  011:   return ($this);
  012: }

Here we set our handle and table name. While it's not obvious so far, we'll 
see later that $handle (line: 006) gets passed via C<::Record::new> when a 
new instance is created.  That's actually an important concept: the DB handle
is not bound to a single object but rather, it is shared across objects.

  013:
  014: sub _ClassAccessible {
  015:   {
  016:     Foo => { 'read'  => 1 },
  017:     Bar => { 'read'  => 1, 'write' => 1  },
  018:     Id  => { 'read'  => 1 }
  019:   };
  020: }

What's happening might be obvious, but just in case this method is going to
return a reference to a hash. That hash is where our columns are defined,
as well as what type of operations are acceptable.

  021:
  022: 1;

Like all perl modules, this needs to end with a true value.

Now, on to the code that will actually *do* something with this object.
This code would be placed in your Perl script.

  000: use DBIx::SearchBuilder::Handle;
  001: use Simple;

Use two packages, the first is where I get the DB handle from, the latter
is the object I just created.

  002:
  003: my $handle = DBIx::SearchBuilder::Handle->new();
  004:    $handle->Connect( 'Driver'   => 'Pg',
  005:                    'Database' => 'test',
  006:                    'Host'     => 'reason',
  007:                    'User'     => 'mhat',
  008:                    'Password' => '');

Creates a new DBIx::SearchBuilder::Handle, and then connects to the database using
that handle.  Pretty straight forward, the password '' is what I use
when there is no password.  I could probably leave it blank, but I find
it to be more clear to define it.

  009:
  010: my $s = Simple->new($handle);
  011:
  012: $s->LoadById(1);

LoadById is one of four 'LoadBy' methods,  as the name suggests it searches
for an row in the database that has id='0'.  ::SearchBuilder has, what I
think is a bug, in that it current requires there to be an id field. More
reasonably it also assumes that the id field is unique. LoadById($id) will
do undefined things if there is >1 row with the same id.

In addition to LoadById, we also have:

=over 4

=item LoadByCol

Takes two arguments, a column name and a value.  Again, it will do
undefined things if you use non-unique things.

=item LoadByCols

Takes a hash of columns=>values and returns the *first* to match.
First is probably lossy across databases vendors.

=item LoadFromHash

Populates this record with data from a DBIx::SearchBuilder.  I'm
currently assuming that DBIx::SearchBuilder is what we use in
cases where we expect > 1 record.  More on this later.

=back

Now that we have a populated object, we should do something with it! ::Record
automagically generates accessos and mutators for us, so all we need to do
is call the methods.  Accessors are named <Field>(), and Mutators are named
Set<Field>($).  On to the example, just appending this to the code from
the last example.

  013:
  014: print "ID  : ", $s->Id(),  "\n";
  015: print "Foo : ", $s->Foo(), "\n";
  016: print "Bar : ", $s->Bar(), "\n";

That's all you have to to get the data. Now to change the data!

  017:
  018: $s->SetBar('NewBar');

Pretty simple! That's really all there is to it.  Set<Field>($) returns
a boolean and a string describing the problem.  Let's look at an example of
what will happen if we try to set a 'Id' which we previously defined as 
read only. 

  019: my ($res, $str) = $s->SetId('2');
  020: if (! $res) {
  021:   ## Print the error!
  022:   print "$str\n";
  023: }

The output will be:

  >> Immutable field

Currently Set<Field> updates the data in the database as soon as you call
it.  In the future I hope to extend ::Record to better support transactional
operations, such that updates will only happen when "you" say so.

Finally, adding a removing records from the database.  ::Record provides a
Create method which simply takes a hash of key=>value pairs.  The keys
exactly	map to database fields.

  023: ## Get a new record object.
  024: $s1 = Simple->new($handle);
  025: $s1->Create('Id'  => 4,
  026:             'Foo' => 'Foooooo',
  027:             'Bar' => 'Barrrrr');

Poof! A new row in the database has been created!  Now let's delete the
object! 

  028:
  029: $s1 = undef;
  030: $s1 = Simple->new($handle);
  031: $s1->LoadById(4);
  032: $s1->Delete();

And it's gone.

For simple use, that's more or less all there is to it.  In the future, we
hope to expand this how-to to discuss using container classes, overloading,
etc.

=head1 METHOD NAMING

Each method has a lower case alias; '_' is used to separate words.
For example, the method C<_PrimaryKeys> has the alias C<_primary_keys>.

=head1 METHODS

=cut



=head2  new

Instantiate a new record object.

=cut


sub new  {
    my $proto = shift;

    my $class = ref($proto) || $proto;
    my $self  = {};
    bless ($self, $class);
    $self->_Init(@_);

    return $self;
}


# Not yet documented here.  Should almost certainly be overloaded.
sub _Init {
    my $self = shift;
    my $handle = shift;
    $self->_Handle($handle);
}


=head2 id

Returns this row's primary key.

=cut



*id = \&Id;

sub Id  {
    my $pkey = $_[0]->_PrimaryKey();
    return $_[0]->{'values'}->{ $pkey };
}


=head2 primary_keys

=head2 PrimaryKeys

Return a hash of the values of our primary keys for this function.

=cut




sub PrimaryKeys {
    my $self = shift;
    return map { $_ => $self->{'values'}->{lc $_} } @{$self->_PrimaryKeys};
}




sub DESTROY {
    return 1;
}


sub AUTOLOAD {
    my $self = $_[0];

    no strict 'refs';
    my ($Attrib) = ( $AUTOLOAD =~ /::(\w+)$/o );

    if ( $self->_Accessible( $Attrib, 'read' ) ) {
        *{$AUTOLOAD} = sub { return ( $_[0]->_Value($Attrib) ) };
        goto &$AUTOLOAD;
    }
    elsif ( $self->_Accessible( $Attrib, 'record-read') ) {
        *{$AUTOLOAD} = sub { $_[0]->_ToRecord( $Attrib, $_[0]->__Value($Attrib) ) };
        goto &$AUTOLOAD;
    }
    elsif ( $self->_Accessible( $Attrib, 'foreign-collection') ) {
        *{$AUTOLOAD} = sub { $_[0]->_CollectionValue( $Attrib ) };
        goto &$AUTOLOAD;
    }
    elsif ( $AUTOLOAD =~ /.*::[sS]et_?(\w+)/o ) {
        $Attrib = $1;

        if ( $self->_Accessible( $Attrib, 'write' ) ) {
            *{$AUTOLOAD} = sub {
                return ( $_[0]->_Set( Field => $Attrib, Value => $_[1] ) );
            };
            goto &$AUTOLOAD;
        } elsif ( $self->_Accessible( $Attrib, 'record-write') ) {
            *{$AUTOLOAD} = sub {
                my $self = shift;
                my $val = shift;

                $val = $val->id if UNIVERSAL::isa($val, 'DBIx::SearchBuilder::Record');
                return ( $self->_Set( Field => $Attrib, Value => $val ) );
            };
            goto &$AUTOLOAD;
        }
        elsif ( $self->_Accessible( $Attrib, 'read' ) ) {
            *{$AUTOLOAD} = sub { return ( 0, 'Immutable field' ) };
            goto &$AUTOLOAD;
        }
        else {
            return ( 0, 'Nonexistant field?' );
        }
    }
    elsif ( $AUTOLOAD =~ /.*::(\w+?)_?[oO]bj$/o ) {
        $Attrib = $1;
        if ( $self->_Accessible( $Attrib, 'object' ) ) {
            *{$AUTOLOAD} = sub {
                return (shift)->_Object(
                    Field => $Attrib,
                    Args  => [@_],
                );
            };
            goto &$AUTOLOAD;
        }
        else {
            return ( 0, 'No object mapping for field' );
        }
    }

    #Previously, I checked for writability here. but I'm not sure that's the
    #right idea. it breaks the ability to do ValidateQueue for a ticket
    #on creation.

    elsif ( $AUTOLOAD =~ /.*::[vV]alidate_?(\w+)/o ) {
        $Attrib = $1;

        *{$AUTOLOAD} = sub { return ( $_[0]->_Validate( $Attrib, $_[1] ) ) };
        goto &$AUTOLOAD;
    }

    # TODO: if autoload = 0 or 1 _ then a combination of lowercase and _ chars,
    # turn them into studlycapped phrases

    else {
        my ( $package, $filename, $line );
        ( $package, $filename, $line ) = caller;

        die "$AUTOLOAD Unimplemented in $package. ($filename line $line) \n";
    }

}



=head2 _Accessible KEY MODE

Private method.

Returns undef unless C<KEY> is accessible in C<MODE> otherwise returns C<MODE> value

=cut


sub _Accessible {
    my $self = shift;
    my $attr = shift;
    my $mode = lc(shift || '');

    my $attribute = $self->_ClassAccessible(@_)->{$attr};
    return unless defined $attribute;
    return $attribute->{$mode};
}



=head2 _PrimaryKeys

Return our primary keys. (Subclasses should override this, but our default is that we have one primary key, named 'id'.)

=cut

sub _PrimaryKeys {
    my $self = shift;
    return ['id'];
}


sub _PrimaryKey {
    my $self = shift;
    my $pkeys = $self->_PrimaryKeys();
    die "No primary key" unless ( ref($pkeys) eq 'ARRAY' and $pkeys->[0] );
    die "Too many primary keys" unless ( scalar(@$pkeys) == 1 );
    return $pkeys->[0];
}


=head2 _ClassAccessible

An older way to specify fields attributes in a derived class.
(The current preferred method is by overriding C<Schema>; if you do
this and don't override C<_ClassAccessible>, the module will generate
an appropriate C<_ClassAccessible> based on your C<Schema>.)

Here's an example declaration:

  sub _ClassAccessible {
    {
         Tofu  => { 'read'=>1, 'write'=>1 },
         Maz   => { 'auto'=>1, },
         Roo   => { 'read'=>1, 'auto'=>1, 'public'=>1, },
    };
  }

=cut


sub _ClassAccessible {
    my $self = shift;

    return $self->_ClassAccessibleFromSchema if $self->can('Schema');

    # XXX This is stub code to deal with the old way we used to do _Accessible
    # It should never be called by modern code

    my %accessible;
    while ( my $col = shift ) {
        $accessible{$col}->{lc($_)} = 1
            foreach split(/[\/,]/, shift);
    }
    return(\%accessible);
}

sub _ClassAccessibleFromSchema {
    my $self = shift;

    my $accessible = {};
    foreach my $key ($self->_PrimaryKeys) {
        $accessible->{$key} = { 'read' => 1 };
    };

    my $schema = $self->Schema;

    for my $field (keys %$schema) {
        if ($schema->{$field}{'TYPE'}) {
            $accessible->{$field} = { 'read' => 1, 'write' => 1 };
        } elsif (my $refclass = $schema->{$field}{'REFERENCES'}) {
            if (UNIVERSAL::isa($refclass, 'DBIx::SearchBuilder::Record')) {
                if ($field =~ /(.*)_id$/) {
                    $accessible->{$field} = { 'read' => 1, 'write' => 1 };
                    $accessible->{$1}     = { 'record-read' => 1, 'column' => $field };
                } else {
                    $accessible->{$field} = { 'record-read' => 1, 'record-write' => 1 };
                }
            } elsif (UNIVERSAL::isa($refclass, 'DBIx::SearchBuilder')) {
                $accessible->{$field} = { 'foreign-collection' => 1 };
            } else {
                warn "Error: $refclass neither Record nor Collection";
            }
        }
    }

    return $accessible;
}


sub _ToRecord {
    my $self = shift;
    my $field = shift;
    my $value = shift;

    return unless defined $value;

    my $schema = $self->Schema;
    my $description = $schema->{$field} || $schema->{$field . "_id"};

    die "Can't get schema for $field on $self" unless $description;

    return unless $description;

    return $value unless $description->{'REFERENCES'};

    my $classname = $description->{'REFERENCES'};

    return unless UNIVERSAL::isa($classname, 'DBIx::SearchBuilder::Record');

    # XXX TODO FIXME perhaps this is not what should be passed to new, but it needs it
    my $object = $classname->new( $self->_Handle );
    $object->LoadById( $value );
    return $object;
}

sub _CollectionValue {
    my $self = shift;

    my $method_name =  shift;
    return unless defined $method_name;

    my $schema = $self->Schema;
    my $description = $schema->{$method_name};
    return unless $description;

    my $classname = $description->{'REFERENCES'};

    return unless UNIVERSAL::isa($classname, 'DBIx::SearchBuilder');

    my $coll = $classname->new( Handle => $self->_Handle );

    $coll->Limit( FIELD => $description->{'KEY'}, VALUE => $self->id);

    return $coll;
}

# sub {{{ ReadableAttributes

=head2 ReadableAttributes

Returns an array of the attributes of this class defined as "read" => 1 in this class' _ClassAccessible datastructure

=cut

sub ReadableAttributes {
    my $self = shift;
    my $ca = $self->_ClassAccessible();
    my @readable = grep { $ca->{$_}->{'read'} or $ca->{$_}->{'record-read'} } sort keys %{$ca};
    return (@readable);
}



=head2 WritableAttributes

Returns an array of the attributes of this class defined as "write" => 1 in this class' _ClassAccessible datastructure

=cut

sub WritableAttributes {
    my $self = shift;
    my $ca = $self->_ClassAccessible();
    my @writable = grep { $ca->{$_}->{'write'} || $ca->{$_}->{'record-write'} } sort keys %{$ca};
    return @writable;
}




=head2 __Value

Takes a field name and returns that field's value. Subclasses should never
override __Value.

This method doesn't do any extra work to modify or normalize the encoding of
the field's value. Different databases and database drivers have different ways
of handling encoding on returned values. For example, L<DBD::Oracle> automatically
marks values as UTF-8 if C<NLS_NCHAR> is set to C<AL32UTF8>. Review the documentation
for the database driver you are using and test to make sure you handle special
characters in returned content.

=cut


sub __Value {
    my $self = shift;
    my $field = lc shift;

    $field = $self->_Accessible($field, "column") || $field;

    return $self->{'values'}{$field} if $self->{'fetched'}{$field};
    $self->{'fetched'}{$field} = 1;

    my %pk = $self->PrimaryKeys;
    return undef if grep !defined, values %pk;

    my $query = "SELECT $field FROM ". $self->QuotedTableName
        ." WHERE ". join " AND ", map "$_ = ?", sort keys %pk;
    my $sth = $self->_Handle->SimpleQuery( $query, sorted_values(%pk) ) or return undef;
    return $self->{'values'}{$field} = ($sth->fetchrow_array)[0];
}

=head2 _Value

_Value takes a single column name and returns that column's value for this row.
Subclasses can override _Value to insert custom access control.

=cut


sub _Value  {
    my $self = shift;
    return ($self->__Value(@_));
}



=head2 _Set

_Set takes a single column name and a single unquoted value.
It updates both the in-memory value of this column and the in-database copy.
Subclasses can override _Set to insert custom access control.

=cut


sub _Set {
    my $self = shift;
    return ($self->__Set(@_));
}




sub __Set {
    my $self = shift;

    my %args = (
        'Field' => undef,
        'Value' => undef,
        'IsSQL' => undef,
        @_
    );

    $args{'Column'}        = delete $args{'Field'};
    $args{'IsSQLFunction'} = delete $args{'IsSQL'};

    my $ret = Class::ReturnValue->new();

    unless ( $args{'Column'} ) {
        $ret->as_array( 0, 'No column specified' );
        $ret->as_error(
            errno        => 5,
            do_backtrace => 0,
            message      => "No column specified"
        );
        return ( $ret->return_value );
    }
    my $column = lc $args{'Column'};

    # XXX: OLD behaviour, no_undefs_in_set will go away
    if ( !defined $args{'Value'} && $self->{'no_undefs_in_set' } ) {
        $ret->as_array( 0, "No value passed to _Set" );
        $ret->as_error(
            errno        => 2,
            do_backtrace => 0,
            message      => "No value passed to _Set"
        );
        return ( $ret->return_value );
    }

    if ( defined $args{'Value'} ) {
        if ( $args{'Value'} eq '' &&
             ( $self->_Accessible( $args{'Column'}, 'is_numeric' )
               || ($self->_Accessible( $args{'Column'}, 'type' ) || '') =~ /INT/i ) )
        {
            $args{'Value'} = 0;
        }
    }
    else {
        if ( $self->_Accessible( $args{Column}, 'no_nulls' ) ) {
            my $default = $self->_Accessible( $args{Column}, 'default' );

            if ( defined $default ) {
                $args{'Value'} = $default;
            }
            else {
                $ret->as_array( 0, 'Illegal value for non-nullable field ' . $args{'Column'} . ": undef/null value provided and no default specified by class" );
                $ret->as_error(
                    errno        => 3,
                    do_backtrace => 0,
                    message      => "Illegal value for non-nullable field " . $args{'Column'} . ": undef/null value provided and no default specified by class"
                );
                return ( $ret->return_value );
            }
        }
    }

    # First, we truncate the value, if we need to.
    $args{'Value'} = $self->TruncateValue( $args{'Column'}, $args{'Value'} );

    my $current_value = $self->__Value($column);

    if (
        ( !defined $args{'Value'} && !defined $current_value )
        || (   defined $args{'Value'}
            && defined $current_value
            && ( $args{'Value'} eq $current_value ) )
      )
    {
        $ret->as_array( 0, "That is already the current value" );
        $ret->as_error(
            errno        => 1,
            do_backtrace => 0,
            message      => "That is already the current value"
        );
        return ( $ret->return_value );
    }

    my $method = "Validate" . $args{'Column'};
    unless ( $self->$method( $args{'Value'} ) ) {
        $ret->as_array( 0, 'Illegal value for ' . $args{'Column'} );
        $ret->as_error(
            errno        => 3,
            do_backtrace => 0,
            message      => "Illegal value for " . $args{'Column'}
        );
        return ( $ret->return_value );
    }

    $args{'Table'}       = $self->Table();
    $args{'PrimaryKeys'} = { $self->PrimaryKeys() };

    # The blob handling will destroy $args{'Value'}. But we assign
    # that back to the object at the end. this works around that
    my $unmunged_value = $args{'Value'};

    unless ( $self->_Handle->KnowsBLOBs ) {
        # Support for databases which don't deal with LOBs automatically
        my $ca = $self->_ClassAccessible();
        my $key = $args{'Column'};
        if ( ( $ca->{$key}->{'type'} // '' ) =~ /^(text|longtext|clob|longblob|blob|lob)$/i ) {
            my $bhash = $self->_Handle->BLOBParams( $key, $ca->{$key}->{'type'} );
            if ( ref($bhash) eq 'HASH'
                && ( ( defined $args{'Value'} && length $args{'Value'} ) || $self->_Handle->HasSupportForEmptyString ) )
            {
                $bhash->{'value'} = $args{'Value'};
                $args{'Value'} = $bhash;
            }
        }
    }


    my $val = $self->_Handle->UpdateRecordValue(%args);
    unless ($val) {
        my $message =
            $args{'Column'}
          . " could not be set to "
          . ( defined $args{'Value'} ? $args{'Value'} : 'undef' ) . ".";
        $ret->as_array( 0, $message);
        $ret->as_error(
            errno        => 4,
            do_backtrace => 0,
            message      => $message
        );
        return ( $ret->return_value );
    }
    # If we've performed some sort of "functional update"
    # then we need to reload the object from the DB to know what's
    # really going on. (ex SET Cost = Cost+5)
    if ( $args{'IsSQLFunction'} ) {
        $self->Load( $self->Id );
    }
    else {
        $self->{'values'}->{"$column"} = $unmunged_value;
    }
    $ret->as_array( 1, "The new value has been set." );
    return ( $ret->return_value );
}

=head2 _Canonicalize PARAMHASH

This routine massages an input value (VALUE) for FIELD into something that's
going to be acceptable.

Takes

=over

=item FIELD

=item VALUE

=item FUNCTION

=back


Takes:

=over

=item FIELD

=item VALUE

=item FUNCTION

=back

Returns a replacement VALUE.

=cut

sub _Canonicalize {
    my $self = shift;
    my $field = shift;



}


=head2 _Validate FIELD VALUE

Validate that VALUE will be an acceptable value for FIELD.

Currently, this routine does nothing whatsoever.

If it succeeds (which is always the case right now), returns true. Otherwise returns false.

=cut




sub _Validate  {
    my $self = shift;
    my $field = shift;
    my $value = shift;

    #Check type of input
    #If it's null, are nulls permitted?
    #If it's an int, check the # of bits
    #If it's a string,
    #check length
    #check for nonprintables
    #If it's a blob, check for length
    #In an ideal world, if this is a link to another table, check the dependency.
    return(1);
}



=head2 TruncateValue  KEY VALUE

Truncate a value that's about to be set so that it will fit inside the database'
s idea of how big the column is.

(Actually, it looks at SearchBuilder's concept of the database, not directly into the db).

=cut

sub TruncateValue {
    my $self  = shift;
    my $key   = shift;
    my $value = shift;

    # We don't need to truncate empty things.
    return undef unless defined $value;

    my $metadata = $self->_ClassAccessible->{$key};
    return $value unless $metadata;

    my $truncate_to;
    if ( $metadata->{'length'} && !$metadata->{'is_numeric'} ) {
        $truncate_to = int $metadata->{'length'};
    }
    elsif ($metadata->{'type'} && $metadata->{'type'} =~ /char\((\d+)\)/ ) {
        $truncate_to = $1;
    }
    return $value unless $truncate_to;

    # return asap if length in bytes is smaller than limit
    return $value if $truncate_to >= do { use bytes; length $value };

    if ( Encode::is_utf8($value) ) {
        return Encode::decode_utf8(
            substr( Encode::encode_utf8( $value ), 0, $truncate_to ),
            Encode::FB_QUIET(),
        );
    }
    else {
        # XXX: if it's not UTF-8 then why do we convert it to?
        return Encode::encode_utf8( Encode::decode_utf8 (
            substr( $value, 0, $truncate_to ), Encode::FB_QUIET(),
        ) );
    }
}


=head2 _Object

_Object takes a single column name and an array reference.
It creates new object instance of class specified in _ClassAccessable
structure and calls LoadById on recently created object with the
current column value as argument. It uses the array reference as
the object constructor's arguments.
Subclasses can override _Object to insert custom access control or
define default constructor arguments.

Note that if you are using a C<Schema> with a C<REFERENCES> field,
this is unnecessary: the method to access the column's value will
automatically turn it into the appropriate object.

=cut

sub _Object {
    my $self = shift;
    return $self->__Object(@_);
}

sub __Object {
    my $self = shift;
    my %args = ( Field => '', Args => [], @_ );

    my $field = $args{'Field'};
    my $class = $self->_Accessible( $field, 'object' );

    # Globs magic to be sure that we call 'eval "require $class"' only once
    # because eval is quite slow -- cubic@acronis.ru
    no strict qw( refs );
    my $vglob = ${ $class . '::' }{'VERSION'};
    unless ( $vglob && *$vglob{'SCALAR'} ) {
        eval "require $class";
        die "Couldn't use $class: $@" if ($@);
        unless ( $vglob && *$vglob{'SCALAR'} ) {
            *{ $class . "::VERSION" } = '-1, By DBIx::SearchBuilder';
        }
    }

    my $object = $class->new( @{ $args{'Args'} } );
    $object->LoadById( $self->__Value($field) );
    return $object;
}




# load should do a bit of overloading
# if we call it with only one argument, we're trying to load by reference.
# if we call it with a passel of arguments, we're trying to load by value
# The latter is primarily important when we've got a whole set of record that we're
# reading in with a recordset class and want to instantiate objefcts for each record.

=head2 Load

Takes a single argument, $id. Calls LoadById to retrieve the row whose primary key
is $id

=cut



sub Load {
    my $self = shift;
    return $self->LoadById(@_);
}


=head2 LoadByCol

Takes two arguments, a column and a value. The column can be any table column
which contains unique values.  Behavior when using a non-unique value is
undefined

=cut

sub LoadByCol  {
    my $self = shift;
    return $self->LoadByCols(@_);
}



=head2 LoadByCols

Takes a hash of columns and values. Loads the first record that matches all
keys.

The hash's keys are the columns to look at.

The hash's values are either: scalar values to look for
OR has references which contain 'operator' and 'value'

=cut


sub LoadByCols  {
    my $self = shift;
    my %hash  = (@_);
    my (@bind, @phrases);
    foreach my $key (sort keys %hash) {
        if (defined $hash{$key} &&  $hash{$key} ne '') {
            my $op;
            my $value;
            my $function = "?";
            if (ref $hash{$key} eq 'HASH') {
                $op = $hash{$key}->{operator};
                $value = $hash{$key}->{value};
                $function = $hash{$key}->{function} || "?";
            } else {
                $op = '=';
                $value = $hash{$key};
            }

            push @phrases, "$key $op $function";
            push @bind, $value;
        }
        else {
            push @phrases, "($key IS NULL OR $key = ?)";
            my $meta = $self->_ClassAccessible->{$key};
            $meta->{'type'} ||= '';
            # TODO: type checking should be done in generic way
            if ( $meta->{'is_numeric'} || $meta->{'type'} =~ /INT|NUMERIC|DECIMAL|REAL|DOUBLE|FLOAT/i  ) {
                 push @bind, 0;
            } else {
                 push @bind, '';
            }
        }
    }

    my $QueryString = "SELECT  * FROM ".$self->QuotedTableName." WHERE ".
    join(' AND ', @phrases) ;
    return ($self->_LoadFromSQL($QueryString, @bind));
}




=head2 LoadById

Loads a record by its primary key. Your record class must define a single primary key column.

=cut


sub LoadById  {
    my ($self, $id) = @_;
    return $self->LoadByCols( $self->_PrimaryKey, defined $id? $id: 0 );
}




=head2 LoadByPrimaryKeys

Like LoadById with basic support for compound primary keys.

=cut



sub LoadByPrimaryKeys {
    my $self = shift;
    my $data = (ref $_[0] eq 'HASH')? $_[0]: {@_};

    my %cols=();
    foreach (@{$self->_PrimaryKeys}) {
        return (0, "Missing PK field: '$_'") unless defined $data->{$_};
        $cols{$_}=$data->{$_};
    }
    return ($self->LoadByCols(%cols));
}




=head2 LoadFromHash

Takes a hashref, such as created by DBIx::SearchBuilder and populates this record's
loaded values hash.

=cut



sub LoadFromHash {
    my $self = shift;
    my $hashref = shift;

    foreach my $f ( keys %$hashref ) {
        $self->{'fetched'}{lc $f} = 1;
    }

    $self->{'values'} = $hashref;
    return $self->id();
}



=head2 _LoadFromSQL QUERYSTRING @BIND_VALUES

Load a record as the result of an SQL statement

=cut




sub _LoadFromSQL {
    my $self        = shift;
    my $QueryString = shift;
    my @bind_values = (@_);

    my $sth = $self->_Handle->SimpleQuery( $QueryString, @bind_values );

    #TODO this only gets the first row. we should check if there are more.

    return ( 0, "Couldn't execute query: ".$self->_Handle->dbh->errstr ) unless $sth;

    $self->{'values'} = $sth->fetchrow_hashref;
    $self->{'fetched'} = {};
    if ( !$self->{'values'} && $sth->err ) {
        return ( 0, "Couldn't fetch row: ". $sth->err );
    }

    unless ( $self->{'values'} ) {
        return ( 0, "Couldn't find row" );
    }

    ## I guess to be consistant with the old code, make sure the primary
    ## keys exist.

    if( grep { not defined } $self->PrimaryKeys ) {
        return ( 0, "Missing a primary key?" );
    }

    foreach my $f ( keys %{$self->{'values'}} ) {
        $self->{'fetched'}{lc $f} = 1;
    }
    return ( 1, "Found Object" );

}





=head2 Create

Takes an array of key-value pairs and drops any keys that aren't known
as columns for this recordtype

=cut



sub Create {
    my $self    = shift;
    my %attribs = @_;

    my ($key);
    foreach $key ( keys %attribs ) {

        if ( $self->_Accessible( $key, 'record-write' ) ) {
            $attribs{$key} = $attribs{$key}->id
              if UNIVERSAL::isa( $attribs{$key},
                'DBIx::SearchBuilder::Record' );
        }

        if ( defined $attribs{$key} ) {
            if ( $attribs{$key} eq '' &&
                 ( $self->_Accessible( $key, 'is_numeric' )
                   || ($self->_Accessible( $key, 'type' ) || '') =~ /INT/i ) )
            {
                $attribs{$key} = 0;
            }
        }
        else {
            $attribs{$key} = $self->_Accessible( $key, 'default' )
                if $self->_Accessible( $key, 'no_nulls' );
        }

        #Truncate things that are too long for their datatypes
        $attribs{$key} = $self->TruncateValue( $key => $attribs{$key} );

    }
    unless ( $self->_Handle->KnowsBLOBs ) {

        # Support for databases which don't deal with LOBs automatically
        my $ca = $self->_ClassAccessible();
        foreach $key ( keys %attribs ) {
            my $type = $ca->{$key}->{'type'};
            next unless $type && $type =~ /^(text|longtext|clob|blob|lob|longblob)$/i;

            my $bhash = $self->_Handle->BLOBParams( $key, $type );
            if ( ref($bhash) eq 'HASH'
                && ( ( defined $attribs{$key} && length $attribs{$key} ) || $self->_Handle->HasSupportForEmptyString ) )
            {
                $bhash->{'value'} = $attribs{$key};
                $attribs{$key} = $bhash;
            }
        }
    }
    return ( $self->_Handle->Insert( $self->Table, %attribs ) );
}


=head2 Delete

Delete this record from the database. On failure return a Class::ReturnValue with the error. On success, return 1;

=cut

*delete =  \&Delete;

sub Delete {
    $_[0]->__Delete;
}

sub __Delete {
    my $self = shift;

    #TODO Check to make sure the key's not already listed.
    #TODO Update internal data structure

    ## Constructs the where clause.
    my @bind=();
    my %pkeys=$self->PrimaryKeys();
    my $where  = 'WHERE ';
    foreach my $key (sort keys %pkeys) {
        $where .= $key . "=?" . " AND ";
        push (@bind, $pkeys{$key});
    }

    $where =~ s/AND\s$//;
    my $QueryString = "DELETE FROM ". $self->QuotedTableName . ' ' . $where;
    my $return = $self->_Handle->SimpleQuery($QueryString, @bind);

    if (UNIVERSAL::isa($return, 'Class::ReturnValue')) {
        return ($return);
    } else {
        return(1);
    }
}





=head2 Table

Returns or sets the name of the current Table

=cut



sub Table {
    my $self = shift;
    if (@_) {
        $self->{'table'} = shift;
    }
    return ($self->{'table'});
}

=head2 QuotedTableName

Returns the name of current Table, or the table provided as an argument, including any quoting
 based on yje Handle's QuoteTableNames flag and driver method.

=cut

sub QuotedTableName {
    my ($self, $name) = @_;
    unless ($name) {
        return $self->{'_quoted_table'} if defined $self->{'_quoted_table'};
        $self->{'_quoted_table'}
            = $self->_Handle->QuoteTableNames ? $self->_Handle->QuoteName( $self->Table ) : $self->Table;
        return $self->{'_quoted_table'};
    }
    return $self->_Handle->QuoteTableNames ? $self->_Handle->QuoteName($name) : $name;
}

=head2 _Handle

Returns or sets the current DBIx::SearchBuilder::Handle object

=cut


sub _Handle  {
    my $self = shift;
    if (@_) {
        $self->{'DBIxHandle'} = shift;
    }
    return ($self->{'DBIxHandle'});
}


if( eval { require capitalization } ) {
    capitalization->unimport( __PACKAGE__ );
}

1;