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);
9 use Scalar::Util qw( blessed );
10 use File::CounterFile;
12 use DBI qw(:sql_types);
13 use DBIx::DBSchema 0.33;
14 use FS::UID qw(dbh getotaker datasrc driver_name);
16 use FS::Schema qw(dbdef);
18 use FS::Msgcat qw(gettext);
19 #use FS::Conf; #dependency loop bs, in install_callback below instead
21 use FS::part_virtual_field;
27 #export dbdef for now... everything else expects to find it here
28 @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch
29 str2time_sql str2time_sql_closing );
34 $nowarn_identical = 0;
42 FS::UID->install_callback( sub {
46 $File::CounterFile::DEFAULT_DIR = $conf->base_dir . "/counters.". datasrc;
52 FS::Record - Database record objects
57 use FS::Record qw(dbh fields qsearch qsearchs);
59 $record = new FS::Record 'table', \%hash;
60 $record = new FS::Record 'table', { 'column' => 'value', ... };
62 $record = qsearchs FS::Record 'table', \%hash;
63 $record = qsearchs FS::Record 'table', { 'column' => 'value', ... };
64 @records = qsearch FS::Record 'table', \%hash;
65 @records = qsearch FS::Record 'table', { 'column' => 'value', ... };
67 $table = $record->table;
68 $dbdef_table = $record->dbdef_table;
70 $value = $record->get('column');
71 $value = $record->getfield('column');
72 $value = $record->column;
74 $record->set( 'column' => 'value' );
75 $record->setfield( 'column' => 'value' );
76 $record->column('value');
78 %hash = $record->hash;
80 $hashref = $record->hashref;
82 $error = $record->insert;
84 $error = $record->delete;
86 $error = $new_record->replace($old_record);
88 # external use deprecated - handled by the database (at least for Pg, mysql)
89 $value = $record->unique('column');
91 $error = $record->ut_float('column');
92 $error = $record->ut_floatn('column');
93 $error = $record->ut_number('column');
94 $error = $record->ut_numbern('column');
95 $error = $record->ut_snumber('column');
96 $error = $record->ut_snumbern('column');
97 $error = $record->ut_money('column');
98 $error = $record->ut_text('column');
99 $error = $record->ut_textn('column');
100 $error = $record->ut_alpha('column');
101 $error = $record->ut_alphan('column');
102 $error = $record->ut_phonen('column');
103 $error = $record->ut_anything('column');
104 $error = $record->ut_name('column');
106 $quoted_value = _quote($value,'table','field');
109 $fields = hfields('table');
110 if ( $fields->{Field} ) { # etc.
112 @fields = fields 'table'; #as a subroutine
113 @fields = $record->fields; #as a method call
118 (Mostly) object-oriented interface to database records. Records are currently
119 implemented on top of DBI. FS::Record is intended as a base class for
120 table-specific classes to inherit from, i.e. FS::cust_main.
126 =item new [ TABLE, ] HASHREF
128 Creates a new record. It doesn't store it in the database, though. See
129 L<"insert"> for that.
131 Note that the object stores this hash reference, not a distinct copy of the
132 hash it points to. You can ask the object for a copy with the I<hash>
135 TABLE can only be omitted when a dervived class overrides the table method.
141 my $class = ref($proto) || $proto;
143 bless ($self, $class);
145 unless ( defined ( $self->table ) ) {
146 $self->{'Table'} = shift;
147 carp "warning: FS::Record::new called with table name ". $self->{'Table'};
150 $self->{'Hash'} = shift;
152 foreach my $field ( grep !defined($self->{'Hash'}{$_}), $self->fields ) {
153 $self->{'Hash'}{$field}='';
156 $self->_rebless if $self->can('_rebless');
158 $self->{'modified'} = 0;
160 $self->_cache($self->{'Hash'}, shift) if $self->can('_cache') && @_;
167 my $class = ref($proto) || $proto;
169 bless ($self, $class);
171 $self->{'Table'} = shift unless defined ( $self->table );
173 my $hashref = $self->{'Hash'} = shift;
175 if ( defined( $cache->cache->{$hashref->{$cache->key}} ) ) {
176 my $obj = $cache->cache->{$hashref->{$cache->key}};
177 $obj->_cache($hashref, $cache) if $obj->can('_cache');
180 $cache->cache->{$hashref->{$cache->key}} = $self->new($hashref, $cache);
187 my $class = ref($proto) || $proto;
189 bless ($self, $class);
190 if ( defined $self->table ) {
191 cluck "create constructor is deprecated, use new!";
194 croak "FS::Record::create called (not from a subclass)!";
198 =item qsearch PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
200 Searches the database for all records matching (at least) the key/value pairs
201 in HASHREF. Returns all the records found as `FS::TABLE' objects if that
202 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
205 The preferred usage is to pass a hash reference of named parameters:
207 my @records = qsearch( {
208 'table' => 'table_name',
209 'hashref' => { 'field' => 'value'
210 'field' => { 'op' => '<',
215 #these are optional...
217 'extra_sql' => 'AND field ',
218 'order_by' => 'ORDER BY something',
219 #'cache_obj' => '', #optional
220 'addl_from' => 'LEFT JOIN othtable USING ( field )',
225 Much code still uses old-style positional parameters, this is also probably
226 fine in the common case where there are only two parameters:
228 my @records = qsearch( 'table', { 'field' => 'value' } );
230 ###oops, argh, FS::Record::new only lets us create database fields.
231 #Normal behaviour if SELECT is not specified is `*', as in
232 #C<SELECT * FROM table WHERE ...>. However, there is an experimental new
233 #feature where you can specify SELECT - remember, the objects returned,
234 #although blessed into the appropriate `FS::TABLE' package, will only have the
235 #fields you specify. This might have unwanted results if you then go calling
236 #regular FS::TABLE methods
242 my($stable, $record, $select, $extra_sql, $order_by, $cache, $addl_from );
244 if ( ref($_[0]) ) { #hashref for now, eventually maybe accept a list too
246 $stable = $opt->{'table'} or die "table name is required";
247 $record = $opt->{'hashref'} || {};
248 $select = $opt->{'select'} || '*';
249 $extra_sql = $opt->{'extra_sql'} || '';
250 $order_by = $opt->{'order_by'} || '';
251 $cache = $opt->{'cache_obj'} || '';
252 $addl_from = $opt->{'addl_from'} || '';
253 $debug = $opt->{'debug'} || '';
255 ($stable, $record, $select, $extra_sql, $cache, $addl_from ) = @_;
259 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
261 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
265 my $table = $cache ? $cache->table : $stable;
266 my $dbdef_table = dbdef->table($table)
267 or die "No schema for table $table found - ".
268 "do you need to run freeside-upgrade?";
269 my $pkey = $dbdef_table->primary_key;
271 my @real_fields = grep exists($record->{$_}), real_fields($table);
273 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
274 @virtual_fields = grep exists($record->{$_}), "FS::$table"->virtual_fields;
276 cluck "warning: FS::$table not loaded; virtual fields not searchable";
277 @virtual_fields = ();
280 my $statement = "SELECT $select FROM $stable";
281 $statement .= " $addl_from" if $addl_from;
282 if ( @real_fields or @virtual_fields ) {
283 $statement .= ' WHERE '. join(' AND ',
284 get_real_fields($table, $record, \@real_fields) ,
285 get_virtual_fields($table, $pkey, $record, \@virtual_fields),
289 $statement .= " $extra_sql" if defined($extra_sql);
290 $statement .= " $order_by" if defined($order_by);
292 warn "[debug]$me $statement\n" if $DEBUG > 1 || $debug;
293 my $sth = $dbh->prepare($statement)
294 or croak "$dbh->errstr doing $statement";
299 grep defined( $record->{$_} ) && $record->{$_} ne '', @real_fields
301 my $value = $record->{$field};
302 my $type = dbdef->table($table)->column($field)->type;
303 if ( $type =~ /(int|(big)?serial)/i && $value =~ /^\d+(\.\d+)?$/ ) {
304 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_INTEGER } );
305 } elsif ( ( $type =~ /(numeric)/i && $value =~ /^[+-]?\d+(\.\d+)?$/)
306 || ( $type =~ /(real|float4)/i
307 && $value =~ /[-+]?\d*\.?\d+([eE][-+]?\d+)?/
310 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_FLOAT } );
312 $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_VARCHAR } );
316 # $sth->execute( map $record->{$_},
317 # grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
318 # ) or croak "Error executing \"$statement\": ". $sth->errstr;
320 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
322 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
323 @virtual_fields = "FS::$table"->virtual_fields;
325 cluck "warning: FS::$table not loaded; virtual fields not returned either";
326 @virtual_fields = ();
330 tie %result, "Tie::IxHash";
331 my @stuff = @{ $sth->fetchall_arrayref( {} ) };
332 if ( $pkey && scalar(@stuff) && $stuff[0]->{$pkey} ) {
333 %result = map { $_->{$pkey}, $_ } @stuff;
335 @result{@stuff} = @stuff;
340 if ( keys(%result) and @virtual_fields ) {
342 "SELECT virtual_field.recnum, part_virtual_field.name, ".
343 "virtual_field.value ".
344 "FROM part_virtual_field JOIN virtual_field USING (vfieldpart) ".
345 "WHERE part_virtual_field.dbtable = '$table' AND ".
346 "virtual_field.recnum IN (".
347 join(',', keys(%result)). ") AND part_virtual_field.name IN ('".
348 join(q!', '!, @virtual_fields) . "')";
349 warn "[debug]$me $statement\n" if $DEBUG > 1;
350 $sth = $dbh->prepare($statement) or croak "$dbh->errstr doing $statement";
351 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
353 foreach (@{ $sth->fetchall_arrayref({}) }) {
354 my $recnum = $_->{recnum};
355 my $name = $_->{name};
356 my $value = $_->{value};
357 if (exists($result{$recnum})) {
358 $result{$recnum}->{$name} = $value;
363 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
364 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
365 #derivied class didn't override new method, so this optimization is safe
368 new_or_cached( "FS::$table", { %{$_} }, $cache )
372 new( "FS::$table", { %{$_} } )
376 #okay, its been tested
377 # warn "untested code (class FS::$table uses custom new method)";
379 eval 'FS::'. $table. '->new( { %{$_} } )';
383 # Check for encrypted fields and decrypt them.
384 ## only in the local copy, not the cached object
385 if ( $conf && $conf->exists('encryption') # $conf doesn't exist when doing
386 # the initial search for
388 && eval 'defined(@FS::'. $table . '::encrypted_fields)') {
389 foreach my $record (@return) {
390 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
391 # Set it directly... This may cause a problem in the future...
392 $record->setfield($field, $record->decrypt($record->getfield($field)));
397 cluck "warning: FS::$table not loaded; returning FS::Record objects";
399 FS::Record->new( $table, { %{$_} } );
405 ## makes this easier to read
407 sub get_virtual_fields {
411 my $virtual_fields = shift;
417 if ( ref($record->{$_}) ) {
418 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
419 if ( uc($op) eq 'ILIKE' ) {
421 $record->{$_}{'value'} = lc($record->{$_}{'value'});
422 $column = "LOWER($_)";
424 $record->{$_} = $record->{$_}{'value'};
427 # ... EXISTS ( SELECT name, value FROM part_virtual_field
429 # ON part_virtual_field.vfieldpart = virtual_field.vfieldpart
430 # WHERE recnum = svc_acct.svcnum
431 # AND (name, value) = ('egad', 'brain') )
433 my $value = $record->{$_};
437 $subq = ($value ? 'EXISTS ' : 'NOT EXISTS ') .
438 "( SELECT part_virtual_field.name, virtual_field.value ".
439 "FROM part_virtual_field JOIN virtual_field ".
440 "ON part_virtual_field.vfieldpart = virtual_field.vfieldpart ".
441 "WHERE virtual_field.recnum = ${table}.${pkey} ".
442 "AND part_virtual_field.name = '${column}'".
444 " AND virtual_field.value ${op} '${value}'"
448 } @{ $virtual_fields } ) ;
451 sub get_real_fields {
454 my $real_fields = shift;
456 ## this huge map was previously inline, just broke it out to help read the qsearch method, should be optimized for readability
462 if ( ref($record->{$_}) ) {
463 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
464 #$op = 'LIKE' if $op =~ /^ILIKE$/i && driver_name ne 'Pg';
465 if ( uc($op) eq 'ILIKE' ) {
467 $record->{$_}{'value'} = lc($record->{$_}{'value'});
468 $column = "LOWER($_)";
470 $record->{$_} = $record->{$_}{'value'}
473 if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) {
475 if ( driver_name eq 'Pg' ) {
476 my $type = dbdef->table($table)->column($column)->type;
477 if ( $type =~ /(int|(big)?serial)/i ) {
478 qq-( $column IS NULL )-;
480 qq-( $column IS NULL OR $column = '' )-;
483 qq-( $column IS NULL OR $column = "" )-;
485 } elsif ( $op eq '!=' ) {
486 if ( driver_name eq 'Pg' ) {
487 my $type = dbdef->table($table)->column($column)->type;
488 if ( $type =~ /(int|(big)?serial)/i ) {
489 qq-( $column IS NOT NULL )-;
491 qq-( $column IS NOT NULL AND $column != '' )-;
494 qq-( $column IS NOT NULL AND $column != "" )-;
497 if ( driver_name eq 'Pg' ) {
498 qq-( $column $op '' )-;
500 qq-( $column $op "" )-;
506 } @{ $real_fields } );
509 =item by_key PRIMARY_KEY_VALUE
511 This is a class method that returns the record with the given primary key
512 value. This method is only useful in FS::Record subclasses. For example:
514 my $cust_main = FS::cust_main->by_key(1); # retrieve customer with custnum 1
518 my $cust_main = qsearchs('cust_main', { 'custnum' => 1 } );
523 my ($class, $pkey_value) = @_;
525 my $table = $class->table
526 or croak "No table for $class found";
528 my $dbdef_table = dbdef->table($table)
529 or die "No schema for table $table found - ".
530 "do you need to create it or run dbdef-create?";
531 my $pkey = $dbdef_table->primary_key
532 or die "No primary key for table $table";
534 return qsearchs($table, { $pkey => $pkey_value });
537 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
539 Experimental JOINed search method. Using this method, you can execute a
540 single SELECT spanning multiple tables, and cache the results for subsequent
541 method calls. Interface will almost definately change in an incompatible
549 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
550 my $cache = FS::SearchCache->new( $ptable, $pkey );
553 grep { !$saw{$_->getfield($pkey)}++ }
554 qsearch($table, $record, $select, $extra_sql, $cache )
558 =item qsearchs PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
560 Same as qsearch, except that if more than one record matches, it B<carp>s but
561 returns the first. If this happens, you either made a logic error in asking
562 for a single item, or your data is corrupted.
566 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
568 my(@result) = qsearch(@_);
569 cluck "warning: Multiple records in scalar search ($table)"
570 if scalar(@result) > 1;
571 #should warn more vehemently if the search was on a primary key?
572 scalar(@result) ? ($result[0]) : ();
583 Returns the table name.
588 # cluck "warning: FS::Record::table deprecated; supply one in subclass!";
595 Returns the DBIx::DBSchema::Table object for the table.
601 my($table)=$self->table;
602 dbdef->table($table);
607 Returns the primary key for the table.
613 my $pkey = $self->dbdef_table->primary_key;
616 =item get, getfield COLUMN
618 Returns the value of the column/field/key COLUMN.
623 my($self,$field) = @_;
624 # to avoid "Use of unitialized value" errors
625 if ( defined ( $self->{Hash}->{$field} ) ) {
626 $self->{Hash}->{$field};
636 =item set, setfield COLUMN, VALUE
638 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
643 my($self,$field,$value) = @_;
644 $self->{'modified'} = 1;
645 $self->{'Hash'}->{$field} = $value;
652 =item AUTLOADED METHODS
654 $record->column is a synonym for $record->get('column');
656 $record->column('value') is a synonym for $record->set('column','value');
663 my($field)=$AUTOLOAD;
665 if ( defined($value) ) {
666 confess "errant AUTOLOAD $field for $self (arg $value)"
667 unless blessed($self) && $self->can('setfield');
668 $self->setfield($field,$value);
670 confess "errant AUTOLOAD $field for $self (no args)"
671 unless blessed($self) && $self->can('getfield');
672 $self->getfield($field);
678 # my $field = $AUTOLOAD;
680 # if ( defined($_[1]) ) {
681 # $_[0]->setfield($field, $_[1]);
683 # $_[0]->getfield($field);
689 Returns a list of the column/value pairs, usually for assigning to a new hash.
691 To make a distinct duplicate of an FS::Record object, you can do:
693 $new = new FS::Record ( $old->table, { $old->hash } );
699 confess $self. ' -> hash: Hash attribute is undefined'
700 unless defined($self->{'Hash'});
701 %{ $self->{'Hash'} };
706 Returns a reference to the column/value hash. This may be deprecated in the
707 future; if there's a reason you can't just use the autoloaded or get/set
719 Returns true if any of this object's values have been modified with set (or via
720 an autoloaded method). Doesn't yet recognize when you retreive a hashref and
730 =item select_for_update
732 Selects this record with the SQL "FOR UPDATE" command. This can be useful as
737 sub select_for_update {
739 my $primary_key = $self->primary_key;
742 'table' => $self->table,
743 'hashref' => { $primary_key => $self->$primary_key() },
744 'extra_sql' => 'FOR UPDATE',
750 Inserts this record to the database. If there is an error, returns the error,
751 otherwise returns false.
759 warn "$self -> insert" if $DEBUG;
761 my $error = $self->check;
762 return $error if $error;
764 #single-field unique keys are given a value if false
765 #(like MySQL's AUTO_INCREMENT or Pg SERIAL)
766 foreach ( $self->dbdef_table->unique_singles) {
767 $self->unique($_) unless $self->getfield($_);
770 #and also the primary key, if the database isn't going to
771 my $primary_key = $self->dbdef_table->primary_key;
773 if ( $primary_key ) {
774 my $col = $self->dbdef_table->column($primary_key);
777 uc($col->type) =~ /^(BIG)?SERIAL\d?/
778 || ( driver_name eq 'Pg'
779 && defined($col->default)
780 && $col->default =~ /^nextval\(/i
782 || ( driver_name eq 'mysql'
783 && defined($col->local)
784 && $col->local =~ /AUTO_INCREMENT/i
786 $self->unique($primary_key) unless $self->getfield($primary_key) || $db_seq;
789 my $table = $self->table;
792 # Encrypt before the database
793 if ($conf->exists('encryption') && defined(eval '@FS::'. $table . '::encrypted_fields')) {
794 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
795 $self->{'saved'} = $self->getfield($field);
796 $self->setfield($field, $self->encrypt($self->getfield($field)));
801 #false laziness w/delete
803 grep { defined($self->getfield($_)) && $self->getfield($_) ne "" }
806 my @values = map { _quote( $self->getfield($_), $table, $_) } @real_fields;
809 my $statement = "INSERT INTO $table ";
810 if ( @real_fields ) {
813 join( ', ', @real_fields ).
815 join( ', ', @values ).
819 $statement .= 'DEFAULT VALUES';
821 warn "[debug]$me $statement\n" if $DEBUG > 1;
822 my $sth = dbh->prepare($statement) or return dbh->errstr;
824 local $SIG{HUP} = 'IGNORE';
825 local $SIG{INT} = 'IGNORE';
826 local $SIG{QUIT} = 'IGNORE';
827 local $SIG{TERM} = 'IGNORE';
828 local $SIG{TSTP} = 'IGNORE';
829 local $SIG{PIPE} = 'IGNORE';
831 $sth->execute or return $sth->errstr;
833 # get inserted id from the database, if applicable & needed
834 if ( $db_seq && ! $self->getfield($primary_key) ) {
835 warn "[debug]$me retreiving sequence from database\n" if $DEBUG;
839 if ( driver_name eq 'Pg' ) {
841 #my $oid = $sth->{'pg_oid_status'};
842 #my $i_sql = "SELECT $primary_key FROM $table WHERE oid = ?";
844 my $default = $self->dbdef_table->column($primary_key)->default;
845 unless ( $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i ) {
846 dbh->rollback if $FS::UID::AutoCommit;
847 return "can't parse $table.$primary_key default value".
848 " for sequence name: $default";
852 my $i_sql = "SELECT currval('$sequence')";
853 my $i_sth = dbh->prepare($i_sql) or do {
854 dbh->rollback if $FS::UID::AutoCommit;
857 $i_sth->execute() or do { #$i_sth->execute($oid)
858 dbh->rollback if $FS::UID::AutoCommit;
859 return $i_sth->errstr;
861 $insertid = $i_sth->fetchrow_arrayref->[0];
863 } elsif ( driver_name eq 'mysql' ) {
865 $insertid = dbh->{'mysql_insertid'};
866 # work around mysql_insertid being null some of the time, ala RT :/
867 unless ( $insertid ) {
868 warn "WARNING: DBD::mysql didn't return mysql_insertid; ".
869 "using SELECT LAST_INSERT_ID();";
870 my $i_sql = "SELECT LAST_INSERT_ID()";
871 my $i_sth = dbh->prepare($i_sql) or do {
872 dbh->rollback if $FS::UID::AutoCommit;
875 $i_sth->execute or do {
876 dbh->rollback if $FS::UID::AutoCommit;
877 return $i_sth->errstr;
879 $insertid = $i_sth->fetchrow_arrayref->[0];
884 dbh->rollback if $FS::UID::AutoCommit;
885 return "don't know how to retreive inserted ids from ". driver_name.
886 ", try using counterfiles (maybe run dbdef-create?)";
890 $self->setfield($primary_key, $insertid);
895 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
896 $self->virtual_fields;
897 if (@virtual_fields) {
898 my %v_values = map { $_, $self->getfield($_) } @virtual_fields;
900 my $vfieldpart = $self->vfieldpart_hashref;
902 my $v_statement = "INSERT INTO virtual_field(recnum, vfieldpart, value) ".
905 my $v_sth = dbh->prepare($v_statement) or do {
906 dbh->rollback if $FS::UID::AutoCommit;
910 foreach (keys(%v_values)) {
911 $v_sth->execute($self->getfield($primary_key),
915 dbh->rollback if $FS::UID::AutoCommit;
916 return $v_sth->errstr;
923 if ( defined dbdef->table('h_'. $table) ) {
924 my $h_statement = $self->_h_statement('insert');
925 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
926 $h_sth = dbh->prepare($h_statement) or do {
927 dbh->rollback if $FS::UID::AutoCommit;
933 $h_sth->execute or return $h_sth->errstr if $h_sth;
935 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
937 # Now that it has been saved, reset the encrypted fields so that $new
939 foreach my $field (keys %{$saved}) {
940 $self->setfield($field, $saved->{$field});
948 Depriciated (use insert instead).
953 cluck "warning: FS::Record::add deprecated!";
954 insert @_; #call method in this scope
959 Delete this record from the database. If there is an error, returns the error,
960 otherwise returns false.
967 my $statement = "DELETE FROM ". $self->table. " WHERE ". join(' AND ',
969 $self->getfield($_) eq ''
970 #? "( $_ IS NULL OR $_ = \"\" )"
971 ? ( driver_name eq 'Pg'
973 : "( $_ IS NULL OR $_ = \"\" )"
975 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
976 } ( $self->dbdef_table->primary_key )
977 ? ( $self->dbdef_table->primary_key)
978 : real_fields($self->table)
980 warn "[debug]$me $statement\n" if $DEBUG > 1;
981 my $sth = dbh->prepare($statement) or return dbh->errstr;
984 if ( defined dbdef->table('h_'. $self->table) ) {
985 my $h_statement = $self->_h_statement('delete');
986 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
987 $h_sth = dbh->prepare($h_statement) or return dbh->errstr;
992 my $primary_key = $self->dbdef_table->primary_key;
995 my $vfp = $self->vfieldpart_hashref;
996 foreach($self->virtual_fields) {
997 next if $self->getfield($_) eq '';
998 unless(@del_vfields) {
999 my $st = "DELETE FROM virtual_field WHERE recnum = ? AND vfieldpart = ?";
1000 $v_sth = dbh->prepare($st) or return dbh->errstr;
1002 push @del_vfields, $_;
1005 local $SIG{HUP} = 'IGNORE';
1006 local $SIG{INT} = 'IGNORE';
1007 local $SIG{QUIT} = 'IGNORE';
1008 local $SIG{TERM} = 'IGNORE';
1009 local $SIG{TSTP} = 'IGNORE';
1010 local $SIG{PIPE} = 'IGNORE';
1012 my $rc = $sth->execute or return $sth->errstr;
1013 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
1014 $h_sth->execute or return $h_sth->errstr if $h_sth;
1015 $v_sth->execute($self->getfield($primary_key), $vfp->{$_})
1016 or return $v_sth->errstr
1017 foreach (@del_vfields);
1019 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1021 #no need to needlessly destoy the data either (causes problems actually)
1022 #undef $self; #no need to keep object!
1029 Depriciated (use delete instead).
1034 cluck "warning: FS::Record::del deprecated!";
1035 &delete(@_); #call method in this scope
1038 =item replace OLD_RECORD
1040 Replace the OLD_RECORD with this one in the database. If there is an error,
1041 returns the error, otherwise returns false.
1046 my ($new, $old) = (shift, shift);
1048 $old = $new->replace_old unless defined($old);
1050 warn "[debug]$me $new ->replace $old\n" if $DEBUG;
1052 if ( $new->can('replace_check') ) {
1053 my $error = $new->replace_check($old);
1054 return $error if $error;
1057 return "Records not in same table!" unless $new->table eq $old->table;
1059 my $primary_key = $old->dbdef_table->primary_key;
1060 return "Can't change primary key $primary_key ".
1061 'from '. $old->getfield($primary_key).
1062 ' to ' . $new->getfield($primary_key)
1064 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
1066 my $error = $new->check;
1067 return $error if $error;
1069 # Encrypt for replace
1071 if ($conf->exists('encryption') && defined(eval '@FS::'. $new->table . '::encrypted_fields')) {
1072 foreach my $field (eval '@FS::'. $new->table . '::encrypted_fields') {
1073 $saved->{$field} = $new->getfield($field);
1074 $new->setfield($field, $new->encrypt($new->getfield($field)));
1078 #my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
1079 my %diff = map { ($new->getfield($_) ne $old->getfield($_))
1080 ? ($_, $new->getfield($_)) : () } $old->fields;
1082 unless (keys(%diff) || $no_update_diff ) {
1083 carp "[warning]$me $new -> replace $old: records identical"
1084 unless $nowarn_identical;
1088 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
1090 "$_ = ". _quote($new->getfield($_),$old->table,$_)
1091 } real_fields($old->table)
1096 if ( $old->getfield($_) eq '' ) {
1098 #false laziness w/qsearch
1099 if ( driver_name eq 'Pg' ) {
1100 my $type = $old->dbdef_table->column($_)->type;
1101 if ( $type =~ /(int|(big)?serial)/i ) {
1104 qq-( $_ IS NULL OR $_ = '' )-;
1107 qq-( $_ IS NULL OR $_ = "" )-;
1111 "$_ = ". _quote($old->getfield($_),$old->table,$_);
1114 } ( $primary_key ? ( $primary_key ) : real_fields($old->table) )
1117 warn "[debug]$me $statement\n" if $DEBUG > 1;
1118 my $sth = dbh->prepare($statement) or return dbh->errstr;
1121 if ( defined dbdef->table('h_'. $old->table) ) {
1122 my $h_old_statement = $old->_h_statement('replace_old');
1123 warn "[debug]$me $h_old_statement\n" if $DEBUG > 2;
1124 $h_old_sth = dbh->prepare($h_old_statement) or return dbh->errstr;
1130 if ( defined dbdef->table('h_'. $new->table) ) {
1131 my $h_new_statement = $new->_h_statement('replace_new');
1132 warn "[debug]$me $h_new_statement\n" if $DEBUG > 2;
1133 $h_new_sth = dbh->prepare($h_new_statement) or return dbh->errstr;
1138 # For virtual fields we have three cases with different SQL
1139 # statements: add, replace, delete
1143 my (@add_vfields, @rep_vfields, @del_vfields);
1144 my $vfp = $old->vfieldpart_hashref;
1145 foreach(grep { exists($diff{$_}) } $new->virtual_fields) {
1146 if($diff{$_} eq '') {
1148 unless(@del_vfields) {
1149 my $st = "DELETE FROM virtual_field WHERE recnum = ? ".
1150 "AND vfieldpart = ?";
1151 warn "[debug]$me $st\n" if $DEBUG > 2;
1152 $v_del_sth = dbh->prepare($st) or return dbh->errstr;
1154 push @del_vfields, $_;
1155 } elsif($old->getfield($_) eq '') {
1157 unless(@add_vfields) {
1158 my $st = "INSERT INTO virtual_field (value, recnum, vfieldpart) ".
1160 warn "[debug]$me $st\n" if $DEBUG > 2;
1161 $v_add_sth = dbh->prepare($st) or return dbh->errstr;
1163 push @add_vfields, $_;
1166 unless(@rep_vfields) {
1167 my $st = "UPDATE virtual_field SET value = ? ".
1168 "WHERE recnum = ? AND vfieldpart = ?";
1169 warn "[debug]$me $st\n" if $DEBUG > 2;
1170 $v_rep_sth = dbh->prepare($st) or return dbh->errstr;
1172 push @rep_vfields, $_;
1176 local $SIG{HUP} = 'IGNORE';
1177 local $SIG{INT} = 'IGNORE';
1178 local $SIG{QUIT} = 'IGNORE';
1179 local $SIG{TERM} = 'IGNORE';
1180 local $SIG{TSTP} = 'IGNORE';
1181 local $SIG{PIPE} = 'IGNORE';
1183 my $rc = $sth->execute or return $sth->errstr;
1184 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
1185 $h_old_sth->execute or return $h_old_sth->errstr if $h_old_sth;
1186 $h_new_sth->execute or return $h_new_sth->errstr if $h_new_sth;
1188 $v_del_sth->execute($old->getfield($primary_key),
1190 or return $v_del_sth->errstr
1191 foreach(@del_vfields);
1193 $v_add_sth->execute($new->getfield($_),
1194 $old->getfield($primary_key),
1196 or return $v_add_sth->errstr
1197 foreach(@add_vfields);
1199 $v_rep_sth->execute($new->getfield($_),
1200 $old->getfield($primary_key),
1202 or return $v_rep_sth->errstr
1203 foreach(@rep_vfields);
1205 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1207 # Now that it has been saved, reset the encrypted fields so that $new
1208 # can still be used.
1209 foreach my $field (keys %{$saved}) {
1210 $new->setfield($field, $saved->{$field});
1218 my( $self ) = shift;
1219 warn "[$me] replace called with no arguments; autoloading old record\n"
1222 my $primary_key = $self->dbdef_table->primary_key;
1223 if ( $primary_key ) {
1224 $self->by_key( $self->$primary_key() ) #this is what's returned
1225 or croak "can't find ". $self->table. ".$primary_key ".
1226 $self->$primary_key();
1228 croak $self->table. " has no primary key; pass old record as argument";
1235 Depriciated (use replace instead).
1240 cluck "warning: FS::Record::rep deprecated!";
1241 replace @_; #call method in this scope
1246 Checks virtual fields (using check_blocks). Subclasses should still provide
1247 a check method to validate real fields, foreign keys, etc., and call this
1248 method via $self->SUPER::check.
1250 (FIXME: Should this method try to make sure that it I<is> being called from
1251 a subclass's check method, to keep the current semantics as far as possible?)
1256 #confess "FS::Record::check not implemented; supply one in subclass!";
1259 foreach my $field ($self->virtual_fields) {
1260 for ($self->getfield($field)) {
1261 # See notes on check_block in FS::part_virtual_field.
1262 eval $self->pvf($field)->check_block;
1264 #this is bad, probably want to follow the stack backtrace up and see
1266 my $err = "Fatal error checking $field for $self";
1268 return "$err (see log for backtrace): $@";
1271 $self->setfield($field, $_);
1278 my( $self, $action, $time ) = @_;
1283 grep { defined($self->getfield($_)) && $self->getfield($_) ne "" }
1284 real_fields($self->table);
1287 # If we're encrypting then don't ever store the payinfo or CVV2 in the history....
1288 # You can see if it changed by the paymask...
1289 if ($conf->exists('encryption') ) {
1290 @fields = grep $_ ne 'payinfo' && $_ ne 'cvv2', @fields;
1292 my @values = map { _quote( $self->getfield($_), $self->table, $_) } @fields;
1294 "INSERT INTO h_". $self->table. " ( ".
1295 join(', ', qw(history_date history_user history_action), @fields ).
1297 join(', ', $time, dbh->quote(getotaker()), dbh->quote($action), @values).
1304 B<Warning>: External use is B<deprecated>.
1306 Replaces COLUMN in record with a unique number, using counters in the
1307 filesystem. Used by the B<insert> method on single-field unique columns
1308 (see L<DBIx::DBSchema::Table>) and also as a fallback for primary keys
1309 that aren't SERIAL (Pg) or AUTO_INCREMENT (mysql).
1311 Returns the new value.
1316 my($self,$field) = @_;
1317 my($table)=$self->table;
1319 croak "Unique called on field $field, but it is ",
1320 $self->getfield($field),
1322 if $self->getfield($field);
1324 #warn "table $table is tainted" if is_tainted($table);
1325 #warn "field $field is tainted" if is_tainted($field);
1327 my($counter) = new File::CounterFile "$table.$field",0;
1329 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
1331 # my($counter) = new File::CounterFile "$user/$table.$field",0;
1334 my $index = $counter->inc;
1335 $index = $counter->inc while qsearchs($table, { $field=>$index } );
1337 $index =~ /^(\d*)$/;
1340 $self->setfield($field,$index);
1344 =item ut_float COLUMN
1346 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
1347 null. If there is an error, returns the error, otherwise returns false.
1352 my($self,$field)=@_ ;
1353 ($self->getfield($field) =~ /^\s*(\d+\.\d+)\s*$/ ||
1354 $self->getfield($field) =~ /^\s*(\d+)\s*$/ ||
1355 $self->getfield($field) =~ /^\s*(\d+\.\d+e\d+)\s*$/ ||
1356 $self->getfield($field) =~ /^\s*(\d+e\d+)\s*$/)
1357 or return "Illegal or empty (float) $field: ". $self->getfield($field);
1358 $self->setfield($field,$1);
1361 =item ut_floatn COLUMN
1363 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
1364 null. If there is an error, returns the error, otherwise returns false.
1368 #false laziness w/ut_ipn
1370 my( $self, $field ) = @_;
1371 if ( $self->getfield($field) =~ /^()$/ ) {
1372 $self->setfield($field,'');
1375 $self->ut_float($field);
1379 =item ut_sfloat COLUMN
1381 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10.
1382 May not be null. If there is an error, returns the error, otherwise returns
1388 my($self,$field)=@_ ;
1389 ($self->getfield($field) =~ /^\s*(-?\d+\.\d+)\s*$/ ||
1390 $self->getfield($field) =~ /^\s*(-?\d+)\s*$/ ||
1391 $self->getfield($field) =~ /^\s*(-?\d+\.\d+[eE]-?\d+)\s*$/ ||
1392 $self->getfield($field) =~ /^\s*(-?\d+[eE]-?\d+)\s*$/)
1393 or return "Illegal or empty (float) $field: ". $self->getfield($field);
1394 $self->setfield($field,$1);
1397 =item ut_sfloatn COLUMN
1399 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
1400 null. If there is an error, returns the error, otherwise returns false.
1405 my( $self, $field ) = @_;
1406 if ( $self->getfield($field) =~ /^()$/ ) {
1407 $self->setfield($field,'');
1410 $self->ut_sfloat($field);
1414 =item ut_snumber COLUMN
1416 Check/untaint signed numeric data (whole numbers). If there is an error,
1417 returns the error, otherwise returns false.
1422 my($self, $field) = @_;
1423 $self->getfield($field) =~ /^\s*(-?)\s*(\d+)\s*$/
1424 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1425 $self->setfield($field, "$1$2");
1429 =item ut_snumbern COLUMN
1431 Check/untaint signed numeric data (whole numbers). If there is an error,
1432 returns the error, otherwise returns false.
1437 my($self, $field) = @_;
1438 $self->getfield($field) =~ /^\s*(-?)\s*(\d*)\s*$/
1439 or return "Illegal (numeric) $field: ". $self->getfield($field);
1441 return "Illegal (numeric) $field: ". $self->getfield($field)
1444 $self->setfield($field, "$1$2");
1448 =item ut_number COLUMN
1450 Check/untaint simple numeric data (whole numbers). May not be null. If there
1451 is an error, returns the error, otherwise returns false.
1456 my($self,$field)=@_;
1457 $self->getfield($field) =~ /^\s*(\d+)\s*$/
1458 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
1459 $self->setfield($field,$1);
1463 =item ut_numbern COLUMN
1465 Check/untaint simple numeric data (whole numbers). May be null. If there is
1466 an error, returns the error, otherwise returns false.
1471 my($self,$field)=@_;
1472 $self->getfield($field) =~ /^\s*(\d*)\s*$/
1473 or return "Illegal (numeric) $field: ". $self->getfield($field);
1474 $self->setfield($field,$1);
1478 =item ut_money COLUMN
1480 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
1481 is an error, returns the error, otherwise returns false.
1486 my($self,$field)=@_;
1487 $self->setfield($field, 0) if $self->getfield($field) eq '';
1488 $self->getfield($field) =~ /^\s*(\-)?\s*(\d*)(\.\d{2})?\s*$/
1489 or return "Illegal (money) $field: ". $self->getfield($field);
1490 #$self->setfield($field, "$1$2$3" || 0);
1491 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
1495 =item ut_text COLUMN
1497 Check/untaint text. Alphanumerics, spaces, and the following punctuation
1498 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / = [ ]
1499 May not be null. If there is an error, returns the error, otherwise returns
1505 my($self,$field)=@_;
1506 #warn "msgcat ". \&msgcat. "\n";
1507 #warn "notexist ". \¬exist. "\n";
1508 #warn "AUTOLOAD ". \&AUTOLOAD. "\n";
1509 $self->getfield($field)
1510 =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]+)$/
1511 or return gettext('illegal_or_empty_text'). " $field: ".
1512 $self->getfield($field);
1513 $self->setfield($field,$1);
1517 =item ut_textn COLUMN
1519 Check/untaint text. Alphanumerics, spaces, and the following punctuation
1520 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
1521 May be null. If there is an error, returns the error, otherwise returns false.
1526 my($self,$field)=@_;
1527 $self->getfield($field)
1528 =~ /^([\w \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]]*)$/
1529 or return gettext('illegal_text'). " $field: ". $self->getfield($field);
1530 $self->setfield($field,$1);
1534 =item ut_alpha COLUMN
1536 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
1537 an error, returns the error, otherwise returns false.
1542 my($self,$field)=@_;
1543 $self->getfield($field) =~ /^(\w+)$/
1544 or return "Illegal or empty (alphanumeric) $field: ".
1545 $self->getfield($field);
1546 $self->setfield($field,$1);
1550 =item ut_alpha COLUMN
1552 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
1553 error, returns the error, otherwise returns false.
1558 my($self,$field)=@_;
1559 $self->getfield($field) =~ /^(\w*)$/
1560 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
1561 $self->setfield($field,$1);
1565 =item ut_alpha_lower COLUMN
1567 Check/untaint lowercase alphanumeric strings (no spaces). May not be null. If
1568 there is an error, returns the error, otherwise returns false.
1572 sub ut_alpha_lower {
1573 my($self,$field)=@_;
1574 $self->getfield($field) =~ /[[:upper:]]/
1575 and return "Uppercase characters are not permitted in $field";
1576 $self->ut_alpha($field);
1579 =item ut_phonen COLUMN [ COUNTRY ]
1581 Check/untaint phone numbers. May be null. If there is an error, returns
1582 the error, otherwise returns false.
1584 Takes an optional two-letter ISO country code; without it or with unsupported
1585 countries, ut_phonen simply calls ut_alphan.
1590 my( $self, $field, $country ) = @_;
1591 return $self->ut_alphan($field) unless defined $country;
1592 my $phonen = $self->getfield($field);
1593 if ( $phonen eq '' ) {
1594 $self->setfield($field,'');
1595 } elsif ( $country eq 'US' || $country eq 'CA' ) {
1597 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
1598 or return gettext('illegal_phone'). " $field: ". $self->getfield($field);
1599 $phonen = "$1-$2-$3";
1600 $phonen .= " x$4" if $4;
1601 $self->setfield($field,$phonen);
1603 warn "warning: don't know how to check phone numbers for country $country";
1604 return $self->ut_textn($field);
1611 Check/untaint hexadecimal values.
1616 my($self, $field) = @_;
1617 $self->getfield($field) =~ /^([\da-fA-F]+)$/
1618 or return "Illegal (hex) $field: ". $self->getfield($field);
1619 $self->setfield($field, uc($1));
1623 =item ut_hexn COLUMN
1625 Check/untaint hexadecimal values. May be null.
1630 my($self, $field) = @_;
1631 $self->getfield($field) =~ /^([\da-fA-F]*)$/
1632 or return "Illegal (hex) $field: ". $self->getfield($field);
1633 $self->setfield($field, uc($1));
1638 Check/untaint ip addresses. IPv4 only for now.
1643 my( $self, $field ) = @_;
1644 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
1645 or return "Illegal (IP address) $field: ". $self->getfield($field);
1646 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
1647 $self->setfield($field, "$1.$2.$3.$4");
1653 Check/untaint ip addresses. IPv4 only for now. May be null.
1658 my( $self, $field ) = @_;
1659 if ( $self->getfield($field) =~ /^()$/ ) {
1660 $self->setfield($field,'');
1663 $self->ut_ip($field);
1667 =item ut_coord COLUMN [ LOWER [ UPPER ] ]
1669 Check/untaint coordinates.
1670 Accepts the following forms:
1680 The "DDD MM SS" and "DDD MM MMM" are potentially ambiguous.
1681 The latter form (that is, the MMM are thousands of minutes) is
1682 assumed if the "MMM" is exactly three digits or two digits > 59.
1684 To be safe, just use the DDD.DDDDD form.
1686 If LOWER or UPPER are specified, then the coordinate is checked
1687 for lower and upper bounds, respectively.
1693 my ($self, $field) = (shift, shift);
1695 my $lower = shift if scalar(@_);
1696 my $upper = shift if scalar(@_);
1697 my $coord = $self->getfield($field);
1698 my $neg = $coord =~ s/^(-)//;
1700 my ($d, $m, $s) = (0, 0, 0);
1703 (($d) = ($coord =~ /^(\s*\d{1,3}(?:\.\d+)?)\s*$/)) ||
1704 (($d, $m) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2}(?:\.\d+))\s*$/)) ||
1705 (($d, $m, $s) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2})\s+(\d{1,3})\s*$/))
1707 $s = (((($s =~ /^\d{3}$/) or $s > 59) ? ($s / 1000) : ($s / 60)) / 60);
1710 return "Invalid (coordinate with minutes > 59) $field: "
1711 . $self->getfield($field);
1714 $coord = ($neg ? -1 : 1) * sprintf('%.8f', $d + $m + $s);
1716 if (defined($lower) and ($coord < $lower)) {
1717 return "Invalid (coordinate < $lower) $field: "
1718 . $self->getfield($field);;
1721 if (defined($upper) and ($coord > $upper)) {
1722 return "Invalid (coordinate > $upper) $field: "
1723 . $self->getfield($field);;
1726 $self->setfield($field, $coord);
1730 return "Invalid (coordinate) $field: " . $self->getfield($field);
1734 =item ut_coordn COLUMN [ LOWER [ UPPER ] ]
1736 Same as ut_coord, except optionally null.
1742 my ($self, $field) = (shift, shift);
1744 if ($self->getfield($field) =~ /^$/) {
1747 return $self->ut_coord($field, @_);
1753 =item ut_domain COLUMN
1755 Check/untaint host and domain names.
1760 my( $self, $field ) = @_;
1761 #$self->getfield($field) =~/^(\w+\.)*\w+$/
1762 $self->getfield($field) =~/^(([\w\-]+\.)*\w+)$/
1763 or return "Illegal (domain) $field: ". $self->getfield($field);
1764 $self->setfield($field,$1);
1768 =item ut_name COLUMN
1770 Check/untaint proper names; allows alphanumerics, spaces and the following
1771 punctuation: , . - '
1778 my( $self, $field ) = @_;
1779 $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
1780 or return gettext('illegal_name'). " $field: ". $self->getfield($field);
1781 $self->setfield($field,$1);
1787 Check/untaint zip codes.
1791 my @zip_reqd_countries = qw( AU CA US ); #CA, US implicit...
1794 my( $self, $field, $country ) = @_;
1796 if ( $country eq 'US' ) {
1798 $self->getfield($field) =~ /^\s*(\d{5}(\-\d{4})?)\s*$/
1799 or return gettext('illegal_zip'). " $field for country $country: ".
1800 $self->getfield($field);
1801 $self->setfield($field, $1);
1803 } elsif ( $country eq 'CA' ) {
1805 $self->getfield($field) =~ /^\s*([A-Z]\d[A-Z])\s*(\d[A-Z]\d)\s*$/i
1806 or return gettext('illegal_zip'). " $field for country $country: ".
1807 $self->getfield($field);
1808 $self->setfield($field, "$1 $2");
1812 if ( $self->getfield($field) =~ /^\s*$/
1813 && ( !$country || ! grep { $_ eq $country } @zip_reqd_countries )
1816 $self->setfield($field,'');
1818 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
1819 or return gettext('illegal_zip'). " $field: ". $self->getfield($field);
1820 $self->setfield($field,$1);
1828 =item ut_country COLUMN
1830 Check/untaint country codes. Country names are changed to codes, if possible -
1831 see L<Locale::Country>.
1836 my( $self, $field ) = @_;
1837 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
1838 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
1839 && country2code($1) ) {
1840 $self->setfield($field,uc(country2code($1)));
1843 $self->getfield($field) =~ /^(\w\w)$/
1844 or return "Illegal (country) $field: ". $self->getfield($field);
1845 $self->setfield($field,uc($1));
1849 =item ut_anything COLUMN
1851 Untaints arbitrary data. Be careful.
1856 my( $self, $field ) = @_;
1857 $self->getfield($field) =~ /^(.*)$/s
1858 or return "Illegal $field: ". $self->getfield($field);
1859 $self->setfield($field,$1);
1863 =item ut_enum COLUMN CHOICES_ARRAYREF
1865 Check/untaint a column, supplying all possible choices, like the "enum" type.
1870 my( $self, $field, $choices ) = @_;
1871 foreach my $choice ( @$choices ) {
1872 if ( $self->getfield($field) eq $choice ) {
1873 $self->setfield($choice);
1877 return "Illegal (enum) field $field: ". $self->getfield($field);
1880 =item ut_foreign_key COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1882 Check/untaint a foreign column key. Call a regular ut_ method (like ut_number)
1883 on the column first.
1887 sub ut_foreign_key {
1888 my( $self, $field, $table, $foreign ) = @_;
1889 qsearchs($table, { $foreign => $self->getfield($field) })
1890 or return "Can't find ". $self->table. ".$field ". $self->getfield($field).
1891 " in $table.$foreign";
1895 =item ut_foreign_keyn COLUMN FOREIGN_TABLE FOREIGN_COLUMN
1897 Like ut_foreign_key, except the null value is also allowed.
1901 sub ut_foreign_keyn {
1902 my( $self, $field, $table, $foreign ) = @_;
1903 $self->getfield($field)
1904 ? $self->ut_foreign_key($field, $table, $foreign)
1908 =item ut_agentnum_acl
1910 Checks this column as an agentnum, taking into account the current users's
1915 sub ut_agentnum_acl {
1916 my( $self, $field, $null_acl ) = @_;
1918 my $error = $self->ut_foreign_keyn($field, 'agent', 'agentnum');
1919 return "Illegal agentnum: $error" if $error;
1921 my $curuser = $FS::CurrentUser::CurrentUser;
1923 if ( $self->$field() ) {
1925 return "Access denied"
1926 unless $curuser->agentnum($self->$field());
1930 return "Access denied"
1931 unless $curuser->access_right($null_acl);
1939 =item virtual_fields [ TABLE ]
1941 Returns a list of virtual fields defined for the table. This should not
1942 be exported, and should only be called as an instance or class method.
1946 sub virtual_fields {
1949 $table = $self->table or confess "virtual_fields called on non-table";
1951 confess "Unknown table $table" unless dbdef->table($table);
1953 return () unless dbdef->table('part_virtual_field');
1955 unless ( $virtual_fields_cache{$table} ) {
1956 my $query = 'SELECT name from part_virtual_field ' .
1957 "WHERE dbtable = '$table'";
1959 my $result = $dbh->selectcol_arrayref($query);
1960 confess "Error executing virtual fields query: $query: ". $dbh->errstr
1962 $virtual_fields_cache{$table} = $result;
1965 @{$virtual_fields_cache{$table}};
1970 =item fields [ TABLE ]
1972 This is a wrapper for real_fields and virtual_fields. Code that called
1973 fields before should probably continue to call fields.
1978 my $something = shift;
1980 if($something->isa('FS::Record')) {
1981 $table = $something->table;
1983 $table = $something;
1984 $something = "FS::$table";
1986 return (real_fields($table), $something->virtual_fields());
1989 =item pvf FIELD_NAME
1991 Returns the FS::part_virtual_field object corresponding to a field in the
1992 record (specified by FIELD_NAME).
1997 my ($self, $name) = (shift, shift);
1999 if(grep /^$name$/, $self->virtual_fields) {
2000 return qsearchs('part_virtual_field', { dbtable => $self->table,
2006 =item vfieldpart_hashref TABLE
2008 Returns a hashref of virtual field names and vfieldparts applicable to the given
2013 sub vfieldpart_hashref {
2015 my $table = $self->table;
2017 return {} unless dbdef->table('part_virtual_field');
2020 my $statement = "SELECT vfieldpart, name FROM part_virtual_field WHERE ".
2021 "dbtable = '$table'";
2022 my $sth = $dbh->prepare($statement);
2023 $sth->execute or croak "Execution of '$statement' failed: ".$dbh->errstr;
2024 return { map { $_->{name}, $_->{vfieldpart} }
2025 @{$sth->fetchall_arrayref({})} };
2029 =item encrypt($value)
2031 Encrypts the credit card using a combination of PK to encrypt and uuencode to armour.
2033 Returns the encrypted string.
2035 You should generally not have to worry about calling this, as the system handles this for you.
2040 my ($self, $value) = @_;
2043 if ($conf->exists('encryption')) {
2044 if ($self->is_encrypted($value)) {
2045 # Return the original value if it isn't plaintext.
2046 $encrypted = $value;
2049 if (ref($rsa_encrypt) =~ /::RSA/) { # We Can Encrypt
2050 # RSA doesn't like the empty string so let's pack it up
2051 # The database doesn't like the RSA data so uuencode it
2052 my $length = length($value)+1;
2053 $encrypted = pack("u*",$rsa_encrypt->encrypt(pack("Z$length",$value)));
2055 die ("You can't encrypt w/o a valid RSA engine - Check your installation or disable encryption");
2062 =item is_encrypted($value)
2064 Checks to see if the string is encrypted and returns true or false (1/0) to indicate it's status.
2070 my ($self, $value) = @_;
2071 # Possible Bug - Some work may be required here....
2073 if ($value =~ /^M/ && length($value) > 80) {
2080 =item decrypt($value)
2082 Uses the private key to decrypt the string. Returns the decryoted string or undef on failure.
2084 You should generally not have to worry about calling this, as the system handles this for you.
2089 my ($self,$value) = @_;
2090 my $decrypted = $value; # Will return the original value if it isn't encrypted or can't be decrypted.
2091 if ($conf->exists('encryption') && $self->is_encrypted($value)) {
2093 if (ref($rsa_decrypt) =~ /::RSA/) {
2094 my $encrypted = unpack ("u*", $value);
2095 $decrypted = unpack("Z*", eval{$rsa_decrypt->decrypt($encrypted)});
2096 if ($@) {warn "Decryption Failed"};
2104 #Initialize the Module
2105 $rsa_module = 'Crypt::OpenSSL::RSA'; # The Default
2107 if ($conf->exists('encryptionmodule') && $conf->config_binary('encryptionmodule') ne '') {
2108 $rsa_module = $conf->config('encryptionmodule');
2112 eval ("require $rsa_module"); # No need to import the namespace
2115 # Initialize Encryption
2116 if ($conf->exists('encryptionpublickey') && $conf->config_binary('encryptionpublickey') ne '') {
2117 my $public_key = join("\n",$conf->config('encryptionpublickey'));
2118 $rsa_encrypt = $rsa_module->new_public_key($public_key);
2121 # Intitalize Decryption
2122 if ($conf->exists('encryptionprivatekey') && $conf->config_binary('encryptionprivatekey') ne '') {
2123 my $private_key = join("\n",$conf->config('encryptionprivatekey'));
2124 $rsa_decrypt = $rsa_module->new_private_key($private_key);
2128 =item h_search ACTION
2130 Given an ACTION, either "insert", or "delete", returns the appropriate history
2131 record corresponding to this record, if any.
2136 my( $self, $action ) = @_;
2138 my $table = $self->table;
2141 my $primary_key = dbdef->table($table)->primary_key;
2144 'table' => "h_$table",
2145 'hashref' => { $primary_key => $self->$primary_key(),
2146 'history_action' => $action,
2154 Given an ACTION, either "insert", or "delete", returns the timestamp of the
2155 appropriate history record corresponding to this record, if any.
2160 my($self, $action) = @_;
2161 my $h = $self->h_search($action);
2162 $h ? $h->history_date : '';
2171 =item real_fields [ TABLE ]
2173 Returns a list of the real columns in the specified table. Called only by
2174 fields() and other subroutines elsewhere in FS::Record.
2181 my($table_obj) = dbdef->table($table);
2182 confess "Unknown table $table" unless $table_obj;
2183 $table_obj->columns;
2186 =item _quote VALUE, TABLE, COLUMN
2188 This is an internal function used to construct SQL statements. It returns
2189 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
2190 type (see L<DBIx::DBSchema::Column>) does not end in `char' or `binary'.
2195 my($value, $table, $column) = @_;
2196 my $column_obj = dbdef->table($table)->column($column);
2197 my $column_type = $column_obj->type;
2198 my $nullable = $column_obj->null;
2200 warn " $table.$column: $value ($column_type".
2201 ( $nullable ? ' NULL' : ' NOT NULL' ).
2202 ")\n" if $DEBUG > 2;
2204 if ( $value eq '' && $nullable ) {
2206 } elsif ( $value eq '' && $column_type =~ /^(int|numeric)/ ) {
2207 cluck "WARNING: Attempting to set non-null integer $table.$column null; ".
2210 } elsif ( $value =~ /^\d+(\.\d+)?$/ &&
2211 ! $column_type =~ /(char|binary|text)$/i ) {
2220 This is deprecated. Don't use it.
2222 It returns a hash-type list with the fields of this record's table set true.
2227 carp "warning: hfields is deprecated";
2230 foreach (fields($table)) {
2239 "$_: ". $self->getfield($_). "|"
2240 } (fields($self->table)) );
2243 sub DESTROY { return; }
2247 # #use Carp qw(cluck);
2248 # #cluck "DESTROYING $self";
2249 # warn "DESTROYING $self";
2253 # return ! eval { join('',@_), kill 0; 1; };
2256 =item str2time_sql [ DRIVER_NAME ]
2258 Returns a function to convert to unix time based on database type, such as
2259 "EXTRACT( EPOCH FROM" for Pg or "UNIX_TIMESTAMP(" for mysql. See
2260 the str2time_sql_closing method to return a closing string rather than just
2261 using a closing parenthesis as previously suggested.
2263 You can pass an optional driver name such as "Pg", "mysql" or
2264 $dbh->{Driver}->{Name} to return a function for that database instead of
2265 the current database.
2270 my $driver = shift || driver_name;
2272 return 'UNIX_TIMESTAMP(' if $driver =~ /^mysql/i;
2273 return 'EXTRACT( EPOCH FROM ' if $driver =~ /^Pg/i;
2275 warn "warning: unknown database type $driver; guessing how to convert ".
2276 "dates to UNIX timestamps";
2277 return 'EXTRACT(EPOCH FROM ';
2281 =item str2time_sql_closing [ DRIVER_NAME ]
2283 Returns the closing suffix of a function to convert to unix time based on
2284 database type, such as ")::integer" for Pg or ")" for mysql.
2286 You can pass an optional driver name such as "Pg", "mysql" or
2287 $dbh->{Driver}->{Name} to return a function for that database instead of
2288 the current database.
2292 sub str2time_sql_closing {
2293 my $driver = shift || driver_name;
2295 return ' )::INTEGER ' if $driver =~ /^Pg/i;
2303 This module should probably be renamed, since much of the functionality is
2304 of general use. It is not completely unlike Adapter::DBI (see below).
2306 Exported qsearch and qsearchs should be deprecated in favor of method calls
2307 (against an FS::Record object like the old search and searchs that qsearch
2308 and qsearchs were on top of.)
2310 The whole fields / hfields mess should be removed.
2312 The various WHERE clauses should be subroutined.
2314 table string should be deprecated in favor of DBIx::DBSchema::Table.
2316 No doubt we could benefit from a Tied hash. Documenting how exists / defined
2317 true maps to the database (and WHERE clauses) would also help.
2319 The ut_ methods should ask the dbdef for a default length.
2321 ut_sqltype (like ut_varchar) should all be defined
2323 A fallback check method should be provided which uses the dbdef.
2325 The ut_money method assumes money has two decimal digits.
2327 The Pg money kludge in the new method only strips `$'.
2329 The ut_phonen method only checks US-style phone numbers.
2331 The _quote function should probably use ut_float instead of a regex.
2333 All the subroutines probably should be methods, here or elsewhere.
2335 Probably should borrow/use some dbdef methods where appropriate (like sub
2338 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
2339 or allow it to be set. Working around it is ugly any way around - DBI should
2340 be fixed. (only affects RDBMS which return uppercase column names)
2342 ut_zip should take an optional country like ut_phone.
2346 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
2348 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.