4 use vars qw($dbdef_file $dbdef $setup_hack $AUTOLOAD @ISA @EXPORT_OK);
5 use subs qw(reload_dbdef);
9 use FS::UID qw(dbh checkruid swapuid getotaker datasrc);
13 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef);
15 $File::CounterFile::DEFAULT_DIR = "/var/spool/freeside/counters" ;
17 $dbdef_file = "/var/spool/freeside/dbdef.". datasrc;
19 reload_dbdef unless $setup_hack;
23 FS::Record - Database record objects
28 use FS::Record qw(dbh fields hfields qsearch qsearchs dbdef);
30 $record = new FS::Record 'table', \%hash;
31 $record = new FS::Record 'table', { 'column' => 'value', ... };
33 $record = qsearchs FS::Record 'table', \%hash;
34 $record = qsearchs FS::Record 'table', { 'column' => 'value', ... };
35 @records = qsearch FS::Record 'table', \%hash;
36 @records = qsearch FS::Record 'table', { 'column' => 'value', ... };
38 $table = $record->table;
39 $dbdef_table = $record->dbdef_table;
41 $value = $record->get('column');
42 $value = $record->getfield('column');
43 $value = $record->column;
45 $record->set( 'column' => 'value' );
46 $record->setfield( 'column' => 'value' );
47 $record->column('value');
49 %hash = $record->hash;
51 $hashref = $record->hashref;
53 $error = $record->add;
55 $error = $record->del;
57 $error = $new_record->rep($old_record);
59 $value = $record->unique('column');
61 $value = $record->ut_float('column');
62 $value = $record->ut_number('column');
63 $value = $record->ut_numbern('column');
64 $value = $record->ut_money('column');
65 $value = $record->ut_text('column');
66 $value = $record->ut_textn('column');
67 $value = $record->ut_alpha('column');
68 $value = $record->ut_alphan('column');
69 $value = $record->ut_phonen('column');
70 $value = $record->ut_anythingn('column');
72 $dbdef = reload_dbdef;
73 $dbdef = reload_dbdef "/non/standard/filename";
76 $quoted_value = _quote($value,'table','field');
79 $fields = hfields('table');
80 if ( $fields->{Field} ) { # etc.
82 @fields = fields 'table';
87 (Mostly) object-oriented interface to database records. Records are currently
88 implemented on top of DBI. FS::Record is intended as a base class for
89 table-specific classes to inherit from, i.e. FS::cust_main.
95 =item new TABLE, HASHREF
97 Creates a new record. It doesn't store it in the database, though. See
100 Note that the object stores this hash reference, not a distinct copy of the
101 hash it points to. You can ask the object for a copy with the I<hash>
107 my($proto,$table,$hashref) = @_;
108 confess "Second arguement to FS::Record->new is not a HASH ref: ",
109 ref($hashref), " ", $hashref, "\n"
110 unless ref($hashref) eq 'HASH'; #bad practice?
112 #check to make sure $table exists? (ask dbdef)
114 foreach my $field ( FS::Record::fields $table ) {
115 $hashref->{$field}='' unless defined $hashref->{$field};
118 # mySQL must rtrim the inbound text strings or store them z-terminated
119 # I simulate this for Postgres below
120 # Turned off in favor of ChopBlanks in UID.pm (see man DBI)
121 #if (datasrc =~ m/Pg/)
123 # foreach my $index (keys %$hashref)
125 # $$hashref{$index} = unpack("A255", $$hashref{$index})
126 # if ($$hashref{$index} =~ m/ $/) ;
130 foreach my $column ( FS::Record::fields $table ) {
131 #trim the '$' from money fields for Pg (beong HERE?)
132 #(what about Pg i18n?)
133 if ( datasrc =~ m/Pg/
134 && $dbdef->table($table)->column($column)->type eq 'money' ) {
135 ${$hashref}{$column} =~ s/^\$//;
137 #foreach my $column ( grep $dbdef->table($table)->column($_)->type eq 'money', keys %{$hashref} ) {
138 # ${$hashref}{$column} =~ s/^\$//;
142 my $class = ref($proto) || $proto;
143 my $self = { 'Table' => $table,
147 bless ($self, $class);
151 =item qsearch TABLE, HASHREF
153 Searches the database for all records matching (at least) the key/value pairs
154 in HASHREF. Returns all the records found as objects blessed into
155 `FS::TABLE' if that module is loaded (i.e. via `use FS::cust_main;'), otherwise
156 returns an FS::Record object;
160 # Usage: @records = &FS::Search::qsearch($table,\%hash);
161 # Each element of @records is a FS::Record object.
163 my($table,$record) = @_;
166 my(@fields)=grep exists($record->{$_}), fields($table);
169 my($statement) = "SELECT * FROM $table". ( @fields
170 ? " WHERE ". join(' AND ',
171 map("$_ = ". _quote($record->{$_},$table,$_), @fields)
175 $sth=$dbh->prepare($statement)
176 or croak $dbh->errstr; #is that a little too harsh? hmm.
178 if ( eval ' scalar(@FS::'. $table. '::ISA);' ) {
180 eval 'create FS::'. $table. ' ( $sth->fetchrow_hashref );';
181 } ( 1 .. $sth->execute );
183 carp "qsearch: warning: FS::$table not loaded; returning generic FS::Record objects";
185 new FS::Record ($table,$sth->fetchrow_hashref);
186 } ( 1 .. $sth->execute );
191 =item qsearchs TABLE, HASHREF
193 Same as qsearch, except that if more than one record matches, it B<carp>s but
194 returns the first. If this happens, you either made a logic error in asking
195 for a single item, or your data is corrupted.
199 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
200 my(@result) = qsearch(@_);
201 carp "Multiple records in scalar search!" if scalar(@result) > 1;
202 #should warn more vehemently if the search was on a primary key?
208 Returns the table name.
219 Returns the FS::dbdef_table object for the table.
225 my($table)=$self->table;
226 $dbdef->table($table);
229 =item get, getfield COLUMN
231 Returns the value of the column/field/key COLUMN.
236 my($self,$field) = @_;
237 # to avoid "Use of unitialized value" errors
238 if ( defined ( $self->{Hash}->{$field} ) ) {
239 $self->{Hash}->{$field};
248 =item set, setfield COLUMN, VALUE
250 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
255 my($self,$field,$value) = @_;
256 $self->{'Hash'}->{$field} = $value;
262 =item AUTLOADED METHODS
264 $record->column is a synonym for $record->get('column');
266 $record->column('value') is a synonym for $record->set('column','value');
272 my($field)=$AUTOLOAD;
274 if ( defined($value) ) {
275 $self->setfield($field,$value);
277 $self->getfield($field);
283 Returns a list of the column/value pairs, usually for assigning to a new hash.
285 To make a distinct duplicate of an FS::Record object, you can do:
287 $new = new FS::Record ( $old->table, { $old->hash } );
293 %{ $self->{'Hash'} };
298 Returns a reference to the column/value hash.
309 Adds this record to the database. If there is an error, returns the error,
310 otherwise returns false.
317 my($table)=$self->table;
319 #single-field unique keys are given a value if false
320 #(like MySQL's AUTO_INCREMENT)
321 foreach ( $dbdef->table($table)->unique->singles ) {
322 $self->unique($_) unless $self->getfield($_);
324 #and also the primary key
325 my($primary_key)=$dbdef->table($table)->primary_key;
326 $self->unique($primary_key)
327 if $primary_key && ! $self->getfield($primary_key);
330 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
335 my($statement)="INSERT INTO $table ( ".
338 join(', ',map(_quote($self->getfield($_),$table,$_), @fields)).
341 $sth = $dbh->prepare($statement) or return $dbh->errstr;
343 local $SIG{HUP} = 'IGNORE';
344 local $SIG{INT} = 'IGNORE';
345 local $SIG{QUIT} = 'IGNORE';
346 local $SIG{TERM} = 'IGNORE';
347 local $SIG{TSTP} = 'IGNORE';
349 $sth->execute or return $sth->errstr;
356 Delete this record from the database. If there is an error, returns the error,
357 otherwise returns false.
364 my($table)=$self->table;
367 my($statement)="DELETE FROM $table WHERE ". join(' AND ',
369 $self->getfield($_) eq ''
371 : "$_ = ". _quote($self->getfield($_),$table,$_)
372 } ( $dbdef->table($table)->primary_key )
373 ? ($dbdef->table($table)->primary_key)
376 $sth = $dbh->prepare($statement) or return $dbh->errstr;
378 local $SIG{HUP} = 'IGNORE';
379 local $SIG{INT} = 'IGNORE';
380 local $SIG{QUIT} = 'IGNORE';
381 local $SIG{TERM} = 'IGNORE';
382 local $SIG{TSTP} = 'IGNORE';
385 $rc=$sth->execute or return $sth->errstr;
386 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
388 undef $self; #no need to keep object!
395 Replace the OLD_RECORD with this one in the database. If there is an error,
396 returns the error, otherwise returns false.
403 my($table)=$old->table;
404 my(@fields)=fields($table);
405 my(@diff)=grep $new->getfield($_) ne $old->getfield($_), @fields;
407 if ( scalar(@diff) == 0 ) {
408 carp "Records identical";
412 return "Records not in same table!" unless $new->table eq $table;
415 my($statement)="UPDATE $table SET ". join(', ',
417 "$_ = ". _quote($new->getfield($_),$table,$_)
422 $old->getfield($_) eq ''
424 : "$_ = ". _quote($old->getfield($_),$table,$_)
426 # } ( primary_key($table) ? (primary_key($table)) : @fields )
427 } ( $dbdef->table($table)->primary_key
428 ? ($dbdef->table($table)->primary_key)
434 $sth = $dbh->prepare($statement) or return $dbh->errstr;
436 local $SIG{HUP} = 'IGNORE';
437 local $SIG{INT} = 'IGNORE';
438 local $SIG{QUIT} = 'IGNORE';
439 local $SIG{TERM} = 'IGNORE';
440 local $SIG{TSTP} = 'IGNORE';
443 $rc=$sth->execute or return $sth->errstr;
444 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
452 Replaces COLUMN in record with a unique number. Called by the B<add> method
453 on primary keys and single-field unique columns (see L<FS::dbdef_table>).
454 Returns the new value.
459 my($self,$field) = @_;
460 my($table)=$self->table;
462 croak("&FS::UID::checkruid failed") unless &checkruid;
464 croak "Unique called on field $field, but it is ",
465 $self->getfield($field),
467 if $self->getfield($field);
469 #warn "table $table is tainted" if is_tainted($table);
470 #warn "field $field is tainted" if is_tainted($field);
473 my($counter) = new File::CounterFile "$table.$field",0;
475 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
477 # my($counter) = new File::CounterFile "$user/$table.$field",0;
480 my($index)=$counter->inc;
482 while qsearchs($table,{$field=>$index}); #just in case
488 $self->setfield($field,$index);
492 =item ut_float COLUMN
494 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
495 null. If there is an error, returns the error, otherwise returns false.
500 my($self,$field)=@_ ;
501 ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
502 $self->getfield($field) =~ /^(\d+)$/ ||
503 $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
504 $self->getfield($field) =~ /^(\d+e\d+)$/)
505 or return "Illegal or empty (float) $field!";
506 $self->setfield($field,$1);
510 =item ut_number COLUMN
512 Check/untaint simple numeric data (whole numbers). May not be null. If there
513 is an error, returns the error, otherwise returns false.
519 $self->getfield($field) =~ /^(\d+)$/
520 or return "Illegal or empty (numeric) $field!";
521 $self->setfield($field,$1);
525 =item ut_numbern COLUMN
527 Check/untaint simple numeric data (whole numbers). May be null. If there is
528 an error, returns the error, otherwise returns false.
534 $self->getfield($field) =~ /^(\d*)$/
535 or return "Illegal (numeric) $field!";
536 $self->setfield($field,$1);
540 =item ut_money COLUMN
542 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
543 is an error, returns the error, otherwise returns false.
549 $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
550 or return "Illegal (money) $field!";
551 $self->setfield($field,"$1$2$3" || 0);
557 Check/untaint text. Alphanumerics, spaces, and the following punctuation
558 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
559 May not be null. If there is an error, returns the error, otherwise returns
566 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]+)$/
567 or return "Illegal or empty (text) $field";
568 $self->setfield($field,$1);
572 =item ut_textn COLUMN
574 Check/untaint text. Alphanumerics, spaces, and the following punctuation
575 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
576 May be null. If there is an error, returns the error, otherwise returns false.
582 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]*)$/
583 or return "Illegal (text) $field";
584 $self->setfield($field,$1);
588 =item ut_alpha COLUMN
590 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
591 an error, returns the error, otherwise returns false.
597 $self->getfield($field) =~ /^(\w+)$/
598 or return "Illegal or empty (alphanumeric) $field!";
599 $self->setfield($field,$1);
603 =item ut_alpha COLUMN
605 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
606 error, returns the error, otherwise returns false.
612 $self->getfield($field) =~ /^(\w*)$/
613 or return "Illegal (alphanumeric) $field!";
614 $self->setfield($field,$1);
618 =item ut_phonen COLUMN
620 Check/untaint phone numbers. May be null. If there is an error, returns
621 the error, otherwise returns false.
627 my $phonen = $self->getfield($field);
628 if ( $phonen eq '' ) {
629 $self->setfield($field,'');
632 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
633 or return "Illegal (phone) $field!";
634 $phonen = "$1-$2-$3";
635 $phonen .= " x$4" if $4;
636 $self->setfield($field,$phonen);
641 =item ut_anything COLUMN
643 Untaints arbitrary data. Be careful.
649 $self->getfield($field) =~ /^(.*)$/ or return "Illegal $field!";
650 $self->setfield($field,$1);
659 =item reload_dbdef([FILENAME])
661 Load a database definition (see L<FS::dbdef>), optionally from a non-default
662 filename. This command is executed at startup unless
663 I<$FS::Record::setup_hack> is true. Returns a FS::dbdef object.
668 my $file = shift || $dbdef_file;
669 $dbdef = load FS::dbdef ($file);
674 Returns the current database definition. See L<FS::dbdef>.
678 sub dbdef { $dbdef; }
680 =item _quote VALUE, TABLE, COLUMN
682 This is an internal function used to construct SQL statements. It returns
683 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
684 type (see L<dbdef_column>) does not end in `char' or `binary'.
689 my($value,$table,$field)=@_;
691 if ( $value =~ /^\d+(\.\d+)?$/ &&
692 # ! ( datatype($table,$field) =~ /^char/ )
693 ! ( $dbdef->table($table)->column($field)->type =~ /(char|binary)$/i )
703 This is depriciated. Don't use it.
705 It returns a hash-type list with the fields of this record's table set true.
710 carp "hfields is depriciated";
713 foreach (fields($table)) {
721 This returns a list of the columns in this record's table
722 (See L<dbdef_table>).
726 # Usage: @fields = fields($table);
729 #my(@fields) = $dbdef->table($table)->columns;
730 croak "Usage: \@fields = fields(\$table)" unless $table;
731 my($table_obj) = $dbdef->table($table);
732 croak "Unknown table $table" unless $table_obj;
739 # "$_: ". $self->getfield($_). "|"
740 # } (fields($self->table)) );
745 # #use Carp qw(cluck);
746 # #cluck "DESTROYING $self";
747 # warn "DESTROYING $self";
751 # return ! eval { join('',@_), kill 0; 1; };
758 This module should probably be renamed, since much of the functionality is
759 of general use. It is not completely unlike Adapter::DBI (see below).
761 Exported qsearch and qsearchs should be depriciated in favor of method calls
762 (against an FS::Record object like the old search and searchs that qsearch
763 and qsearchs were on top of.)
765 The whole fields / hfields mess should be removed.
767 The various WHERE clauses should be subroutined.
769 table string should be depriciated in favor of FS::dbdef_table.
771 No doubt we could benefit from a Tied hash. Documenting how exists / defined
772 true maps to the database (and WHERE clauses) would also help.
774 The ut_ methods should ask the dbdef for a default length.
776 ut_sqltype (like ut_varchar) should all be defined
778 A fallback check method should be provided whith uses the dbdef.
780 The ut_money method assumes money has two decimal digits.
782 The Pg money kludge in the new method only strips `$'.
784 The ut_phonen method assumes US-style phone numbers.
786 The _quote function should probably use ut_float instead of a regex.
788 All the subroutines probably should be methods, here or elsewhere.
792 L<FS::dbdef>, L<FS::UID>, L<DBI>
794 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.
798 ivan@voicenet.com 97-jun-2 - 9, 19, 25, 27, 30
801 ivan@sisd.com 97-nov-8 - 12
803 cleaned up, added autoloaded $self->any_field calls, moved DBI login stuff
805 ivan@sisd.com 97-nov-21-23
807 since AUTO_INCREMENT is MySQL specific, use my own unique number generator
809 ivan@sisd.com 97-dec-4
811 untaint $user in unique (web demo hack...bah)
812 make unique skip multiple-field unique's from dbdef
813 ivan@sisd.com 97-dec-11
815 merge with FS::Search, which after all was just alternate constructors for
816 FS::Record objects. Makes lots of things cleaner. :)
817 ivan@sisd.com 97-dec-13
819 use FS::dbdef::primary key in replace searches, hopefully for all practical
820 purposes the string/number problem in SQL statements should be gone?
822 ivan@sisd.com 98-jan-20
824 Put all SQL statments in $statment before we $sth=$dbh->prepare( them,
825 for debugging reasons (warn $statement) ivan@sisd.com 98-feb-19
827 (sigh)... use dbdef type (char, etc.) instead of a regex to decide
828 what to quote in _quote (more sillines...) SQL bites.
829 ivan@sisd.com 98-feb-20
831 more friendly error messages ivan@sisd.com 98-mar-13
833 Added import of datasrc from FS::UID to allow Pg6.3 to work
834 Added code to right-trim strings read from Pg6.3 databases
835 Modified 'add' to only insert fields that actually have data
836 Added ut_float to handle floating point numbers (for sales tax).
837 Pg6.3 does not have a "SHOW FIELDS" statement, so I faked it 8).
838 bmccane@maxbaud.net 98-apr-3
840 commented out Pg wrapper around `` Modified 'add' to only insert fields that
841 actually have data '' ivan@sisd.com 98-apr-16
843 dbdef usage changes ivan@sisd.com 98-jun-1
845 sub fields now asks dbdef, not database ivan@sisd.com 98-jun-2
847 added debugging method ->_dump ivan@sisd.com 98-jun-16
849 use FS::dbdef::primary key in delete searches as well as replace
850 searches (SQL still bites) ivan@sisd.com 98-jun-22
852 sub dbdef_table ivan@sisd.com 98-jun-28
854 removed Pg wrapper around `` Modified 'add' to only insert fields that
855 actually have data '' ivan@sisd.com 98-jul-14
857 sub fields croaks on errors ivan@sisd.com 98-jul-17
859 $rc eq '0E0' doesn't mean we couldn't delete for all rdbmss
860 ivan@sisd.com 98-jul-18
862 commented out code to right-trim strings read from Pg6.3 databases;
863 ChopBlanks is in UID.pm ivan@sisd.com 98-aug-16
865 added code (with Pg wrapper) to deal with Pg money fields
866 ivan@sisd.com 98-aug-18
868 added pod documentation ivan@sisd.com 98-sep-6
870 ut_phonen got ''; at the end ivan@sisd.com 98-sep-27
873 Revision 1.3 1998-11-10 07:44:08 ivan
874 qsearch returns `FS::TABLE' objects if that module is loaded (i.e. via
875 `use FS::cust_main;') instead of always an FS::Record object
877 Revision 1.2 1998/11/07 05:17:18 ivan
878 In sub new, Pg wrapper for money fields from dbdef (FS::Record::fields $table),
879 not keys of supplied hashref.