4 use vars qw( $AUTOLOAD @ISA @EXPORT_OK $DEBUG
6 $conf $conf_encryption $money_char $lat_lower $lon_upper
8 $nowarn_identical $nowarn_classload
9 $no_update_diff $no_check_foreign
13 use Carp qw(carp cluck croak confess);
14 use Scalar::Util qw( blessed );
15 use File::CounterFile;
18 use File::Slurp qw( slurp );
19 use DBI qw(:sql_types);
20 use DBIx::DBSchema 0.38;
21 use FS::UID qw(dbh getotaker datasrc driver_name);
23 use FS::Schema qw(dbdef);
25 use FS::Msgcat qw(gettext);
26 use NetAddr::IP; # for validation
27 #use FS::Conf; #dependency loop bs, in install_callback below instead
29 use FS::part_virtual_field;
35 @encrypt_payby = qw( CARD DCRD CHEK DCHK );
37 #export dbdef for now... everything else expects to find it here
39 dbh fields hfields qsearch qsearchs dbdef jsearch
40 str2time_sql str2time_sql_closing regexp_sql not_regexp_sql concat_sql
46 $nowarn_identical = 0;
47 $nowarn_classload = 0;
49 $no_check_foreign = 0;
57 $conf_encryption = '';
58 FS::UID->install_callback( sub {
62 $conf = FS::Conf->new;
63 $conf_encryption = $conf->exists('encryption');
64 $money_char = $conf->config('money_char') || '$';
65 my $nw_coords = $conf->exists('geocode-require_nw_coordinates');
66 $lat_lower = $nw_coords ? 1 : -90;
67 $lon_upper = $nw_coords ? -1 : 180;
69 $File::CounterFile::DEFAULT_DIR = $conf->base_dir . "/counters.". datasrc;
71 if ( driver_name eq 'Pg' ) {
72 eval "use DBD::Pg ':pg_types'";
75 eval "sub PG_BYTEA { die 'guru meditation #9: calling PG_BYTEA when not running Pg?'; }";
82 FS::Record - Database record objects
87 use FS::Record qw(dbh fields qsearch qsearchs);
89 $record = new FS::Record 'table', \%hash;
90 $record = new FS::Record 'table', { 'column' => 'value', ... };
92 $record = qsearchs FS::Record 'table', \%hash;
93 $record = qsearchs FS::Record 'table', { 'column' => 'value', ... };
94 @records = qsearch FS::Record 'table', \%hash;
95 @records = qsearch FS::Record 'table', { 'column' => 'value', ... };
97 $table = $record->table;
98 $dbdef_table = $record->dbdef_table;
100 $value = $record->get('column');
101 $value = $record->getfield('column');
102 $value = $record->column;
104 $record->set( 'column' => 'value' );
105 $record->setfield( 'column' => 'value' );
106 $record->column('value');
108 %hash = $record->hash;
110 $hashref = $record->hashref;
112 $error = $record->insert;
114 $error = $record->delete;
116 $error = $new_record->replace($old_record);
118 # external use deprecated - handled by the database (at least for Pg, mysql)
119 $value = $record->unique('column');
121 $error = $record->ut_float('column');
122 $error = $record->ut_floatn('column');
123 $error = $record->ut_number('column');
124 $error = $record->ut_numbern('column');
125 $error = $record->ut_snumber('column');
126 $error = $record->ut_snumbern('column');
127 $error = $record->ut_money('column');
128 $error = $record->ut_text('column');
129 $error = $record->ut_textn('column');
130 $error = $record->ut_alpha('column');
131 $error = $record->ut_alphan('column');
132 $error = $record->ut_phonen('column');
133 $error = $record->ut_anything('column');
134 $error = $record->ut_name('column');
136 $quoted_value = _quote($value,'table','field');
139 $fields = hfields('table');
140 if ( $fields->{Field} ) { # etc.
142 @fields = fields 'table'; #as a subroutine
143 @fields = $record->fields; #as a method call
148 (Mostly) object-oriented interface to database records. Records are currently
149 implemented on top of DBI. FS::Record is intended as a base class for
150 table-specific classes to inherit from, i.e. FS::cust_main.
156 =item new [ TABLE, ] HASHREF
158 Creates a new record. It doesn't store it in the database, though. See
159 L<"insert"> for that.
161 Note that the object stores this hash reference, not a distinct copy of the
162 hash it points to. You can ask the object for a copy with the I<hash>
165 TABLE can only be omitted when a dervived class overrides the table method.
171 my $class = ref($proto) || $proto;
173 bless ($self, $class);
175 unless ( defined ( $self->table ) ) {
176 $self->{'Table'} = shift;
177 carp "warning: FS::Record::new called with table name ". $self->{'Table'}
178 unless $nowarn_classload;
181 $self->{'Hash'} = shift;
183 foreach my $field ( grep !defined($self->{'Hash'}{$_}), $self->fields ) {
184 $self->{'Hash'}{$field}='';
187 $self->_rebless if $self->can('_rebless');
189 $self->{'modified'} = 0;
191 $self->_cache($self->{'Hash'}, shift) if $self->can('_cache') && @_;
198 my $class = ref($proto) || $proto;
200 bless ($self, $class);
202 $self->{'Table'} = shift unless defined ( $self->table );
204 my $hashref = $self->{'Hash'} = shift;
206 if ( defined( $cache->cache->{$hashref->{$cache->key}} ) ) {
207 my $obj = $cache->cache->{$hashref->{$cache->key}};
208 $obj->_cache($hashref, $cache) if $obj->can('_cache');
211 $cache->cache->{$hashref->{$cache->key}} = $self->new($hashref, $cache);
218 my $class = ref($proto) || $proto;
220 bless ($self, $class);
221 if ( defined $self->table ) {
222 cluck "create constructor is deprecated, use new!";
225 croak "FS::Record::create called (not from a subclass)!";
229 =item qsearch PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
231 Searches the database for all records matching (at least) the key/value pairs
232 in HASHREF. Returns all the records found as `FS::TABLE' objects if that
233 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
236 The preferred usage is to pass a hash reference of named parameters:
238 @records = qsearch( {
239 'table' => 'table_name',
240 'hashref' => { 'field' => 'value'
241 'field' => { 'op' => '<',
246 #these are optional...
248 'extra_sql' => 'AND field = ? AND intfield = ?',
249 'extra_param' => [ 'value', [ 5, 'int' ] ],
250 'order_by' => 'ORDER BY something',
251 #'cache_obj' => '', #optional
252 'addl_from' => 'LEFT JOIN othtable USING ( field )',
257 Much code still uses old-style positional parameters, this is also probably
258 fine in the common case where there are only two parameters:
260 my @records = qsearch( 'table', { 'field' => 'value' } );
262 Also possible is an experimental LISTREF of PARAMS_HASHREFs for a UNION of
263 the individual PARAMS_HASHREF queries
265 ###oops, argh, FS::Record::new only lets us create database fields.
266 #Normal behaviour if SELECT is not specified is `*', as in
267 #C<SELECT * FROM table WHERE ...>. However, there is an experimental new
268 #feature where you can specify SELECT - remember, the objects returned,
269 #although blessed into the appropriate `FS::TABLE' package, will only have the
270 #fields you specify. This might have unwanted results if you then go calling
271 #regular FS::TABLE methods
276 my %TYPE = (); #for debugging
279 my($type, $value) = @_;
281 my $bind_type = { TYPE => SQL_VARCHAR };
283 if ( $type =~ /(big)?(int|serial)/i && $value =~ /^-?\d+(\.\d+)?$/ ) {
285 $bind_type = { TYPE => SQL_INTEGER };
287 } elsif ( $type =~ /^bytea$/i || $type =~ /(blob|varbinary)/i ) {
289 if ( driver_name eq 'Pg' ) {
291 $bind_type = { pg_type => PG_BYTEA };
293 # $bind_type = ? #SQL_VARCHAR could be fine?
296 #DBD::Pg 1.49: Cannot bind ... unknown sql_type 6 with SQL_FLOAT
297 #fixed by DBD::Pg 2.11.8
298 #can change back to SQL_FLOAT in early-mid 2010, once everyone's upgraded
299 #(make a Tron test first)
300 } elsif ( _is_fs_float( $type, $value ) ) {
302 $bind_type = { TYPE => SQL_DECIMAL };
311 my($type, $value) = @_;
312 if ( ( $type =~ /(numeric)/i && $value =~ /^[+-]?\d+(\.\d+)?$/ ) ||
313 ( $type =~ /(real|float4)/i && $value =~ /[-+]?\d*\.?\d+([eE][-+]?\d+)?/)
321 my( @stable, @record, @cache );
322 my( @select, @extra_sql, @extra_param, @order_by, @addl_from );
324 my %union_options = ();
325 if ( ref($_[0]) eq 'ARRAY' ) {
328 foreach my $href ( @$optlist ) {
329 push @stable, ( $href->{'table'} or die "table name is required" );
330 push @record, ( $href->{'hashref'} || {} );
331 push @select, ( $href->{'select'} || '*' );
332 push @extra_sql, ( $href->{'extra_sql'} || '' );
333 push @extra_param, ( $href->{'extra_param'} || [] );
334 push @order_by, ( $href->{'order_by'} || '' );
335 push @cache, ( $href->{'cache_obj'} || '' );
336 push @addl_from, ( $href->{'addl_from'} || '' );
337 push @debug, ( $href->{'debug'} || '' );
339 die "at least one hashref is required" unless scalar(@stable);
340 } elsif ( ref($_[0]) eq 'HASH' ) {
342 $stable[0] = $opt->{'table'} or die "table name is required";
343 $record[0] = $opt->{'hashref'} || {};
344 $select[0] = $opt->{'select'} || '*';
345 $extra_sql[0] = $opt->{'extra_sql'} || '';
346 $extra_param[0] = $opt->{'extra_param'} || [];
347 $order_by[0] = $opt->{'order_by'} || '';
348 $cache[0] = $opt->{'cache_obj'} || '';
349 $addl_from[0] = $opt->{'addl_from'} || '';
350 $debug[0] = $opt->{'debug'} || '';
361 my $cache = $cache[0];
367 foreach my $stable ( @stable ) {
368 #stop altering the caller's hashref
369 my $record = { %{ shift(@record) || {} } };#and be liberal in receipt
370 my $select = shift @select;
371 my $extra_sql = shift @extra_sql;
372 my $extra_param = shift @extra_param;
373 my $order_by = shift @order_by;
374 my $cache = shift @cache;
375 my $addl_from = shift @addl_from;
376 my $debug = shift @debug;
378 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
380 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
383 my $table = $cache ? $cache->table : $stable;
384 my $dbdef_table = dbdef->table($table)
385 or die "No schema for table $table found - ".
386 "do you need to run freeside-upgrade?";
387 my $pkey = $dbdef_table->primary_key;
389 my @real_fields = grep exists($record->{$_}), real_fields($table);
391 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
392 @virtual_fields = grep exists($record->{$_}), "FS::$table"->virtual_fields;
394 cluck "warning: FS::$table not loaded; virtual fields not searchable"
395 unless $nowarn_classload;
396 @virtual_fields = ();
399 my $statement .= "SELECT $select FROM $stable";
400 $statement .= " $addl_from" if $addl_from;
401 if ( @real_fields or @virtual_fields ) {
402 $statement .= ' WHERE '. join(' AND ',
403 get_real_fields($table, $record, \@real_fields) ,
404 get_virtual_fields($table, $pkey, $record, \@virtual_fields),
408 $statement .= " $extra_sql" if defined($extra_sql);
409 $statement .= " $order_by" if defined($order_by);
411 push @statement, $statement;
413 warn "[debug]$me $statement\n" if $DEBUG > 1 || $debug;
417 grep defined( $record->{$_} ) && $record->{$_} ne '', @real_fields
420 my $value = $record->{$field};
421 my $op = (ref($value) && $value->{op}) ? $value->{op} : '=';
422 $value = $value->{'value'} if ref($value);
423 my $type = dbdef->table($table)->column($field)->type;
425 my $bind_type = _bind_type($type, $value);
429 # %TYPE = map { &{"DBI::$_"}() => $_ } @{ $DBI::EXPORT_TAGS{sql_types} }
431 # warn " bind_param $bind (for field $field), $value, TYPE $TYPE{$TYPE}\n";
435 push @bind_type, $bind_type;
439 foreach my $param ( @$extra_param ) {
440 my $bind_type = { TYPE => SQL_VARCHAR };
443 $value = $param->[0];
444 my $type = $param->[1];
445 $bind_type = _bind_type($type, $value);
448 push @bind_type, $bind_type;
452 my $statement = join( ' ) UNION ( ', @statement );
453 $statement = "( $statement )" if scalar(@statement) > 1;
454 $statement .= " $union_options{order_by}" if $union_options{order_by};
456 my $sth = $dbh->prepare($statement)
457 or croak "$dbh->errstr doing $statement";
460 foreach my $value ( @value ) {
461 my $bind_type = shift @bind_type;
462 $sth->bind_param($bind++, $value, $bind_type );
465 # $sth->execute( map $record->{$_},
466 # grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
467 # ) or croak "Error executing \"$statement\": ". $sth->errstr;
469 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
471 # virtual fields and blessings are nonsense in a heterogeneous UNION, right?
472 my $table = $stable[0];
474 $table = '' if grep { $_ ne $table } @stable;
475 $pkey = dbdef->table($table)->primary_key if $table;
477 my @virtual_fields = ();
478 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
479 @virtual_fields = "FS::$table"->virtual_fields;
481 cluck "warning: FS::$table not loaded; virtual fields not returned either"
482 unless $nowarn_classload;
483 @virtual_fields = ();
487 tie %result, "Tie::IxHash";
488 my @stuff = @{ $sth->fetchall_arrayref( {} ) };
489 if ( $pkey && scalar(@stuff) && $stuff[0]->{$pkey} ) {
490 %result = map { $_->{$pkey}, $_ } @stuff;
492 @result{@stuff} = @stuff;
497 if ( keys(%result) and @virtual_fields ) {
499 "SELECT virtual_field.recnum, part_virtual_field.name, ".
500 "virtual_field.value ".
501 "FROM part_virtual_field JOIN virtual_field USING (vfieldpart) ".
502 "WHERE part_virtual_field.dbtable = '$table' AND ".
503 "virtual_field.recnum IN (".
504 join(',', keys(%result)). ") AND part_virtual_field.name IN ('".
505 join(q!', '!, @virtual_fields) . "')";
506 warn "[debug]$me $statement\n" if $DEBUG > 1;
507 $sth = $dbh->prepare($statement) or croak "$dbh->errstr doing $statement";
508 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
510 foreach (@{ $sth->fetchall_arrayref({}) }) {
511 my $recnum = $_->{recnum};
512 my $name = $_->{name};
513 my $value = $_->{value};
514 if (exists($result{$recnum})) {
515 $result{$recnum}->{$name} = $value;
520 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
521 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
522 #derivied class didn't override new method, so this optimization is safe
525 new_or_cached( "FS::$table", { %{$_} }, $cache )
529 new( "FS::$table", { %{$_} } )
533 #okay, its been tested
534 # warn "untested code (class FS::$table uses custom new method)";
536 eval 'FS::'. $table. '->new( { %{$_} } )';
540 # Check for encrypted fields and decrypt them.
541 ## only in the local copy, not the cached object
542 if ( $conf_encryption
543 && eval 'defined(@FS::'. $table . '::encrypted_fields)' ) {
544 foreach my $record (@return) {
545 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
546 next if $field eq 'payinfo'
547 && ($record->isa('FS::payinfo_transaction_Mixin')
548 || $record->isa('FS::payinfo_Mixin') )
550 && !grep { $record->payby eq $_ } @encrypt_payby;
551 # Set it directly... This may cause a problem in the future...
552 $record->setfield($field, $record->decrypt($record->getfield($field)));
557 cluck "warning: FS::$table not loaded; returning FS::Record objects"
558 unless $nowarn_classload;
560 FS::Record->new( $table, { %{$_} } );
566 ## makes this easier to read
568 sub get_virtual_fields {
572 my $virtual_fields = shift;
578 if ( ref($record->{$_}) ) {
579 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
580 if ( uc($op) eq 'ILIKE' ) {
582 $record->{$_}{'value'} = lc($record->{$_}{'value'});
583 $column = "LOWER($_)";
585 $record->{$_} = $record->{$_}{'value'};
588 # ... EXISTS ( SELECT name, value FROM part_virtual_field
590 # ON part_virtual_field.vfieldpart = virtual_field.vfieldpart
591 # WHERE recnum = svc_acct.svcnum
592 # AND (name, value) = ('egad', 'brain') )
594 my $value = $record->{$_};
598 $subq = ($value ? 'EXISTS ' : 'NOT EXISTS ') .
599 "( SELECT part_virtual_field.name, virtual_field.value ".
600 "FROM part_virtual_field JOIN virtual_field ".
601 "ON part_virtual_field.vfieldpart = virtual_field.vfieldpart ".
602 "WHERE virtual_field.recnum = ${table}.${pkey} ".
603 "AND part_virtual_field.name = '${column}'".
605 " AND virtual_field.value ${op} '${value}'"
609 } @{ $virtual_fields } ) ;
612 sub get_real_fields {
615 my $real_fields = shift;
617 ## this huge map was previously inline, just broke it out to help read the qsearch method, should be optimized for readability
623 my $type = dbdef->table($table)->column($column)->type;
624 my $value = $record->{$column};
625 $value = $value->{'value'} if ref($value);
626 if ( ref($record->{$_}) ) {
627 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
628 #$op = 'LIKE' if $op =~ /^ILIKE$/i && driver_name ne 'Pg';
629 if ( uc($op) eq 'ILIKE' ) {
631 $record->{$_}{'value'} = lc($record->{$_}{'value'});
632 $column = "LOWER($_)";
634 $record->{$_} = $record->{$_}{'value'}
637 if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) {
639 if ( driver_name eq 'Pg' ) {
640 if ( $type =~ /(int|numeric|real|float4|(big)?serial)/i ) {
641 qq-( $column IS NULL )-;
643 qq-( $column IS NULL OR $column = '' )-;
646 qq-( $column IS NULL OR $column = "" )-;
648 } elsif ( $op eq '!=' ) {
649 if ( driver_name eq 'Pg' ) {
650 if ( $type =~ /(int|numeric|real|float4|(big)?serial)/i ) {
651 qq-( $column IS NOT NULL )-;
653 qq-( $column IS NOT NULL AND $column != '' )-;
656 qq-( $column IS NOT NULL AND $column != "" )-;
659 if ( driver_name eq 'Pg' ) {
660 qq-( $column $op '' )-;
662 qq-( $column $op "" )-;
665 #if this needs to be re-enabled, it needs to use a custom op like
666 #"APPROX=" or something (better name?, not '=', to avoid affecting other
668 #} elsif ( $op eq 'APPROX=' && _is_fs_float( $type, $value ) ) {
669 # ( "$column <= ?", "$column >= ?" );
673 } @{ $real_fields } );
676 =item by_key PRIMARY_KEY_VALUE
678 This is a class method that returns the record with the given primary key
679 value. This method is only useful in FS::Record subclasses. For example:
681 my $cust_main = FS::cust_main->by_key(1); # retrieve customer with custnum 1
685 my $cust_main = qsearchs('cust_main', { 'custnum' => 1 } );
690 my ($class, $pkey_value) = @_;
692 my $table = $class->table
693 or croak "No table for $class found";
695 my $dbdef_table = dbdef->table($table)
696 or die "No schema for table $table found - ".
697 "do you need to create it or run dbdef-create?";
698 my $pkey = $dbdef_table->primary_key
699 or die "No primary key for table $table";
701 return qsearchs($table, { $pkey => $pkey_value });
704 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
706 Experimental JOINed search method. Using this method, you can execute a
707 single SELECT spanning multiple tables, and cache the results for subsequent
708 method calls. Interface will almost definately change in an incompatible
716 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
717 my $cache = FS::SearchCache->new( $ptable, $pkey );
720 grep { !$saw{$_->getfield($pkey)}++ }
721 qsearch($table, $record, $select, $extra_sql, $cache )
725 =item qsearchs PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
727 Same as qsearch, except that if more than one record matches, it B<carp>s but
728 returns the first. If this happens, you either made a logic error in asking
729 for a single item, or your data is corrupted.
733 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
735 my(@result) = qsearch(@_);
736 cluck "warning: Multiple records in scalar search ($table)"
737 if scalar(@result) > 1;
738 #should warn more vehemently if the search was on a primary key?
739 scalar(@result) ? ($result[0]) : ();
750 Returns the table name.
755 # cluck "warning: FS::Record::table deprecated; supply one in subclass!";
762 Returns the DBIx::DBSchema::Table object for the table.
768 my($table)=$self->table;
769 dbdef->table($table);
774 Returns the primary key for the table.
780 my $pkey = $self->dbdef_table->primary_key;
783 =item get, getfield COLUMN
785 Returns the value of the column/field/key COLUMN.
790 my($self,$field) = @_;
791 # to avoid "Use of unitialized value" errors
792 if ( defined ( $self->{Hash}->{$field} ) ) {
793 $self->{Hash}->{$field};
803 =item set, setfield COLUMN, VALUE
805 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
810 my($self,$field,$value) = @_;
811 $self->{'modified'} = 1;
812 $self->{'Hash'}->{$field} = $value;
821 Returns true if the column/field/key COLUMN exists.
826 my($self,$field) = @_;
827 exists($self->{Hash}->{$field});
830 =item AUTLOADED METHODS
832 $record->column is a synonym for $record->get('column');
834 $record->column('value') is a synonym for $record->set('column','value');
841 my($field)=$AUTOLOAD;
843 if ( defined($value) ) {
844 confess "errant AUTOLOAD $field for $self (arg $value)"
845 unless blessed($self) && $self->can('setfield');
846 $self->setfield($field,$value);
848 confess "errant AUTOLOAD $field for $self (no args)"
849 unless blessed($self) && $self->can('getfield');
850 $self->getfield($field);
856 # my $field = $AUTOLOAD;
858 # if ( defined($_[1]) ) {
859 # $_[0]->setfield($field, $_[1]);
861 # $_[0]->getfield($field);
867 Returns a list of the column/value pairs, usually for assigning to a new hash.
869 To make a distinct duplicate of an FS::Record object, you can do:
871 $new = new FS::Record ( $old->table, { $old->hash } );
877 confess $self. ' -> hash: Hash attribute is undefined'
878 unless defined($self->{'Hash'});
879 %{ $self->{'Hash'} };
884 Returns a reference to the column/value hash. This may be deprecated in the
885 future; if there's a reason you can't just use the autoloaded or get/set
897 Returns true if any of this object's values have been modified with set (or via
898 an autoloaded method). Doesn't yet recognize when you retreive a hashref and
908 =item select_for_update
910 Selects this record with the SQL "FOR UPDATE" command. This can be useful as
915 sub select_for_update {
917 my $primary_key = $self->primary_key;
920 'table' => $self->table,
921 'hashref' => { $primary_key => $self->$primary_key() },
922 'extra_sql' => 'FOR UPDATE',
928 Locks this table with a database-driver specific lock method. This is used
929 as a mutex in order to do a duplicate search.
931 For PostgreSQL, does "LOCK TABLE tablename IN SHARE ROW EXCLUSIVE MODE".
933 For MySQL, does a SELECT FOR UPDATE on the duplicate_lock table.
935 Errors are fatal; no useful return value.
937 Note: To use this method for new tables other than svc_acct and svc_phone,
938 edit freeside-upgrade and add those tables to the duplicate_lock list.
944 my $table = $self->table;
946 warn "$me locking $table table\n" if $DEBUG;
948 if ( driver_name =~ /^Pg/i ) {
950 dbh->do("LOCK TABLE $table IN SHARE ROW EXCLUSIVE MODE")
953 } elsif ( driver_name =~ /^mysql/i ) {
955 dbh->do("SELECT * FROM duplicate_lock
956 WHERE lockname = '$table'
958 ) or die dbh->errstr;
962 die "unknown database ". driver_name. "; don't know how to lock table";
966 warn "$me acquired $table table lock\n" if $DEBUG;
972 Inserts this record to the database. If there is an error, returns the error,
973 otherwise returns false.
981 warn "$self -> insert" if $DEBUG;
983 my $error = $self->check;
984 return $error if $error;
986 #single-field unique keys are given a value if false
987 #(like MySQL's AUTO_INCREMENT or Pg SERIAL)
988 foreach ( $self->dbdef_table->unique_singles) {
989 $self->unique($_) unless $self->getfield($_);
992 #and also the primary key, if the database isn't going to
993 my $primary_key = $self->dbdef_table->primary_key;
995 if ( $primary_key ) {
996 my $col = $self->dbdef_table->column($primary_key);
999 uc($col->type) =~ /^(BIG)?SERIAL\d?/
1000 || ( driver_name eq 'Pg'
1001 && defined($col->default)
1002 && $col->quoted_default =~ /^nextval\(/i
1004 || ( driver_name eq 'mysql'
1005 && defined($col->local)
1006 && $col->local =~ /AUTO_INCREMENT/i
1008 $self->unique($primary_key) unless $self->getfield($primary_key) || $db_seq;
1011 my $table = $self->table;
1013 # Encrypt before the database
1014 if ( defined(eval '@FS::'. $table . '::encrypted_fields')
1015 && scalar( eval '@FS::'. $table . '::encrypted_fields')
1016 && $conf->exists('encryption')
1018 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
1019 next if $field eq 'payinfo'
1020 && ($self->isa('FS::payinfo_transaction_Mixin')
1021 || $self->isa('FS::payinfo_Mixin') )
1023 && !grep { $self->payby eq $_ } @encrypt_payby;
1024 $saved->{$field} = $self->getfield($field);
1025 $self->setfield($field, $self->encrypt($self->getfield($field)));
1029 #false laziness w/delete
1031 grep { defined($self->getfield($_)) && $self->getfield($_) ne "" }
1034 my @values = map { _quote( $self->getfield($_), $table, $_) } @real_fields;
1037 my $statement = "INSERT INTO $table ";
1038 if ( @real_fields ) {
1041 join( ', ', @real_fields ).
1043 join( ', ', @values ).
1047 $statement .= 'DEFAULT VALUES';
1049 warn "[debug]$me $statement\n" if $DEBUG > 1;
1050 my $sth = dbh->prepare($statement) or return dbh->errstr;
1052 local $SIG{HUP} = 'IGNORE';
1053 local $SIG{INT} = 'IGNORE';
1054 local $SIG{QUIT} = 'IGNORE';
1055 local $SIG{TERM} = 'IGNORE';
1056 local $SIG{TSTP} = 'IGNORE';
1057 local $SIG{PIPE} = 'IGNORE';
1059 $sth->execute or return $sth->errstr;
1061 # get inserted id from the database, if applicable & needed
1062 if ( $db_seq && ! $self->getfield($primary_key) ) {
1063 warn "[debug]$me retreiving sequence from database\n" if $DEBUG;
1067 if ( driver_name eq 'Pg' ) {
1069 #my $oid = $sth->{'pg_oid_status'};
1070 #my $i_sql = "SELECT $primary_key FROM $table WHERE oid = ?";
1072 my $default = $self->dbdef_table->column($primary_key)->quoted_default;
1073 unless ( $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i ) {
1074 dbh->rollback if $FS::UID::AutoCommit;
1075 return "can't parse $table.$primary_key default value".
1076 " for sequence name: $default";
1080 my $i_sql = "SELECT currval('$sequence')";
1081 my $i_sth = dbh->prepare($i_sql) or do {
1082 dbh->rollback if $FS::UID::AutoCommit;
1085 $i_sth->execute() or do { #$i_sth->execute($oid)
1086 dbh->rollback if $FS::UID::AutoCommit;
1087 return $i_sth->errstr;
1089 $insertid = $i_sth->fetchrow_arrayref->[0];
1091 } elsif ( driver_name eq 'mysql' ) {
1093 $insertid = dbh->{'mysql_insertid'};
1094 # work around mysql_insertid being null some of the time, ala RT :/
1095 unless ( $insertid ) {
1096 warn "WARNING: DBD::mysql didn't return mysql_insertid; ".
1097 "using SELECT LAST_INSERT_ID();";
1098 my $i_sql = "SELECT LAST_INSERT_ID()";
1099 my $i_sth = dbh->prepare($i_sql) or do {
1100 dbh->rollback if $FS::UID::AutoCommit;
1103 $i_sth->execute or do {
1104 dbh->rollback if $FS::UID::AutoCommit;
1105 return $i_sth->errstr;
1107 $insertid = $i_sth->fetchrow_arrayref->[0];
1112 dbh->rollback if $FS::UID::AutoCommit;
1113 return "don't know how to retreive inserted ids from ". driver_name.
1114 ", try using counterfiles (maybe run dbdef-create?)";
1118 $self->setfield($primary_key, $insertid);
1122 my @virtual_fields =
1123 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
1124 $self->virtual_fields;
1125 if (@virtual_fields) {
1126 my %v_values = map { $_, $self->getfield($_) } @virtual_fields;
1128 my $vfieldpart = $self->vfieldpart_hashref;
1130 my $v_statement = "INSERT INTO virtual_field(recnum, vfieldpart, value) ".
1133 my $v_sth = dbh->prepare($v_statement) or do {
1134 dbh->rollback if $FS::UID::AutoCommit;
1138 foreach (keys(%v_values)) {
1139 $v_sth->execute($self->getfield($primary_key),
1143 dbh->rollback if $FS::UID::AutoCommit;
1144 return $v_sth->errstr;
1151 if ( defined dbdef->table('h_'. $table) ) {
1152 my $h_statement = $self->_h_statement('insert');
1153 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
1154 $h_sth = dbh->prepare($h_statement) or do {
1155 dbh->rollback if $FS::UID::AutoCommit;
1161 $h_sth->execute or return $h_sth->errstr if $h_sth;
1163 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1165 # Now that it has been saved, reset the encrypted fields so that $new
1166 # can still be used.
1167 foreach my $field (keys %{$saved}) {
1168 $self->setfield($field, $saved->{$field});
1176 Depriciated (use insert instead).
1181 cluck "warning: FS::Record::add deprecated!";
1182 insert @_; #call method in this scope
1187 Delete this record from the database. If there is an error, returns the error,
1188 otherwise returns false.
1195 my $statement = "DELETE FROM ". $self->table. " WHERE ". join(' AND ',
1197 $self->getfield($_) eq ''
1198 #? "( $_ IS NULL OR $_ = \"\" )"
1199 ? ( driver_name eq 'Pg'
1201 : "( $_ IS NULL OR $_ = \"\" )"
1203 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
1204 } ( $self->dbdef_table->primary_key )
1205 ? ( $self->dbdef_table->primary_key)
1206 : real_fields($self->table)
1208 warn "[debug]$me $statement\n" if $DEBUG > 1;
1209 my $sth = dbh->prepare($statement) or return dbh->errstr;
1212 if ( defined dbdef->table('h_'. $self->table) ) {
1213 my $h_statement = $self->_h_statement('delete');
1214 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
1215 $h_sth = dbh->prepare($h_statement) or return dbh->errstr;
1220 my $primary_key = $self->dbdef_table->primary_key;
1223 my $vfp = $self->vfieldpart_hashref;
1224 foreach($self->virtual_fields) {
1225 next if $self->getfield($_) eq '';
1226 unless(@del_vfields) {
1227 my $st = "DELETE FROM virtual_field WHERE recnum = ? AND vfieldpart = ?";
1228 $v_sth = dbh->prepare($st) or return dbh->errstr;
1230 push @del_vfields, $_;
1233 local $SIG{HUP} = 'IGNORE';
1234 local $SIG{INT} = 'IGNORE';
1235 local $SIG{QUIT} = 'IGNORE';
1236 local $SIG{TERM} = 'IGNORE';
1237 local $SIG{TSTP} = 'IGNORE';
1238 local $SIG{PIPE} = 'IGNORE';
1240 my $rc = $sth->execute or return $sth->errstr;
1241 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
1242 $h_sth->execute or return $h_sth->errstr if $h_sth;
1243 $v_sth->execute($self->getfield($primary_key), $vfp->{$_})
1244 or return $v_sth->errstr
1245 foreach (@del_vfields);
1247 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1249 #no need to needlessly destoy the data either (causes problems actually)
1250 #undef $self; #no need to keep object!
1257 Depriciated (use delete instead).
1262 cluck "warning: FS::Record::del deprecated!";
1263 &delete(@_); #call method in this scope
1266 =item replace OLD_RECORD
1268 Replace the OLD_RECORD with this one in the database. If there is an error,
1269 returns the error, otherwise returns false.
1274 my ($new, $old) = (shift, shift);
1276 $old = $new->replace_old unless defined($old);
1278 warn "[debug]$me $new ->replace $old\n" if $DEBUG;
1280 if ( $new->can('replace_check') ) {
1281 my $error = $new->replace_check($old);
1282 return $error if $error;
1285 return "Records not in same table!" unless $new->table eq $old->table;
1287 my $primary_key = $old->dbdef_table->primary_key;
1288 return "Can't change primary key $primary_key ".
1289 'from '. $old->getfield($primary_key).
1290 ' to ' . $new->getfield($primary_key)
1292 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
1294 my $error = $new->check;
1295 return $error if $error;
1297 # Encrypt for replace
1299 if ( $conf->exists('encryption')
1300 && defined(eval '@FS::'. $new->table . '::encrypted_fields')
1301 && scalar( eval '@FS::'. $new->table . '::encrypted_fields')
1303 foreach my $field (eval '@FS::'. $new->table . '::encrypted_fields') {
1304 next if $field eq 'payinfo'
1305 && ($new->isa('FS::payinfo_transaction_Mixin')
1306 || $new->isa('FS::payinfo_Mixin') )
1308 && !grep { $new->payby eq $_ } @encrypt_payby;
1309 $saved->{$field} = $new->getfield($field);
1310 $new->setfield($field, $new->encrypt($new->getfield($field)));
1314 #my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
1315 my %diff = map { ($new->getfield($_) ne $old->getfield($_))
1316 ? ($_, $new->getfield($_)) : () } $old->fields;
1318 unless (keys(%diff) || $no_update_diff ) {
1319 carp "[warning]$me ". ref($new)."->replace ".
1320 ( $primary_key ? "$primary_key ".$new->get($primary_key) : '' ).
1321 ": records identical"
1322 unless $nowarn_identical;
1326 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
1328 "$_ = ". _quote($new->getfield($_),$old->table,$_)
1329 } real_fields($old->table)
1334 if ( $old->getfield($_) eq '' ) {
1336 #false laziness w/qsearch
1337 if ( driver_name eq 'Pg' ) {
1338 my $type = $old->dbdef_table->column($_)->type;
1339 if ( $type =~ /(int|(big)?serial)/i ) {
1342 qq-( $_ IS NULL OR $_ = '' )-;
1345 qq-( $_ IS NULL OR $_ = "" )-;
1349 "$_ = ". _quote($old->getfield($_),$old->table,$_);
1352 } ( $primary_key ? ( $primary_key ) : real_fields($old->table) )
1355 warn "[debug]$me $statement\n" if $DEBUG > 1;
1356 my $sth = dbh->prepare($statement) or return dbh->errstr;
1359 if ( defined dbdef->table('h_'. $old->table) ) {
1360 my $h_old_statement = $old->_h_statement('replace_old');
1361 warn "[debug]$me $h_old_statement\n" if $DEBUG > 2;
1362 $h_old_sth = dbh->prepare($h_old_statement) or return dbh->errstr;
1368 if ( defined dbdef->table('h_'. $new->table) ) {
1369 my $h_new_statement = $new->_h_statement('replace_new');
1370 warn "[debug]$me $h_new_statement\n" if $DEBUG > 2;
1371 $h_new_sth = dbh->prepare($h_new_statement) or return dbh->errstr;
1376 # For virtual fields we have three cases with different SQL
1377 # statements: add, replace, delete
1381 my (@add_vfields, @rep_vfields, @del_vfields);
1382 my $vfp = $old->vfieldpart_hashref;
1383 foreach(grep { exists($diff{$_}) } $new->virtual_fields) {
1384 if($diff{$_} eq '') {
1386 unless(@del_vfields) {
1387 my $st = "DELETE FROM virtual_field WHERE recnum = ? ".
1388 "AND vfieldpart = ?";
1389 warn "[debug]$me $st\n" if $DEBUG > 2;
1390 $v_del_sth = dbh->prepare($st) or return dbh->errstr;
1392 push @del_vfields, $_;
1393 } elsif($old->getfield($_) eq '') {
1395 unless(@add_vfields) {
1396 my $st = "INSERT INTO virtual_field (value, recnum, vfieldpart) ".
1398 warn "[debug]$me $st\n" if $DEBUG > 2;
1399 $v_add_sth = dbh->prepare($st) or return dbh->errstr;
1401 push @add_vfields, $_;
1404 unless(@rep_vfields) {
1405 my $st = "UPDATE virtual_field SET value = ? ".
1406 "WHERE recnum = ? AND vfieldpart = ?";
1407 warn "[debug]$me $st\n" if $DEBUG > 2;
1408 $v_rep_sth = dbh->prepare($st) or return dbh->errstr;
1410 push @rep_vfields, $_;
1414 local $SIG{HUP} = 'IGNORE';
1415 local $SIG{INT} = 'IGNORE';
1416 local $SIG{QUIT} = 'IGNORE';
1417 local $SIG{TERM} = 'IGNORE';
1418 local $SIG{TSTP} = 'IGNORE';
1419 local $SIG{PIPE} = 'IGNORE';
1421 my $rc = $sth->execute or return $sth->errstr;
1422 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
1423 $h_old_sth->execute or return $h_old_sth->errstr if $h_old_sth;
1424 $h_new_sth->execute or return $h_new_sth->errstr if $h_new_sth;
1426 $v_del_sth->execute($old->getfield($primary_key),
1428 or return $v_del_sth->errstr
1429 foreach(@del_vfields);
1431 $v_add_sth->execute($new->getfield($_),
1432 $old->getfield($primary_key),
1434 or return $v_add_sth->errstr
1435 foreach(@add_vfields);
1437 $v_rep_sth->execute($new->getfield($_),
1438 $old->getfield($primary_key),
1440 or return $v_rep_sth->errstr
1441 foreach(@rep_vfields);
1443 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1445 # Now that it has been saved, reset the encrypted fields so that $new
1446 # can still be used.
1447 foreach my $field (keys %{$saved}) {
1448 $new->setfield($field, $saved->{$field});
1456 my( $self ) = shift;
1457 warn "[$me] replace called with no arguments; autoloading old record\n"
1460 my $primary_key = $self->dbdef_table->primary_key;
1461 if ( $primary_key ) {
1462 $self->by_key( $self->$primary_key() ) #this is what's returned
1463 or croak "can't find ". $self->table. ".$primary_key ".
1464 $self->$primary_key();
1466 croak $self->table. " has no primary key; pass old record as argument";
1473 Depriciated (use replace instead).
1478 cluck "warning: FS::Record::rep deprecated!";
1479 replace @_; #call method in this scope
1484 Checks virtual fields (using check_blocks). Subclasses should still provide
1485 a check method to validate real fields, foreign keys, etc., and call this
1486 method via $self->SUPER::check.
1488 (FIXME: Should this method try to make sure that it I<is> being called from
1489 a subclass's check method, to keep the current semantics as far as possible?)
1494 #confess "FS::Record::check not implemented; supply one in subclass!";
1497 foreach my $field ($self->virtual_fields) {
1498 for ($self->getfield($field)) {
1499 # See notes on check_block in FS::part_virtual_field.
1500 eval $self->pvf($field)->check_block;
1502 #this is bad, probably want to follow the stack backtrace up and see
1504 my $err = "Fatal error checking $field for $self";
1506 return "$err (see log for backtrace): $@";
1509 $self->setfield($field, $_);
1515 =item process_batch_import JOB OPTIONS_HASHREF PARAMS
1517 Processes a batch import as a queued JSRPC job
1519 JOB is an FS::queue entry.
1521 OPTIONS_HASHREF can have the following keys:
1527 Table name (required).
1531 Listref of field names for static fields. They will be given values from the
1532 PARAMS hashref and passed as a "params" hashref to batch_import.
1536 Formats hashref. Keys are field names, values are listrefs that define the
1539 Each listref value can be a column name or a code reference. Coderefs are run
1540 with the row object, data and a FS::Conf object as the three parameters.
1541 For example, this coderef does the same thing as using the "columnname" string:
1544 my( $record, $data, $conf ) = @_;
1545 $record->columnname( $data );
1548 Coderefs are run after all "column name" fields are assigned.
1552 Optional format hashref of types. Keys are field names, values are "csv",
1553 "xls" or "fixedlength". Overrides automatic determination of file type
1556 =item format_headers
1558 Optional format hashref of header lines. Keys are field names, values are 0
1559 for no header, 1 to ignore the first line, or to higher numbers to ignore that
1562 =item format_sep_chars
1564 Optional format hashref of CSV sep_chars. Keys are field names, values are the
1565 CSV separation character.
1567 =item format_fixedlenth_formats
1569 Optional format hashref of fixed length format defintiions. Keys are field
1570 names, values Parse::FixedLength listrefs of field definitions.
1574 Set true to default to CSV file type if the filename does not contain a
1575 recognizable ".csv" or ".xls" extension (and type is not pre-specified by
1580 PARAMS is a base64-encoded Storable string containing the POSTed data as
1581 a hash ref. It normally contains at least one field, "uploaded files",
1582 generated by /elements/file-upload.html and containing the list of uploaded
1583 files. Currently only supports a single file named "file".
1587 use Storable qw(thaw);
1590 sub process_batch_import {
1591 my($job, $opt) = ( shift, shift );
1593 my $table = $opt->{table};
1594 my @pass_params = $opt->{params} ? @{ $opt->{params} } : ();
1595 my %formats = %{ $opt->{formats} };
1597 my $param = thaw(decode_base64(shift));
1598 warn Dumper($param) if $DEBUG;
1600 my $files = $param->{'uploaded_files'}
1601 or die "No files provided.\n";
1603 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
1605 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
1606 my $file = $dir. $files{'file'};
1611 formats => \%formats,
1612 format_types => $opt->{format_types},
1613 format_headers => $opt->{format_headers},
1614 format_sep_chars => $opt->{format_sep_chars},
1615 format_fixedlength_formats => $opt->{format_fixedlength_formats},
1616 format_xml_formats => $opt->{format_xml_formats},
1617 format_row_callbacks => $opt->{format_row_callbacks},
1622 format => $param->{format},
1623 params => { map { $_ => $param->{$_} } @pass_params },
1625 default_csv => $opt->{default_csv},
1626 postinsert_callback => $opt->{postinsert_callback},
1629 if ( $opt->{'batch_namecol'} ) {
1630 $iopt{'batch_namevalue'} = $param->{ $opt->{'batch_namecol'} };
1631 $iopt{$_} = $opt->{$_} foreach qw( batch_keycol batch_table batch_namecol );
1634 my $error = FS::Record::batch_import( \%iopt );
1638 die "$error\n" if $error;
1641 =item batch_import PARAM_HASHREF
1643 Class method for batch imports. Available params:
1649 =item format - usual way to specify import, with this format string selecting data from the formats and format_* info hashes
1655 =item format_headers
1657 =item format_sep_chars
1659 =item format_fixedlength_formats
1661 =item format_row_callbacks
1663 =item fields - Alternate way to specify import, specifying import fields directly as a listref
1665 =item preinsert_callback
1667 =item postinsert_callback
1673 FS::queue object, will be updated with progress
1679 csv, xls, fixedlength, xml
1690 warn "$me batch_import call with params: \n". Dumper($param)
1693 my $table = $param->{table};
1695 my $job = $param->{job};
1696 my $file = $param->{file};
1697 my $params = $param->{params} || {};
1699 my( $type, $header, $sep_char, $fixedlength_format,
1700 $xml_format, $row_callback, @fields );
1702 my $postinsert_callback = '';
1703 $postinsert_callback = $param->{'postinsert_callback'}
1704 if $param->{'postinsert_callback'};
1705 my $preinsert_callback = '';
1706 $preinsert_callback = $param->{'preinsert_callback'}
1707 if $param->{'preinsert_callback'};
1709 if ( $param->{'format'} ) {
1711 my $format = $param->{'format'};
1712 my $formats = $param->{formats};
1713 die "unknown format $format" unless exists $formats->{ $format };
1715 $type = $param->{'format_types'}
1716 ? $param->{'format_types'}{ $format }
1717 : $param->{type} || 'csv';
1720 $header = $param->{'format_headers'}
1721 ? $param->{'format_headers'}{ $param->{'format'} }
1724 $sep_char = $param->{'format_sep_chars'}
1725 ? $param->{'format_sep_chars'}{ $param->{'format'} }
1728 $fixedlength_format =
1729 $param->{'format_fixedlength_formats'}
1730 ? $param->{'format_fixedlength_formats'}{ $param->{'format'} }
1734 $param->{'format_xml_formats'}
1735 ? $param->{'format_xml_formats'}{ $param->{'format'} }
1739 $param->{'format_row_callbacks'}
1740 ? $param->{'format_row_callbacks'}{ $param->{'format'} }
1743 @fields = @{ $formats->{ $format } };
1745 } elsif ( $param->{'fields'} ) {
1747 $type = ''; #infer from filename
1750 $fixedlength_format = '';
1752 @fields = @{ $param->{'fields'} };
1755 die "neither format nor fields specified";
1758 #my $file = $param->{file};
1761 if ( $file =~ /\.(\w+)$/i ) {
1765 warn "can't parse file type from filename $file; defaulting to CSV";
1769 if $param->{'default_csv'} && $type ne 'xls';
1777 if ( $type eq 'csv' || $type eq 'fixedlength' ) {
1779 if ( $type eq 'csv' ) {
1782 $attr{sep_char} = $sep_char if $sep_char;
1783 $parser = new Text::CSV_XS \%attr;
1785 } elsif ( $type eq 'fixedlength' ) {
1787 eval "use Parse::FixedLength;";
1789 $parser = Parse::FixedLength->new($fixedlength_format);
1793 die "Unknown file type $type\n";
1796 @buffer = split(/\r?\n/, slurp($file) );
1797 splice(@buffer, 0, ($header || 0) );
1798 $count = scalar(@buffer);
1800 } elsif ( $type eq 'xls' ) {
1802 eval "use Spreadsheet::ParseExcel;";
1805 eval "use DateTime::Format::Excel;";
1806 #for now, just let the error be thrown if it is used, since only CDR
1807 # formats bill_west and troop use it, not other excel-parsing things
1810 my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($file);
1812 $parser = $excel->{Worksheet}[0]; #first sheet
1814 $count = $parser->{MaxRow} || $parser->{MinRow};
1817 $row = $header || 0;
1818 } elsif ( $type eq 'xml' ) {
1820 eval "use XML::Simple;";
1822 my $xmlrow = $xml_format->{'xmlrow'};
1823 $parser = $xml_format->{'xmlkeys'};
1824 die 'no xmlkeys specified' unless ref $parser eq 'ARRAY';
1825 my $data = XML::Simple::XMLin(
1827 'SuppressEmpty' => '', #sets empty values to ''
1831 $rows = $rows->{$_} foreach @$xmlrow;
1832 $rows = [ $rows ] if ref($rows) ne 'ARRAY';
1833 $count = @buffer = @$rows;
1835 die "Unknown file type $type\n";
1840 local $SIG{HUP} = 'IGNORE';
1841 local $SIG{INT} = 'IGNORE';
1842 local $SIG{QUIT} = 'IGNORE';
1843 local $SIG{TERM} = 'IGNORE';
1844 local $SIG{TSTP} = 'IGNORE';
1845 local $SIG{PIPE} = 'IGNORE';
1847 my $oldAutoCommit = $FS::UID::AutoCommit;
1848 local $FS::UID::AutoCommit = 0;
1851 #my $params = $param->{params} || {};
1852 if ( $param->{'batch_namecol'} && $param->{'batch_namevalue'} ) {
1853 my $batch_col = $param->{'batch_keycol'};
1855 my $batch_class = 'FS::'. $param->{'batch_table'};
1856 my $batch = $batch_class->new({
1857 $param->{'batch_namecol'} => $param->{'batch_namevalue'}
1859 my $error = $batch->insert;
1861 $dbh->rollback if $oldAutoCommit;
1862 return "can't insert batch record: $error";
1864 #primary key via dbdef? (so the column names don't have to match)
1865 my $batch_value = $batch->get( $param->{'batch_keycol'} );
1867 $params->{ $batch_col } = $batch_value;
1870 #my $job = $param->{job};
1873 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
1877 if ( $type eq 'csv' ) {
1879 last unless scalar(@buffer);
1880 $line = shift(@buffer);
1882 next if $line =~ /^\s*$/; #skip empty lines
1884 $line = &{$row_callback}($line) if $row_callback;
1886 next if $line =~ /^\s*$/; #skip empty lines
1888 $parser->parse($line) or do {
1889 $dbh->rollback if $oldAutoCommit;
1890 return "can't parse: ". $parser->error_input() . " " . $parser->error_diag;
1892 @columns = $parser->fields();
1894 } elsif ( $type eq 'fixedlength' ) {
1896 last unless scalar(@buffer);
1897 $line = shift(@buffer);
1899 @columns = $parser->parse($line);
1901 } elsif ( $type eq 'xls' ) {
1903 last if $row > ($parser->{MaxRow} || $parser->{MinRow})
1904 || ! $parser->{Cells}[$row];
1906 my @row = @{ $parser->{Cells}[$row] };
1907 @columns = map $_->{Val}, @row;
1910 #warn $z++. ": $_\n" for @columns;
1912 } elsif ( $type eq 'xml' ) {
1913 # $parser = [ 'Column0Key', 'Column1Key' ... ]
1914 last unless scalar(@buffer);
1915 my $row = shift @buffer;
1916 @columns = @{ $row }{ @$parser };
1918 die "Unknown file type $type\n";
1922 my %hash = %$params;
1924 foreach my $field ( @fields ) {
1926 my $value = shift @columns;
1928 if ( ref($field) eq 'CODE' ) {
1929 #&{$field}(\%hash, $value);
1930 push @later, $field, $value;
1932 #??? $hash{$field} = $value if length($value);
1933 $hash{$field} = $value if defined($value) && length($value);
1938 #my $table = $param->{table};
1939 my $class = "FS::$table";
1941 my $record = $class->new( \%hash );
1944 while ( scalar(@later) ) {
1945 my $sub = shift @later;
1946 my $data = shift @later;
1948 &{$sub}($record, $data, $conf, $param); # $record->&{$sub}($data, $conf)
1951 $dbh->rollback if $oldAutoCommit;
1952 return "can't insert record". ( $line ? " for $line" : '' ). ": $@";
1954 last if exists( $param->{skiprow} );
1956 next if exists( $param->{skiprow} );
1958 if ( $preinsert_callback ) {
1959 my $error = &{$preinsert_callback}($record, $param);
1961 $dbh->rollback if $oldAutoCommit;
1962 return "preinsert_callback error". ( $line ? " for $line" : '' ).
1965 next if exists $param->{skiprow} && $param->{skiprow};
1968 my $error = $record->insert;
1971 $dbh->rollback if $oldAutoCommit;
1972 return "can't insert record". ( $line ? " for $line" : '' ). ": $error";
1978 if ( $postinsert_callback ) {
1979 my $error = &{$postinsert_callback}($record, $param);
1981 $dbh->rollback if $oldAutoCommit;
1982 return "postinsert_callback error". ( $line ? " for $line" : '' ).
1987 if ( $job && time - $min_sec > $last ) { #progress bar
1988 $job->update_statustext( int(100 * $imported / $count) );
1994 unless ( $imported || $param->{empty_ok} ) {
1995 $dbh->rollback if $oldAutoCommit;
1996 return "Empty file!";
1999 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
2006 my( $self, $action, $time ) = @_;
2010 my %nohistory = map { $_=>1 } $self->nohistory_fields;
2013 grep { defined($self->get($_)) && $self->get($_) ne "" && ! $nohistory{$_} }
2014 real_fields($self->table);
2017 # If we're encrypting then don't store the payinfo in the history
2018 if ( $conf && $conf->exists('encryption') && $self->table ne 'banned_pay' ) {
2019 @fields = grep { $_ ne 'payinfo' } @fields;
2022 my @values = map { _quote( $self->getfield($_), $self->table, $_) } @fields;
2024 "INSERT INTO h_". $self->table. " ( ".
2025 join(', ', qw(history_date history_user history_action), @fields ).
2027 join(', ', $time, dbh->quote(getotaker()), dbh->quote($action), @values).
2034 B<Warning>: External use is B<deprecated>.
2036 Replaces COLUMN in record with a unique number, using counters in the
2037 filesystem. Used by the B<insert> method on single-field unique columns
2038 (see L<DBIx::DBSchema::Table>) and also as a fallback for primary keys
2039 that aren't SERIAL (Pg) or AUTO_INCREMENT (mysql).
2041 Returns the new value.
2046 my($self,$field) = @_;
2047 my($table)=$self->table;
2049 croak "Unique called on field $field, but it is ",
2050 $self->getfield($field),
2052 if $self->getfield($field);
2054 #warn "table $table is tainted" if is_tainted($table);
2055 #warn "field $field is tainted" if is_tainted($field);
2057 my($counter) = new File::CounterFile "$table.$field",0;
2059 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
2061 # my($counter) = new File::CounterFile "$user/$table.$field",0;
2064 my $index = $counter->inc;
2065 $index = $counter->inc while qsearchs($table, { $field=>$index } );
2067 $index =~ /^(\d*)$/;
2070 $self->setfield($field,$index);
2074 =item ut_float COLUMN
2076 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
2077 null. If there is an error, returns the error, otherwise returns false.
2082 my($self,$field)=@_ ;
2083 ($self->getfield($field) =~ /^\s*(\d+\.\d+)\s*$/ ||
2084 $self->getfield($field) =~ /^\s*(\d+)\s*$/ ||
2085 $self->getfield($field) =~ /^\s*(\d+\.\d+e\d+)\s*$/ ||
2086 $self->getfield($field) =~ /^\s*(\d+e\d+)\s*$/)
2087 or return "Illegal or empty (float) $field: ". $self->getfield($field);
2088 $self->setfield($field,$1);
2091 =item ut_floatn COLUMN
2093 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
2094 null. If there is an error, returns the error, otherwise returns false.
2098 #false laziness w/ut_ipn
2100 my( $self, $field ) = @_;
2101 if ( $self->getfield($field) =~ /^()$/ ) {
2102 $self->setfield($field,'');
2105 $self->ut_float($field);
2109 =item ut_sfloat COLUMN
2111 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10.
2112 May not be null. If there is an error, returns the error, otherwise returns
2118 my($self,$field)=@_ ;
2119 ($self->getfield($field) =~ /^\s*(-?\d+\.\d+)\s*$/ ||
2120 $self->getfield($field) =~ /^\s*(-?\d+)\s*$/ ||
2121 $self->getfield($field) =~ /^\s*(-?\d+\.\d+[eE]-?\d+)\s*$/ ||
2122 $self->getfield($field) =~ /^\s*(-?\d+[eE]-?\d+)\s*$/)
2123 or return "Illegal or empty (float) $field: ". $self->getfield($field);
2124 $self->setfield($field,$1);
2127 =item ut_sfloatn COLUMN
2129 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
2130 null. If there is an error, returns the error, otherwise returns false.
2135 my( $self, $field ) = @_;
2136 if ( $self->getfield($field) =~ /^()$/ ) {
2137 $self->setfield($field,'');
2140 $self->ut_sfloat($field);
2144 =item ut_snumber COLUMN
2146 Check/untaint signed numeric data (whole numbers). If there is an error,
2147 returns the error, otherwise returns false.
2152 my($self, $field) = @_;
2153 $self->getfield($field) =~ /^\s*(-?)\s*(\d+)\s*$/
2154 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
2155 $self->setfield($field, "$1$2");
2159 =item ut_snumbern COLUMN
2161 Check/untaint signed numeric data (whole numbers). If there is an error,
2162 returns the error, otherwise returns false.
2167 my($self, $field) = @_;
2168 $self->getfield($field) =~ /^\s*(-?)\s*(\d*)\s*$/
2169 or return "Illegal (numeric) $field: ". $self->getfield($field);
2171 return "Illegal (numeric) $field: ". $self->getfield($field)
2174 $self->setfield($field, "$1$2");
2178 =item ut_number COLUMN
2180 Check/untaint simple numeric data (whole numbers). May not be null. If there
2181 is an error, returns the error, otherwise returns false.
2186 my($self,$field)=@_;
2187 $self->getfield($field) =~ /^\s*(\d+)\s*$/
2188 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
2189 $self->setfield($field,$1);
2193 =item ut_numbern COLUMN
2195 Check/untaint simple numeric data (whole numbers). May be null. If there is
2196 an error, returns the error, otherwise returns false.
2201 my($self,$field)=@_;
2202 $self->getfield($field) =~ /^\s*(\d*)\s*$/
2203 or return "Illegal (numeric) $field: ". $self->getfield($field);
2204 $self->setfield($field,$1);
2208 =item ut_money COLUMN
2210 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
2211 is an error, returns the error, otherwise returns false.
2216 my($self,$field)=@_;
2217 $self->setfield($field, 0) if $self->getfield($field) eq '';
2218 $self->getfield($field) =~ /^\s*(\-)?\s*(\d*)(\.\d{2})?\s*$/
2219 or return "Illegal (money) $field: ". $self->getfield($field);
2220 #$self->setfield($field, "$1$2$3" || 0);
2221 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
2225 =item ut_moneyn COLUMN
2227 Check/untaint monetary numbers. May be negative. If there
2228 is an error, returns the error, otherwise returns false.
2233 my($self,$field)=@_;
2234 if ($self->getfield($field) eq '') {
2235 $self->setfield($field, '');
2238 $self->ut_money($field);
2241 =item ut_text COLUMN
2243 Check/untaint text. Alphanumerics, spaces, and the following punctuation
2244 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / = [ ] < >
2245 May not be null. If there is an error, returns the error, otherwise returns
2251 my($self,$field)=@_;
2252 #warn "msgcat ". \&msgcat. "\n";
2253 #warn "notexist ". \¬exist. "\n";
2254 #warn "AUTOLOAD ". \&AUTOLOAD. "\n";
2255 $self->getfield($field)
2256 =~ /^([\wô \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]\<\>$money_char]+)$/
2257 or return gettext('illegal_or_empty_text'). " $field: ".
2258 $self->getfield($field);
2259 $self->setfield($field,$1);
2263 =item ut_textn COLUMN
2265 Check/untaint text. Alphanumerics, spaces, and the following punctuation
2266 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / = [ ] < >
2267 May be null. If there is an error, returns the error, otherwise returns false.
2272 my($self,$field)=@_;
2273 return $self->setfield($field, '') if $self->getfield($field) =~ /^$/;
2274 $self->ut_text($field);
2277 =item ut_alpha COLUMN
2279 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
2280 an error, returns the error, otherwise returns false.
2285 my($self,$field)=@_;
2286 $self->getfield($field) =~ /^(\w+)$/
2287 or return "Illegal or empty (alphanumeric) $field: ".
2288 $self->getfield($field);
2289 $self->setfield($field,$1);
2293 =item ut_alphan COLUMN
2295 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
2296 error, returns the error, otherwise returns false.
2301 my($self,$field)=@_;
2302 $self->getfield($field) =~ /^(\w*)$/
2303 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
2304 $self->setfield($field,$1);
2308 =item ut_alphasn COLUMN
2310 Check/untaint alphanumeric strings, spaces allowed. May be null. If there is
2311 an error, returns the error, otherwise returns false.
2316 my($self,$field)=@_;
2317 $self->getfield($field) =~ /^([\w ]*)$/
2318 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
2319 $self->setfield($field,$1);
2324 =item ut_alpha_lower COLUMN
2326 Check/untaint lowercase alphanumeric strings (no spaces). May not be null. If
2327 there is an error, returns the error, otherwise returns false.
2331 sub ut_alpha_lower {
2332 my($self,$field)=@_;
2333 $self->getfield($field) =~ /[[:upper:]]/
2334 and return "Uppercase characters are not permitted in $field";
2335 $self->ut_alpha($field);
2338 =item ut_phonen COLUMN [ COUNTRY ]
2340 Check/untaint phone numbers. May be null. If there is an error, returns
2341 the error, otherwise returns false.
2343 Takes an optional two-letter ISO country code; without it or with unsupported
2344 countries, ut_phonen simply calls ut_alphan.
2349 my( $self, $field, $country ) = @_;
2350 return $self->ut_alphan($field) unless defined $country;
2351 my $phonen = $self->getfield($field);
2352 if ( $phonen eq '' ) {
2353 $self->setfield($field,'');
2354 } elsif ( $country eq 'US' || $country eq 'CA' ) {
2356 $phonen = $conf->config('cust_main-default_areacode').$phonen
2357 if length($phonen)==7 && $conf->config('cust_main-default_areacode');
2358 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
2359 or return gettext('illegal_phone'). " $field: ". $self->getfield($field);
2360 $phonen = "$1-$2-$3";
2361 $phonen .= " x$4" if $4;
2362 $self->setfield($field,$phonen);
2364 warn "warning: don't know how to check phone numbers for country $country";
2365 return $self->ut_textn($field);
2372 Check/untaint hexadecimal values.
2377 my($self, $field) = @_;
2378 $self->getfield($field) =~ /^([\da-fA-F]+)$/
2379 or return "Illegal (hex) $field: ". $self->getfield($field);
2380 $self->setfield($field, uc($1));
2384 =item ut_hexn COLUMN
2386 Check/untaint hexadecimal values. May be null.
2391 my($self, $field) = @_;
2392 $self->getfield($field) =~ /^([\da-fA-F]*)$/
2393 or return "Illegal (hex) $field: ". $self->getfield($field);
2394 $self->setfield($field, uc($1));
2398 =item ut_mac_addr COLUMN
2400 Check/untaint mac addresses. May be null.
2405 my($self, $field) = @_;
2407 my $mac = $self->get($field);
2410 $self->set($field, $mac);
2412 my $e = $self->ut_hex($field);
2415 return "Illegal (mac address) $field: ". $self->getfield($field)
2416 unless length($self->getfield($field)) == 12;
2422 =item ut_mac_addrn COLUMN
2424 Check/untaint mac addresses. May be null.
2429 my($self, $field) = @_;
2430 ($self->getfield($field) eq '') ? '' : $self->ut_mac_addr($field);
2435 Check/untaint ip addresses. IPv4 only for now, though ::1 is auto-translated
2441 my( $self, $field ) = @_;
2442 $self->setfield($field, '127.0.0.1') if $self->getfield($field) eq '::1';
2443 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
2444 or return "Illegal (IP address) $field: ". $self->getfield($field);
2445 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
2446 $self->setfield($field, "$1.$2.$3.$4");
2452 Check/untaint ip addresses. IPv4 only for now, though ::1 is auto-translated
2453 to 127.0.0.1. May be null.
2458 my( $self, $field ) = @_;
2459 if ( $self->getfield($field) =~ /^()$/ ) {
2460 $self->setfield($field,'');
2463 $self->ut_ip($field);
2467 =item ut_ip46 COLUMN
2469 Check/untaint IPv4 or IPv6 address.
2474 my( $self, $field ) = @_;
2475 my $ip = NetAddr::IP->new($self->getfield($field))
2476 or return "Illegal (IP address) $field: ".$self->getfield($field);
2477 $self->setfield($field, lc($ip->addr));
2483 Check/untaint IPv6 or IPv6 address. May be null.
2488 my( $self, $field ) = @_;
2489 if ( $self->getfield($field) =~ /^$/ ) {
2490 $self->setfield($field, '');
2493 $self->ut_ip46($field);
2496 =item ut_coord COLUMN [ LOWER [ UPPER ] ]
2498 Check/untaint coordinates.
2499 Accepts the following forms:
2509 The "DDD MM SS" and "DDD MM MMM" are potentially ambiguous.
2510 The latter form (that is, the MMM are thousands of minutes) is
2511 assumed if the "MMM" is exactly three digits or two digits > 59.
2513 To be safe, just use the DDD.DDDDD form.
2515 If LOWER or UPPER are specified, then the coordinate is checked
2516 for lower and upper bounds, respectively.
2521 my ($self, $field) = (shift, shift);
2524 if ( $field =~ /latitude/ ) {
2525 $lower = $lat_lower;
2527 } elsif ( $field =~ /longitude/ ) {
2529 $upper = $lon_upper;
2532 my $coord = $self->getfield($field);
2533 my $neg = $coord =~ s/^(-)//;
2535 my ($d, $m, $s) = (0, 0, 0);
2538 (($d) = ($coord =~ /^(\s*\d{1,3}(?:\.\d+)?)\s*$/)) ||
2539 (($d, $m) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2}(?:\.\d+))\s*$/)) ||
2540 (($d, $m, $s) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2})\s+(\d{1,3})\s*$/))
2542 $s = (((($s =~ /^\d{3}$/) or $s > 59) ? ($s / 1000) : ($s / 60)) / 60);
2545 return "Invalid (coordinate with minutes > 59) $field: "
2546 . $self->getfield($field);
2549 $coord = ($neg ? -1 : 1) * sprintf('%.8f', $d + $m + $s);
2551 if (defined($lower) and ($coord < $lower)) {
2552 return "Invalid (coordinate < $lower) $field: "
2553 . $self->getfield($field);;
2556 if (defined($upper) and ($coord > $upper)) {
2557 return "Invalid (coordinate > $upper) $field: "
2558 . $self->getfield($field);;
2561 $self->setfield($field, $coord);
2565 return "Invalid (coordinate) $field: " . $self->getfield($field);
2569 =item ut_coordn COLUMN [ LOWER [ UPPER ] ]
2571 Same as ut_coord, except optionally null.
2577 my ($self, $field) = (shift, shift);
2579 if ($self->getfield($field) =~ /^\s*$/) {
2582 return $self->ut_coord($field, @_);
2588 =item ut_domain COLUMN
2590 Check/untaint host and domain names.
2595 my( $self, $field ) = @_;
2596 #$self->getfield($field) =~/^(\w+\.)*\w+$/
2597 $self->getfield($field) =~/^(([\w\-]+\.)*\w+)$/
2598 or return "Illegal (domain) $field: ". $self->getfield($field);
2599 $self->setfield($field,$1);
2603 =item ut_name COLUMN
2605 Check/untaint proper names; allows alphanumerics, spaces and the following
2606 punctuation: , . - '
2613 my( $self, $field ) = @_;
2614 # warn "ut_name allowed alphanumerics: +(sort grep /\w/, map { chr() } 0..255), "\n";
2615 $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
2616 or return gettext('illegal_name'). " $field: ". $self->getfield($field);
2617 $self->setfield($field,$1);
2623 Check/untaint zip codes.
2627 my @zip_reqd_countries = qw( AU CA US ); #CA, US implicit...
2630 my( $self, $field, $country ) = @_;
2632 if ( $country eq 'US' ) {
2634 $self->getfield($field) =~ /^\s*(\d{5}(\-\d{4})?)\s*$/
2635 or return gettext('illegal_zip'). " $field for country $country: ".
2636 $self->getfield($field);
2637 $self->setfield($field, $1);
2639 } elsif ( $country eq 'CA' ) {
2641 $self->getfield($field) =~ /^\s*([A-Z]\d[A-Z])\s*(\d[A-Z]\d)\s*$/i
2642 or return gettext('illegal_zip'). " $field for country $country: ".
2643 $self->getfield($field);
2644 $self->setfield($field, "$1 $2");
2648 if ( $self->getfield($field) =~ /^\s*$/
2649 && ( !$country || ! grep { $_ eq $country } @zip_reqd_countries )
2652 $self->setfield($field,'');
2654 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{0,8}\w)\s*$/
2655 or return gettext('illegal_zip'). " $field: ". $self->getfield($field);
2656 $self->setfield($field,$1);
2664 =item ut_country COLUMN
2666 Check/untaint country codes. Country names are changed to codes, if possible -
2667 see L<Locale::Country>.
2672 my( $self, $field ) = @_;
2673 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
2674 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
2675 && country2code($1) ) {
2676 $self->setfield($field,uc(country2code($1)));
2679 $self->getfield($field) =~ /^(\w\w)$/
2680 or return "Illegal (country) $field: ". $self->getfield($field);
2681 $self->setfield($field,uc($1));
2685 =item ut_anything COLUMN
2687 Untaints arbitrary data. Be careful.
2692 my( $self, $field ) = @_;
2693 $self->getfield($field) =~ /^(.*)$/s
2694 or return "Illegal $field: ". $self->getfield($field);
2695 $self->setfield($field,$1);
2699 =item ut_enum COLUMN CHOICES_ARRAYREF
2701 Check/untaint a column, supplying all possible choices, like the "enum" type.
2706 my( $self, $field, $choices ) = @_;
2707 foreach my $choice ( @$choices ) {
2708 if ( $self->getfield($field) eq $choice ) {
2709 $self->setfield($field, $choice);
2713 return "Illegal (enum) field $field: ". $self->getfield($field);
2716 =item ut_enumn COLUMN CHOICES_ARRAYREF
2718 Like ut_enum, except the null value is also allowed.
2723 my( $self, $field, $choices ) = @_;
2724 $self->getfield($field)
2725 ? $self->ut_enum($field, $choices)
2730 =item ut_foreign_key COLUMN FOREIGN_TABLE FOREIGN_COLUMN
2732 Check/untaint a foreign column key. Call a regular ut_ method (like ut_number)
2733 on the column first.
2737 sub ut_foreign_key {
2738 my( $self, $field, $table, $foreign ) = @_;
2739 return '' if $no_check_foreign;
2740 qsearchs($table, { $foreign => $self->getfield($field) })
2741 or return "Can't find ". $self->table. ".$field ". $self->getfield($field).
2742 " in $table.$foreign";
2746 =item ut_foreign_keyn COLUMN FOREIGN_TABLE FOREIGN_COLUMN
2748 Like ut_foreign_key, except the null value is also allowed.
2752 sub ut_foreign_keyn {
2753 my( $self, $field, $table, $foreign ) = @_;
2754 $self->getfield($field)
2755 ? $self->ut_foreign_key($field, $table, $foreign)
2759 =item ut_agentnum_acl COLUMN [ NULL_RIGHT | NULL_RIGHT_LISTREF ]
2761 Checks this column as an agentnum, taking into account the current users's
2762 ACLs. NULL_RIGHT or NULL_RIGHT_LISTREF, if specified, indicates the access
2763 right or rights allowing no agentnum.
2767 sub ut_agentnum_acl {
2768 my( $self, $field ) = (shift, shift);
2769 my $null_acl = scalar(@_) ? shift : [];
2770 $null_acl = [ $null_acl ] unless ref($null_acl);
2772 my $error = $self->ut_foreign_keyn($field, 'agent', 'agentnum');
2773 return "Illegal agentnum: $error" if $error;
2775 my $curuser = $FS::CurrentUser::CurrentUser;
2777 if ( $self->$field() ) {
2779 return "Access denied"
2780 unless $curuser->agentnum($self->$field());
2784 return "Access denied"
2785 unless grep $curuser->access_right($_), @$null_acl;
2793 =item virtual_fields [ TABLE ]
2795 Returns a list of virtual fields defined for the table. This should not
2796 be exported, and should only be called as an instance or class method.
2800 sub virtual_fields {
2803 $table = $self->table or confess "virtual_fields called on non-table";
2805 confess "Unknown table $table" unless dbdef->table($table);
2807 return () unless dbdef->table('part_virtual_field');
2809 unless ( $virtual_fields_cache{$table} ) {
2810 my $query = 'SELECT name from part_virtual_field ' .
2811 "WHERE dbtable = '$table'";
2813 my $result = $dbh->selectcol_arrayref($query);
2814 confess "Error executing virtual fields query: $query: ". $dbh->errstr
2816 $virtual_fields_cache{$table} = $result;
2819 @{$virtual_fields_cache{$table}};
2824 =item fields [ TABLE ]
2826 This is a wrapper for real_fields and virtual_fields. Code that called
2827 fields before should probably continue to call fields.
2832 my $something = shift;
2834 if($something->isa('FS::Record')) {
2835 $table = $something->table;
2837 $table = $something;
2838 $something = "FS::$table";
2840 return (real_fields($table), $something->virtual_fields());
2843 =item pvf FIELD_NAME
2845 Returns the FS::part_virtual_field object corresponding to a field in the
2846 record (specified by FIELD_NAME).
2851 my ($self, $name) = (shift, shift);
2853 if(grep /^$name$/, $self->virtual_fields) {
2854 return qsearchs('part_virtual_field', { dbtable => $self->table,
2860 =item vfieldpart_hashref TABLE
2862 Returns a hashref of virtual field names and vfieldparts applicable to the given
2867 sub vfieldpart_hashref {
2869 my $table = $self->table;
2871 return {} unless dbdef->table('part_virtual_field');
2874 my $statement = "SELECT vfieldpart, name FROM part_virtual_field WHERE ".
2875 "dbtable = '$table'";
2876 my $sth = $dbh->prepare($statement);
2877 $sth->execute or croak "Execution of '$statement' failed: ".$dbh->errstr;
2878 return { map { $_->{name}, $_->{vfieldpart} }
2879 @{$sth->fetchall_arrayref({})} };
2883 =item encrypt($value)
2885 Encrypts the credit card using a combination of PK to encrypt and uuencode to armour.
2887 Returns the encrypted string.
2889 You should generally not have to worry about calling this, as the system handles this for you.
2894 my ($self, $value) = @_;
2897 if ($conf->exists('encryption')) {
2898 if ($self->is_encrypted($value)) {
2899 # Return the original value if it isn't plaintext.
2900 $encrypted = $value;
2903 if (ref($rsa_encrypt) =~ /::RSA/) { # We Can Encrypt
2904 # RSA doesn't like the empty string so let's pack it up
2905 # The database doesn't like the RSA data so uuencode it
2906 my $length = length($value)+1;
2907 $encrypted = pack("u*",$rsa_encrypt->encrypt(pack("Z$length",$value)));
2909 die ("You can't encrypt w/o a valid RSA engine - Check your installation or disable encryption");
2916 =item is_encrypted($value)
2918 Checks to see if the string is encrypted and returns true or false (1/0) to indicate it's status.
2924 my ($self, $value) = @_;
2925 # Possible Bug - Some work may be required here....
2927 if ($value =~ /^M/ && length($value) > 80) {
2934 =item decrypt($value)
2936 Uses the private key to decrypt the string. Returns the decryoted string or undef on failure.
2938 You should generally not have to worry about calling this, as the system handles this for you.
2943 my ($self,$value) = @_;
2944 my $decrypted = $value; # Will return the original value if it isn't encrypted or can't be decrypted.
2945 if ($conf->exists('encryption') && $self->is_encrypted($value)) {
2947 if (ref($rsa_decrypt) =~ /::RSA/) {
2948 my $encrypted = unpack ("u*", $value);
2949 $decrypted = unpack("Z*", eval{$rsa_decrypt->decrypt($encrypted)});
2950 if ($@) {warn "Decryption Failed"};
2958 #Initialize the Module
2959 $rsa_module = 'Crypt::OpenSSL::RSA'; # The Default
2961 if ($conf->exists('encryptionmodule') && $conf->config('encryptionmodule') ne '') {
2962 $rsa_module = $conf->config('encryptionmodule');
2966 eval ("require $rsa_module"); # No need to import the namespace
2969 # Initialize Encryption
2970 if ($conf->exists('encryptionpublickey') && $conf->config('encryptionpublickey') ne '') {
2971 my $public_key = join("\n",$conf->config('encryptionpublickey'));
2972 $rsa_encrypt = $rsa_module->new_public_key($public_key);
2975 # Intitalize Decryption
2976 if ($conf->exists('encryptionprivatekey') && $conf->config('encryptionprivatekey') ne '') {
2977 my $private_key = join("\n",$conf->config('encryptionprivatekey'));
2978 $rsa_decrypt = $rsa_module->new_private_key($private_key);
2982 =item h_search ACTION
2984 Given an ACTION, either "insert", or "delete", returns the appropriate history
2985 record corresponding to this record, if any.
2990 my( $self, $action ) = @_;
2992 my $table = $self->table;
2995 my $primary_key = dbdef->table($table)->primary_key;
2998 'table' => "h_$table",
2999 'hashref' => { $primary_key => $self->$primary_key(),
3000 'history_action' => $action,
3008 Given an ACTION, either "insert", or "delete", returns the timestamp of the
3009 appropriate history record corresponding to this record, if any.
3014 my($self, $action) = @_;
3015 my $h = $self->h_search($action);
3016 $h ? $h->history_date : '';
3019 =item scalar_sql SQL [ PLACEHOLDER, ... ]
3021 A class or object method. Executes the sql statement represented by SQL and
3022 returns a scalar representing the result: the first column of the first row.
3024 Dies on bogus SQL. Returns an empty string if no row is returned.
3026 Typically used for statments which return a single value such as "SELECT
3027 COUNT(*) FROM table WHERE something" OR "SELECT column FROM table WHERE key = ?"
3032 my($self, $sql) = (shift, shift);
3033 my $sth = dbh->prepare($sql) or die dbh->errstr;
3035 or die "Unexpected error executing statement $sql: ". $sth->errstr;
3036 my $row = $sth->fetchrow_arrayref or return '';
3037 my $scalar = $row->[0];
3038 defined($scalar) ? $scalar : '';
3047 =item real_fields [ TABLE ]
3049 Returns a list of the real columns in the specified table. Called only by
3050 fields() and other subroutines elsewhere in FS::Record.
3057 my($table_obj) = dbdef->table($table);
3058 confess "Unknown table $table" unless $table_obj;
3059 $table_obj->columns;
3062 =item _quote VALUE, TABLE, COLUMN
3064 This is an internal function used to construct SQL statements. It returns
3065 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
3066 type (see L<DBIx::DBSchema::Column>) does not end in `char' or `binary'.
3071 my($value, $table, $column) = @_;
3072 my $column_obj = dbdef->table($table)->column($column);
3073 my $column_type = $column_obj->type;
3074 my $nullable = $column_obj->null;
3076 warn " $table.$column: $value ($column_type".
3077 ( $nullable ? ' NULL' : ' NOT NULL' ).
3078 ")\n" if $DEBUG > 2;
3080 if ( $value eq '' && $nullable ) {
3082 } elsif ( $value eq '' && $column_type =~ /^(int|numeric)/ ) {
3083 cluck "WARNING: Attempting to set non-null integer $table.$column null; ".
3086 } elsif ( $value =~ /^\d+(\.\d+)?$/ &&
3087 ! $column_type =~ /(char|binary|text)$/i ) {
3089 } elsif (( $column_type =~ /^bytea$/i || $column_type =~ /(blob|varbinary)/i )
3090 && driver_name eq 'Pg'
3094 # dbh->quote($value, { pg_type => PG_BYTEA() }); # doesn't work right
3095 # Pg binary string quoting: convert each character to 3-digit octal prefixed with \\,
3096 # single-quote the whole mess, and put an "E" in front.
3097 return ("E'" . join('', map { sprintf('\\\\%03o', ord($_)) } split(//, $value) ) . "'");
3105 This is deprecated. Don't use it.
3107 It returns a hash-type list with the fields of this record's table set true.
3112 carp "warning: hfields is deprecated";
3115 foreach (fields($table)) {
3124 "$_: ". $self->getfield($_). "|"
3125 } (fields($self->table)) );
3128 sub DESTROY { return; }
3132 # #use Carp qw(cluck);
3133 # #cluck "DESTROYING $self";
3134 # warn "DESTROYING $self";
3138 # return ! eval { join('',@_), kill 0; 1; };
3141 =item str2time_sql [ DRIVER_NAME ]
3143 Returns a function to convert to unix time based on database type, such as
3144 "EXTRACT( EPOCH FROM" for Pg or "UNIX_TIMESTAMP(" for mysql. See
3145 the str2time_sql_closing method to return a closing string rather than just
3146 using a closing parenthesis as previously suggested.
3148 You can pass an optional driver name such as "Pg", "mysql" or
3149 $dbh->{Driver}->{Name} to return a function for that database instead of
3150 the current database.
3155 my $driver = shift || driver_name;
3157 return 'UNIX_TIMESTAMP(' if $driver =~ /^mysql/i;
3158 return 'EXTRACT( EPOCH FROM ' if $driver =~ /^Pg/i;
3160 warn "warning: unknown database type $driver; guessing how to convert ".
3161 "dates to UNIX timestamps";
3162 return 'EXTRACT(EPOCH FROM ';
3166 =item str2time_sql_closing [ DRIVER_NAME ]
3168 Returns the closing suffix of a function to convert to unix time based on
3169 database type, such as ")::integer" for Pg or ")" for mysql.
3171 You can pass an optional driver name such as "Pg", "mysql" or
3172 $dbh->{Driver}->{Name} to return a function for that database instead of
3173 the current database.
3177 sub str2time_sql_closing {
3178 my $driver = shift || driver_name;
3180 return ' )::INTEGER ' if $driver =~ /^Pg/i;
3184 =item regexp_sql [ DRIVER_NAME ]
3186 Returns the operator to do a regular expression comparison based on database
3187 type, such as '~' for Pg or 'REGEXP' for mysql.
3189 You can pass an optional driver name such as "Pg", "mysql" or
3190 $dbh->{Driver}->{Name} to return a function for that database instead of
3191 the current database.
3196 my $driver = shift || driver_name;
3198 return '~' if $driver =~ /^Pg/i;
3199 return 'REGEXP' if $driver =~ /^mysql/i;
3201 die "don't know how to use regular expressions in ". driver_name." databases";
3205 =item not_regexp_sql [ DRIVER_NAME ]
3207 Returns the operator to do a regular expression negation based on database
3208 type, such as '!~' for Pg or 'NOT REGEXP' for mysql.
3210 You can pass an optional driver name such as "Pg", "mysql" or
3211 $dbh->{Driver}->{Name} to return a function for that database instead of
3212 the current database.
3216 sub not_regexp_sql {
3217 my $driver = shift || driver_name;
3219 return '!~' if $driver =~ /^Pg/i;
3220 return 'NOT REGEXP' if $driver =~ /^mysql/i;
3222 die "don't know how to use regular expressions in ". driver_name." databases";
3226 =item concat_sql [ DRIVER_NAME ] ITEMS_ARRAYREF
3228 Returns the items concatendated based on database type, using "CONCAT()" for
3229 mysql and " || " for Pg and other databases.
3231 You can pass an optional driver name such as "Pg", "mysql" or
3232 $dbh->{Driver}->{Name} to return a function for that database instead of
3233 the current database.
3238 my $driver = ref($_[0]) ? driver_name : shift;
3241 if ( $driver =~ /^mysql/i ) {
3242 'CONCAT('. join(',', @$items). ')';
3244 join('||', @$items);
3253 This module should probably be renamed, since much of the functionality is
3254 of general use. It is not completely unlike Adapter::DBI (see below).
3256 Exported qsearch and qsearchs should be deprecated in favor of method calls
3257 (against an FS::Record object like the old search and searchs that qsearch
3258 and qsearchs were on top of.)
3260 The whole fields / hfields mess should be removed.
3262 The various WHERE clauses should be subroutined.
3264 table string should be deprecated in favor of DBIx::DBSchema::Table.
3266 No doubt we could benefit from a Tied hash. Documenting how exists / defined
3267 true maps to the database (and WHERE clauses) would also help.
3269 The ut_ methods should ask the dbdef for a default length.
3271 ut_sqltype (like ut_varchar) should all be defined
3273 A fallback check method should be provided which uses the dbdef.
3275 The ut_money method assumes money has two decimal digits.
3277 The Pg money kludge in the new method only strips `$'.
3279 The ut_phonen method only checks US-style phone numbers.
3281 The _quote function should probably use ut_float instead of a regex.
3283 All the subroutines probably should be methods, here or elsewhere.
3285 Probably should borrow/use some dbdef methods where appropriate (like sub
3288 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
3289 or allow it to be set. Working around it is ugly any way around - DBI should
3290 be fixed. (only affects RDBMS which return uppercase column names)
3292 ut_zip should take an optional country like ut_phone.
3296 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
3298 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.