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 DBIx::DBSchema 0.19;
11 use FS::UID qw(dbh checkruid getotaker datasrc driver_name);
15 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch);
19 #ask FS::UID to run this stuff for us later
20 $FS::UID::callback{'FS::Record'} = sub {
21 $File::CounterFile::DEFAULT_DIR = "/usr/local/etc/freeside/counters.". datasrc;
22 $dbdef_file = "/usr/local/etc/freeside/dbdef.". datasrc;
23 &reload_dbdef unless $setup_hack; #$setup_hack needed now?
28 FS::Record - Database record objects
33 use FS::Record qw(dbh fields qsearch qsearchs dbdef);
35 $record = new FS::Record 'table', \%hash;
36 $record = new FS::Record 'table', { 'column' => 'value', ... };
38 $record = qsearchs FS::Record 'table', \%hash;
39 $record = qsearchs FS::Record 'table', { 'column' => 'value', ... };
40 @records = qsearch FS::Record 'table', \%hash;
41 @records = qsearch FS::Record 'table', { 'column' => 'value', ... };
43 $table = $record->table;
44 $dbdef_table = $record->dbdef_table;
46 $value = $record->get('column');
47 $value = $record->getfield('column');
48 $value = $record->column;
50 $record->set( 'column' => 'value' );
51 $record->setfield( 'column' => 'value' );
52 $record->column('value');
54 %hash = $record->hash;
56 $hashref = $record->hashref;
58 $error = $record->insert;
59 #$error = $record->add; #depriciated
61 $error = $record->delete;
62 #$error = $record->del; #depriciated
64 $error = $new_record->replace($old_record);
65 #$error = $new_record->rep($old_record); #depriciated
67 $value = $record->unique('column');
69 $error = $record->ut_float('column');
70 $error = $record->ut_number('column');
71 $error = $record->ut_numbern('column');
72 $error = $record->ut_money('column');
73 $error = $record->ut_text('column');
74 $error = $record->ut_textn('column');
75 $error = $record->ut_alpha('column');
76 $error = $record->ut_alphan('column');
77 $error = $record->ut_phonen('column');
78 $error = $record->ut_anything('column');
79 $error = $record->ut_name('column');
81 $dbdef = reload_dbdef;
82 $dbdef = reload_dbdef "/non/standard/filename";
85 $quoted_value = _quote($value,'table','field');
88 $fields = hfields('table');
89 if ( $fields->{Field} ) { # etc.
91 @fields = fields 'table'; #as a subroutine
92 @fields = $record->fields; #as a method call
97 (Mostly) object-oriented interface to database records. Records are currently
98 implemented on top of DBI. FS::Record is intended as a base class for
99 table-specific classes to inherit from, i.e. FS::cust_main.
105 =item new [ TABLE, ] HASHREF
107 Creates a new record. It doesn't store it in the database, though. See
108 L<"insert"> for that.
110 Note that the object stores this hash reference, not a distinct copy of the
111 hash it points to. You can ask the object for a copy with the I<hash>
114 TABLE can only be omitted when a dervived class overrides the table method.
120 my $class = ref($proto) || $proto;
122 bless ($self, $class);
124 $self->{'Table'} = shift unless defined ( $self->table );
126 my $hashref = $self->{'Hash'} = shift;
128 foreach my $field ( $self->fields ) {
129 $hashref->{$field}='' unless defined $hashref->{$field};
130 #trim the '$' and ',' from money fields for Pg (belong HERE?)
131 #(what about Pg i18n?)
132 if ( driver_name =~ /^Pg$/i
133 && $self->dbdef_table->column($field)->type eq 'money' ) {
134 ${$hashref}{$field} =~ s/^\$//;
135 ${$hashref}{$field} =~ s/\,//;
139 $self->_cache($hashref, shift) if $self->can('_cache') && @_;
146 my $class = ref($proto) || $proto;
148 bless ($self, $class);
150 $self->{'Table'} = shift unless defined ( $self->table );
152 my $hashref = $self->{'Hash'} = shift;
154 if ( defined( $cache->cache->{$hashref->{$cache->key}} ) ) {
155 my $obj = $cache->cache->{$hashref->{$cache->key}};
156 $obj->_cache($hashref, $cache) if $obj->can('_cache');
159 $cache->cache->{$hashref->{$cache->key}} = $self->new($hashref, $cache);
166 my $class = ref($proto) || $proto;
168 bless ($self, $class);
169 if ( defined $self->table ) {
170 cluck "create constructor is depriciated, use new!";
173 croak "FS::Record::create called (not from a subclass)!";
177 =item qsearch TABLE, HASHREF, SELECT, EXTRA_SQL
179 Searches the database for all records matching (at least) the key/value pairs
180 in HASHREF. Returns all the records found as `FS::TABLE' objects if that
181 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
184 ###oops, argh, FS::Record::new only lets us create database fields.
185 #Normal behaviour if SELECT is not specified is `*', as in
186 #C<SELECT * FROM table WHERE ...>. However, there is an experimental new
187 #feature where you can specify SELECT - remember, the objects returned,
188 #although blessed into the appropriate `FS::TABLE' package, will only have the
189 #fields you specify. This might have unwanted results if you then go calling
190 #regular FS::TABLE methods
196 my($stable, $record, $select, $extra_sql, $cache ) = @_;
197 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
199 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
204 my $table = $cache ? $cache->table : $stable;
206 my @fields = grep exists($record->{$_}), fields($table);
208 my $statement = "SELECT $select FROM $stable";
210 $statement .= ' WHERE '. join(' AND ', map {
211 if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) {
212 if ( driver_name =~ /^Pg$/i ) {
215 qq-( $_ IS NULL OR $_ = "" )-;
222 $statement .= " $extra_sql" if defined($extra_sql);
224 warn $statement if $DEBUG;
225 my $sth = $dbh->prepare($statement)
226 or croak "$dbh->errstr doing $statement";
228 $sth->execute( map $record->{$_},
229 grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
230 ) or croak "Error executing \"$statement\": ". $dbh->errstr;
231 $dbh->commit or croak $dbh->errstr if $FS::UID::AutoCommit;
233 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
234 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
235 #derivied class didn't override new method, so this optimization is safe
238 new_or_cached( "FS::$table", { %{$_} }, $cache )
239 } @{$sth->fetchall_arrayref( {} )};
242 new( "FS::$table", { %{$_} } )
243 } @{$sth->fetchall_arrayref( {} )};
246 warn "untested code (class FS::$table uses custom new method)";
248 eval 'FS::'. $table. '->new( { %{$_} } )';
249 } @{$sth->fetchall_arrayref( {} )};
252 cluck "warning: FS::$table not loaded; returning FS::Record objects";
254 FS::Record->new( $table, { %{$_} } );
255 } @{$sth->fetchall_arrayref( {} )};
262 Experimental JOINed search method. Using this method, you can execute a
263 single SELECT spanning multiple tables, and cache the results for subsequent
264 method calls. Interface will almost definately change in an incompatible
270 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
271 my $cache = FS::SearchCache->new( $ptable, $pkey );
274 grep { !$saw{$_->getfield($pkey)}++ }
275 qsearch($table, $record, $select, $extra_sql, $cache )
279 =item qsearchs TABLE, HASHREF
281 Same as qsearch, except that if more than one record matches, it B<carp>s but
282 returns the first. If this happens, you either made a logic error in asking
283 for a single item, or your data is corrupted.
287 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
288 my(@result) = qsearch(@_);
289 carp "warning: Multiple records in scalar search!" if scalar(@result) > 1;
290 #should warn more vehemently if the search was on a primary key?
291 scalar(@result) ? ($result[0]) : ();
302 Returns the table name.
307 # cluck "warning: FS::Record::table depriciated; supply one in subclass!";
314 Returns the FS::dbdef_table object for the table.
320 my($table)=$self->table;
321 $dbdef->table($table);
324 =item get, getfield COLUMN
326 Returns the value of the column/field/key COLUMN.
331 my($self,$field) = @_;
332 # to avoid "Use of unitialized value" errors
333 if ( defined ( $self->{Hash}->{$field} ) ) {
334 $self->{Hash}->{$field};
344 =item set, setfield COLUMN, VALUE
346 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
351 my($self,$field,$value) = @_;
352 $self->{'Hash'}->{$field} = $value;
359 =item AUTLOADED METHODS
361 $record->column is a synonym for $record->get('column');
363 $record->column('value') is a synonym for $record->set('column','value');
369 # my($self,$value)=@_;
370 # my($field)=$AUTOLOAD;
372 # if ( defined($value) ) {
373 # confess "errant AUTOLOAD $field for $self (arg $value)"
374 # unless $self->can('setfield');
375 # $self->setfield($field,$value);
377 # confess "errant AUTOLOAD $field for $self (no args)"
378 # unless $self->can('getfield');
379 # $self->getfield($field);
385 my $field = $AUTOLOAD;
387 if ( defined($_[1]) ) {
388 $_[0]->setfield($field, $_[1]);
390 $_[0]->getfield($field);
396 Returns a list of the column/value pairs, usually for assigning to a new hash.
398 To make a distinct duplicate of an FS::Record object, you can do:
400 $new = new FS::Record ( $old->table, { $old->hash } );
406 %{ $self->{'Hash'} };
411 Returns a reference to the column/value hash.
422 Inserts this record to the database. If there is an error, returns the error,
423 otherwise returns false.
430 my $error = $self->check;
431 return $error if $error;
433 #single-field unique keys are given a value if false
434 #(like MySQL's AUTO_INCREMENT)
435 foreach ( $self->dbdef_table->unique->singles ) {
436 $self->unique($_) unless $self->getfield($_);
438 #and also the primary key
439 my $primary_key = $self->dbdef_table->primary_key;
440 $self->unique($primary_key)
441 if $primary_key && ! $self->getfield($primary_key);
444 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
448 my $statement = "INSERT INTO ". $self->table. " ( ".
451 join(', ',map(_quote($self->getfield($_),$self->table,$_), @fields)).
454 my $sth = dbh->prepare($statement) or return dbh->errstr;
456 local $SIG{HUP} = 'IGNORE';
457 local $SIG{INT} = 'IGNORE';
458 local $SIG{QUIT} = 'IGNORE';
459 local $SIG{TERM} = 'IGNORE';
460 local $SIG{TSTP} = 'IGNORE';
461 local $SIG{PIPE} = 'IGNORE';
463 $sth->execute or return $sth->errstr;
464 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
471 Depriciated (use insert instead).
476 cluck "warning: FS::Record::add depriciated!";
477 insert @_; #call method in this scope
482 Delete this record from the database. If there is an error, returns the error,
483 otherwise returns false.
490 my($statement)="DELETE FROM ". $self->table. " WHERE ". join(' AND ',
492 $self->getfield($_) eq ''
493 #? "( $_ IS NULL OR $_ = \"\" )"
494 ? ( driver_name =~ /^Pg$/i
496 : "( $_ IS NULL OR $_ = \"\" )"
498 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
499 } ( $self->dbdef_table->primary_key )
500 ? ( $self->dbdef_table->primary_key)
503 my $sth = dbh->prepare($statement) or return dbh->errstr;
505 local $SIG{HUP} = 'IGNORE';
506 local $SIG{INT} = 'IGNORE';
507 local $SIG{QUIT} = 'IGNORE';
508 local $SIG{TERM} = 'IGNORE';
509 local $SIG{TSTP} = 'IGNORE';
510 local $SIG{PIPE} = 'IGNORE';
512 my $rc = $sth->execute or return $sth->errstr;
513 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
514 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
516 undef $self; #no need to keep object!
523 Depriciated (use delete instead).
528 cluck "warning: FS::Record::del depriciated!";
529 &delete(@_); #call method in this scope
532 =item replace OLD_RECORD
534 Replace the OLD_RECORD with this one in the database. If there is an error,
535 returns the error, otherwise returns false.
540 my ( $new, $old ) = ( shift, shift );
541 warn "[debug][FS::Record] $new ->replace $old\n" if $DEBUG;
543 my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
545 carp "[warning][FS::Record] $new -> replace $old: records identical";
549 return "Records not in same table!" unless $new->table eq $old->table;
551 my $primary_key = $old->dbdef_table->primary_key;
552 return "Can't change $primary_key"
554 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
556 my $error = $new->check;
557 return $error if $error;
559 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
561 "$_ = ". _quote($new->getfield($_),$old->table,$_)
566 $old->getfield($_) eq ''
567 #? "( $_ IS NULL OR $_ = \"\" )"
568 ? ( driver_name =~ /^Pg$/i
570 : "( $_ IS NULL OR $_ = \"\" )"
572 : "$_ = ". _quote($old->getfield($_),$old->table,$_)
573 } ( $primary_key ? ( $primary_key ) : $old->fields )
576 my $sth = dbh->prepare($statement) or return dbh->errstr;
578 local $SIG{HUP} = 'IGNORE';
579 local $SIG{INT} = 'IGNORE';
580 local $SIG{QUIT} = 'IGNORE';
581 local $SIG{TERM} = 'IGNORE';
582 local $SIG{TSTP} = 'IGNORE';
583 local $SIG{PIPE} = 'IGNORE';
585 my $rc = $sth->execute or return $sth->errstr;
586 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
587 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
595 Depriciated (use replace instead).
600 cluck "warning: FS::Record::rep depriciated!";
601 replace @_; #call method in this scope
606 Not yet implemented, croaks. Derived classes should provide a check method.
611 confess "FS::Record::check not implemented; supply one in subclass!";
616 Replaces COLUMN in record with a unique number. Called by the B<add> method
617 on primary keys and single-field unique columns (see L<DBIx::DBSchema::Table>).
618 Returns the new value.
623 my($self,$field) = @_;
624 my($table)=$self->table;
626 croak("&FS::UID::checkruid failed") unless &checkruid;
628 croak "Unique called on field $field, but it is ",
629 $self->getfield($field),
631 if $self->getfield($field);
633 #warn "table $table is tainted" if is_tainted($table);
634 #warn "field $field is tainted" if is_tainted($field);
636 my($counter) = new File::CounterFile "$table.$field",0;
638 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
640 # my($counter) = new File::CounterFile "$user/$table.$field",0;
643 my($index)=$counter->inc;
645 while qsearchs($table,{$field=>$index}); #just in case
650 $self->setfield($field,$index);
654 =item ut_float COLUMN
656 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
657 null. If there is an error, returns the error, otherwise returns false.
662 my($self,$field)=@_ ;
663 ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
664 $self->getfield($field) =~ /^(\d+)$/ ||
665 $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
666 $self->getfield($field) =~ /^(\d+e\d+)$/)
667 or return "Illegal or empty (float) $field: ". $self->getfield($field);
668 $self->setfield($field,$1);
672 =item ut_number COLUMN
674 Check/untaint simple numeric data (whole numbers). May not be null. If there
675 is an error, returns the error, otherwise returns false.
681 $self->getfield($field) =~ /^(\d+)$/
682 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
683 $self->setfield($field,$1);
687 =item ut_numbern COLUMN
689 Check/untaint simple numeric data (whole numbers). May be null. If there is
690 an error, returns the error, otherwise returns false.
696 $self->getfield($field) =~ /^(\d*)$/
697 or return "Illegal (numeric) $field: ". $self->getfield($field);
698 $self->setfield($field,$1);
702 =item ut_money COLUMN
704 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
705 is an error, returns the error, otherwise returns false.
711 $self->setfield($field, 0) if $self->getfield($field) eq '';
712 $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
713 or return "Illegal (money) $field: ". $self->getfield($field);
714 #$self->setfield($field, "$1$2$3" || 0);
715 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
721 Check/untaint text. Alphanumerics, spaces, and the following punctuation
722 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
723 May not be null. If there is an error, returns the error, otherwise returns
730 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]+)$/
731 or return "Illegal or empty (text) $field: ". $self->getfield($field);
732 $self->setfield($field,$1);
736 =item ut_textn COLUMN
738 Check/untaint text. Alphanumerics, spaces, and the following punctuation
739 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
740 May be null. If there is an error, returns the error, otherwise returns false.
746 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]*)$/
747 or return "Illegal (text) $field: ". $self->getfield($field);
748 $self->setfield($field,$1);
752 =item ut_alpha COLUMN
754 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
755 an error, returns the error, otherwise returns false.
761 $self->getfield($field) =~ /^(\w+)$/
762 or return "Illegal or empty (alphanumeric) $field: ".
763 $self->getfield($field);
764 $self->setfield($field,$1);
768 =item ut_alpha COLUMN
770 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
771 error, returns the error, otherwise returns false.
777 $self->getfield($field) =~ /^(\w*)$/
778 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
779 $self->setfield($field,$1);
783 =item ut_phonen COLUMN [ COUNTRY ]
785 Check/untaint phone numbers. May be null. If there is an error, returns
786 the error, otherwise returns false.
788 Takes an optional two-letter ISO country code; without it or with unsupported
789 countries, ut_phonen simply calls ut_alphan.
794 my( $self, $field, $country ) = @_;
795 return $self->ut_alphan($field) unless defined $country;
796 my $phonen = $self->getfield($field);
797 if ( $phonen eq '' ) {
798 $self->setfield($field,'');
799 } elsif ( $country eq 'US' || $country eq 'CA' ) {
801 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
802 or return "Illegal (phone) $field: ". $self->getfield($field);
803 $phonen = "$1-$2-$3";
804 $phonen .= " x$4" if $4;
805 $self->setfield($field,$phonen);
807 warn "warning: don't know how to check phone numbers for country $country";
808 return $self->ut_textn($field);
815 Check/untaint ip addresses. IPv4 only for now.
820 my( $self, $field ) = @_;
821 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
822 or return "Illegal (IP address) $field: ". $self->getfield($field);
823 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
824 $self->setfield($field, "$1.$2.$3.$3");
830 Check/untaint ip addresses. IPv4 only for now. May be null.
835 my( $self, $field ) = @_;
836 if ( $self->getfield($field) =~ /^()$/ ) {
837 $self->setfield($field,'');
840 $self->ut_ip($field);
844 =item ut_domain COLUMN
846 Check/untaint host and domain names.
851 my( $self, $field ) = @_;
852 #$self->getfield($field) =~/^(\w+\.)*\w+$/
853 $self->getfield($field) =~/^(\w+\.)*\w+$/
854 or return "Illegal (domain) $field: ". $self->getfield($field);
855 $self->setfield($field,$1);
861 Check/untaint proper names; allows alphanumerics, spaces and the following
869 my( $self, $field ) = @_;
870 $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
871 or return "Illegal (name) $field: ". $self->getfield($field);
872 $self->setfield($field,$1);
878 Check/untaint zip codes.
883 my( $self, $field, $country ) = @_;
884 if ( $country eq 'US' ) {
885 $self->getfield($field) =~ /\s*(\d{5}(\-\d{4})?)\s*$/
886 or return "Illegal (zip) $field for country $country: ".
887 $self->getfield($field);
888 $self->setfield($field,$1);
890 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
891 or return "Illegal (zip) $field: ". $self->getfield($field);
892 $self->setfield($field,$1);
897 =item ut_country COLUMN
899 Check/untaint country codes. Country names are changed to codes, if possible -
900 see L<Locale::Country>.
905 my( $self, $field ) = @_;
906 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
907 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
908 && country2code($1) ) {
909 $self->setfield($field,uc(country2code($1)));
912 $self->getfield($field) =~ /^(\w\w)$/
913 or return "Illegal (country) $field: ". $self->getfield($field);
914 $self->setfield($field,uc($1));
918 =item ut_anything COLUMN
920 Untaints arbitrary data. Be careful.
925 my( $self, $field ) = @_;
926 $self->getfield($field) =~ /^(.*)$/s
927 or return "Illegal $field: ". $self->getfield($field);
928 $self->setfield($field,$1);
932 =item ut_enum COLUMN CHOICES_ARRAYREF
934 Check/untaint a column, supplying all possible choices, like the "enum" type.
939 my( $self, $field, $choices ) = @_;
940 foreach my $choice ( @$choices ) {
941 if ( $self->getfield($field) eq $choice ) {
942 $self->setfield($choice);
946 return "Illegal (enum) field $field: ". $self->getfield($field);
949 =item fields [ TABLE ]
951 This can be used as both a subroutine and a method call. It returns a list
952 of the columns in this record's table, or an explicitly specified table.
953 (See L<DBIx::DBSchema::Table>).
957 # Usage: @fields = fields($table);
958 # @fields = $record->fields;
960 my $something = shift;
962 if ( ref($something) ) {
963 $table = $something->table;
967 #croak "Usage: \@fields = fields(\$table)\n or: \@fields = \$record->fields" unless $table;
968 my($table_obj) = $dbdef->table($table);
969 confess "Unknown table $table" unless $table_obj;
979 =item reload_dbdef([FILENAME])
981 Load a database definition (see L<DBIx::DBSchema>), optionally from a
982 non-default filename. This command is executed at startup unless
983 I<$FS::Record::setup_hack> is true. Returns a DBIx::DBSchema object.
988 my $file = shift || $dbdef_file;
989 $dbdef = load DBIx::DBSchema $file;
994 Returns the current database definition. See L<FS::dbdef>.
998 sub dbdef { $dbdef; }
1000 =item _quote VALUE, TABLE, COLUMN
1002 This is an internal function used to construct SQL statements. It returns
1003 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
1004 type (see L<FS::dbdef_column>) does not end in `char' or `binary'.
1009 my($value,$table,$field)=@_;
1011 if ( $value =~ /^\d+(\.\d+)?$/ &&
1012 # ! ( datatype($table,$field) =~ /^char/ )
1013 ! ( $dbdef->table($table)->column($field)->type =~ /(char|binary)$/i )
1017 $dbh->quote($value);
1023 This is depriciated. Don't use it.
1025 It returns a hash-type list with the fields of this record's table set true.
1030 carp "warning: hfields is depriciated";
1033 foreach (fields($table)) {
1042 "$_: ". $self->getfield($_). "|"
1043 } (fields($self->table)) );
1046 sub DESTROY { return; }
1050 # #use Carp qw(cluck);
1051 # #cluck "DESTROYING $self";
1052 # warn "DESTROYING $self";
1056 # return ! eval { join('',@_), kill 0; 1; };
1063 This module should probably be renamed, since much of the functionality is
1064 of general use. It is not completely unlike Adapter::DBI (see below).
1066 Exported qsearch and qsearchs should be depriciated in favor of method calls
1067 (against an FS::Record object like the old search and searchs that qsearch
1068 and qsearchs were on top of.)
1070 The whole fields / hfields mess should be removed.
1072 The various WHERE clauses should be subroutined.
1074 table string should be depriciated in favor of FS::dbdef_table.
1076 No doubt we could benefit from a Tied hash. Documenting how exists / defined
1077 true maps to the database (and WHERE clauses) would also help.
1079 The ut_ methods should ask the dbdef for a default length.
1081 ut_sqltype (like ut_varchar) should all be defined
1083 A fallback check method should be provided which uses the dbdef.
1085 The ut_money method assumes money has two decimal digits.
1087 The Pg money kludge in the new method only strips `$'.
1089 The ut_phonen method only checks US-style phone numbers.
1091 The _quote function should probably use ut_float instead of a regex.
1093 All the subroutines probably should be methods, here or elsewhere.
1095 Probably should borrow/use some dbdef methods where appropriate (like sub
1098 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
1099 or allow it to be set. Working around it is ugly any way around - DBI should
1100 be fixed. (only affects RDBMS which return uppercase column names)
1102 ut_zip should take an optional country like ut_phone.
1106 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
1108 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.