use FS::part_svc and FS::svc_acct_pop to avoid warnings
[freeside.git] / site_perl / Record.pm
1 package FS::Record;
2
3 use strict;
4 use vars qw($dbdef_file $dbdef $setup_hack $AUTOLOAD @ISA @EXPORT_OK);
5 use subs qw(reload_dbdef);
6 use Exporter;
7 use Carp qw(carp cluck croak confess);
8 use File::CounterFile;
9 use FS::UID qw(dbh checkruid swapuid getotaker datasrc);
10 use FS::dbdef;
11
12 @ISA = qw(Exporter);
13 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef);
14
15 #ask FS::UID to run this stuff for us later
16 $FS::UID::callback{'FS::Record'} = sub { 
17   $File::CounterFile::DEFAULT_DIR = "/usr/local/etc/freeside/counters.". datasrc;
18   $dbdef_file = "/usr/local/etc/freeside/dbdef.". datasrc;
19   &reload_dbdef unless $setup_hack; #$setup_hack needed now?
20 };
21
22 =head1 NAME
23
24 FS::Record - Database record objects
25
26 =head1 SYNOPSIS
27
28     use FS::Record;
29     use FS::Record qw(dbh fields qsearch qsearchs dbdef);
30
31     $record = new FS::Record 'table', \%hash;
32     $record = new FS::Record 'table', { 'column' => 'value', ... };
33
34     $record  = qsearchs FS::Record 'table', \%hash;
35     $record  = qsearchs FS::Record 'table', { 'column' => 'value', ... };
36     @records = qsearch  FS::Record 'table', \%hash; 
37     @records = qsearch  FS::Record 'table', { 'column' => 'value', ... };
38
39     $table = $record->table;
40     $dbdef_table = $record->dbdef_table;
41
42     $value = $record->get('column');
43     $value = $record->getfield('column');
44     $value = $record->column;
45
46     $record->set( 'column' => 'value' );
47     $record->setfield( 'column' => 'value' );
48     $record->column('value');
49
50     %hash = $record->hash;
51
52     $hashref = $record->hashref;
53
54     $error = $record->insert;
55     #$error = $record->add; #depriciated
56
57     $error = $record->delete;
58     #$error = $record->del; #depriciated
59
60     $error = $new_record->replace($old_record);
61     #$error = $new_record->rep($old_record); #depriciated
62
63     $value = $record->unique('column');
64
65     $value = $record->ut_float('column');
66     $value = $record->ut_number('column');
67     $value = $record->ut_numbern('column');
68     $value = $record->ut_money('column');
69     $value = $record->ut_text('column');
70     $value = $record->ut_textn('column');
71     $value = $record->ut_alpha('column');
72     $value = $record->ut_alphan('column');
73     $value = $record->ut_phonen('column');
74     $value = $record->ut_anythingn('column');
75
76     $dbdef = reload_dbdef;
77     $dbdef = reload_dbdef "/non/standard/filename";
78     $dbdef = dbdef;
79
80     $quoted_value = _quote($value,'table','field');
81
82     #depriciated
83     $fields = hfields('table');
84     if ( $fields->{Field} ) { # etc.
85
86     @fields = fields 'table'; #as a subroutine
87     @fields = $record->fields; #as a method call
88
89
90 =head1 DESCRIPTION
91
92 (Mostly) object-oriented interface to database records.  Records are currently
93 implemented on top of DBI.  FS::Record is intended as a base class for
94 table-specific classes to inherit from, i.e. FS::cust_main.
95
96 =head1 CONSTRUCTORS
97
98 =over 4
99
100 =item new [ TABLE, ] HASHREF
101
102 Creates a new record.  It doesn't store it in the database, though.  See
103 L<"insert"> for that.
104
105 Note that the object stores this hash reference, not a distinct copy of the
106 hash it points to.  You can ask the object for a copy with the I<hash> 
107 method.
108
109 TABLE can only be omitted when a dervived class overrides the table method.
110
111 =cut
112
113 sub new { 
114   my $proto = shift;
115   my $class = ref($proto) || $proto;
116   my $self = {};
117   bless ($self, $class);
118
119   $self->{'Table'} = shift unless defined ( $self->table );
120
121   my $hashref = $self->{'Hash'} = shift;
122
123   foreach my $field ( $self->fields ) { 
124     $hashref->{$field}='' unless defined $hashref->{$field};
125     #trim the '$' from money fields for Pg (belong HERE?)
126     #(what about Pg i18n?)
127     if ( datasrc =~ m/Pg/ 
128          && $self->dbdef_table->column($field)->type eq 'money' ) {
129       ${$hashref}{$field} =~ s/^\$//;
130     }
131   }
132
133   $self;
134 }
135
136 sub create {
137   my $proto = shift;
138   my $class = ref($proto) || $proto;
139   my $self = {};
140   bless ($self, $class);
141   if ( defined $self->table ) {
142     cluck "create constructor is depriciated, use new!";
143     $self->new(@_);
144   } else {
145     croak "FS::Record::create called (not from a subclass)!";
146   }
147 }
148
149 =item qsearch TABLE, HASHREF
150
151 Searches the database for all records matching (at least) the key/value pairs
152 in HASHREF.  Returns all the records found as `FS::TABLE' objects if that
153 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
154 objects.
155
156 =cut
157
158 sub qsearch {
159   my($table,$record) = @_;
160   my($dbh) = dbh;
161
162   my(@fields)=grep exists($record->{$_}), fields($table);
163
164   my($sth);
165   my($statement) = "SELECT * FROM $table". ( @fields
166     ? " WHERE ". join(' AND ',
167       map {
168         $record->{$_} eq ''
169           ? "( $_ IS NULL OR $_ = \"\" )"
170           : "$_ = ". _quote($record->{$_},$table,$_)
171       } @fields
172     ) : ''
173   );
174   $sth=$dbh->prepare($statement)
175     or croak $dbh->errstr; #is that a little too harsh?  hmm.
176
177   if ( eval ' scalar(@FS::'. $table. '::ISA);' ) {
178     map {
179       eval 'new FS::'. $table. ' ( $sth->fetchrow_hashref );';
180     } ( 1 .. $sth->execute );
181   } else {
182     cluck "qsearch: warning: FS::$table not loaded; returning generic FS::Record objects";
183     map {
184       new FS::Record ($table,$sth->fetchrow_hashref);
185     } ( 1 .. $sth->execute );
186   }
187
188 }
189
190 =item qsearchs TABLE, HASHREF
191
192 Same as qsearch, except that if more than one record matches, it B<carp>s but
193 returns the first.  If this happens, you either made a logic error in asking
194 for a single item, or your data is corrupted.
195
196 =cut
197
198 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
199   my(@result) = qsearch(@_);
200   carp "warning: Multiple records in scalar search!" if scalar(@result) > 1;
201     #should warn more vehemently if the search was on a primary key?
202   $result[0];
203 }
204
205 =back
206
207 =head1 METHODS
208
209 =over 4
210
211 =item table
212
213 Returns the table name.
214
215 =cut
216
217 sub table {
218 #  cluck "warning: FS::Record::table depriciated; supply one in subclass!";
219   my $self = shift;
220   $self -> {'Table'};
221 }
222
223 =item dbdef_table
224
225 Returns the FS::dbdef_table object for the table.
226
227 =cut
228
229 sub dbdef_table {
230   my($self)=@_;
231   my($table)=$self->table;
232   $dbdef->table($table);
233 }
234
235 =item get, getfield COLUMN
236
237 Returns the value of the column/field/key COLUMN.
238
239 =cut
240
241 sub get {
242   my($self,$field) = @_;
243   # to avoid "Use of unitialized value" errors
244   if ( defined ( $self->{Hash}->{$field} ) ) {
245     $self->{Hash}->{$field};
246   } else { 
247     '';
248   }
249 }
250 sub getfield {
251   my $self = shift;
252   $self->get(@_);
253 }
254
255 =item set, setfield COLUMN, VALUE
256
257 Sets the value of the column/field/key COLUMN to VALUE.  Returns VALUE.
258
259 =cut
260
261 sub set { 
262   my($self,$field,$value) = @_;
263   $self->{'Hash'}->{$field} = $value;
264 }
265 sub setfield {
266   my $self = shift;
267   $self->set(@_);
268 }
269
270 =item AUTLOADED METHODS
271
272 $record->column is a synonym for $record->get('column');
273
274 $record->column('value') is a synonym for $record->set('column','value');
275
276 =cut
277
278 sub AUTOLOAD {
279   my($self,$value)=@_;
280   my($field)=$AUTOLOAD;
281   $field =~ s/.*://;
282   if ( defined($value) ) {
283     $self->setfield($field,$value);
284   } else {
285     $self->getfield($field);
286   }    
287 }
288
289 =item hash
290
291 Returns a list of the column/value pairs, usually for assigning to a new hash.
292
293 To make a distinct duplicate of an FS::Record object, you can do:
294
295     $new = new FS::Record ( $old->table, { $old->hash } );
296
297 =cut
298
299 sub hash {
300   my($self) = @_;
301   %{ $self->{'Hash'} }; 
302 }
303
304 =item hashref
305
306 Returns a reference to the column/value hash.
307
308 =cut
309
310 sub hashref {
311   my($self) = @_;
312   $self->{'Hash'};
313 }
314
315 =item insert
316
317 Inserts this record to the database.  If there is an error, returns the error,
318 otherwise returns false.
319
320 =cut
321
322 sub insert {
323   my $self = shift;
324
325   my $error = $self->check;
326   return $error if $error;
327
328   #single-field unique keys are given a value if false
329   #(like MySQL's AUTO_INCREMENT)
330   foreach ( $self->dbdef_table->unique->singles ) {
331     $self->unique($_) unless $self->getfield($_);
332   }
333   #and also the primary key
334   my $primary_key = $self->dbdef_table->primary_key;
335   $self->unique($primary_key) 
336     if $primary_key && ! $self->getfield($primary_key);
337
338   my @fields =
339     grep defined($self->getfield($_)) && $self->getfield($_) ne "",
340     $self->fields
341   ;
342
343   my $statement = "INSERT INTO ". $self->table. " ( ".
344       join(', ',@fields ).
345     ") VALUES (".
346       join(', ',map(_quote($self->getfield($_),$self->table,$_), @fields)).
347     ")"
348   ;
349   my $sth = dbh->prepare($statement) or return dbh->errstr;
350
351   local $SIG{HUP} = 'IGNORE';
352   local $SIG{INT} = 'IGNORE';
353   local $SIG{QUIT} = 'IGNORE'; 
354   local $SIG{TERM} = 'IGNORE';
355   local $SIG{TSTP} = 'IGNORE';
356   local $SIG{PIPE} = 'IGNORE';
357
358   $sth->execute or return $sth->errstr;
359
360   '';
361 }
362
363 =item add
364
365 Depriciated (use insert instead).
366
367 =cut
368
369 sub add {
370   cluck "warning: FS::Record::add depriciated!";
371   insert @_; #call method in this scope
372 }
373
374 =item delete
375
376 Delete this record from the database.  If there is an error, returns the error,
377 otherwise returns false.
378
379 =cut
380
381 sub delete {
382   my $self = shift;
383
384   my($statement)="DELETE FROM ". $self->table. " WHERE ". join(' AND ',
385     map {
386       $self->getfield($_) eq ''
387         ? "( $_ IS NULL OR $_ = \"\" )"
388         : "$_ = ". _quote($self->getfield($_),$self->table,$_)
389     } ( $self->dbdef_table->primary_key )
390           ? ( $self->dbdef_table->primary_key)
391           : $self->fields
392   );
393   my $sth = dbh->prepare($statement) or return dbh->errstr;
394
395   local $SIG{HUP} = 'IGNORE';
396   local $SIG{INT} = 'IGNORE';
397   local $SIG{QUIT} = 'IGNORE'; 
398   local $SIG{TERM} = 'IGNORE';
399   local $SIG{TSTP} = 'IGNORE';
400   local $SIG{PIPE} = 'IGNORE';
401
402   my $rc = $sth->execute or return $sth->errstr;
403   #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
404
405   undef $self; #no need to keep object!
406
407   '';
408 }
409
410 =item del
411
412 Depriciated (use delete instead).
413
414 =cut
415
416 sub del {
417   cluck "warning: FS::Record::del depriciated!";
418   &delete(@_); #call method in this scope
419 }
420
421 =item replace OLD_RECORD
422
423 Replace the OLD_RECORD with this one in the database.  If there is an error,
424 returns the error, otherwise returns false.
425
426 =cut
427
428 sub replace {
429   my ( $new, $old ) = ( shift, shift );
430
431   my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
432   unless ( @diff ) {
433     carp "warning: records identical";
434     return '';
435   }
436
437   return "Records not in same table!" unless $new->table eq $old->table;
438
439   my $primary_key = $old->dbdef_table->primary_key;
440   return "Can't change $primary_key"
441     if $primary_key
442        && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
443
444   my $error = $new->check;
445   return $error if $error;
446
447   my $statement = "UPDATE ". $old->table. " SET ". join(', ',
448     map {
449       "$_ = ". _quote($new->getfield($_),$old->table,$_) 
450     } @diff
451   ). ' WHERE '.
452     join(' AND ',
453       map {
454         $old->getfield($_) eq ''
455           ? "( $_ IS NULL OR $_ = \"\" )"
456           : "$_ = ". _quote($old->getfield($_),$old->table,$_)
457       } ( $primary_key ? ( $primary_key ) : $old->fields )
458     )
459   ;
460   my $sth = dbh->prepare($statement) or return dbh->errstr;
461
462   local $SIG{HUP} = 'IGNORE';
463   local $SIG{INT} = 'IGNORE';
464   local $SIG{QUIT} = 'IGNORE'; 
465   local $SIG{TERM} = 'IGNORE';
466   local $SIG{TSTP} = 'IGNORE';
467   local $SIG{PIPE} = 'IGNORE';
468
469   my $rc = $sth->execute or return $sth->errstr;
470   #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
471
472   '';
473
474 }
475
476 =item rep
477
478 Depriciated (use replace instead).
479
480 =cut
481
482 sub rep {
483   cluck "warning: FS::Record::rep depriciated!";
484   replace @_; #call method in this scope
485 }
486
487 =item check
488
489 Not yet implemented, croaks.  Derived classes should provide a check method.
490
491 =cut
492
493 sub check {
494   croak "FS::Record::check not implemented; supply one in subclass!";
495 }
496
497 =item unique COLUMN
498
499 Replaces COLUMN in record with a unique number.  Called by the B<add> method
500 on primary keys and single-field unique columns (see L<FS::dbdef_table>).
501 Returns the new value.
502
503 =cut
504
505 sub unique {
506   my($self,$field) = @_;
507   my($table)=$self->table;
508
509   croak("&FS::UID::checkruid failed") unless &checkruid;
510
511   croak "Unique called on field $field, but it is ",
512         $self->getfield($field),
513         ", not null!"
514     if $self->getfield($field);
515
516   #warn "table $table is tainted" if is_tainted($table);
517   #warn "field $field is tainted" if is_tainted($field);
518
519   &swapuid;
520   my($counter) = new File::CounterFile "$table.$field",0;
521 # hack for web demo
522 #  getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
523 #  my($user)=$1;
524 #  my($counter) = new File::CounterFile "$user/$table.$field",0;
525 # endhack
526
527   my($index)=$counter->inc;
528   $index=$counter->inc
529     while qsearchs($table,{$field=>$index}); #just in case
530   &swapuid;
531
532   $index =~ /^(\d*)$/;
533   $index=$1;
534
535   $self->setfield($field,$index);
536
537 }
538
539 =item ut_float COLUMN
540
541 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10.  May not be
542 null.  If there is an error, returns the error, otherwise returns false.
543
544 =cut
545
546 sub ut_float {
547   my($self,$field)=@_ ;
548   ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
549    $self->getfield($field) =~ /^(\d+)$/ ||
550    $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
551    $self->getfield($field) =~ /^(\d+e\d+)$/)
552     or return "Illegal or empty (float) $field!";
553   $self->setfield($field,$1);
554   '';
555 }
556
557 =item ut_number COLUMN
558
559 Check/untaint simple numeric data (whole numbers).  May not be null.  If there
560 is an error, returns the error, otherwise returns false.
561
562 =cut
563
564 sub ut_number {
565   my($self,$field)=@_;
566   $self->getfield($field) =~ /^(\d+)$/
567     or return "Illegal or empty (numeric) $field!";
568   $self->setfield($field,$1);
569   '';
570 }
571
572 =item ut_numbern COLUMN
573
574 Check/untaint simple numeric data (whole numbers).  May be null.  If there is
575 an error, returns the error, otherwise returns false.
576
577 =cut
578
579 sub ut_numbern {
580   my($self,$field)=@_;
581   $self->getfield($field) =~ /^(\d*)$/
582     or return "Illegal (numeric) $field!";
583   $self->setfield($field,$1);
584   '';
585 }
586
587 =item ut_money COLUMN
588
589 Check/untaint monetary numbers.  May be negative.  Set to 0 if null.  If there
590 is an error, returns the error, otherwise returns false.
591
592 =cut
593
594 sub ut_money {
595   my($self,$field)=@_;
596   $self->setfield($field, 0) if $self->getfield($field) eq '';
597   $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
598     or return "Illegal (money) $field!";
599   #$self->setfield($field, "$1$2$3" || 0);
600   $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
601   '';
602 }
603
604 =item ut_text COLUMN
605
606 Check/untaint text.  Alphanumerics, spaces, and the following punctuation
607 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
608 May not be null.  If there is an error, returns the error, otherwise returns
609 false.
610
611 =cut
612
613 sub ut_text {
614   my($self,$field)=@_;
615   $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]+)$/
616     or return "Illegal or empty (text) $field";
617   $self->setfield($field,$1);
618   '';
619 }
620
621 =item ut_textn COLUMN
622
623 Check/untaint text.  Alphanumerics, spaces, and the following punctuation
624 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
625 May be null.  If there is an error, returns the error, otherwise returns false.
626
627 =cut
628
629 sub ut_textn {
630   my($self,$field)=@_;
631   $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]*)$/
632     or return "Illegal (text) $field";
633   $self->setfield($field,$1);
634   '';
635 }
636
637 =item ut_alpha COLUMN
638
639 Check/untaint alphanumeric strings (no spaces).  May not be null.  If there is
640 an error, returns the error, otherwise returns false.
641
642 =cut
643
644 sub ut_alpha {
645   my($self,$field)=@_;
646   $self->getfield($field) =~ /^(\w+)$/
647     or return "Illegal or empty (alphanumeric) $field!";
648   $self->setfield($field,$1);
649   '';
650 }
651
652 =item ut_alpha COLUMN
653
654 Check/untaint alphanumeric strings (no spaces).  May be null.  If there is an
655 error, returns the error, otherwise returns false.
656
657 =cut
658
659 sub ut_alphan {
660   my($self,$field)=@_;
661   $self->getfield($field) =~ /^(\w*)$/ 
662     or return "Illegal (alphanumeric) $field!";
663   $self->setfield($field,$1);
664   '';
665 }
666
667 =item ut_phonen COLUMN
668
669 Check/untaint phone numbers.  May be null.  If there is an error, returns
670 the error, otherwise returns false.
671
672 =cut
673
674 sub ut_phonen {
675   my($self,$field)=@_;
676   my $phonen = $self->getfield($field);
677   if ( $phonen eq '' ) {
678     $self->setfield($field,'');
679   } else {
680     $phonen =~ s/\D//g;
681     $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
682       or return "Illegal (phone) $field!";
683     $phonen = "$1-$2-$3";
684     $phonen .= " x$4" if $4;
685     $self->setfield($field,$phonen);
686   }
687   '';
688 }
689
690 =item ut_anything COLUMN
691
692 Untaints arbitrary data.  Be careful.
693
694 =cut
695
696 sub ut_anything {
697   my($self,$field)=@_;
698   $self->getfield($field) =~ /^(.*)$/ or return "Illegal $field!";
699   $self->setfield($field,$1);
700   '';
701 }
702
703 =item fields [ TABLE ]
704
705 This can be used as both a subroutine and a method call.  It returns a list
706 of the columns in this record's table, or an explicitly specified table.
707 (See L<dbdef_table>).
708
709 =cut
710
711 # Usage: @fields = fields($table);
712 #        @fields = $record->fields;
713 sub fields {
714   my $something = shift;
715   my $table;
716   if ( ref($something) ) {
717     $table = $something->table;
718   } else {
719     $table = $something;
720   }
721   #croak "Usage: \@fields = fields(\$table)\n   or: \@fields = \$record->fields" unless $table;
722   my($table_obj) = $dbdef->table($table);
723   croak "Unknown table $table" unless $table_obj;
724   $table_obj->columns;
725 }
726
727 =head1 SUBROUTINES
728
729 =over 4
730
731 =item reload_dbdef([FILENAME])
732
733 Load a database definition (see L<FS::dbdef>), optionally from a non-default
734 filename.  This command is executed at startup unless
735 I<$FS::Record::setup_hack> is true.  Returns a FS::dbdef object.
736
737 =cut
738
739 sub reload_dbdef {
740   my $file = shift || $dbdef_file;
741   $dbdef = load FS::dbdef ($file);
742 }
743
744 =item dbdef
745
746 Returns the current database definition.  See L<FS::dbdef>.
747
748 =cut
749
750 sub dbdef { $dbdef; }
751
752 =item _quote VALUE, TABLE, COLUMN
753
754 This is an internal function used to construct SQL statements.  It returns
755 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
756 type (see L<dbdef_column>) does not end in `char' or `binary'.
757
758 =cut
759
760 sub _quote {
761   my($value,$table,$field)=@_;
762   my($dbh)=dbh;
763   if ( $value =~ /^\d+(\.\d+)?$/ && 
764 #       ! ( datatype($table,$field) =~ /^char/ ) 
765        ! ( $dbdef->table($table)->column($field)->type =~ /(char|binary)$/i ) 
766   ) {
767     $value;
768   } else {
769     $dbh->quote($value);
770   }
771 }
772
773 =item hfields TABLE
774
775 This is depriciated.  Don't use it.
776
777 It returns a hash-type list with the fields of this record's table set true.
778
779 =cut
780
781 sub hfields {
782   carp "warning: hfields is depriciated";
783   my($table)=@_;
784   my(%hash);
785   foreach (fields($table)) {
786     $hash{$_}=1;
787   }
788   \%hash;
789 }
790
791 #sub _dump {
792 #  my($self)=@_;
793 #  join("\n", map {
794 #    "$_: ". $self->getfield($_). "|"
795 #  } (fields($self->table)) );
796 #}
797
798 #sub DESTROY {
799 #  my $self = shift;
800 #  #use Carp qw(cluck);
801 #  #cluck "DESTROYING $self";
802 #  warn "DESTROYING $self";
803 #}
804
805 #sub is_tainted {
806 #             return ! eval { join('',@_), kill 0; 1; };
807 #         }
808
809 =back
810
811 =head1 VERSION
812
813 $Id: Record.pm,v 1.13 1999-03-29 11:55:43 ivan Exp $
814
815 =head1 BUGS
816
817 This module should probably be renamed, since much of the functionality is
818 of general use.  It is not completely unlike Adapter::DBI (see below).
819
820 Exported qsearch and qsearchs should be depriciated in favor of method calls
821 (against an FS::Record object like the old search and searchs that qsearch
822 and qsearchs were on top of.)
823
824 The whole fields / hfields mess should be removed.
825
826 The various WHERE clauses should be subroutined.
827
828 table string should be depriciated in favor of FS::dbdef_table.
829
830 No doubt we could benefit from a Tied hash.  Documenting how exists / defined
831 true maps to the database (and WHERE clauses) would also help.
832
833 The ut_ methods should ask the dbdef for a default length.
834
835 ut_sqltype (like ut_varchar) should all be defined
836
837 A fallback check method should be provided whith uses the dbdef.
838
839 The ut_money method assumes money has two decimal digits.
840
841 The Pg money kludge in the new method only strips `$'.
842
843 The ut_phonen method assumes US-style phone numbers.
844
845 The _quote function should probably use ut_float instead of a regex.
846
847 All the subroutines probably should be methods, here or elsewhere.
848
849 Probably should borrow/use some dbdef methods where appropriate (like sub
850 fields)
851
852 =head1 SEE ALSO
853
854 L<FS::dbdef>, L<FS::UID>, L<DBI>
855
856 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.
857
858 =head1 HISTORY
859
860 ivan@voicenet.com 97-jun-2 - 9, 19, 25, 27, 30
861
862 DBI version
863 ivan@sisd.com 97-nov-8 - 12
864
865 cleaned up, added autoloaded $self->any_field calls, moved DBI login stuff
866 to FS::UID
867 ivan@sisd.com 97-nov-21-23
868
869 since AUTO_INCREMENT is MySQL specific, use my own unique number generator
870 (again)
871 ivan@sisd.com 97-dec-4
872
873 untaint $user in unique (web demo hack...bah)
874 make unique skip multiple-field unique's from dbdef
875 ivan@sisd.com 97-dec-11
876
877 merge with FS::Search, which after all was just alternate constructors for
878 FS::Record objects.  Makes lots of things cleaner.  :)
879 ivan@sisd.com 97-dec-13
880
881 use FS::dbdef::primary key in replace searches, hopefully for all practical 
882 purposes the string/number problem in SQL statements should be gone?
883 (SQL bites)
884 ivan@sisd.com 98-jan-20
885
886 Put all SQL statments in $statment before we $sth=$dbh->prepare( them,
887 for debugging reasons (warn $statement) ivan@sisd.com 98-feb-19
888
889 (sigh)... use dbdef type (char, etc.) instead of a regex to decide
890 what to quote in _quote (more sillines...)  SQL bites.
891 ivan@sisd.com 98-feb-20
892
893 more friendly error messages ivan@sisd.com 98-mar-13
894
895 Added import of datasrc from FS::UID to allow Pg6.3 to work
896 Added code to right-trim strings read from Pg6.3 databases
897 Modified 'add' to only insert fields that actually have data
898 Added ut_float to handle floating point numbers (for sales tax).
899 Pg6.3 does not have a "SHOW FIELDS" statement, so I faked it 8).
900         bmccane@maxbaud.net     98-apr-3
901
902 commented out Pg wrapper around `` Modified 'add' to only insert fields that
903 actually have data '' ivan@sisd.com 98-apr-16
904
905 dbdef usage changes ivan@sisd.com 98-jun-1
906
907 sub fields now asks dbdef, not database ivan@sisd.com 98-jun-2
908
909 added debugging method ->_dump ivan@sisd.com 98-jun-16
910
911 use FS::dbdef::primary key in delete searches as well as replace
912 searches (SQL still bites) ivan@sisd.com 98-jun-22
913
914 sub dbdef_table ivan@sisd.com 98-jun-28
915
916 removed Pg wrapper around `` Modified 'add' to only insert fields that
917 actually have data '' ivan@sisd.com 98-jul-14
918
919 sub fields croaks on errors ivan@sisd.com 98-jul-17
920
921 $rc eq '0E0' doesn't mean we couldn't delete for all rdbmss 
922 ivan@sisd.com 98-jul-18
923
924 commented out code to right-trim strings read from Pg6.3 databases;
925 ChopBlanks is in UID.pm ivan@sisd.com 98-aug-16
926
927 added code (with Pg wrapper) to deal with Pg money fields
928 ivan@sisd.com 98-aug-18
929
930 added pod documentation ivan@sisd.com 98-sep-6
931
932 ut_phonen got ''; at the end ivan@sisd.com 98-sep-27
933
934 $Log: Record.pm,v $
935 Revision 1.13  1999-03-29 11:55:43  ivan
936 eliminate warnings in ut_money
937
938 Revision 1.12  1999/01/25 12:26:06  ivan
939 yet more mod_perl stuff
940
941 Revision 1.11  1999/01/18 09:22:38  ivan
942 changes to track email addresses for email invoicing
943
944 Revision 1.10  1998/12/29 11:59:33  ivan
945 mostly properly OO, some work still to be done with svc_ stuff
946
947 Revision 1.9  1998/11/21 07:26:45  ivan
948 "Records identical" carp tells us it is just a warning.
949
950 Revision 1.8  1998/11/15 11:02:04  ivan
951 bugsquash
952
953 Revision 1.7  1998/11/15 10:56:31  ivan
954 qsearch gets sames "IS NULL" semantics as other WHERE clauses
955
956 Revision 1.6  1998/11/15 05:31:03  ivan
957 bugfix for new config layout
958
959 Revision 1.5  1998/11/13 09:56:51  ivan
960 change configuration file layout to support multiple distinct databases (with
961 own set of config files, export, etc.)
962
963 Revision 1.4  1998/11/10 07:45:25  ivan
964 doc clarification
965
966 Revision 1.2  1998/11/07 05:17:18  ivan
967 In sub new, Pg wrapper for money fields from dbdef (FS::Record::fields $table),
968 not keys of supplied hashref.
969
970
971 =cut
972
973 1;
974