4 use vars qw( $AUTOLOAD @ISA @EXPORT_OK $DEBUG
6 %virtual_fields_cache $nowarn_identical $no_update_diff );
8 use Carp qw(carp cluck croak confess);
11 use DBI qw(:sql_types);
12 use DBIx::DBSchema 0.25;
13 #use DBIx::DBSchema 0.33; #when check for ->can('unique_singles') is sub insert
15 use FS::UID qw(dbh getotaker datasrc driver_name);
17 use FS::Schema qw(dbdef);
19 use FS::Msgcat qw(gettext);
22 use FS::part_virtual_field;
28 #export dbdef for now... everything else expects to find it here
29 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch);
34 $nowarn_identical = 0;
42 FS::UID->install_callback( sub {
44 $File::CounterFile::DEFAULT_DIR = $conf->base_dir . "/counters.". datasrc;
50 FS::Record - Database record objects
55 use FS::Record qw(dbh fields qsearch qsearchs);
57 $record = new FS::Record 'table', \%hash;
58 $record = new FS::Record 'table', { 'column' => 'value', ... };
60 $record = qsearchs FS::Record 'table', \%hash;
61 $record = qsearchs FS::Record 'table', { 'column' => 'value', ... };
62 @records = qsearch FS::Record 'table', \%hash;
63 @records = qsearch FS::Record 'table', { 'column' => 'value', ... };
65 $table = $record->table;
66 $dbdef_table = $record->dbdef_table;
68 $value = $record->get('column');
69 $value = $record->getfield('column');
70 $value = $record->column;
72 $record->set( 'column' => 'value' );
73 $record->setfield( 'column' => 'value' );
74 $record->column('value');
76 %hash = $record->hash;
78 $hashref = $record->hashref;
80 $error = $record->insert;
82 $error = $record->delete;
84 $error = $new_record->replace($old_record);
86 # external use deprecated - handled by the database (at least for Pg, mysql)
87 $value = $record->unique('column');
89 $error = $record->ut_float('column');
90 $error = $record->ut_floatn('column');
91 $error = $record->ut_number('column');
92 $error = $record->ut_numbern('column');
93 $error = $record->ut_snumber('column');
94 $error = $record->ut_snumbern('column');
95 $error = $record->ut_money('column');
96 $error = $record->ut_text('column');
97 $error = $record->ut_textn('column');
98 $error = $record->ut_alpha('column');
99 $error = $record->ut_alphan('column');
100 $error = $record->ut_phonen('column');
101 $error = $record->ut_anything('column');
102 $error = $record->ut_name('column');
104 $quoted_value = _quote($value,'table','field');
107 $fields = hfields('table');
108 if ( $fields->{Field} ) { # etc.
110 @fields = fields 'table'; #as a subroutine
111 @fields = $record->fields; #as a method call
116 (Mostly) object-oriented interface to database records. Records are currently
117 implemented on top of DBI. FS::Record is intended as a base class for
118 table-specific classes to inherit from, i.e. FS::cust_main.
124 =item new [ TABLE, ] HASHREF
126 Creates a new record. It doesn't store it in the database, though. See
127 L<"insert"> for that.
129 Note that the object stores this hash reference, not a distinct copy of the
130 hash it points to. You can ask the object for a copy with the I<hash>
133 TABLE can only be omitted when a dervived class overrides the table method.
139 my $class = ref($proto) || $proto;
141 bless ($self, $class);
143 unless ( defined ( $self->table ) ) {
144 $self->{'Table'} = shift;
145 carp "warning: FS::Record::new called with table name ". $self->{'Table'};
148 $self->{'Hash'} = shift;
150 foreach my $field ( grep !defined($self->{'Hash'}{$_}), $self->fields ) {
151 $self->{'Hash'}{$field}='';
154 $self->_rebless if $self->can('_rebless');
156 $self->{'modified'} = 0;
158 $self->_cache($self->{'Hash'}, shift) if $self->can('_cache') && @_;
165 my $class = ref($proto) || $proto;
167 bless ($self, $class);
169 $self->{'Table'} = shift unless defined ( $self->table );
171 my $hashref = $self->{'Hash'} = shift;
173 if ( defined( $cache->cache->{$hashref->{$cache->key}} ) ) {
174 my $obj = $cache->cache->{$hashref->{$cache->key}};
175 $obj->_cache($hashref, $cache) if $obj->can('_cache');
178 $cache->cache->{$hashref->{$cache->key}} = $self->new($hashref, $cache);
185 my $class = ref($proto) || $proto;
187 bless ($self, $class);
188 if ( defined $self->table ) {
189 cluck "create constructor is deprecated, use new!";
192 croak "FS::Record::create called (not from a subclass)!";
196 =item qsearch PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
198 Searches the database for all records matching (at least) the key/value pairs
199 in HASHREF. Returns all the records found as `FS::TABLE' objects if that
200 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
203 The preferred usage is to pass a hash reference of named parameters:
205 my @records = qsearch( {
206 'table' => 'table_name',
207 'hashref' => { 'field' => 'value'
208 'field' => { 'op' => '<',
213 #these are optional...
215 'extra_sql' => 'AND field ',
216 'order_by' => 'ORDER BY something',
217 #'cache_obj' => '', #optional
218 'addl_from' => 'LEFT JOIN othtable USING ( field )',
222 Much code still uses old-style positional parameters, this is also probably
223 fine in the common case where there are only two parameters:
225 my @records = qsearch( 'table', { 'field' => 'value' } );
227 ###oops, argh, FS::Record::new only lets us create database fields.
228 #Normal behaviour if SELECT is not specified is `*', as in
229 #C<SELECT * FROM table WHERE ...>. However, there is an experimental new
230 #feature where you can specify SELECT - remember, the objects returned,
231 #although blessed into the appropriate `FS::TABLE' package, will only have the
232 #fields you specify. This might have unwanted results if you then go calling
233 #regular FS::TABLE methods
239 my($stable, $record, $select, $extra_sql, $order_by, $cache, $addl_from );
240 if ( ref($_[0]) ) { #hashref for now, eventually maybe accept a list too
242 $stable = $opt->{'table'} or die "table name is required";
243 $record = $opt->{'hashref'} || {};
244 $select = $opt->{'select'} || '*';
245 $extra_sql = $opt->{'extra_sql'} || '';
246 $order_by = $opt->{'order_by'} || '';
247 $cache = $opt->{'cache_obj'} || '';
248 $addl_from = $opt->{'addl_from'} || '';
250 ($stable, $record, $select, $extra_sql, $cache, $addl_from ) = @_;
254 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
256 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
260 my $table = $cache ? $cache->table : $stable;
261 my $dbdef_table = dbdef->table($table)
262 or die "No schema for table $table found - ".
263 "do you need to run freeside-upgrade?";
264 my $pkey = $dbdef_table->primary_key;
266 my @real_fields = grep exists($record->{$_}), real_fields($table);
268 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
269 @virtual_fields = grep exists($record->{$_}), "FS::$table"->virtual_fields;
271 cluck "warning: FS::$table not loaded; virtual fields not searchable";
272 @virtual_fields = ();
275 my $statement = "SELECT $select FROM $stable";
276 $statement .= " $addl_from" if $addl_from;
277 if ( @real_fields or @virtual_fields ) {
278 $statement .= ' WHERE '. join(' AND ',
283 if ( ref($record->{$_}) ) {
284 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
285 #$op = 'LIKE' if $op =~ /^ILIKE$/i && driver_name ne 'Pg';
286 if ( uc($op) eq 'ILIKE' ) {
288 $record->{$_}{'value'} = lc($record->{$_}{'value'});
289 $column = "LOWER($_)";
291 $record->{$_} = $record->{$_}{'value'}
294 if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) {
296 if ( driver_name eq 'Pg' ) {
297 my $type = dbdef->table($table)->column($column)->type;
298 if ( $type =~ /(int|(big)?serial)/i ) {
299 qq-( $column IS NULL )-;
301 qq-( $column IS NULL OR $column = '' )-;
304 qq-( $column IS NULL OR $column = "" )-;
306 } elsif ( $op eq '!=' ) {
307 if ( driver_name eq 'Pg' ) {
308 my $type = dbdef->table($table)->column($column)->type;
309 if ( $type =~ /(int|(big)?serial)/i ) {
310 qq-( $column IS NOT NULL )-;
312 qq-( $column IS NOT NULL AND $column != '' )-;
315 qq-( $column IS NOT NULL AND $column != "" )-;
318 if ( driver_name eq 'Pg' ) {
319 qq-( $column $op '' )-;
321 qq-( $column $op "" )-;
331 if ( ref($record->{$_}) ) {
332 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
333 if ( uc($op) eq 'ILIKE' ) {
335 $record->{$_}{'value'} = lc($record->{$_}{'value'});
336 $column = "LOWER($_)";
338 $record->{$_} = $record->{$_}{'value'};
341 # ... EXISTS ( SELECT name, value FROM part_virtual_field
343 # ON part_virtual_field.vfieldpart = virtual_field.vfieldpart
344 # WHERE recnum = svc_acct.svcnum
345 # AND (name, value) = ('egad', 'brain') )
347 my $value = $record->{$_};
351 $subq = ($value ? 'EXISTS ' : 'NOT EXISTS ') .
352 "( SELECT part_virtual_field.name, virtual_field.value ".
353 "FROM part_virtual_field JOIN virtual_field ".
354 "ON part_virtual_field.vfieldpart = virtual_field.vfieldpart ".
355 "WHERE virtual_field.recnum = ${table}.${pkey} ".
356 "AND part_virtual_field.name = '${column}'".
358 " AND virtual_field.value ${op} '${value}'"
362 } @virtual_fields ) );
366 $statement .= " $extra_sql" if defined($extra_sql);
367 $statement .= " $order_by" if defined($order_by);
369 warn "[debug]$me $statement\n" if $DEBUG > 1;
370 my $sth = $dbh->prepare($statement)
371 or croak "$dbh->errstr doing $statement";
376 grep defined( $record->{$_} ) && $record->{$_} ne '', @real_fields
378 if ( $record->{$field} =~ /^\d+(\.\d+)?$/
379 && dbdef->table($table)->column($field)->type =~ /(int|(big)?serial)/i
381 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_INTEGER } );
383 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_VARCHAR } );
387 # $sth->execute( map $record->{$_},
388 # grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
389 # ) or croak "Error executing \"$statement\": ". $sth->errstr;
391 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
393 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
394 @virtual_fields = "FS::$table"->virtual_fields;
396 cluck "warning: FS::$table not loaded; virtual fields not returned either";
397 @virtual_fields = ();
401 tie %result, "Tie::IxHash";
402 my @stuff = @{ $sth->fetchall_arrayref( {} ) };
403 if ( $pkey && scalar(@stuff) && $stuff[0]->{$pkey} ) {
404 %result = map { $_->{$pkey}, $_ } @stuff;
406 @result{@stuff} = @stuff;
411 if ( keys(%result) and @virtual_fields ) {
413 "SELECT virtual_field.recnum, part_virtual_field.name, ".
414 "virtual_field.value ".
415 "FROM part_virtual_field JOIN virtual_field USING (vfieldpart) ".
416 "WHERE part_virtual_field.dbtable = '$table' AND ".
417 "virtual_field.recnum IN (".
418 join(',', keys(%result)). ") AND part_virtual_field.name IN ('".
419 join(q!', '!, @virtual_fields) . "')";
420 warn "[debug]$me $statement\n" if $DEBUG > 1;
421 $sth = $dbh->prepare($statement) or croak "$dbh->errstr doing $statement";
422 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
424 foreach (@{ $sth->fetchall_arrayref({}) }) {
425 my $recnum = $_->{recnum};
426 my $name = $_->{name};
427 my $value = $_->{value};
428 if (exists($result{$recnum})) {
429 $result{$recnum}->{$name} = $value;
434 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
435 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
436 #derivied class didn't override new method, so this optimization is safe
439 new_or_cached( "FS::$table", { %{$_} }, $cache )
443 new( "FS::$table", { %{$_} } )
447 #okay, its been tested
448 # warn "untested code (class FS::$table uses custom new method)";
450 eval 'FS::'. $table. '->new( { %{$_} } )';
454 # Check for encrypted fields and decrypt them.
455 ## only in the local copy, not the cached object
456 if ( $conf && $conf->exists('encryption') # $conf doesn't exist when doing
457 # the initial search for
459 && eval 'defined(@FS::'. $table . '::encrypted_fields)') {
460 foreach my $record (@return) {
461 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
462 # Set it directly... This may cause a problem in the future...
463 $record->setfield($field, $record->decrypt($record->getfield($field)));
468 cluck "warning: FS::$table not loaded; returning FS::Record objects";
470 FS::Record->new( $table, { %{$_} } );
476 =item by_key PRIMARY_KEY_VALUE
478 This is a class method that returns the record with the given primary key
479 value. This method is only useful in FS::Record subclasses. For example:
481 my $cust_main = FS::cust_main->by_key(1); # retrieve customer with custnum 1
485 my $cust_main = qsearchs('cust_main', { 'custnum' => 1 } );
490 my ($class, $pkey_value) = @_;
492 my $table = $class->table
493 or croak "No table for $class found";
495 my $dbdef_table = dbdef->table($table)
496 or die "No schema for table $table found - ".
497 "do you need to create it or run dbdef-create?";
498 my $pkey = $dbdef_table->primary_key
499 or die "No primary key for table $table";
501 return qsearchs($table, { $pkey => $pkey_value });
504 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
506 Experimental JOINed search method. Using this method, you can execute a
507 single SELECT spanning multiple tables, and cache the results for subsequent
508 method calls. Interface will almost definately change in an incompatible
516 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
517 my $cache = FS::SearchCache->new( $ptable, $pkey );
520 grep { !$saw{$_->getfield($pkey)}++ }
521 qsearch($table, $record, $select, $extra_sql, $cache )
525 =item qsearchs PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
527 Same as qsearch, except that if more than one record matches, it B<carp>s but
528 returns the first. If this happens, you either made a logic error in asking
529 for a single item, or your data is corrupted.
533 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
535 my(@result) = qsearch(@_);
536 cluck "warning: Multiple records in scalar search ($table)"
537 if scalar(@result) > 1;
538 #should warn more vehemently if the search was on a primary key?
539 scalar(@result) ? ($result[0]) : ();
550 Returns the table name.
555 # cluck "warning: FS::Record::table deprecated; supply one in subclass!";
562 Returns the DBIx::DBSchema::Table object for the table.
568 my($table)=$self->table;
569 dbdef->table($table);
574 Returns the primary key for the table.
580 my $pkey = $self->dbdef_table->primary_key;
583 =item get, getfield COLUMN
585 Returns the value of the column/field/key COLUMN.
590 my($self,$field) = @_;
591 # to avoid "Use of unitialized value" errors
592 if ( defined ( $self->{Hash}->{$field} ) ) {
593 $self->{Hash}->{$field};
603 =item set, setfield COLUMN, VALUE
605 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
610 my($self,$field,$value) = @_;
611 $self->{'modified'} = 1;
612 $self->{'Hash'}->{$field} = $value;
619 =item AUTLOADED METHODS
621 $record->column is a synonym for $record->get('column');
623 $record->column('value') is a synonym for $record->set('column','value');
630 my($field)=$AUTOLOAD;
632 if ( defined($value) ) {
633 confess "errant AUTOLOAD $field for $self (arg $value)"
634 unless ref($self) && $self->can('setfield');
635 $self->setfield($field,$value);
637 confess "errant AUTOLOAD $field for $self (no args)"
638 unless ref($self) && $self->can('getfield');
639 $self->getfield($field);
645 # my $field = $AUTOLOAD;
647 # if ( defined($_[1]) ) {
648 # $_[0]->setfield($field, $_[1]);
650 # $_[0]->getfield($field);
656 Returns a list of the column/value pairs, usually for assigning to a new hash.
658 To make a distinct duplicate of an FS::Record object, you can do:
660 $new = new FS::Record ( $old->table, { $old->hash } );
666 confess $self. ' -> hash: Hash attribute is undefined'
667 unless defined($self->{'Hash'});
668 %{ $self->{'Hash'} };
673 Returns a reference to the column/value hash. This may be deprecated in the
674 future; if there's a reason you can't just use the autoloaded or get/set
686 Returns true if any of this object's values have been modified with set (or via
687 an autoloaded method). Doesn't yet recognize when you retreive a hashref and
697 =item select_for_update
699 Selects this record with the SQL "FOR UPDATE" command. This can be useful as
704 sub select_for_update {
706 my $primary_key = $self->primary_key;
709 'table' => $self->table,
710 'hashref' => { $primary_key => $self->$primary_key() },
711 'extra_sql' => 'FOR UPDATE',
717 Inserts this record to the database. If there is an error, returns the error,
718 otherwise returns false.
726 warn "$self -> insert" if $DEBUG;
728 my $error = $self->check;
729 return $error if $error;
731 #single-field unique keys are given a value if false
732 #(like MySQL's AUTO_INCREMENT or Pg SERIAL)
733 foreach ( $self->dbdef_table->can('unique_singles')
734 ? $self->dbdef_table->unique_singles
735 : $self->dbdef_table->unique->singles
737 $self->unique($_) unless $self->getfield($_);
740 #and also the primary key, if the database isn't going to
741 my $primary_key = $self->dbdef_table->primary_key;
743 if ( $primary_key ) {
744 my $col = $self->dbdef_table->column($primary_key);
747 uc($col->type) =~ /^(BIG)?SERIAL\d?/
748 || ( driver_name eq 'Pg'
749 && defined($col->default)
750 && $col->default =~ /^nextval\(/i
752 || ( driver_name eq 'mysql'
753 && defined($col->local)
754 && $col->local =~ /AUTO_INCREMENT/i
756 $self->unique($primary_key) unless $self->getfield($primary_key) || $db_seq;
759 my $table = $self->table;
762 # Encrypt before the database
763 my $conf = new FS::Conf;
764 if ($conf->exists('encryption') && defined(eval '@FS::'. $table . '::encrypted_fields')) {
765 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
766 $self->{'saved'} = $self->getfield($field);
767 $self->setfield($field, $self->encrypt($self->getfield($field)));
772 #false laziness w/delete
774 grep { defined($self->getfield($_)) && $self->getfield($_) ne "" }
777 my @values = map { _quote( $self->getfield($_), $table, $_) } @real_fields;
780 my $statement = "INSERT INTO $table ";
781 if ( @real_fields ) {
784 join( ', ', @real_fields ).
786 join( ', ', @values ).
790 $statement .= 'DEFAULT VALUES';
792 warn "[debug]$me $statement\n" if $DEBUG > 1;
793 my $sth = dbh->prepare($statement) or return dbh->errstr;
795 local $SIG{HUP} = 'IGNORE';
796 local $SIG{INT} = 'IGNORE';
797 local $SIG{QUIT} = 'IGNORE';
798 local $SIG{TERM} = 'IGNORE';
799 local $SIG{TSTP} = 'IGNORE';
800 local $SIG{PIPE} = 'IGNORE';
802 $sth->execute or return $sth->errstr;
804 # get inserted id from the database, if applicable & needed
805 if ( $db_seq && ! $self->getfield($primary_key) ) {
806 warn "[debug]$me retreiving sequence from database\n" if $DEBUG;
810 if ( driver_name eq 'Pg' ) {
812 #my $oid = $sth->{'pg_oid_status'};
813 #my $i_sql = "SELECT $primary_key FROM $table WHERE oid = ?";
815 my $default = $self->dbdef_table->column($primary_key)->default;
816 unless ( $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i ) {
817 dbh->rollback if $FS::UID::AutoCommit;
818 return "can't parse $table.$primary_key default value".
819 " for sequence name: $default";
823 my $i_sql = "SELECT currval('$sequence')";
824 my $i_sth = dbh->prepare($i_sql) or do {
825 dbh->rollback if $FS::UID::AutoCommit;
828 $i_sth->execute() or do { #$i_sth->execute($oid)
829 dbh->rollback if $FS::UID::AutoCommit;
830 return $i_sth->errstr;
832 $insertid = $i_sth->fetchrow_arrayref->[0];
834 } elsif ( driver_name eq 'mysql' ) {
836 $insertid = dbh->{'mysql_insertid'};
837 # work around mysql_insertid being null some of the time, ala RT :/
838 unless ( $insertid ) {
839 warn "WARNING: DBD::mysql didn't return mysql_insertid; ".
840 "using SELECT LAST_INSERT_ID();";
841 my $i_sql = "SELECT LAST_INSERT_ID()";
842 my $i_sth = dbh->prepare($i_sql) or do {
843 dbh->rollback if $FS::UID::AutoCommit;
846 $i_sth->execute or do {
847 dbh->rollback if $FS::UID::AutoCommit;
848 return $i_sth->errstr;
850 $insertid = $i_sth->fetchrow_arrayref->[0];
855 dbh->rollback if $FS::UID::AutoCommit;
856 return "don't know how to retreive inserted ids from ". driver_name.
857 ", try using counterfiles (maybe run dbdef-create?)";
861 $self->setfield($primary_key, $insertid);
866 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
867 $self->virtual_fields;
868 if (@virtual_fields) {
869 my %v_values = map { $_, $self->getfield($_) } @virtual_fields;
871 my $vfieldpart = $self->vfieldpart_hashref;
873 my $v_statement = "INSERT INTO virtual_field(recnum, vfieldpart, value) ".
876 my $v_sth = dbh->prepare($v_statement) or do {
877 dbh->rollback if $FS::UID::AutoCommit;
881 foreach (keys(%v_values)) {
882 $v_sth->execute($self->getfield($primary_key),
886 dbh->rollback if $FS::UID::AutoCommit;
887 return $v_sth->errstr;
894 if ( defined dbdef->table('h_'. $table) ) {
895 my $h_statement = $self->_h_statement('insert');
896 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
897 $h_sth = dbh->prepare($h_statement) or do {
898 dbh->rollback if $FS::UID::AutoCommit;
904 $h_sth->execute or return $h_sth->errstr if $h_sth;
906 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
908 # Now that it has been saved, reset the encrypted fields so that $new
910 foreach my $field (keys %{$saved}) {
911 $self->setfield($field, $saved->{$field});
919 Depriciated (use insert instead).
924 cluck "warning: FS::Record::add deprecated!";
925 insert @_; #call method in this scope
930 Delete this record from the database. If there is an error, returns the error,
931 otherwise returns false.
938 my $statement = "DELETE FROM ". $self->table. " WHERE ". join(' AND ',
940 $self->getfield($_) eq ''
941 #? "( $_ IS NULL OR $_ = \"\" )"
942 ? ( driver_name eq 'Pg'
944 : "( $_ IS NULL OR $_ = \"\" )"
946 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
947 } ( $self->dbdef_table->primary_key )
948 ? ( $self->dbdef_table->primary_key)
949 : real_fields($self->table)
951 warn "[debug]$me $statement\n" if $DEBUG > 1;
952 my $sth = dbh->prepare($statement) or return dbh->errstr;
955 if ( defined dbdef->table('h_'. $self->table) ) {
956 my $h_statement = $self->_h_statement('delete');
957 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
958 $h_sth = dbh->prepare($h_statement) or return dbh->errstr;
963 my $primary_key = $self->dbdef_table->primary_key;
966 my $vfp = $self->vfieldpart_hashref;
967 foreach($self->virtual_fields) {
968 next if $self->getfield($_) eq '';
969 unless(@del_vfields) {
970 my $st = "DELETE FROM virtual_field WHERE recnum = ? AND vfieldpart = ?";
971 $v_sth = dbh->prepare($st) or return dbh->errstr;
973 push @del_vfields, $_;
976 local $SIG{HUP} = 'IGNORE';
977 local $SIG{INT} = 'IGNORE';
978 local $SIG{QUIT} = 'IGNORE';
979 local $SIG{TERM} = 'IGNORE';
980 local $SIG{TSTP} = 'IGNORE';
981 local $SIG{PIPE} = 'IGNORE';
983 my $rc = $sth->execute or return $sth->errstr;
984 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
985 $h_sth->execute or return $h_sth->errstr if $h_sth;
986 $v_sth->execute($self->getfield($primary_key), $vfp->{$_})
987 or return $v_sth->errstr
988 foreach (@del_vfields);
990 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
992 #no need to needlessly destoy the data either (causes problems actually)
993 #undef $self; #no need to keep object!
1000 Depriciated (use delete instead).
1005 cluck "warning: FS::Record::del deprecated!";
1006 &delete(@_); #call method in this scope
1009 =item replace OLD_RECORD
1011 Replace the OLD_RECORD with this one in the database. If there is an error,
1012 returns the error, otherwise returns false.
1017 my ($new, $old) = (shift, shift);
1019 $old = $new->replace_old unless defined($old);
1021 warn "[debug]$me $new ->replace $old\n" if $DEBUG;
1023 if ( $new->can('replace_check') ) {
1024 my $error = $new->replace_check($old);
1025 return $error if $error;
1028 return "Records not in same table!" unless $new->table eq $old->table;
1030 my $primary_key = $old->dbdef_table->primary_key;
1031 return "Can't change primary key $primary_key ".
1032 'from '. $old->getfield($primary_key).
1033 ' to ' . $new->getfield($primary_key)
1035 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
1037 my $error = $new->check;
1038 return $error if $error;
1040 # Encrypt for replace
1041 my $conf = new FS::Conf;
1043 if ($conf->exists('encryption') && defined(eval '@FS::'. $new->table . '::encrypted_fields')) {
1044 foreach my $field (eval '@FS::'. $new->table . '::encrypted_fields') {
1045 $saved->{$field} = $new->getfield($field);
1046 $new->setfield($field, $new->encrypt($new->getfield($field)));
1050 #my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
1051 my %diff = map { ($new->getfield($_) ne $old->getfield($_))
1052 ? ($_, $new->getfield($_)) : () } $old->fields;
1054 unless (keys(%diff) || $no_update_diff ) {
1055 carp "[warning]$me $new -> replace $old: records identical"
1056 unless $nowarn_identical;
1060 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
1062 "$_ = ". _quote($new->getfield($_),$old->table,$_)
1063 } real_fields($old->table)
1068 if ( $old->getfield($_) eq '' ) {
1070 #false laziness w/qsearch
1071 if ( driver_name eq 'Pg' ) {
1072 my $type = $old->dbdef_table->column($_)->type;
1073 if ( $type =~ /(int|(big)?serial)/i ) {
1076 qq-( $_ IS NULL OR $_ = '' )-;
1079 qq-( $_ IS NULL OR $_ = "" )-;
1083 "$_ = ". _quote($old->getfield($_),$old->table,$_);
1086 } ( $primary_key ? ( $primary_key ) : real_fields($old->table) )
1089 warn "[debug]$me $statement\n" if $DEBUG > 1;
1090 my $sth = dbh->prepare($statement) or return dbh->errstr;
1093 if ( defined dbdef->table('h_'. $old->table) ) {
1094 my $h_old_statement = $old->_h_statement('replace_old');
1095 warn "[debug]$me $h_old_statement\n" if $DEBUG > 2;
1096 $h_old_sth = dbh->prepare($h_old_statement) or return dbh->errstr;
1102 if ( defined dbdef->table('h_'. $new->table) ) {
1103 my $h_new_statement = $new->_h_statement('replace_new');
1104 warn "[debug]$me $h_new_statement\n" if $DEBUG > 2;
1105 $h_new_sth = dbh->prepare($h_new_statement) or return dbh->errstr;
1110 # For virtual fields we have three cases with different SQL
1111 # statements: add, replace, delete
1115 my (@add_vfields, @rep_vfields, @del_vfields);
1116 my $vfp = $old->vfieldpart_hashref;
1117 foreach(grep { exists($diff{$_}) } $new->virtual_fields) {
1118 if($diff{$_} eq '') {
1120 unless(@del_vfields) {
1121 my $st = "DELETE FROM virtual_field WHERE recnum = ? ".
1122 "AND vfieldpart = ?";
1123 warn "[debug]$me $st\n" if $DEBUG > 2;
1124 $v_del_sth = dbh->prepare($st) or return dbh->errstr;
1126 push @del_vfields, $_;
1127 } elsif($old->getfield($_) eq '') {
1129 unless(@add_vfields) {
1130 my $st = "INSERT INTO virtual_field (value, recnum, vfieldpart) ".
1132 warn "[debug]$me $st\n" if $DEBUG > 2;
1133 $v_add_sth = dbh->prepare($st) or return dbh->errstr;
1135 push @add_vfields, $_;
1138 unless(@rep_vfields) {
1139 my $st = "UPDATE virtual_field SET value = ? ".
1140 "WHERE recnum = ? AND vfieldpart = ?";
1141 warn "[debug]$me $st\n" if $DEBUG > 2;
1142 $v_rep_sth = dbh->prepare($st) or return dbh->errstr;
1144 push @rep_vfields, $_;
1148 local $SIG{HUP} = 'IGNORE';
1149 local $SIG{INT} = 'IGNORE';
1150 local $SIG{QUIT} = 'IGNORE';
1151 local $SIG{TERM} = 'IGNORE';
1152 local $SIG{TSTP} = 'IGNORE';
1153 local $SIG{PIPE} = 'IGNORE';
1155 my $rc = $sth->execute or return $sth->errstr;
1156 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
1157 $h_old_sth->execute or return $h_old_sth->errstr if $h_old_sth;
1158 $h_new_sth->execute or return $h_new_sth->errstr if $h_new_sth;
1160 $v_del_sth->execute($old->getfield($primary_key),
1162 or return $v_del_sth->errstr
1163 foreach(@del_vfields);
1165 $v_add_sth->execute($new->getfield($_),
1166 $old->getfield($primary_key),
1168 or return $v_add_sth->errstr
1169 foreach(@add_vfields);
1171 $v_rep_sth->execute($new->getfield($_),
1172 $old->getfield($primary_key),
1174 or return $v_rep_sth->errstr
1175 foreach(@rep_vfields);
1177 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1179 # Now that it has been saved, reset the encrypted fields so that $new
1180 # can still be used.
1181 foreach my $field (keys %{$saved}) {
1182 $new->setfield($field, $saved->{$field});
1190 my( $self ) = shift;
1191 warn "[$me] replace called with no arguments; autoloading old record\n"
1194 my $primary_key = $self->dbdef_table->primary_key;
1195 if ( $primary_key ) {
1196 $self->by_key( $self->$primary_key() ) #this is what's returned
1197 or croak "can't find ". $self->table. ".$primary_key ".
1198 $self->$primary_key();
1200 croak $self->table. " has no primary key; pass old record as argument";
1207 Depriciated (use replace instead).
1212 cluck "warning: FS::Record::rep deprecated!";
1213 replace @_; #call method in this scope
1218 Checks virtual fields (using check_blocks). Subclasses should still provide
1219 a check method to validate real fields, foreign keys, etc., and call this
1220 method via $self->SUPER::check.
1222 (FIXME: Should this method try to make sure that it I<is> being called from
1223 a subclass's check method, to keep the current semantics as far as possible?)
1228 #confess "FS::Record::check not implemented; supply one in subclass!";
1231 foreach my $field ($self->virtual_fields) {
1232 for ($self->getfield($field)) {
1233 # See notes on check_block in FS::part_virtual_field.
1234 eval $self->pvf($field)->check_block;
1236 #this is bad, probably want to follow the stack backtrace up and see
1238 my $err = "Fatal error checking $field for $self";
1240 return "$err (see log for backtrace): $@";
1243 $self->setfield($field, $_);
1250 my( $self, $action, $time ) = @_;
1255 grep { defined($self->getfield($_)) && $self->getfield($_) ne "" }
1256 real_fields($self->table);
1259 # If we're encrypting then don't ever store the payinfo or CVV2 in the history....
1260 # You can see if it changed by the paymask...
1261 my $conf = new FS::Conf;
1262 if ($conf->exists('encryption') ) {
1263 @fields = grep $_ ne 'payinfo' && $_ ne 'cvv2', @fields;
1265 my @values = map { _quote( $self->getfield($_), $self->table, $_) } @fields;
1267 "INSERT INTO h_". $self->table. " ( ".
1268 join(', ', qw(history_date history_user history_action), @fields ).
1270 join(', ', $time, dbh->quote(getotaker()), dbh->quote($action), @values).
1277 B<Warning>: External use is B<deprecated>.
1279 Replaces COLUMN in record with a unique number, using counters in the
1280 filesystem. Used by the B<insert> method on single-field unique columns
1281 (see L<DBIx::DBSchema::Table>) and also as a fallback for primary keys
1282 that aren't SERIAL (Pg) or AUTO_INCREMENT (mysql).
1284 Returns the new value.
1289 my($self,$field) = @_;
1290 my($table)=$self->table;
1292 croak "Unique called on field $field, but it is ",
1293 $self->getfield($field),
1295 if $self->getfield($field);
1297 #warn "table $table is tainted" if is_tainted($table);
1298 #warn "field $field is tainted" if is_tainted($field);
1300 my($counter) = new File::CounterFile "$table.$field",0;
1302 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
1304 # my($counter) = new File::CounterFile "$user/$table.$field",0;
1307 my $index = $counter->inc;
1308 $index = $counter->inc while qsearchs($table, { $field=>$index } );
1310 $index =~ /^(\d*)$/;
1313 $self->setfield($field,$index);
1317 =item ut_float COLUMN
1319 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
1320 null. If there is an error, returns the error, otherwise returns false.
1325 my($self,$field)=@_ ;
1326 ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
1327 $self->getfield($field) =~ /^(\d+)$/ ||
1328 $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
1329 $self->getfield($field) =~ /^(\d+e\d+)$/)
1330 or return "Illegal or empty (float) $field: ". $self->getfield($field);
1331 $self->setfield($field,$1);
1334 =item ut_floatn COLUMN
1336 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
1337 null. If there is an error, returns the error, otherwise returns false.
1341 #false laziness w/ut_ipn
1343 my( $self, $field ) = @_;
1344 if ( $self->getfield($field) =~ /^()$/ ) {
1345 $self->setfield($field,'');
1348 $self->ut_float($field);
1352 =item ut_sfloat COLUMN
1354 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10.
1355 May not be null. If there is an error, returns the error, otherwise returns
1361 my($self,$field)=@_ ;
1362 ($self->getfield($field) =~ /^(-?\d+\.\d+)$/ ||
1363 $self->getfield($field) =~ /^(-?\d+)$/ ||
1364 $self->getfield($field) =~ /^(-?\d+\.\d+[eE]-?\d+)$/ ||
1365 $self->getfield($field) =~ /^(-?\d+[eE]-?\d+)$/)
1366 or return "Illegal or empty (float) $field: ". $self->getfield($field);
1367 $self->setfield($field,$1);
1370 =item ut_sfloatn COLUMN
1372 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
1373 null. If there is an error, returns the error, otherwise returns false.
1378 my( $self, $field ) = @_;
1379 if ( $self->getfield($field) =~ /^()$/ ) {
1380 $self->setfield($field,'');
1383 $self->ut_sfloat($field);
1387 =item ut_snumber COLUMN
1389 Check/untaint signed numeric data (whole numbers). If there is an error,
1390 returns the error, otherwise returns false.
1395 my($self, $field) = @_;
1396 $self->getfield($field) =~ /^(-?)\s*(\d+)$/
1397 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1398 $self->setfield($field, "$1$2");
1402 =item ut_snumbern COLUMN
1404 Check/untaint signed numeric data (whole numbers). If there is an error,
1405 returns the error, otherwise returns false.
1410 my($self, $field) = @_;
1411 $self->getfield($field) =~ /^(-?)\s*(\d*)$/
1412 or return "Illegal (numeric) $field: ". $self->getfield($field);
1414 return "Illegal (numeric) $field: ". $self->getfield($field)
1417 $self->setfield($field, "$1$2");
1421 =item ut_number COLUMN
1423 Check/untaint simple numeric data (whole numbers). May not be null. If there
1424 is an error, returns the error, otherwise returns false.
1429 my($self,$field)=@_;
1430 $self->getfield($field) =~ /^(\d+)$/
1431 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1432 $self->setfield($field,$1);
1436 =item ut_numbern COLUMN
1438 Check/untaint simple numeric data (whole numbers). May be null. If there is
1439 an error, returns the error, otherwise returns false.
1444 my($self,$field)=@_;
1445 $self->getfield($field) =~ /^(\d*)$/
1446 or return "Illegal (numeric) $field: ". $self->getfield($field);
1447 $self->setfield($field,$1);
1451 =item ut_money COLUMN
1453 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
1454 is an error, returns the error, otherwise returns false.
1459 my($self,$field)=@_;
1460 $self->setfield($field, 0) if $self->getfield($field) eq '';
1461 $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
1462 or return "Illegal (money) $field: ". $self->getfield($field);
1463 #$self->setfield($field, "$1$2$3" || 0);
1464 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
1468 =item ut_text COLUMN
1470 Check/untaint text. Alphanumerics, spaces, and the following punctuation
1471 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / = [ ]
1472 May not be null. If there is an error, returns the error, otherwise returns
1478 my($self,$field)=@_;
1479 #warn "msgcat ". \&msgcat. "\n";
1480 #warn "notexist ". \¬exist. "\n";
1481 #warn "AUTOLOAD ". \&AUTOLOAD. "\n";
1482 $self->getfield($field)
1483 =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]+)$/
1484 or return gettext('illegal_or_empty_text'). " $field: ".
1485 $self->getfield($field);
1486 $self->setfield($field,$1);
1490 =item ut_textn COLUMN
1492 Check/untaint text. Alphanumerics, spaces, and the following punctuation
1493 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
1494 May be null. If there is an error, returns the error, otherwise returns false.
1499 my($self,$field)=@_;
1500 $self->getfield($field)
1501 =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]*)$/
1502 or return gettext('illegal_text'). " $field: ". $self->getfield($field);
1503 $self->setfield($field,$1);
1507 =item ut_alpha COLUMN
1509 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
1510 an error, returns the error, otherwise returns false.
1515 my($self,$field)=@_;
1516 $self->getfield($field) =~ /^(\w+)$/
1517 or return "Illegal or empty (alphanumeric) $field: ".
1518 $self->getfield($field);
1519 $self->setfield($field,$1);
1523 =item ut_alpha COLUMN
1525 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
1526 error, returns the error, otherwise returns false.
1531 my($self,$field)=@_;
1532 $self->getfield($field) =~ /^(\w*)$/
1533 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
1534 $self->setfield($field,$1);
1538 =item ut_phonen COLUMN [ COUNTRY ]
1540 Check/untaint phone numbers. May be null. If there is an error, returns
1541 the error, otherwise returns false.
1543 Takes an optional two-letter ISO country code; without it or with unsupported
1544 countries, ut_phonen simply calls ut_alphan.
1549 my( $self, $field, $country ) = @_;
1550 return $self->ut_alphan($field) unless defined $country;
1551 my $phonen = $self->getfield($field);
1552 if ( $phonen eq '' ) {
1553 $self->setfield($field,'');
1554 } elsif ( $country eq 'US' || $country eq 'CA' ) {
1556 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
1557 or return gettext('illegal_phone'). " $field: ". $self->getfield($field);
1558 $phonen = "$1-$2-$3";
1559 $phonen .= " x$4" if $4;
1560 $self->setfield($field,$phonen);
1562 warn "warning: don't know how to check phone numbers for country $country";
1563 return $self->ut_textn($field);
1570 Check/untaint hexadecimal values.
1575 my($self, $field) = @_;
1576 $self->getfield($field) =~ /^([\da-fA-F]+)$/
1577 or return "Illegal (hex) $field: ". $self->getfield($field);
1578 $self->setfield($field, uc($1));
1582 =item ut_hexn COLUMN
1584 Check/untaint hexadecimal values. May be null.
1589 my($self, $field) = @_;
1590 $self->getfield($field) =~ /^([\da-fA-F]*)$/
1591 or return "Illegal (hex) $field: ". $self->getfield($field);
1592 $self->setfield($field, uc($1));
1597 Check/untaint ip addresses. IPv4 only for now.
1602 my( $self, $field ) = @_;
1603 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
1604 or return "Illegal (IP address) $field: ". $self->getfield($field);
1605 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
1606 $self->setfield($field, "$1.$2.$3.$4");
1612 Check/untaint ip addresses. IPv4 only for now. May be null.
1617 my( $self, $field ) = @_;
1618 if ( $self->getfield($field) =~ /^()$/ ) {
1619 $self->setfield($field,'');
1622 $self->ut_ip($field);
1626 =item ut_coord COLUMN [ LOWER [ UPPER ] ]
1628 Check/untaint coordinates.
1629 Accepts the following forms:
1639 The "DDD MM SS" and "DDD MM MMM" are potentially ambiguous.
1640 The latter form (that is, the MMM are thousands of minutes) is
1641 assumed if the "MMM" is exactly three digits or two digits > 59.
1643 To be safe, just use the DDD.DDDDD form.
1645 If LOWER or UPPER are specified, then the coordinate is checked
1646 for lower and upper bounds, respectively.
1652 my ($self, $field) = (shift, shift);
1654 my $lower = shift if scalar(@_);
1655 my $upper = shift if scalar(@_);
1656 my $coord = $self->getfield($field);
1657 my $neg = $coord =~ s/^(-)//;
1659 my ($d, $m, $s) = (0, 0, 0);
1662 (($d) = ($coord =~ /^(\s*\d{1,3}(?:\.\d+)?)\s*$/)) ||
1663 (($d, $m) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2}(?:\.\d+))\s*$/)) ||
1664 (($d, $m, $s) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2})\s+(\d{1,3})\s*$/))
1666 $s = (((($s =~ /^\d{3}$/) or $s > 59) ? ($s / 1000) : ($s / 60)) / 60);
1669 return "Invalid (coordinate with minutes > 59) $field: "
1670 . $self->getfield($field);
1673 $coord = ($neg ? -1 : 1) * sprintf('%.8f', $d + $m + $s);
1675 if (defined($lower) and ($coord < $lower)) {
1676 return "Invalid (coordinate < $lower) $field: "
1677 . $self->getfield($field);;
1680 if (defined($upper) and ($coord > $upper)) {
1681 return "Invalid (coordinate > $upper) $field: "
1682 . $self->getfield($field);;
1685 $self->setfield($field, $coord);
1689 return "Invalid (coordinate) $field: " . $self->getfield($field);
1693 =item ut_coordn COLUMN [ LOWER [ UPPER ] ]
1695 Same as ut_coord, except optionally null.
1701 my ($self, $field) = (shift, shift);
1703 if ($self->getfield($field) =~ /^$/) {
1706 return $self->ut_coord($field, @_);
1712 =item ut_domain COLUMN
1714 Check/untaint host and domain names.
1719 my( $self, $field ) = @_;
1720 #$self->getfield($field) =~/^(\w+\.)*\w+$/
1721 $self->getfield($field) =~/^(([\w\-]+\.)*\w+)$/
1722 or return "Illegal (domain) $field: ". $self->getfield($field);
1723 $self->setfield($field,$1);
1727 =item ut_name COLUMN
1729 Check/untaint proper names; allows alphanumerics, spaces and the following
1730 punctuation: , . - '
1737 my( $self, $field ) = @_;
1738 $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
1739 or return gettext('illegal_name'). " $field: ". $self->getfield($field);
1740 $self->setfield($field,$1);
1746 Check/untaint zip codes.
1750 my @zip_reqd_countries = qw( AU CA US ); #CA, US implicit...
1753 my( $self, $field, $country ) = @_;
1755 if ( $country eq 'US' ) {
1757 $self->getfield($field) =~ /^\s*(\d{5}(\-\d{4})?)\s*$/
1758 or return gettext('illegal_zip'). " $field for country $country: ".
1759 $self->getfield($field);
1760 $self->setfield($field, $1);
1762 } elsif ( $country eq 'CA' ) {
1764 $self->getfield($field) =~ /^\s*([A-Z]\d[A-Z])\s*(\d[A-Z]\d)\s*$/i
1765 or return gettext('illegal_zip'). " $field for country $country: ".
1766 $self->getfield($field);
1767 $self->setfield($field, "$1 $2");
1771 if ( $self->getfield($field) =~ /^\s*$/
1772 && ( !$country || ! grep { $_ eq $country } @zip_reqd_countries )
1775 $self->setfield($field,'');
1777 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
1778 or return gettext('illegal_zip'). " $field: ". $self->getfield($field);
1779 $self->setfield($field,$1);
1787 =item ut_country COLUMN
1789 Check/untaint country codes. Country names are changed to codes, if possible -
1790 see L<Locale::Country>.
1795 my( $self, $field ) = @_;
1796 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
1797 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
1798 && country2code($1) ) {
1799 $self->setfield($field,uc(country2code($1)));
1802 $self->getfield($field) =~ /^(\w\w)$/
1803 or return "Illegal (country) $field: ". $self->getfield($field);
1804 $self->setfield($field,uc($1));
1808 =item ut_anything COLUMN
1810 Untaints arbitrary data. Be careful.
1815 my( $self, $field ) = @_;
1816 $self->getfield($field) =~ /^(.*)$/s
1817 or return "Illegal $field: ". $self->getfield($field);
1818 $self->setfield($field,$1);
1822 =item ut_enum COLUMN CHOICES_ARRAYREF
1824 Check/untaint a column, supplying all possible choices, like the "enum" type.
1829 my( $self, $field, $choices ) = @_;
1830 foreach my $choice ( @$choices ) {
1831 if ( $self->getfield($field) eq $choice ) {
1832 $self->setfield($choice);
1836 return "Illegal (enum) field $field: ". $self->getfield($field);
1839 =item ut_foreign_key COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1841 Check/untaint a foreign column key. Call a regular ut_ method (like ut_number)
1842 on the column first.
1846 sub ut_foreign_key {
1847 my( $self, $field, $table, $foreign ) = @_;
1848 qsearchs($table, { $foreign => $self->getfield($field) })
1849 or return "Can't find ". $self->table. ".$field ". $self->getfield($field).
1850 " in $table.$foreign";
1854 =item ut_foreign_keyn COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1856 Like ut_foreign_key, except the null value is also allowed.
1860 sub ut_foreign_keyn {
1861 my( $self, $field, $table, $foreign ) = @_;
1862 $self->getfield($field)
1863 ? $self->ut_foreign_key($field, $table, $foreign)
1867 =item ut_agentnum_acl
1869 Checks this column as an agentnum, taking into account the current users's
1874 sub ut_agentnum_acl {
1875 my( $self, $field, $null_acl ) = @_;
1877 my $error = $self->ut_foreign_keyn($field, 'agent', 'agentnum');
1878 return "Illegal agentnum: $error" if $error;
1880 my $curuser = $FS::CurrentUser::CurrentUser;
1882 if ( $self->$field() ) {
1884 return "Access deined"
1885 unless $curuser->agentnum($self->$field());
1889 return "Access denied"
1890 unless $curuser->access_right($null_acl);
1898 =item virtual_fields [ TABLE ]
1900 Returns a list of virtual fields defined for the table. This should not
1901 be exported, and should only be called as an instance or class method.
1905 sub virtual_fields {
1908 $table = $self->table or confess "virtual_fields called on non-table";
1910 confess "Unknown table $table" unless dbdef->table($table);
1912 return () unless dbdef->table('part_virtual_field');
1914 unless ( $virtual_fields_cache{$table} ) {
1915 my $query = 'SELECT name from part_virtual_field ' .
1916 "WHERE dbtable = '$table'";
1918 my $result = $dbh->selectcol_arrayref($query);
1919 confess "Error executing virtual fields query: $query: ". $dbh->errstr
1921 $virtual_fields_cache{$table} = $result;
1924 @{$virtual_fields_cache{$table}};
1929 =item fields [ TABLE ]
1931 This is a wrapper for real_fields and virtual_fields. Code that called
1932 fields before should probably continue to call fields.
1937 my $something = shift;
1939 if($something->isa('FS::Record')) {
1940 $table = $something->table;
1942 $table = $something;
1943 $something = "FS::$table";
1945 return (real_fields($table), $something->virtual_fields());
1950 =item pvf FIELD_NAME
1952 Returns the FS::part_virtual_field object corresponding to a field in the
1953 record (specified by FIELD_NAME).
1958 my ($self, $name) = (shift, shift);
1960 if(grep /^$name$/, $self->virtual_fields) {
1961 return qsearchs('part_virtual_field', { dbtable => $self->table,
1971 =item real_fields [ TABLE ]
1973 Returns a list of the real columns in the specified table. Called only by
1974 fields() and other subroutines elsewhere in FS::Record.
1981 my($table_obj) = dbdef->table($table);
1982 confess "Unknown table $table" unless $table_obj;
1983 $table_obj->columns;
1986 =item _quote VALUE, TABLE, COLUMN
1988 This is an internal function used to construct SQL statements. It returns
1989 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
1990 type (see L<DBIx::DBSchema::Column>) does not end in `char' or `binary'.
1995 my($value, $table, $column) = @_;
1996 my $column_obj = dbdef->table($table)->column($column);
1997 my $column_type = $column_obj->type;
1998 my $nullable = $column_obj->null;
2000 warn " $table.$column: $value ($column_type".
2001 ( $nullable ? ' NULL' : ' NOT NULL' ).
2002 ")\n" if $DEBUG > 2;
2004 if ( $value eq '' && $nullable ) {
2006 } elsif ( $value eq '' && $column_type =~ /^(int|numeric)/ ) {
2007 cluck "WARNING: Attempting to set non-null integer $table.$column null; ".
2010 } elsif ( $value =~ /^\d+(\.\d+)?$/ &&
2011 ! $column_type =~ /(char|binary|text)$/i ) {
2018 =item vfieldpart_hashref TABLE
2020 Returns a hashref of virtual field names and vfieldparts applicable to the given
2025 sub vfieldpart_hashref {
2027 my $table = $self->table;
2029 return {} unless dbdef->table('part_virtual_field');
2032 my $statement = "SELECT vfieldpart, name FROM part_virtual_field WHERE ".
2033 "dbtable = '$table'";
2034 my $sth = $dbh->prepare($statement);
2035 $sth->execute or croak "Execution of '$statement' failed: ".$dbh->errstr;
2036 return { map { $_->{name}, $_->{vfieldpart} }
2037 @{$sth->fetchall_arrayref({})} };
2044 This is deprecated. Don't use it.
2046 It returns a hash-type list with the fields of this record's table set true.
2051 carp "warning: hfields is deprecated";
2054 foreach (fields($table)) {
2063 "$_: ". $self->getfield($_). "|"
2064 } (fields($self->table)) );
2067 =item encrypt($value)
2069 Encrypts the credit card using a combination of PK to encrypt and uuencode to armour.
2071 Returns the encrypted string.
2073 You should generally not have to worry about calling this, as the system handles this for you.
2079 my ($self, $value) = @_;
2082 my $conf = new FS::Conf;
2083 if ($conf->exists('encryption')) {
2084 if ($self->is_encrypted($value)) {
2085 # Return the original value if it isn't plaintext.
2086 $encrypted = $value;
2089 if (ref($rsa_encrypt) =~ /::RSA/) { # We Can Encrypt
2090 # RSA doesn't like the empty string so let's pack it up
2091 # The database doesn't like the RSA data so uuencode it
2092 my $length = length($value)+1;
2093 $encrypted = pack("u*",$rsa_encrypt->encrypt(pack("Z$length",$value)));
2095 die ("You can't encrypt w/o a valid RSA engine - Check your installation or disable encryption");
2102 =item is_encrypted($value)
2104 Checks to see if the string is encrypted and returns true or false (1/0) to indicate it's status.
2110 my ($self, $value) = @_;
2111 # Possible Bug - Some work may be required here....
2113 if ($value =~ /^M/ && length($value) > 80) {
2120 =item decrypt($value)
2122 Uses the private key to decrypt the string. Returns the decryoted string or undef on failure.
2124 You should generally not have to worry about calling this, as the system handles this for you.
2129 my ($self,$value) = @_;
2130 my $decrypted = $value; # Will return the original value if it isn't encrypted or can't be decrypted.
2131 my $conf = new FS::Conf;
2132 if ($conf->exists('encryption') && $self->is_encrypted($value)) {
2134 if (ref($rsa_decrypt) =~ /::RSA/) {
2135 my $encrypted = unpack ("u*", $value);
2136 $decrypted = unpack("Z*", eval{$rsa_decrypt->decrypt($encrypted)});
2137 if ($@) {warn "Decryption Failed"};
2145 #Initialize the Module
2146 $rsa_module = 'Crypt::OpenSSL::RSA'; # The Default
2148 my $conf = new FS::Conf;
2149 if ($conf->exists('encryptionmodule') && $conf->config_binary('encryptionmodule') ne '') {
2150 $rsa_module = $conf->config('encryptionmodule');
2154 eval ("require $rsa_module"); # No need to import the namespace
2157 # Initialize Encryption
2158 if ($conf->exists('encryptionpublickey') && $conf->config_binary('encryptionpublickey') ne '') {
2159 my $public_key = join("\n",$conf->config('encryptionpublickey'));
2160 $rsa_encrypt = $rsa_module->new_public_key($public_key);
2163 # Intitalize Decryption
2164 if ($conf->exists('encryptionprivatekey') && $conf->config_binary('encryptionprivatekey') ne '') {
2165 my $private_key = join("\n",$conf->config('encryptionprivatekey'));
2166 $rsa_decrypt = $rsa_module->new_private_key($private_key);
2170 sub DESTROY { return; }
2174 # #use Carp qw(cluck);
2175 # #cluck "DESTROYING $self";
2176 # warn "DESTROYING $self";
2180 # return ! eval { join('',@_), kill 0; 1; };
2187 This module should probably be renamed, since much of the functionality is
2188 of general use. It is not completely unlike Adapter::DBI (see below).
2190 Exported qsearch and qsearchs should be deprecated in favor of method calls
2191 (against an FS::Record object like the old search and searchs that qsearch
2192 and qsearchs were on top of.)
2194 The whole fields / hfields mess should be removed.
2196 The various WHERE clauses should be subroutined.
2198 table string should be deprecated in favor of DBIx::DBSchema::Table.
2200 No doubt we could benefit from a Tied hash. Documenting how exists / defined
2201 true maps to the database (and WHERE clauses) would also help.
2203 The ut_ methods should ask the dbdef for a default length.
2205 ut_sqltype (like ut_varchar) should all be defined
2207 A fallback check method should be provided which uses the dbdef.
2209 The ut_money method assumes money has two decimal digits.
2211 The Pg money kludge in the new method only strips `$'.
2213 The ut_phonen method only checks US-style phone numbers.
2215 The _quote function should probably use ut_float instead of a regex.
2217 All the subroutines probably should be methods, here or elsewhere.
2219 Probably should borrow/use some dbdef methods where appropriate (like sub
2222 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
2223 or allow it to be set. Working around it is ugly any way around - DBI should
2224 be fixed. (only affects RDBMS which return uppercase column names)
2226 ut_zip should take an optional country like ut_phone.
2230 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
2232 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.