changes backported from 1.2.3 release, bugfix from web demo
[freeside.git] / FS / FS / 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   scalar(@result) ? ($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   confess "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: ". $self->getfield($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: ". $self->getfield($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: ". $self->getfield($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: ". $self->getfield($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: ". $self->getfield($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: ". $self->getfield($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->getfield($field);
662   $self->setfield($field,$1);
663   '';
664 }
665
666 =item ut_alpha COLUMN
667
668 Check/untaint alphanumeric strings (no spaces).  May be null.  If there is an
669 error, returns the error, otherwise returns false.
670
671 =cut
672
673 sub ut_alphan {
674   my($self,$field)=@_;
675   $self->getfield($field) =~ /^(\w*)$/ 
676     or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
677   $self->setfield($field,$1);
678   '';
679 }
680
681 =item ut_phonen COLUMN
682
683 Check/untaint phone numbers.  May be null.  If there is an error, returns
684 the error, otherwise returns false.
685
686 =cut
687
688 sub ut_phonen {
689   my($self,$field)=@_;
690   my $phonen = $self->getfield($field);
691   if ( $phonen eq '' ) {
692     $self->setfield($field,'');
693   } else {
694     $phonen =~ s/\D//g;
695     $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
696       or return "Illegal (phone) $field: ". $self->getfield($field);
697     $phonen = "$1-$2-$3";
698     $phonen .= " x$4" if $4;
699     $self->setfield($field,$phonen);
700   }
701   '';
702 }
703
704 =item ut_anything COLUMN
705
706 Untaints arbitrary data.  Be careful.
707
708 =cut
709
710 sub ut_anything {
711   my($self,$field)=@_;
712   $self->getfield($field) =~ /^(.*)$/
713     or return "Illegal $field: ". $self->getfield($field);
714   $self->setfield($field,$1);
715   '';
716 }
717
718 =item fields [ TABLE ]
719
720 This can be used as both a subroutine and a method call.  It returns a list
721 of the columns in this record's table, or an explicitly specified table.
722 (See L<FS::dbdef_table>).
723
724 =cut
725
726 # Usage: @fields = fields($table);
727 #        @fields = $record->fields;
728 sub fields {
729   my $something = shift;
730   my $table;
731   if ( ref($something) ) {
732     $table = $something->table;
733   } else {
734     $table = $something;
735   }
736   #croak "Usage: \@fields = fields(\$table)\n   or: \@fields = \$record->fields" unless $table;
737   my($table_obj) = $dbdef->table($table);
738   croak "Unknown table $table" unless $table_obj;
739   $table_obj->columns;
740 }
741
742 =head1 SUBROUTINES
743
744 =over 4
745
746 =item reload_dbdef([FILENAME])
747
748 Load a database definition (see L<FS::dbdef>), optionally from a non-default
749 filename.  This command is executed at startup unless
750 I<$FS::Record::setup_hack> is true.  Returns a FS::dbdef object.
751
752 =cut
753
754 sub reload_dbdef {
755   my $file = shift || $dbdef_file;
756   $dbdef = load FS::dbdef ($file);
757 }
758
759 =item dbdef
760
761 Returns the current database definition.  See L<FS::dbdef>.
762
763 =cut
764
765 sub dbdef { $dbdef; }
766
767 =item _quote VALUE, TABLE, COLUMN
768
769 This is an internal function used to construct SQL statements.  It returns
770 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
771 type (see L<FS::dbdef_column>) does not end in `char' or `binary'.
772
773 =cut
774
775 sub _quote {
776   my($value,$table,$field)=@_;
777   my($dbh)=dbh;
778   if ( $value =~ /^\d+(\.\d+)?$/ && 
779 #       ! ( datatype($table,$field) =~ /^char/ ) 
780        ! ( $dbdef->table($table)->column($field)->type =~ /(char|binary)$/i ) 
781   ) {
782     $value;
783   } else {
784     $dbh->quote($value);
785   }
786 }
787
788 =item hfields TABLE
789
790 This is depriciated.  Don't use it.
791
792 It returns a hash-type list with the fields of this record's table set true.
793
794 =cut
795
796 sub hfields {
797   carp "warning: hfields is depriciated";
798   my($table)=@_;
799   my(%hash);
800   foreach (fields($table)) {
801     $hash{$_}=1;
802   }
803   \%hash;
804 }
805
806 #sub _dump {
807 #  my($self)=@_;
808 #  join("\n", map {
809 #    "$_: ". $self->getfield($_). "|"
810 #  } (fields($self->table)) );
811 #}
812
813 #sub DESTROY {
814 #  my $self = shift;
815 #  #use Carp qw(cluck);
816 #  #cluck "DESTROYING $self";
817 #  warn "DESTROYING $self";
818 #}
819
820 #sub is_tainted {
821 #             return ! eval { join('',@_), kill 0; 1; };
822 #         }
823
824 =back
825
826 =head1 VERSION
827
828 $Id: Record.pm,v 1.3 2000-03-03 18:21:38 ivan Exp $
829
830 =head1 BUGS
831
832 This module should probably be renamed, since much of the functionality is
833 of general use.  It is not completely unlike Adapter::DBI (see below).
834
835 Exported qsearch and qsearchs should be depriciated in favor of method calls
836 (against an FS::Record object like the old search and searchs that qsearch
837 and qsearchs were on top of.)
838
839 The whole fields / hfields mess should be removed.
840
841 The various WHERE clauses should be subroutined.
842
843 table string should be depriciated in favor of FS::dbdef_table.
844
845 No doubt we could benefit from a Tied hash.  Documenting how exists / defined
846 true maps to the database (and WHERE clauses) would also help.
847
848 The ut_ methods should ask the dbdef for a default length.
849
850 ut_sqltype (like ut_varchar) should all be defined
851
852 A fallback check method should be provided whith uses the dbdef.
853
854 The ut_money method assumes money has two decimal digits.
855
856 The Pg money kludge in the new method only strips `$'.
857
858 The ut_phonen method assumes US-style phone numbers.
859
860 The _quote function should probably use ut_float instead of a regex.
861
862 All the subroutines probably should be methods, here or elsewhere.
863
864 Probably should borrow/use some dbdef methods where appropriate (like sub
865 fields)
866
867 =head1 SEE ALSO
868
869 L<FS::dbdef>, L<FS::UID>, L<DBI>
870
871 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.
872
873 =cut
874
875 1;
876