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 #'cache_obj' => '', #optional
217 'addl_from' => 'LEFT JOIN othtable USING ( field )',
221 Much code still uses old-style positional parameters, this is also probably
222 fine in the common case where there are only two parameters:
224 my @records = qsearch( 'table', { 'field' => 'value' } );
226 ###oops, argh, FS::Record::new only lets us create database fields.
227 #Normal behaviour if SELECT is not specified is `*', as in
228 #C<SELECT * FROM table WHERE ...>. However, there is an experimental new
229 #feature where you can specify SELECT - remember, the objects returned,
230 #although blessed into the appropriate `FS::TABLE' package, will only have the
231 #fields you specify. This might have unwanted results if you then go calling
232 #regular FS::TABLE methods
238 my($stable, $record, $select, $extra_sql, $cache, $addl_from );
239 if ( ref($_[0]) ) { #hashref for now, eventually maybe accept a list too
241 $stable = $opt->{'table'} or die "table name is required";
242 $record = $opt->{'hashref'} || {};
243 $select = $opt->{'select'} || '*';
244 $extra_sql = $opt->{'extra_sql'} || '';
245 $cache = $opt->{'cache_obj'} || '';
246 $addl_from = $opt->{'addl_from'} || '';
248 ($stable, $record, $select, $extra_sql, $cache, $addl_from ) = @_;
252 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
254 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
258 my $table = $cache ? $cache->table : $stable;
259 my $dbdef_table = dbdef->table($table)
260 or die "No schema for table $table found - ".
261 "do you need to run freeside-upgrade?";
262 my $pkey = $dbdef_table->primary_key;
264 my @real_fields = grep exists($record->{$_}), real_fields($table);
266 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
267 @virtual_fields = grep exists($record->{$_}), "FS::$table"->virtual_fields;
269 cluck "warning: FS::$table not loaded; virtual fields not searchable";
270 @virtual_fields = ();
273 my $statement = "SELECT $select FROM $stable";
274 $statement .= " $addl_from" if $addl_from;
275 if ( @real_fields or @virtual_fields ) {
276 $statement .= ' WHERE '. join(' AND ',
281 if ( ref($record->{$_}) ) {
282 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
283 #$op = 'LIKE' if $op =~ /^ILIKE$/i && driver_name ne 'Pg';
284 if ( uc($op) eq 'ILIKE' ) {
286 $record->{$_}{'value'} = lc($record->{$_}{'value'});
287 $column = "LOWER($_)";
289 $record->{$_} = $record->{$_}{'value'}
292 if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) {
294 if ( driver_name eq 'Pg' ) {
295 my $type = dbdef->table($table)->column($column)->type;
296 if ( $type =~ /(int|(big)?serial)/i ) {
297 qq-( $column IS NULL )-;
299 qq-( $column IS NULL OR $column = '' )-;
302 qq-( $column IS NULL OR $column = "" )-;
304 } elsif ( $op eq '!=' ) {
305 if ( driver_name eq 'Pg' ) {
306 my $type = dbdef->table($table)->column($column)->type;
307 if ( $type =~ /(int|(big)?serial)/i ) {
308 qq-( $column IS NOT NULL )-;
310 qq-( $column IS NOT NULL AND $column != '' )-;
313 qq-( $column IS NOT NULL AND $column != "" )-;
316 if ( driver_name eq 'Pg' ) {
317 qq-( $column $op '' )-;
319 qq-( $column $op "" )-;
329 if ( ref($record->{$_}) ) {
330 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
331 if ( uc($op) eq 'ILIKE' ) {
333 $record->{$_}{'value'} = lc($record->{$_}{'value'});
334 $column = "LOWER($_)";
336 $record->{$_} = $record->{$_}{'value'};
339 # ... EXISTS ( SELECT name, value FROM part_virtual_field
341 # ON part_virtual_field.vfieldpart = virtual_field.vfieldpart
342 # WHERE recnum = svc_acct.svcnum
343 # AND (name, value) = ('egad', 'brain') )
345 my $value = $record->{$_};
349 $subq = ($value ? 'EXISTS ' : 'NOT EXISTS ') .
350 "( SELECT part_virtual_field.name, virtual_field.value ".
351 "FROM part_virtual_field JOIN virtual_field ".
352 "ON part_virtual_field.vfieldpart = virtual_field.vfieldpart ".
353 "WHERE virtual_field.recnum = ${table}.${pkey} ".
354 "AND part_virtual_field.name = '${column}'".
356 " AND virtual_field.value ${op} '${value}'"
360 } @virtual_fields ) );
364 $statement .= " $extra_sql" if defined($extra_sql);
366 warn "[debug]$me $statement\n" if $DEBUG > 1;
367 my $sth = $dbh->prepare($statement)
368 or croak "$dbh->errstr doing $statement";
373 grep defined( $record->{$_} ) && $record->{$_} ne '', @real_fields
375 if ( $record->{$field} =~ /^\d+(\.\d+)?$/
376 && dbdef->table($table)->column($field)->type =~ /(int|(big)?serial)/i
378 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_INTEGER } );
380 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_VARCHAR } );
384 # $sth->execute( map $record->{$_},
385 # grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
386 # ) or croak "Error executing \"$statement\": ". $sth->errstr;
388 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
390 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
391 @virtual_fields = "FS::$table"->virtual_fields;
393 cluck "warning: FS::$table not loaded; virtual fields not returned either";
394 @virtual_fields = ();
398 tie %result, "Tie::IxHash";
399 my @stuff = @{ $sth->fetchall_arrayref( {} ) };
400 if ( $pkey && scalar(@stuff) && $stuff[0]->{$pkey} ) {
401 %result = map { $_->{$pkey}, $_ } @stuff;
403 @result{@stuff} = @stuff;
408 if ( keys(%result) and @virtual_fields ) {
410 "SELECT virtual_field.recnum, part_virtual_field.name, ".
411 "virtual_field.value ".
412 "FROM part_virtual_field JOIN virtual_field USING (vfieldpart) ".
413 "WHERE part_virtual_field.dbtable = '$table' AND ".
414 "virtual_field.recnum IN (".
415 join(',', keys(%result)). ") AND part_virtual_field.name IN ('".
416 join(q!', '!, @virtual_fields) . "')";
417 warn "[debug]$me $statement\n" if $DEBUG > 1;
418 $sth = $dbh->prepare($statement) or croak "$dbh->errstr doing $statement";
419 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
421 foreach (@{ $sth->fetchall_arrayref({}) }) {
422 my $recnum = $_->{recnum};
423 my $name = $_->{name};
424 my $value = $_->{value};
425 if (exists($result{$recnum})) {
426 $result{$recnum}->{$name} = $value;
431 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
432 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
433 #derivied class didn't override new method, so this optimization is safe
436 new_or_cached( "FS::$table", { %{$_} }, $cache )
440 new( "FS::$table", { %{$_} } )
444 #okay, its been tested
445 # warn "untested code (class FS::$table uses custom new method)";
447 eval 'FS::'. $table. '->new( { %{$_} } )';
451 # Check for encrypted fields and decrypt them.
452 ## only in the local copy, not the cached object
453 if ( $conf && $conf->exists('encryption') # $conf doesn't exist when doing
454 # the initial search for
456 && eval 'defined(@FS::'. $table . '::encrypted_fields)') {
457 foreach my $record (@return) {
458 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
459 # Set it directly... This may cause a problem in the future...
460 $record->setfield($field, $record->decrypt($record->getfield($field)));
465 cluck "warning: FS::$table not loaded; returning FS::Record objects";
467 FS::Record->new( $table, { %{$_} } );
473 =item by_key PRIMARY_KEY_VALUE
475 This is a class method that returns the record with the given primary key
476 value. This method is only useful in FS::Record subclasses. For example:
478 my $cust_main = FS::cust_main->by_key(1); # retrieve customer with custnum 1
482 my $cust_main = qsearchs('cust_main', { 'custnum' => 1 } );
487 my ($class, $pkey_value) = @_;
489 my $table = $class->table
490 or croak "No table for $class found";
492 my $dbdef_table = dbdef->table($table)
493 or die "No schema for table $table found - ".
494 "do you need to create it or run dbdef-create?";
495 my $pkey = $dbdef_table->primary_key
496 or die "No primary key for table $table";
498 return qsearchs($table, { $pkey => $pkey_value });
501 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
503 Experimental JOINed search method. Using this method, you can execute a
504 single SELECT spanning multiple tables, and cache the results for subsequent
505 method calls. Interface will almost definately change in an incompatible
513 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
514 my $cache = FS::SearchCache->new( $ptable, $pkey );
517 grep { !$saw{$_->getfield($pkey)}++ }
518 qsearch($table, $record, $select, $extra_sql, $cache )
522 =item qsearchs PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
524 Same as qsearch, except that if more than one record matches, it B<carp>s but
525 returns the first. If this happens, you either made a logic error in asking
526 for a single item, or your data is corrupted.
530 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
532 my(@result) = qsearch(@_);
533 cluck "warning: Multiple records in scalar search ($table)"
534 if scalar(@result) > 1;
535 #should warn more vehemently if the search was on a primary key?
536 scalar(@result) ? ($result[0]) : ();
547 Returns the table name.
552 # cluck "warning: FS::Record::table deprecated; supply one in subclass!";
559 Returns the DBIx::DBSchema::Table object for the table.
565 my($table)=$self->table;
566 dbdef->table($table);
571 Returns the primary key for the table.
577 my $pkey = $self->dbdef_table->primary_key;
580 =item get, getfield COLUMN
582 Returns the value of the column/field/key COLUMN.
587 my($self,$field) = @_;
588 # to avoid "Use of unitialized value" errors
589 if ( defined ( $self->{Hash}->{$field} ) ) {
590 $self->{Hash}->{$field};
600 =item set, setfield COLUMN, VALUE
602 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
607 my($self,$field,$value) = @_;
608 $self->{'modified'} = 1;
609 $self->{'Hash'}->{$field} = $value;
616 =item AUTLOADED METHODS
618 $record->column is a synonym for $record->get('column');
620 $record->column('value') is a synonym for $record->set('column','value');
627 my($field)=$AUTOLOAD;
629 if ( defined($value) ) {
630 confess "errant AUTOLOAD $field for $self (arg $value)"
631 unless ref($self) && $self->can('setfield');
632 $self->setfield($field,$value);
634 confess "errant AUTOLOAD $field for $self (no args)"
635 unless ref($self) && $self->can('getfield');
636 $self->getfield($field);
642 # my $field = $AUTOLOAD;
644 # if ( defined($_[1]) ) {
645 # $_[0]->setfield($field, $_[1]);
647 # $_[0]->getfield($field);
653 Returns a list of the column/value pairs, usually for assigning to a new hash.
655 To make a distinct duplicate of an FS::Record object, you can do:
657 $new = new FS::Record ( $old->table, { $old->hash } );
663 confess $self. ' -> hash: Hash attribute is undefined'
664 unless defined($self->{'Hash'});
665 %{ $self->{'Hash'} };
670 Returns a reference to the column/value hash. This may be deprecated in the
671 future; if there's a reason you can't just use the autoloaded or get/set
683 Returns true if any of this object's values have been modified with set (or via
684 an autoloaded method). Doesn't yet recognize when you retreive a hashref and
694 =item select_for_update
696 Selects this record with the SQL "FOR UPDATE" command. This can be useful as
701 sub select_for_update {
703 my $primary_key = $self->primary_key;
706 'table' => $self->table,
707 'hashref' => { $primary_key => $self->$primary_key() },
708 'extra_sql' => 'FOR UPDATE',
714 Inserts this record to the database. If there is an error, returns the error,
715 otherwise returns false.
723 warn "$self -> insert" if $DEBUG;
725 my $error = $self->check;
726 return $error if $error;
728 #single-field unique keys are given a value if false
729 #(like MySQL's AUTO_INCREMENT or Pg SERIAL)
730 foreach ( $self->dbdef_table->can('unique_singles')
731 ? $self->dbdef_table->unique_singles
732 : $self->dbdef_table->unique->singles
734 $self->unique($_) unless $self->getfield($_);
737 #and also the primary key, if the database isn't going to
738 my $primary_key = $self->dbdef_table->primary_key;
740 if ( $primary_key ) {
741 my $col = $self->dbdef_table->column($primary_key);
744 uc($col->type) =~ /^(BIG)?SERIAL\d?/
745 || ( driver_name eq 'Pg'
746 && defined($col->default)
747 && $col->default =~ /^nextval\(/i
749 || ( driver_name eq 'mysql'
750 && defined($col->local)
751 && $col->local =~ /AUTO_INCREMENT/i
753 $self->unique($primary_key) unless $self->getfield($primary_key) || $db_seq;
756 my $table = $self->table;
759 # Encrypt before the database
760 if ($conf->exists('encryption') && defined(eval '@FS::'. $table . '::encrypted_fields')) {
761 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
762 $self->{'saved'} = $self->getfield($field);
763 $self->setfield($field, $self->encrypt($self->getfield($field)));
768 #false laziness w/delete
770 grep { defined($self->getfield($_)) && $self->getfield($_) ne "" }
773 my @values = map { _quote( $self->getfield($_), $table, $_) } @real_fields;
776 my $statement = "INSERT INTO $table ";
777 if ( @real_fields ) {
780 join( ', ', @real_fields ).
782 join( ', ', @values ).
786 $statement .= 'DEFAULT VALUES';
788 warn "[debug]$me $statement\n" if $DEBUG > 1;
789 my $sth = dbh->prepare($statement) or return dbh->errstr;
791 local $SIG{HUP} = 'IGNORE';
792 local $SIG{INT} = 'IGNORE';
793 local $SIG{QUIT} = 'IGNORE';
794 local $SIG{TERM} = 'IGNORE';
795 local $SIG{TSTP} = 'IGNORE';
796 local $SIG{PIPE} = 'IGNORE';
798 $sth->execute or return $sth->errstr;
800 # get inserted id from the database, if applicable & needed
801 if ( $db_seq && ! $self->getfield($primary_key) ) {
802 warn "[debug]$me retreiving sequence from database\n" if $DEBUG;
806 if ( driver_name eq 'Pg' ) {
808 #my $oid = $sth->{'pg_oid_status'};
809 #my $i_sql = "SELECT $primary_key FROM $table WHERE oid = ?";
811 my $default = $self->dbdef_table->column($primary_key)->default;
812 unless ( $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i ) {
813 dbh->rollback if $FS::UID::AutoCommit;
814 return "can't parse $table.$primary_key default value".
815 " for sequence name: $default";
819 my $i_sql = "SELECT currval('$sequence')";
820 my $i_sth = dbh->prepare($i_sql) or do {
821 dbh->rollback if $FS::UID::AutoCommit;
824 $i_sth->execute() or do { #$i_sth->execute($oid)
825 dbh->rollback if $FS::UID::AutoCommit;
826 return $i_sth->errstr;
828 $insertid = $i_sth->fetchrow_arrayref->[0];
830 } elsif ( driver_name eq 'mysql' ) {
832 $insertid = dbh->{'mysql_insertid'};
833 # work around mysql_insertid being null some of the time, ala RT :/
834 unless ( $insertid ) {
835 warn "WARNING: DBD::mysql didn't return mysql_insertid; ".
836 "using SELECT LAST_INSERT_ID();";
837 my $i_sql = "SELECT LAST_INSERT_ID()";
838 my $i_sth = dbh->prepare($i_sql) or do {
839 dbh->rollback if $FS::UID::AutoCommit;
842 $i_sth->execute or do {
843 dbh->rollback if $FS::UID::AutoCommit;
844 return $i_sth->errstr;
846 $insertid = $i_sth->fetchrow_arrayref->[0];
851 dbh->rollback if $FS::UID::AutoCommit;
852 return "don't know how to retreive inserted ids from ". driver_name.
853 ", try using counterfiles (maybe run dbdef-create?)";
857 $self->setfield($primary_key, $insertid);
862 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
863 $self->virtual_fields;
864 if (@virtual_fields) {
865 my %v_values = map { $_, $self->getfield($_) } @virtual_fields;
867 my $vfieldpart = $self->vfieldpart_hashref;
869 my $v_statement = "INSERT INTO virtual_field(recnum, vfieldpart, value) ".
872 my $v_sth = dbh->prepare($v_statement) or do {
873 dbh->rollback if $FS::UID::AutoCommit;
877 foreach (keys(%v_values)) {
878 $v_sth->execute($self->getfield($primary_key),
882 dbh->rollback if $FS::UID::AutoCommit;
883 return $v_sth->errstr;
890 if ( defined dbdef->table('h_'. $table) ) {
891 my $h_statement = $self->_h_statement('insert');
892 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
893 $h_sth = dbh->prepare($h_statement) or do {
894 dbh->rollback if $FS::UID::AutoCommit;
900 $h_sth->execute or return $h_sth->errstr if $h_sth;
902 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
904 # Now that it has been saved, reset the encrypted fields so that $new
906 foreach my $field (keys %{$saved}) {
907 $self->setfield($field, $saved->{$field});
915 Depriciated (use insert instead).
920 cluck "warning: FS::Record::add deprecated!";
921 insert @_; #call method in this scope
926 Delete this record from the database. If there is an error, returns the error,
927 otherwise returns false.
934 my $statement = "DELETE FROM ". $self->table. " WHERE ". join(' AND ',
936 $self->getfield($_) eq ''
937 #? "( $_ IS NULL OR $_ = \"\" )"
938 ? ( driver_name eq 'Pg'
940 : "( $_ IS NULL OR $_ = \"\" )"
942 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
943 } ( $self->dbdef_table->primary_key )
944 ? ( $self->dbdef_table->primary_key)
945 : real_fields($self->table)
947 warn "[debug]$me $statement\n" if $DEBUG > 1;
948 my $sth = dbh->prepare($statement) or return dbh->errstr;
951 if ( defined dbdef->table('h_'. $self->table) ) {
952 my $h_statement = $self->_h_statement('delete');
953 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
954 $h_sth = dbh->prepare($h_statement) or return dbh->errstr;
959 my $primary_key = $self->dbdef_table->primary_key;
962 my $vfp = $self->vfieldpart_hashref;
963 foreach($self->virtual_fields) {
964 next if $self->getfield($_) eq '';
965 unless(@del_vfields) {
966 my $st = "DELETE FROM virtual_field WHERE recnum = ? AND vfieldpart = ?";
967 $v_sth = dbh->prepare($st) or return dbh->errstr;
969 push @del_vfields, $_;
972 local $SIG{HUP} = 'IGNORE';
973 local $SIG{INT} = 'IGNORE';
974 local $SIG{QUIT} = 'IGNORE';
975 local $SIG{TERM} = 'IGNORE';
976 local $SIG{TSTP} = 'IGNORE';
977 local $SIG{PIPE} = 'IGNORE';
979 my $rc = $sth->execute or return $sth->errstr;
980 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
981 $h_sth->execute or return $h_sth->errstr if $h_sth;
982 $v_sth->execute($self->getfield($primary_key), $vfp->{$_})
983 or return $v_sth->errstr
984 foreach (@del_vfields);
986 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
988 #no need to needlessly destoy the data either (causes problems actually)
989 #undef $self; #no need to keep object!
996 Depriciated (use delete instead).
1001 cluck "warning: FS::Record::del deprecated!";
1002 &delete(@_); #call method in this scope
1005 =item replace OLD_RECORD
1007 Replace the OLD_RECORD with this one in the database. If there is an error,
1008 returns the error, otherwise returns false.
1013 my ($new, $old) = (shift, shift);
1015 $old = $new->replace_old unless defined($old);
1017 warn "[debug]$me $new ->replace $old\n" if $DEBUG;
1019 if ( $new->can('replace_check') ) {
1020 my $error = $new->replace_check($old);
1021 return $error if $error;
1024 return "Records not in same table!" unless $new->table eq $old->table;
1026 my $primary_key = $old->dbdef_table->primary_key;
1027 return "Can't change primary key $primary_key ".
1028 'from '. $old->getfield($primary_key).
1029 ' to ' . $new->getfield($primary_key)
1031 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
1033 my $error = $new->check;
1034 return $error if $error;
1036 # Encrypt for replace
1037 my $conf = new FS::Conf;
1039 if ($conf->exists('encryption') && defined(eval '@FS::'. $new->table . '::encrypted_fields')) {
1040 foreach my $field (eval '@FS::'. $new->table . '::encrypted_fields') {
1041 $saved->{$field} = $new->getfield($field);
1042 $new->setfield($field, $new->encrypt($new->getfield($field)));
1046 #my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
1047 my %diff = map { ($new->getfield($_) ne $old->getfield($_))
1048 ? ($_, $new->getfield($_)) : () } $old->fields;
1050 unless (keys(%diff) || $no_update_diff ) {
1051 carp "[warning]$me $new -> replace $old: records identical"
1052 unless $nowarn_identical;
1056 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
1058 "$_ = ". _quote($new->getfield($_),$old->table,$_)
1059 } real_fields($old->table)
1064 if ( $old->getfield($_) eq '' ) {
1066 #false laziness w/qsearch
1067 if ( driver_name eq 'Pg' ) {
1068 my $type = $old->dbdef_table->column($_)->type;
1069 if ( $type =~ /(int|(big)?serial)/i ) {
1072 qq-( $_ IS NULL OR $_ = '' )-;
1075 qq-( $_ IS NULL OR $_ = "" )-;
1079 "$_ = ". _quote($old->getfield($_),$old->table,$_);
1082 } ( $primary_key ? ( $primary_key ) : real_fields($old->table) )
1085 warn "[debug]$me $statement\n" if $DEBUG > 1;
1086 my $sth = dbh->prepare($statement) or return dbh->errstr;
1089 if ( defined dbdef->table('h_'. $old->table) ) {
1090 my $h_old_statement = $old->_h_statement('replace_old');
1091 warn "[debug]$me $h_old_statement\n" if $DEBUG > 2;
1092 $h_old_sth = dbh->prepare($h_old_statement) or return dbh->errstr;
1098 if ( defined dbdef->table('h_'. $new->table) ) {
1099 my $h_new_statement = $new->_h_statement('replace_new');
1100 warn "[debug]$me $h_new_statement\n" if $DEBUG > 2;
1101 $h_new_sth = dbh->prepare($h_new_statement) or return dbh->errstr;
1106 # For virtual fields we have three cases with different SQL
1107 # statements: add, replace, delete
1111 my (@add_vfields, @rep_vfields, @del_vfields);
1112 my $vfp = $old->vfieldpart_hashref;
1113 foreach(grep { exists($diff{$_}) } $new->virtual_fields) {
1114 if($diff{$_} eq '') {
1116 unless(@del_vfields) {
1117 my $st = "DELETE FROM virtual_field WHERE recnum = ? ".
1118 "AND vfieldpart = ?";
1119 warn "[debug]$me $st\n" if $DEBUG > 2;
1120 $v_del_sth = dbh->prepare($st) or return dbh->errstr;
1122 push @del_vfields, $_;
1123 } elsif($old->getfield($_) eq '') {
1125 unless(@add_vfields) {
1126 my $st = "INSERT INTO virtual_field (value, recnum, vfieldpart) ".
1128 warn "[debug]$me $st\n" if $DEBUG > 2;
1129 $v_add_sth = dbh->prepare($st) or return dbh->errstr;
1131 push @add_vfields, $_;
1134 unless(@rep_vfields) {
1135 my $st = "UPDATE virtual_field SET value = ? ".
1136 "WHERE recnum = ? AND vfieldpart = ?";
1137 warn "[debug]$me $st\n" if $DEBUG > 2;
1138 $v_rep_sth = dbh->prepare($st) or return dbh->errstr;
1140 push @rep_vfields, $_;
1144 local $SIG{HUP} = 'IGNORE';
1145 local $SIG{INT} = 'IGNORE';
1146 local $SIG{QUIT} = 'IGNORE';
1147 local $SIG{TERM} = 'IGNORE';
1148 local $SIG{TSTP} = 'IGNORE';
1149 local $SIG{PIPE} = 'IGNORE';
1151 my $rc = $sth->execute or return $sth->errstr;
1152 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
1153 $h_old_sth->execute or return $h_old_sth->errstr if $h_old_sth;
1154 $h_new_sth->execute or return $h_new_sth->errstr if $h_new_sth;
1156 $v_del_sth->execute($old->getfield($primary_key),
1158 or return $v_del_sth->errstr
1159 foreach(@del_vfields);
1161 $v_add_sth->execute($new->getfield($_),
1162 $old->getfield($primary_key),
1164 or return $v_add_sth->errstr
1165 foreach(@add_vfields);
1167 $v_rep_sth->execute($new->getfield($_),
1168 $old->getfield($primary_key),
1170 or return $v_rep_sth->errstr
1171 foreach(@rep_vfields);
1173 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1175 # Now that it has been saved, reset the encrypted fields so that $new
1176 # can still be used.
1177 foreach my $field (keys %{$saved}) {
1178 $new->setfield($field, $saved->{$field});
1186 my( $self ) = shift;
1187 warn "[$me] replace called with no arguments; autoloading old record\n"
1190 my $primary_key = $self->dbdef_table->primary_key;
1191 if ( $primary_key ) {
1192 $self->by_key( $self->$primary_key() ) #this is what's returned
1193 or croak "can't find ". $self->table. ".$primary_key ".
1194 $self->$primary_key();
1196 croak $self->table. " has no primary key; pass old record as argument";
1203 Depriciated (use replace instead).
1208 cluck "warning: FS::Record::rep deprecated!";
1209 replace @_; #call method in this scope
1214 Checks virtual fields (using check_blocks). Subclasses should still provide
1215 a check method to validate real fields, foreign keys, etc., and call this
1216 method via $self->SUPER::check.
1218 (FIXME: Should this method try to make sure that it I<is> being called from
1219 a subclass's check method, to keep the current semantics as far as possible?)
1224 #confess "FS::Record::check not implemented; supply one in subclass!";
1227 foreach my $field ($self->virtual_fields) {
1228 for ($self->getfield($field)) {
1229 # See notes on check_block in FS::part_virtual_field.
1230 eval $self->pvf($field)->check_block;
1232 #this is bad, probably want to follow the stack backtrace up and see
1234 my $err = "Fatal error checking $field for $self";
1236 return "$err (see log for backtrace): $@";
1239 $self->setfield($field, $_);
1246 my( $self, $action, $time ) = @_;
1251 grep { defined($self->getfield($_)) && $self->getfield($_) ne "" }
1252 real_fields($self->table);
1255 # If we're encrypting then don't ever store the payinfo or CVV2 in the history....
1256 # You can see if it changed by the paymask...
1257 if ($conf->exists('encryption') ) {
1258 @fields = grep $_ ne 'payinfo' && $_ ne 'cvv2', @fields;
1260 my @values = map { _quote( $self->getfield($_), $self->table, $_) } @fields;
1262 "INSERT INTO h_". $self->table. " ( ".
1263 join(', ', qw(history_date history_user history_action), @fields ).
1265 join(', ', $time, dbh->quote(getotaker()), dbh->quote($action), @values).
1272 B<Warning>: External use is B<deprecated>.
1274 Replaces COLUMN in record with a unique number, using counters in the
1275 filesystem. Used by the B<insert> method on single-field unique columns
1276 (see L<DBIx::DBSchema::Table>) and also as a fallback for primary keys
1277 that aren't SERIAL (Pg) or AUTO_INCREMENT (mysql).
1279 Returns the new value.
1284 my($self,$field) = @_;
1285 my($table)=$self->table;
1287 croak "Unique called on field $field, but it is ",
1288 $self->getfield($field),
1290 if $self->getfield($field);
1292 #warn "table $table is tainted" if is_tainted($table);
1293 #warn "field $field is tainted" if is_tainted($field);
1295 my($counter) = new File::CounterFile "$table.$field",0;
1297 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
1299 # my($counter) = new File::CounterFile "$user/$table.$field",0;
1302 my $index = $counter->inc;
1303 $index = $counter->inc while qsearchs($table, { $field=>$index } );
1305 $index =~ /^(\d*)$/;
1308 $self->setfield($field,$index);
1312 =item ut_float COLUMN
1314 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
1315 null. If there is an error, returns the error, otherwise returns false.
1320 my($self,$field)=@_ ;
1321 ($self->getfield($field) =~ /^(\d+\.\d+)$/ ||
1322 $self->getfield($field) =~ /^(\d+)$/ ||
1323 $self->getfield($field) =~ /^(\d+\.\d+e\d+)$/ ||
1324 $self->getfield($field) =~ /^(\d+e\d+)$/)
1325 or return "Illegal or empty (float) $field: ". $self->getfield($field);
1326 $self->setfield($field,$1);
1329 =item ut_floatn COLUMN
1331 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
1332 null. If there is an error, returns the error, otherwise returns false.
1336 #false laziness w/ut_ipn
1338 my( $self, $field ) = @_;
1339 if ( $self->getfield($field) =~ /^()$/ ) {
1340 $self->setfield($field,'');
1343 $self->ut_float($field);
1347 =item ut_sfloat COLUMN
1349 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10.
1350 May not be null. If there is an error, returns the error, otherwise returns
1356 my($self,$field)=@_ ;
1357 ($self->getfield($field) =~ /^(-?\d+\.\d+)$/ ||
1358 $self->getfield($field) =~ /^(-?\d+)$/ ||
1359 $self->getfield($field) =~ /^(-?\d+\.\d+[eE]-?\d+)$/ ||
1360 $self->getfield($field) =~ /^(-?\d+[eE]-?\d+)$/)
1361 or return "Illegal or empty (float) $field: ". $self->getfield($field);
1362 $self->setfield($field,$1);
1365 =item ut_sfloatn COLUMN
1367 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
1368 null. If there is an error, returns the error, otherwise returns false.
1373 my( $self, $field ) = @_;
1374 if ( $self->getfield($field) =~ /^()$/ ) {
1375 $self->setfield($field,'');
1378 $self->ut_sfloat($field);
1382 =item ut_snumber COLUMN
1384 Check/untaint signed numeric data (whole numbers). If there is an error,
1385 returns the error, otherwise returns false.
1390 my($self, $field) = @_;
1391 $self->getfield($field) =~ /^(-?)\s*(\d+)$/
1392 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1393 $self->setfield($field, "$1$2");
1397 =item ut_snumbern COLUMN
1399 Check/untaint signed numeric data (whole numbers). If there is an error,
1400 returns the error, otherwise returns false.
1405 my($self, $field) = @_;
1406 $self->getfield($field) =~ /^(-?)\s*(\d*)$/
1407 or return "Illegal (numeric) $field: ". $self->getfield($field);
1409 return "Illegal (numeric) $field: ". $self->getfield($field)
1412 $self->setfield($field, "$1$2");
1416 =item ut_number COLUMN
1418 Check/untaint simple numeric data (whole numbers). May not be null. If there
1419 is an error, returns the error, otherwise returns false.
1424 my($self,$field)=@_;
1425 $self->getfield($field) =~ /^(\d+)$/
1426 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1427 $self->setfield($field,$1);
1431 =item ut_numbern COLUMN
1433 Check/untaint simple numeric data (whole numbers). May be null. If there is
1434 an error, returns the error, otherwise returns false.
1439 my($self,$field)=@_;
1440 $self->getfield($field) =~ /^(\d*)$/
1441 or return "Illegal (numeric) $field: ". $self->getfield($field);
1442 $self->setfield($field,$1);
1446 =item ut_money COLUMN
1448 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
1449 is an error, returns the error, otherwise returns false.
1454 my($self,$field)=@_;
1455 $self->setfield($field, 0) if $self->getfield($field) eq '';
1456 $self->getfield($field) =~ /^(\-)? ?(\d*)(\.\d{2})?$/
1457 or return "Illegal (money) $field: ". $self->getfield($field);
1458 #$self->setfield($field, "$1$2$3" || 0);
1459 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
1463 =item ut_text COLUMN
1465 Check/untaint text. Alphanumerics, spaces, and the following punctuation
1466 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / = [ ]
1467 May not be null. If there is an error, returns the error, otherwise returns
1473 my($self,$field)=@_;
1474 #warn "msgcat ". \&msgcat. "\n";
1475 #warn "notexist ". \¬exist. "\n";
1476 #warn "AUTOLOAD ". \&AUTOLOAD. "\n";
1477 $self->getfield($field)
1478 =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]+)$/
1479 or return gettext('illegal_or_empty_text'). " $field: ".
1480 $self->getfield($field);
1481 $self->setfield($field,$1);
1485 =item ut_textn COLUMN
1487 Check/untaint text. Alphanumerics, spaces, and the following punctuation
1488 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
1489 May be null. If there is an error, returns the error, otherwise returns false.
1494 my($self,$field)=@_;
1495 $self->getfield($field)
1496 =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]*)$/
1497 or return gettext('illegal_text'). " $field: ". $self->getfield($field);
1498 $self->setfield($field,$1);
1502 =item ut_alpha COLUMN
1504 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
1505 an error, returns the error, otherwise returns false.
1510 my($self,$field)=@_;
1511 $self->getfield($field) =~ /^(\w+)$/
1512 or return "Illegal or empty (alphanumeric) $field: ".
1513 $self->getfield($field);
1514 $self->setfield($field,$1);
1518 =item ut_alpha COLUMN
1520 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
1521 error, returns the error, otherwise returns false.
1526 my($self,$field)=@_;
1527 $self->getfield($field) =~ /^(\w*)$/
1528 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
1529 $self->setfield($field,$1);
1533 =item ut_phonen COLUMN [ COUNTRY ]
1535 Check/untaint phone numbers. May be null. If there is an error, returns
1536 the error, otherwise returns false.
1538 Takes an optional two-letter ISO country code; without it or with unsupported
1539 countries, ut_phonen simply calls ut_alphan.
1544 my( $self, $field, $country ) = @_;
1545 return $self->ut_alphan($field) unless defined $country;
1546 my $phonen = $self->getfield($field);
1547 if ( $phonen eq '' ) {
1548 $self->setfield($field,'');
1549 } elsif ( $country eq 'US' || $country eq 'CA' ) {
1551 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
1552 or return gettext('illegal_phone'). " $field: ". $self->getfield($field);
1553 $phonen = "$1-$2-$3";
1554 $phonen .= " x$4" if $4;
1555 $self->setfield($field,$phonen);
1557 warn "warning: don't know how to check phone numbers for country $country";
1558 return $self->ut_textn($field);
1565 Check/untaint hexadecimal values.
1570 my($self, $field) = @_;
1571 $self->getfield($field) =~ /^([\da-fA-F]+)$/
1572 or return "Illegal (hex) $field: ". $self->getfield($field);
1573 $self->setfield($field, uc($1));
1577 =item ut_hexn COLUMN
1579 Check/untaint hexadecimal values. May be null.
1584 my($self, $field) = @_;
1585 $self->getfield($field) =~ /^([\da-fA-F]*)$/
1586 or return "Illegal (hex) $field: ". $self->getfield($field);
1587 $self->setfield($field, uc($1));
1592 Check/untaint ip addresses. IPv4 only for now.
1597 my( $self, $field ) = @_;
1598 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
1599 or return "Illegal (IP address) $field: ". $self->getfield($field);
1600 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
1601 $self->setfield($field, "$1.$2.$3.$4");
1607 Check/untaint ip addresses. IPv4 only for now. May be null.
1612 my( $self, $field ) = @_;
1613 if ( $self->getfield($field) =~ /^()$/ ) {
1614 $self->setfield($field,'');
1617 $self->ut_ip($field);
1621 =item ut_coord COLUMN [ LOWER [ UPPER ] ]
1623 Check/untaint coordinates.
1624 Accepts the following forms:
1634 The "DDD MM SS" and "DDD MM MMM" are potentially ambiguous.
1635 The latter form (that is, the MMM are thousands of minutes) is
1636 assumed if the "MMM" is exactly three digits or two digits > 59.
1638 To be safe, just use the DDD.DDDDD form.
1640 If LOWER or UPPER are specified, then the coordinate is checked
1641 for lower and upper bounds, respectively.
1647 my ($self, $field) = (shift, shift);
1649 my $lower = shift if scalar(@_);
1650 my $upper = shift if scalar(@_);
1651 my $coord = $self->getfield($field);
1652 my $neg = $coord =~ s/^(-)//;
1654 my ($d, $m, $s) = (0, 0, 0);
1657 (($d) = ($coord =~ /^(\s*\d{1,3}(?:\.\d+)?)\s*$/)) ||
1658 (($d, $m) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2}(?:\.\d+))\s*$/)) ||
1659 (($d, $m, $s) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2})\s+(\d{1,3})\s*$/))
1661 $s = (((($s =~ /^\d{3}$/) or $s > 59) ? ($s / 1000) : ($s / 60)) / 60);
1664 return "Invalid (coordinate with minutes > 59) $field: "
1665 . $self->getfield($field);
1668 $coord = ($neg ? -1 : 1) * sprintf('%.8f', $d + $m + $s);
1670 if (defined($lower) and ($coord < $lower)) {
1671 return "Invalid (coordinate < $lower) $field: "
1672 . $self->getfield($field);;
1675 if (defined($upper) and ($coord > $upper)) {
1676 return "Invalid (coordinate > $upper) $field: "
1677 . $self->getfield($field);;
1680 $self->setfield($field, $coord);
1684 return "Invalid (coordinate) $field: " . $self->getfield($field);
1688 =item ut_coordn COLUMN [ LOWER [ UPPER ] ]
1690 Same as ut_coord, except optionally null.
1696 my ($self, $field) = (shift, shift);
1698 if ($self->getfield($field) =~ /^$/) {
1701 return $self->ut_coord($field, @_);
1707 =item ut_domain COLUMN
1709 Check/untaint host and domain names.
1714 my( $self, $field ) = @_;
1715 #$self->getfield($field) =~/^(\w+\.)*\w+$/
1716 $self->getfield($field) =~/^(([\w\-]+\.)*\w+)$/
1717 or return "Illegal (domain) $field: ". $self->getfield($field);
1718 $self->setfield($field,$1);
1722 =item ut_name COLUMN
1724 Check/untaint proper names; allows alphanumerics, spaces and the following
1725 punctuation: , . - '
1732 my( $self, $field ) = @_;
1733 $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
1734 or return gettext('illegal_name'). " $field: ". $self->getfield($field);
1735 $self->setfield($field,$1);
1741 Check/untaint zip codes.
1745 my @zip_reqd_countries = qw( AU CA US ); #CA, US implicit...
1748 my( $self, $field, $country ) = @_;
1750 if ( $country eq 'US' ) {
1752 $self->getfield($field) =~ /^\s*(\d{5}(\-\d{4})?)\s*$/
1753 or return gettext('illegal_zip'). " $field for country $country: ".
1754 $self->getfield($field);
1755 $self->setfield($field, $1);
1757 } elsif ( $country eq 'CA' ) {
1759 $self->getfield($field) =~ /^\s*([A-Z]\d[A-Z])\s*(\d[A-Z]\d)\s*$/i
1760 or return gettext('illegal_zip'). " $field for country $country: ".
1761 $self->getfield($field);
1762 $self->setfield($field, "$1 $2");
1766 if ( $self->getfield($field) =~ /^\s*$/
1767 && ( !$country || ! grep { $_ eq $country } @zip_reqd_countries )
1770 $self->setfield($field,'');
1772 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
1773 or return gettext('illegal_zip'). " $field: ". $self->getfield($field);
1774 $self->setfield($field,$1);
1782 =item ut_country COLUMN
1784 Check/untaint country codes. Country names are changed to codes, if possible -
1785 see L<Locale::Country>.
1790 my( $self, $field ) = @_;
1791 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
1792 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
1793 && country2code($1) ) {
1794 $self->setfield($field,uc(country2code($1)));
1797 $self->getfield($field) =~ /^(\w\w)$/
1798 or return "Illegal (country) $field: ". $self->getfield($field);
1799 $self->setfield($field,uc($1));
1803 =item ut_anything COLUMN
1805 Untaints arbitrary data. Be careful.
1810 my( $self, $field ) = @_;
1811 $self->getfield($field) =~ /^(.*)$/s
1812 or return "Illegal $field: ". $self->getfield($field);
1813 $self->setfield($field,$1);
1817 =item ut_enum COLUMN CHOICES_ARRAYREF
1819 Check/untaint a column, supplying all possible choices, like the "enum" type.
1824 my( $self, $field, $choices ) = @_;
1825 foreach my $choice ( @$choices ) {
1826 if ( $self->getfield($field) eq $choice ) {
1827 $self->setfield($choice);
1831 return "Illegal (enum) field $field: ". $self->getfield($field);
1834 =item ut_foreign_key COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1836 Check/untaint a foreign column key. Call a regular ut_ method (like ut_number)
1837 on the column first.
1841 sub ut_foreign_key {
1842 my( $self, $field, $table, $foreign ) = @_;
1843 qsearchs($table, { $foreign => $self->getfield($field) })
1844 or return "Can't find ". $self->table. ".$field ". $self->getfield($field).
1845 " in $table.$foreign";
1849 =item ut_foreign_keyn COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1851 Like ut_foreign_key, except the null value is also allowed.
1855 sub ut_foreign_keyn {
1856 my( $self, $field, $table, $foreign ) = @_;
1857 $self->getfield($field)
1858 ? $self->ut_foreign_key($field, $table, $foreign)
1862 =item ut_agentnum_acl
1864 Checks this column as an agentnum, taking into account the current users's
1869 sub ut_agentnum_acl {
1870 my( $self, $field, $null_acl ) = @_;
1872 my $error = $self->ut_foreign_keyn($field, 'agent', 'agentnum');
1873 return "Illegal agentnum: $error" if $error;
1875 my $curuser = $FS::CurrentUser::CurrentUser;
1877 if ( $self->$field() ) {
1879 return "Access deined"
1880 unless $curuser->agentnum($self->$field());
1884 return "Access denied"
1885 unless $curuser->access_right($null_acl);
1893 =item virtual_fields [ TABLE ]
1895 Returns a list of virtual fields defined for the table. This should not
1896 be exported, and should only be called as an instance or class method.
1900 sub virtual_fields {
1903 $table = $self->table or confess "virtual_fields called on non-table";
1905 confess "Unknown table $table" unless dbdef->table($table);
1907 return () unless dbdef->table('part_virtual_field');
1909 unless ( $virtual_fields_cache{$table} ) {
1910 my $query = 'SELECT name from part_virtual_field ' .
1911 "WHERE dbtable = '$table'";
1913 my $result = $dbh->selectcol_arrayref($query);
1914 confess "Error executing virtual fields query: $query: ". $dbh->errstr
1916 $virtual_fields_cache{$table} = $result;
1919 @{$virtual_fields_cache{$table}};
1924 =item fields [ TABLE ]
1926 This is a wrapper for real_fields and virtual_fields. Code that called
1927 fields before should probably continue to call fields.
1932 my $something = shift;
1934 if($something->isa('FS::Record')) {
1935 $table = $something->table;
1937 $table = $something;
1938 $something = "FS::$table";
1940 return (real_fields($table), $something->virtual_fields());
1945 =item pvf FIELD_NAME
1947 Returns the FS::part_virtual_field object corresponding to a field in the
1948 record (specified by FIELD_NAME).
1953 my ($self, $name) = (shift, shift);
1955 if(grep /^$name$/, $self->virtual_fields) {
1956 return qsearchs('part_virtual_field', { dbtable => $self->table,
1966 =item real_fields [ TABLE ]
1968 Returns a list of the real columns in the specified table. Called only by
1969 fields() and other subroutines elsewhere in FS::Record.
1976 my($table_obj) = dbdef->table($table);
1977 confess "Unknown table $table" unless $table_obj;
1978 $table_obj->columns;
1981 =item _quote VALUE, TABLE, COLUMN
1983 This is an internal function used to construct SQL statements. It returns
1984 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
1985 type (see L<DBIx::DBSchema::Column>) does not end in `char' or `binary'.
1990 my($value, $table, $column) = @_;
1991 my $column_obj = dbdef->table($table)->column($column);
1992 my $column_type = $column_obj->type;
1993 my $nullable = $column_obj->null;
1995 warn " $table.$column: $value ($column_type".
1996 ( $nullable ? ' NULL' : ' NOT NULL' ).
1997 ")\n" if $DEBUG > 2;
1999 if ( $value eq '' && $nullable ) {
2001 } elsif ( $value eq '' && $column_type =~ /^(int|numeric)/ ) {
2002 cluck "WARNING: Attempting to set non-null integer $table.$column null; ".
2005 } elsif ( $value =~ /^\d+(\.\d+)?$/ &&
2006 ! $column_type =~ /(char|binary|text)$/i ) {
2013 =item vfieldpart_hashref TABLE
2015 Returns a hashref of virtual field names and vfieldparts applicable to the given
2020 sub vfieldpart_hashref {
2022 my $table = $self->table;
2024 return {} unless dbdef->table('part_virtual_field');
2027 my $statement = "SELECT vfieldpart, name FROM part_virtual_field WHERE ".
2028 "dbtable = '$table'";
2029 my $sth = $dbh->prepare($statement);
2030 $sth->execute or croak "Execution of '$statement' failed: ".$dbh->errstr;
2031 return { map { $_->{name}, $_->{vfieldpart} }
2032 @{$sth->fetchall_arrayref({})} };
2039 This is deprecated. Don't use it.
2041 It returns a hash-type list with the fields of this record's table set true.
2046 carp "warning: hfields is deprecated";
2049 foreach (fields($table)) {
2058 "$_: ". $self->getfield($_). "|"
2059 } (fields($self->table)) );
2062 =item encrypt($value)
2064 Encrypts the credit card using a combination of PK to encrypt and uuencode to armour.
2066 Returns the encrypted string.
2068 You should generally not have to worry about calling this, as the system handles this for you.
2074 my ($self, $value) = @_;
2077 my $conf = new FS::Conf;
2078 if ($conf->exists('encryption')) {
2079 if ($self->is_encrypted($value)) {
2080 # Return the original value if it isn't plaintext.
2081 $encrypted = $value;
2084 if (ref($rsa_encrypt) =~ /::RSA/) { # We Can Encrypt
2085 # RSA doesn't like the empty string so let's pack it up
2086 # The database doesn't like the RSA data so uuencode it
2087 my $length = length($value)+1;
2088 $encrypted = pack("u*",$rsa_encrypt->encrypt(pack("Z$length",$value)));
2090 die ("You can't encrypt w/o a valid RSA engine - Check your installation or disable encryption");
2097 =item is_encrypted($value)
2099 Checks to see if the string is encrypted and returns true or false (1/0) to indicate it's status.
2105 my ($self, $value) = @_;
2106 # Possible Bug - Some work may be required here....
2108 if ($value =~ /^M/ && length($value) > 80) {
2115 =item decrypt($value)
2117 Uses the private key to decrypt the string. Returns the decryoted string or undef on failure.
2119 You should generally not have to worry about calling this, as the system handles this for you.
2124 my ($self,$value) = @_;
2125 my $decrypted = $value; # Will return the original value if it isn't encrypted or can't be decrypted.
2126 my $conf = new FS::Conf;
2127 if ($conf->exists('encryption') && $self->is_encrypted($value)) {
2129 if (ref($rsa_decrypt) =~ /::RSA/) {
2130 my $encrypted = unpack ("u*", $value);
2131 $decrypted = unpack("Z*", eval{$rsa_decrypt->decrypt($encrypted)});
2132 if ($@) {warn "Decryption Failed"};
2140 #Initialize the Module
2141 $rsa_module = 'Crypt::OpenSSL::RSA'; # The Default
2143 my $conf = new FS::Conf;
2144 if ($conf->exists('encryptionmodule') && $conf->config('encryptionmodule') ne '') {
2145 $rsa_module = $conf->config('encryptionmodule');
2149 eval ("require $rsa_module"); # No need to import the namespace
2152 # Initialize Encryption
2153 if ($conf->exists('encryptionpublickey') && $conf->config('encryptionpublickey') ne '') {
2154 my $public_key = join("\n",$conf->config('encryptionpublickey'));
2155 $rsa_encrypt = $rsa_module->new_public_key($public_key);
2158 # Intitalize Decryption
2159 if ($conf->exists('encryptionprivatekey') && $conf->config('encryptionprivatekey') ne '') {
2160 my $private_key = join("\n",$conf->config('encryptionprivatekey'));
2161 $rsa_decrypt = $rsa_module->new_private_key($private_key);
2165 sub DESTROY { return; }
2169 # #use Carp qw(cluck);
2170 # #cluck "DESTROYING $self";
2171 # warn "DESTROYING $self";
2175 # return ! eval { join('',@_), kill 0; 1; };
2182 This module should probably be renamed, since much of the functionality is
2183 of general use. It is not completely unlike Adapter::DBI (see below).
2185 Exported qsearch and qsearchs should be deprecated in favor of method calls
2186 (against an FS::Record object like the old search and searchs that qsearch
2187 and qsearchs were on top of.)
2189 The whole fields / hfields mess should be removed.
2191 The various WHERE clauses should be subroutined.
2193 table string should be deprecated in favor of DBIx::DBSchema::Table.
2195 No doubt we could benefit from a Tied hash. Documenting how exists / defined
2196 true maps to the database (and WHERE clauses) would also help.
2198 The ut_ methods should ask the dbdef for a default length.
2200 ut_sqltype (like ut_varchar) should all be defined
2202 A fallback check method should be provided which uses the dbdef.
2204 The ut_money method assumes money has two decimal digits.
2206 The Pg money kludge in the new method only strips `$'.
2208 The ut_phonen method only checks US-style phone numbers.
2210 The _quote function should probably use ut_float instead of a regex.
2212 All the subroutines probably should be methods, here or elsewhere.
2214 Probably should borrow/use some dbdef methods where appropriate (like sub
2217 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
2218 or allow it to be set. Working around it is ugly any way around - DBI should
2219 be fixed. (only affects RDBMS which return uppercase column names)
2221 ut_zip should take an optional country like ut_phone.
2225 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
2227 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.