4 use vars qw( $dbdef_file $dbdef $setup_hack $AUTOLOAD @ISA @EXPORT_OK $DEBUG
5 $me %dbdef_cache %virtual_fields_cache );
6 use subs qw(reload_dbdef);
8 use Carp qw(carp cluck croak confess);
11 use DBI qw(:sql_types);
12 use DBIx::DBSchema 0.23;
13 use FS::UID qw(dbh getotaker datasrc driver_name);
15 use FS::Msgcat qw(gettext);
18 use FS::part_virtual_field;
23 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch);
34 #ask FS::UID to run this stuff for us later
35 $FS::UID::callback{'FS::Record'} = sub {
36 $File::CounterFile::DEFAULT_DIR = "/usr/local/etc/freeside/counters.". datasrc;
37 $dbdef_file = "/usr/local/etc/freeside/dbdef.". datasrc;
38 &reload_dbdef unless $setup_hack; #$setup_hack needed now?
43 FS::Record - Database record objects
48 use FS::Record qw(dbh fields qsearch qsearchs dbdef);
50 $record = new FS::Record 'table', \%hash;
51 $record = new FS::Record 'table', { 'column' => 'value', ... };
53 $record = qsearchs FS::Record 'table', \%hash;
54 $record = qsearchs FS::Record 'table', { 'column' => 'value', ... };
55 @records = qsearch FS::Record 'table', \%hash;
56 @records = qsearch FS::Record 'table', { 'column' => 'value', ... };
58 $table = $record->table;
59 $dbdef_table = $record->dbdef_table;
61 $value = $record->get('column');
62 $value = $record->getfield('column');
63 $value = $record->column;
65 $record->set( 'column' => 'value' );
66 $record->setfield( 'column' => 'value' );
67 $record->column('value');
69 %hash = $record->hash;
71 $hashref = $record->hashref;
73 $error = $record->insert;
75 $error = $record->delete;
77 $error = $new_record->replace($old_record);
79 # external use deprecated - handled by the database (at least for Pg, mysql)
80 $value = $record->unique('column');
82 $error = $record->ut_float('column');
83 $error = $record->ut_number('column');
84 $error = $record->ut_numbern('column');
85 $error = $record->ut_money('column');
86 $error = $record->ut_text('column');
87 $error = $record->ut_textn('column');
88 $error = $record->ut_alpha('column');
89 $error = $record->ut_alphan('column');
90 $error = $record->ut_phonen('column');
91 $error = $record->ut_anything('column');
92 $error = $record->ut_name('column');
94 $dbdef = reload_dbdef;
95 $dbdef = reload_dbdef "/non/standard/filename";
98 $quoted_value = _quote($value,'table','field');
101 $fields = hfields('table');
102 if ( $fields->{Field} ) { # etc.
104 @fields = fields 'table'; #as a subroutine
105 @fields = $record->fields; #as a method call
110 (Mostly) object-oriented interface to database records. Records are currently
111 implemented on top of DBI. FS::Record is intended as a base class for
112 table-specific classes to inherit from, i.e. FS::cust_main.
118 =item new [ TABLE, ] HASHREF
120 Creates a new record. It doesn't store it in the database, though. See
121 L<"insert"> for that.
123 Note that the object stores this hash reference, not a distinct copy of the
124 hash it points to. You can ask the object for a copy with the I<hash>
127 TABLE can only be omitted when a dervived class overrides the table method.
133 my $class = ref($proto) || $proto;
135 bless ($self, $class);
137 unless ( defined ( $self->table ) ) {
138 $self->{'Table'} = shift;
139 carp "warning: FS::Record::new called with table name ". $self->{'Table'};
142 $self->{'Hash'} = shift;
144 foreach my $field ( grep !defined($self->{'Hash'}{$_}), $self->fields ) {
145 $self->{'Hash'}{$field}='';
148 $self->_rebless if $self->can('_rebless');
150 $self->{'modified'} = 0;
152 $self->_cache($self->{'Hash'}, shift) if $self->can('_cache') && @_;
159 my $class = ref($proto) || $proto;
161 bless ($self, $class);
163 $self->{'Table'} = shift unless defined ( $self->table );
165 my $hashref = $self->{'Hash'} = shift;
167 if ( defined( $cache->cache->{$hashref->{$cache->key}} ) ) {
168 my $obj = $cache->cache->{$hashref->{$cache->key}};
169 $obj->_cache($hashref, $cache) if $obj->can('_cache');
172 $cache->cache->{$hashref->{$cache->key}} = $self->new($hashref, $cache);
179 my $class = ref($proto) || $proto;
181 bless ($self, $class);
182 if ( defined $self->table ) {
183 cluck "create constructor is deprecated, use new!";
186 croak "FS::Record::create called (not from a subclass)!";
190 =item qsearch TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
192 Searches the database for all records matching (at least) the key/value pairs
193 in HASHREF. Returns all the records found as `FS::TABLE' objects if that
194 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
197 ###oops, argh, FS::Record::new only lets us create database fields.
198 #Normal behaviour if SELECT is not specified is `*', as in
199 #C<SELECT * FROM table WHERE ...>. However, there is an experimental new
200 #feature where you can specify SELECT - remember, the objects returned,
201 #although blessed into the appropriate `FS::TABLE' package, will only have the
202 #fields you specify. This might have unwanted results if you then go calling
203 #regular FS::TABLE methods
209 my($stable, $record, $select, $extra_sql, $cache, $addl_from ) = @_;
210 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
212 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
217 my $table = $cache ? $cache->table : $stable;
218 my $dbdef_table = $dbdef->table($table)
219 or die "No schema for table $table found - ".
220 "do you need to create it or run dbdef-create?";
221 my $pkey = $dbdef_table->primary_key;
223 my @real_fields = grep exists($record->{$_}), real_fields($table);
225 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
226 @virtual_fields = grep exists($record->{$_}), "FS::$table"->virtual_fields;
228 cluck "warning: FS::$table not loaded; virtual fields not searchable";
229 @virtual_fields = ();
232 my $statement = "SELECT $select FROM $stable";
233 $statement .= " $addl_from" if $addl_from;
234 if ( @real_fields or @virtual_fields ) {
235 $statement .= ' WHERE '. join(' AND ',
240 if ( ref($record->{$_}) ) {
241 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
242 #$op = 'LIKE' if $op =~ /^ILIKE$/i && driver_name ne 'Pg';
243 if ( uc($op) eq 'ILIKE' ) {
245 $record->{$_}{'value'} = lc($record->{$_}{'value'});
246 $column = "LOWER($_)";
248 $record->{$_} = $record->{$_}{'value'}
251 if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) {
253 if ( driver_name eq 'Pg' ) {
254 my $type = $dbdef->table($table)->column($column)->type;
255 if ( $type =~ /(int|serial)/i ) {
256 qq-( $column IS NULL )-;
258 qq-( $column IS NULL OR $column = '' )-;
261 qq-( $column IS NULL OR $column = "" )-;
263 } elsif ( $op eq '!=' ) {
264 if ( driver_name eq 'Pg' ) {
265 my $type = $dbdef->table($table)->column($column)->type;
266 if ( $type =~ /(int|serial)/i ) {
267 qq-( $column IS NOT NULL )-;
269 qq-( $column IS NOT NULL AND $column != '' )-;
272 qq-( $column IS NOT NULL AND $column != "" )-;
275 if ( driver_name eq 'Pg' ) {
276 qq-( $column $op '' )-;
278 qq-( $column $op "" )-;
288 if ( ref($record->{$_}) ) {
289 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
290 if ( uc($op) eq 'ILIKE' ) {
292 $record->{$_}{'value'} = lc($record->{$_}{'value'});
293 $column = "LOWER($_)";
295 $record->{$_} = $record->{$_}{'value'};
298 # ... EXISTS ( SELECT name, value FROM part_virtual_field
300 # ON part_virtual_field.vfieldpart = virtual_field.vfieldpart
301 # WHERE recnum = svc_acct.svcnum
302 # AND (name, value) = ('egad', 'brain') )
304 my $value = $record->{$_};
308 $subq = ($value ? 'EXISTS ' : 'NOT EXISTS ') .
309 "( SELECT part_virtual_field.name, virtual_field.value ".
310 "FROM part_virtual_field JOIN virtual_field ".
311 "ON part_virtual_field.vfieldpart = virtual_field.vfieldpart ".
312 "WHERE virtual_field.recnum = ${table}.${pkey} ".
313 "AND part_virtual_field.name = '${column}'".
315 " AND virtual_field.value ${op} '${value}'"
319 } @virtual_fields ) );
323 $statement .= " $extra_sql" if defined($extra_sql);
325 warn "[debug]$me $statement\n" if $DEBUG > 1;
326 my $sth = $dbh->prepare($statement)
327 or croak "$dbh->errstr doing $statement";
332 grep defined( $record->{$_} ) && $record->{$_} ne '', @real_fields
334 if ( $record->{$field} =~ /^\d+(\.\d+)?$/
335 && $dbdef->table($table)->column($field)->type =~ /(int|serial)/i
337 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_INTEGER } );
339 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_VARCHAR } );
343 # $sth->execute( map $record->{$_},
344 # grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
345 # ) or croak "Error executing \"$statement\": ". $sth->errstr;
347 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
349 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
350 @virtual_fields = "FS::$table"->virtual_fields;
352 cluck "warning: FS::$table not loaded; virtual fields not returned either";
353 @virtual_fields = ();
357 tie %result, "Tie::IxHash";
358 my @stuff = @{ $sth->fetchall_arrayref( {} ) };
360 %result = map { $_->{$pkey}, $_ } @stuff;
362 @result{@stuff} = @stuff;
367 if ( keys(%result) and @virtual_fields ) {
369 "SELECT virtual_field.recnum, part_virtual_field.name, ".
370 "virtual_field.value ".
371 "FROM part_virtual_field JOIN virtual_field USING (vfieldpart) ".
372 "WHERE part_virtual_field.dbtable = '$table' AND ".
373 "virtual_field.recnum IN (".
374 join(',', keys(%result)). ") AND part_virtual_field.name IN ('".
375 join(q!', '!, @virtual_fields) . "')";
376 warn "[debug]$me $statement\n" if $DEBUG > 1;
377 $sth = $dbh->prepare($statement) or croak "$dbh->errstr doing $statement";
378 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
380 foreach (@{ $sth->fetchall_arrayref({}) }) {
381 my $recnum = $_->{recnum};
382 my $name = $_->{name};
383 my $value = $_->{value};
384 if (exists($result{$recnum})) {
385 $result{$recnum}->{$name} = $value;
390 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
391 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
392 #derivied class didn't override new method, so this optimization is safe
395 new_or_cached( "FS::$table", { %{$_} }, $cache )
399 new( "FS::$table", { %{$_} } )
403 warn "untested code (class FS::$table uses custom new method)";
405 eval 'FS::'. $table. '->new( { %{$_} } )';
409 # Check for encrypted fields and decrypt them.
410 if ($conf->exists('encryption') && eval 'defined(@FS::'. $table . '::encrypted_fields)') {
411 foreach my $record (@return) {
412 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
413 # Set it directly... This may cause a problem in the future...
414 $record->setfield($field, $record->decrypt($record->getfield($field)));
419 cluck "warning: FS::$table not loaded; returning FS::Record objects";
421 FS::Record->new( $table, { %{$_} } );
427 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
429 Experimental JOINed search method. Using this method, you can execute a
430 single SELECT spanning multiple tables, and cache the results for subsequent
431 method calls. Interface will almost definately change in an incompatible
439 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
440 my $cache = FS::SearchCache->new( $ptable, $pkey );
443 grep { !$saw{$_->getfield($pkey)}++ }
444 qsearch($table, $record, $select, $extra_sql, $cache )
448 =item qsearchs TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
450 Same as qsearch, except that if more than one record matches, it B<carp>s but
451 returns the first. If this happens, you either made a logic error in asking
452 for a single item, or your data is corrupted.
456 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
458 my(@result) = qsearch(@_);
459 cluck "warning: Multiple records in scalar search ($table)"
460 if scalar(@result) > 1;
461 #should warn more vehemently if the search was on a primary key?
462 scalar(@result) ? ($result[0]) : ();
473 Returns the table name.
478 # cluck "warning: FS::Record::table deprecated; supply one in subclass!";
485 Returns the DBIx::DBSchema::Table object for the table.
491 my($table)=$self->table;
492 $dbdef->table($table);
495 =item get, getfield COLUMN
497 Returns the value of the column/field/key COLUMN.
502 my($self,$field) = @_;
503 # to avoid "Use of unitialized value" errors
504 if ( defined ( $self->{Hash}->{$field} ) ) {
505 $self->{Hash}->{$field};
515 =item set, setfield COLUMN, VALUE
517 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
522 my($self,$field,$value) = @_;
523 $self->{'modified'} = 1;
524 $self->{'Hash'}->{$field} = $value;
531 =item AUTLOADED METHODS
533 $record->column is a synonym for $record->get('column');
535 $record->column('value') is a synonym for $record->set('column','value');
542 my($field)=$AUTOLOAD;
544 if ( defined($value) ) {
545 confess "errant AUTOLOAD $field for $self (arg $value)"
546 unless ref($self) && $self->can('setfield');
547 $self->setfield($field,$value);
549 confess "errant AUTOLOAD $field for $self (no args)"
550 unless ref($self) && $self->can('getfield');
551 $self->getfield($field);
557 # my $field = $AUTOLOAD;
559 # if ( defined($_[1]) ) {
560 # $_[0]->setfield($field, $_[1]);
562 # $_[0]->getfield($field);
568 Returns a list of the column/value pairs, usually for assigning to a new hash.
570 To make a distinct duplicate of an FS::Record object, you can do:
572 $new = new FS::Record ( $old->table, { $old->hash } );
578 confess $self. ' -> hash: Hash attribute is undefined'
579 unless defined($self->{'Hash'});
580 %{ $self->{'Hash'} };
585 Returns a reference to the column/value hash. This may be deprecated in the
586 future; if there's a reason you can't just use the autoloaded or get/set
598 Returns true if any of this object's values have been modified with set (or via
599 an autoloaded method). Doesn't yet recognize when you retreive a hashref and
611 Inserts this record to the database. If there is an error, returns the error,
612 otherwise returns false.
620 my $error = $self->check;
621 return $error if $error;
623 #single-field unique keys are given a value if false
624 #(like MySQL's AUTO_INCREMENT or Pg SERIAL)
625 foreach ( $self->dbdef_table->unique->singles ) {
626 $self->unique($_) unless $self->getfield($_);
629 #and also the primary key, if the database isn't going to
630 my $primary_key = $self->dbdef_table->primary_key;
632 if ( $primary_key ) {
633 my $col = $self->dbdef_table->column($primary_key);
636 uc($col->type) eq 'SERIAL'
637 || ( driver_name eq 'Pg'
638 && defined($col->default)
639 && $col->default =~ /^nextval\(/i
641 || ( driver_name eq 'mysql'
642 && defined($col->local)
643 && $col->local =~ /AUTO_INCREMENT/i
645 $self->unique($primary_key) unless $self->getfield($primary_key) || $db_seq;
648 my $table = $self->table;
651 # Encrypt before the database
652 if ($conf->exists('encryption') && defined(eval '@FS::'. $table . 'encrypted_fields')) {
653 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
654 $self->{'saved'} = $self->getfield($field);
655 $self->setfield($field, $self->enrypt($self->getfield($field)));
660 #false laziness w/delete
662 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
665 my @values = map { _quote( $self->getfield($_), $table, $_) } @real_fields;
668 my $statement = "INSERT INTO $table ( ".
669 join( ', ', @real_fields ).
671 join( ', ', @values ).
674 warn "[debug]$me $statement\n" if $DEBUG > 1;
675 my $sth = dbh->prepare($statement) or return dbh->errstr;
677 local $SIG{HUP} = 'IGNORE';
678 local $SIG{INT} = 'IGNORE';
679 local $SIG{QUIT} = 'IGNORE';
680 local $SIG{TERM} = 'IGNORE';
681 local $SIG{TSTP} = 'IGNORE';
682 local $SIG{PIPE} = 'IGNORE';
684 $sth->execute or return $sth->errstr;
687 if ( $db_seq ) { # get inserted id from the database, if applicable
688 warn "[debug]$me retreiving sequence from database\n" if $DEBUG;
689 if ( driver_name eq 'Pg' ) {
691 my $oid = $sth->{'pg_oid_status'};
692 my $i_sql = "SELECT $primary_key FROM $table WHERE oid = ?";
693 my $i_sth = dbh->prepare($i_sql) or do {
694 dbh->rollback if $FS::UID::AutoCommit;
697 $i_sth->execute($oid) or do {
698 dbh->rollback if $FS::UID::AutoCommit;
699 return $i_sth->errstr;
701 $insertid = $i_sth->fetchrow_arrayref->[0];
703 } elsif ( driver_name eq 'mysql' ) {
705 $insertid = dbh->{'mysql_insertid'};
706 # work around mysql_insertid being null some of the time, ala RT :/
707 unless ( $insertid ) {
708 warn "WARNING: DBD::mysql didn't return mysql_insertid; ".
709 "using SELECT LAST_INSERT_ID();";
710 my $i_sql = "SELECT LAST_INSERT_ID()";
711 my $i_sth = dbh->prepare($i_sql) or do {
712 dbh->rollback if $FS::UID::AutoCommit;
715 $i_sth->execute or do {
716 dbh->rollback if $FS::UID::AutoCommit;
717 return $i_sth->errstr;
719 $insertid = $i_sth->fetchrow_arrayref->[0];
723 dbh->rollback if $FS::UID::AutoCommit;
724 return "don't know how to retreive inserted ids from ". driver_name.
725 ", try using counterfiles (maybe run dbdef-create?)";
727 $self->setfield($primary_key, $insertid);
731 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
732 $self->virtual_fields;
733 if (@virtual_fields) {
734 my %v_values = map { $_, $self->getfield($_) } @virtual_fields;
736 my $vfieldpart = $self->vfieldpart_hashref;
738 my $v_statement = "INSERT INTO virtual_field(recnum, vfieldpart, value) ".
741 my $v_sth = dbh->prepare($v_statement) or do {
742 dbh->rollback if $FS::UID::AutoCommit;
746 foreach (keys(%v_values)) {
747 $v_sth->execute($self->getfield($primary_key),
751 dbh->rollback if $FS::UID::AutoCommit;
752 return $v_sth->errstr;
759 if ( defined $dbdef->table('h_'. $table) ) {
760 my $h_statement = $self->_h_statement('insert');
761 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
762 $h_sth = dbh->prepare($h_statement) or do {
763 dbh->rollback if $FS::UID::AutoCommit;
769 $h_sth->execute or return $h_sth->errstr if $h_sth;
771 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
773 # Now that it has been saved, reset the encrypted fields so that $new
775 foreach my $field (keys %{$saved}) {
776 $self->setfield($field, $saved->{$field});
784 Depriciated (use insert instead).
789 cluck "warning: FS::Record::add deprecated!";
790 insert @_; #call method in this scope
795 Delete this record from the database. If there is an error, returns the error,
796 otherwise returns false.
803 my $statement = "DELETE FROM ". $self->table. " WHERE ". join(' AND ',
805 $self->getfield($_) eq ''
806 #? "( $_ IS NULL OR $_ = \"\" )"
807 ? ( driver_name eq 'Pg'
809 : "( $_ IS NULL OR $_ = \"\" )"
811 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
812 } ( $self->dbdef_table->primary_key )
813 ? ( $self->dbdef_table->primary_key)
814 : real_fields($self->table)
816 warn "[debug]$me $statement\n" if $DEBUG > 1;
817 my $sth = dbh->prepare($statement) or return dbh->errstr;
820 if ( defined $dbdef->table('h_'. $self->table) ) {
821 my $h_statement = $self->_h_statement('delete');
822 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
823 $h_sth = dbh->prepare($h_statement) or return dbh->errstr;
828 my $primary_key = $self->dbdef_table->primary_key;
831 my $vfp = $self->vfieldpart_hashref;
832 foreach($self->virtual_fields) {
833 next if $self->getfield($_) eq '';
834 unless(@del_vfields) {
835 my $st = "DELETE FROM virtual_field WHERE recnum = ? AND vfieldpart = ?";
836 $v_sth = dbh->prepare($st) or return dbh->errstr;
838 push @del_vfields, $_;
841 local $SIG{HUP} = 'IGNORE';
842 local $SIG{INT} = 'IGNORE';
843 local $SIG{QUIT} = 'IGNORE';
844 local $SIG{TERM} = 'IGNORE';
845 local $SIG{TSTP} = 'IGNORE';
846 local $SIG{PIPE} = 'IGNORE';
848 my $rc = $sth->execute or return $sth->errstr;
849 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
850 $h_sth->execute or return $h_sth->errstr if $h_sth;
851 $v_sth->execute($self->getfield($primary_key), $vfp->{$_})
852 or return $v_sth->errstr
853 foreach (@del_vfields);
855 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
857 #no need to needlessly destoy the data either (causes problems actually)
858 #undef $self; #no need to keep object!
865 Depriciated (use delete instead).
870 cluck "warning: FS::Record::del deprecated!";
871 &delete(@_); #call method in this scope
874 =item replace OLD_RECORD
876 Replace the OLD_RECORD with this one in the database. If there is an error,
877 returns the error, otherwise returns false.
887 if (!defined($old)) {
888 warn "[debug]$me replace called with no arguments; autoloading old record\n"
890 my $primary_key = $new->dbdef_table->primary_key;
891 if ( $primary_key ) {
892 $old = qsearchs($new->table, { $primary_key => $new->$primary_key() } )
893 or croak "can't find ". $new->table. ".$primary_key ".
894 $new->$primary_key();
896 croak $new->table. " has no primary key; pass old record as argument";
900 warn "[debug]$me $new ->replace $old\n" if $DEBUG;
902 return "Records not in same table!" unless $new->table eq $old->table;
904 my $primary_key = $old->dbdef_table->primary_key;
905 return "Can't change $primary_key"
907 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
909 my $error = $new->check;
910 return $error if $error;
912 # Encrypt for replace
913 if ($conf->exists('encryption') && defined(eval '@FS::'. $new->table . 'encrypted_fields')) {
914 foreach my $field (eval '@FS::'. $new->table . '::encrypted_fields') {
915 $saved->{$field} = $new->getfield($field);
916 $new->setfield($field, $new->encrypt($new->getfield($field)));
920 #my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
921 my %diff = map { ($new->getfield($_) ne $old->getfield($_))
922 ? ($_, $new->getfield($_)) : () } $old->fields;
924 unless ( keys(%diff) ) {
925 carp "[warning]$me $new -> replace $old: records identical";
929 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
931 "$_ = ". _quote($new->getfield($_),$old->table,$_)
932 } real_fields($old->table)
937 if ( $old->getfield($_) eq '' ) {
939 #false laziness w/qsearch
940 if ( driver_name eq 'Pg' ) {
941 my $type = $old->dbdef_table->column($_)->type;
942 if ( $type =~ /(int|serial)/i ) {
945 qq-( $_ IS NULL OR $_ = '' )-;
948 qq-( $_ IS NULL OR $_ = "" )-;
952 "$_ = ". _quote($old->getfield($_),$old->table,$_);
955 } ( $primary_key ? ( $primary_key ) : real_fields($old->table) )
958 warn "[debug]$me $statement\n" if $DEBUG > 1;
959 my $sth = dbh->prepare($statement) or return dbh->errstr;
962 if ( defined $dbdef->table('h_'. $old->table) ) {
963 my $h_old_statement = $old->_h_statement('replace_old');
964 warn "[debug]$me $h_old_statement\n" if $DEBUG > 2;
965 $h_old_sth = dbh->prepare($h_old_statement) or return dbh->errstr;
971 if ( defined $dbdef->table('h_'. $new->table) ) {
972 my $h_new_statement = $new->_h_statement('replace_new');
973 warn "[debug]$me $h_new_statement\n" if $DEBUG > 2;
974 $h_new_sth = dbh->prepare($h_new_statement) or return dbh->errstr;
979 # For virtual fields we have three cases with different SQL
980 # statements: add, replace, delete
984 my (@add_vfields, @rep_vfields, @del_vfields);
985 my $vfp = $old->vfieldpart_hashref;
986 foreach(grep { exists($diff{$_}) } $new->virtual_fields) {
987 if($diff{$_} eq '') {
989 unless(@del_vfields) {
990 my $st = "DELETE FROM virtual_field WHERE recnum = ? ".
991 "AND vfieldpart = ?";
992 warn "[debug]$me $st\n" if $DEBUG > 2;
993 $v_del_sth = dbh->prepare($st) or return dbh->errstr;
995 push @del_vfields, $_;
996 } elsif($old->getfield($_) eq '') {
998 unless(@add_vfields) {
999 my $st = "INSERT INTO virtual_field (value, recnum, vfieldpart) ".
1001 warn "[debug]$me $st\n" if $DEBUG > 2;
1002 $v_add_sth = dbh->prepare($st) or return dbh->errstr;
1004 push @add_vfields, $_;
1007 unless(@rep_vfields) {
1008 my $st = "UPDATE virtual_field SET value = ? ".
1009 "WHERE recnum = ? AND vfieldpart = ?";
1010 warn "[debug]$me $st\n" if $DEBUG > 2;
1011 $v_rep_sth = dbh->prepare($st) or return dbh->errstr;
1013 push @rep_vfields, $_;
1017 local $SIG{HUP} = 'IGNORE';
1018 local $SIG{INT} = 'IGNORE';
1019 local $SIG{QUIT} = 'IGNORE';
1020 local $SIG{TERM} = 'IGNORE';
1021 local $SIG{TSTP} = 'IGNORE';
1022 local $SIG{PIPE} = 'IGNORE';
1024 my $rc = $sth->execute or return $sth->errstr;
1025 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
1026 $h_old_sth->execute or return $h_old_sth->errstr if $h_old_sth;
1027 $h_new_sth->execute or return $h_new_sth->errstr if $h_new_sth;
1029 $v_del_sth->execute($old->getfield($primary_key),
1031 or return $v_del_sth->errstr
1032 foreach(@del_vfields);
1034 $v_add_sth->execute($new->getfield($_),
1035 $old->getfield($primary_key),
1037 or return $v_add_sth->errstr
1038 foreach(@add_vfields);
1040 $v_rep_sth->execute($new->getfield($_),
1041 $old->getfield($primary_key),
1043 or return $v_rep_sth->errstr
1044 foreach(@rep_vfields);
1046 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1048 # Now that it has been saved, reset the encrypted fields so that $new
1049 # can still be used.
1050 foreach my $field (keys %{$saved}) {
1051 $new->setfield($field, $saved->{$field});
1060 Depriciated (use replace instead).
1065 cluck "warning: FS::Record::rep deprecated!";
1066 replace @_; #call method in this scope
1071 Checks virtual fields (using check_blocks). Subclasses should still provide
1072 a check method to validate real fields, foreign keys, etc., and call this
1073 method via $self->SUPER::check.
1075 (FIXME: Should this method try to make sure that it I<is> being called from
1076 a subclass's check method, to keep the current semantics as far as possible?)
1081 #confess "FS::Record::check not implemented; supply one in subclass!";
1084 foreach my $field ($self->virtual_fields) {
1085 for ($self->getfield($field)) {
1086 # See notes on check_block in FS::part_virtual_field.
1087 eval $self->pvf($field)->check_block;
1089 #this is bad, probably want to follow the stack backtrace up and see
1091 my $err = "Fatal error checking $field for $self";
1093 return "$err (see log for backtrace): $@";
1096 $self->setfield($field, $_);
1103 my( $self, $action ) = @_;
1106 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
1107 real_fields($self->table);
1109 my @values = map { _quote( $self->getfield($_), $self->table, $_) } @fields;
1111 "INSERT INTO h_". $self->table. " ( ".
1112 join(', ', qw(history_date history_user history_action), @fields ).
1114 join(', ', time, dbh->quote(getotaker()), dbh->quote($action), @values).
1121 B<Warning>: External use is B<deprecated>.
1123 Replaces COLUMN in record with a unique number, using counters in the
1124 filesystem. Used by the B<insert> method on single-field unique columns
1125 (see L<DBIx::DBSchema::Table>) and also as a fallback for primary keys
1126 that aren't SERIAL (Pg) or AUTO_INCREMENT (mysql).
1128 Returns the new value.
1133 my($self,$field) = @_;
1134 my($table)=$self->table;
1136 croak "Unique called on field $field, but it is ",
1137 $self->getfield($field),
1139 if $self->getfield($field);
1141 #warn "table $table is tainted" if is_tainted($table);
1142 #warn "field $field is tainted" if is_tainted($field);
1144 my($counter) = new File::CounterFile "$table.$field",0;
1146 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
1148 # my($counter) = new File::CounterFile "$user/$table.$field",0;
1151 my $index = $counter->inc;
1152 $index = $counter->inc while qsearchs($table, { $field=>$index } );
1154 $index =~ /^(\d*)$/;
1157 $self->setfield($field,$index);
1161 =item ut_float COLUMN
1163 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
1164 null. If there is an error, returns the error, otherwise returns false.
1169 my($self,$field)=@_ ;
1170 ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
1171 $self->getfield($field) =~ /^(\d+)$/ ||
1172 $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
1173 $self->getfield($field) =~ /^(\d+e\d+)$/)
1174 or return "Illegal or empty (float) $field: ". $self->getfield($field);
1175 $self->setfield($field,$1);
1179 =item ut_snumber COLUMN
1181 Check/untaint signed numeric data (whole numbers). May not be null. If there
1182 is an error, returns the error, otherwise returns false.
1187 my($self, $field) = @_;
1188 $self->getfield($field) =~ /^(-?)\s*(\d+)$/
1189 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1190 $self->setfield($field, "$1$2");
1194 =item ut_number COLUMN
1196 Check/untaint simple numeric data (whole numbers). May not be null. If there
1197 is an error, returns the error, otherwise returns false.
1202 my($self,$field)=@_;
1203 $self->getfield($field) =~ /^(\d+)$/
1204 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1205 $self->setfield($field,$1);
1209 =item ut_numbern COLUMN
1211 Check/untaint simple numeric data (whole numbers). May be null. If there is
1212 an error, returns the error, otherwise returns false.
1217 my($self,$field)=@_;
1218 $self->getfield($field) =~ /^(\d*)$/
1219 or return "Illegal (numeric) $field: ". $self->getfield($field);
1220 $self->setfield($field,$1);
1224 =item ut_money COLUMN
1226 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
1227 is an error, returns the error, otherwise returns false.
1232 my($self,$field)=@_;
1233 $self->setfield($field, 0) if $self->getfield($field) eq '';
1234 $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
1235 or return "Illegal (money) $field: ". $self->getfield($field);
1236 #$self->setfield($field, "$1$2$3" || 0);
1237 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
1241 =item ut_text COLUMN
1243 Check/untaint text. Alphanumerics, spaces, and the following punctuation
1244 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / =
1245 May not be null. If there is an error, returns the error, otherwise returns
1251 my($self,$field)=@_;
1252 #warn "msgcat ". \&msgcat. "\n";
1253 #warn "notexist ". \¬exist. "\n";
1254 #warn "AUTOLOAD ". \&AUTOLOAD. "\n";
1255 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]+)$/
1256 or return gettext('illegal_or_empty_text'). " $field: ".
1257 $self->getfield($field);
1258 $self->setfield($field,$1);
1262 =item ut_textn COLUMN
1264 Check/untaint text. Alphanumerics, spaces, and the following punctuation
1265 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
1266 May be null. If there is an error, returns the error, otherwise returns false.
1271 my($self,$field)=@_;
1272 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
1273 or return gettext('illegal_text'). " $field: ". $self->getfield($field);
1274 $self->setfield($field,$1);
1278 =item ut_alpha COLUMN
1280 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
1281 an error, returns the error, otherwise returns false.
1286 my($self,$field)=@_;
1287 $self->getfield($field) =~ /^(\w+)$/
1288 or return "Illegal or empty (alphanumeric) $field: ".
1289 $self->getfield($field);
1290 $self->setfield($field,$1);
1294 =item ut_alpha COLUMN
1296 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
1297 error, returns the error, otherwise returns false.
1302 my($self,$field)=@_;
1303 $self->getfield($field) =~ /^(\w*)$/
1304 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
1305 $self->setfield($field,$1);
1309 =item ut_phonen COLUMN [ COUNTRY ]
1311 Check/untaint phone numbers. May be null. If there is an error, returns
1312 the error, otherwise returns false.
1314 Takes an optional two-letter ISO country code; without it or with unsupported
1315 countries, ut_phonen simply calls ut_alphan.
1320 my( $self, $field, $country ) = @_;
1321 return $self->ut_alphan($field) unless defined $country;
1322 my $phonen = $self->getfield($field);
1323 if ( $phonen eq '' ) {
1324 $self->setfield($field,'');
1325 } elsif ( $country eq 'US' || $country eq 'CA' ) {
1327 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
1328 or return gettext('illegal_phone'). " $field: ". $self->getfield($field);
1329 $phonen = "$1-$2-$3";
1330 $phonen .= " x$4" if $4;
1331 $self->setfield($field,$phonen);
1333 warn "warning: don't know how to check phone numbers for country $country";
1334 return $self->ut_textn($field);
1341 Check/untaint ip addresses. IPv4 only for now.
1346 my( $self, $field ) = @_;
1347 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
1348 or return "Illegal (IP address) $field: ". $self->getfield($field);
1349 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
1350 $self->setfield($field, "$1.$2.$3.$4");
1356 Check/untaint ip addresses. IPv4 only for now. May be null.
1361 my( $self, $field ) = @_;
1362 if ( $self->getfield($field) =~ /^()$/ ) {
1363 $self->setfield($field,'');
1366 $self->ut_ip($field);
1370 =item ut_domain COLUMN
1372 Check/untaint host and domain names.
1377 my( $self, $field ) = @_;
1378 #$self->getfield($field) =~/^(\w+\.)*\w+$/
1379 $self->getfield($field) =~/^(([\w\-]+\.)*\w+)$/
1380 or return "Illegal (domain) $field: ". $self->getfield($field);
1381 $self->setfield($field,$1);
1385 =item ut_name COLUMN
1387 Check/untaint proper names; allows alphanumerics, spaces and the following
1388 punctuation: , . - '
1395 my( $self, $field ) = @_;
1396 $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
1397 or return gettext('illegal_name'). " $field: ". $self->getfield($field);
1398 $self->setfield($field,$1);
1404 Check/untaint zip codes.
1409 my( $self, $field, $country ) = @_;
1410 if ( $country eq 'US' ) {
1411 $self->getfield($field) =~ /\s*(\d{5}(\-\d{4})?)\s*$/
1412 or return gettext('illegal_zip'). " $field for country $country: ".
1413 $self->getfield($field);
1414 $self->setfield($field,$1);
1416 if ( $self->getfield($field) =~ /^\s*$/ ) {
1417 $self->setfield($field,'');
1419 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
1420 or return gettext('illegal_zip'). " $field: ". $self->getfield($field);
1421 $self->setfield($field,$1);
1427 =item ut_country COLUMN
1429 Check/untaint country codes. Country names are changed to codes, if possible -
1430 see L<Locale::Country>.
1435 my( $self, $field ) = @_;
1436 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
1437 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
1438 && country2code($1) ) {
1439 $self->setfield($field,uc(country2code($1)));
1442 $self->getfield($field) =~ /^(\w\w)$/
1443 or return "Illegal (country) $field: ". $self->getfield($field);
1444 $self->setfield($field,uc($1));
1448 =item ut_anything COLUMN
1450 Untaints arbitrary data. Be careful.
1455 my( $self, $field ) = @_;
1456 $self->getfield($field) =~ /^(.*)$/s
1457 or return "Illegal $field: ". $self->getfield($field);
1458 $self->setfield($field,$1);
1462 =item ut_enum COLUMN CHOICES_ARRAYREF
1464 Check/untaint a column, supplying all possible choices, like the "enum" type.
1469 my( $self, $field, $choices ) = @_;
1470 foreach my $choice ( @$choices ) {
1471 if ( $self->getfield($field) eq $choice ) {
1472 $self->setfield($choice);
1476 return "Illegal (enum) field $field: ". $self->getfield($field);
1479 =item ut_foreign_key COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1481 Check/untaint a foreign column key. Call a regular ut_ method (like ut_number)
1482 on the column first.
1486 sub ut_foreign_key {
1487 my( $self, $field, $table, $foreign ) = @_;
1488 qsearchs($table, { $foreign => $self->getfield($field) })
1489 or return "Can't find ". $self->table. ".$field ". $self->getfield($field).
1490 " in $table.$foreign";
1494 =item ut_foreign_keyn COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1496 Like ut_foreign_key, except the null value is also allowed.
1500 sub ut_foreign_keyn {
1501 my( $self, $field, $table, $foreign ) = @_;
1502 $self->getfield($field)
1503 ? $self->ut_foreign_key($field, $table, $foreign)
1508 =item virtual_fields [ TABLE ]
1510 Returns a list of virtual fields defined for the table. This should not
1511 be exported, and should only be called as an instance or class method.
1515 sub virtual_fields {
1518 $table = $self->table or confess "virtual_fields called on non-table";
1520 confess "Unknown table $table" unless $dbdef->table($table);
1522 return () unless $self->dbdef->table('part_virtual_field');
1524 unless ( $virtual_fields_cache{$table} ) {
1525 my $query = 'SELECT name from part_virtual_field ' .
1526 "WHERE dbtable = '$table'";
1528 my $result = $dbh->selectcol_arrayref($query);
1529 confess $dbh->errstr if $dbh->err;
1530 $virtual_fields_cache{$table} = $result;
1533 @{$virtual_fields_cache{$table}};
1538 =item fields [ TABLE ]
1540 This is a wrapper for real_fields and virtual_fields. Code that called
1541 fields before should probably continue to call fields.
1546 my $something = shift;
1548 if($something->isa('FS::Record')) {
1549 $table = $something->table;
1551 $table = $something;
1552 $something = "FS::$table";
1554 return (real_fields($table), $something->virtual_fields());
1559 =item pvf FIELD_NAME
1561 Returns the FS::part_virtual_field object corresponding to a field in the
1562 record (specified by FIELD_NAME).
1567 my ($self, $name) = (shift, shift);
1569 if(grep /^$name$/, $self->virtual_fields) {
1570 return qsearchs('part_virtual_field', { dbtable => $self->table,
1580 =item real_fields [ TABLE ]
1582 Returns a list of the real columns in the specified table. Called only by
1583 fields() and other subroutines elsewhere in FS::Record.
1590 my($table_obj) = $dbdef->table($table);
1591 confess "Unknown table $table" unless $table_obj;
1592 $table_obj->columns;
1595 =item reload_dbdef([FILENAME])
1597 Load a database definition (see L<DBIx::DBSchema>), optionally from a
1598 non-default filename. This command is executed at startup unless
1599 I<$FS::Record::setup_hack> is true. Returns a DBIx::DBSchema object.
1604 my $file = shift || $dbdef_file;
1606 unless ( exists $dbdef_cache{$file} ) {
1607 warn "[debug]$me loading dbdef for $file\n" if $DEBUG;
1608 $dbdef_cache{$file} = DBIx::DBSchema->load( $file )
1609 or die "can't load database schema from $file";
1611 warn "[debug]$me re-using cached dbdef for $file\n" if $DEBUG;
1613 $dbdef = $dbdef_cache{$file};
1618 Returns the current database definition. See L<DBIx::DBSchema>.
1622 sub dbdef { $dbdef; }
1624 =item _quote VALUE, TABLE, COLUMN
1626 This is an internal function used to construct SQL statements. It returns
1627 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
1628 type (see L<DBIx::DBSchema::Column>) does not end in `char' or `binary'.
1633 my($value, $table, $column) = @_;
1634 my $column_obj = $dbdef->table($table)->column($column);
1635 my $column_type = $column_obj->type;
1636 my $nullable = $column_obj->null;
1638 warn " $table.$column: $value ($column_type".
1639 ( $nullable ? ' NULL' : ' NOT NULL' ).
1640 ")\n" if $DEBUG > 2;
1642 if ( $value eq '' && $column_type =~ /^int/ ) {
1646 cluck "WARNING: Attempting to set non-null integer $table.$column null; ".
1650 } elsif ( $value =~ /^\d+(\.\d+)?$/ &&
1651 ! $column_type =~ /(char|binary|text)$/i ) {
1658 =item vfieldpart_hashref TABLE
1660 Returns a hashref of virtual field names and vfieldparts applicable to the given
1665 sub vfieldpart_hashref {
1667 my $table = $self->table;
1669 return {} unless $self->dbdef->table('part_virtual_field');
1672 my $statement = "SELECT vfieldpart, name FROM part_virtual_field WHERE ".
1673 "dbtable = '$table'";
1674 my $sth = $dbh->prepare($statement);
1675 $sth->execute or croak "Execution of '$statement' failed: ".$dbh->errstr;
1676 return { map { $_->{name}, $_->{vfieldpart} }
1677 @{$sth->fetchall_arrayref({})} };
1684 This is deprecated. Don't use it.
1686 It returns a hash-type list with the fields of this record's table set true.
1691 carp "warning: hfields is deprecated";
1694 foreach (fields($table)) {
1703 "$_: ". $self->getfield($_). "|"
1704 } (fields($self->table)) );
1708 my ($self, $value) = @_;
1711 if ($conf->exists('encryption')) {
1712 if ($self->is_encrypted($value)) {
1713 # Return the original value if it isn't plaintext.
1714 $encrypted = $value;
1717 if (ref($rsa_encrypt) =~ /::RSA/) { # We Can Encrypt
1718 # RSA doesn't like the empty string so let's pack it up
1719 # The database doesn't like the RSA data so uuencode it
1720 my $length = length($value)+1;
1721 $encrypted = pack("u*",$rsa_encrypt->encrypt(pack("Z$length",$value)));
1723 die ("You can't encrypt w/o a valid RSA engine - Check your installation or disable encryption");
1731 my ($self, $value) = @_;
1732 # Possible Bug - Some work may be required here....
1734 if (length($value) > 80) {
1742 my ($self,$value) = @_;
1743 my $decrypted = $value; # Will return the original value if it isn't encrypted or can't be decrypted.
1744 if ($conf->exists('encryption') && $self->is_encrypted($value)) {
1746 if (ref($rsa_decrypt) =~ /::RSA/) {
1747 my $encrypted = unpack ("u*", $value);
1748 $decrypted = unpack("Z*", $rsa_decrypt->decrypt($encrypted));
1756 #Initialize the Module
1757 $rsa_module = 'Crypt::OpenSSL::RSA'; # The Default
1759 if ($conf->exists('encryptionmodule') && $conf->config('encryptionmodule') ne '') {
1760 $rsa_module = $conf->config('encryptionmodule');
1764 eval ("require $rsa_module"); # No need to import the namespace
1767 # Initialize Encryption
1768 if ($conf->exists('encryptionpublickey') && $conf->config('encryptionpublickey') ne '') {
1769 my $public_key = join("\n",$conf->config('encryptionpublickey'));
1770 $rsa_encrypt = $rsa_module->new_public_key($public_key);
1773 # Intitalize Decryption
1774 if ($conf->exists('encryptionprivatekey') && $conf->config('encryptionprivatekey') ne '') {
1775 my $private_key = join("\n",$conf->config('encryptionprivatekey'));
1776 $rsa_decrypt = $rsa_module->new_private_key($private_key);
1780 sub DESTROY { return; }
1784 # #use Carp qw(cluck);
1785 # #cluck "DESTROYING $self";
1786 # warn "DESTROYING $self";
1790 # return ! eval { join('',@_), kill 0; 1; };
1797 This module should probably be renamed, since much of the functionality is
1798 of general use. It is not completely unlike Adapter::DBI (see below).
1800 Exported qsearch and qsearchs should be deprecated in favor of method calls
1801 (against an FS::Record object like the old search and searchs that qsearch
1802 and qsearchs were on top of.)
1804 The whole fields / hfields mess should be removed.
1806 The various WHERE clauses should be subroutined.
1808 table string should be deprecated in favor of DBIx::DBSchema::Table.
1810 No doubt we could benefit from a Tied hash. Documenting how exists / defined
1811 true maps to the database (and WHERE clauses) would also help.
1813 The ut_ methods should ask the dbdef for a default length.
1815 ut_sqltype (like ut_varchar) should all be defined
1817 A fallback check method should be provided which uses the dbdef.
1819 The ut_money method assumes money has two decimal digits.
1821 The Pg money kludge in the new method only strips `$'.
1823 The ut_phonen method only checks US-style phone numbers.
1825 The _quote function should probably use ut_float instead of a regex.
1827 All the subroutines probably should be methods, here or elsewhere.
1829 Probably should borrow/use some dbdef methods where appropriate (like sub
1832 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
1833 or allow it to be set. Working around it is ugly any way around - DBI should
1834 be fixed. (only affects RDBMS which return uppercase column names)
1836 ut_zip should take an optional country like ut_phone.
1840 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
1842 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.