4 use vars qw($dbdef_file $dbdef $setup_hack $AUTOLOAD @ISA @EXPORT_OK);
5 use subs qw(reload_dbdef);
7 use Carp qw(carp cluck croak confess);
9 use FS::UID qw(dbh checkruid swapuid getotaker datasrc);
13 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef);
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?
24 FS::Record - Database record objects
29 use FS::Record qw(dbh fields hfields qsearch qsearchs dbdef);
31 $record = new FS::Record 'table', \%hash;
32 $record = new FS::Record 'table', { 'column' => 'value', ... };
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', ... };
39 $table = $record->table;
40 $dbdef_table = $record->dbdef_table;
42 $value = $record->get('column');
43 $value = $record->getfield('column');
44 $value = $record->column;
46 $record->set( 'column' => 'value' );
47 $record->setfield( 'column' => 'value' );
48 $record->column('value');
50 %hash = $record->hash;
52 $hashref = $record->hashref;
54 $error = $record->add;
56 $error = $record->del;
58 $error = $new_record->rep($old_record);
60 $value = $record->unique('column');
62 $value = $record->ut_float('column');
63 $value = $record->ut_number('column');
64 $value = $record->ut_numbern('column');
65 $value = $record->ut_money('column');
66 $value = $record->ut_text('column');
67 $value = $record->ut_textn('column');
68 $value = $record->ut_alpha('column');
69 $value = $record->ut_alphan('column');
70 $value = $record->ut_phonen('column');
71 $value = $record->ut_anythingn('column');
73 $dbdef = reload_dbdef;
74 $dbdef = reload_dbdef "/non/standard/filename";
77 $quoted_value = _quote($value,'table','field');
80 $fields = hfields('table');
81 if ( $fields->{Field} ) { # etc.
83 @fields = fields 'table';
88 (Mostly) object-oriented interface to database records. Records are currently
89 implemented on top of DBI. FS::Record is intended as a base class for
90 table-specific classes to inherit from, i.e. FS::cust_main.
96 =item new TABLE, HASHREF
98 Creates a new record. It doesn't store it in the database, though. See
101 Note that the object stores this hash reference, not a distinct copy of the
102 hash it points to. You can ask the object for a copy with the I<hash>
108 my($proto,$table,$hashref) = @_;
109 confess "Second arguement to FS::Record->new is not a HASH ref: ",
110 ref($hashref), " ", $hashref, "\n"
111 unless ref($hashref) eq 'HASH'; #bad practice?
113 #check to make sure $table exists? (ask dbdef)
115 foreach my $field ( FS::Record::fields $table ) {
116 $hashref->{$field}='' unless defined $hashref->{$field};
119 # mySQL must rtrim the inbound text strings or store them z-terminated
120 # I simulate this for Postgres below
121 # Turned off in favor of ChopBlanks in UID.pm (see man DBI)
122 #if (datasrc =~ m/Pg/)
124 # foreach my $index (keys %$hashref)
126 # $$hashref{$index} = unpack("A255", $$hashref{$index})
127 # if ($$hashref{$index} =~ m/ $/) ;
131 foreach my $column ( FS::Record::fields $table ) {
132 #trim the '$' from money fields for Pg (beong HERE?)
133 #(what about Pg i18n?)
134 if ( datasrc =~ m/Pg/
135 && $dbdef->table($table)->column($column)->type eq 'money' ) {
136 ${$hashref}{$column} =~ s/^\$//;
138 #foreach my $column ( grep $dbdef->table($table)->column($_)->type eq 'money', keys %{$hashref} ) {
139 # ${$hashref}{$column} =~ s/^\$//;
143 my $class = ref($proto) || $proto;
144 my $self = { 'Table' => $table,
148 bless ($self, $class);
152 =item qsearch TABLE, HASHREF
154 Searches the database for all records matching (at least) the key/value pairs
155 in HASHREF. Returns all the records found as `FS::TABLE' objects if that
156 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
161 # Usage: @records = &FS::Search::qsearch($table,\%hash);
162 # Each element of @records is a FS::Record object.
164 my($table,$record) = @_;
167 my(@fields)=grep exists($record->{$_}), fields($table);
170 my($statement) = "SELECT * FROM $table". ( @fields
171 ? " WHERE ". join(' AND ',
172 map("$_ = ". _quote($record->{$_},$table,$_), @fields)
176 $sth=$dbh->prepare($statement)
177 or croak $dbh->errstr; #is that a little too harsh? hmm.
179 if ( eval ' scalar(@FS::'. $table. '::ISA);' ) {
181 eval 'create FS::'. $table. ' ( $sth->fetchrow_hashref );';
182 } ( 1 .. $sth->execute );
184 cluck "qsearch: warning: FS::$table not loaded; returning generic FS::Record objects";
186 new FS::Record ($table,$sth->fetchrow_hashref);
187 } ( 1 .. $sth->execute );
192 =item qsearchs TABLE, HASHREF
194 Same as qsearch, except that if more than one record matches, it B<carp>s but
195 returns the first. If this happens, you either made a logic error in asking
196 for a single item, or your data is corrupted.
200 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
201 my(@result) = qsearch(@_);
202 carp "Multiple records in scalar search!" if scalar(@result) > 1;
203 #should warn more vehemently if the search was on a primary key?
209 Returns the table name.
220 Returns the FS::dbdef_table object for the table.
226 my($table)=$self->table;
227 $dbdef->table($table);
230 =item get, getfield COLUMN
232 Returns the value of the column/field/key COLUMN.
237 my($self,$field) = @_;
238 # to avoid "Use of unitialized value" errors
239 if ( defined ( $self->{Hash}->{$field} ) ) {
240 $self->{Hash}->{$field};
249 =item set, setfield COLUMN, VALUE
251 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
256 my($self,$field,$value) = @_;
257 $self->{'Hash'}->{$field} = $value;
263 =item AUTLOADED METHODS
265 $record->column is a synonym for $record->get('column');
267 $record->column('value') is a synonym for $record->set('column','value');
273 my($field)=$AUTOLOAD;
275 if ( defined($value) ) {
276 $self->setfield($field,$value);
278 $self->getfield($field);
284 Returns a list of the column/value pairs, usually for assigning to a new hash.
286 To make a distinct duplicate of an FS::Record object, you can do:
288 $new = new FS::Record ( $old->table, { $old->hash } );
294 %{ $self->{'Hash'} };
299 Returns a reference to the column/value hash.
310 Adds this record to the database. If there is an error, returns the error,
311 otherwise returns false.
318 my($table)=$self->table;
320 #single-field unique keys are given a value if false
321 #(like MySQL's AUTO_INCREMENT)
322 foreach ( $dbdef->table($table)->unique->singles ) {
323 $self->unique($_) unless $self->getfield($_);
325 #and also the primary key
326 my($primary_key)=$dbdef->table($table)->primary_key;
327 $self->unique($primary_key)
328 if $primary_key && ! $self->getfield($primary_key);
331 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
336 my($statement)="INSERT INTO $table ( ".
339 join(', ',map(_quote($self->getfield($_),$table,$_), @fields)).
342 $sth = $dbh->prepare($statement) or return $dbh->errstr;
344 local $SIG{HUP} = 'IGNORE';
345 local $SIG{INT} = 'IGNORE';
346 local $SIG{QUIT} = 'IGNORE';
347 local $SIG{TERM} = 'IGNORE';
348 local $SIG{TSTP} = 'IGNORE';
350 $sth->execute or return $sth->errstr;
357 Delete this record from the database. If there is an error, returns the error,
358 otherwise returns false.
365 my($table)=$self->table;
368 my($statement)="DELETE FROM $table WHERE ". join(' AND ',
370 $self->getfield($_) eq ''
372 : "$_ = ". _quote($self->getfield($_),$table,$_)
373 } ( $dbdef->table($table)->primary_key )
374 ? ($dbdef->table($table)->primary_key)
377 $sth = $dbh->prepare($statement) or return $dbh->errstr;
379 local $SIG{HUP} = 'IGNORE';
380 local $SIG{INT} = 'IGNORE';
381 local $SIG{QUIT} = 'IGNORE';
382 local $SIG{TERM} = 'IGNORE';
383 local $SIG{TSTP} = 'IGNORE';
386 $rc=$sth->execute or return $sth->errstr;
387 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
389 undef $self; #no need to keep object!
396 Replace the OLD_RECORD with this one in the database. If there is an error,
397 returns the error, otherwise returns false.
404 my($table)=$old->table;
405 my(@fields)=fields($table);
406 my(@diff)=grep $new->getfield($_) ne $old->getfield($_), @fields;
408 if ( scalar(@diff) == 0 ) {
409 carp "Records identical";
413 return "Records not in same table!" unless $new->table eq $table;
416 my($statement)="UPDATE $table SET ". join(', ',
418 "$_ = ". _quote($new->getfield($_),$table,$_)
423 $old->getfield($_) eq ''
425 : "$_ = ". _quote($old->getfield($_),$table,$_)
427 # } ( primary_key($table) ? (primary_key($table)) : @fields )
428 } ( $dbdef->table($table)->primary_key
429 ? ($dbdef->table($table)->primary_key)
435 $sth = $dbh->prepare($statement) or return $dbh->errstr;
437 local $SIG{HUP} = 'IGNORE';
438 local $SIG{INT} = 'IGNORE';
439 local $SIG{QUIT} = 'IGNORE';
440 local $SIG{TERM} = 'IGNORE';
441 local $SIG{TSTP} = 'IGNORE';
444 $rc=$sth->execute or return $sth->errstr;
445 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
453 Replaces COLUMN in record with a unique number. Called by the B<add> method
454 on primary keys and single-field unique columns (see L<FS::dbdef_table>).
455 Returns the new value.
460 my($self,$field) = @_;
461 my($table)=$self->table;
463 croak("&FS::UID::checkruid failed") unless &checkruid;
465 croak "Unique called on field $field, but it is ",
466 $self->getfield($field),
468 if $self->getfield($field);
470 #warn "table $table is tainted" if is_tainted($table);
471 #warn "field $field is tainted" if is_tainted($field);
474 my($counter) = new File::CounterFile "$table.$field",0;
476 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
478 # my($counter) = new File::CounterFile "$user/$table.$field",0;
481 my($index)=$counter->inc;
483 while qsearchs($table,{$field=>$index}); #just in case
489 $self->setfield($field,$index);
493 =item ut_float COLUMN
495 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
496 null. If there is an error, returns the error, otherwise returns false.
501 my($self,$field)=@_ ;
502 ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
503 $self->getfield($field) =~ /^(\d+)$/ ||
504 $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
505 $self->getfield($field) =~ /^(\d+e\d+)$/)
506 or return "Illegal or empty (float) $field!";
507 $self->setfield($field,$1);
511 =item ut_number COLUMN
513 Check/untaint simple numeric data (whole numbers). May not be null. If there
514 is an error, returns the error, otherwise returns false.
520 $self->getfield($field) =~ /^(\d+)$/
521 or return "Illegal or empty (numeric) $field!";
522 $self->setfield($field,$1);
526 =item ut_numbern COLUMN
528 Check/untaint simple numeric data (whole numbers). May be null. If there is
529 an error, returns the error, otherwise returns false.
535 $self->getfield($field) =~ /^(\d*)$/
536 or return "Illegal (numeric) $field!";
537 $self->setfield($field,$1);
541 =item ut_money COLUMN
543 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
544 is an error, returns the error, otherwise returns false.
550 $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
551 or return "Illegal (money) $field!";
552 $self->setfield($field,"$1$2$3" || 0);
558 Check/untaint text. Alphanumerics, spaces, and the following punctuation
559 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
560 May not be null. If there is an error, returns the error, otherwise returns
567 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]+)$/
568 or return "Illegal or empty (text) $field";
569 $self->setfield($field,$1);
573 =item ut_textn COLUMN
575 Check/untaint text. Alphanumerics, spaces, and the following punctuation
576 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
577 May be null. If there is an error, returns the error, otherwise returns false.
583 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]*)$/
584 or return "Illegal (text) $field";
585 $self->setfield($field,$1);
589 =item ut_alpha COLUMN
591 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
592 an error, returns the error, otherwise returns false.
598 $self->getfield($field) =~ /^(\w+)$/
599 or return "Illegal or empty (alphanumeric) $field!";
600 $self->setfield($field,$1);
604 =item ut_alpha COLUMN
606 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
607 error, returns the error, otherwise returns false.
613 $self->getfield($field) =~ /^(\w*)$/
614 or return "Illegal (alphanumeric) $field!";
615 $self->setfield($field,$1);
619 =item ut_phonen COLUMN
621 Check/untaint phone numbers. May be null. If there is an error, returns
622 the error, otherwise returns false.
628 my $phonen = $self->getfield($field);
629 if ( $phonen eq '' ) {
630 $self->setfield($field,'');
633 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
634 or return "Illegal (phone) $field!";
635 $phonen = "$1-$2-$3";
636 $phonen .= " x$4" if $4;
637 $self->setfield($field,$phonen);
642 =item ut_anything COLUMN
644 Untaints arbitrary data. Be careful.
650 $self->getfield($field) =~ /^(.*)$/ or return "Illegal $field!";
651 $self->setfield($field,$1);
660 =item reload_dbdef([FILENAME])
662 Load a database definition (see L<FS::dbdef>), optionally from a non-default
663 filename. This command is executed at startup unless
664 I<$FS::Record::setup_hack> is true. Returns a FS::dbdef object.
669 my $file = shift || $dbdef_file;
670 $dbdef = load FS::dbdef ($file);
675 Returns the current database definition. See L<FS::dbdef>.
679 sub dbdef { $dbdef; }
681 =item _quote VALUE, TABLE, COLUMN
683 This is an internal function used to construct SQL statements. It returns
684 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
685 type (see L<dbdef_column>) does not end in `char' or `binary'.
690 my($value,$table,$field)=@_;
692 if ( $value =~ /^\d+(\.\d+)?$/ &&
693 # ! ( datatype($table,$field) =~ /^char/ )
694 ! ( $dbdef->table($table)->column($field)->type =~ /(char|binary)$/i )
704 This is depriciated. Don't use it.
706 It returns a hash-type list with the fields of this record's table set true.
711 carp "hfields is depriciated";
714 foreach (fields($table)) {
722 This returns a list of the columns in this record's table
723 (See L<dbdef_table>).
727 # Usage: @fields = fields($table);
730 #my(@fields) = $dbdef->table($table)->columns;
731 croak "Usage: \@fields = fields(\$table)" unless $table;
732 my($table_obj) = $dbdef->table($table);
733 croak "Unknown table $table" unless $table_obj;
740 # "$_: ". $self->getfield($_). "|"
741 # } (fields($self->table)) );
746 # #use Carp qw(cluck);
747 # #cluck "DESTROYING $self";
748 # warn "DESTROYING $self";
752 # return ! eval { join('',@_), kill 0; 1; };
759 This module should probably be renamed, since much of the functionality is
760 of general use. It is not completely unlike Adapter::DBI (see below).
762 Exported qsearch and qsearchs should be depriciated in favor of method calls
763 (against an FS::Record object like the old search and searchs that qsearch
764 and qsearchs were on top of.)
766 The whole fields / hfields mess should be removed.
768 The various WHERE clauses should be subroutined.
770 table string should be depriciated in favor of FS::dbdef_table.
772 No doubt we could benefit from a Tied hash. Documenting how exists / defined
773 true maps to the database (and WHERE clauses) would also help.
775 The ut_ methods should ask the dbdef for a default length.
777 ut_sqltype (like ut_varchar) should all be defined
779 A fallback check method should be provided whith uses the dbdef.
781 The ut_money method assumes money has two decimal digits.
783 The Pg money kludge in the new method only strips `$'.
785 The ut_phonen method assumes US-style phone numbers.
787 The _quote function should probably use ut_float instead of a regex.
789 All the subroutines probably should be methods, here or elsewhere.
793 L<FS::dbdef>, L<FS::UID>, L<DBI>
795 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.
799 ivan@voicenet.com 97-jun-2 - 9, 19, 25, 27, 30
802 ivan@sisd.com 97-nov-8 - 12
804 cleaned up, added autoloaded $self->any_field calls, moved DBI login stuff
806 ivan@sisd.com 97-nov-21-23
808 since AUTO_INCREMENT is MySQL specific, use my own unique number generator
810 ivan@sisd.com 97-dec-4
812 untaint $user in unique (web demo hack...bah)
813 make unique skip multiple-field unique's from dbdef
814 ivan@sisd.com 97-dec-11
816 merge with FS::Search, which after all was just alternate constructors for
817 FS::Record objects. Makes lots of things cleaner. :)
818 ivan@sisd.com 97-dec-13
820 use FS::dbdef::primary key in replace searches, hopefully for all practical
821 purposes the string/number problem in SQL statements should be gone?
823 ivan@sisd.com 98-jan-20
825 Put all SQL statments in $statment before we $sth=$dbh->prepare( them,
826 for debugging reasons (warn $statement) ivan@sisd.com 98-feb-19
828 (sigh)... use dbdef type (char, etc.) instead of a regex to decide
829 what to quote in _quote (more sillines...) SQL bites.
830 ivan@sisd.com 98-feb-20
832 more friendly error messages ivan@sisd.com 98-mar-13
834 Added import of datasrc from FS::UID to allow Pg6.3 to work
835 Added code to right-trim strings read from Pg6.3 databases
836 Modified 'add' to only insert fields that actually have data
837 Added ut_float to handle floating point numbers (for sales tax).
838 Pg6.3 does not have a "SHOW FIELDS" statement, so I faked it 8).
839 bmccane@maxbaud.net 98-apr-3
841 commented out Pg wrapper around `` Modified 'add' to only insert fields that
842 actually have data '' ivan@sisd.com 98-apr-16
844 dbdef usage changes ivan@sisd.com 98-jun-1
846 sub fields now asks dbdef, not database ivan@sisd.com 98-jun-2
848 added debugging method ->_dump ivan@sisd.com 98-jun-16
850 use FS::dbdef::primary key in delete searches as well as replace
851 searches (SQL still bites) ivan@sisd.com 98-jun-22
853 sub dbdef_table ivan@sisd.com 98-jun-28
855 removed Pg wrapper around `` Modified 'add' to only insert fields that
856 actually have data '' ivan@sisd.com 98-jul-14
858 sub fields croaks on errors ivan@sisd.com 98-jul-17
860 $rc eq '0E0' doesn't mean we couldn't delete for all rdbmss
861 ivan@sisd.com 98-jul-18
863 commented out code to right-trim strings read from Pg6.3 databases;
864 ChopBlanks is in UID.pm ivan@sisd.com 98-aug-16
866 added code (with Pg wrapper) to deal with Pg money fields
867 ivan@sisd.com 98-aug-18
869 added pod documentation ivan@sisd.com 98-sep-6
871 ut_phonen got ''; at the end ivan@sisd.com 98-sep-27
874 Revision 1.6 1998-11-15 05:31:03 ivan
875 bugfix for new config layout
877 Revision 1.5 1998/11/13 09:56:51 ivan
878 change configuration file layout to support multiple distinct databases (with
879 own set of config files, export, etc.)
881 Revision 1.4 1998/11/10 07:45:25 ivan
884 Revision 1.2 1998/11/07 05:17:18 ivan
885 In sub new, Pg wrapper for money fields from dbdef (FS::Record::fields $table),
886 not keys of supplied hashref.