4 use vars qw($dbdef_file $dbdef $setup_hack $AUTOLOAD @ISA @EXPORT_OK $DEBUG);
5 use subs qw(reload_dbdef);
7 use Carp qw(carp cluck croak confess);
10 use DBI qw(:sql_types);
11 use DBIx::DBSchema 0.19;
12 use FS::UID qw(dbh checkruid getotaker datasrc driver_name);
16 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch);
20 #ask FS::UID to run this stuff for us later
21 $FS::UID::callback{'FS::Record'} = sub {
22 $File::CounterFile::DEFAULT_DIR = "/usr/local/etc/freeside/counters.". datasrc;
23 $dbdef_file = "/usr/local/etc/freeside/dbdef.". datasrc;
24 &reload_dbdef unless $setup_hack; #$setup_hack needed now?
29 FS::Record - Database record objects
34 use FS::Record qw(dbh fields qsearch qsearchs dbdef);
36 $record = new FS::Record 'table', \%hash;
37 $record = new FS::Record 'table', { 'column' => 'value', ... };
39 $record = qsearchs FS::Record 'table', \%hash;
40 $record = qsearchs FS::Record 'table', { 'column' => 'value', ... };
41 @records = qsearch FS::Record 'table', \%hash;
42 @records = qsearch FS::Record 'table', { 'column' => 'value', ... };
44 $table = $record->table;
45 $dbdef_table = $record->dbdef_table;
47 $value = $record->get('column');
48 $value = $record->getfield('column');
49 $value = $record->column;
51 $record->set( 'column' => 'value' );
52 $record->setfield( 'column' => 'value' );
53 $record->column('value');
55 %hash = $record->hash;
57 $hashref = $record->hashref;
59 $error = $record->insert;
60 #$error = $record->add; #depriciated
62 $error = $record->delete;
63 #$error = $record->del; #depriciated
65 $error = $new_record->replace($old_record);
66 #$error = $new_record->rep($old_record); #depriciated
68 $value = $record->unique('column');
70 $error = $record->ut_float('column');
71 $error = $record->ut_number('column');
72 $error = $record->ut_numbern('column');
73 $error = $record->ut_money('column');
74 $error = $record->ut_text('column');
75 $error = $record->ut_textn('column');
76 $error = $record->ut_alpha('column');
77 $error = $record->ut_alphan('column');
78 $error = $record->ut_phonen('column');
79 $error = $record->ut_anything('column');
80 $error = $record->ut_name('column');
82 $dbdef = reload_dbdef;
83 $dbdef = reload_dbdef "/non/standard/filename";
86 $quoted_value = _quote($value,'table','field');
89 $fields = hfields('table');
90 if ( $fields->{Field} ) { # etc.
92 @fields = fields 'table'; #as a subroutine
93 @fields = $record->fields; #as a method call
98 (Mostly) object-oriented interface to database records. Records are currently
99 implemented on top of DBI. FS::Record is intended as a base class for
100 table-specific classes to inherit from, i.e. FS::cust_main.
106 =item new [ TABLE, ] HASHREF
108 Creates a new record. It doesn't store it in the database, though. See
109 L<"insert"> for that.
111 Note that the object stores this hash reference, not a distinct copy of the
112 hash it points to. You can ask the object for a copy with the I<hash>
115 TABLE can only be omitted when a dervived class overrides the table method.
121 my $class = ref($proto) || $proto;
123 bless ($self, $class);
125 $self->{'Table'} = shift unless defined ( $self->table );
127 my $hashref = $self->{'Hash'} = shift;
129 foreach my $field ( $self->fields ) {
130 $hashref->{$field}='' unless defined $hashref->{$field};
131 #trim the '$' and ',' from money fields for Pg (belong HERE?)
132 #(what about Pg i18n?)
133 if ( driver_name =~ /^Pg$/i
134 && $self->dbdef_table->column($field)->type eq 'money' ) {
135 ${$hashref}{$field} =~ s/^\$//;
136 ${$hashref}{$field} =~ s/\,//;
140 $self->_cache($hashref, shift) if $self->can('_cache') && @_;
147 my $class = ref($proto) || $proto;
149 bless ($self, $class);
151 $self->{'Table'} = shift unless defined ( $self->table );
153 my $hashref = $self->{'Hash'} = shift;
155 if ( defined( $cache->cache->{$hashref->{$cache->key}} ) ) {
156 my $obj = $cache->cache->{$hashref->{$cache->key}};
157 $obj->_cache($hashref, $cache) if $obj->can('_cache');
160 $cache->cache->{$hashref->{$cache->key}} = $self->new($hashref, $cache);
167 my $class = ref($proto) || $proto;
169 bless ($self, $class);
170 if ( defined $self->table ) {
171 cluck "create constructor is depriciated, use new!";
174 croak "FS::Record::create called (not from a subclass)!";
178 =item qsearch TABLE, HASHREF, SELECT, EXTRA_SQL
180 Searches the database for all records matching (at least) the key/value pairs
181 in HASHREF. Returns all the records found as `FS::TABLE' objects if that
182 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
185 ###oops, argh, FS::Record::new only lets us create database fields.
186 #Normal behaviour if SELECT is not specified is `*', as in
187 #C<SELECT * FROM table WHERE ...>. However, there is an experimental new
188 #feature where you can specify SELECT - remember, the objects returned,
189 #although blessed into the appropriate `FS::TABLE' package, will only have the
190 #fields you specify. This might have unwanted results if you then go calling
191 #regular FS::TABLE methods
197 my($stable, $record, $select, $extra_sql, $cache ) = @_;
198 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
200 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
205 my $table = $cache ? $cache->table : $stable;
207 my @fields = grep exists($record->{$_}), fields($table);
209 my $statement = "SELECT $select FROM $stable";
211 $statement .= ' WHERE '. join(' AND ', map {
212 if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) {
213 if ( driver_name =~ /^Pg$/i ) {
214 qq-( $_ IS NULL OR $_ = '' )-;
216 qq-( $_ IS NULL OR $_ = "" )-;
223 $statement .= " $extra_sql" if defined($extra_sql);
225 warn $statement if $DEBUG;
226 my $sth = $dbh->prepare($statement)
227 or croak "$dbh->errstr doing $statement";
232 grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
234 if ( $record->{$field} =~ /^\d+(\.\d+)?$/
235 && $dbdef->table($table)->column($field)->type =~ /(int)/i
237 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_INTEGER } );
239 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_VARCHAR } );
243 # $sth->execute( map $record->{$_},
244 # grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
245 # ) or croak "Error executing \"$statement\": ". $sth->errstr;
247 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
249 $dbh->commit or croak $dbh->errstr if $FS::UID::AutoCommit;
251 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
252 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
253 #derivied class didn't override new method, so this optimization is safe
256 new_or_cached( "FS::$table", { %{$_} }, $cache )
257 } @{$sth->fetchall_arrayref( {} )};
260 new( "FS::$table", { %{$_} } )
261 } @{$sth->fetchall_arrayref( {} )};
264 warn "untested code (class FS::$table uses custom new method)";
266 eval 'FS::'. $table. '->new( { %{$_} } )';
267 } @{$sth->fetchall_arrayref( {} )};
270 cluck "warning: FS::$table not loaded; returning FS::Record objects";
272 FS::Record->new( $table, { %{$_} } );
273 } @{$sth->fetchall_arrayref( {} )};
278 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
280 Experimental JOINed search method. Using this method, you can execute a
281 single SELECT spanning multiple tables, and cache the results for subsequent
282 method calls. Interface will almost definately change in an incompatible
290 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
291 my $cache = FS::SearchCache->new( $ptable, $pkey );
294 grep { !$saw{$_->getfield($pkey)}++ }
295 qsearch($table, $record, $select, $extra_sql, $cache )
299 =item qsearchs TABLE, HASHREF
301 Same as qsearch, except that if more than one record matches, it B<carp>s but
302 returns the first. If this happens, you either made a logic error in asking
303 for a single item, or your data is corrupted.
307 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
308 my(@result) = qsearch(@_);
309 carp "warning: Multiple records in scalar search!" if scalar(@result) > 1;
310 #should warn more vehemently if the search was on a primary key?
311 scalar(@result) ? ($result[0]) : ();
322 Returns the table name.
327 # cluck "warning: FS::Record::table depriciated; supply one in subclass!";
334 Returns the FS::dbdef_table object for the table.
340 my($table)=$self->table;
341 $dbdef->table($table);
344 =item get, getfield COLUMN
346 Returns the value of the column/field/key COLUMN.
351 my($self,$field) = @_;
352 # to avoid "Use of unitialized value" errors
353 if ( defined ( $self->{Hash}->{$field} ) ) {
354 $self->{Hash}->{$field};
364 =item set, setfield COLUMN, VALUE
366 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
371 my($self,$field,$value) = @_;
372 $self->{'Hash'}->{$field} = $value;
379 =item AUTLOADED METHODS
381 $record->column is a synonym for $record->get('column');
383 $record->column('value') is a synonym for $record->set('column','value');
389 # my($self,$value)=@_;
390 # my($field)=$AUTOLOAD;
392 # if ( defined($value) ) {
393 # confess "errant AUTOLOAD $field for $self (arg $value)"
394 # unless $self->can('setfield');
395 # $self->setfield($field,$value);
397 # confess "errant AUTOLOAD $field for $self (no args)"
398 # unless $self->can('getfield');
399 # $self->getfield($field);
405 my $field = $AUTOLOAD;
407 if ( defined($_[1]) ) {
408 $_[0]->setfield($field, $_[1]);
410 $_[0]->getfield($field);
416 Returns a list of the column/value pairs, usually for assigning to a new hash.
418 To make a distinct duplicate of an FS::Record object, you can do:
420 $new = new FS::Record ( $old->table, { $old->hash } );
426 %{ $self->{'Hash'} };
431 Returns a reference to the column/value hash.
442 Inserts this record to the database. If there is an error, returns the error,
443 otherwise returns false.
450 my $error = $self->check;
451 return $error if $error;
453 #single-field unique keys are given a value if false
454 #(like MySQL's AUTO_INCREMENT)
455 foreach ( $self->dbdef_table->unique->singles ) {
456 $self->unique($_) unless $self->getfield($_);
458 #and also the primary key
459 my $primary_key = $self->dbdef_table->primary_key;
460 $self->unique($primary_key)
461 if $primary_key && ! $self->getfield($primary_key);
464 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
468 my $statement = "INSERT INTO ". $self->table. " ( ".
471 join(', ',map(_quote($self->getfield($_),$self->table,$_), @fields)).
474 my $sth = dbh->prepare($statement) or return dbh->errstr;
476 local $SIG{HUP} = 'IGNORE';
477 local $SIG{INT} = 'IGNORE';
478 local $SIG{QUIT} = 'IGNORE';
479 local $SIG{TERM} = 'IGNORE';
480 local $SIG{TSTP} = 'IGNORE';
481 local $SIG{PIPE} = 'IGNORE';
483 $sth->execute or return $sth->errstr;
484 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
491 Depriciated (use insert instead).
496 cluck "warning: FS::Record::add depriciated!";
497 insert @_; #call method in this scope
502 Delete this record from the database. If there is an error, returns the error,
503 otherwise returns false.
510 my($statement)="DELETE FROM ". $self->table. " WHERE ". join(' AND ',
512 $self->getfield($_) eq ''
513 #? "( $_ IS NULL OR $_ = \"\" )"
514 ? ( driver_name =~ /^Pg$/i
516 : "( $_ IS NULL OR $_ = \"\" )"
518 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
519 } ( $self->dbdef_table->primary_key )
520 ? ( $self->dbdef_table->primary_key)
523 my $sth = dbh->prepare($statement) or return dbh->errstr;
525 local $SIG{HUP} = 'IGNORE';
526 local $SIG{INT} = 'IGNORE';
527 local $SIG{QUIT} = 'IGNORE';
528 local $SIG{TERM} = 'IGNORE';
529 local $SIG{TSTP} = 'IGNORE';
530 local $SIG{PIPE} = 'IGNORE';
532 my $rc = $sth->execute or return $sth->errstr;
533 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
534 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
536 undef $self; #no need to keep object!
543 Depriciated (use delete instead).
548 cluck "warning: FS::Record::del depriciated!";
549 &delete(@_); #call method in this scope
552 =item replace OLD_RECORD
554 Replace the OLD_RECORD with this one in the database. If there is an error,
555 returns the error, otherwise returns false.
560 my ( $new, $old ) = ( shift, shift );
561 warn "[debug][FS::Record] $new ->replace $old\n" if $DEBUG;
563 my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
565 carp "[warning][FS::Record] $new -> replace $old: records identical";
569 return "Records not in same table!" unless $new->table eq $old->table;
571 my $primary_key = $old->dbdef_table->primary_key;
572 return "Can't change $primary_key"
574 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
576 my $error = $new->check;
577 return $error if $error;
579 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
581 "$_ = ". _quote($new->getfield($_),$old->table,$_)
586 $old->getfield($_) eq ''
587 #? "( $_ IS NULL OR $_ = \"\" )"
588 ? ( driver_name =~ /^Pg$/i
590 : "( $_ IS NULL OR $_ = \"\" )"
592 : "$_ = ". _quote($old->getfield($_),$old->table,$_)
593 } ( $primary_key ? ( $primary_key ) : $old->fields )
596 my $sth = dbh->prepare($statement) or return dbh->errstr;
598 local $SIG{HUP} = 'IGNORE';
599 local $SIG{INT} = 'IGNORE';
600 local $SIG{QUIT} = 'IGNORE';
601 local $SIG{TERM} = 'IGNORE';
602 local $SIG{TSTP} = 'IGNORE';
603 local $SIG{PIPE} = 'IGNORE';
605 my $rc = $sth->execute or return $sth->errstr;
606 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
607 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
615 Depriciated (use replace instead).
620 cluck "warning: FS::Record::rep depriciated!";
621 replace @_; #call method in this scope
626 Not yet implemented, croaks. Derived classes should provide a check method.
631 confess "FS::Record::check not implemented; supply one in subclass!";
636 Replaces COLUMN in record with a unique number. Called by the B<add> method
637 on primary keys and single-field unique columns (see L<DBIx::DBSchema::Table>).
638 Returns the new value.
643 my($self,$field) = @_;
644 my($table)=$self->table;
646 croak("&FS::UID::checkruid failed") unless &checkruid;
648 croak "Unique called on field $field, but it is ",
649 $self->getfield($field),
651 if $self->getfield($field);
653 #warn "table $table is tainted" if is_tainted($table);
654 #warn "field $field is tainted" if is_tainted($field);
656 my($counter) = new File::CounterFile "$table.$field",0;
658 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
660 # my($counter) = new File::CounterFile "$user/$table.$field",0;
663 my($index)=$counter->inc;
665 while qsearchs($table,{$field=>$index}); #just in case
670 $self->setfield($field,$index);
674 =item ut_float COLUMN
676 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
677 null. If there is an error, returns the error, otherwise returns false.
682 my($self,$field)=@_ ;
683 ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
684 $self->getfield($field) =~ /^(\d+)$/ ||
685 $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
686 $self->getfield($field) =~ /^(\d+e\d+)$/)
687 or return "Illegal or empty (float) $field: ". $self->getfield($field);
688 $self->setfield($field,$1);
692 =item ut_number COLUMN
694 Check/untaint simple numeric data (whole numbers). May not be null. If there
695 is an error, returns the error, otherwise returns false.
701 $self->getfield($field) =~ /^(\d+)$/
702 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
703 $self->setfield($field,$1);
707 =item ut_numbern COLUMN
709 Check/untaint simple numeric data (whole numbers). May be null. If there is
710 an error, returns the error, otherwise returns false.
716 $self->getfield($field) =~ /^(\d*)$/
717 or return "Illegal (numeric) $field: ". $self->getfield($field);
718 $self->setfield($field,$1);
722 =item ut_money COLUMN
724 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
725 is an error, returns the error, otherwise returns false.
731 $self->setfield($field, 0) if $self->getfield($field) eq '';
732 $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
733 or return "Illegal (money) $field: ". $self->getfield($field);
734 #$self->setfield($field, "$1$2$3" || 0);
735 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
741 Check/untaint text. Alphanumerics, spaces, and the following punctuation
742 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
743 May not be null. If there is an error, returns the error, otherwise returns
750 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]+)$/
751 or return "Illegal or empty (text) $field: ". $self->getfield($field);
752 $self->setfield($field,$1);
756 =item ut_textn COLUMN
758 Check/untaint text. Alphanumerics, spaces, and the following punctuation
759 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
760 May be null. If there is an error, returns the error, otherwise returns false.
766 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]*)$/
767 or return "Illegal (text) $field: ". $self->getfield($field);
768 $self->setfield($field,$1);
772 =item ut_alpha COLUMN
774 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
775 an error, returns the error, otherwise returns false.
781 $self->getfield($field) =~ /^(\w+)$/
782 or return "Illegal or empty (alphanumeric) $field: ".
783 $self->getfield($field);
784 $self->setfield($field,$1);
788 =item ut_alpha COLUMN
790 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
791 error, returns the error, otherwise returns false.
797 $self->getfield($field) =~ /^(\w*)$/
798 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
799 $self->setfield($field,$1);
803 =item ut_phonen COLUMN [ COUNTRY ]
805 Check/untaint phone numbers. May be null. If there is an error, returns
806 the error, otherwise returns false.
808 Takes an optional two-letter ISO country code; without it or with unsupported
809 countries, ut_phonen simply calls ut_alphan.
814 my( $self, $field, $country ) = @_;
815 return $self->ut_alphan($field) unless defined $country;
816 my $phonen = $self->getfield($field);
817 if ( $phonen eq '' ) {
818 $self->setfield($field,'');
819 } elsif ( $country eq 'US' || $country eq 'CA' ) {
821 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
822 or return "Illegal (phone) $field: ". $self->getfield($field);
823 $phonen = "$1-$2-$3";
824 $phonen .= " x$4" if $4;
825 $self->setfield($field,$phonen);
827 warn "warning: don't know how to check phone numbers for country $country";
828 return $self->ut_textn($field);
835 Check/untaint ip addresses. IPv4 only for now.
840 my( $self, $field ) = @_;
841 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
842 or return "Illegal (IP address) $field: ". $self->getfield($field);
843 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
844 $self->setfield($field, "$1.$2.$3.$3");
850 Check/untaint ip addresses. IPv4 only for now. May be null.
855 my( $self, $field ) = @_;
856 if ( $self->getfield($field) =~ /^()$/ ) {
857 $self->setfield($field,'');
860 $self->ut_ip($field);
864 =item ut_domain COLUMN
866 Check/untaint host and domain names.
871 my( $self, $field ) = @_;
872 #$self->getfield($field) =~/^(\w+\.)*\w+$/
873 $self->getfield($field) =~/^(\w+\.)*\w+$/
874 or return "Illegal (domain) $field: ". $self->getfield($field);
875 $self->setfield($field,$1);
881 Check/untaint proper names; allows alphanumerics, spaces and the following
889 my( $self, $field ) = @_;
890 $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
891 or return "Illegal (name) $field: ". $self->getfield($field);
892 $self->setfield($field,$1);
898 Check/untaint zip codes.
903 my( $self, $field, $country ) = @_;
904 if ( $country eq 'US' ) {
905 $self->getfield($field) =~ /\s*(\d{5}(\-\d{4})?)\s*$/
906 or return "Illegal (zip) $field for country $country: ".
907 $self->getfield($field);
908 $self->setfield($field,$1);
910 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
911 or return "Illegal (zip) $field: ". $self->getfield($field);
912 $self->setfield($field,$1);
917 =item ut_country COLUMN
919 Check/untaint country codes. Country names are changed to codes, if possible -
920 see L<Locale::Country>.
925 my( $self, $field ) = @_;
926 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
927 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
928 && country2code($1) ) {
929 $self->setfield($field,uc(country2code($1)));
932 $self->getfield($field) =~ /^(\w\w)$/
933 or return "Illegal (country) $field: ". $self->getfield($field);
934 $self->setfield($field,uc($1));
938 =item ut_anything COLUMN
940 Untaints arbitrary data. Be careful.
945 my( $self, $field ) = @_;
946 $self->getfield($field) =~ /^(.*)$/s
947 or return "Illegal $field: ". $self->getfield($field);
948 $self->setfield($field,$1);
952 =item ut_enum COLUMN CHOICES_ARRAYREF
954 Check/untaint a column, supplying all possible choices, like the "enum" type.
959 my( $self, $field, $choices ) = @_;
960 foreach my $choice ( @$choices ) {
961 if ( $self->getfield($field) eq $choice ) {
962 $self->setfield($choice);
966 return "Illegal (enum) field $field: ". $self->getfield($field);
969 =item fields [ TABLE ]
971 This can be used as both a subroutine and a method call. It returns a list
972 of the columns in this record's table, or an explicitly specified table.
973 (See L<DBIx::DBSchema::Table>).
977 # Usage: @fields = fields($table);
978 # @fields = $record->fields;
980 my $something = shift;
982 if ( ref($something) ) {
983 $table = $something->table;
987 #croak "Usage: \@fields = fields(\$table)\n or: \@fields = \$record->fields" unless $table;
988 my($table_obj) = $dbdef->table($table);
989 confess "Unknown table $table" unless $table_obj;
999 =item reload_dbdef([FILENAME])
1001 Load a database definition (see L<DBIx::DBSchema>), optionally from a
1002 non-default filename. This command is executed at startup unless
1003 I<$FS::Record::setup_hack> is true. Returns a DBIx::DBSchema object.
1008 my $file = shift || $dbdef_file;
1009 $dbdef = load DBIx::DBSchema $file;
1014 Returns the current database definition. See L<FS::dbdef>.
1018 sub dbdef { $dbdef; }
1020 =item _quote VALUE, TABLE, COLUMN
1022 This is an internal function used to construct SQL statements. It returns
1023 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
1024 type (see L<FS::dbdef_column>) does not end in `char' or `binary'.
1029 my($value,$table,$field)=@_;
1031 if ( $value =~ /^\d+(\.\d+)?$/ &&
1032 # ! ( datatype($table,$field) =~ /^char/ )
1033 ! ( $dbdef->table($table)->column($field)->type =~ /(char|binary)$/i )
1037 $dbh->quote($value);
1043 This is depriciated. Don't use it.
1045 It returns a hash-type list with the fields of this record's table set true.
1050 carp "warning: hfields is depriciated";
1053 foreach (fields($table)) {
1062 "$_: ". $self->getfield($_). "|"
1063 } (fields($self->table)) );
1066 sub DESTROY { return; }
1070 # #use Carp qw(cluck);
1071 # #cluck "DESTROYING $self";
1072 # warn "DESTROYING $self";
1076 # return ! eval { join('',@_), kill 0; 1; };
1083 This module should probably be renamed, since much of the functionality is
1084 of general use. It is not completely unlike Adapter::DBI (see below).
1086 Exported qsearch and qsearchs should be depriciated in favor of method calls
1087 (against an FS::Record object like the old search and searchs that qsearch
1088 and qsearchs were on top of.)
1090 The whole fields / hfields mess should be removed.
1092 The various WHERE clauses should be subroutined.
1094 table string should be depriciated in favor of FS::dbdef_table.
1096 No doubt we could benefit from a Tied hash. Documenting how exists / defined
1097 true maps to the database (and WHERE clauses) would also help.
1099 The ut_ methods should ask the dbdef for a default length.
1101 ut_sqltype (like ut_varchar) should all be defined
1103 A fallback check method should be provided which uses the dbdef.
1105 The ut_money method assumes money has two decimal digits.
1107 The Pg money kludge in the new method only strips `$'.
1109 The ut_phonen method only checks US-style phone numbers.
1111 The _quote function should probably use ut_float instead of a regex.
1113 All the subroutines probably should be methods, here or elsewhere.
1115 Probably should borrow/use some dbdef methods where appropriate (like sub
1118 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
1119 or allow it to be set. Working around it is ugly any way around - DBI should
1120 be fixed. (only affects RDBMS which return uppercase column names)
1122 ut_zip should take an optional country like ut_phone.
1126 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
1128 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.