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\": ". $sth->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( {} )};
260 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
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
272 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
273 my $cache = FS::SearchCache->new( $ptable, $pkey );
276 grep { !$saw{$_->getfield($pkey)}++ }
277 qsearch($table, $record, $select, $extra_sql, $cache )
281 =item qsearchs TABLE, HASHREF
283 Same as qsearch, except that if more than one record matches, it B<carp>s but
284 returns the first. If this happens, you either made a logic error in asking
285 for a single item, or your data is corrupted.
289 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
290 my(@result) = qsearch(@_);
291 carp "warning: Multiple records in scalar search!" if scalar(@result) > 1;
292 #should warn more vehemently if the search was on a primary key?
293 scalar(@result) ? ($result[0]) : ();
304 Returns the table name.
309 # cluck "warning: FS::Record::table depriciated; supply one in subclass!";
316 Returns the FS::dbdef_table object for the table.
322 my($table)=$self->table;
323 $dbdef->table($table);
326 =item get, getfield COLUMN
328 Returns the value of the column/field/key COLUMN.
333 my($self,$field) = @_;
334 # to avoid "Use of unitialized value" errors
335 if ( defined ( $self->{Hash}->{$field} ) ) {
336 $self->{Hash}->{$field};
346 =item set, setfield COLUMN, VALUE
348 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
353 my($self,$field,$value) = @_;
354 $self->{'Hash'}->{$field} = $value;
361 =item AUTLOADED METHODS
363 $record->column is a synonym for $record->get('column');
365 $record->column('value') is a synonym for $record->set('column','value');
371 # my($self,$value)=@_;
372 # my($field)=$AUTOLOAD;
374 # if ( defined($value) ) {
375 # confess "errant AUTOLOAD $field for $self (arg $value)"
376 # unless $self->can('setfield');
377 # $self->setfield($field,$value);
379 # confess "errant AUTOLOAD $field for $self (no args)"
380 # unless $self->can('getfield');
381 # $self->getfield($field);
387 my $field = $AUTOLOAD;
389 if ( defined($_[1]) ) {
390 $_[0]->setfield($field, $_[1]);
392 $_[0]->getfield($field);
398 Returns a list of the column/value pairs, usually for assigning to a new hash.
400 To make a distinct duplicate of an FS::Record object, you can do:
402 $new = new FS::Record ( $old->table, { $old->hash } );
408 %{ $self->{'Hash'} };
413 Returns a reference to the column/value hash.
424 Inserts this record to the database. If there is an error, returns the error,
425 otherwise returns false.
432 my $error = $self->check;
433 return $error if $error;
435 #single-field unique keys are given a value if false
436 #(like MySQL's AUTO_INCREMENT)
437 foreach ( $self->dbdef_table->unique->singles ) {
438 $self->unique($_) unless $self->getfield($_);
440 #and also the primary key
441 my $primary_key = $self->dbdef_table->primary_key;
442 $self->unique($primary_key)
443 if $primary_key && ! $self->getfield($primary_key);
446 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
450 my $statement = "INSERT INTO ". $self->table. " ( ".
453 join(', ',map(_quote($self->getfield($_),$self->table,$_), @fields)).
456 my $sth = dbh->prepare($statement) or return dbh->errstr;
458 local $SIG{HUP} = 'IGNORE';
459 local $SIG{INT} = 'IGNORE';
460 local $SIG{QUIT} = 'IGNORE';
461 local $SIG{TERM} = 'IGNORE';
462 local $SIG{TSTP} = 'IGNORE';
463 local $SIG{PIPE} = 'IGNORE';
465 $sth->execute or return $sth->errstr;
466 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
473 Depriciated (use insert instead).
478 cluck "warning: FS::Record::add depriciated!";
479 insert @_; #call method in this scope
484 Delete this record from the database. If there is an error, returns the error,
485 otherwise returns false.
492 my($statement)="DELETE FROM ". $self->table. " WHERE ". join(' AND ',
494 $self->getfield($_) eq ''
495 #? "( $_ IS NULL OR $_ = \"\" )"
496 ? ( driver_name =~ /^Pg$/i
498 : "( $_ IS NULL OR $_ = \"\" )"
500 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
501 } ( $self->dbdef_table->primary_key )
502 ? ( $self->dbdef_table->primary_key)
505 my $sth = dbh->prepare($statement) or return dbh->errstr;
507 local $SIG{HUP} = 'IGNORE';
508 local $SIG{INT} = 'IGNORE';
509 local $SIG{QUIT} = 'IGNORE';
510 local $SIG{TERM} = 'IGNORE';
511 local $SIG{TSTP} = 'IGNORE';
512 local $SIG{PIPE} = 'IGNORE';
514 my $rc = $sth->execute or return $sth->errstr;
515 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
516 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
518 undef $self; #no need to keep object!
525 Depriciated (use delete instead).
530 cluck "warning: FS::Record::del depriciated!";
531 &delete(@_); #call method in this scope
534 =item replace OLD_RECORD
536 Replace the OLD_RECORD with this one in the database. If there is an error,
537 returns the error, otherwise returns false.
542 my ( $new, $old ) = ( shift, shift );
543 warn "[debug][FS::Record] $new ->replace $old\n" if $DEBUG;
545 my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
547 carp "[warning][FS::Record] $new -> replace $old: records identical";
551 return "Records not in same table!" unless $new->table eq $old->table;
553 my $primary_key = $old->dbdef_table->primary_key;
554 return "Can't change $primary_key"
556 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
558 my $error = $new->check;
559 return $error if $error;
561 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
563 "$_ = ". _quote($new->getfield($_),$old->table,$_)
568 $old->getfield($_) eq ''
569 #? "( $_ IS NULL OR $_ = \"\" )"
570 ? ( driver_name =~ /^Pg$/i
572 : "( $_ IS NULL OR $_ = \"\" )"
574 : "$_ = ". _quote($old->getfield($_),$old->table,$_)
575 } ( $primary_key ? ( $primary_key ) : $old->fields )
578 my $sth = dbh->prepare($statement) or return dbh->errstr;
580 local $SIG{HUP} = 'IGNORE';
581 local $SIG{INT} = 'IGNORE';
582 local $SIG{QUIT} = 'IGNORE';
583 local $SIG{TERM} = 'IGNORE';
584 local $SIG{TSTP} = 'IGNORE';
585 local $SIG{PIPE} = 'IGNORE';
587 my $rc = $sth->execute or return $sth->errstr;
588 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
589 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
597 Depriciated (use replace instead).
602 cluck "warning: FS::Record::rep depriciated!";
603 replace @_; #call method in this scope
608 Not yet implemented, croaks. Derived classes should provide a check method.
613 confess "FS::Record::check not implemented; supply one in subclass!";
618 Replaces COLUMN in record with a unique number. Called by the B<add> method
619 on primary keys and single-field unique columns (see L<DBIx::DBSchema::Table>).
620 Returns the new value.
625 my($self,$field) = @_;
626 my($table)=$self->table;
628 croak("&FS::UID::checkruid failed") unless &checkruid;
630 croak "Unique called on field $field, but it is ",
631 $self->getfield($field),
633 if $self->getfield($field);
635 #warn "table $table is tainted" if is_tainted($table);
636 #warn "field $field is tainted" if is_tainted($field);
638 my($counter) = new File::CounterFile "$table.$field",0;
640 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
642 # my($counter) = new File::CounterFile "$user/$table.$field",0;
645 my($index)=$counter->inc;
647 while qsearchs($table,{$field=>$index}); #just in case
652 $self->setfield($field,$index);
656 =item ut_float COLUMN
658 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
659 null. If there is an error, returns the error, otherwise returns false.
664 my($self,$field)=@_ ;
665 ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
666 $self->getfield($field) =~ /^(\d+)$/ ||
667 $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
668 $self->getfield($field) =~ /^(\d+e\d+)$/)
669 or return "Illegal or empty (float) $field: ". $self->getfield($field);
670 $self->setfield($field,$1);
674 =item ut_number COLUMN
676 Check/untaint simple numeric data (whole numbers). May not be null. If there
677 is an error, returns the error, otherwise returns false.
683 $self->getfield($field) =~ /^(\d+)$/
684 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
685 $self->setfield($field,$1);
689 =item ut_numbern COLUMN
691 Check/untaint simple numeric data (whole numbers). May be null. If there is
692 an error, returns the error, otherwise returns false.
698 $self->getfield($field) =~ /^(\d*)$/
699 or return "Illegal (numeric) $field: ". $self->getfield($field);
700 $self->setfield($field,$1);
704 =item ut_money COLUMN
706 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
707 is an error, returns the error, otherwise returns false.
713 $self->setfield($field, 0) if $self->getfield($field) eq '';
714 $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
715 or return "Illegal (money) $field: ". $self->getfield($field);
716 #$self->setfield($field, "$1$2$3" || 0);
717 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
723 Check/untaint text. Alphanumerics, spaces, and the following punctuation
724 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
725 May not be null. If there is an error, returns the error, otherwise returns
732 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]+)$/
733 or return "Illegal or empty (text) $field: ". $self->getfield($field);
734 $self->setfield($field,$1);
738 =item ut_textn COLUMN
740 Check/untaint text. Alphanumerics, spaces, and the following punctuation
741 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
742 May be null. If there is an error, returns the error, otherwise returns false.
748 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/]*)$/
749 or return "Illegal (text) $field: ". $self->getfield($field);
750 $self->setfield($field,$1);
754 =item ut_alpha COLUMN
756 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
757 an error, returns the error, otherwise returns false.
763 $self->getfield($field) =~ /^(\w+)$/
764 or return "Illegal or empty (alphanumeric) $field: ".
765 $self->getfield($field);
766 $self->setfield($field,$1);
770 =item ut_alpha COLUMN
772 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
773 error, returns the error, otherwise returns false.
779 $self->getfield($field) =~ /^(\w*)$/
780 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
781 $self->setfield($field,$1);
785 =item ut_phonen COLUMN [ COUNTRY ]
787 Check/untaint phone numbers. May be null. If there is an error, returns
788 the error, otherwise returns false.
790 Takes an optional two-letter ISO country code; without it or with unsupported
791 countries, ut_phonen simply calls ut_alphan.
796 my( $self, $field, $country ) = @_;
797 return $self->ut_alphan($field) unless defined $country;
798 my $phonen = $self->getfield($field);
799 if ( $phonen eq '' ) {
800 $self->setfield($field,'');
801 } elsif ( $country eq 'US' || $country eq 'CA' ) {
803 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
804 or return "Illegal (phone) $field: ". $self->getfield($field);
805 $phonen = "$1-$2-$3";
806 $phonen .= " x$4" if $4;
807 $self->setfield($field,$phonen);
809 warn "warning: don't know how to check phone numbers for country $country";
810 return $self->ut_textn($field);
817 Check/untaint ip addresses. IPv4 only for now.
822 my( $self, $field ) = @_;
823 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
824 or return "Illegal (IP address) $field: ". $self->getfield($field);
825 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
826 $self->setfield($field, "$1.$2.$3.$3");
832 Check/untaint ip addresses. IPv4 only for now. May be null.
837 my( $self, $field ) = @_;
838 if ( $self->getfield($field) =~ /^()$/ ) {
839 $self->setfield($field,'');
842 $self->ut_ip($field);
846 =item ut_domain COLUMN
848 Check/untaint host and domain names.
853 my( $self, $field ) = @_;
854 #$self->getfield($field) =~/^(\w+\.)*\w+$/
855 $self->getfield($field) =~/^(\w+\.)*\w+$/
856 or return "Illegal (domain) $field: ". $self->getfield($field);
857 $self->setfield($field,$1);
863 Check/untaint proper names; allows alphanumerics, spaces and the following
871 my( $self, $field ) = @_;
872 $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
873 or return "Illegal (name) $field: ". $self->getfield($field);
874 $self->setfield($field,$1);
880 Check/untaint zip codes.
885 my( $self, $field, $country ) = @_;
886 if ( $country eq 'US' ) {
887 $self->getfield($field) =~ /\s*(\d{5}(\-\d{4})?)\s*$/
888 or return "Illegal (zip) $field for country $country: ".
889 $self->getfield($field);
890 $self->setfield($field,$1);
892 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
893 or return "Illegal (zip) $field: ". $self->getfield($field);
894 $self->setfield($field,$1);
899 =item ut_country COLUMN
901 Check/untaint country codes. Country names are changed to codes, if possible -
902 see L<Locale::Country>.
907 my( $self, $field ) = @_;
908 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
909 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
910 && country2code($1) ) {
911 $self->setfield($field,uc(country2code($1)));
914 $self->getfield($field) =~ /^(\w\w)$/
915 or return "Illegal (country) $field: ". $self->getfield($field);
916 $self->setfield($field,uc($1));
920 =item ut_anything COLUMN
922 Untaints arbitrary data. Be careful.
927 my( $self, $field ) = @_;
928 $self->getfield($field) =~ /^(.*)$/s
929 or return "Illegal $field: ". $self->getfield($field);
930 $self->setfield($field,$1);
934 =item ut_enum COLUMN CHOICES_ARRAYREF
936 Check/untaint a column, supplying all possible choices, like the "enum" type.
941 my( $self, $field, $choices ) = @_;
942 foreach my $choice ( @$choices ) {
943 if ( $self->getfield($field) eq $choice ) {
944 $self->setfield($choice);
948 return "Illegal (enum) field $field: ". $self->getfield($field);
951 =item fields [ TABLE ]
953 This can be used as both a subroutine and a method call. It returns a list
954 of the columns in this record's table, or an explicitly specified table.
955 (See L<DBIx::DBSchema::Table>).
959 # Usage: @fields = fields($table);
960 # @fields = $record->fields;
962 my $something = shift;
964 if ( ref($something) ) {
965 $table = $something->table;
969 #croak "Usage: \@fields = fields(\$table)\n or: \@fields = \$record->fields" unless $table;
970 my($table_obj) = $dbdef->table($table);
971 confess "Unknown table $table" unless $table_obj;
981 =item reload_dbdef([FILENAME])
983 Load a database definition (see L<DBIx::DBSchema>), optionally from a
984 non-default filename. This command is executed at startup unless
985 I<$FS::Record::setup_hack> is true. Returns a DBIx::DBSchema object.
990 my $file = shift || $dbdef_file;
991 $dbdef = load DBIx::DBSchema $file;
996 Returns the current database definition. See L<FS::dbdef>.
1000 sub dbdef { $dbdef; }
1002 =item _quote VALUE, TABLE, COLUMN
1004 This is an internal function used to construct SQL statements. It returns
1005 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
1006 type (see L<FS::dbdef_column>) does not end in `char' or `binary'.
1011 my($value,$table,$field)=@_;
1013 if ( $value =~ /^\d+(\.\d+)?$/ &&
1014 # ! ( datatype($table,$field) =~ /^char/ )
1015 ! ( $dbdef->table($table)->column($field)->type =~ /(char|binary)$/i )
1019 $dbh->quote($value);
1025 This is depriciated. Don't use it.
1027 It returns a hash-type list with the fields of this record's table set true.
1032 carp "warning: hfields is depriciated";
1035 foreach (fields($table)) {
1044 "$_: ". $self->getfield($_). "|"
1045 } (fields($self->table)) );
1048 sub DESTROY { return; }
1052 # #use Carp qw(cluck);
1053 # #cluck "DESTROYING $self";
1054 # warn "DESTROYING $self";
1058 # return ! eval { join('',@_), kill 0; 1; };
1065 This module should probably be renamed, since much of the functionality is
1066 of general use. It is not completely unlike Adapter::DBI (see below).
1068 Exported qsearch and qsearchs should be depriciated in favor of method calls
1069 (against an FS::Record object like the old search and searchs that qsearch
1070 and qsearchs were on top of.)
1072 The whole fields / hfields mess should be removed.
1074 The various WHERE clauses should be subroutined.
1076 table string should be depriciated in favor of FS::dbdef_table.
1078 No doubt we could benefit from a Tied hash. Documenting how exists / defined
1079 true maps to the database (and WHERE clauses) would also help.
1081 The ut_ methods should ask the dbdef for a default length.
1083 ut_sqltype (like ut_varchar) should all be defined
1085 A fallback check method should be provided which uses the dbdef.
1087 The ut_money method assumes money has two decimal digits.
1089 The Pg money kludge in the new method only strips `$'.
1091 The ut_phonen method only checks US-style phone numbers.
1093 The _quote function should probably use ut_float instead of a regex.
1095 All the subroutines probably should be methods, here or elsewhere.
1097 Probably should borrow/use some dbdef methods where appropriate (like sub
1100 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
1101 or allow it to be set. Working around it is ugly any way around - DBI should
1102 be fixed. (only affects RDBMS which return uppercase column names)
1104 ut_zip should take an optional country like ut_phone.
1108 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
1110 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.