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