4 use vars qw( $AUTOLOAD @ISA @EXPORT_OK $DEBUG
5 $me %virtual_fields_cache $nowarn_identical );
7 use Carp qw(carp cluck croak confess);
10 use DBI qw(:sql_types);
11 use DBIx::DBSchema 0.25;
12 use FS::UID qw(dbh getotaker datasrc driver_name);
13 use FS::Schema qw(dbdef);
15 use FS::Msgcat qw(gettext);
18 use FS::part_virtual_field;
24 #export dbdef for now... everything else expects to find it here
25 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch);
30 $nowarn_identical = 0;
38 FS::UID->install_callback( sub {
40 $File::CounterFile::DEFAULT_DIR = "/usr/local/etc/freeside/counters.". datasrc;
45 FS::Record - Database record objects
50 use FS::Record qw(dbh fields qsearch qsearchs);
52 $record = new FS::Record 'table', \%hash;
53 $record = new FS::Record 'table', { 'column' => 'value', ... };
55 $record = qsearchs FS::Record 'table', \%hash;
56 $record = qsearchs FS::Record 'table', { 'column' => 'value', ... };
57 @records = qsearch FS::Record 'table', \%hash;
58 @records = qsearch FS::Record 'table', { 'column' => 'value', ... };
60 $table = $record->table;
61 $dbdef_table = $record->dbdef_table;
63 $value = $record->get('column');
64 $value = $record->getfield('column');
65 $value = $record->column;
67 $record->set( 'column' => 'value' );
68 $record->setfield( 'column' => 'value' );
69 $record->column('value');
71 %hash = $record->hash;
73 $hashref = $record->hashref;
75 $error = $record->insert;
77 $error = $record->delete;
79 $error = $new_record->replace($old_record);
81 # external use deprecated - handled by the database (at least for Pg, mysql)
82 $value = $record->unique('column');
84 $error = $record->ut_float('column');
85 $error = $record->ut_number('column');
86 $error = $record->ut_numbern('column');
87 $error = $record->ut_money('column');
88 $error = $record->ut_text('column');
89 $error = $record->ut_textn('column');
90 $error = $record->ut_alpha('column');
91 $error = $record->ut_alphan('column');
92 $error = $record->ut_phonen('column');
93 $error = $record->ut_anything('column');
94 $error = $record->ut_name('column');
96 $quoted_value = _quote($value,'table','field');
99 $fields = hfields('table');
100 if ( $fields->{Field} ) { # etc.
102 @fields = fields 'table'; #as a subroutine
103 @fields = $record->fields; #as a method call
108 (Mostly) object-oriented interface to database records. Records are currently
109 implemented on top of DBI. FS::Record is intended as a base class for
110 table-specific classes to inherit from, i.e. FS::cust_main.
116 =item new [ TABLE, ] HASHREF
118 Creates a new record. It doesn't store it in the database, though. See
119 L<"insert"> for that.
121 Note that the object stores this hash reference, not a distinct copy of the
122 hash it points to. You can ask the object for a copy with the I<hash>
125 TABLE can only be omitted when a dervived class overrides the table method.
131 my $class = ref($proto) || $proto;
133 bless ($self, $class);
135 unless ( defined ( $self->table ) ) {
136 $self->{'Table'} = shift;
137 carp "warning: FS::Record::new called with table name ". $self->{'Table'};
140 $self->{'Hash'} = shift;
142 foreach my $field ( grep !defined($self->{'Hash'}{$_}), $self->fields ) {
143 $self->{'Hash'}{$field}='';
146 $self->_rebless if $self->can('_rebless');
148 $self->{'modified'} = 0;
150 $self->_cache($self->{'Hash'}, shift) if $self->can('_cache') && @_;
157 my $class = ref($proto) || $proto;
159 bless ($self, $class);
161 $self->{'Table'} = shift unless defined ( $self->table );
163 my $hashref = $self->{'Hash'} = shift;
165 if ( defined( $cache->cache->{$hashref->{$cache->key}} ) ) {
166 my $obj = $cache->cache->{$hashref->{$cache->key}};
167 $obj->_cache($hashref, $cache) if $obj->can('_cache');
170 $cache->cache->{$hashref->{$cache->key}} = $self->new($hashref, $cache);
177 my $class = ref($proto) || $proto;
179 bless ($self, $class);
180 if ( defined $self->table ) {
181 cluck "create constructor is deprecated, use new!";
184 croak "FS::Record::create called (not from a subclass)!";
188 =item qsearch TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
190 Searches the database for all records matching (at least) the key/value pairs
191 in HASHREF. Returns all the records found as `FS::TABLE' objects if that
192 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
195 ###oops, argh, FS::Record::new only lets us create database fields.
196 #Normal behaviour if SELECT is not specified is `*', as in
197 #C<SELECT * FROM table WHERE ...>. However, there is an experimental new
198 #feature where you can specify SELECT - remember, the objects returned,
199 #although blessed into the appropriate `FS::TABLE' package, will only have the
200 #fields you specify. This might have unwanted results if you then go calling
201 #regular FS::TABLE methods
207 my($stable, $record, $select, $extra_sql, $cache, $addl_from ) = @_;
208 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
210 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
215 my $table = $cache ? $cache->table : $stable;
216 my $dbdef_table = dbdef->table($table)
217 or die "No schema for table $table found - ".
218 "do you need to create it or run dbdef-create?";
219 my $pkey = $dbdef_table->primary_key;
221 my @real_fields = grep exists($record->{$_}), real_fields($table);
223 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
224 @virtual_fields = grep exists($record->{$_}), "FS::$table"->virtual_fields;
226 cluck "warning: FS::$table not loaded; virtual fields not searchable";
227 @virtual_fields = ();
230 my $statement = "SELECT $select FROM $stable";
231 $statement .= " $addl_from" if $addl_from;
232 if ( @real_fields or @virtual_fields ) {
233 $statement .= ' WHERE '. join(' AND ',
238 if ( ref($record->{$_}) ) {
239 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
240 #$op = 'LIKE' if $op =~ /^ILIKE$/i && driver_name ne 'Pg';
241 if ( uc($op) eq 'ILIKE' ) {
243 $record->{$_}{'value'} = lc($record->{$_}{'value'});
244 $column = "LOWER($_)";
246 $record->{$_} = $record->{$_}{'value'}
249 if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) {
251 if ( driver_name eq 'Pg' ) {
252 my $type = dbdef->table($table)->column($column)->type;
253 if ( $type =~ /(int|serial)/i ) {
254 qq-( $column IS NULL )-;
256 qq-( $column IS NULL OR $column = '' )-;
259 qq-( $column IS NULL OR $column = "" )-;
261 } elsif ( $op eq '!=' ) {
262 if ( driver_name eq 'Pg' ) {
263 my $type = dbdef->table($table)->column($column)->type;
264 if ( $type =~ /(int|serial)/i ) {
265 qq-( $column IS NOT NULL )-;
267 qq-( $column IS NOT NULL AND $column != '' )-;
270 qq-( $column IS NOT NULL AND $column != "" )-;
273 if ( driver_name eq 'Pg' ) {
274 qq-( $column $op '' )-;
276 qq-( $column $op "" )-;
286 if ( ref($record->{$_}) ) {
287 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
288 if ( uc($op) eq 'ILIKE' ) {
290 $record->{$_}{'value'} = lc($record->{$_}{'value'});
291 $column = "LOWER($_)";
293 $record->{$_} = $record->{$_}{'value'};
296 # ... EXISTS ( SELECT name, value FROM part_virtual_field
298 # ON part_virtual_field.vfieldpart = virtual_field.vfieldpart
299 # WHERE recnum = svc_acct.svcnum
300 # AND (name, value) = ('egad', 'brain') )
302 my $value = $record->{$_};
306 $subq = ($value ? 'EXISTS ' : 'NOT EXISTS ') .
307 "( SELECT part_virtual_field.name, virtual_field.value ".
308 "FROM part_virtual_field JOIN virtual_field ".
309 "ON part_virtual_field.vfieldpart = virtual_field.vfieldpart ".
310 "WHERE virtual_field.recnum = ${table}.${pkey} ".
311 "AND part_virtual_field.name = '${column}'".
313 " AND virtual_field.value ${op} '${value}'"
317 } @virtual_fields ) );
321 $statement .= " $extra_sql" if defined($extra_sql);
323 warn "[debug]$me $statement\n" if $DEBUG > 1;
324 my $sth = $dbh->prepare($statement)
325 or croak "$dbh->errstr doing $statement";
330 grep defined( $record->{$_} ) && $record->{$_} ne '', @real_fields
332 if ( $record->{$field} =~ /^\d+(\.\d+)?$/
333 && dbdef->table($table)->column($field)->type =~ /(int|serial)/i
335 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_INTEGER } );
337 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_VARCHAR } );
341 # $sth->execute( map $record->{$_},
342 # grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
343 # ) or croak "Error executing \"$statement\": ". $sth->errstr;
345 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
347 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
348 @virtual_fields = "FS::$table"->virtual_fields;
350 cluck "warning: FS::$table not loaded; virtual fields not returned either";
351 @virtual_fields = ();
355 tie %result, "Tie::IxHash";
356 my @stuff = @{ $sth->fetchall_arrayref( {} ) };
358 %result = map { $_->{$pkey}, $_ } @stuff;
360 @result{@stuff} = @stuff;
365 if ( keys(%result) and @virtual_fields ) {
367 "SELECT virtual_field.recnum, part_virtual_field.name, ".
368 "virtual_field.value ".
369 "FROM part_virtual_field JOIN virtual_field USING (vfieldpart) ".
370 "WHERE part_virtual_field.dbtable = '$table' AND ".
371 "virtual_field.recnum IN (".
372 join(',', keys(%result)). ") AND part_virtual_field.name IN ('".
373 join(q!', '!, @virtual_fields) . "')";
374 warn "[debug]$me $statement\n" if $DEBUG > 1;
375 $sth = $dbh->prepare($statement) or croak "$dbh->errstr doing $statement";
376 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
378 foreach (@{ $sth->fetchall_arrayref({}) }) {
379 my $recnum = $_->{recnum};
380 my $name = $_->{name};
381 my $value = $_->{value};
382 if (exists($result{$recnum})) {
383 $result{$recnum}->{$name} = $value;
388 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
389 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
390 #derivied class didn't override new method, so this optimization is safe
393 new_or_cached( "FS::$table", { %{$_} }, $cache )
397 new( "FS::$table", { %{$_} } )
401 warn "untested code (class FS::$table uses custom new method)";
403 eval 'FS::'. $table. '->new( { %{$_} } )';
407 # Check for encrypted fields and decrypt them.
408 if ($conf->exists('encryption') && eval 'defined(@FS::'. $table . '::encrypted_fields)') {
409 foreach my $record (@return) {
410 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
411 # Set it directly... This may cause a problem in the future...
412 $record->setfield($field, $record->decrypt($record->getfield($field)));
417 cluck "warning: FS::$table not loaded; returning FS::Record objects";
419 FS::Record->new( $table, { %{$_} } );
425 =item by_key PRIMARY_KEY_VALUE
427 This is a class method that returns the record with the given primary key
428 value. This method is only useful in FS::Record subclasses. For example:
430 my $cust_main = FS::cust_main->by_key(1); # retrieve customer with custnum 1
434 my $cust_main = qsearchs('cust_main', { 'custnum' => 1 } );
439 my ($class, $pkey_value) = @_;
441 my $table = $class->table
442 or croak "No table for $class found";
444 my $dbdef_table = dbdef->table($table)
445 or die "No schema for table $table found - ".
446 "do you need to create it or run dbdef-create?";
447 my $pkey = $dbdef_table->primary_key
448 or die "No primary key for table $table";
450 return qsearchs($table, { $pkey => $pkey_value });
453 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
455 Experimental JOINed search method. Using this method, you can execute a
456 single SELECT spanning multiple tables, and cache the results for subsequent
457 method calls. Interface will almost definately change in an incompatible
465 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
466 my $cache = FS::SearchCache->new( $ptable, $pkey );
469 grep { !$saw{$_->getfield($pkey)}++ }
470 qsearch($table, $record, $select, $extra_sql, $cache )
474 =item qsearchs TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
476 Same as qsearch, except that if more than one record matches, it B<carp>s but
477 returns the first. If this happens, you either made a logic error in asking
478 for a single item, or your data is corrupted.
482 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
484 my(@result) = qsearch(@_);
485 cluck "warning: Multiple records in scalar search ($table)"
486 if scalar(@result) > 1;
487 #should warn more vehemently if the search was on a primary key?
488 scalar(@result) ? ($result[0]) : ();
499 Returns the table name.
504 # cluck "warning: FS::Record::table deprecated; supply one in subclass!";
511 Returns the DBIx::DBSchema::Table object for the table.
517 my($table)=$self->table;
518 dbdef->table($table);
521 =item get, getfield COLUMN
523 Returns the value of the column/field/key COLUMN.
528 my($self,$field) = @_;
529 # to avoid "Use of unitialized value" errors
530 if ( defined ( $self->{Hash}->{$field} ) ) {
531 $self->{Hash}->{$field};
541 =item set, setfield COLUMN, VALUE
543 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
548 my($self,$field,$value) = @_;
549 $self->{'modified'} = 1;
550 $self->{'Hash'}->{$field} = $value;
557 =item AUTLOADED METHODS
559 $record->column is a synonym for $record->get('column');
561 $record->column('value') is a synonym for $record->set('column','value');
568 my($field)=$AUTOLOAD;
570 if ( defined($value) ) {
571 confess "errant AUTOLOAD $field for $self (arg $value)"
572 unless ref($self) && $self->can('setfield');
573 $self->setfield($field,$value);
575 confess "errant AUTOLOAD $field for $self (no args)"
576 unless ref($self) && $self->can('getfield');
577 $self->getfield($field);
583 # my $field = $AUTOLOAD;
585 # if ( defined($_[1]) ) {
586 # $_[0]->setfield($field, $_[1]);
588 # $_[0]->getfield($field);
594 Returns a list of the column/value pairs, usually for assigning to a new hash.
596 To make a distinct duplicate of an FS::Record object, you can do:
598 $new = new FS::Record ( $old->table, { $old->hash } );
604 confess $self. ' -> hash: Hash attribute is undefined'
605 unless defined($self->{'Hash'});
606 %{ $self->{'Hash'} };
611 Returns a reference to the column/value hash. This may be deprecated in the
612 future; if there's a reason you can't just use the autoloaded or get/set
624 Returns true if any of this object's values have been modified with set (or via
625 an autoloaded method). Doesn't yet recognize when you retreive a hashref and
637 Inserts this record to the database. If there is an error, returns the error,
638 otherwise returns false.
646 my $error = $self->check;
647 return $error if $error;
649 #single-field unique keys are given a value if false
650 #(like MySQL's AUTO_INCREMENT or Pg SERIAL)
651 foreach ( $self->dbdef_table->unique->singles ) {
652 $self->unique($_) unless $self->getfield($_);
655 #and also the primary key, if the database isn't going to
656 my $primary_key = $self->dbdef_table->primary_key;
658 if ( $primary_key ) {
659 my $col = $self->dbdef_table->column($primary_key);
662 uc($col->type) eq 'SERIAL'
663 || ( driver_name eq 'Pg'
664 && defined($col->default)
665 && $col->default =~ /^nextval\(/i
667 || ( driver_name eq 'mysql'
668 && defined($col->local)
669 && $col->local =~ /AUTO_INCREMENT/i
671 $self->unique($primary_key) unless $self->getfield($primary_key) || $db_seq;
674 my $table = $self->table;
677 # Encrypt before the database
678 if ($conf->exists('encryption') && defined(eval '@FS::'. $table . 'encrypted_fields')) {
679 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
680 $self->{'saved'} = $self->getfield($field);
681 $self->setfield($field, $self->enrypt($self->getfield($field)));
686 #false laziness w/delete
688 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
691 my @values = map { _quote( $self->getfield($_), $table, $_) } @real_fields;
694 my $statement = "INSERT INTO $table ( ".
695 join( ', ', @real_fields ).
697 join( ', ', @values ).
700 warn "[debug]$me $statement\n" if $DEBUG > 1;
701 my $sth = dbh->prepare($statement) or return dbh->errstr;
703 local $SIG{HUP} = 'IGNORE';
704 local $SIG{INT} = 'IGNORE';
705 local $SIG{QUIT} = 'IGNORE';
706 local $SIG{TERM} = 'IGNORE';
707 local $SIG{TSTP} = 'IGNORE';
708 local $SIG{PIPE} = 'IGNORE';
710 $sth->execute or return $sth->errstr;
713 if ( $db_seq ) { # get inserted id from the database, if applicable
714 warn "[debug]$me retreiving sequence from database\n" if $DEBUG;
715 if ( driver_name eq 'Pg' ) {
717 my $oid = $sth->{'pg_oid_status'};
718 my $i_sql = "SELECT $primary_key FROM $table WHERE oid = ?";
719 my $i_sth = dbh->prepare($i_sql) or do {
720 dbh->rollback if $FS::UID::AutoCommit;
723 $i_sth->execute($oid) or do {
724 dbh->rollback if $FS::UID::AutoCommit;
725 return $i_sth->errstr;
727 $insertid = $i_sth->fetchrow_arrayref->[0];
729 } elsif ( driver_name eq 'mysql' ) {
731 $insertid = dbh->{'mysql_insertid'};
732 # work around mysql_insertid being null some of the time, ala RT :/
733 unless ( $insertid ) {
734 warn "WARNING: DBD::mysql didn't return mysql_insertid; ".
735 "using SELECT LAST_INSERT_ID();";
736 my $i_sql = "SELECT LAST_INSERT_ID()";
737 my $i_sth = dbh->prepare($i_sql) or do {
738 dbh->rollback if $FS::UID::AutoCommit;
741 $i_sth->execute or do {
742 dbh->rollback if $FS::UID::AutoCommit;
743 return $i_sth->errstr;
745 $insertid = $i_sth->fetchrow_arrayref->[0];
749 dbh->rollback if $FS::UID::AutoCommit;
750 return "don't know how to retreive inserted ids from ". driver_name.
751 ", try using counterfiles (maybe run dbdef-create?)";
753 $self->setfield($primary_key, $insertid);
757 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
758 $self->virtual_fields;
759 if (@virtual_fields) {
760 my %v_values = map { $_, $self->getfield($_) } @virtual_fields;
762 my $vfieldpart = $self->vfieldpart_hashref;
764 my $v_statement = "INSERT INTO virtual_field(recnum, vfieldpart, value) ".
767 my $v_sth = dbh->prepare($v_statement) or do {
768 dbh->rollback if $FS::UID::AutoCommit;
772 foreach (keys(%v_values)) {
773 $v_sth->execute($self->getfield($primary_key),
777 dbh->rollback if $FS::UID::AutoCommit;
778 return $v_sth->errstr;
785 if ( defined dbdef->table('h_'. $table) ) {
786 my $h_statement = $self->_h_statement('insert');
787 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
788 $h_sth = dbh->prepare($h_statement) or do {
789 dbh->rollback if $FS::UID::AutoCommit;
795 $h_sth->execute or return $h_sth->errstr if $h_sth;
797 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
799 # Now that it has been saved, reset the encrypted fields so that $new
801 foreach my $field (keys %{$saved}) {
802 $self->setfield($field, $saved->{$field});
810 Depriciated (use insert instead).
815 cluck "warning: FS::Record::add deprecated!";
816 insert @_; #call method in this scope
821 Delete this record from the database. If there is an error, returns the error,
822 otherwise returns false.
829 my $statement = "DELETE FROM ". $self->table. " WHERE ". join(' AND ',
831 $self->getfield($_) eq ''
832 #? "( $_ IS NULL OR $_ = \"\" )"
833 ? ( driver_name eq 'Pg'
835 : "( $_ IS NULL OR $_ = \"\" )"
837 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
838 } ( $self->dbdef_table->primary_key )
839 ? ( $self->dbdef_table->primary_key)
840 : real_fields($self->table)
842 warn "[debug]$me $statement\n" if $DEBUG > 1;
843 my $sth = dbh->prepare($statement) or return dbh->errstr;
846 if ( defined dbdef->table('h_'. $self->table) ) {
847 my $h_statement = $self->_h_statement('delete');
848 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
849 $h_sth = dbh->prepare($h_statement) or return dbh->errstr;
854 my $primary_key = $self->dbdef_table->primary_key;
857 my $vfp = $self->vfieldpart_hashref;
858 foreach($self->virtual_fields) {
859 next if $self->getfield($_) eq '';
860 unless(@del_vfields) {
861 my $st = "DELETE FROM virtual_field WHERE recnum = ? AND vfieldpart = ?";
862 $v_sth = dbh->prepare($st) or return dbh->errstr;
864 push @del_vfields, $_;
867 local $SIG{HUP} = 'IGNORE';
868 local $SIG{INT} = 'IGNORE';
869 local $SIG{QUIT} = 'IGNORE';
870 local $SIG{TERM} = 'IGNORE';
871 local $SIG{TSTP} = 'IGNORE';
872 local $SIG{PIPE} = 'IGNORE';
874 my $rc = $sth->execute or return $sth->errstr;
875 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
876 $h_sth->execute or return $h_sth->errstr if $h_sth;
877 $v_sth->execute($self->getfield($primary_key), $vfp->{$_})
878 or return $v_sth->errstr
879 foreach (@del_vfields);
881 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
883 #no need to needlessly destoy the data either (causes problems actually)
884 #undef $self; #no need to keep object!
891 Depriciated (use delete instead).
896 cluck "warning: FS::Record::del deprecated!";
897 &delete(@_); #call method in this scope
900 =item replace OLD_RECORD
902 Replace the OLD_RECORD with this one in the database. If there is an error,
903 returns the error, otherwise returns false.
911 if (!defined($old)) {
912 warn "[debug]$me replace called with no arguments; autoloading old record\n"
914 my $primary_key = $new->dbdef_table->primary_key;
915 if ( $primary_key ) {
916 $old = qsearchs($new->table, { $primary_key => $new->$primary_key() } )
917 or croak "can't find ". $new->table. ".$primary_key ".
918 $new->$primary_key();
920 croak $new->table. " has no primary key; pass old record as argument";
924 warn "[debug]$me $new ->replace $old\n" if $DEBUG;
926 return "Records not in same table!" unless $new->table eq $old->table;
928 my $primary_key = $old->dbdef_table->primary_key;
929 return "Can't change primary key $primary_key ".
930 'from '. $old->getfield($primary_key).
931 ' to ' . $new->getfield($primary_key)
933 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
935 my $error = $new->check;
936 return $error if $error;
938 # Encrypt for replace
940 if ($conf->exists('encryption') && defined(eval '@FS::'. $new->table . 'encrypted_fields')) {
941 foreach my $field (eval '@FS::'. $new->table . '::encrypted_fields') {
942 $saved->{$field} = $new->getfield($field);
943 $new->setfield($field, $new->encrypt($new->getfield($field)));
947 #my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
948 my %diff = map { ($new->getfield($_) ne $old->getfield($_))
949 ? ($_, $new->getfield($_)) : () } $old->fields;
951 unless ( keys(%diff) ) {
952 carp "[warning]$me $new -> replace $old: records identical"
953 unless $nowarn_identical;
957 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
959 "$_ = ". _quote($new->getfield($_),$old->table,$_)
960 } real_fields($old->table)
965 if ( $old->getfield($_) eq '' ) {
967 #false laziness w/qsearch
968 if ( driver_name eq 'Pg' ) {
969 my $type = $old->dbdef_table->column($_)->type;
970 if ( $type =~ /(int|serial)/i ) {
973 qq-( $_ IS NULL OR $_ = '' )-;
976 qq-( $_ IS NULL OR $_ = "" )-;
980 "$_ = ". _quote($old->getfield($_),$old->table,$_);
983 } ( $primary_key ? ( $primary_key ) : real_fields($old->table) )
986 warn "[debug]$me $statement\n" if $DEBUG > 1;
987 my $sth = dbh->prepare($statement) or return dbh->errstr;
990 if ( defined dbdef->table('h_'. $old->table) ) {
991 my $h_old_statement = $old->_h_statement('replace_old');
992 warn "[debug]$me $h_old_statement\n" if $DEBUG > 2;
993 $h_old_sth = dbh->prepare($h_old_statement) or return dbh->errstr;
999 if ( defined dbdef->table('h_'. $new->table) ) {
1000 my $h_new_statement = $new->_h_statement('replace_new');
1001 warn "[debug]$me $h_new_statement\n" if $DEBUG > 2;
1002 $h_new_sth = dbh->prepare($h_new_statement) or return dbh->errstr;
1007 # For virtual fields we have three cases with different SQL
1008 # statements: add, replace, delete
1012 my (@add_vfields, @rep_vfields, @del_vfields);
1013 my $vfp = $old->vfieldpart_hashref;
1014 foreach(grep { exists($diff{$_}) } $new->virtual_fields) {
1015 if($diff{$_} eq '') {
1017 unless(@del_vfields) {
1018 my $st = "DELETE FROM virtual_field WHERE recnum = ? ".
1019 "AND vfieldpart = ?";
1020 warn "[debug]$me $st\n" if $DEBUG > 2;
1021 $v_del_sth = dbh->prepare($st) or return dbh->errstr;
1023 push @del_vfields, $_;
1024 } elsif($old->getfield($_) eq '') {
1026 unless(@add_vfields) {
1027 my $st = "INSERT INTO virtual_field (value, recnum, vfieldpart) ".
1029 warn "[debug]$me $st\n" if $DEBUG > 2;
1030 $v_add_sth = dbh->prepare($st) or return dbh->errstr;
1032 push @add_vfields, $_;
1035 unless(@rep_vfields) {
1036 my $st = "UPDATE virtual_field SET value = ? ".
1037 "WHERE recnum = ? AND vfieldpart = ?";
1038 warn "[debug]$me $st\n" if $DEBUG > 2;
1039 $v_rep_sth = dbh->prepare($st) or return dbh->errstr;
1041 push @rep_vfields, $_;
1045 local $SIG{HUP} = 'IGNORE';
1046 local $SIG{INT} = 'IGNORE';
1047 local $SIG{QUIT} = 'IGNORE';
1048 local $SIG{TERM} = 'IGNORE';
1049 local $SIG{TSTP} = 'IGNORE';
1050 local $SIG{PIPE} = 'IGNORE';
1052 my $rc = $sth->execute or return $sth->errstr;
1053 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
1054 $h_old_sth->execute or return $h_old_sth->errstr if $h_old_sth;
1055 $h_new_sth->execute or return $h_new_sth->errstr if $h_new_sth;
1057 $v_del_sth->execute($old->getfield($primary_key),
1059 or return $v_del_sth->errstr
1060 foreach(@del_vfields);
1062 $v_add_sth->execute($new->getfield($_),
1063 $old->getfield($primary_key),
1065 or return $v_add_sth->errstr
1066 foreach(@add_vfields);
1068 $v_rep_sth->execute($new->getfield($_),
1069 $old->getfield($primary_key),
1071 or return $v_rep_sth->errstr
1072 foreach(@rep_vfields);
1074 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1076 # Now that it has been saved, reset the encrypted fields so that $new
1077 # can still be used.
1078 foreach my $field (keys %{$saved}) {
1079 $new->setfield($field, $saved->{$field});
1088 Depriciated (use replace instead).
1093 cluck "warning: FS::Record::rep deprecated!";
1094 replace @_; #call method in this scope
1099 Checks virtual fields (using check_blocks). Subclasses should still provide
1100 a check method to validate real fields, foreign keys, etc., and call this
1101 method via $self->SUPER::check.
1103 (FIXME: Should this method try to make sure that it I<is> being called from
1104 a subclass's check method, to keep the current semantics as far as possible?)
1109 #confess "FS::Record::check not implemented; supply one in subclass!";
1112 foreach my $field ($self->virtual_fields) {
1113 for ($self->getfield($field)) {
1114 # See notes on check_block in FS::part_virtual_field.
1115 eval $self->pvf($field)->check_block;
1117 #this is bad, probably want to follow the stack backtrace up and see
1119 my $err = "Fatal error checking $field for $self";
1121 return "$err (see log for backtrace): $@";
1124 $self->setfield($field, $_);
1131 my( $self, $action, $time ) = @_;
1136 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
1137 real_fields($self->table);
1139 my @values = map { _quote( $self->getfield($_), $self->table, $_) } @fields;
1141 "INSERT INTO h_". $self->table. " ( ".
1142 join(', ', qw(history_date history_user history_action), @fields ).
1144 join(', ', $time, dbh->quote(getotaker()), dbh->quote($action), @values).
1151 B<Warning>: External use is B<deprecated>.
1153 Replaces COLUMN in record with a unique number, using counters in the
1154 filesystem. Used by the B<insert> method on single-field unique columns
1155 (see L<DBIx::DBSchema::Table>) and also as a fallback for primary keys
1156 that aren't SERIAL (Pg) or AUTO_INCREMENT (mysql).
1158 Returns the new value.
1163 my($self,$field) = @_;
1164 my($table)=$self->table;
1166 croak "Unique called on field $field, but it is ",
1167 $self->getfield($field),
1169 if $self->getfield($field);
1171 #warn "table $table is tainted" if is_tainted($table);
1172 #warn "field $field is tainted" if is_tainted($field);
1174 my($counter) = new File::CounterFile "$table.$field",0;
1176 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
1178 # my($counter) = new File::CounterFile "$user/$table.$field",0;
1181 my $index = $counter->inc;
1182 $index = $counter->inc while qsearchs($table, { $field=>$index } );
1184 $index =~ /^(\d*)$/;
1187 $self->setfield($field,$index);
1191 =item ut_float COLUMN
1193 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
1194 null. If there is an error, returns the error, otherwise returns false.
1199 my($self,$field)=@_ ;
1200 ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
1201 $self->getfield($field) =~ /^(\d+)$/ ||
1202 $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
1203 $self->getfield($field) =~ /^(\d+e\d+)$/)
1204 or return "Illegal or empty (float) $field: ". $self->getfield($field);
1205 $self->setfield($field,$1);
1209 =item ut_snumber COLUMN
1211 Check/untaint signed numeric data (whole numbers). May not be null. If there
1212 is an error, returns the error, otherwise returns false.
1217 my($self, $field) = @_;
1218 $self->getfield($field) =~ /^(-?)\s*(\d+)$/
1219 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1220 $self->setfield($field, "$1$2");
1224 =item ut_number COLUMN
1226 Check/untaint simple numeric data (whole numbers). May not be null. If there
1227 is an error, returns the error, otherwise returns false.
1232 my($self,$field)=@_;
1233 $self->getfield($field) =~ /^(\d+)$/
1234 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1235 $self->setfield($field,$1);
1239 =item ut_numbern COLUMN
1241 Check/untaint simple numeric data (whole numbers). May be null. If there is
1242 an error, returns the error, otherwise returns false.
1247 my($self,$field)=@_;
1248 $self->getfield($field) =~ /^(\d*)$/
1249 or return "Illegal (numeric) $field: ". $self->getfield($field);
1250 $self->setfield($field,$1);
1254 =item ut_money COLUMN
1256 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
1257 is an error, returns the error, otherwise returns false.
1262 my($self,$field)=@_;
1263 $self->setfield($field, 0) if $self->getfield($field) eq '';
1264 $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
1265 or return "Illegal (money) $field: ". $self->getfield($field);
1266 #$self->setfield($field, "$1$2$3" || 0);
1267 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
1271 =item ut_text COLUMN
1273 Check/untaint text. Alphanumerics, spaces, and the following punctuation
1274 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / =
1275 May not be null. If there is an error, returns the error, otherwise returns
1281 my($self,$field)=@_;
1282 #warn "msgcat ". \&msgcat. "\n";
1283 #warn "notexist ". \¬exist. "\n";
1284 #warn "AUTOLOAD ". \&AUTOLOAD. "\n";
1285 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]+)$/
1286 or return gettext('illegal_or_empty_text'). " $field: ".
1287 $self->getfield($field);
1288 $self->setfield($field,$1);
1292 =item ut_textn COLUMN
1294 Check/untaint text. Alphanumerics, spaces, and the following punctuation
1295 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
1296 May be null. If there is an error, returns the error, otherwise returns false.
1301 my($self,$field)=@_;
1302 $self->getfield($field) =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=]*)$/
1303 or return gettext('illegal_text'). " $field: ". $self->getfield($field);
1304 $self->setfield($field,$1);
1308 =item ut_alpha COLUMN
1310 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
1311 an error, returns the error, otherwise returns false.
1316 my($self,$field)=@_;
1317 $self->getfield($field) =~ /^(\w+)$/
1318 or return "Illegal or empty (alphanumeric) $field: ".
1319 $self->getfield($field);
1320 $self->setfield($field,$1);
1324 =item ut_alpha COLUMN
1326 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
1327 error, returns the error, otherwise returns false.
1332 my($self,$field)=@_;
1333 $self->getfield($field) =~ /^(\w*)$/
1334 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
1335 $self->setfield($field,$1);
1339 =item ut_phonen COLUMN [ COUNTRY ]
1341 Check/untaint phone numbers. May be null. If there is an error, returns
1342 the error, otherwise returns false.
1344 Takes an optional two-letter ISO country code; without it or with unsupported
1345 countries, ut_phonen simply calls ut_alphan.
1350 my( $self, $field, $country ) = @_;
1351 return $self->ut_alphan($field) unless defined $country;
1352 my $phonen = $self->getfield($field);
1353 if ( $phonen eq '' ) {
1354 $self->setfield($field,'');
1355 } elsif ( $country eq 'US' || $country eq 'CA' ) {
1357 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
1358 or return gettext('illegal_phone'). " $field: ". $self->getfield($field);
1359 $phonen = "$1-$2-$3";
1360 $phonen .= " x$4" if $4;
1361 $self->setfield($field,$phonen);
1363 warn "warning: don't know how to check phone numbers for country $country";
1364 return $self->ut_textn($field);
1371 Check/untaint ip addresses. IPv4 only for now.
1376 my( $self, $field ) = @_;
1377 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
1378 or return "Illegal (IP address) $field: ". $self->getfield($field);
1379 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
1380 $self->setfield($field, "$1.$2.$3.$4");
1386 Check/untaint ip addresses. IPv4 only for now. May be null.
1391 my( $self, $field ) = @_;
1392 if ( $self->getfield($field) =~ /^()$/ ) {
1393 $self->setfield($field,'');
1396 $self->ut_ip($field);
1400 =item ut_domain COLUMN
1402 Check/untaint host and domain names.
1407 my( $self, $field ) = @_;
1408 #$self->getfield($field) =~/^(\w+\.)*\w+$/
1409 $self->getfield($field) =~/^(([\w\-]+\.)*\w+)$/
1410 or return "Illegal (domain) $field: ". $self->getfield($field);
1411 $self->setfield($field,$1);
1415 =item ut_name COLUMN
1417 Check/untaint proper names; allows alphanumerics, spaces and the following
1418 punctuation: , . - '
1425 my( $self, $field ) = @_;
1426 $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
1427 or return gettext('illegal_name'). " $field: ". $self->getfield($field);
1428 $self->setfield($field,$1);
1434 Check/untaint zip codes.
1439 my( $self, $field, $country ) = @_;
1440 if ( $country eq 'US' ) {
1441 $self->getfield($field) =~ /\s*(\d{5}(\-\d{4})?)\s*$/
1442 or return gettext('illegal_zip'). " $field for country $country: ".
1443 $self->getfield($field);
1444 $self->setfield($field,$1);
1446 if ( $self->getfield($field) =~ /^\s*$/ ) {
1447 $self->setfield($field,'');
1449 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
1450 or return gettext('illegal_zip'). " $field: ". $self->getfield($field);
1451 $self->setfield($field,$1);
1457 =item ut_country COLUMN
1459 Check/untaint country codes. Country names are changed to codes, if possible -
1460 see L<Locale::Country>.
1465 my( $self, $field ) = @_;
1466 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
1467 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
1468 && country2code($1) ) {
1469 $self->setfield($field,uc(country2code($1)));
1472 $self->getfield($field) =~ /^(\w\w)$/
1473 or return "Illegal (country) $field: ". $self->getfield($field);
1474 $self->setfield($field,uc($1));
1478 =item ut_anything COLUMN
1480 Untaints arbitrary data. Be careful.
1485 my( $self, $field ) = @_;
1486 $self->getfield($field) =~ /^(.*)$/s
1487 or return "Illegal $field: ". $self->getfield($field);
1488 $self->setfield($field,$1);
1492 =item ut_enum COLUMN CHOICES_ARRAYREF
1494 Check/untaint a column, supplying all possible choices, like the "enum" type.
1499 my( $self, $field, $choices ) = @_;
1500 foreach my $choice ( @$choices ) {
1501 if ( $self->getfield($field) eq $choice ) {
1502 $self->setfield($choice);
1506 return "Illegal (enum) field $field: ". $self->getfield($field);
1509 =item ut_foreign_key COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1511 Check/untaint a foreign column key. Call a regular ut_ method (like ut_number)
1512 on the column first.
1516 sub ut_foreign_key {
1517 my( $self, $field, $table, $foreign ) = @_;
1518 qsearchs($table, { $foreign => $self->getfield($field) })
1519 or return "Can't find ". $self->table. ".$field ". $self->getfield($field).
1520 " in $table.$foreign";
1524 =item ut_foreign_keyn COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1526 Like ut_foreign_key, except the null value is also allowed.
1530 sub ut_foreign_keyn {
1531 my( $self, $field, $table, $foreign ) = @_;
1532 $self->getfield($field)
1533 ? $self->ut_foreign_key($field, $table, $foreign)
1538 =item virtual_fields [ TABLE ]
1540 Returns a list of virtual fields defined for the table. This should not
1541 be exported, and should only be called as an instance or class method.
1545 sub virtual_fields {
1548 $table = $self->table or confess "virtual_fields called on non-table";
1550 confess "Unknown table $table" unless dbdef->table($table);
1552 return () unless dbdef->table('part_virtual_field');
1554 unless ( $virtual_fields_cache{$table} ) {
1555 my $query = 'SELECT name from part_virtual_field ' .
1556 "WHERE dbtable = '$table'";
1558 my $result = $dbh->selectcol_arrayref($query);
1559 confess $dbh->errstr if $dbh->err;
1560 $virtual_fields_cache{$table} = $result;
1563 @{$virtual_fields_cache{$table}};
1568 =item fields [ TABLE ]
1570 This is a wrapper for real_fields and virtual_fields. Code that called
1571 fields before should probably continue to call fields.
1576 my $something = shift;
1578 if($something->isa('FS::Record')) {
1579 $table = $something->table;
1581 $table = $something;
1582 $something = "FS::$table";
1584 return (real_fields($table), $something->virtual_fields());
1589 =item pvf FIELD_NAME
1591 Returns the FS::part_virtual_field object corresponding to a field in the
1592 record (specified by FIELD_NAME).
1597 my ($self, $name) = (shift, shift);
1599 if(grep /^$name$/, $self->virtual_fields) {
1600 return qsearchs('part_virtual_field', { dbtable => $self->table,
1610 =item real_fields [ TABLE ]
1612 Returns a list of the real columns in the specified table. Called only by
1613 fields() and other subroutines elsewhere in FS::Record.
1620 my($table_obj) = dbdef->table($table);
1621 confess "Unknown table $table" unless $table_obj;
1622 $table_obj->columns;
1625 =item _quote VALUE, TABLE, COLUMN
1627 This is an internal function used to construct SQL statements. It returns
1628 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
1629 type (see L<DBIx::DBSchema::Column>) does not end in `char' or `binary'.
1634 my($value, $table, $column) = @_;
1635 my $column_obj = dbdef->table($table)->column($column);
1636 my $column_type = $column_obj->type;
1637 my $nullable = $column_obj->null;
1639 warn " $table.$column: $value ($column_type".
1640 ( $nullable ? ' NULL' : ' NOT NULL' ).
1641 ")\n" if $DEBUG > 2;
1643 if ( $value eq '' && $column_type =~ /^int/ ) {
1647 cluck "WARNING: Attempting to set non-null integer $table.$column null; ".
1651 } elsif ( $value =~ /^\d+(\.\d+)?$/ &&
1652 ! $column_type =~ /(char|binary|text)$/i ) {
1659 =item vfieldpart_hashref TABLE
1661 Returns a hashref of virtual field names and vfieldparts applicable to the given
1666 sub vfieldpart_hashref {
1668 my $table = $self->table;
1670 return {} unless dbdef->table('part_virtual_field');
1673 my $statement = "SELECT vfieldpart, name FROM part_virtual_field WHERE ".
1674 "dbtable = '$table'";
1675 my $sth = $dbh->prepare($statement);
1676 $sth->execute or croak "Execution of '$statement' failed: ".$dbh->errstr;
1677 return { map { $_->{name}, $_->{vfieldpart} }
1678 @{$sth->fetchall_arrayref({})} };
1685 This is deprecated. Don't use it.
1687 It returns a hash-type list with the fields of this record's table set true.
1692 carp "warning: hfields is deprecated";
1695 foreach (fields($table)) {
1704 "$_: ". $self->getfield($_). "|"
1705 } (fields($self->table)) );
1709 my ($self, $value) = @_;
1712 if ($conf->exists('encryption')) {
1713 if ($self->is_encrypted($value)) {
1714 # Return the original value if it isn't plaintext.
1715 $encrypted = $value;
1718 if (ref($rsa_encrypt) =~ /::RSA/) { # We Can Encrypt
1719 # RSA doesn't like the empty string so let's pack it up
1720 # The database doesn't like the RSA data so uuencode it
1721 my $length = length($value)+1;
1722 $encrypted = pack("u*",$rsa_encrypt->encrypt(pack("Z$length",$value)));
1724 die ("You can't encrypt w/o a valid RSA engine - Check your installation or disable encryption");
1732 my ($self, $value) = @_;
1733 # Possible Bug - Some work may be required here....
1735 if (length($value) > 80) {
1743 my ($self,$value) = @_;
1744 my $decrypted = $value; # Will return the original value if it isn't encrypted or can't be decrypted.
1745 if ($conf->exists('encryption') && $self->is_encrypted($value)) {
1747 if (ref($rsa_decrypt) =~ /::RSA/) {
1748 my $encrypted = unpack ("u*", $value);
1749 $decrypted = unpack("Z*", $rsa_decrypt->decrypt($encrypted));
1757 #Initialize the Module
1758 $rsa_module = 'Crypt::OpenSSL::RSA'; # The Default
1760 if ($conf->exists('encryptionmodule') && $conf->config('encryptionmodule') ne '') {
1761 $rsa_module = $conf->config('encryptionmodule');
1765 eval ("require $rsa_module"); # No need to import the namespace
1768 # Initialize Encryption
1769 if ($conf->exists('encryptionpublickey') && $conf->config('encryptionpublickey') ne '') {
1770 my $public_key = join("\n",$conf->config('encryptionpublickey'));
1771 $rsa_encrypt = $rsa_module->new_public_key($public_key);
1774 # Intitalize Decryption
1775 if ($conf->exists('encryptionprivatekey') && $conf->config('encryptionprivatekey') ne '') {
1776 my $private_key = join("\n",$conf->config('encryptionprivatekey'));
1777 $rsa_decrypt = $rsa_module->new_private_key($private_key);
1781 sub DESTROY { return; }
1785 # #use Carp qw(cluck);
1786 # #cluck "DESTROYING $self";
1787 # warn "DESTROYING $self";
1791 # return ! eval { join('',@_), kill 0; 1; };
1798 This module should probably be renamed, since much of the functionality is
1799 of general use. It is not completely unlike Adapter::DBI (see below).
1801 Exported qsearch and qsearchs should be deprecated in favor of method calls
1802 (against an FS::Record object like the old search and searchs that qsearch
1803 and qsearchs were on top of.)
1805 The whole fields / hfields mess should be removed.
1807 The various WHERE clauses should be subroutined.
1809 table string should be deprecated in favor of DBIx::DBSchema::Table.
1811 No doubt we could benefit from a Tied hash. Documenting how exists / defined
1812 true maps to the database (and WHERE clauses) would also help.
1814 The ut_ methods should ask the dbdef for a default length.
1816 ut_sqltype (like ut_varchar) should all be defined
1818 A fallback check method should be provided which uses the dbdef.
1820 The ut_money method assumes money has two decimal digits.
1822 The Pg money kludge in the new method only strips `$'.
1824 The ut_phonen method only checks US-style phone numbers.
1826 The _quote function should probably use ut_float instead of a regex.
1828 All the subroutines probably should be methods, here or elsewhere.
1830 Probably should borrow/use some dbdef methods where appropriate (like sub
1833 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
1834 or allow it to be set. Working around it is ugly any way around - DBI should
1835 be fixed. (only affects RDBMS which return uppercase column names)
1837 ut_zip should take an optional country like ut_phone.
1841 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
1843 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.