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