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