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_history $qsearch_qualify_columns
14 use Carp qw(carp cluck croak confess);
15 use Scalar::Util qw( blessed );
16 use File::CounterFile;
19 use File::Slurp qw( slurp );
20 use DBI qw(:sql_types);
21 use DBIx::DBSchema 0.38;
22 use FS::UID qw(dbh getotaker datasrc driver_name);
24 use FS::Schema qw(dbdef);
26 use FS::Msgcat qw(gettext);
27 use NetAddr::IP; # for validation
29 #use FS::Conf; #dependency loop bs, in install_callback below instead
31 use FS::part_virtual_field;
37 @encrypt_payby = qw( CARD DCRD CHEK DCHK );
39 #export dbdef for now... everything else expects to find it here
41 dbh fields hfields qsearch qsearchs dbdef jsearch
42 str2time_sql str2time_sql_closing regexp_sql not_regexp_sql
43 concat_sql group_concat_sql
50 $nowarn_identical = 0;
51 $nowarn_classload = 0;
55 $qsearch_qualify_columns = 0;
57 $no_check_foreign = 0;
65 $conf_encryption = '';
66 FS::UID->install_callback( sub {
70 $conf = FS::Conf->new;
71 $conf_encryption = $conf->exists('encryption');
72 $money_char = $conf->config('money_char') || '$';
73 my $nw_coords = $conf->exists('geocode-require_nw_coordinates');
74 $lat_lower = $nw_coords ? 1 : -90;
75 $lon_upper = $nw_coords ? -1 : 180;
77 $File::CounterFile::DEFAULT_DIR = $conf->base_dir . "/counters.". datasrc;
79 if ( driver_name eq 'Pg' ) {
80 eval "use DBD::Pg ':pg_types'";
83 eval "sub PG_BYTEA { die 'guru meditation #9: calling PG_BYTEA when not running Pg?'; }";
90 FS::Record - Database record objects
95 use FS::Record qw(dbh fields qsearch qsearchs);
97 $record = new FS::Record 'table', \%hash;
98 $record = new FS::Record 'table', { 'column' => 'value', ... };
100 $record = qsearchs FS::Record 'table', \%hash;
101 $record = qsearchs FS::Record 'table', { 'column' => 'value', ... };
102 @records = qsearch FS::Record 'table', \%hash;
103 @records = qsearch FS::Record 'table', { 'column' => 'value', ... };
105 $table = $record->table;
106 $dbdef_table = $record->dbdef_table;
108 $value = $record->get('column');
109 $value = $record->getfield('column');
110 $value = $record->column;
112 $record->set( 'column' => 'value' );
113 $record->setfield( 'column' => 'value' );
114 $record->column('value');
116 %hash = $record->hash;
118 $hashref = $record->hashref;
120 $error = $record->insert;
122 $error = $record->delete;
124 $error = $new_record->replace($old_record);
126 # external use deprecated - handled by the database (at least for Pg, mysql)
127 $value = $record->unique('column');
129 $error = $record->ut_float('column');
130 $error = $record->ut_floatn('column');
131 $error = $record->ut_number('column');
132 $error = $record->ut_numbern('column');
133 $error = $record->ut_decimal('column');
134 $error = $record->ut_decimaln('column');
135 $error = $record->ut_snumber('column');
136 $error = $record->ut_snumbern('column');
137 $error = $record->ut_money('column');
138 $error = $record->ut_text('column');
139 $error = $record->ut_textn('column');
140 $error = $record->ut_alpha('column');
141 $error = $record->ut_alphan('column');
142 $error = $record->ut_phonen('column');
143 $error = $record->ut_anything('column');
144 $error = $record->ut_name('column');
146 $quoted_value = _quote($value,'table','field');
149 $fields = hfields('table');
150 if ( $fields->{Field} ) { # etc.
152 @fields = fields 'table'; #as a subroutine
153 @fields = $record->fields; #as a method call
158 (Mostly) object-oriented interface to database records. Records are currently
159 implemented on top of DBI. FS::Record is intended as a base class for
160 table-specific classes to inherit from, i.e. FS::cust_main.
166 =item new [ TABLE, ] HASHREF
168 Creates a new record. It doesn't store it in the database, though. See
169 L<"insert"> for that.
171 Note that the object stores this hash reference, not a distinct copy of the
172 hash it points to. You can ask the object for a copy with the I<hash>
175 TABLE can only be omitted when a dervived class overrides the table method.
181 my $class = ref($proto) || $proto;
183 bless ($self, $class);
185 unless ( defined ( $self->table ) ) {
186 $self->{'Table'} = shift;
187 carp "warning: FS::Record::new called with table name ". $self->{'Table'}
188 unless $nowarn_classload;
191 $self->{'Hash'} = shift;
193 foreach my $field ( grep !defined($self->{'Hash'}{$_}), $self->fields ) {
194 $self->{'Hash'}{$field}='';
197 $self->_rebless if $self->can('_rebless');
199 $self->{'modified'} = 0;
201 $self->_cache($self->{'Hash'}, shift) if $self->can('_cache') && @_;
208 my $class = ref($proto) || $proto;
210 bless ($self, $class);
212 $self->{'Table'} = shift unless defined ( $self->table );
214 my $hashref = $self->{'Hash'} = shift;
216 if ( defined( $cache->cache->{$hashref->{$cache->key}} ) ) {
217 my $obj = $cache->cache->{$hashref->{$cache->key}};
218 $obj->_cache($hashref, $cache) if $obj->can('_cache');
221 $cache->cache->{$hashref->{$cache->key}} = $self->new($hashref, $cache);
228 my $class = ref($proto) || $proto;
230 bless ($self, $class);
231 if ( defined $self->table ) {
232 cluck "create constructor is deprecated, use new!";
235 croak "FS::Record::create called (not from a subclass)!";
239 =item qsearch PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
241 Searches the database for all records matching (at least) the key/value pairs
242 in HASHREF. Returns all the records found as `FS::TABLE' objects if that
243 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
246 The preferred usage is to pass a hash reference of named parameters:
248 @records = qsearch( {
249 'table' => 'table_name',
250 'hashref' => { 'field' => 'value'
251 'field' => { 'op' => '<',
256 #these are optional...
258 'extra_sql' => 'AND field = ? AND intfield = ?',
259 'extra_param' => [ 'value', [ 5, 'int' ] ],
260 'order_by' => 'ORDER BY something',
261 #'cache_obj' => '', #optional
262 'addl_from' => 'LEFT JOIN othtable USING ( field )',
267 Much code still uses old-style positional parameters, this is also probably
268 fine in the common case where there are only two parameters:
270 my @records = qsearch( 'table', { 'field' => 'value' } );
272 Also possible is an experimental LISTREF of PARAMS_HASHREFs for a UNION of
273 the individual PARAMS_HASHREF queries
275 ###oops, argh, FS::Record::new only lets us create database fields.
276 #Normal behaviour if SELECT is not specified is `*', as in
277 #C<SELECT * FROM table WHERE ...>. However, there is an experimental new
278 #feature where you can specify SELECT - remember, the objects returned,
279 #although blessed into the appropriate `FS::TABLE' package, will only have the
280 #fields you specify. This might have unwanted results if you then go calling
281 #regular FS::TABLE methods
286 my %TYPE = (); #for debugging
289 my($type, $value) = @_;
291 my $bind_type = { TYPE => SQL_VARCHAR };
293 if ( $type =~ /(big)?(int|serial)/i && $value =~ /^-?\d+(\.\d+)?$/ ) {
295 $bind_type = { TYPE => SQL_INTEGER };
297 } elsif ( $type =~ /^bytea$/i || $type =~ /(blob|varbinary)/i ) {
299 if ( driver_name eq 'Pg' ) {
301 $bind_type = { pg_type => PG_BYTEA };
303 # $bind_type = ? #SQL_VARCHAR could be fine?
306 #DBD::Pg 1.49: Cannot bind ... unknown sql_type 6 with SQL_FLOAT
307 #fixed by DBD::Pg 2.11.8
308 #can change back to SQL_FLOAT in early-mid 2010, once everyone's upgraded
309 #(make a Tron test first)
310 } elsif ( _is_fs_float( $type, $value ) ) {
312 $bind_type = { TYPE => SQL_DECIMAL };
321 my($type, $value) = @_;
322 if ( ( $type =~ /(numeric)/i && $value =~ /^[+-]?\d+(\.\d+)?$/ ) ||
323 ( $type =~ /(real|float4)/i && $value =~ /[-+]?\d*\.?\d+([eE][-+]?\d+)?/)
331 my( @stable, @record, @cache );
332 my( @select, @extra_sql, @extra_param, @order_by, @addl_from );
334 my %union_options = ();
335 if ( ref($_[0]) eq 'ARRAY' ) {
338 foreach my $href ( @$optlist ) {
339 push @stable, ( $href->{'table'} or die "table name is required" );
340 push @record, ( $href->{'hashref'} || {} );
341 push @select, ( $href->{'select'} || '*' );
342 push @extra_sql, ( $href->{'extra_sql'} || '' );
343 push @extra_param, ( $href->{'extra_param'} || [] );
344 push @order_by, ( $href->{'order_by'} || '' );
345 push @cache, ( $href->{'cache_obj'} || '' );
346 push @addl_from, ( $href->{'addl_from'} || '' );
347 push @debug, ( $href->{'debug'} || '' );
349 die "at least one hashref is required" unless scalar(@stable);
350 } elsif ( ref($_[0]) eq 'HASH' ) {
352 $stable[0] = $opt->{'table'} or die "table name is required";
353 $record[0] = $opt->{'hashref'} || {};
354 $select[0] = $opt->{'select'} || '*';
355 $extra_sql[0] = $opt->{'extra_sql'} || '';
356 $extra_param[0] = $opt->{'extra_param'} || [];
357 $order_by[0] = $opt->{'order_by'} || '';
358 $cache[0] = $opt->{'cache_obj'} || '';
359 $addl_from[0] = $opt->{'addl_from'} || '';
360 $debug[0] = $opt->{'debug'} || '';
371 my $cache = $cache[0];
377 foreach my $stable ( @stable ) {
378 #stop altering the caller's hashref
379 my $record = { %{ shift(@record) || {} } };#and be liberal in receipt
380 my $select = shift @select;
381 my $extra_sql = shift @extra_sql;
382 my $extra_param = shift @extra_param;
383 my $order_by = shift @order_by;
384 my $cache = shift @cache;
385 my $addl_from = shift @addl_from;
386 my $debug = shift @debug;
388 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
390 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
393 my $table = $cache ? $cache->table : $stable;
394 my $dbdef_table = dbdef->table($table)
395 or die "No schema for table $table found - ".
396 "do you need to run freeside-upgrade?";
397 my $pkey = $dbdef_table->primary_key;
399 my @real_fields = grep exists($record->{$_}), real_fields($table);
401 my $statement .= "SELECT $select FROM $stable";
402 $statement .= " $addl_from" if $addl_from;
403 if ( @real_fields ) {
404 $statement .= ' WHERE '. join(' AND ',
405 get_real_fields($table, $record, \@real_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 my $ok = $sth->execute;
471 my $error = "Error executing \"$statement\"";
472 $error .= ' (' . join(', ', map {"'$_'"} @value) . ')' if @value;
473 $error .= ': '. $sth->errstr;
477 my $table = $stable[0];
479 $table = '' if grep { $_ ne $table } @stable;
480 $pkey = dbdef->table($table)->primary_key if $table;
483 tie %result, "Tie::IxHash";
484 my @stuff = @{ $sth->fetchall_arrayref( {} ) };
485 if ( $pkey && scalar(@stuff) && $stuff[0]->{$pkey} ) {
486 %result = map { $_->{$pkey}, $_ } @stuff;
488 @result{@stuff} = @stuff;
494 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
495 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
496 #derivied class didn't override new method, so this optimization is safe
499 new_or_cached( "FS::$table", { %{$_} }, $cache )
503 new( "FS::$table", { %{$_} } )
507 #okay, its been tested
508 # warn "untested code (class FS::$table uses custom new method)";
510 eval 'FS::'. $table. '->new( { %{$_} } )';
514 # Check for encrypted fields and decrypt them.
515 ## only in the local copy, not the cached object
516 if ( $conf_encryption
517 && eval 'defined(@FS::'. $table . '::encrypted_fields)' ) {
518 foreach my $record (@return) {
519 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
520 next if $field eq 'payinfo'
521 && ($record->isa('FS::payinfo_transaction_Mixin')
522 || $record->isa('FS::payinfo_Mixin') )
524 && !grep { $record->payby eq $_ } @encrypt_payby;
525 # Set it directly... This may cause a problem in the future...
526 $record->setfield($field, $record->decrypt($record->getfield($field)));
531 cluck "warning: FS::$table not loaded; returning FS::Record objects"
532 unless $nowarn_classload;
534 FS::Record->new( $table, { %{$_} } );
542 Construct the SQL statement and parameter-binding list for qsearch. Takes
543 the qsearch parameters.
545 Returns a hash containing:
546 'table': The primary table name (if there is one).
547 'statement': The SQL statement itself.
548 'bind_type': An arrayref of bind types.
549 'value': An arrayref of parameter values.
550 'cache': The cache object, if one was passed.
555 my( @stable, @record, @cache );
556 my( @select, @extra_sql, @extra_param, @order_by, @addl_from );
559 my %union_options = ();
560 if ( ref($_[0]) eq 'ARRAY' ) {
563 foreach my $href ( @$optlist ) {
564 push @stable, ( $href->{'table'} or die "table name is required" );
565 push @record, ( $href->{'hashref'} || {} );
566 push @select, ( $href->{'select'} || '*' );
567 push @extra_sql, ( $href->{'extra_sql'} || '' );
568 push @extra_param, ( $href->{'extra_param'} || [] );
569 push @order_by, ( $href->{'order_by'} || '' );
570 push @cache, ( $href->{'cache_obj'} || '' );
571 push @addl_from, ( $href->{'addl_from'} || '' );
572 push @debug, ( $href->{'debug'} || '' );
574 die "at least one hashref is required" unless scalar(@stable);
575 } elsif ( ref($_[0]) eq 'HASH' ) {
577 $stable[0] = $opt->{'table'} or die "table name is required";
578 $record[0] = $opt->{'hashref'} || {};
579 $select[0] = $opt->{'select'} || '*';
580 $extra_sql[0] = $opt->{'extra_sql'} || '';
581 $extra_param[0] = $opt->{'extra_param'} || [];
582 $order_by[0] = $opt->{'order_by'} || '';
583 $cache[0] = $opt->{'cache_obj'} || '';
584 $addl_from[0] = $opt->{'addl_from'} || '';
585 $debug[0] = $opt->{'debug'} || '';
596 my $cache = $cache[0];
602 my $result_table = $stable[0];
603 foreach my $stable ( @stable ) {
604 #stop altering the caller's hashref
605 my $record = { %{ shift(@record) || {} } };#and be liberal in receipt
606 my $select = shift @select;
607 my $extra_sql = shift @extra_sql;
608 my $extra_param = shift @extra_param;
609 my $order_by = shift @order_by;
610 my $cache = shift @cache;
611 my $addl_from = shift @addl_from;
612 my $debug = shift @debug;
614 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
616 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
619 $result_table = '' if $result_table ne $stable;
621 my $table = $cache ? $cache->table : $stable;
622 my $dbdef_table = dbdef->table($table)
623 or die "No schema for table $table found - ".
624 "do you need to run freeside-upgrade?";
625 my $pkey = $dbdef_table->primary_key;
627 my @real_fields = grep exists($record->{$_}), real_fields($table);
629 my $statement .= "SELECT $select FROM $stable";
630 $statement .= " $addl_from" if $addl_from;
631 if ( @real_fields ) {
632 $statement .= ' WHERE '. join(' AND ',
633 get_real_fields($table, $record, \@real_fields));
636 $statement .= " $extra_sql" if defined($extra_sql);
637 $statement .= " $order_by" if defined($order_by);
639 push @statement, $statement;
641 warn "[debug]$me $statement\n" if $DEBUG > 1 || $debug;
645 grep defined( $record->{$_} ) && $record->{$_} ne '', @real_fields
648 my $value = $record->{$field};
649 my $op = (ref($value) && $value->{op}) ? $value->{op} : '=';
650 $value = $value->{'value'} if ref($value);
651 my $type = dbdef->table($table)->column($field)->type;
653 my $bind_type = _bind_type($type, $value);
657 # %TYPE = map { &{"DBI::$_"}() => $_ } @{ $DBI::EXPORT_TAGS{sql_types} }
659 # warn " bind_param $bind (for field $field), $value, TYPE $TYPE{$TYPE}\n";
663 push @bind_type, $bind_type;
667 foreach my $param ( @$extra_param ) {
668 my $bind_type = { TYPE => SQL_VARCHAR };
671 $value = $param->[0];
672 my $type = $param->[1];
673 $bind_type = _bind_type($type, $value);
676 push @bind_type, $bind_type;
680 my $statement = join( ' ) UNION ( ', @statement );
681 $statement = "( $statement )" if scalar(@statement) > 1;
682 $statement .= " $union_options{order_by}" if $union_options{order_by};
685 statement => $statement,
686 bind_type => \@bind_type,
688 table => $result_table,
693 # qsearch should eventually use this
695 my ($table, $cache, @hashrefs) = @_;
697 # XXX get rid of these string evals at some point
698 # (when we have time to test it)
699 # my $class = "FS::$table" if $table;
700 # if ( $class and $class->isa('FS::Record') )
701 # if ( $class->can('new') eq \&new )
703 if ( $table && eval 'scalar(@FS::'. $table. '::ISA);' ) {
704 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
705 #derivied class didn't override new method, so this optimization is safe
708 new_or_cached( "FS::$table", { %{$_} }, $cache )
712 new( "FS::$table", { %{$_} } )
716 #okay, its been tested
717 # warn "untested code (class FS::$table uses custom new method)";
719 eval 'FS::'. $table. '->new( { %{$_} } )';
723 # Check for encrypted fields and decrypt them.
724 ## only in the local copy, not the cached object
725 if ( $conf_encryption
726 && eval 'defined(@FS::'. $table . '::encrypted_fields)' ) {
727 foreach my $record (@return) {
728 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
729 next if $field eq 'payinfo'
730 && ($record->isa('FS::payinfo_transaction_Mixin')
731 || $record->isa('FS::payinfo_Mixin') )
733 && !grep { $record->payby eq $_ } @encrypt_payby;
734 # Set it directly... This may cause a problem in the future...
735 $record->setfield($field, $record->decrypt($record->getfield($field)));
740 cluck "warning: FS::$table not loaded; returning FS::Record objects"
741 unless $nowarn_classload;
743 FS::Record->new( $table, { %{$_} } );
749 sub get_real_fields {
752 my $real_fields = shift;
754 ## could be optimized more for readability
760 my $table_column = $qsearch_qualify_columns ? "$table.$column" : $column;
761 my $type = dbdef->table($table)->column($column)->type;
762 my $value = $record->{$column};
763 $value = $value->{'value'} if ref($value);
765 if ( ref($record->{$column}) ) {
766 $op = $record->{$column}{'op'} if $record->{$column}{'op'};
767 #$op = 'LIKE' if $op =~ /^ILIKE$/i && driver_name ne 'Pg';
768 if ( uc($op) eq 'ILIKE' ) {
770 $record->{$column}{'value'} = lc($record->{$column}{'value'});
771 $table_column = "LOWER($table_column)";
773 $record->{$column} = $record->{$column}{'value'}
776 if ( ! defined( $record->{$column} ) || $record->{$column} eq '' ) {
778 if ( driver_name eq 'Pg' ) {
779 if ( $type =~ /(int|numeric|real|float4|(big)?serial)/i ) {
780 qq-( $table_column IS NULL )-;
782 qq-( $table_column IS NULL OR $table_column = '' )-;
785 qq-( $table_column IS NULL OR $table_column = "" )-;
787 } elsif ( $op eq '!=' ) {
788 if ( driver_name eq 'Pg' ) {
789 if ( $type =~ /(int|numeric|real|float4|(big)?serial)/i ) {
790 qq-( $table_column IS NOT NULL )-;
792 qq-( $table_column IS NOT NULL AND $table_column != '' )-;
795 qq-( $table_column IS NOT NULL AND $table_column != "" )-;
798 if ( driver_name eq 'Pg' ) {
799 qq-( $table_column $op '' )-;
801 qq-( $table_column $op "" )-;
804 } elsif ( $op eq '!=' ) {
805 qq-( $table_column IS NULL OR $table_column != ? )-;
806 #if this needs to be re-enabled, it needs to use a custom op like
807 #"APPROX=" or something (better name?, not '=', to avoid affecting other
809 #} elsif ( $op eq 'APPROX=' && _is_fs_float( $type, $value ) ) {
810 # ( "$table_column <= ?", "$table_column >= ?" );
812 "$table_column $op ?";
819 =item by_key PRIMARY_KEY_VALUE
821 This is a class method that returns the record with the given primary key
822 value. This method is only useful in FS::Record subclasses. For example:
824 my $cust_main = FS::cust_main->by_key(1); # retrieve customer with custnum 1
828 my $cust_main = qsearchs('cust_main', { 'custnum' => 1 } );
833 my ($class, $pkey_value) = @_;
835 my $table = $class->table
836 or croak "No table for $class found";
838 my $dbdef_table = dbdef->table($table)
839 or die "No schema for table $table found - ".
840 "do you need to create it or run dbdef-create?";
841 my $pkey = $dbdef_table->primary_key
842 or die "No primary key for table $table";
844 return qsearchs($table, { $pkey => $pkey_value });
847 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
849 Experimental JOINed search method. Using this method, you can execute a
850 single SELECT spanning multiple tables, and cache the results for subsequent
851 method calls. Interface will almost definately change in an incompatible
859 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
860 my $cache = FS::SearchCache->new( $ptable, $pkey );
863 grep { !$saw{$_->getfield($pkey)}++ }
864 qsearch($table, $record, $select, $extra_sql, $cache )
868 =item qsearchs PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
870 Same as qsearch, except that if more than one record matches, it B<carp>s but
871 returns the first. If this happens, you either made a logic error in asking
872 for a single item, or your data is corrupted.
876 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
878 my(@result) = qsearch(@_);
879 cluck "warning: Multiple records in scalar search ($table)"
880 if scalar(@result) > 1;
881 #should warn more vehemently if the search was on a primary key?
882 scalar(@result) ? ($result[0]) : ();
893 Returns the table name.
898 # cluck "warning: FS::Record::table deprecated; supply one in subclass!";
905 Returns the DBIx::DBSchema::Table object for the table.
911 my($table)=$self->table;
912 dbdef->table($table);
917 Returns the primary key for the table.
923 my $pkey = $self->dbdef_table->primary_key;
926 =item get, getfield COLUMN
928 Returns the value of the column/field/key COLUMN.
933 my($self,$field) = @_;
934 # to avoid "Use of unitialized value" errors
935 if ( defined ( $self->{Hash}->{$field} ) ) {
936 $self->{Hash}->{$field};
946 =item set, setfield COLUMN, VALUE
948 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
953 my($self,$field,$value) = @_;
954 $self->{'modified'} = 1;
955 $self->{'Hash'}->{$field} = $value;
964 Returns true if the column/field/key COLUMN exists.
969 my($self,$field) = @_;
970 exists($self->{Hash}->{$field});
973 =item AUTLOADED METHODS
975 $record->column is a synonym for $record->get('column');
977 $record->column('value') is a synonym for $record->set('column','value');
984 my($field)=$AUTOLOAD;
986 if ( defined($value) ) {
987 confess "errant AUTOLOAD $field for $self (arg $value)"
988 unless blessed($self) && $self->can('setfield');
989 $self->setfield($field,$value);
991 confess "errant AUTOLOAD $field for $self (no args)"
992 unless blessed($self) && $self->can('getfield');
993 $self->getfield($field);
999 # my $field = $AUTOLOAD;
1000 # $field =~ s/.*://;
1001 # if ( defined($_[1]) ) {
1002 # $_[0]->setfield($field, $_[1]);
1004 # $_[0]->getfield($field);
1010 Returns a list of the column/value pairs, usually for assigning to a new hash.
1012 To make a distinct duplicate of an FS::Record object, you can do:
1014 $new = new FS::Record ( $old->table, { $old->hash } );
1020 confess $self. ' -> hash: Hash attribute is undefined'
1021 unless defined($self->{'Hash'});
1022 %{ $self->{'Hash'} };
1027 Returns a reference to the column/value hash. This may be deprecated in the
1028 future; if there's a reason you can't just use the autoloaded or get/set
1040 Returns true if any of this object's values have been modified with set (or via
1041 an autoloaded method). Doesn't yet recognize when you retreive a hashref and
1048 $self->{'modified'};
1051 =item select_for_update
1053 Selects this record with the SQL "FOR UPDATE" command. This can be useful as
1058 sub select_for_update {
1060 my $primary_key = $self->primary_key;
1063 'table' => $self->table,
1064 'hashref' => { $primary_key => $self->$primary_key() },
1065 'extra_sql' => 'FOR UPDATE',
1071 Locks this table with a database-driver specific lock method. This is used
1072 as a mutex in order to do a duplicate search.
1074 For PostgreSQL, does "LOCK TABLE tablename IN SHARE ROW EXCLUSIVE MODE".
1076 For MySQL, does a SELECT FOR UPDATE on the duplicate_lock table.
1078 Errors are fatal; no useful return value.
1080 Note: To use this method for new tables other than svc_acct and svc_phone,
1081 edit freeside-upgrade and add those tables to the duplicate_lock list.
1087 my $table = $self->table;
1089 warn "$me locking $table table\n" if $DEBUG;
1091 if ( driver_name =~ /^Pg/i ) {
1093 dbh->do("LOCK TABLE $table IN SHARE ROW EXCLUSIVE MODE")
1096 } elsif ( driver_name =~ /^mysql/i ) {
1098 dbh->do("SELECT * FROM duplicate_lock
1099 WHERE lockname = '$table'
1101 ) or die dbh->errstr;
1105 die "unknown database ". driver_name. "; don't know how to lock table";
1109 warn "$me acquired $table table lock\n" if $DEBUG;
1115 Inserts this record to the database. If there is an error, returns the error,
1116 otherwise returns false.
1124 warn "$self -> insert" if $DEBUG;
1126 my $error = $self->check;
1127 return $error if $error;
1129 #single-field non-null unique keys are given a value if empty
1130 #(like MySQL's AUTO_INCREMENT or Pg SERIAL)
1131 foreach ( $self->dbdef_table->unique_singles) {
1132 next if $self->getfield($_);
1133 next if $self->dbdef_table->column($_)->null eq 'NULL';
1137 #and also the primary key, if the database isn't going to
1138 my $primary_key = $self->dbdef_table->primary_key;
1140 if ( $primary_key ) {
1141 my $col = $self->dbdef_table->column($primary_key);
1144 uc($col->type) =~ /^(BIG)?SERIAL\d?/
1145 || ( driver_name eq 'Pg'
1146 && defined($col->default)
1147 && $col->quoted_default =~ /^nextval\(/i
1149 || ( driver_name eq 'mysql'
1150 && defined($col->local)
1151 && $col->local =~ /AUTO_INCREMENT/i
1153 $self->unique($primary_key) unless $self->getfield($primary_key) || $db_seq;
1156 my $table = $self->table;
1158 # Encrypt before the database
1159 if ( defined(eval '@FS::'. $table . '::encrypted_fields')
1160 && scalar( eval '@FS::'. $table . '::encrypted_fields')
1161 && $conf->exists('encryption')
1163 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
1164 next if $field eq 'payinfo'
1165 && ($self->isa('FS::payinfo_transaction_Mixin')
1166 || $self->isa('FS::payinfo_Mixin') )
1168 && !grep { $self->payby eq $_ } @encrypt_payby;
1169 $saved->{$field} = $self->getfield($field);
1170 $self->setfield($field, $self->encrypt($self->getfield($field)));
1174 #false laziness w/delete
1176 grep { defined($self->getfield($_)) && $self->getfield($_) ne "" }
1179 my @values = map { _quote( $self->getfield($_), $table, $_) } @real_fields;
1182 my $statement = "INSERT INTO $table ";
1183 if ( @real_fields ) {
1186 join( ', ', @real_fields ).
1188 join( ', ', @values ).
1192 $statement .= 'DEFAULT VALUES';
1194 warn "[debug]$me $statement\n" if $DEBUG > 1;
1195 my $sth = dbh->prepare($statement) or return dbh->errstr;
1197 local $SIG{HUP} = 'IGNORE';
1198 local $SIG{INT} = 'IGNORE';
1199 local $SIG{QUIT} = 'IGNORE';
1200 local $SIG{TERM} = 'IGNORE';
1201 local $SIG{TSTP} = 'IGNORE';
1202 local $SIG{PIPE} = 'IGNORE';
1204 $sth->execute or return $sth->errstr;
1206 # get inserted id from the database, if applicable & needed
1207 if ( $db_seq && ! $self->getfield($primary_key) ) {
1208 warn "[debug]$me retreiving sequence from database\n" if $DEBUG;
1212 if ( driver_name eq 'Pg' ) {
1214 #my $oid = $sth->{'pg_oid_status'};
1215 #my $i_sql = "SELECT $primary_key FROM $table WHERE oid = ?";
1217 my $default = $self->dbdef_table->column($primary_key)->quoted_default;
1218 unless ( $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i ) {
1219 dbh->rollback if $FS::UID::AutoCommit;
1220 return "can't parse $table.$primary_key default value".
1221 " for sequence name: $default";
1225 my $i_sql = "SELECT currval('$sequence')";
1226 my $i_sth = dbh->prepare($i_sql) or do {
1227 dbh->rollback if $FS::UID::AutoCommit;
1230 $i_sth->execute() or do { #$i_sth->execute($oid)
1231 dbh->rollback if $FS::UID::AutoCommit;
1232 return $i_sth->errstr;
1234 $insertid = $i_sth->fetchrow_arrayref->[0];
1236 } elsif ( driver_name eq 'mysql' ) {
1238 $insertid = dbh->{'mysql_insertid'};
1239 # work around mysql_insertid being null some of the time, ala RT :/
1240 unless ( $insertid ) {
1241 warn "WARNING: DBD::mysql didn't return mysql_insertid; ".
1242 "using SELECT LAST_INSERT_ID();";
1243 my $i_sql = "SELECT LAST_INSERT_ID()";
1244 my $i_sth = dbh->prepare($i_sql) or do {
1245 dbh->rollback if $FS::UID::AutoCommit;
1248 $i_sth->execute or do {
1249 dbh->rollback if $FS::UID::AutoCommit;
1250 return $i_sth->errstr;
1252 $insertid = $i_sth->fetchrow_arrayref->[0];
1257 dbh->rollback if $FS::UID::AutoCommit;
1258 return "don't know how to retreive inserted ids from ". driver_name.
1259 ", try using counterfiles (maybe run dbdef-create?)";
1263 $self->setfield($primary_key, $insertid);
1268 if ( defined( dbdef->table('h_'. $table) ) && ! $no_history ) {
1269 my $h_statement = $self->_h_statement('insert');
1270 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
1271 $h_sth = dbh->prepare($h_statement) or do {
1272 dbh->rollback if $FS::UID::AutoCommit;
1278 $h_sth->execute or return $h_sth->errstr if $h_sth;
1280 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1282 # Now that it has been saved, reset the encrypted fields so that $new
1283 # can still be used.
1284 foreach my $field (keys %{$saved}) {
1285 $self->setfield($field, $saved->{$field});
1293 Depriciated (use insert instead).
1298 cluck "warning: FS::Record::add deprecated!";
1299 insert @_; #call method in this scope
1304 Delete this record from the database. If there is an error, returns the error,
1305 otherwise returns false.
1312 my $statement = "DELETE FROM ". $self->table. " WHERE ". join(' AND ',
1314 $self->getfield($_) eq ''
1315 #? "( $_ IS NULL OR $_ = \"\" )"
1316 ? ( driver_name eq 'Pg'
1318 : "( $_ IS NULL OR $_ = \"\" )"
1320 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
1321 } ( $self->dbdef_table->primary_key )
1322 ? ( $self->dbdef_table->primary_key)
1323 : real_fields($self->table)
1325 warn "[debug]$me $statement\n" if $DEBUG > 1;
1326 my $sth = dbh->prepare($statement) or return dbh->errstr;
1329 if ( defined dbdef->table('h_'. $self->table) ) {
1330 my $h_statement = $self->_h_statement('delete');
1331 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
1332 $h_sth = dbh->prepare($h_statement) or return dbh->errstr;
1337 my $primary_key = $self->dbdef_table->primary_key;
1339 local $SIG{HUP} = 'IGNORE';
1340 local $SIG{INT} = 'IGNORE';
1341 local $SIG{QUIT} = 'IGNORE';
1342 local $SIG{TERM} = 'IGNORE';
1343 local $SIG{TSTP} = 'IGNORE';
1344 local $SIG{PIPE} = 'IGNORE';
1346 my $rc = $sth->execute or return $sth->errstr;
1347 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
1348 $h_sth->execute or return $h_sth->errstr if $h_sth;
1350 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1352 #no need to needlessly destoy the data either (causes problems actually)
1353 #undef $self; #no need to keep object!
1360 Depriciated (use delete instead).
1365 cluck "warning: FS::Record::del deprecated!";
1366 &delete(@_); #call method in this scope
1369 =item replace OLD_RECORD
1371 Replace the OLD_RECORD with this one in the database. If there is an error,
1372 returns the error, otherwise returns false.
1377 my ($new, $old) = (shift, shift);
1379 $old = $new->replace_old unless defined($old);
1381 warn "[debug]$me $new ->replace $old\n" if $DEBUG;
1383 if ( $new->can('replace_check') ) {
1384 my $error = $new->replace_check($old);
1385 return $error if $error;
1388 return "Records not in same table!" unless $new->table eq $old->table;
1390 my $primary_key = $old->dbdef_table->primary_key;
1391 return "Can't change primary key $primary_key ".
1392 'from '. $old->getfield($primary_key).
1393 ' to ' . $new->getfield($primary_key)
1395 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
1397 my $error = $new->check;
1398 return $error if $error;
1400 # Encrypt for replace
1402 if ( $conf->exists('encryption')
1403 && defined(eval '@FS::'. $new->table . '::encrypted_fields')
1404 && scalar( eval '@FS::'. $new->table . '::encrypted_fields')
1406 foreach my $field (eval '@FS::'. $new->table . '::encrypted_fields') {
1407 next if $field eq 'payinfo'
1408 && ($new->isa('FS::payinfo_transaction_Mixin')
1409 || $new->isa('FS::payinfo_Mixin') )
1411 && !grep { $new->payby eq $_ } @encrypt_payby;
1412 $saved->{$field} = $new->getfield($field);
1413 $new->setfield($field, $new->encrypt($new->getfield($field)));
1417 #my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
1418 my %diff = map { ($new->getfield($_) ne $old->getfield($_))
1419 ? ($_, $new->getfield($_)) : () } $old->fields;
1421 unless (keys(%diff) || $no_update_diff ) {
1422 carp "[warning]$me ". ref($new)."->replace ".
1423 ( $primary_key ? "$primary_key ".$new->get($primary_key) : '' ).
1424 ": records identical"
1425 unless $nowarn_identical;
1429 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
1431 "$_ = ". _quote($new->getfield($_),$old->table,$_)
1432 } real_fields($old->table)
1437 if ( $old->getfield($_) eq '' ) {
1439 #false laziness w/qsearch
1440 if ( driver_name eq 'Pg' ) {
1441 my $type = $old->dbdef_table->column($_)->type;
1442 if ( $type =~ /(int|(big)?serial)/i ) {
1445 qq-( $_ IS NULL OR $_ = '' )-;
1448 qq-( $_ IS NULL OR $_ = "" )-;
1452 "$_ = ". _quote($old->getfield($_),$old->table,$_);
1455 } ( $primary_key ? ( $primary_key ) : real_fields($old->table) )
1458 warn "[debug]$me $statement\n" if $DEBUG > 1;
1459 my $sth = dbh->prepare($statement) or return dbh->errstr;
1462 if ( defined dbdef->table('h_'. $old->table) ) {
1463 my $h_old_statement = $old->_h_statement('replace_old');
1464 warn "[debug]$me $h_old_statement\n" if $DEBUG > 2;
1465 $h_old_sth = dbh->prepare($h_old_statement) or return dbh->errstr;
1471 if ( defined dbdef->table('h_'. $new->table) ) {
1472 my $h_new_statement = $new->_h_statement('replace_new');
1473 warn "[debug]$me $h_new_statement\n" if $DEBUG > 2;
1474 $h_new_sth = dbh->prepare($h_new_statement) or return dbh->errstr;
1479 local $SIG{HUP} = 'IGNORE';
1480 local $SIG{INT} = 'IGNORE';
1481 local $SIG{QUIT} = 'IGNORE';
1482 local $SIG{TERM} = 'IGNORE';
1483 local $SIG{TSTP} = 'IGNORE';
1484 local $SIG{PIPE} = 'IGNORE';
1486 my $rc = $sth->execute or return $sth->errstr;
1487 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
1488 $h_old_sth->execute or return $h_old_sth->errstr if $h_old_sth;
1489 $h_new_sth->execute or return $h_new_sth->errstr if $h_new_sth;
1491 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1493 # Now that it has been saved, reset the encrypted fields so that $new
1494 # can still be used.
1495 foreach my $field (keys %{$saved}) {
1496 $new->setfield($field, $saved->{$field});
1504 my( $self ) = shift;
1505 warn "[$me] replace called with no arguments; autoloading old record\n"
1508 my $primary_key = $self->dbdef_table->primary_key;
1509 if ( $primary_key ) {
1510 $self->by_key( $self->$primary_key() ) #this is what's returned
1511 or croak "can't find ". $self->table. ".$primary_key ".
1512 $self->$primary_key();
1514 croak $self->table. " has no primary key; pass old record as argument";
1521 Depriciated (use replace instead).
1526 cluck "warning: FS::Record::rep deprecated!";
1527 replace @_; #call method in this scope
1532 Checks custom fields. Subclasses should still provide a check method to validate
1533 non-custom fields, foreign keys, etc., and call this method via $self->SUPER::check.
1539 foreach my $field ($self->virtual_fields) {
1540 my $error = $self->ut_textn($field);
1541 return $error if $error;
1546 =item virtual_fields [ TABLE ]
1548 Returns a list of virtual fields defined for the table. This should not
1549 be exported, and should only be called as an instance or class method.
1553 sub virtual_fields {
1556 $table = $self->table or confess "virtual_fields called on non-table";
1558 confess "Unknown table $table" unless dbdef->table($table);
1560 return () unless dbdef->table('part_virtual_field');
1562 unless ( $virtual_fields_cache{$table} ) {
1563 my $concat = [ "'cf_'", "name" ];
1564 my $query = "SELECT ".concat_sql($concat).' from part_virtual_field ' .
1565 "WHERE dbtable = '$table'";
1567 my $result = $dbh->selectcol_arrayref($query);
1568 confess "Error executing virtual fields query: $query: ". $dbh->errstr
1570 $virtual_fields_cache{$table} = $result;
1573 @{$virtual_fields_cache{$table}};
1577 =item process_batch_import JOB OPTIONS_HASHREF PARAMS
1579 Processes a batch import as a queued JSRPC job
1581 JOB is an FS::queue entry.
1583 OPTIONS_HASHREF can have the following keys:
1589 Table name (required).
1593 Arrayref of field names for static fields. They will be given values from the
1594 PARAMS hashref and passed as a "params" hashref to batch_import.
1598 Formats hashref. Keys are field names, values are listrefs that define the
1601 Each listref value can be a column name or a code reference. Coderefs are run
1602 with the row object, data and a FS::Conf object as the three parameters.
1603 For example, this coderef does the same thing as using the "columnname" string:
1606 my( $record, $data, $conf ) = @_;
1607 $record->columnname( $data );
1610 Coderefs are run after all "column name" fields are assigned.
1614 Optional format hashref of types. Keys are field names, values are "csv",
1615 "xls" or "fixedlength". Overrides automatic determination of file type
1618 =item format_headers
1620 Optional format hashref of header lines. Keys are field names, values are 0
1621 for no header, 1 to ignore the first line, or to higher numbers to ignore that
1624 =item format_sep_chars
1626 Optional format hashref of CSV sep_chars. Keys are field names, values are the
1627 CSV separation character.
1629 =item format_fixedlenth_formats
1631 Optional format hashref of fixed length format defintiions. Keys are field
1632 names, values Parse::FixedLength listrefs of field definitions.
1636 Set true to default to CSV file type if the filename does not contain a
1637 recognizable ".csv" or ".xls" extension (and type is not pre-specified by
1642 PARAMS is a hashref (or base64-encoded Storable hashref) containing the
1643 POSTed data. It must contain the field "uploaded files", generated by
1644 /elements/file-upload.html and containing the list of uploaded files.
1645 Currently only supports a single file named "file".
1649 # uploaded_files is kind of bizarre; fix that some time
1651 use Storable qw(thaw);
1654 sub process_batch_import {
1655 my($job, $opt) = ( shift, shift );
1657 my $table = $opt->{table};
1658 my @pass_params = $opt->{params} ? @{ $opt->{params} } : ();
1659 my %formats = %{ $opt->{formats} };
1662 # because some job-spawning code (JSRPC) pre-freezes the arguments,
1663 # and then the 'frozen' attribute doesn't get set, and thus $job->args
1664 # doesn't know to thaw them, we have to do this everywhere.
1666 $param = thaw(decode_base64($param));
1668 warn Dumper($param) if $DEBUG;
1670 my $files = $param->{'uploaded_files'}
1671 or die "No files provided.\n";
1673 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
1675 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
1676 my $file = $dir. $files{'file'};
1681 formats => \%formats,
1682 format_types => $opt->{format_types},
1683 format_headers => $opt->{format_headers},
1684 format_sep_chars => $opt->{format_sep_chars},
1685 format_fixedlength_formats => $opt->{format_fixedlength_formats},
1686 format_xml_formats => $opt->{format_xml_formats},
1687 format_asn_formats => $opt->{format_asn_formats},
1688 format_row_callbacks => $opt->{format_row_callbacks},
1693 format => $param->{format},
1694 params => { map { $_ => $param->{$_} } @pass_params },
1696 default_csv => $opt->{default_csv},
1697 postinsert_callback => $opt->{postinsert_callback},
1700 if ( $opt->{'batch_namecol'} ) {
1701 $iopt{'batch_namevalue'} = $param->{ $opt->{'batch_namecol'} };
1702 $iopt{$_} = $opt->{$_} foreach qw( batch_keycol batch_table batch_namecol );
1705 my $error = FS::Record::batch_import( \%iopt );
1709 die "$error\n" if $error;
1712 =item batch_import PARAM_HASHREF
1714 Class method for batch imports. Available params:
1720 =item format - usual way to specify import, with this format string selecting data from the formats and format_* info hashes
1726 =item format_headers
1728 =item format_sep_chars
1730 =item format_fixedlength_formats
1732 =item format_row_callbacks
1734 =item fields - Alternate way to specify import, specifying import fields directly as a listref
1736 =item preinsert_callback
1738 =item postinsert_callback
1744 FS::queue object, will be updated with progress
1750 csv, xls, fixedlength, xml
1761 warn "$me batch_import call with params: \n". Dumper($param)
1764 my $table = $param->{table};
1766 my $job = $param->{job};
1767 my $file = $param->{file};
1768 my $params = $param->{params} || {};
1770 my $custnum_prefix = $conf->config('cust_main-custnum-display_prefix');
1771 my $custnum_length = $conf->config('cust_main-custnum-display_length') || 8;
1773 my( $type, $header, $sep_char,
1774 $fixedlength_format, $xml_format, $asn_format,
1775 $parser_opt, $row_callback, @fields );
1777 my $postinsert_callback = '';
1778 $postinsert_callback = $param->{'postinsert_callback'}
1779 if $param->{'postinsert_callback'};
1780 my $preinsert_callback = '';
1781 $preinsert_callback = $param->{'preinsert_callback'}
1782 if $param->{'preinsert_callback'};
1784 if ( $param->{'format'} ) {
1786 my $format = $param->{'format'};
1787 my $formats = $param->{formats};
1788 die "unknown format $format" unless exists $formats->{ $format };
1790 $type = $param->{'format_types'}
1791 ? $param->{'format_types'}{ $format }
1792 : $param->{type} || 'csv';
1795 $header = $param->{'format_headers'}
1796 ? $param->{'format_headers'}{ $param->{'format'} }
1799 $sep_char = $param->{'format_sep_chars'}
1800 ? $param->{'format_sep_chars'}{ $param->{'format'} }
1803 $fixedlength_format =
1804 $param->{'format_fixedlength_formats'}
1805 ? $param->{'format_fixedlength_formats'}{ $param->{'format'} }
1809 $param->{'format_parser_opts'}
1810 ? $param->{'format_parser_opts'}{ $param->{'format'} }
1814 $param->{'format_xml_formats'}
1815 ? $param->{'format_xml_formats'}{ $param->{'format'} }
1819 $param->{'format_asn_formats'}
1820 ? $param->{'format_asn_formats'}{ $param->{'format'} }
1824 $param->{'format_row_callbacks'}
1825 ? $param->{'format_row_callbacks'}{ $param->{'format'} }
1828 @fields = @{ $formats->{ $format } };
1830 } elsif ( $param->{'fields'} ) {
1832 $type = ''; #infer from filename
1835 $fixedlength_format = '';
1837 @fields = @{ $param->{'fields'} };
1840 die "neither format nor fields specified";
1843 #my $file = $param->{file};
1846 if ( $file =~ /\.(\w+)$/i ) {
1850 warn "can't parse file type from filename $file; defaulting to CSV";
1854 if $param->{'default_csv'} && $type ne 'xls';
1862 my $asn_header_buffer;
1863 if ( $type eq 'csv' || $type eq 'fixedlength' ) {
1865 if ( $type eq 'csv' ) {
1867 $parser_opt->{'binary'} = 1;
1868 $parser_opt->{'sep_char'} = $sep_char if $sep_char;
1869 $parser = Text::CSV_XS->new($parser_opt);
1871 } elsif ( $type eq 'fixedlength' ) {
1873 eval "use Parse::FixedLength;";
1875 $parser = Parse::FixedLength->new($fixedlength_format, $parser_opt);
1878 die "Unknown file type $type\n";
1881 @buffer = split(/\r?\n/, slurp($file) );
1882 splice(@buffer, 0, ($header || 0) );
1883 $count = scalar(@buffer);
1885 } elsif ( $type eq 'xls' ) {
1887 eval "use Spreadsheet::ParseExcel;";
1890 eval "use DateTime::Format::Excel;";
1891 #for now, just let the error be thrown if it is used, since only CDR
1892 # formats bill_west and troop use it, not other excel-parsing things
1895 my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($file);
1897 $parser = $excel->{Worksheet}[0]; #first sheet
1899 $count = $parser->{MaxRow} || $parser->{MinRow};
1902 $row = $header || 0;
1904 } elsif ( $type eq 'xml' ) {
1907 eval "use XML::Simple;";
1909 my $xmlrow = $xml_format->{'xmlrow'};
1910 $parser = $xml_format->{'xmlkeys'};
1911 die 'no xmlkeys specified' unless ref $parser eq 'ARRAY';
1912 my $data = XML::Simple::XMLin(
1914 'SuppressEmpty' => '', #sets empty values to ''
1918 $rows = $rows->{$_} foreach @$xmlrow;
1919 $rows = [ $rows ] if ref($rows) ne 'ARRAY';
1920 $count = @buffer = @$rows;
1922 } elsif ( $type eq 'asn.1' ) {
1924 eval "use Convert::ASN1";
1927 my $asn = Convert::ASN1->new;
1928 $asn->prepare( $asn_format->{'spec'} ) or die $asn->error;
1930 $parser = $asn->find( $asn_format->{'macro'} ) or die $asn->error;
1932 my $data = slurp($file);
1933 my $asn_output = $parser->decode( $data )
1934 or return "No ". $asn_format->{'macro'}. " found\n";
1936 $asn_header_buffer = &{ $asn_format->{'header_buffer'} }( $asn_output );
1938 my $rows = &{ $asn_format->{'arrayref'} }( $asn_output );
1939 $count = @buffer = @$rows;
1942 die "Unknown file type $type\n";
1947 local $SIG{HUP} = 'IGNORE';
1948 local $SIG{INT} = 'IGNORE';
1949 local $SIG{QUIT} = 'IGNORE';
1950 local $SIG{TERM} = 'IGNORE';
1951 local $SIG{TSTP} = 'IGNORE';
1952 local $SIG{PIPE} = 'IGNORE';
1954 my $oldAutoCommit = $FS::UID::AutoCommit;
1955 local $FS::UID::AutoCommit = 0;
1958 #my $params = $param->{params} || {};
1959 if ( $param->{'batch_namecol'} && $param->{'batch_namevalue'} ) {
1960 my $batch_col = $param->{'batch_keycol'};
1962 my $batch_class = 'FS::'. $param->{'batch_table'};
1963 my $batch = $batch_class->new({
1964 $param->{'batch_namecol'} => $param->{'batch_namevalue'}
1966 my $error = $batch->insert;
1968 $dbh->rollback if $oldAutoCommit;
1969 return "can't insert batch record: $error";
1971 #primary key via dbdef? (so the column names don't have to match)
1972 my $batch_value = $batch->get( $param->{'batch_keycol'} );
1974 $params->{ $batch_col } = $batch_value;
1977 #my $job = $param->{job};
1980 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
1984 my %hash = %$params;
1985 if ( $type eq 'csv' ) {
1987 last unless scalar(@buffer);
1988 $line = shift(@buffer);
1990 next if $line =~ /^\s*$/; #skip empty lines
1992 $line = &{$row_callback}($line) if $row_callback;
1994 next if $line =~ /^\s*$/; #skip empty lines
1996 $parser->parse($line) or do {
1997 $dbh->rollback if $oldAutoCommit;
1998 return "can't parse: ". $parser->error_input() . " " . $parser->error_diag;
2000 @columns = $parser->fields();
2002 } elsif ( $type eq 'fixedlength' ) {
2004 last unless scalar(@buffer);
2005 $line = shift(@buffer);
2007 @columns = $parser->parse($line);
2009 } elsif ( $type eq 'xls' ) {
2011 last if $row > ($parser->{MaxRow} || $parser->{MinRow})
2012 || ! $parser->{Cells}[$row];
2014 my @row = @{ $parser->{Cells}[$row] };
2015 @columns = map $_->{Val}, @row;
2018 #warn $z++. ": $_\n" for @columns;
2020 } elsif ( $type eq 'xml' ) {
2022 # $parser = [ 'Column0Key', 'Column1Key' ... ]
2023 last unless scalar(@buffer);
2024 my $row = shift @buffer;
2025 @columns = @{ $row }{ @$parser };
2027 } elsif ( $type eq 'asn.1' ) {
2029 last unless scalar(@buffer);
2030 my $row = shift @buffer;
2031 &{ $asn_format->{row_callback} }( $row, $asn_header_buffer )
2032 if $asn_format->{row_callback};
2033 foreach my $key ( keys %{ $asn_format->{map} } ) {
2034 $hash{$key} = &{ $asn_format->{map}{$key} }( $row, $asn_header_buffer );
2038 die "Unknown file type $type\n";
2043 foreach my $field ( @fields ) {
2045 my $value = shift @columns;
2047 if ( ref($field) eq 'CODE' ) {
2048 #&{$field}(\%hash, $value);
2049 push @later, $field, $value;
2051 #??? $hash{$field} = $value if length($value);
2052 $hash{$field} = $value if defined($value) && length($value);
2057 if ( $custnum_prefix && $hash{custnum} =~ /^$custnum_prefix(0*([1-9]\d*))$/
2058 && length($1) == $custnum_length ) {
2059 $hash{custnum} = $2;
2062 #my $table = $param->{table};
2063 my $class = "FS::$table";
2065 my $record = $class->new( \%hash );
2068 while ( scalar(@later) ) {
2069 my $sub = shift @later;
2070 my $data = shift @later;
2072 &{$sub}($record, $data, $conf, $param); # $record->&{$sub}($data, $conf)
2075 $dbh->rollback if $oldAutoCommit;
2076 return "can't insert record". ( $line ? " for $line" : '' ). ": $@";
2078 last if exists( $param->{skiprow} );
2080 next if exists( $param->{skiprow} );
2082 if ( $preinsert_callback ) {
2083 my $error = &{$preinsert_callback}($record, $param);
2085 $dbh->rollback if $oldAutoCommit;
2086 return "preinsert_callback error". ( $line ? " for $line" : '' ).
2089 next if exists $param->{skiprow} && $param->{skiprow};
2092 my $error = $record->insert;
2095 $dbh->rollback if $oldAutoCommit;
2096 return "can't insert record". ( $line ? " for $line" : '' ). ": $error";
2102 if ( $postinsert_callback ) {
2103 my $error = &{$postinsert_callback}($record, $param);
2105 $dbh->rollback if $oldAutoCommit;
2106 return "postinsert_callback error". ( $line ? " for $line" : '' ).
2111 if ( $job && time - $min_sec > $last ) { #progress bar
2112 $job->update_statustext( int(100 * $imported / $count) );
2118 unless ( $imported || $param->{empty_ok} ) {
2119 $dbh->rollback if $oldAutoCommit;
2120 return "Empty file!";
2123 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2130 my( $self, $action, $time ) = @_;
2134 my %nohistory = map { $_=>1 } $self->nohistory_fields;
2137 grep { defined($self->get($_)) && $self->get($_) ne "" && ! $nohistory{$_} }
2138 real_fields($self->table);
2141 # If we're encrypting then don't store the payinfo in the history
2142 if ( $conf && $conf->exists('encryption') && $self->table ne 'banned_pay' ) {
2143 @fields = grep { $_ ne 'payinfo' } @fields;
2146 my @values = map { _quote( $self->getfield($_), $self->table, $_) } @fields;
2148 "INSERT INTO h_". $self->table. " ( ".
2149 join(', ', qw(history_date history_user history_action), @fields ).
2151 join(', ', $time, dbh->quote(getotaker()), dbh->quote($action), @values).
2158 B<Warning>: External use is B<deprecated>.
2160 Replaces COLUMN in record with a unique number, using counters in the
2161 filesystem. Used by the B<insert> method on single-field unique columns
2162 (see L<DBIx::DBSchema::Table>) and also as a fallback for primary keys
2163 that aren't SERIAL (Pg) or AUTO_INCREMENT (mysql).
2165 Returns the new value.
2170 my($self,$field) = @_;
2171 my($table)=$self->table;
2173 croak "Unique called on field $field, but it is ",
2174 $self->getfield($field),
2176 if $self->getfield($field);
2178 #warn "table $table is tainted" if is_tainted($table);
2179 #warn "field $field is tainted" if is_tainted($field);
2181 my($counter) = new File::CounterFile "$table.$field",0;
2183 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
2185 # my($counter) = new File::CounterFile "$user/$table.$field",0;
2188 my $index = $counter->inc;
2189 $index = $counter->inc while qsearchs($table, { $field=>$index } );
2191 $index =~ /^(\d*)$/;
2194 $self->setfield($field,$index);
2198 =item ut_float COLUMN
2200 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
2201 null. If there is an error, returns the error, otherwise returns false.
2206 my($self,$field)=@_ ;
2207 ($self->getfield($field) =~ /^\s*(\d+\.\d+)\s*$/ ||
2208 $self->getfield($field) =~ /^\s*(\d+)\s*$/ ||
2209 $self->getfield($field) =~ /^\s*(\d+\.\d+e\d+)\s*$/ ||
2210 $self->getfield($field) =~ /^\s*(\d+e\d+)\s*$/)
2211 or return "Illegal or empty (float) $field: ". $self->getfield($field);
2212 $self->setfield($field,$1);
2215 =item ut_floatn COLUMN
2217 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
2218 null. If there is an error, returns the error, otherwise returns false.
2222 #false laziness w/ut_ipn
2224 my( $self, $field ) = @_;
2225 if ( $self->getfield($field) =~ /^()$/ ) {
2226 $self->setfield($field,'');
2229 $self->ut_float($field);
2233 =item ut_sfloat COLUMN
2235 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10.
2236 May not be null. If there is an error, returns the error, otherwise returns
2242 my($self,$field)=@_ ;
2243 ($self->getfield($field) =~ /^\s*(-?\d+\.\d+)\s*$/ ||
2244 $self->getfield($field) =~ /^\s*(-?\d+)\s*$/ ||
2245 $self->getfield($field) =~ /^\s*(-?\d+\.\d+[eE]-?\d+)\s*$/ ||
2246 $self->getfield($field) =~ /^\s*(-?\d+[eE]-?\d+)\s*$/)
2247 or return "Illegal or empty (float) $field: ". $self->getfield($field);
2248 $self->setfield($field,$1);
2251 =item ut_sfloatn COLUMN
2253 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
2254 null. If there is an error, returns the error, otherwise returns false.
2259 my( $self, $field ) = @_;
2260 if ( $self->getfield($field) =~ /^()$/ ) {
2261 $self->setfield($field,'');
2264 $self->ut_sfloat($field);
2268 =item ut_snumber COLUMN
2270 Check/untaint signed numeric data (whole numbers). If there is an error,
2271 returns the error, otherwise returns false.
2276 my($self, $field) = @_;
2277 $self->getfield($field) =~ /^\s*(-?)\s*(\d+)\s*$/
2278 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
2279 $self->setfield($field, "$1$2");
2283 =item ut_snumbern COLUMN
2285 Check/untaint signed numeric data (whole numbers). If there is an error,
2286 returns the error, otherwise returns false.
2291 my($self, $field) = @_;
2292 $self->getfield($field) =~ /^\s*(-?)\s*(\d*)\s*$/
2293 or return "Illegal (numeric) $field: ". $self->getfield($field);
2295 return "Illegal (numeric) $field: ". $self->getfield($field)
2298 $self->setfield($field, "$1$2");
2302 =item ut_number COLUMN
2304 Check/untaint simple numeric data (whole numbers). May not be null. If there
2305 is an error, returns the error, otherwise returns false.
2310 my($self,$field)=@_;
2311 $self->getfield($field) =~ /^\s*(\d+)\s*$/
2312 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
2313 $self->setfield($field,$1);
2317 =item ut_numbern COLUMN
2319 Check/untaint simple numeric data (whole numbers). May be null. If there is
2320 an error, returns the error, otherwise returns false.
2325 my($self,$field)=@_;
2326 $self->getfield($field) =~ /^\s*(\d*)\s*$/
2327 or return "Illegal (numeric) $field: ". $self->getfield($field);
2328 $self->setfield($field,$1);
2332 =item ut_decimal COLUMN[, DIGITS]
2334 Check/untaint decimal numbers (up to DIGITS decimal places. If there is an
2335 error, returns the error, otherwise returns false.
2337 =item ut_decimaln COLUMN[, DIGITS]
2339 Check/untaint decimal numbers. May be null. If there is an error, returns
2340 the error, otherwise returns false.
2345 my($self, $field, $digits) = @_;
2347 $self->getfield($field) =~ /^\s*(\d+(\.\d{0,$digits})?)\s*$/
2348 or return "Illegal or empty (decimal) $field: ".$self->getfield($field);
2349 $self->setfield($field, $1);
2354 my($self, $field, $digits) = @_;
2355 $self->getfield($field) =~ /^\s*(\d*(\.\d{0,$digits})?)\s*$/
2356 or return "Illegal (decimal) $field: ".$self->getfield($field);
2357 $self->setfield($field, $1);
2361 =item ut_money COLUMN
2363 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
2364 is an error, returns the error, otherwise returns false.
2369 my($self,$field)=@_;
2371 if ( $self->getfield($field) eq '' ) {
2372 $self->setfield($field, 0);
2373 } elsif ( $self->getfield($field) =~ /^\s*(\-)?\s*(\d*)(\.\d{1})\s*$/ ) {
2374 #handle one decimal place without barfing out
2375 $self->setfield($field, ( ($1||''). ($2||''). ($3.'0') ) || 0);
2376 } elsif ( $self->getfield($field) =~ /^\s*(\-)?\s*(\d*)(\.\d{2})?\s*$/ ) {
2377 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
2379 return "Illegal (money) $field: ". $self->getfield($field);
2385 =item ut_moneyn COLUMN
2387 Check/untaint monetary numbers. May be negative. If there
2388 is an error, returns the error, otherwise returns false.
2393 my($self,$field)=@_;
2394 if ($self->getfield($field) eq '') {
2395 $self->setfield($field, '');
2398 $self->ut_money($field);
2401 =item ut_text COLUMN
2403 Check/untaint text. Alphanumerics, spaces, and the following punctuation
2404 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / = [ ] < >
2405 May not be null. If there is an error, returns the error, otherwise returns
2411 my($self,$field)=@_;
2412 #warn "msgcat ". \&msgcat. "\n";
2413 #warn "notexist ". \¬exist. "\n";
2414 #warn "AUTOLOAD ". \&AUTOLOAD. "\n";
2415 # \p{Word} = alphanumerics, marks (diacritics), and connectors
2416 # see perldoc perluniprops
2417 $self->getfield($field)
2418 =~ /^([\p{Word} \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]\<\>$money_char]+)$/
2419 or return gettext('illegal_or_empty_text'). " $field: ".
2420 $self->getfield($field);
2421 $self->setfield($field,$1);
2425 =item ut_textn COLUMN
2427 Check/untaint text. Alphanumerics, spaces, and the following punctuation
2428 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / = [ ] < >
2429 May be null. If there is an error, returns the error, otherwise returns false.
2434 my($self,$field)=@_;
2435 return $self->setfield($field, '') if $self->getfield($field) =~ /^$/;
2436 $self->ut_text($field);
2439 =item ut_alpha COLUMN
2441 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
2442 an error, returns the error, otherwise returns false.
2447 my($self,$field)=@_;
2448 $self->getfield($field) =~ /^(\w+)$/
2449 or return "Illegal or empty (alphanumeric) $field: ".
2450 $self->getfield($field);
2451 $self->setfield($field,$1);
2455 =item ut_alphan COLUMN
2457 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
2458 error, returns the error, otherwise returns false.
2463 my($self,$field)=@_;
2464 $self->getfield($field) =~ /^(\w*)$/
2465 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
2466 $self->setfield($field,$1);
2470 =item ut_alphasn COLUMN
2472 Check/untaint alphanumeric strings, spaces allowed. May be null. If there is
2473 an error, returns the error, otherwise returns false.
2478 my($self,$field)=@_;
2479 $self->getfield($field) =~ /^([\w ]*)$/
2480 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
2481 $self->setfield($field,$1);
2486 =item ut_alpha_lower COLUMN
2488 Check/untaint lowercase alphanumeric strings (no spaces). May not be null. If
2489 there is an error, returns the error, otherwise returns false.
2493 sub ut_alpha_lower {
2494 my($self,$field)=@_;
2495 $self->getfield($field) =~ /[[:upper:]]/
2496 and return "Uppercase characters are not permitted in $field";
2497 $self->ut_alpha($field);
2500 =item ut_phonen COLUMN [ COUNTRY ]
2502 Check/untaint phone numbers. May be null. If there is an error, returns
2503 the error, otherwise returns false.
2505 Takes an optional two-letter ISO 3166-1 alpha-2 country code; without
2506 it or with unsupported countries, ut_phonen simply calls ut_alphan.
2511 my( $self, $field, $country ) = @_;
2512 return $self->ut_alphan($field) unless defined $country;
2513 my $phonen = $self->getfield($field);
2514 if ( $phonen eq '' ) {
2515 $self->setfield($field,'');
2516 } elsif ( $country eq 'US' || $country eq 'CA' ) {
2518 $phonen = $conf->config('cust_main-default_areacode').$phonen
2519 if length($phonen)==7 && $conf->config('cust_main-default_areacode');
2520 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
2521 or return gettext('illegal_phone'). " $field: ". $self->getfield($field);
2522 $phonen = "$1-$2-$3";
2523 $phonen .= " x$4" if $4;
2524 $self->setfield($field,$phonen);
2526 warn "warning: don't know how to check phone numbers for country $country";
2527 return $self->ut_textn($field);
2534 Check/untaint hexadecimal values.
2539 my($self, $field) = @_;
2540 $self->getfield($field) =~ /^([\da-fA-F]+)$/
2541 or return "Illegal (hex) $field: ". $self->getfield($field);
2542 $self->setfield($field, uc($1));
2546 =item ut_hexn COLUMN
2548 Check/untaint hexadecimal values. May be null.
2553 my($self, $field) = @_;
2554 $self->getfield($field) =~ /^([\da-fA-F]*)$/
2555 or return "Illegal (hex) $field: ". $self->getfield($field);
2556 $self->setfield($field, uc($1));
2560 =item ut_mac_addr COLUMN
2562 Check/untaint mac addresses. May be null.
2567 my($self, $field) = @_;
2569 my $mac = $self->get($field);
2572 $self->set($field, $mac);
2574 my $e = $self->ut_hex($field);
2577 return "Illegal (mac address) $field: ". $self->getfield($field)
2578 unless length($self->getfield($field)) == 12;
2584 =item ut_mac_addrn COLUMN
2586 Check/untaint mac addresses. May be null.
2591 my($self, $field) = @_;
2592 ($self->getfield($field) eq '') ? '' : $self->ut_mac_addr($field);
2597 Check/untaint ip addresses. IPv4 only for now, though ::1 is auto-translated
2603 my( $self, $field ) = @_;
2604 $self->setfield($field, '127.0.0.1') if $self->getfield($field) eq '::1';
2605 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
2606 or return "Illegal (IP address) $field: ". $self->getfield($field);
2607 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
2608 $self->setfield($field, "$1.$2.$3.$4");
2614 Check/untaint ip addresses. IPv4 only for now, though ::1 is auto-translated
2615 to 127.0.0.1. May be null.
2620 my( $self, $field ) = @_;
2621 if ( $self->getfield($field) =~ /^()$/ ) {
2622 $self->setfield($field,'');
2625 $self->ut_ip($field);
2629 =item ut_ip46 COLUMN
2631 Check/untaint IPv4 or IPv6 address.
2636 my( $self, $field ) = @_;
2637 my $ip = NetAddr::IP->new($self->getfield($field))
2638 or return "Illegal (IP address) $field: ".$self->getfield($field);
2639 $self->setfield($field, lc($ip->addr));
2645 Check/untaint IPv6 or IPv6 address. May be null.
2650 my( $self, $field ) = @_;
2651 if ( $self->getfield($field) =~ /^$/ ) {
2652 $self->setfield($field, '');
2655 $self->ut_ip46($field);
2658 =item ut_coord COLUMN [ LOWER [ UPPER ] ]
2660 Check/untaint coordinates.
2661 Accepts the following forms:
2671 The "DDD MM SS" and "DDD MM MMM" are potentially ambiguous.
2672 The latter form (that is, the MMM are thousands of minutes) is
2673 assumed if the "MMM" is exactly three digits or two digits > 59.
2675 To be safe, just use the DDD.DDDDD form.
2677 If LOWER or UPPER are specified, then the coordinate is checked
2678 for lower and upper bounds, respectively.
2683 my ($self, $field) = (shift, shift);
2686 if ( $field =~ /latitude/ ) {
2687 $lower = $lat_lower;
2689 } elsif ( $field =~ /longitude/ ) {
2691 $upper = $lon_upper;
2694 my $coord = $self->getfield($field);
2695 my $neg = $coord =~ s/^(-)//;
2697 my ($d, $m, $s) = (0, 0, 0);
2700 (($d) = ($coord =~ /^(\s*\d{1,3}(?:\.\d+)?)\s*$/)) ||
2701 (($d, $m) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2}(?:\.\d+))\s*$/)) ||
2702 (($d, $m, $s) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2})\s+(\d{1,3})\s*$/))
2704 $s = (((($s =~ /^\d{3}$/) or $s > 59) ? ($s / 1000) : ($s / 60)) / 60);
2707 return "Invalid (coordinate with minutes > 59) $field: "
2708 . $self->getfield($field);
2711 $coord = ($neg ? -1 : 1) * sprintf('%.8f', $d + $m + $s);
2713 if (defined($lower) and ($coord < $lower)) {
2714 return "Invalid (coordinate < $lower) $field: "
2715 . $self->getfield($field);;
2718 if (defined($upper) and ($coord > $upper)) {
2719 return "Invalid (coordinate > $upper) $field: "
2720 . $self->getfield($field);;
2723 $self->setfield($field, $coord);
2727 return "Invalid (coordinate) $field: " . $self->getfield($field);
2731 =item ut_coordn COLUMN [ LOWER [ UPPER ] ]
2733 Same as ut_coord, except optionally null.
2739 my ($self, $field) = (shift, shift);
2741 if ($self->getfield($field) =~ /^\s*$/) {
2744 return $self->ut_coord($field, @_);
2749 =item ut_domain COLUMN
2751 Check/untaint host and domain names. May not be null.
2756 my( $self, $field ) = @_;
2757 #$self->getfield($field) =~/^(\w+\.)*\w+$/
2758 $self->getfield($field) =~/^(([\w\-]+\.)*\w+)$/
2759 or return "Illegal (hostname) $field: ". $self->getfield($field);
2760 $self->setfield($field,$1);
2764 =item ut_domainn COLUMN
2766 Check/untaint host and domain names. May be null.
2771 my( $self, $field ) = @_;
2772 if ( $self->getfield($field) =~ /^()$/ ) {
2773 $self->setfield($field,'');
2776 $self->ut_domain($field);
2780 =item ut_name COLUMN
2782 Check/untaint proper names; allows alphanumerics, spaces and the following
2783 punctuation: , . - '
2790 my( $self, $field ) = @_;
2791 # warn "ut_name allowed alphanumerics: +(sort grep /\w/, map { chr() } 0..255), "\n";
2792 $self->getfield($field) =~ /^([\p{Word} \,\.\-\']+)$/
2793 or return gettext('illegal_name'). " $field: ". $self->getfield($field);
2798 $self->setfield($field, $name);
2802 =item ut_namen COLUMN
2804 Check/untaint proper names; allows alphanumerics, spaces and the following
2805 punctuation: , . - '
2812 my( $self, $field ) = @_;
2813 return $self->setfield($field, '') if $self->getfield($field) =~ /^$/;
2814 $self->ut_name($field);
2819 Check/untaint zip codes.
2823 my @zip_reqd_countries = qw( AU CA US ); #CA, US implicit...
2826 my( $self, $field, $country ) = @_;
2828 if ( $country eq 'US' ) {
2830 $self->getfield($field) =~ /^\s*(\d{5}(\-\d{4})?)\s*$/
2831 or return gettext('illegal_zip'). " $field for country $country: ".
2832 $self->getfield($field);
2833 $self->setfield($field, $1);
2835 } elsif ( $country eq 'CA' ) {
2837 $self->getfield($field) =~ /^\s*([A-Z]\d[A-Z])\s*(\d[A-Z]\d)\s*$/i
2838 or return gettext('illegal_zip'). " $field for country $country: ".
2839 $self->getfield($field);
2840 $self->setfield($field, "$1 $2");
2844 if ( $self->getfield($field) =~ /^\s*$/
2845 && ( !$country || ! grep { $_ eq $country } @zip_reqd_countries )
2848 $self->setfield($field,'');
2850 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{0,8}\w)\s*$/
2851 or return gettext('illegal_zip'). " $field: ". $self->getfield($field);
2852 $self->setfield($field,$1);
2860 =item ut_country COLUMN
2862 Check/untaint country codes. Country names are changed to codes, if possible -
2863 see L<Locale::Country>.
2868 my( $self, $field ) = @_;
2869 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
2870 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
2871 && country2code($1) ) {
2872 $self->setfield($field,uc(country2code($1)));
2875 $self->getfield($field) =~ /^(\w\w)$/
2876 or return "Illegal (country) $field: ". $self->getfield($field);
2877 $self->setfield($field,uc($1));
2881 =item ut_anything COLUMN
2883 Untaints arbitrary data. Be careful.
2888 my( $self, $field ) = @_;
2889 $self->getfield($field) =~ /^(.*)$/s
2890 or return "Illegal $field: ". $self->getfield($field);
2891 $self->setfield($field,$1);
2895 =item ut_enum COLUMN CHOICES_ARRAYREF
2897 Check/untaint a column, supplying all possible choices, like the "enum" type.
2902 my( $self, $field, $choices ) = @_;
2903 foreach my $choice ( @$choices ) {
2904 if ( $self->getfield($field) eq $choice ) {
2905 $self->setfield($field, $choice);
2909 return "Illegal (enum) field $field: ". $self->getfield($field);
2912 =item ut_enumn COLUMN CHOICES_ARRAYREF
2914 Like ut_enum, except the null value is also allowed.
2919 my( $self, $field, $choices ) = @_;
2920 $self->getfield($field)
2921 ? $self->ut_enum($field, $choices)
2925 =item ut_flag COLUMN
2927 Check/untaint a column if it contains either an empty string or 'Y'. This
2928 is the standard form for boolean flags in Freeside.
2933 my( $self, $field ) = @_;
2934 my $value = uc($self->getfield($field));
2935 if ( $value eq '' or $value eq 'Y' ) {
2936 $self->setfield($field, $value);
2939 return "Illegal (flag) field $field: $value";
2942 =item ut_foreign_key COLUMN FOREIGN_TABLE FOREIGN_COLUMN
2944 Check/untaint a foreign column key. Call a regular ut_ method (like ut_number)
2945 on the column first.
2949 sub ut_foreign_key {
2950 my( $self, $field, $table, $foreign ) = @_;
2951 return '' if $no_check_foreign;
2952 qsearchs($table, { $foreign => $self->getfield($field) })
2953 or return "Can't find ". $self->table. ".$field ". $self->getfield($field).
2954 " in $table.$foreign";
2958 =item ut_foreign_keyn COLUMN FOREIGN_TABLE FOREIGN_COLUMN
2960 Like ut_foreign_key, except the null value is also allowed.
2964 sub ut_foreign_keyn {
2965 my( $self, $field, $table, $foreign ) = @_;
2966 $self->getfield($field)
2967 ? $self->ut_foreign_key($field, $table, $foreign)
2971 =item ut_agentnum_acl COLUMN [ NULL_RIGHT | NULL_RIGHT_LISTREF ]
2973 Checks this column as an agentnum, taking into account the current users's
2974 ACLs. NULL_RIGHT or NULL_RIGHT_LISTREF, if specified, indicates the access
2975 right or rights allowing no agentnum.
2979 sub ut_agentnum_acl {
2980 my( $self, $field ) = (shift, shift);
2981 my $null_acl = scalar(@_) ? shift : [];
2982 $null_acl = [ $null_acl ] unless ref($null_acl);
2984 my $error = $self->ut_foreign_keyn($field, 'agent', 'agentnum');
2985 return "Illegal agentnum: $error" if $error;
2987 my $curuser = $FS::CurrentUser::CurrentUser;
2989 if ( $self->$field() ) {
2991 return "Access denied"
2992 unless $curuser->agentnum($self->$field());
2996 return "Access denied"
2997 unless grep $curuser->access_right($_), @$null_acl;
3005 =item fields [ TABLE ]
3007 This is a wrapper for real_fields. Code that called
3008 fields before should probably continue to call fields.
3013 my $something = shift;
3015 if($something->isa('FS::Record')) {
3016 $table = $something->table;
3018 $table = $something;
3019 $something = "FS::$table";
3021 return (real_fields($table));
3025 =item encrypt($value)
3027 Encrypts the credit card using a combination of PK to encrypt and uuencode to armour.
3029 Returns the encrypted string.
3031 You should generally not have to worry about calling this, as the system handles this for you.
3036 my ($self, $value) = @_;
3037 my $encrypted = $value;
3039 if ($conf->exists('encryption')) {
3040 if ($self->is_encrypted($value)) {
3041 # Return the original value if it isn't plaintext.
3042 $encrypted = $value;
3045 if (ref($rsa_encrypt) =~ /::RSA/) { # We Can Encrypt
3046 # RSA doesn't like the empty string so let's pack it up
3047 # The database doesn't like the RSA data so uuencode it
3048 my $length = length($value)+1;
3049 $encrypted = pack("u*",$rsa_encrypt->encrypt(pack("Z$length",$value)));
3051 die ("You can't encrypt w/o a valid RSA engine - Check your installation or disable encryption");
3058 =item is_encrypted($value)
3060 Checks to see if the string is encrypted and returns true or false (1/0) to indicate it's status.
3066 my ($self, $value) = @_;
3067 # Possible Bug - Some work may be required here....
3069 if ($value =~ /^M/ && length($value) > 80) {
3076 =item decrypt($value)
3078 Uses the private key to decrypt the string. Returns the decryoted string or undef on failure.
3080 You should generally not have to worry about calling this, as the system handles this for you.
3085 my ($self,$value) = @_;
3086 my $decrypted = $value; # Will return the original value if it isn't encrypted or can't be decrypted.
3087 if ($conf->exists('encryption') && $self->is_encrypted($value)) {
3089 if (ref($rsa_decrypt) =~ /::RSA/) {
3090 my $encrypted = unpack ("u*", $value);
3091 $decrypted = unpack("Z*", eval{$rsa_decrypt->decrypt($encrypted)});
3092 if ($@) {warn "Decryption Failed"};
3100 #Initialize the Module
3101 $rsa_module = 'Crypt::OpenSSL::RSA'; # The Default
3103 if ($conf->exists('encryptionmodule') && $conf->config('encryptionmodule') ne '') {
3104 $rsa_module = $conf->config('encryptionmodule');
3108 eval ("require $rsa_module"); # No need to import the namespace
3111 # Initialize Encryption
3112 if ($conf->exists('encryptionpublickey') && $conf->config('encryptionpublickey') ne '') {
3113 my $public_key = join("\n",$conf->config('encryptionpublickey'));
3114 $rsa_encrypt = $rsa_module->new_public_key($public_key);
3117 # Intitalize Decryption
3118 if ($conf->exists('encryptionprivatekey') && $conf->config('encryptionprivatekey') ne '') {
3119 my $private_key = join("\n",$conf->config('encryptionprivatekey'));
3120 $rsa_decrypt = $rsa_module->new_private_key($private_key);
3124 =item h_search ACTION
3126 Given an ACTION, either "insert", or "delete", returns the appropriate history
3127 record corresponding to this record, if any.
3132 my( $self, $action ) = @_;
3134 my $table = $self->table;
3137 my $primary_key = dbdef->table($table)->primary_key;
3140 'table' => "h_$table",
3141 'hashref' => { $primary_key => $self->$primary_key(),
3142 'history_action' => $action,
3150 Given an ACTION, either "insert", or "delete", returns the timestamp of the
3151 appropriate history record corresponding to this record, if any.
3156 my($self, $action) = @_;
3157 my $h = $self->h_search($action);
3158 $h ? $h->history_date : '';
3161 =item scalar_sql SQL [ PLACEHOLDER, ... ]
3163 A class or object method. Executes the sql statement represented by SQL and
3164 returns a scalar representing the result: the first column of the first row.
3166 Dies on bogus SQL. Returns an empty string if no row is returned.
3168 Typically used for statments which return a single value such as "SELECT
3169 COUNT(*) FROM table WHERE something" OR "SELECT column FROM table WHERE key = ?"
3174 my($self, $sql) = (shift, shift);
3175 my $sth = dbh->prepare($sql) or die dbh->errstr;
3177 or die "Unexpected error executing statement $sql: ". $sth->errstr;
3178 my $row = $sth->fetchrow_arrayref or return '';
3179 my $scalar = $row->[0];
3180 defined($scalar) ? $scalar : '';
3183 =item count [ WHERE [, PLACEHOLDER ...] ]
3185 Convenience method for the common case of "SELECT COUNT(*) FROM table",
3186 with optional WHERE. Must be called as method on a class with an
3192 my($self, $where) = (shift, shift);
3193 my $table = $self->table or die 'count called on object of class '.ref($self);
3194 my $sql = "SELECT COUNT(*) FROM $table";
3195 $sql .= " WHERE $where" if $where;
3196 $self->scalar_sql($sql, @_);
3199 =item row_exists [ WHERE [, PLACEHOLDER ...] ]
3201 Convenience method for the common case of "SELECT 1 FROM table ... LIMIT 1"
3202 with optional (but almost always needed) WHERE.
3207 my($self, $where) = (shift, shift);
3208 my $table = $self->table or die 'row_exists called on object of class '.ref($self);
3209 my $sql = "SELECT 1 FROM $table";
3210 $sql .= " WHERE $where" if $where;
3212 $self->scalar_sql($sql, @_);
3221 =item real_fields [ TABLE ]
3223 Returns a list of the real columns in the specified table. Called only by
3224 fields() and other subroutines elsewhere in FS::Record.
3231 my($table_obj) = dbdef->table($table);
3232 confess "Unknown table $table" unless $table_obj;
3233 $table_obj->columns;
3236 =item pvf FIELD_NAME
3238 Returns the FS::part_virtual_field object corresponding to a field in the
3239 record (specified by FIELD_NAME).
3244 my ($self, $name) = (shift, shift);
3246 if(grep /^$name$/, $self->virtual_fields) {
3248 my $concat = [ "'cf_'", "name" ];
3249 return qsearchs({ table => 'part_virtual_field',
3250 hashref => { dbtable => $self->table,
3253 select => 'vfieldpart, dbtable, length, label, '.concat_sql($concat).' as name',
3259 =item _quote VALUE, TABLE, COLUMN
3261 This is an internal function used to construct SQL statements. It returns
3262 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
3263 type (see L<DBIx::DBSchema::Column>) does not end in `char' or `binary'.
3268 my($value, $table, $column) = @_;
3269 my $column_obj = dbdef->table($table)->column($column);
3270 my $column_type = $column_obj->type;
3271 my $nullable = $column_obj->null;
3273 utf8::upgrade($value);
3275 warn " $table.$column: $value ($column_type".
3276 ( $nullable ? ' NULL' : ' NOT NULL' ).
3277 ")\n" if $DEBUG > 2;
3279 if ( $value eq '' && $nullable ) {
3281 } elsif ( $value eq '' && $column_type =~ /^(int|numeric)/ ) {
3282 cluck "WARNING: Attempting to set non-null integer $table.$column null; ".
3285 } elsif ( $value =~ /^\d+(\.\d+)?$/ &&
3286 ! $column_type =~ /(char|binary|text)$/i ) {
3288 } elsif (( $column_type =~ /^bytea$/i || $column_type =~ /(blob|varbinary)/i )
3289 && driver_name eq 'Pg'
3293 # dbh->quote($value, { pg_type => PG_BYTEA() }); # doesn't work right
3294 # Pg binary string quoting: convert each character to 3-digit octal prefixed with \\,
3295 # single-quote the whole mess, and put an "E" in front.
3296 return ("E'" . join('', map { sprintf('\\\\%03o', ord($_)) } split(//, $value) ) . "'");
3304 This is deprecated. Don't use it.
3306 It returns a hash-type list with the fields of this record's table set true.
3311 carp "warning: hfields is deprecated";
3314 foreach (fields($table)) {
3323 "$_: ". $self->getfield($_). "|"
3324 } (fields($self->table)) );
3327 sub DESTROY { return; }
3331 # #use Carp qw(cluck);
3332 # #cluck "DESTROYING $self";
3333 # warn "DESTROYING $self";
3337 # return ! eval { join('',@_), kill 0; 1; };
3340 =item str2time_sql [ DRIVER_NAME ]
3342 Returns a function to convert to unix time based on database type, such as
3343 "EXTRACT( EPOCH FROM" for Pg or "UNIX_TIMESTAMP(" for mysql. See
3344 the str2time_sql_closing method to return a closing string rather than just
3345 using a closing parenthesis as previously suggested.
3347 You can pass an optional driver name such as "Pg", "mysql" or
3348 $dbh->{Driver}->{Name} to return a function for that database instead of
3349 the current database.
3354 my $driver = shift || driver_name;
3356 return 'UNIX_TIMESTAMP(' if $driver =~ /^mysql/i;
3357 return 'EXTRACT( EPOCH FROM ' if $driver =~ /^Pg/i;
3359 warn "warning: unknown database type $driver; guessing how to convert ".
3360 "dates to UNIX timestamps";
3361 return 'EXTRACT(EPOCH FROM ';
3365 =item str2time_sql_closing [ DRIVER_NAME ]
3367 Returns the closing suffix of a function to convert to unix time based on
3368 database type, such as ")::integer" for Pg or ")" for mysql.
3370 You can pass an optional driver name such as "Pg", "mysql" or
3371 $dbh->{Driver}->{Name} to return a function for that database instead of
3372 the current database.
3376 sub str2time_sql_closing {
3377 my $driver = shift || driver_name;
3379 return ' )::INTEGER ' if $driver =~ /^Pg/i;
3383 =item regexp_sql [ DRIVER_NAME ]
3385 Returns the operator to do a regular expression comparison based on database
3386 type, such as '~' for Pg or 'REGEXP' for mysql.
3388 You can pass an optional driver name such as "Pg", "mysql" or
3389 $dbh->{Driver}->{Name} to return a function for that database instead of
3390 the current database.
3395 my $driver = shift || driver_name;
3397 return '~' if $driver =~ /^Pg/i;
3398 return 'REGEXP' if $driver =~ /^mysql/i;
3400 die "don't know how to use regular expressions in ". driver_name." databases";
3404 =item not_regexp_sql [ DRIVER_NAME ]
3406 Returns the operator to do a regular expression negation based on database
3407 type, such as '!~' for Pg or 'NOT REGEXP' for mysql.
3409 You can pass an optional driver name such as "Pg", "mysql" or
3410 $dbh->{Driver}->{Name} to return a function for that database instead of
3411 the current database.
3415 sub not_regexp_sql {
3416 my $driver = shift || driver_name;
3418 return '!~' if $driver =~ /^Pg/i;
3419 return 'NOT REGEXP' if $driver =~ /^mysql/i;
3421 die "don't know how to use regular expressions in ". driver_name." databases";
3425 =item concat_sql [ DRIVER_NAME ] ITEMS_ARRAYREF
3427 Returns the items concatenated based on database type, using "CONCAT()" for
3428 mysql and " || " for Pg and other databases.
3430 You can pass an optional driver name such as "Pg", "mysql" or
3431 $dbh->{Driver}->{Name} to return a function for that database instead of
3432 the current database.
3437 my $driver = ref($_[0]) ? driver_name : shift;
3440 if ( $driver =~ /^mysql/i ) {
3441 'CONCAT('. join(',', @$items). ')';
3443 join('||', @$items);
3448 =item group_concat_sql COLUMN, DELIMITER
3450 Returns an SQL expression to concatenate an aggregate column, using
3451 GROUP_CONCAT() for mysql and array_to_string() and array_agg() for Pg.
3455 sub group_concat_sql {
3456 my ($col, $delim) = @_;
3457 $delim = dbh->quote($delim);
3458 if ( driver_name() =~ /^mysql/i ) {
3459 # DISTINCT(foo) is valid as $col
3460 return "GROUP_CONCAT($col SEPARATOR $delim)";
3462 return "array_to_string(array_agg($col), $delim)";
3466 =item midnight_sql DATE
3468 Returns an SQL expression to convert DATE (a unix timestamp) to midnight
3469 on that day in the system timezone, using the default driver name.
3474 my $driver = driver_name;
3476 if ( $driver =~ /^mysql/i ) {
3477 "UNIX_TIMESTAMP(DATE(FROM_UNIXTIME($expr)))";
3480 "EXTRACT( EPOCH FROM DATE(TO_TIMESTAMP($expr)) )";
3488 This module should probably be renamed, since much of the functionality is
3489 of general use. It is not completely unlike Adapter::DBI (see below).
3491 Exported qsearch and qsearchs should be deprecated in favor of method calls
3492 (against an FS::Record object like the old search and searchs that qsearch
3493 and qsearchs were on top of.)
3495 The whole fields / hfields mess should be removed.
3497 The various WHERE clauses should be subroutined.
3499 table string should be deprecated in favor of DBIx::DBSchema::Table.
3501 No doubt we could benefit from a Tied hash. Documenting how exists / defined
3502 true maps to the database (and WHERE clauses) would also help.
3504 The ut_ methods should ask the dbdef for a default length.
3506 ut_sqltype (like ut_varchar) should all be defined
3508 A fallback check method should be provided which uses the dbdef.
3510 The ut_money method assumes money has two decimal digits.
3512 The Pg money kludge in the new method only strips `$'.
3514 The ut_phonen method only checks US-style phone numbers.
3516 The _quote function should probably use ut_float instead of a regex.
3518 All the subroutines probably should be methods, here or elsewhere.
3520 Probably should borrow/use some dbdef methods where appropriate (like sub
3523 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
3524 or allow it to be set. Working around it is ugly any way around - DBI should
3525 be fixed. (only affects RDBMS which return uppercase column names)
3527 ut_zip should take an optional country like ut_phone.
3531 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
3533 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.