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 unless defined ( $self->table ) {
126 $self->{'Table'} = shift;
127 carp "warning: FS::Record::new called with table name ". $self->{'Table'};
130 my $hashref = $self->{'Hash'} = shift;
132 foreach my $field ( $self->fields ) {
133 $hashref->{$field}='' unless defined $hashref->{$field};
134 #trim the '$' and ',' from money fields for Pg (belong HERE?)
135 #(what about Pg i18n?)
136 if ( driver_name =~ /^Pg$/i
137 && $self->dbdef_table->column($field)->type eq 'money' ) {
138 ${$hashref}{$field} =~ s/^\$//;
139 ${$hashref}{$field} =~ s/\,//;
143 $self->_cache($hashref, shift) if $self->can('_cache') && @_;
150 my $class = ref($proto) || $proto;
152 bless ($self, $class);
154 $self->{'Table'} = shift unless defined ( $self->table );
156 my $hashref = $self->{'Hash'} = shift;
158 if ( defined( $cache->cache->{$hashref->{$cache->key}} ) ) {
159 my $obj = $cache->cache->{$hashref->{$cache->key}};
160 $obj->_cache($hashref, $cache) if $obj->can('_cache');
163 $cache->cache->{$hashref->{$cache->key}} = $self->new($hashref, $cache);
170 my $class = ref($proto) || $proto;
172 bless ($self, $class);
173 if ( defined $self->table ) {
174 cluck "create constructor is depriciated, use new!";
177 croak "FS::Record::create called (not from a subclass)!";
181 =item qsearch TABLE, HASHREF, SELECT, EXTRA_SQL
183 Searches the database for all records matching (at least) the key/value pairs
184 in HASHREF. Returns all the records found as `FS::TABLE' objects if that
185 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
188 ###oops, argh, FS::Record::new only lets us create database fields.
189 #Normal behaviour if SELECT is not specified is `*', as in
190 #C<SELECT * FROM table WHERE ...>. However, there is an experimental new
191 #feature where you can specify SELECT - remember, the objects returned,
192 #although blessed into the appropriate `FS::TABLE' package, will only have the
193 #fields you specify. This might have unwanted results if you then go calling
194 #regular FS::TABLE methods
200 my($stable, $record, $select, $extra_sql, $cache ) = @_;
201 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
203 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
208 my $table = $cache ? $cache->table : $stable;
210 my @fields = grep exists($record->{$_}), fields($table);
212 my $statement = "SELECT $select FROM $stable";
214 $statement .= ' WHERE '. join(' AND ', map {
215 if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) {
216 if ( driver_name =~ /^Pg$/i ) {
217 qq-( $_ IS NULL OR $_ = '' )-;
219 qq-( $_ IS NULL OR $_ = "" )-;
226 $statement .= " $extra_sql" if defined($extra_sql);
228 warn $statement if $DEBUG;
229 my $sth = $dbh->prepare($statement)
230 or croak "$dbh->errstr doing $statement";
235 grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
237 if ( $record->{$field} =~ /^\d+(\.\d+)?$/
238 && $dbdef->table($table)->column($field)->type =~ /(int)/i
240 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_INTEGER } );
242 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_VARCHAR } );
246 # $sth->execute( map $record->{$_},
247 # grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
248 # ) or croak "Error executing \"$statement\": ". $sth->errstr;
250 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
252 $dbh->commit or croak $dbh->errstr if $FS::UID::AutoCommit;
254 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
255 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
256 #derivied class didn't override new method, so this optimization is safe
259 new_or_cached( "FS::$table", { %{$_} }, $cache )
260 } @{$sth->fetchall_arrayref( {} )};
263 new( "FS::$table", { %{$_} } )
264 } @{$sth->fetchall_arrayref( {} )};
267 warn "untested code (class FS::$table uses custom new method)";
269 eval 'FS::'. $table. '->new( { %{$_} } )';
270 } @{$sth->fetchall_arrayref( {} )};
273 cluck "warning: FS::$table not loaded; returning FS::Record objects";
275 FS::Record->new( $table, { %{$_} } );
276 } @{$sth->fetchall_arrayref( {} )};
281 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
283 Experimental JOINed search method. Using this method, you can execute a
284 single SELECT spanning multiple tables, and cache the results for subsequent
285 method calls. Interface will almost definately change in an incompatible
293 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
294 my $cache = FS::SearchCache->new( $ptable, $pkey );
297 grep { !$saw{$_->getfield($pkey)}++ }
298 qsearch($table, $record, $select, $extra_sql, $cache )
302 =item qsearchs TABLE, HASHREF
304 Same as qsearch, except that if more than one record matches, it B<carp>s but
305 returns the first. If this happens, you either made a logic error in asking
306 for a single item, or your data is corrupted.
310 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
311 my(@result) = qsearch(@_);
312 carp "warning: Multiple records in scalar search!" if scalar(@result) > 1;
313 #should warn more vehemently if the search was on a primary key?
314 scalar(@result) ? ($result[0]) : ();
325 Returns the table name.
330 # cluck "warning: FS::Record::table depriciated; supply one in subclass!";
337 Returns the FS::dbdef_table object for the table.
343 my($table)=$self->table;
344 $dbdef->table($table);
347 =item get, getfield COLUMN
349 Returns the value of the column/field/key COLUMN.
354 my($self,$field) = @_;
355 # to avoid "Use of unitialized value" errors
356 if ( defined ( $self->{Hash}->{$field} ) ) {
357 $self->{Hash}->{$field};
367 =item set, setfield COLUMN, VALUE
369 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
374 my($self,$field,$value) = @_;
375 $self->{'Hash'}->{$field} = $value;
382 =item AUTLOADED METHODS
384 $record->column is a synonym for $record->get('column');
386 $record->column('value') is a synonym for $record->set('column','value');
392 # my($self,$value)=@_;
393 # my($field)=$AUTOLOAD;
395 # if ( defined($value) ) {
396 # confess "errant AUTOLOAD $field for $self (arg $value)"
397 # unless $self->can('setfield');
398 # $self->setfield($field,$value);
400 # confess "errant AUTOLOAD $field for $self (no args)"
401 # unless $self->can('getfield');
402 # $self->getfield($field);
408 my $field = $AUTOLOAD;
410 if ( defined($_[1]) ) {
411 $_[0]->setfield($field, $_[1]);
413 $_[0]->getfield($field);
419 Returns a list of the column/value pairs, usually for assigning to a new hash.
421 To make a distinct duplicate of an FS::Record object, you can do:
423 $new = new FS::Record ( $old->table, { $old->hash } );
429 %{ $self->{'Hash'} };
434 Returns a reference to the column/value hash.
445 Inserts this record to the database. If there is an error, returns the error,
446 otherwise returns false.
453 my $error = $self->check;
454 return $error if $error;
456 #single-field unique keys are given a value if false
457 #(like MySQL's AUTO_INCREMENT)
458 foreach ( $self->dbdef_table->unique->singles ) {
459 $self->unique($_) unless $self->getfield($_);
461 #and also the primary key
462 my $primary_key = $self->dbdef_table->primary_key;
463 $self->unique($primary_key)
464 if $primary_key && ! $self->getfield($primary_key);
467 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
471 my $statement = "INSERT INTO ". $self->table. " ( ".
474 join(', ',map(_quote($self->getfield($_),$self->table,$_), @fields)).
477 my $sth = dbh->prepare($statement) or return dbh->errstr;
479 local $SIG{HUP} = 'IGNORE';
480 local $SIG{INT} = 'IGNORE';
481 local $SIG{QUIT} = 'IGNORE';
482 local $SIG{TERM} = 'IGNORE';
483 local $SIG{TSTP} = 'IGNORE';
484 local $SIG{PIPE} = 'IGNORE';
486 $sth->execute or return $sth->errstr;
487 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
494 Depriciated (use insert instead).
499 cluck "warning: FS::Record::add depriciated!";
500 insert @_; #call method in this scope
505 Delete this record from the database. If there is an error, returns the error,
506 otherwise returns false.
513 my($statement)="DELETE FROM ". $self->table. " WHERE ". join(' AND ',
515 $self->getfield($_) eq ''
516 #? "( $_ IS NULL OR $_ = \"\" )"
517 ? ( driver_name =~ /^Pg$/i
519 : "( $_ IS NULL OR $_ = \"\" )"
521 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
522 } ( $self->dbdef_table->primary_key )
523 ? ( $self->dbdef_table->primary_key)
526 my $sth = dbh->prepare($statement) or return dbh->errstr;
528 local $SIG{HUP} = 'IGNORE';
529 local $SIG{INT} = 'IGNORE';
530 local $SIG{QUIT} = 'IGNORE';
531 local $SIG{TERM} = 'IGNORE';
532 local $SIG{TSTP} = 'IGNORE';
533 local $SIG{PIPE} = 'IGNORE';
535 my $rc = $sth->execute or return $sth->errstr;
536 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
537 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
539 undef $self; #no need to keep object!
546 Depriciated (use delete instead).
551 cluck "warning: FS::Record::del depriciated!";
552 &delete(@_); #call method in this scope
555 =item replace OLD_RECORD
557 Replace the OLD_RECORD with this one in the database. If there is an error,
558 returns the error, otherwise returns false.
563 my ( $new, $old ) = ( shift, shift );
564 warn "[debug][FS::Record] $new ->replace $old\n" if $DEBUG;
566 my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
568 carp "[warning][FS::Record] $new -> replace $old: records identical";
572 return "Records not in same table!" unless $new->table eq $old->table;
574 my $primary_key = $old->dbdef_table->primary_key;
575 return "Can't change $primary_key"
577 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
579 my $error = $new->check;
580 return $error if $error;
582 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
584 "$_ = ". _quote($new->getfield($_),$old->table,$_)
589 $old->getfield($_) eq ''
590 #? "( $_ IS NULL OR $_ = \"\" )"
591 ? ( driver_name =~ /^Pg$/i
593 : "( $_ IS NULL OR $_ = \"\" )"
595 : "$_ = ". _quote($old->getfield($_),$old->table,$_)
596 } ( $primary_key ? ( $primary_key ) : $old->fields )
599 my $sth = dbh->prepare($statement) or return dbh->errstr;
601 local $SIG{HUP} = 'IGNORE';
602 local $SIG{INT} = 'IGNORE';
603 local $SIG{QUIT} = 'IGNORE';
604 local $SIG{TERM} = 'IGNORE';
605 local $SIG{TSTP} = 'IGNORE';
606 local $SIG{PIPE} = 'IGNORE';
608 my $rc = $sth->execute or return $sth->errstr;
609 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
610 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
618 Depriciated (use replace instead).
623 cluck "warning: FS::Record::rep depriciated!";
624 replace @_; #call method in this scope
629 Not yet implemented, croaks. Derived classes should provide a check method.
634 confess "FS::Record::check not implemented; supply one in subclass!";
639 Replaces COLUMN in record with a unique number. Called by the B<add> method
640 on primary keys and single-field unique columns (see L<DBIx::DBSchema::Table>).
641 Returns the new value.
646 my($self,$field) = @_;
647 my($table)=$self->table;
649 croak("&FS::UID::checkruid failed") unless &checkruid;
651 croak "Unique called on field $field, but it is ",
652 $self->getfield($field),
654 if $self->getfield($field);
656 #warn "table $table is tainted" if is_tainted($table);
657 #warn "field $field is tainted" if is_tainted($field);
659 my($counter) = new File::CounterFile "$table.$field",0;
661 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
663 # my($counter) = new File::CounterFile "$user/$table.$field",0;
666 my($index)=$counter->inc;
668 while qsearchs($table,{$field=>$index}); #just in case
673 $self->setfield($field,$index);
677 =item ut_float COLUMN
679 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
680 null. If there is an error, returns the error, otherwise returns false.
685 my($self,$field)=@_ ;
686 ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
687 $self->getfield($field) =~ /^(\d+)$/ ||
688 $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
689 $self->getfield($field) =~ /^(\d+e\d+)$/)
690 or return "Illegal or empty (float) $field: ". $self->getfield($field);
691 $self->setfield($field,$1);
695 =item ut_number COLUMN
697 Check/untaint simple numeric data (whole numbers). May not be null. If there
698 is an error, returns the error, otherwise returns false.
704 $self->getfield($field) =~ /^(\d+)$/
705 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
706 $self->setfield($field,$1);
710 =item ut_numbern COLUMN
712 Check/untaint simple numeric data (whole numbers). May be null. If there is
713 an error, returns the error, otherwise returns false.
719 $self->getfield($field) =~ /^(\d*)$/
720 or return "Illegal (numeric) $field: ". $self->getfield($field);
721 $self->setfield($field,$1);
725 =item ut_money COLUMN
727 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
728 is an error, returns the error, otherwise returns false.
734 $self->setfield($field, 0) if $self->getfield($field) eq '';
735 $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
736 or return "Illegal (money) $field: ". $self->getfield($field);
737 #$self->setfield($field, "$1$2$3" || 0);
738 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
744 Check/untaint text. Alphanumerics, spaces, and the following punctuation
745 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
746 May not be null. If there is an error, returns the error, otherwise returns
753 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]+)$/
754 or return "Illegal or empty (text) $field: ". $self->getfield($field);
755 $self->setfield($field,$1);
759 =item ut_textn COLUMN
761 Check/untaint text. Alphanumerics, spaces, and the following punctuation
762 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
763 May be null. If there is an error, returns the error, otherwise returns false.
769 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]*)$/
770 or return "Illegal (text) $field: ". $self->getfield($field);
771 $self->setfield($field,$1);
775 =item ut_alpha COLUMN
777 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
778 an error, returns the error, otherwise returns false.
784 $self->getfield($field) =~ /^(\w+)$/
785 or return "Illegal or empty (alphanumeric) $field: ".
786 $self->getfield($field);
787 $self->setfield($field,$1);
791 =item ut_alpha COLUMN
793 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
794 error, returns the error, otherwise returns false.
800 $self->getfield($field) =~ /^(\w*)$/
801 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
802 $self->setfield($field,$1);
806 =item ut_phonen COLUMN [ COUNTRY ]
808 Check/untaint phone numbers. May be null. If there is an error, returns
809 the error, otherwise returns false.
811 Takes an optional two-letter ISO country code; without it or with unsupported
812 countries, ut_phonen simply calls ut_alphan.
817 my( $self, $field, $country ) = @_;
818 return $self->ut_alphan($field) unless defined $country;
819 my $phonen = $self->getfield($field);
820 if ( $phonen eq '' ) {
821 $self->setfield($field,'');
822 } elsif ( $country eq 'US' || $country eq 'CA' ) {
824 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
825 or return "Illegal (phone) $field: ". $self->getfield($field);
826 $phonen = "$1-$2-$3";
827 $phonen .= " x$4" if $4;
828 $self->setfield($field,$phonen);
830 warn "warning: don't know how to check phone numbers for country $country";
831 return $self->ut_textn($field);
838 Check/untaint ip addresses. IPv4 only for now.
843 my( $self, $field ) = @_;
844 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
845 or return "Illegal (IP address) $field: ". $self->getfield($field);
846 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
847 $self->setfield($field, "$1.$2.$3.$3");
853 Check/untaint ip addresses. IPv4 only for now. May be null.
858 my( $self, $field ) = @_;
859 if ( $self->getfield($field) =~ /^()$/ ) {
860 $self->setfield($field,'');
863 $self->ut_ip($field);
867 =item ut_domain COLUMN
869 Check/untaint host and domain names.
874 my( $self, $field ) = @_;
875 #$self->getfield($field) =~/^(\w+\.)*\w+$/
876 $self->getfield($field) =~/^(\w+\.)*\w+$/
877 or return "Illegal (domain) $field: ". $self->getfield($field);
878 $self->setfield($field,$1);
884 Check/untaint proper names; allows alphanumerics, spaces and the following
892 my( $self, $field ) = @_;
893 $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
894 or return "Illegal (name) $field: ". $self->getfield($field);
895 $self->setfield($field,$1);
901 Check/untaint zip codes.
906 my( $self, $field, $country ) = @_;
907 if ( $country eq 'US' ) {
908 $self->getfield($field) =~ /\s*(\d{5}(\-\d{4})?)\s*$/
909 or return "Illegal (zip) $field for country $country: ".
910 $self->getfield($field);
911 $self->setfield($field,$1);
913 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
914 or return "Illegal (zip) $field: ". $self->getfield($field);
915 $self->setfield($field,$1);
920 =item ut_country COLUMN
922 Check/untaint country codes. Country names are changed to codes, if possible -
923 see L<Locale::Country>.
928 my( $self, $field ) = @_;
929 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
930 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
931 && country2code($1) ) {
932 $self->setfield($field,uc(country2code($1)));
935 $self->getfield($field) =~ /^(\w\w)$/
936 or return "Illegal (country) $field: ". $self->getfield($field);
937 $self->setfield($field,uc($1));
941 =item ut_anything COLUMN
943 Untaints arbitrary data. Be careful.
948 my( $self, $field ) = @_;
949 $self->getfield($field) =~ /^(.*)$/s
950 or return "Illegal $field: ". $self->getfield($field);
951 $self->setfield($field,$1);
955 =item ut_enum COLUMN CHOICES_ARRAYREF
957 Check/untaint a column, supplying all possible choices, like the "enum" type.
962 my( $self, $field, $choices ) = @_;
963 foreach my $choice ( @$choices ) {
964 if ( $self->getfield($field) eq $choice ) {
965 $self->setfield($choice);
969 return "Illegal (enum) field $field: ". $self->getfield($field);
972 =item fields [ TABLE ]
974 This can be used as both a subroutine and a method call. It returns a list
975 of the columns in this record's table, or an explicitly specified table.
976 (See L<DBIx::DBSchema::Table>).
980 # Usage: @fields = fields($table);
981 # @fields = $record->fields;
983 my $something = shift;
985 if ( ref($something) ) {
986 $table = $something->table;
990 #croak "Usage: \@fields = fields(\$table)\n or: \@fields = \$record->fields" unless $table;
991 my($table_obj) = $dbdef->table($table);
992 confess "Unknown table $table" unless $table_obj;
1002 =item reload_dbdef([FILENAME])
1004 Load a database definition (see L<DBIx::DBSchema>), optionally from a
1005 non-default filename. This command is executed at startup unless
1006 I<$FS::Record::setup_hack> is true. Returns a DBIx::DBSchema object.
1011 my $file = shift || $dbdef_file;
1012 $dbdef = load DBIx::DBSchema $file;
1017 Returns the current database definition. See L<FS::dbdef>.
1021 sub dbdef { $dbdef; }
1023 =item _quote VALUE, TABLE, COLUMN
1025 This is an internal function used to construct SQL statements. It returns
1026 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
1027 type (see L<FS::dbdef_column>) does not end in `char' or `binary'.
1032 my($value,$table,$field)=@_;
1034 if ( $value =~ /^\d+(\.\d+)?$/ &&
1035 # ! ( datatype($table,$field) =~ /^char/ )
1036 ! ( $dbdef->table($table)->column($field)->type =~ /(char|binary)$/i )
1040 $dbh->quote($value);
1046 This is depriciated. Don't use it.
1048 It returns a hash-type list with the fields of this record's table set true.
1053 carp "warning: hfields is depriciated";
1056 foreach (fields($table)) {
1065 "$_: ". $self->getfield($_). "|"
1066 } (fields($self->table)) );
1069 sub DESTROY { return; }
1073 # #use Carp qw(cluck);
1074 # #cluck "DESTROYING $self";
1075 # warn "DESTROYING $self";
1079 # return ! eval { join('',@_), kill 0; 1; };
1086 This module should probably be renamed, since much of the functionality is
1087 of general use. It is not completely unlike Adapter::DBI (see below).
1089 Exported qsearch and qsearchs should be depriciated in favor of method calls
1090 (against an FS::Record object like the old search and searchs that qsearch
1091 and qsearchs were on top of.)
1093 The whole fields / hfields mess should be removed.
1095 The various WHERE clauses should be subroutined.
1097 table string should be depriciated in favor of FS::dbdef_table.
1099 No doubt we could benefit from a Tied hash. Documenting how exists / defined
1100 true maps to the database (and WHERE clauses) would also help.
1102 The ut_ methods should ask the dbdef for a default length.
1104 ut_sqltype (like ut_varchar) should all be defined
1106 A fallback check method should be provided which uses the dbdef.
1108 The ut_money method assumes money has two decimal digits.
1110 The Pg money kludge in the new method only strips `$'.
1112 The ut_phonen method only checks US-style phone numbers.
1114 The _quote function should probably use ut_float instead of a regex.
1116 All the subroutines probably should be methods, here or elsewhere.
1118 Probably should borrow/use some dbdef methods where appropriate (like sub
1121 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
1122 or allow it to be set. Working around it is ugly any way around - DBI should
1123 be fixed. (only affects RDBMS which return uppercase column names)
1125 ut_zip should take an optional country like ut_phone.
1129 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
1131 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.