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 } elsif ( $op eq '!=' ) {
666 qq-( $column IS NULL OR $column != ? )-;
667 #if this needs to be re-enabled, it needs to use a custom op like
668 #"APPROX=" or something (better name?, not '=', to avoid affecting other
670 #} elsif ( $op eq 'APPROX=' && _is_fs_float( $type, $value ) ) {
671 # ( "$column <= ?", "$column >= ?" );
675 } @{ $real_fields } );
678 =item by_key PRIMARY_KEY_VALUE
680 This is a class method that returns the record with the given primary key
681 value. This method is only useful in FS::Record subclasses. For example:
683 my $cust_main = FS::cust_main->by_key(1); # retrieve customer with custnum 1
687 my $cust_main = qsearchs('cust_main', { 'custnum' => 1 } );
692 my ($class, $pkey_value) = @_;
694 my $table = $class->table
695 or croak "No table for $class found";
697 my $dbdef_table = dbdef->table($table)
698 or die "No schema for table $table found - ".
699 "do you need to create it or run dbdef-create?";
700 my $pkey = $dbdef_table->primary_key
701 or die "No primary key for table $table";
703 return qsearchs($table, { $pkey => $pkey_value });
706 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
708 Experimental JOINed search method. Using this method, you can execute a
709 single SELECT spanning multiple tables, and cache the results for subsequent
710 method calls. Interface will almost definately change in an incompatible
718 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
719 my $cache = FS::SearchCache->new( $ptable, $pkey );
722 grep { !$saw{$_->getfield($pkey)}++ }
723 qsearch($table, $record, $select, $extra_sql, $cache )
727 =item qsearchs PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
729 Same as qsearch, except that if more than one record matches, it B<carp>s but
730 returns the first. If this happens, you either made a logic error in asking
731 for a single item, or your data is corrupted.
735 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
737 my(@result) = qsearch(@_);
738 cluck "warning: Multiple records in scalar search ($table)"
739 if scalar(@result) > 1;
740 #should warn more vehemently if the search was on a primary key?
741 scalar(@result) ? ($result[0]) : ();
752 Returns the table name.
757 # cluck "warning: FS::Record::table deprecated; supply one in subclass!";
764 Returns the DBIx::DBSchema::Table object for the table.
770 my($table)=$self->table;
771 dbdef->table($table);
776 Returns the primary key for the table.
782 my $pkey = $self->dbdef_table->primary_key;
785 =item get, getfield COLUMN
787 Returns the value of the column/field/key COLUMN.
792 my($self,$field) = @_;
793 # to avoid "Use of unitialized value" errors
794 if ( defined ( $self->{Hash}->{$field} ) ) {
795 $self->{Hash}->{$field};
805 =item set, setfield COLUMN, VALUE
807 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
812 my($self,$field,$value) = @_;
813 $self->{'modified'} = 1;
814 $self->{'Hash'}->{$field} = $value;
823 Returns true if the column/field/key COLUMN exists.
828 my($self,$field) = @_;
829 exists($self->{Hash}->{$field});
832 =item AUTLOADED METHODS
834 $record->column is a synonym for $record->get('column');
836 $record->column('value') is a synonym for $record->set('column','value');
843 my($field)=$AUTOLOAD;
845 if ( defined($value) ) {
846 confess "errant AUTOLOAD $field for $self (arg $value)"
847 unless blessed($self) && $self->can('setfield');
848 $self->setfield($field,$value);
850 confess "errant AUTOLOAD $field for $self (no args)"
851 unless blessed($self) && $self->can('getfield');
852 $self->getfield($field);
858 # my $field = $AUTOLOAD;
860 # if ( defined($_[1]) ) {
861 # $_[0]->setfield($field, $_[1]);
863 # $_[0]->getfield($field);
869 Returns a list of the column/value pairs, usually for assigning to a new hash.
871 To make a distinct duplicate of an FS::Record object, you can do:
873 $new = new FS::Record ( $old->table, { $old->hash } );
879 confess $self. ' -> hash: Hash attribute is undefined'
880 unless defined($self->{'Hash'});
881 %{ $self->{'Hash'} };
886 Returns a reference to the column/value hash. This may be deprecated in the
887 future; if there's a reason you can't just use the autoloaded or get/set
899 Returns true if any of this object's values have been modified with set (or via
900 an autoloaded method). Doesn't yet recognize when you retreive a hashref and
910 =item select_for_update
912 Selects this record with the SQL "FOR UPDATE" command. This can be useful as
917 sub select_for_update {
919 my $primary_key = $self->primary_key;
922 'table' => $self->table,
923 'hashref' => { $primary_key => $self->$primary_key() },
924 'extra_sql' => 'FOR UPDATE',
930 Locks this table with a database-driver specific lock method. This is used
931 as a mutex in order to do a duplicate search.
933 For PostgreSQL, does "LOCK TABLE tablename IN SHARE ROW EXCLUSIVE MODE".
935 For MySQL, does a SELECT FOR UPDATE on the duplicate_lock table.
937 Errors are fatal; no useful return value.
939 Note: To use this method for new tables other than svc_acct and svc_phone,
940 edit freeside-upgrade and add those tables to the duplicate_lock list.
946 my $table = $self->table;
948 warn "$me locking $table table\n" if $DEBUG;
950 if ( driver_name =~ /^Pg/i ) {
952 dbh->do("LOCK TABLE $table IN SHARE ROW EXCLUSIVE MODE")
955 } elsif ( driver_name =~ /^mysql/i ) {
957 dbh->do("SELECT * FROM duplicate_lock
958 WHERE lockname = '$table'
960 ) or die dbh->errstr;
964 die "unknown database ". driver_name. "; don't know how to lock table";
968 warn "$me acquired $table table lock\n" if $DEBUG;
974 Inserts this record to the database. If there is an error, returns the error,
975 otherwise returns false.
983 warn "$self -> insert" if $DEBUG;
985 my $error = $self->check;
986 return $error if $error;
988 #single-field non-null unique keys are given a value if empty
989 #(like MySQL's AUTO_INCREMENT or Pg SERIAL)
990 foreach ( $self->dbdef_table->unique_singles) {
991 next if $self->getfield($_);
992 next if $self->dbdef_table->column($_)->null eq 'NULL';
996 #and also the primary key, if the database isn't going to
997 my $primary_key = $self->dbdef_table->primary_key;
999 if ( $primary_key ) {
1000 my $col = $self->dbdef_table->column($primary_key);
1003 uc($col->type) =~ /^(BIG)?SERIAL\d?/
1004 || ( driver_name eq 'Pg'
1005 && defined($col->default)
1006 && $col->quoted_default =~ /^nextval\(/i
1008 || ( driver_name eq 'mysql'
1009 && defined($col->local)
1010 && $col->local =~ /AUTO_INCREMENT/i
1012 $self->unique($primary_key) unless $self->getfield($primary_key) || $db_seq;
1015 my $table = $self->table;
1017 # Encrypt before the database
1018 if ( defined(eval '@FS::'. $table . '::encrypted_fields')
1019 && scalar( eval '@FS::'. $table . '::encrypted_fields')
1020 && $conf->exists('encryption')
1022 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
1023 next if $field eq 'payinfo'
1024 && ($self->isa('FS::payinfo_transaction_Mixin')
1025 || $self->isa('FS::payinfo_Mixin') )
1027 && !grep { $self->payby eq $_ } @encrypt_payby;
1028 $saved->{$field} = $self->getfield($field);
1029 $self->setfield($field, $self->encrypt($self->getfield($field)));
1033 #false laziness w/delete
1035 grep { defined($self->getfield($_)) && $self->getfield($_) ne "" }
1038 my @values = map { _quote( $self->getfield($_), $table, $_) } @real_fields;
1041 my $statement = "INSERT INTO $table ";
1042 if ( @real_fields ) {
1045 join( ', ', @real_fields ).
1047 join( ', ', @values ).
1051 $statement .= 'DEFAULT VALUES';
1053 warn "[debug]$me $statement\n" if $DEBUG > 1;
1054 my $sth = dbh->prepare($statement) or return dbh->errstr;
1056 local $SIG{HUP} = 'IGNORE';
1057 local $SIG{INT} = 'IGNORE';
1058 local $SIG{QUIT} = 'IGNORE';
1059 local $SIG{TERM} = 'IGNORE';
1060 local $SIG{TSTP} = 'IGNORE';
1061 local $SIG{PIPE} = 'IGNORE';
1063 $sth->execute or return $sth->errstr;
1065 # get inserted id from the database, if applicable & needed
1066 if ( $db_seq && ! $self->getfield($primary_key) ) {
1067 warn "[debug]$me retreiving sequence from database\n" if $DEBUG;
1071 if ( driver_name eq 'Pg' ) {
1073 #my $oid = $sth->{'pg_oid_status'};
1074 #my $i_sql = "SELECT $primary_key FROM $table WHERE oid = ?";
1076 my $default = $self->dbdef_table->column($primary_key)->quoted_default;
1077 unless ( $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i ) {
1078 dbh->rollback if $FS::UID::AutoCommit;
1079 return "can't parse $table.$primary_key default value".
1080 " for sequence name: $default";
1084 my $i_sql = "SELECT currval('$sequence')";
1085 my $i_sth = dbh->prepare($i_sql) or do {
1086 dbh->rollback if $FS::UID::AutoCommit;
1089 $i_sth->execute() or do { #$i_sth->execute($oid)
1090 dbh->rollback if $FS::UID::AutoCommit;
1091 return $i_sth->errstr;
1093 $insertid = $i_sth->fetchrow_arrayref->[0];
1095 } elsif ( driver_name eq 'mysql' ) {
1097 $insertid = dbh->{'mysql_insertid'};
1098 # work around mysql_insertid being null some of the time, ala RT :/
1099 unless ( $insertid ) {
1100 warn "WARNING: DBD::mysql didn't return mysql_insertid; ".
1101 "using SELECT LAST_INSERT_ID();";
1102 my $i_sql = "SELECT LAST_INSERT_ID()";
1103 my $i_sth = dbh->prepare($i_sql) or do {
1104 dbh->rollback if $FS::UID::AutoCommit;
1107 $i_sth->execute or do {
1108 dbh->rollback if $FS::UID::AutoCommit;
1109 return $i_sth->errstr;
1111 $insertid = $i_sth->fetchrow_arrayref->[0];
1116 dbh->rollback if $FS::UID::AutoCommit;
1117 return "don't know how to retreive inserted ids from ". driver_name.
1118 ", try using counterfiles (maybe run dbdef-create?)";
1122 $self->setfield($primary_key, $insertid);
1126 my @virtual_fields =
1127 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
1128 $self->virtual_fields;
1129 if (@virtual_fields) {
1130 my %v_values = map { $_, $self->getfield($_) } @virtual_fields;
1132 my $vfieldpart = $self->vfieldpart_hashref;
1134 my $v_statement = "INSERT INTO virtual_field(recnum, vfieldpart, value) ".
1137 my $v_sth = dbh->prepare($v_statement) or do {
1138 dbh->rollback if $FS::UID::AutoCommit;
1142 foreach (keys(%v_values)) {
1143 $v_sth->execute($self->getfield($primary_key),
1147 dbh->rollback if $FS::UID::AutoCommit;
1148 return $v_sth->errstr;
1155 if ( defined dbdef->table('h_'. $table) ) {
1156 my $h_statement = $self->_h_statement('insert');
1157 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
1158 $h_sth = dbh->prepare($h_statement) or do {
1159 dbh->rollback if $FS::UID::AutoCommit;
1165 $h_sth->execute or return $h_sth->errstr if $h_sth;
1167 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1169 # Now that it has been saved, reset the encrypted fields so that $new
1170 # can still be used.
1171 foreach my $field (keys %{$saved}) {
1172 $self->setfield($field, $saved->{$field});
1180 Depriciated (use insert instead).
1185 cluck "warning: FS::Record::add deprecated!";
1186 insert @_; #call method in this scope
1191 Delete this record from the database. If there is an error, returns the error,
1192 otherwise returns false.
1199 my $statement = "DELETE FROM ". $self->table. " WHERE ". join(' AND ',
1201 $self->getfield($_) eq ''
1202 #? "( $_ IS NULL OR $_ = \"\" )"
1203 ? ( driver_name eq 'Pg'
1205 : "( $_ IS NULL OR $_ = \"\" )"
1207 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
1208 } ( $self->dbdef_table->primary_key )
1209 ? ( $self->dbdef_table->primary_key)
1210 : real_fields($self->table)
1212 warn "[debug]$me $statement\n" if $DEBUG > 1;
1213 my $sth = dbh->prepare($statement) or return dbh->errstr;
1216 if ( defined dbdef->table('h_'. $self->table) ) {
1217 my $h_statement = $self->_h_statement('delete');
1218 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
1219 $h_sth = dbh->prepare($h_statement) or return dbh->errstr;
1224 my $primary_key = $self->dbdef_table->primary_key;
1227 my $vfp = $self->vfieldpart_hashref;
1228 foreach($self->virtual_fields) {
1229 next if $self->getfield($_) eq '';
1230 unless(@del_vfields) {
1231 my $st = "DELETE FROM virtual_field WHERE recnum = ? AND vfieldpart = ?";
1232 $v_sth = dbh->prepare($st) or return dbh->errstr;
1234 push @del_vfields, $_;
1237 local $SIG{HUP} = 'IGNORE';
1238 local $SIG{INT} = 'IGNORE';
1239 local $SIG{QUIT} = 'IGNORE';
1240 local $SIG{TERM} = 'IGNORE';
1241 local $SIG{TSTP} = 'IGNORE';
1242 local $SIG{PIPE} = 'IGNORE';
1244 my $rc = $sth->execute or return $sth->errstr;
1245 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
1246 $h_sth->execute or return $h_sth->errstr if $h_sth;
1247 $v_sth->execute($self->getfield($primary_key), $vfp->{$_})
1248 or return $v_sth->errstr
1249 foreach (@del_vfields);
1251 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1253 #no need to needlessly destoy the data either (causes problems actually)
1254 #undef $self; #no need to keep object!
1261 Depriciated (use delete instead).
1266 cluck "warning: FS::Record::del deprecated!";
1267 &delete(@_); #call method in this scope
1270 =item replace OLD_RECORD
1272 Replace the OLD_RECORD with this one in the database. If there is an error,
1273 returns the error, otherwise returns false.
1278 my ($new, $old) = (shift, shift);
1280 $old = $new->replace_old unless defined($old);
1282 warn "[debug]$me $new ->replace $old\n" if $DEBUG;
1284 if ( $new->can('replace_check') ) {
1285 my $error = $new->replace_check($old);
1286 return $error if $error;
1289 return "Records not in same table!" unless $new->table eq $old->table;
1291 my $primary_key = $old->dbdef_table->primary_key;
1292 return "Can't change primary key $primary_key ".
1293 'from '. $old->getfield($primary_key).
1294 ' to ' . $new->getfield($primary_key)
1296 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
1298 my $error = $new->check;
1299 return $error if $error;
1301 # Encrypt for replace
1303 if ( $conf->exists('encryption')
1304 && defined(eval '@FS::'. $new->table . '::encrypted_fields')
1305 && scalar( eval '@FS::'. $new->table . '::encrypted_fields')
1307 foreach my $field (eval '@FS::'. $new->table . '::encrypted_fields') {
1308 next if $field eq 'payinfo'
1309 && ($new->isa('FS::payinfo_transaction_Mixin')
1310 || $new->isa('FS::payinfo_Mixin') )
1312 && !grep { $new->payby eq $_ } @encrypt_payby;
1313 $saved->{$field} = $new->getfield($field);
1314 $new->setfield($field, $new->encrypt($new->getfield($field)));
1318 #my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
1319 my %diff = map { ($new->getfield($_) ne $old->getfield($_))
1320 ? ($_, $new->getfield($_)) : () } $old->fields;
1322 unless (keys(%diff) || $no_update_diff ) {
1323 carp "[warning]$me ". ref($new)."->replace ".
1324 ( $primary_key ? "$primary_key ".$new->get($primary_key) : '' ).
1325 ": records identical"
1326 unless $nowarn_identical;
1330 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
1332 "$_ = ". _quote($new->getfield($_),$old->table,$_)
1333 } real_fields($old->table)
1338 if ( $old->getfield($_) eq '' ) {
1340 #false laziness w/qsearch
1341 if ( driver_name eq 'Pg' ) {
1342 my $type = $old->dbdef_table->column($_)->type;
1343 if ( $type =~ /(int|(big)?serial)/i ) {
1346 qq-( $_ IS NULL OR $_ = '' )-;
1349 qq-( $_ IS NULL OR $_ = "" )-;
1353 "$_ = ". _quote($old->getfield($_),$old->table,$_);
1356 } ( $primary_key ? ( $primary_key ) : real_fields($old->table) )
1359 warn "[debug]$me $statement\n" if $DEBUG > 1;
1360 my $sth = dbh->prepare($statement) or return dbh->errstr;
1363 if ( defined dbdef->table('h_'. $old->table) ) {
1364 my $h_old_statement = $old->_h_statement('replace_old');
1365 warn "[debug]$me $h_old_statement\n" if $DEBUG > 2;
1366 $h_old_sth = dbh->prepare($h_old_statement) or return dbh->errstr;
1372 if ( defined dbdef->table('h_'. $new->table) ) {
1373 my $h_new_statement = $new->_h_statement('replace_new');
1374 warn "[debug]$me $h_new_statement\n" if $DEBUG > 2;
1375 $h_new_sth = dbh->prepare($h_new_statement) or return dbh->errstr;
1380 # For virtual fields we have three cases with different SQL
1381 # statements: add, replace, delete
1385 my (@add_vfields, @rep_vfields, @del_vfields);
1386 my $vfp = $old->vfieldpart_hashref;
1387 foreach(grep { exists($diff{$_}) } $new->virtual_fields) {
1388 if($diff{$_} eq '') {
1390 unless(@del_vfields) {
1391 my $st = "DELETE FROM virtual_field WHERE recnum = ? ".
1392 "AND vfieldpart = ?";
1393 warn "[debug]$me $st\n" if $DEBUG > 2;
1394 $v_del_sth = dbh->prepare($st) or return dbh->errstr;
1396 push @del_vfields, $_;
1397 } elsif($old->getfield($_) eq '') {
1399 unless(@add_vfields) {
1400 my $st = "INSERT INTO virtual_field (value, recnum, vfieldpart) ".
1402 warn "[debug]$me $st\n" if $DEBUG > 2;
1403 $v_add_sth = dbh->prepare($st) or return dbh->errstr;
1405 push @add_vfields, $_;
1408 unless(@rep_vfields) {
1409 my $st = "UPDATE virtual_field SET value = ? ".
1410 "WHERE recnum = ? AND vfieldpart = ?";
1411 warn "[debug]$me $st\n" if $DEBUG > 2;
1412 $v_rep_sth = dbh->prepare($st) or return dbh->errstr;
1414 push @rep_vfields, $_;
1418 local $SIG{HUP} = 'IGNORE';
1419 local $SIG{INT} = 'IGNORE';
1420 local $SIG{QUIT} = 'IGNORE';
1421 local $SIG{TERM} = 'IGNORE';
1422 local $SIG{TSTP} = 'IGNORE';
1423 local $SIG{PIPE} = 'IGNORE';
1425 my $rc = $sth->execute or return $sth->errstr;
1426 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
1427 $h_old_sth->execute or return $h_old_sth->errstr if $h_old_sth;
1428 $h_new_sth->execute or return $h_new_sth->errstr if $h_new_sth;
1430 $v_del_sth->execute($old->getfield($primary_key),
1432 or return $v_del_sth->errstr
1433 foreach(@del_vfields);
1435 $v_add_sth->execute($new->getfield($_),
1436 $old->getfield($primary_key),
1438 or return $v_add_sth->errstr
1439 foreach(@add_vfields);
1441 $v_rep_sth->execute($new->getfield($_),
1442 $old->getfield($primary_key),
1444 or return $v_rep_sth->errstr
1445 foreach(@rep_vfields);
1447 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1449 # Now that it has been saved, reset the encrypted fields so that $new
1450 # can still be used.
1451 foreach my $field (keys %{$saved}) {
1452 $new->setfield($field, $saved->{$field});
1460 my( $self ) = shift;
1461 warn "[$me] replace called with no arguments; autoloading old record\n"
1464 my $primary_key = $self->dbdef_table->primary_key;
1465 if ( $primary_key ) {
1466 $self->by_key( $self->$primary_key() ) #this is what's returned
1467 or croak "can't find ". $self->table. ".$primary_key ".
1468 $self->$primary_key();
1470 croak $self->table. " has no primary key; pass old record as argument";
1477 Depriciated (use replace instead).
1482 cluck "warning: FS::Record::rep deprecated!";
1483 replace @_; #call method in this scope
1488 Checks virtual fields (using check_blocks). Subclasses should still provide
1489 a check method to validate real fields, foreign keys, etc., and call this
1490 method via $self->SUPER::check.
1492 (FIXME: Should this method try to make sure that it I<is> being called from
1493 a subclass's check method, to keep the current semantics as far as possible?)
1498 #confess "FS::Record::check not implemented; supply one in subclass!";
1501 foreach my $field ($self->virtual_fields) {
1502 for ($self->getfield($field)) {
1503 # See notes on check_block in FS::part_virtual_field.
1504 eval $self->pvf($field)->check_block;
1506 #this is bad, probably want to follow the stack backtrace up and see
1508 my $err = "Fatal error checking $field for $self";
1510 return "$err (see log for backtrace): $@";
1513 $self->setfield($field, $_);
1519 =item process_batch_import JOB OPTIONS_HASHREF PARAMS
1521 Processes a batch import as a queued JSRPC job
1523 JOB is an FS::queue entry.
1525 OPTIONS_HASHREF can have the following keys:
1531 Table name (required).
1535 Listref of field names for static fields. They will be given values from the
1536 PARAMS hashref and passed as a "params" hashref to batch_import.
1540 Formats hashref. Keys are field names, values are listrefs that define the
1543 Each listref value can be a column name or a code reference. Coderefs are run
1544 with the row object, data and a FS::Conf object as the three parameters.
1545 For example, this coderef does the same thing as using the "columnname" string:
1548 my( $record, $data, $conf ) = @_;
1549 $record->columnname( $data );
1552 Coderefs are run after all "column name" fields are assigned.
1556 Optional format hashref of types. Keys are field names, values are "csv",
1557 "xls" or "fixedlength". Overrides automatic determination of file type
1560 =item format_headers
1562 Optional format hashref of header lines. Keys are field names, values are 0
1563 for no header, 1 to ignore the first line, or to higher numbers to ignore that
1566 =item format_sep_chars
1568 Optional format hashref of CSV sep_chars. Keys are field names, values are the
1569 CSV separation character.
1571 =item format_fixedlenth_formats
1573 Optional format hashref of fixed length format defintiions. Keys are field
1574 names, values Parse::FixedLength listrefs of field definitions.
1578 Set true to default to CSV file type if the filename does not contain a
1579 recognizable ".csv" or ".xls" extension (and type is not pre-specified by
1584 PARAMS is a base64-encoded Storable string containing the POSTed data as
1585 a hash ref. It normally contains at least one field, "uploaded files",
1586 generated by /elements/file-upload.html and containing the list of uploaded
1587 files. Currently only supports a single file named "file".
1591 use Storable qw(thaw);
1594 sub process_batch_import {
1595 my($job, $opt) = ( shift, shift );
1597 my $table = $opt->{table};
1598 my @pass_params = $opt->{params} ? @{ $opt->{params} } : ();
1599 my %formats = %{ $opt->{formats} };
1601 my $param = thaw(decode_base64(shift));
1602 warn Dumper($param) if $DEBUG;
1604 my $files = $param->{'uploaded_files'}
1605 or die "No files provided.\n";
1607 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
1609 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
1610 my $file = $dir. $files{'file'};
1615 formats => \%formats,
1616 format_types => $opt->{format_types},
1617 format_headers => $opt->{format_headers},
1618 format_sep_chars => $opt->{format_sep_chars},
1619 format_fixedlength_formats => $opt->{format_fixedlength_formats},
1620 format_xml_formats => $opt->{format_xml_formats},
1621 format_row_callbacks => $opt->{format_row_callbacks},
1626 format => $param->{format},
1627 params => { map { $_ => $param->{$_} } @pass_params },
1629 default_csv => $opt->{default_csv},
1630 postinsert_callback => $opt->{postinsert_callback},
1633 if ( $opt->{'batch_namecol'} ) {
1634 $iopt{'batch_namevalue'} = $param->{ $opt->{'batch_namecol'} };
1635 $iopt{$_} = $opt->{$_} foreach qw( batch_keycol batch_table batch_namecol );
1638 my $error = FS::Record::batch_import( \%iopt );
1642 die "$error\n" if $error;
1645 =item batch_import PARAM_HASHREF
1647 Class method for batch imports. Available params:
1653 =item format - usual way to specify import, with this format string selecting data from the formats and format_* info hashes
1659 =item format_headers
1661 =item format_sep_chars
1663 =item format_fixedlength_formats
1665 =item format_row_callbacks
1667 =item fields - Alternate way to specify import, specifying import fields directly as a listref
1669 =item preinsert_callback
1671 =item postinsert_callback
1677 FS::queue object, will be updated with progress
1683 csv, xls, fixedlength, xml
1694 warn "$me batch_import call with params: \n". Dumper($param)
1697 my $table = $param->{table};
1699 my $job = $param->{job};
1700 my $file = $param->{file};
1701 my $params = $param->{params} || {};
1703 my( $type, $header, $sep_char, $fixedlength_format,
1704 $xml_format, $row_callback, @fields );
1706 my $postinsert_callback = '';
1707 $postinsert_callback = $param->{'postinsert_callback'}
1708 if $param->{'postinsert_callback'};
1709 my $preinsert_callback = '';
1710 $preinsert_callback = $param->{'preinsert_callback'}
1711 if $param->{'preinsert_callback'};
1713 if ( $param->{'format'} ) {
1715 my $format = $param->{'format'};
1716 my $formats = $param->{formats};
1717 die "unknown format $format" unless exists $formats->{ $format };
1719 $type = $param->{'format_types'}
1720 ? $param->{'format_types'}{ $format }
1721 : $param->{type} || 'csv';
1724 $header = $param->{'format_headers'}
1725 ? $param->{'format_headers'}{ $param->{'format'} }
1728 $sep_char = $param->{'format_sep_chars'}
1729 ? $param->{'format_sep_chars'}{ $param->{'format'} }
1732 $fixedlength_format =
1733 $param->{'format_fixedlength_formats'}
1734 ? $param->{'format_fixedlength_formats'}{ $param->{'format'} }
1738 $param->{'format_xml_formats'}
1739 ? $param->{'format_xml_formats'}{ $param->{'format'} }
1743 $param->{'format_row_callbacks'}
1744 ? $param->{'format_row_callbacks'}{ $param->{'format'} }
1747 @fields = @{ $formats->{ $format } };
1749 } elsif ( $param->{'fields'} ) {
1751 $type = ''; #infer from filename
1754 $fixedlength_format = '';
1756 @fields = @{ $param->{'fields'} };
1759 die "neither format nor fields specified";
1762 #my $file = $param->{file};
1765 if ( $file =~ /\.(\w+)$/i ) {
1769 warn "can't parse file type from filename $file; defaulting to CSV";
1773 if $param->{'default_csv'} && $type ne 'xls';
1781 if ( $type eq 'csv' || $type eq 'fixedlength' ) {
1783 if ( $type eq 'csv' ) {
1786 $attr{sep_char} = $sep_char if $sep_char;
1787 $parser = new Text::CSV_XS \%attr;
1789 } elsif ( $type eq 'fixedlength' ) {
1791 eval "use Parse::FixedLength;";
1793 $parser = Parse::FixedLength->new($fixedlength_format);
1797 die "Unknown file type $type\n";
1800 @buffer = split(/\r?\n/, slurp($file) );
1801 splice(@buffer, 0, ($header || 0) );
1802 $count = scalar(@buffer);
1804 } elsif ( $type eq 'xls' ) {
1806 eval "use Spreadsheet::ParseExcel;";
1809 eval "use DateTime::Format::Excel;";
1810 #for now, just let the error be thrown if it is used, since only CDR
1811 # formats bill_west and troop use it, not other excel-parsing things
1814 my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($file);
1816 $parser = $excel->{Worksheet}[0]; #first sheet
1818 $count = $parser->{MaxRow} || $parser->{MinRow};
1821 $row = $header || 0;
1822 } elsif ( $type eq 'xml' ) {
1824 eval "use XML::Simple;";
1826 my $xmlrow = $xml_format->{'xmlrow'};
1827 $parser = $xml_format->{'xmlkeys'};
1828 die 'no xmlkeys specified' unless ref $parser eq 'ARRAY';
1829 my $data = XML::Simple::XMLin(
1831 'SuppressEmpty' => '', #sets empty values to ''
1835 $rows = $rows->{$_} foreach @$xmlrow;
1836 $rows = [ $rows ] if ref($rows) ne 'ARRAY';
1837 $count = @buffer = @$rows;
1839 die "Unknown file type $type\n";
1844 local $SIG{HUP} = 'IGNORE';
1845 local $SIG{INT} = 'IGNORE';
1846 local $SIG{QUIT} = 'IGNORE';
1847 local $SIG{TERM} = 'IGNORE';
1848 local $SIG{TSTP} = 'IGNORE';
1849 local $SIG{PIPE} = 'IGNORE';
1851 my $oldAutoCommit = $FS::UID::AutoCommit;
1852 local $FS::UID::AutoCommit = 0;
1855 #my $params = $param->{params} || {};
1856 if ( $param->{'batch_namecol'} && $param->{'batch_namevalue'} ) {
1857 my $batch_col = $param->{'batch_keycol'};
1859 my $batch_class = 'FS::'. $param->{'batch_table'};
1860 my $batch = $batch_class->new({
1861 $param->{'batch_namecol'} => $param->{'batch_namevalue'}
1863 my $error = $batch->insert;
1865 $dbh->rollback if $oldAutoCommit;
1866 return "can't insert batch record: $error";
1868 #primary key via dbdef? (so the column names don't have to match)
1869 my $batch_value = $batch->get( $param->{'batch_keycol'} );
1871 $params->{ $batch_col } = $batch_value;
1874 #my $job = $param->{job};
1877 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
1881 if ( $type eq 'csv' ) {
1883 last unless scalar(@buffer);
1884 $line = shift(@buffer);
1886 next if $line =~ /^\s*$/; #skip empty lines
1888 $line = &{$row_callback}($line) if $row_callback;
1890 next if $line =~ /^\s*$/; #skip empty lines
1892 $parser->parse($line) or do {
1893 $dbh->rollback if $oldAutoCommit;
1894 return "can't parse: ". $parser->error_input() . " " . $parser->error_diag;
1896 @columns = $parser->fields();
1898 } elsif ( $type eq 'fixedlength' ) {
1900 last unless scalar(@buffer);
1901 $line = shift(@buffer);
1903 @columns = $parser->parse($line);
1905 } elsif ( $type eq 'xls' ) {
1907 last if $row > ($parser->{MaxRow} || $parser->{MinRow})
1908 || ! $parser->{Cells}[$row];
1910 my @row = @{ $parser->{Cells}[$row] };
1911 @columns = map $_->{Val}, @row;
1914 #warn $z++. ": $_\n" for @columns;
1916 } elsif ( $type eq 'xml' ) {
1917 # $parser = [ 'Column0Key', 'Column1Key' ... ]
1918 last unless scalar(@buffer);
1919 my $row = shift @buffer;
1920 @columns = @{ $row }{ @$parser };
1922 die "Unknown file type $type\n";
1926 my %hash = %$params;
1928 foreach my $field ( @fields ) {
1930 my $value = shift @columns;
1932 if ( ref($field) eq 'CODE' ) {
1933 #&{$field}(\%hash, $value);
1934 push @later, $field, $value;
1936 #??? $hash{$field} = $value if length($value);
1937 $hash{$field} = $value if defined($value) && length($value);
1942 #my $table = $param->{table};
1943 my $class = "FS::$table";
1945 my $record = $class->new( \%hash );
1948 while ( scalar(@later) ) {
1949 my $sub = shift @later;
1950 my $data = shift @later;
1952 &{$sub}($record, $data, $conf, $param); # $record->&{$sub}($data, $conf)
1955 $dbh->rollback if $oldAutoCommit;
1956 return "can't insert record". ( $line ? " for $line" : '' ). ": $@";
1958 last if exists( $param->{skiprow} );
1960 next if exists( $param->{skiprow} );
1962 if ( $preinsert_callback ) {
1963 my $error = &{$preinsert_callback}($record, $param);
1965 $dbh->rollback if $oldAutoCommit;
1966 return "preinsert_callback error". ( $line ? " for $line" : '' ).
1969 next if exists $param->{skiprow} && $param->{skiprow};
1972 my $error = $record->insert;
1975 $dbh->rollback if $oldAutoCommit;
1976 return "can't insert record". ( $line ? " for $line" : '' ). ": $error";
1982 if ( $postinsert_callback ) {
1983 my $error = &{$postinsert_callback}($record, $param);
1985 $dbh->rollback if $oldAutoCommit;
1986 return "postinsert_callback error". ( $line ? " for $line" : '' ).
1991 if ( $job && time - $min_sec > $last ) { #progress bar
1992 $job->update_statustext( int(100 * $imported / $count) );
1998 unless ( $imported || $param->{empty_ok} ) {
1999 $dbh->rollback if $oldAutoCommit;
2000 return "Empty file!";
2003 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
2010 my( $self, $action, $time ) = @_;
2014 my %nohistory = map { $_=>1 } $self->nohistory_fields;
2017 grep { defined($self->get($_)) && $self->get($_) ne "" && ! $nohistory{$_} }
2018 real_fields($self->table);
2021 # If we're encrypting then don't store the payinfo in the history
2022 if ( $conf && $conf->exists('encryption') && $self->table ne 'banned_pay' ) {
2023 @fields = grep { $_ ne 'payinfo' } @fields;
2026 my @values = map { _quote( $self->getfield($_), $self->table, $_) } @fields;
2028 "INSERT INTO h_". $self->table. " ( ".
2029 join(', ', qw(history_date history_user history_action), @fields ).
2031 join(', ', $time, dbh->quote(getotaker()), dbh->quote($action), @values).
2038 B<Warning>: External use is B<deprecated>.
2040 Replaces COLUMN in record with a unique number, using counters in the
2041 filesystem. Used by the B<insert> method on single-field unique columns
2042 (see L<DBIx::DBSchema::Table>) and also as a fallback for primary keys
2043 that aren't SERIAL (Pg) or AUTO_INCREMENT (mysql).
2045 Returns the new value.
2050 my($self,$field) = @_;
2051 my($table)=$self->table;
2053 croak "Unique called on field $field, but it is ",
2054 $self->getfield($field),
2056 if $self->getfield($field);
2058 #warn "table $table is tainted" if is_tainted($table);
2059 #warn "field $field is tainted" if is_tainted($field);
2061 my($counter) = new File::CounterFile "$table.$field",0;
2063 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
2065 # my($counter) = new File::CounterFile "$user/$table.$field",0;
2068 my $index = $counter->inc;
2069 $index = $counter->inc while qsearchs($table, { $field=>$index } );
2071 $index =~ /^(\d*)$/;
2074 $self->setfield($field,$index);
2078 =item ut_float COLUMN
2080 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
2081 null. If there is an error, returns the error, otherwise returns false.
2086 my($self,$field)=@_ ;
2087 ($self->getfield($field) =~ /^\s*(\d+\.\d+)\s*$/ ||
2088 $self->getfield($field) =~ /^\s*(\d+)\s*$/ ||
2089 $self->getfield($field) =~ /^\s*(\d+\.\d+e\d+)\s*$/ ||
2090 $self->getfield($field) =~ /^\s*(\d+e\d+)\s*$/)
2091 or return "Illegal or empty (float) $field: ". $self->getfield($field);
2092 $self->setfield($field,$1);
2095 =item ut_floatn COLUMN
2097 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
2098 null. If there is an error, returns the error, otherwise returns false.
2102 #false laziness w/ut_ipn
2104 my( $self, $field ) = @_;
2105 if ( $self->getfield($field) =~ /^()$/ ) {
2106 $self->setfield($field,'');
2109 $self->ut_float($field);
2113 =item ut_sfloat COLUMN
2115 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10.
2116 May not be null. If there is an error, returns the error, otherwise returns
2122 my($self,$field)=@_ ;
2123 ($self->getfield($field) =~ /^\s*(-?\d+\.\d+)\s*$/ ||
2124 $self->getfield($field) =~ /^\s*(-?\d+)\s*$/ ||
2125 $self->getfield($field) =~ /^\s*(-?\d+\.\d+[eE]-?\d+)\s*$/ ||
2126 $self->getfield($field) =~ /^\s*(-?\d+[eE]-?\d+)\s*$/)
2127 or return "Illegal or empty (float) $field: ". $self->getfield($field);
2128 $self->setfield($field,$1);
2131 =item ut_sfloatn COLUMN
2133 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
2134 null. If there is an error, returns the error, otherwise returns false.
2139 my( $self, $field ) = @_;
2140 if ( $self->getfield($field) =~ /^()$/ ) {
2141 $self->setfield($field,'');
2144 $self->ut_sfloat($field);
2148 =item ut_snumber COLUMN
2150 Check/untaint signed numeric data (whole numbers). If there is an error,
2151 returns the error, otherwise returns false.
2156 my($self, $field) = @_;
2157 $self->getfield($field) =~ /^\s*(-?)\s*(\d+)\s*$/
2158 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
2159 $self->setfield($field, "$1$2");
2163 =item ut_snumbern COLUMN
2165 Check/untaint signed numeric data (whole numbers). If there is an error,
2166 returns the error, otherwise returns false.
2171 my($self, $field) = @_;
2172 $self->getfield($field) =~ /^\s*(-?)\s*(\d*)\s*$/
2173 or return "Illegal (numeric) $field: ". $self->getfield($field);
2175 return "Illegal (numeric) $field: ". $self->getfield($field)
2178 $self->setfield($field, "$1$2");
2182 =item ut_number COLUMN
2184 Check/untaint simple numeric data (whole numbers). May not be null. If there
2185 is an error, returns the error, otherwise returns false.
2190 my($self,$field)=@_;
2191 $self->getfield($field) =~ /^\s*(\d+)\s*$/
2192 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
2193 $self->setfield($field,$1);
2197 =item ut_numbern COLUMN
2199 Check/untaint simple numeric data (whole numbers). May be null. If there is
2200 an error, returns the error, otherwise returns false.
2205 my($self,$field)=@_;
2206 $self->getfield($field) =~ /^\s*(\d*)\s*$/
2207 or return "Illegal (numeric) $field: ". $self->getfield($field);
2208 $self->setfield($field,$1);
2212 =item ut_money COLUMN
2214 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
2215 is an error, returns the error, otherwise returns false.
2220 my($self,$field)=@_;
2221 $self->setfield($field, 0) if $self->getfield($field) eq '';
2222 $self->getfield($field) =~ /^\s*(\-)?\s*(\d*)(\.\d{2})?\s*$/
2223 or return "Illegal (money) $field: ". $self->getfield($field);
2224 #$self->setfield($field, "$1$2$3" || 0);
2225 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
2229 =item ut_moneyn COLUMN
2231 Check/untaint monetary numbers. May be negative. If there
2232 is an error, returns the error, otherwise returns false.
2237 my($self,$field)=@_;
2238 if ($self->getfield($field) eq '') {
2239 $self->setfield($field, '');
2242 $self->ut_money($field);
2245 =item ut_text COLUMN
2247 Check/untaint text. Alphanumerics, spaces, and the following punctuation
2248 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / = [ ] < >
2249 May not be null. If there is an error, returns the error, otherwise returns
2255 my($self,$field)=@_;
2256 #warn "msgcat ". \&msgcat. "\n";
2257 #warn "notexist ". \¬exist. "\n";
2258 #warn "AUTOLOAD ". \&AUTOLOAD. "\n";
2259 $self->getfield($field)
2260 =~ /^([\wô \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]\<\>$money_char]+)$/
2261 or return gettext('illegal_or_empty_text'). " $field: ".
2262 $self->getfield($field);
2263 $self->setfield($field,$1);
2267 =item ut_textn COLUMN
2269 Check/untaint text. Alphanumerics, spaces, and the following punctuation
2270 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / = [ ] < >
2271 May be null. If there is an error, returns the error, otherwise returns false.
2276 my($self,$field)=@_;
2277 return $self->setfield($field, '') if $self->getfield($field) =~ /^$/;
2278 $self->ut_text($field);
2281 =item ut_alpha COLUMN
2283 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
2284 an error, returns the error, otherwise returns false.
2289 my($self,$field)=@_;
2290 $self->getfield($field) =~ /^(\w+)$/
2291 or return "Illegal or empty (alphanumeric) $field: ".
2292 $self->getfield($field);
2293 $self->setfield($field,$1);
2297 =item ut_alphan COLUMN
2299 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
2300 error, returns the error, otherwise returns false.
2305 my($self,$field)=@_;
2306 $self->getfield($field) =~ /^(\w*)$/
2307 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
2308 $self->setfield($field,$1);
2312 =item ut_alphasn COLUMN
2314 Check/untaint alphanumeric strings, spaces allowed. May be null. If there is
2315 an error, returns the error, otherwise returns false.
2320 my($self,$field)=@_;
2321 $self->getfield($field) =~ /^([\w ]*)$/
2322 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
2323 $self->setfield($field,$1);
2328 =item ut_alpha_lower COLUMN
2330 Check/untaint lowercase alphanumeric strings (no spaces). May not be null. If
2331 there is an error, returns the error, otherwise returns false.
2335 sub ut_alpha_lower {
2336 my($self,$field)=@_;
2337 $self->getfield($field) =~ /[[:upper:]]/
2338 and return "Uppercase characters are not permitted in $field";
2339 $self->ut_alpha($field);
2342 =item ut_phonen COLUMN [ COUNTRY ]
2344 Check/untaint phone numbers. May be null. If there is an error, returns
2345 the error, otherwise returns false.
2347 Takes an optional two-letter ISO country code; without it or with unsupported
2348 countries, ut_phonen simply calls ut_alphan.
2353 my( $self, $field, $country ) = @_;
2354 return $self->ut_alphan($field) unless defined $country;
2355 my $phonen = $self->getfield($field);
2356 if ( $phonen eq '' ) {
2357 $self->setfield($field,'');
2358 } elsif ( $country eq 'US' || $country eq 'CA' ) {
2360 $phonen = $conf->config('cust_main-default_areacode').$phonen
2361 if length($phonen)==7 && $conf->config('cust_main-default_areacode');
2362 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
2363 or return gettext('illegal_phone'). " $field: ". $self->getfield($field);
2364 $phonen = "$1-$2-$3";
2365 $phonen .= " x$4" if $4;
2366 $self->setfield($field,$phonen);
2368 warn "warning: don't know how to check phone numbers for country $country";
2369 return $self->ut_textn($field);
2376 Check/untaint hexadecimal values.
2381 my($self, $field) = @_;
2382 $self->getfield($field) =~ /^([\da-fA-F]+)$/
2383 or return "Illegal (hex) $field: ". $self->getfield($field);
2384 $self->setfield($field, uc($1));
2388 =item ut_hexn COLUMN
2390 Check/untaint hexadecimal values. May be null.
2395 my($self, $field) = @_;
2396 $self->getfield($field) =~ /^([\da-fA-F]*)$/
2397 or return "Illegal (hex) $field: ". $self->getfield($field);
2398 $self->setfield($field, uc($1));
2402 =item ut_mac_addr COLUMN
2404 Check/untaint mac addresses. May be null.
2409 my($self, $field) = @_;
2411 my $mac = $self->get($field);
2414 $self->set($field, $mac);
2416 my $e = $self->ut_hex($field);
2419 return "Illegal (mac address) $field: ". $self->getfield($field)
2420 unless length($self->getfield($field)) == 12;
2426 =item ut_mac_addrn COLUMN
2428 Check/untaint mac addresses. May be null.
2433 my($self, $field) = @_;
2434 ($self->getfield($field) eq '') ? '' : $self->ut_mac_addr($field);
2439 Check/untaint ip addresses. IPv4 only for now, though ::1 is auto-translated
2445 my( $self, $field ) = @_;
2446 $self->setfield($field, '127.0.0.1') if $self->getfield($field) eq '::1';
2447 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
2448 or return "Illegal (IP address) $field: ". $self->getfield($field);
2449 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
2450 $self->setfield($field, "$1.$2.$3.$4");
2456 Check/untaint ip addresses. IPv4 only for now, though ::1 is auto-translated
2457 to 127.0.0.1. May be null.
2462 my( $self, $field ) = @_;
2463 if ( $self->getfield($field) =~ /^()$/ ) {
2464 $self->setfield($field,'');
2467 $self->ut_ip($field);
2471 =item ut_ip46 COLUMN
2473 Check/untaint IPv4 or IPv6 address.
2478 my( $self, $field ) = @_;
2479 my $ip = NetAddr::IP->new($self->getfield($field))
2480 or return "Illegal (IP address) $field: ".$self->getfield($field);
2481 $self->setfield($field, lc($ip->addr));
2487 Check/untaint IPv6 or IPv6 address. May be null.
2492 my( $self, $field ) = @_;
2493 if ( $self->getfield($field) =~ /^$/ ) {
2494 $self->setfield($field, '');
2497 $self->ut_ip46($field);
2500 =item ut_coord COLUMN [ LOWER [ UPPER ] ]
2502 Check/untaint coordinates.
2503 Accepts the following forms:
2513 The "DDD MM SS" and "DDD MM MMM" are potentially ambiguous.
2514 The latter form (that is, the MMM are thousands of minutes) is
2515 assumed if the "MMM" is exactly three digits or two digits > 59.
2517 To be safe, just use the DDD.DDDDD form.
2519 If LOWER or UPPER are specified, then the coordinate is checked
2520 for lower and upper bounds, respectively.
2525 my ($self, $field) = (shift, shift);
2528 if ( $field =~ /latitude/ ) {
2529 $lower = $lat_lower;
2531 } elsif ( $field =~ /longitude/ ) {
2533 $upper = $lon_upper;
2536 my $coord = $self->getfield($field);
2537 my $neg = $coord =~ s/^(-)//;
2539 my ($d, $m, $s) = (0, 0, 0);
2542 (($d) = ($coord =~ /^(\s*\d{1,3}(?:\.\d+)?)\s*$/)) ||
2543 (($d, $m) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2}(?:\.\d+))\s*$/)) ||
2544 (($d, $m, $s) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2})\s+(\d{1,3})\s*$/))
2546 $s = (((($s =~ /^\d{3}$/) or $s > 59) ? ($s / 1000) : ($s / 60)) / 60);
2549 return "Invalid (coordinate with minutes > 59) $field: "
2550 . $self->getfield($field);
2553 $coord = ($neg ? -1 : 1) * sprintf('%.8f', $d + $m + $s);
2555 if (defined($lower) and ($coord < $lower)) {
2556 return "Invalid (coordinate < $lower) $field: "
2557 . $self->getfield($field);;
2560 if (defined($upper) and ($coord > $upper)) {
2561 return "Invalid (coordinate > $upper) $field: "
2562 . $self->getfield($field);;
2565 $self->setfield($field, $coord);
2569 return "Invalid (coordinate) $field: " . $self->getfield($field);
2573 =item ut_coordn COLUMN [ LOWER [ UPPER ] ]
2575 Same as ut_coord, except optionally null.
2581 my ($self, $field) = (shift, shift);
2583 if ($self->getfield($field) =~ /^\s*$/) {
2586 return $self->ut_coord($field, @_);
2592 =item ut_domain COLUMN
2594 Check/untaint host and domain names.
2599 my( $self, $field ) = @_;
2600 #$self->getfield($field) =~/^(\w+\.)*\w+$/
2601 $self->getfield($field) =~/^(([\w\-]+\.)*\w+)$/
2602 or return "Illegal (domain) $field: ". $self->getfield($field);
2603 $self->setfield($field,$1);
2607 =item ut_name COLUMN
2609 Check/untaint proper names; allows alphanumerics, spaces and the following
2610 punctuation: , . - '
2617 my( $self, $field ) = @_;
2618 # warn "ut_name allowed alphanumerics: +(sort grep /\w/, map { chr() } 0..255), "\n";
2619 $self->getfield($field) =~ /^([\w \,\.\-\']+)$/
2620 or return gettext('illegal_name'). " $field: ". $self->getfield($field);
2621 $self->setfield($field,$1);
2627 Check/untaint zip codes.
2631 my @zip_reqd_countries = qw( AU CA US ); #CA, US implicit...
2634 my( $self, $field, $country ) = @_;
2636 if ( $country eq 'US' ) {
2638 $self->getfield($field) =~ /^\s*(\d{5}(\-\d{4})?)\s*$/
2639 or return gettext('illegal_zip'). " $field for country $country: ".
2640 $self->getfield($field);
2641 $self->setfield($field, $1);
2643 } elsif ( $country eq 'CA' ) {
2645 $self->getfield($field) =~ /^\s*([A-Z]\d[A-Z])\s*(\d[A-Z]\d)\s*$/i
2646 or return gettext('illegal_zip'). " $field for country $country: ".
2647 $self->getfield($field);
2648 $self->setfield($field, "$1 $2");
2652 if ( $self->getfield($field) =~ /^\s*$/
2653 && ( !$country || ! grep { $_ eq $country } @zip_reqd_countries )
2656 $self->setfield($field,'');
2658 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{0,8}\w)\s*$/
2659 or return gettext('illegal_zip'). " $field: ". $self->getfield($field);
2660 $self->setfield($field,$1);
2668 =item ut_country COLUMN
2670 Check/untaint country codes. Country names are changed to codes, if possible -
2671 see L<Locale::Country>.
2676 my( $self, $field ) = @_;
2677 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
2678 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
2679 && country2code($1) ) {
2680 $self->setfield($field,uc(country2code($1)));
2683 $self->getfield($field) =~ /^(\w\w)$/
2684 or return "Illegal (country) $field: ". $self->getfield($field);
2685 $self->setfield($field,uc($1));
2689 =item ut_anything COLUMN
2691 Untaints arbitrary data. Be careful.
2696 my( $self, $field ) = @_;
2697 $self->getfield($field) =~ /^(.*)$/s
2698 or return "Illegal $field: ". $self->getfield($field);
2699 $self->setfield($field,$1);
2703 =item ut_enum COLUMN CHOICES_ARRAYREF
2705 Check/untaint a column, supplying all possible choices, like the "enum" type.
2710 my( $self, $field, $choices ) = @_;
2711 foreach my $choice ( @$choices ) {
2712 if ( $self->getfield($field) eq $choice ) {
2713 $self->setfield($field, $choice);
2717 return "Illegal (enum) field $field: ". $self->getfield($field);
2720 =item ut_enumn COLUMN CHOICES_ARRAYREF
2722 Like ut_enum, except the null value is also allowed.
2727 my( $self, $field, $choices ) = @_;
2728 $self->getfield($field)
2729 ? $self->ut_enum($field, $choices)
2734 =item ut_foreign_key COLUMN FOREIGN_TABLE FOREIGN_COLUMN
2736 Check/untaint a foreign column key. Call a regular ut_ method (like ut_number)
2737 on the column first.
2741 sub ut_foreign_key {
2742 my( $self, $field, $table, $foreign ) = @_;
2743 return '' if $no_check_foreign;
2744 qsearchs($table, { $foreign => $self->getfield($field) })
2745 or return "Can't find ". $self->table. ".$field ". $self->getfield($field).
2746 " in $table.$foreign";
2750 =item ut_foreign_keyn COLUMN FOREIGN_TABLE FOREIGN_COLUMN
2752 Like ut_foreign_key, except the null value is also allowed.
2756 sub ut_foreign_keyn {
2757 my( $self, $field, $table, $foreign ) = @_;
2758 $self->getfield($field)
2759 ? $self->ut_foreign_key($field, $table, $foreign)
2763 =item ut_agentnum_acl COLUMN [ NULL_RIGHT | NULL_RIGHT_LISTREF ]
2765 Checks this column as an agentnum, taking into account the current users's
2766 ACLs. NULL_RIGHT or NULL_RIGHT_LISTREF, if specified, indicates the access
2767 right or rights allowing no agentnum.
2771 sub ut_agentnum_acl {
2772 my( $self, $field ) = (shift, shift);
2773 my $null_acl = scalar(@_) ? shift : [];
2774 $null_acl = [ $null_acl ] unless ref($null_acl);
2776 my $error = $self->ut_foreign_keyn($field, 'agent', 'agentnum');
2777 return "Illegal agentnum: $error" if $error;
2779 my $curuser = $FS::CurrentUser::CurrentUser;
2781 if ( $self->$field() ) {
2783 return "Access denied"
2784 unless $curuser->agentnum($self->$field());
2788 return "Access denied"
2789 unless grep $curuser->access_right($_), @$null_acl;
2797 =item virtual_fields [ TABLE ]
2799 Returns a list of virtual fields defined for the table. This should not
2800 be exported, and should only be called as an instance or class method.
2804 sub virtual_fields {
2807 $table = $self->table or confess "virtual_fields called on non-table";
2809 confess "Unknown table $table" unless dbdef->table($table);
2811 return () unless dbdef->table('part_virtual_field');
2813 unless ( $virtual_fields_cache{$table} ) {
2814 my $query = 'SELECT name from part_virtual_field ' .
2815 "WHERE dbtable = '$table'";
2817 my $result = $dbh->selectcol_arrayref($query);
2818 confess "Error executing virtual fields query: $query: ". $dbh->errstr
2820 $virtual_fields_cache{$table} = $result;
2823 @{$virtual_fields_cache{$table}};
2828 =item fields [ TABLE ]
2830 This is a wrapper for real_fields and virtual_fields. Code that called
2831 fields before should probably continue to call fields.
2836 my $something = shift;
2838 if($something->isa('FS::Record')) {
2839 $table = $something->table;
2841 $table = $something;
2842 $something = "FS::$table";
2844 return (real_fields($table), $something->virtual_fields());
2847 =item pvf FIELD_NAME
2849 Returns the FS::part_virtual_field object corresponding to a field in the
2850 record (specified by FIELD_NAME).
2855 my ($self, $name) = (shift, shift);
2857 if(grep /^$name$/, $self->virtual_fields) {
2858 return qsearchs('part_virtual_field', { dbtable => $self->table,
2864 =item vfieldpart_hashref TABLE
2866 Returns a hashref of virtual field names and vfieldparts applicable to the given
2871 sub vfieldpart_hashref {
2873 my $table = $self->table;
2875 return {} unless dbdef->table('part_virtual_field');
2878 my $statement = "SELECT vfieldpart, name FROM part_virtual_field WHERE ".
2879 "dbtable = '$table'";
2880 my $sth = $dbh->prepare($statement);
2881 $sth->execute or croak "Execution of '$statement' failed: ".$dbh->errstr;
2882 return { map { $_->{name}, $_->{vfieldpart} }
2883 @{$sth->fetchall_arrayref({})} };
2887 =item encrypt($value)
2889 Encrypts the credit card using a combination of PK to encrypt and uuencode to armour.
2891 Returns the encrypted string.
2893 You should generally not have to worry about calling this, as the system handles this for you.
2898 my ($self, $value) = @_;
2901 if ($conf->exists('encryption')) {
2902 if ($self->is_encrypted($value)) {
2903 # Return the original value if it isn't plaintext.
2904 $encrypted = $value;
2907 if (ref($rsa_encrypt) =~ /::RSA/) { # We Can Encrypt
2908 # RSA doesn't like the empty string so let's pack it up
2909 # The database doesn't like the RSA data so uuencode it
2910 my $length = length($value)+1;
2911 $encrypted = pack("u*",$rsa_encrypt->encrypt(pack("Z$length",$value)));
2913 die ("You can't encrypt w/o a valid RSA engine - Check your installation or disable encryption");
2920 =item is_encrypted($value)
2922 Checks to see if the string is encrypted and returns true or false (1/0) to indicate it's status.
2928 my ($self, $value) = @_;
2929 # Possible Bug - Some work may be required here....
2931 if ($value =~ /^M/ && length($value) > 80) {
2938 =item decrypt($value)
2940 Uses the private key to decrypt the string. Returns the decryoted string or undef on failure.
2942 You should generally not have to worry about calling this, as the system handles this for you.
2947 my ($self,$value) = @_;
2948 my $decrypted = $value; # Will return the original value if it isn't encrypted or can't be decrypted.
2949 if ($conf->exists('encryption') && $self->is_encrypted($value)) {
2951 if (ref($rsa_decrypt) =~ /::RSA/) {
2952 my $encrypted = unpack ("u*", $value);
2953 $decrypted = unpack("Z*", eval{$rsa_decrypt->decrypt($encrypted)});
2954 if ($@) {warn "Decryption Failed"};
2962 #Initialize the Module
2963 $rsa_module = 'Crypt::OpenSSL::RSA'; # The Default
2965 if ($conf->exists('encryptionmodule') && $conf->config('encryptionmodule') ne '') {
2966 $rsa_module = $conf->config('encryptionmodule');
2970 eval ("require $rsa_module"); # No need to import the namespace
2973 # Initialize Encryption
2974 if ($conf->exists('encryptionpublickey') && $conf->config('encryptionpublickey') ne '') {
2975 my $public_key = join("\n",$conf->config('encryptionpublickey'));
2976 $rsa_encrypt = $rsa_module->new_public_key($public_key);
2979 # Intitalize Decryption
2980 if ($conf->exists('encryptionprivatekey') && $conf->config('encryptionprivatekey') ne '') {
2981 my $private_key = join("\n",$conf->config('encryptionprivatekey'));
2982 $rsa_decrypt = $rsa_module->new_private_key($private_key);
2986 =item h_search ACTION
2988 Given an ACTION, either "insert", or "delete", returns the appropriate history
2989 record corresponding to this record, if any.
2994 my( $self, $action ) = @_;
2996 my $table = $self->table;
2999 my $primary_key = dbdef->table($table)->primary_key;
3002 'table' => "h_$table",
3003 'hashref' => { $primary_key => $self->$primary_key(),
3004 'history_action' => $action,
3012 Given an ACTION, either "insert", or "delete", returns the timestamp of the
3013 appropriate history record corresponding to this record, if any.
3018 my($self, $action) = @_;
3019 my $h = $self->h_search($action);
3020 $h ? $h->history_date : '';
3023 =item scalar_sql SQL [ PLACEHOLDER, ... ]
3025 A class or object method. Executes the sql statement represented by SQL and
3026 returns a scalar representing the result: the first column of the first row.
3028 Dies on bogus SQL. Returns an empty string if no row is returned.
3030 Typically used for statments which return a single value such as "SELECT
3031 COUNT(*) FROM table WHERE something" OR "SELECT column FROM table WHERE key = ?"
3036 my($self, $sql) = (shift, shift);
3037 my $sth = dbh->prepare($sql) or die dbh->errstr;
3039 or die "Unexpected error executing statement $sql: ". $sth->errstr;
3040 my $row = $sth->fetchrow_arrayref or return '';
3041 my $scalar = $row->[0];
3042 defined($scalar) ? $scalar : '';
3045 =item count [ WHERE ]
3047 Convenience method for the common case of "SELECT COUNT(*) FROM table",
3048 with optional WHERE. Must be called as method on a class with an
3054 my($self, $where) = (shift, shift);
3055 my $table = $self->table or die 'count called on object of class '.ref($self);
3056 my $sql = "SELECT COUNT(*) FROM $table";
3057 $sql .= " WHERE $where" if $where;
3058 $self->scalar_sql($sql);
3067 =item real_fields [ TABLE ]
3069 Returns a list of the real columns in the specified table. Called only by
3070 fields() and other subroutines elsewhere in FS::Record.
3077 my($table_obj) = dbdef->table($table);
3078 confess "Unknown table $table" unless $table_obj;
3079 $table_obj->columns;
3082 =item _quote VALUE, TABLE, COLUMN
3084 This is an internal function used to construct SQL statements. It returns
3085 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
3086 type (see L<DBIx::DBSchema::Column>) does not end in `char' or `binary'.
3091 my($value, $table, $column) = @_;
3092 my $column_obj = dbdef->table($table)->column($column);
3093 my $column_type = $column_obj->type;
3094 my $nullable = $column_obj->null;
3096 warn " $table.$column: $value ($column_type".
3097 ( $nullable ? ' NULL' : ' NOT NULL' ).
3098 ")\n" if $DEBUG > 2;
3100 if ( $value eq '' && $nullable ) {
3102 } elsif ( $value eq '' && $column_type =~ /^(int|numeric)/ ) {
3103 cluck "WARNING: Attempting to set non-null integer $table.$column null; ".
3106 } elsif ( $value =~ /^\d+(\.\d+)?$/ &&
3107 ! $column_type =~ /(char|binary|text)$/i ) {
3109 } elsif (( $column_type =~ /^bytea$/i || $column_type =~ /(blob|varbinary)/i )
3110 && driver_name eq 'Pg'
3114 # dbh->quote($value, { pg_type => PG_BYTEA() }); # doesn't work right
3115 # Pg binary string quoting: convert each character to 3-digit octal prefixed with \\,
3116 # single-quote the whole mess, and put an "E" in front.
3117 return ("E'" . join('', map { sprintf('\\\\%03o', ord($_)) } split(//, $value) ) . "'");
3125 This is deprecated. Don't use it.
3127 It returns a hash-type list with the fields of this record's table set true.
3132 carp "warning: hfields is deprecated";
3135 foreach (fields($table)) {
3144 "$_: ". $self->getfield($_). "|"
3145 } (fields($self->table)) );
3148 sub DESTROY { return; }
3152 # #use Carp qw(cluck);
3153 # #cluck "DESTROYING $self";
3154 # warn "DESTROYING $self";
3158 # return ! eval { join('',@_), kill 0; 1; };
3161 =item str2time_sql [ DRIVER_NAME ]
3163 Returns a function to convert to unix time based on database type, such as
3164 "EXTRACT( EPOCH FROM" for Pg or "UNIX_TIMESTAMP(" for mysql. See
3165 the str2time_sql_closing method to return a closing string rather than just
3166 using a closing parenthesis as previously suggested.
3168 You can pass an optional driver name such as "Pg", "mysql" or
3169 $dbh->{Driver}->{Name} to return a function for that database instead of
3170 the current database.
3175 my $driver = shift || driver_name;
3177 return 'UNIX_TIMESTAMP(' if $driver =~ /^mysql/i;
3178 return 'EXTRACT( EPOCH FROM ' if $driver =~ /^Pg/i;
3180 warn "warning: unknown database type $driver; guessing how to convert ".
3181 "dates to UNIX timestamps";
3182 return 'EXTRACT(EPOCH FROM ';
3186 =item str2time_sql_closing [ DRIVER_NAME ]
3188 Returns the closing suffix of a function to convert to unix time based on
3189 database type, such as ")::integer" for Pg or ")" for mysql.
3191 You can pass an optional driver name such as "Pg", "mysql" or
3192 $dbh->{Driver}->{Name} to return a function for that database instead of
3193 the current database.
3197 sub str2time_sql_closing {
3198 my $driver = shift || driver_name;
3200 return ' )::INTEGER ' if $driver =~ /^Pg/i;
3204 =item regexp_sql [ DRIVER_NAME ]
3206 Returns the operator to do a regular expression comparison based on database
3207 type, such as '~' for Pg or 'REGEXP' for mysql.
3209 You can pass an optional driver name such as "Pg", "mysql" or
3210 $dbh->{Driver}->{Name} to return a function for that database instead of
3211 the current database.
3216 my $driver = shift || driver_name;
3218 return '~' if $driver =~ /^Pg/i;
3219 return 'REGEXP' if $driver =~ /^mysql/i;
3221 die "don't know how to use regular expressions in ". driver_name." databases";
3225 =item not_regexp_sql [ DRIVER_NAME ]
3227 Returns the operator to do a regular expression negation based on database
3228 type, such as '!~' for Pg or 'NOT REGEXP' for mysql.
3230 You can pass an optional driver name such as "Pg", "mysql" or
3231 $dbh->{Driver}->{Name} to return a function for that database instead of
3232 the current database.
3236 sub not_regexp_sql {
3237 my $driver = shift || driver_name;
3239 return '!~' if $driver =~ /^Pg/i;
3240 return 'NOT REGEXP' if $driver =~ /^mysql/i;
3242 die "don't know how to use regular expressions in ". driver_name." databases";
3246 =item concat_sql [ DRIVER_NAME ] ITEMS_ARRAYREF
3248 Returns the items concatendated based on database type, using "CONCAT()" for
3249 mysql and " || " for Pg and other databases.
3251 You can pass an optional driver name such as "Pg", "mysql" or
3252 $dbh->{Driver}->{Name} to return a function for that database instead of
3253 the current database.
3258 my $driver = ref($_[0]) ? driver_name : shift;
3261 if ( $driver =~ /^mysql/i ) {
3262 'CONCAT('. join(',', @$items). ')';
3264 join('||', @$items);
3273 This module should probably be renamed, since much of the functionality is
3274 of general use. It is not completely unlike Adapter::DBI (see below).
3276 Exported qsearch and qsearchs should be deprecated in favor of method calls
3277 (against an FS::Record object like the old search and searchs that qsearch
3278 and qsearchs were on top of.)
3280 The whole fields / hfields mess should be removed.
3282 The various WHERE clauses should be subroutined.
3284 table string should be deprecated in favor of DBIx::DBSchema::Table.
3286 No doubt we could benefit from a Tied hash. Documenting how exists / defined
3287 true maps to the database (and WHERE clauses) would also help.
3289 The ut_ methods should ask the dbdef for a default length.
3291 ut_sqltype (like ut_varchar) should all be defined
3293 A fallback check method should be provided which uses the dbdef.
3295 The ut_money method assumes money has two decimal digits.
3297 The Pg money kludge in the new method only strips `$'.
3299 The ut_phonen method only checks US-style phone numbers.
3301 The _quote function should probably use ut_float instead of a regex.
3303 All the subroutines probably should be methods, here or elsewhere.
3305 Probably should borrow/use some dbdef methods where appropriate (like sub
3308 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
3309 or allow it to be set. Working around it is ugly any way around - DBI should
3310 be fixed. (only affects RDBMS which return uppercase column names)
3312 ut_zip should take an optional country like ut_phone.
3316 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
3318 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.