4 use vars qw( $AUTOLOAD @ISA @EXPORT_OK $DEBUG
5 $conf $conf_encryption $me
7 $nowarn_identical $nowarn_classload
8 $no_update_diff $no_check_foreign
11 use Carp qw(carp cluck croak confess);
12 use Scalar::Util qw( blessed );
13 use File::CounterFile;
16 use File::Slurp qw( slurp );
17 use DBI qw(:sql_types);
18 use DBIx::DBSchema 0.38;
19 use FS::UID qw(dbh getotaker datasrc driver_name);
21 use FS::Schema qw(dbdef);
23 use FS::Msgcat qw(gettext);
24 #use FS::Conf; #dependency loop bs, in install_callback below instead
26 use FS::part_virtual_field;
32 #export dbdef for now... everything else expects to find it here
34 dbh fields hfields qsearch qsearchs dbdef jsearch
35 str2time_sql str2time_sql_closing regexp_sql not_regexp_sql concat_sql
41 $nowarn_identical = 0;
42 $nowarn_classload = 0;
44 $no_check_foreign = 0;
52 $conf_encryption = '';
53 FS::UID->install_callback( sub {
56 $conf = FS::Conf->new;
57 $conf_encryption = $conf->exists('encryption');
58 $File::CounterFile::DEFAULT_DIR = $conf->base_dir . "/counters.". datasrc;
59 if ( driver_name eq 'Pg' ) {
60 eval "use DBD::Pg ':pg_types'";
63 eval "sub PG_BYTEA { die 'guru meditation #9: calling PG_BYTEA when not running Pg?'; }";
69 FS::Record - Database record objects
74 use FS::Record qw(dbh fields qsearch qsearchs);
76 $record = new FS::Record 'table', \%hash;
77 $record = new FS::Record 'table', { 'column' => 'value', ... };
79 $record = qsearchs FS::Record 'table', \%hash;
80 $record = qsearchs FS::Record 'table', { 'column' => 'value', ... };
81 @records = qsearch FS::Record 'table', \%hash;
82 @records = qsearch FS::Record 'table', { 'column' => 'value', ... };
84 $table = $record->table;
85 $dbdef_table = $record->dbdef_table;
87 $value = $record->get('column');
88 $value = $record->getfield('column');
89 $value = $record->column;
91 $record->set( 'column' => 'value' );
92 $record->setfield( 'column' => 'value' );
93 $record->column('value');
95 %hash = $record->hash;
97 $hashref = $record->hashref;
99 $error = $record->insert;
101 $error = $record->delete;
103 $error = $new_record->replace($old_record);
105 # external use deprecated - handled by the database (at least for Pg, mysql)
106 $value = $record->unique('column');
108 $error = $record->ut_float('column');
109 $error = $record->ut_floatn('column');
110 $error = $record->ut_number('column');
111 $error = $record->ut_numbern('column');
112 $error = $record->ut_snumber('column');
113 $error = $record->ut_snumbern('column');
114 $error = $record->ut_money('column');
115 $error = $record->ut_text('column');
116 $error = $record->ut_textn('column');
117 $error = $record->ut_alpha('column');
118 $error = $record->ut_alphan('column');
119 $error = $record->ut_phonen('column');
120 $error = $record->ut_anything('column');
121 $error = $record->ut_name('column');
123 $quoted_value = _quote($value,'table','field');
126 $fields = hfields('table');
127 if ( $fields->{Field} ) { # etc.
129 @fields = fields 'table'; #as a subroutine
130 @fields = $record->fields; #as a method call
135 (Mostly) object-oriented interface to database records. Records are currently
136 implemented on top of DBI. FS::Record is intended as a base class for
137 table-specific classes to inherit from, i.e. FS::cust_main.
143 =item new [ TABLE, ] HASHREF
145 Creates a new record. It doesn't store it in the database, though. See
146 L<"insert"> for that.
148 Note that the object stores this hash reference, not a distinct copy of the
149 hash it points to. You can ask the object for a copy with the I<hash>
152 TABLE can only be omitted when a dervived class overrides the table method.
158 my $class = ref($proto) || $proto;
160 bless ($self, $class);
162 unless ( defined ( $self->table ) ) {
163 $self->{'Table'} = shift;
164 carp "warning: FS::Record::new called with table name ". $self->{'Table'}
165 unless $nowarn_classload;
168 $self->{'Hash'} = shift;
170 foreach my $field ( grep !defined($self->{'Hash'}{$_}), $self->fields ) {
171 $self->{'Hash'}{$field}='';
174 $self->_rebless if $self->can('_rebless');
176 $self->{'modified'} = 0;
178 $self->_cache($self->{'Hash'}, shift) if $self->can('_cache') && @_;
185 my $class = ref($proto) || $proto;
187 bless ($self, $class);
189 $self->{'Table'} = shift unless defined ( $self->table );
191 my $hashref = $self->{'Hash'} = shift;
193 if ( defined( $cache->cache->{$hashref->{$cache->key}} ) ) {
194 my $obj = $cache->cache->{$hashref->{$cache->key}};
195 $obj->_cache($hashref, $cache) if $obj->can('_cache');
198 $cache->cache->{$hashref->{$cache->key}} = $self->new($hashref, $cache);
205 my $class = ref($proto) || $proto;
207 bless ($self, $class);
208 if ( defined $self->table ) {
209 cluck "create constructor is deprecated, use new!";
212 croak "FS::Record::create called (not from a subclass)!";
216 =item qsearch PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
218 Searches the database for all records matching (at least) the key/value pairs
219 in HASHREF. Returns all the records found as `FS::TABLE' objects if that
220 module is loaded (i.e. via `use FS::cust_main;'), otherwise returns FS::Record
223 The preferred usage is to pass a hash reference of named parameters:
225 @records = qsearch( {
226 'table' => 'table_name',
227 'hashref' => { 'field' => 'value'
228 'field' => { 'op' => '<',
233 #these are optional...
235 'extra_sql' => 'AND field = ? AND intfield = ?',
236 'extra_param' => [ 'value', [ 5, 'int' ] ],
237 'order_by' => 'ORDER BY something',
238 #'cache_obj' => '', #optional
239 'addl_from' => 'LEFT JOIN othtable USING ( field )',
244 Much code still uses old-style positional parameters, this is also probably
245 fine in the common case where there are only two parameters:
247 my @records = qsearch( 'table', { 'field' => 'value' } );
249 Also possible is an experimental LISTREF of PARAMS_HASHREFs for a UNION of
250 the individual PARAMS_HASHREF queries
252 ###oops, argh, FS::Record::new only lets us create database fields.
253 #Normal behaviour if SELECT is not specified is `*', as in
254 #C<SELECT * FROM table WHERE ...>. However, there is an experimental new
255 #feature where you can specify SELECT - remember, the objects returned,
256 #although blessed into the appropriate `FS::TABLE' package, will only have the
257 #fields you specify. This might have unwanted results if you then go calling
258 #regular FS::TABLE methods
263 my %TYPE = (); #for debugging
266 my($type, $value) = @_;
268 my $bind_type = { TYPE => SQL_VARCHAR };
270 if ( $type =~ /(big)?(int|serial)/i && $value =~ /^\d+(\.\d+)?$/ ) {
272 $bind_type = { TYPE => SQL_INTEGER };
274 } elsif ( $type =~ /^bytea$/i || $type =~ /(blob|varbinary)/i ) {
276 if ( driver_name eq 'Pg' ) {
278 $bind_type = { pg_type => PG_BYTEA };
280 # $bind_type = ? #SQL_VARCHAR could be fine?
283 #DBD::Pg 1.49: Cannot bind ... unknown sql_type 6 with SQL_FLOAT
284 #fixed by DBD::Pg 2.11.8
285 #can change back to SQL_FLOAT in early-mid 2010, once everyone's upgraded
286 #(make a Tron test first)
287 } elsif ( _is_fs_float( $type, $value ) ) {
289 $bind_type = { TYPE => SQL_DECIMAL };
298 my($type, $value) = @_;
299 if ( ( $type =~ /(numeric)/i && $value =~ /^[+-]?\d+(\.\d+)?$/ ) ||
300 ( $type =~ /(real|float4)/i && $value =~ /[-+]?\d*\.?\d+([eE][-+]?\d+)?/)
308 my( @stable, @record, @cache );
309 my( @select, @extra_sql, @extra_param, @order_by, @addl_from );
311 my %union_options = ();
312 if ( ref($_[0]) eq 'ARRAY' ) {
315 foreach my $href ( @$optlist ) {
316 push @stable, ( $href->{'table'} or die "table name is required" );
317 push @record, ( $href->{'hashref'} || {} );
318 push @select, ( $href->{'select'} || '*' );
319 push @extra_sql, ( $href->{'extra_sql'} || '' );
320 push @extra_param, ( $href->{'extra_param'} || [] );
321 push @order_by, ( $href->{'order_by'} || '' );
322 push @cache, ( $href->{'cache_obj'} || '' );
323 push @addl_from, ( $href->{'addl_from'} || '' );
324 push @debug, ( $href->{'debug'} || '' );
326 die "at least one hashref is required" unless scalar(@stable);
327 } elsif ( ref($_[0]) eq 'HASH' ) {
329 $stable[0] = $opt->{'table'} or die "table name is required";
330 $record[0] = $opt->{'hashref'} || {};
331 $select[0] = $opt->{'select'} || '*';
332 $extra_sql[0] = $opt->{'extra_sql'} || '';
333 $extra_param[0] = $opt->{'extra_param'} || [];
334 $order_by[0] = $opt->{'order_by'} || '';
335 $cache[0] = $opt->{'cache_obj'} || '';
336 $addl_from[0] = $opt->{'addl_from'} || '';
337 $debug[0] = $opt->{'debug'} || '';
348 my $cache = $cache[0];
354 foreach my $stable ( @stable ) {
355 #stop altering the caller's hashref
356 my $record = { %{ shift(@record) || {} } };#and be liberal in receipt
357 my $select = shift @select;
358 my $extra_sql = shift @extra_sql;
359 my $extra_param = shift @extra_param;
360 my $order_by = shift @order_by;
361 my $cache = shift @cache;
362 my $addl_from = shift @addl_from;
363 my $debug = shift @debug;
365 #$stable =~ /^([\w\_]+)$/ or die "Illegal table: $table";
367 $stable =~ /^([\w\s\(\)\.\,\=]+)$/ or die "Illegal table: $stable";
370 my $table = $cache ? $cache->table : $stable;
371 my $dbdef_table = dbdef->table($table)
372 or die "No schema for table $table found - ".
373 "do you need to run freeside-upgrade?";
374 my $pkey = $dbdef_table->primary_key;
376 my @real_fields = grep exists($record->{$_}), real_fields($table);
378 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
379 @virtual_fields = grep exists($record->{$_}), "FS::$table"->virtual_fields;
381 cluck "warning: FS::$table not loaded; virtual fields not searchable"
382 unless $nowarn_classload;
383 @virtual_fields = ();
386 my $statement .= "SELECT $select FROM $stable";
387 $statement .= " $addl_from" if $addl_from;
388 if ( @real_fields or @virtual_fields ) {
389 $statement .= ' WHERE '. join(' AND ',
390 get_real_fields($table, $record, \@real_fields) ,
391 get_virtual_fields($table, $pkey, $record, \@virtual_fields),
395 $statement .= " $extra_sql" if defined($extra_sql);
396 $statement .= " $order_by" if defined($order_by);
398 push @statement, $statement;
400 warn "[debug]$me $statement\n" if $DEBUG > 1 || $debug;
404 grep defined( $record->{$_} ) && $record->{$_} ne '', @real_fields
407 my $value = $record->{$field};
408 my $op = (ref($value) && $value->{op}) ? $value->{op} : '=';
409 $value = $value->{'value'} if ref($value);
410 my $type = dbdef->table($table)->column($field)->type;
412 my $bind_type = _bind_type($type, $value);
416 # %TYPE = map { &{"DBI::$_"}() => $_ } @{ $DBI::EXPORT_TAGS{sql_types} }
418 # warn " bind_param $bind (for field $field), $value, TYPE $TYPE{$TYPE}\n";
422 push @bind_type, $bind_type;
426 foreach my $param ( @$extra_param ) {
427 my $bind_type = { TYPE => SQL_VARCHAR };
430 $value = $param->[0];
431 my $type = $param->[1];
432 $bind_type = _bind_type($type, $value);
435 push @bind_type, $bind_type;
439 my $statement = join( ' ) UNION ( ', @statement );
440 $statement = "( $statement )" if scalar(@statement) > 1;
441 $statement .= " $union_options{order_by}" if $union_options{order_by};
443 my $sth = $dbh->prepare($statement)
444 or croak "$dbh->errstr doing $statement";
447 foreach my $value ( @value ) {
448 my $bind_type = shift @bind_type;
449 $sth->bind_param($bind++, $value, $bind_type );
452 # $sth->execute( map $record->{$_},
453 # grep defined( $record->{$_} ) && $record->{$_} ne '', @fields
454 # ) or croak "Error executing \"$statement\": ". $sth->errstr;
456 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
458 # virtual fields and blessings are nonsense in a heterogeneous UNION, right?
459 my $table = $stable[0];
461 $table = '' if grep { $_ ne $table } @stable;
462 $pkey = dbdef->table($table)->primary_key if $table;
464 my @virtual_fields = ();
465 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
466 @virtual_fields = "FS::$table"->virtual_fields;
468 cluck "warning: FS::$table not loaded; virtual fields not returned either"
469 unless $nowarn_classload;
470 @virtual_fields = ();
474 tie %result, "Tie::IxHash";
475 my @stuff = @{ $sth->fetchall_arrayref( {} ) };
476 if ( $pkey && scalar(@stuff) && $stuff[0]->{$pkey} ) {
477 %result = map { $_->{$pkey}, $_ } @stuff;
479 @result{@stuff} = @stuff;
484 if ( keys(%result) and @virtual_fields ) {
486 "SELECT virtual_field.recnum, part_virtual_field.name, ".
487 "virtual_field.value ".
488 "FROM part_virtual_field JOIN virtual_field USING (vfieldpart) ".
489 "WHERE part_virtual_field.dbtable = '$table' AND ".
490 "virtual_field.recnum IN (".
491 join(',', keys(%result)). ") AND part_virtual_field.name IN ('".
492 join(q!', '!, @virtual_fields) . "')";
493 warn "[debug]$me $statement\n" if $DEBUG > 1;
494 $sth = $dbh->prepare($statement) or croak "$dbh->errstr doing $statement";
495 $sth->execute or croak "Error executing \"$statement\": ". $sth->errstr;
497 foreach (@{ $sth->fetchall_arrayref({}) }) {
498 my $recnum = $_->{recnum};
499 my $name = $_->{name};
500 my $value = $_->{value};
501 if (exists($result{$recnum})) {
502 $result{$recnum}->{$name} = $value;
507 if ( eval 'scalar(@FS::'. $table. '::ISA);' ) {
508 if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) {
509 #derivied class didn't override new method, so this optimization is safe
512 new_or_cached( "FS::$table", { %{$_} }, $cache )
516 new( "FS::$table", { %{$_} } )
520 #okay, its been tested
521 # warn "untested code (class FS::$table uses custom new method)";
523 eval 'FS::'. $table. '->new( { %{$_} } )';
527 # Check for encrypted fields and decrypt them.
528 ## only in the local copy, not the cached object
529 if ( $conf_encryption
530 && eval 'defined(@FS::'. $table . '::encrypted_fields)' ) {
531 foreach my $record (@return) {
532 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
533 # Set it directly... This may cause a problem in the future...
534 $record->setfield($field, $record->decrypt($record->getfield($field)));
539 cluck "warning: FS::$table not loaded; returning FS::Record objects"
540 unless $nowarn_classload;
542 FS::Record->new( $table, { %{$_} } );
548 ## makes this easier to read
550 sub get_virtual_fields {
554 my $virtual_fields = shift;
560 if ( ref($record->{$_}) ) {
561 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
562 if ( uc($op) eq 'ILIKE' ) {
564 $record->{$_}{'value'} = lc($record->{$_}{'value'});
565 $column = "LOWER($_)";
567 $record->{$_} = $record->{$_}{'value'};
570 # ... EXISTS ( SELECT name, value FROM part_virtual_field
572 # ON part_virtual_field.vfieldpart = virtual_field.vfieldpart
573 # WHERE recnum = svc_acct.svcnum
574 # AND (name, value) = ('egad', 'brain') )
576 my $value = $record->{$_};
580 $subq = ($value ? 'EXISTS ' : 'NOT EXISTS ') .
581 "( SELECT part_virtual_field.name, virtual_field.value ".
582 "FROM part_virtual_field JOIN virtual_field ".
583 "ON part_virtual_field.vfieldpart = virtual_field.vfieldpart ".
584 "WHERE virtual_field.recnum = ${table}.${pkey} ".
585 "AND part_virtual_field.name = '${column}'".
587 " AND virtual_field.value ${op} '${value}'"
591 } @{ $virtual_fields } ) ;
594 sub get_real_fields {
597 my $real_fields = shift;
599 ## this huge map was previously inline, just broke it out to help read the qsearch method, should be optimized for readability
605 my $type = dbdef->table($table)->column($column)->type;
606 my $value = $record->{$column};
607 $value = $value->{'value'} if ref($value);
608 if ( ref($record->{$_}) ) {
609 $op = $record->{$_}{'op'} if $record->{$_}{'op'};
610 #$op = 'LIKE' if $op =~ /^ILIKE$/i && driver_name ne 'Pg';
611 if ( uc($op) eq 'ILIKE' ) {
613 $record->{$_}{'value'} = lc($record->{$_}{'value'});
614 $column = "LOWER($_)";
616 $record->{$_} = $record->{$_}{'value'}
619 if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) {
621 if ( driver_name eq 'Pg' ) {
622 if ( $type =~ /(int|numeric|real|float4|(big)?serial)/i ) {
623 qq-( $column IS NULL )-;
625 qq-( $column IS NULL OR $column = '' )-;
628 qq-( $column IS NULL OR $column = "" )-;
630 } elsif ( $op eq '!=' ) {
631 if ( driver_name eq 'Pg' ) {
632 if ( $type =~ /(int|numeric|real|float4|(big)?serial)/i ) {
633 qq-( $column IS NOT NULL )-;
635 qq-( $column IS NOT NULL AND $column != '' )-;
638 qq-( $column IS NOT NULL AND $column != "" )-;
641 if ( driver_name eq 'Pg' ) {
642 qq-( $column $op '' )-;
644 qq-( $column $op "" )-;
647 #if this needs to be re-enabled, it needs to use a custom op like
648 #"APPROX=" or something (better name?, not '=', to avoid affecting other
650 #} elsif ( $op eq 'APPROX=' && _is_fs_float( $type, $value ) ) {
651 # ( "$column <= ?", "$column >= ?" );
655 } @{ $real_fields } );
658 =item by_key PRIMARY_KEY_VALUE
660 This is a class method that returns the record with the given primary key
661 value. This method is only useful in FS::Record subclasses. For example:
663 my $cust_main = FS::cust_main->by_key(1); # retrieve customer with custnum 1
667 my $cust_main = qsearchs('cust_main', { 'custnum' => 1 } );
672 my ($class, $pkey_value) = @_;
674 my $table = $class->table
675 or croak "No table for $class found";
677 my $dbdef_table = dbdef->table($table)
678 or die "No schema for table $table found - ".
679 "do you need to create it or run dbdef-create?";
680 my $pkey = $dbdef_table->primary_key
681 or die "No primary key for table $table";
683 return qsearchs($table, { $pkey => $pkey_value });
686 =item jsearch TABLE, HASHREF, SELECT, EXTRA_SQL, PRIMARY_TABLE, PRIMARY_KEY
688 Experimental JOINed search method. Using this method, you can execute a
689 single SELECT spanning multiple tables, and cache the results for subsequent
690 method calls. Interface will almost definately change in an incompatible
698 my($table, $record, $select, $extra_sql, $ptable, $pkey ) = @_;
699 my $cache = FS::SearchCache->new( $ptable, $pkey );
702 grep { !$saw{$_->getfield($pkey)}++ }
703 qsearch($table, $record, $select, $extra_sql, $cache )
707 =item qsearchs PARAMS_HASHREF | TABLE, HASHREF, SELECT, EXTRA_SQL, CACHE_OBJ, ADDL_FROM
709 Same as qsearch, except that if more than one record matches, it B<carp>s but
710 returns the first. If this happens, you either made a logic error in asking
711 for a single item, or your data is corrupted.
715 sub qsearchs { # $result_record = &FS::Record:qsearchs('table',\%hash);
717 my(@result) = qsearch(@_);
718 cluck "warning: Multiple records in scalar search ($table)"
719 if scalar(@result) > 1;
720 #should warn more vehemently if the search was on a primary key?
721 scalar(@result) ? ($result[0]) : ();
732 Returns the table name.
737 # cluck "warning: FS::Record::table deprecated; supply one in subclass!";
744 Returns the DBIx::DBSchema::Table object for the table.
750 my($table)=$self->table;
751 dbdef->table($table);
756 Returns the primary key for the table.
762 my $pkey = $self->dbdef_table->primary_key;
765 =item get, getfield COLUMN
767 Returns the value of the column/field/key COLUMN.
772 my($self,$field) = @_;
773 # to avoid "Use of unitialized value" errors
774 if ( defined ( $self->{Hash}->{$field} ) ) {
775 $self->{Hash}->{$field};
785 =item set, setfield COLUMN, VALUE
787 Sets the value of the column/field/key COLUMN to VALUE. Returns VALUE.
792 my($self,$field,$value) = @_;
793 $self->{'modified'} = 1;
794 $self->{'Hash'}->{$field} = $value;
803 Returns true if the column/field/key COLUMN exists.
808 my($self,$field) = @_;
809 exists($self->{Hash}->{$field});
812 =item AUTLOADED METHODS
814 $record->column is a synonym for $record->get('column');
816 $record->column('value') is a synonym for $record->set('column','value');
823 my($field)=$AUTOLOAD;
825 if ( defined($value) ) {
826 confess "errant AUTOLOAD $field for $self (arg $value)"
827 unless blessed($self) && $self->can('setfield');
828 $self->setfield($field,$value);
830 confess "errant AUTOLOAD $field for $self (no args)"
831 unless blessed($self) && $self->can('getfield');
832 $self->getfield($field);
838 # my $field = $AUTOLOAD;
840 # if ( defined($_[1]) ) {
841 # $_[0]->setfield($field, $_[1]);
843 # $_[0]->getfield($field);
849 Returns a list of the column/value pairs, usually for assigning to a new hash.
851 To make a distinct duplicate of an FS::Record object, you can do:
853 $new = new FS::Record ( $old->table, { $old->hash } );
859 confess $self. ' -> hash: Hash attribute is undefined'
860 unless defined($self->{'Hash'});
861 %{ $self->{'Hash'} };
866 Returns a reference to the column/value hash. This may be deprecated in the
867 future; if there's a reason you can't just use the autoloaded or get/set
879 Returns true if any of this object's values have been modified with set (or via
880 an autoloaded method). Doesn't yet recognize when you retreive a hashref and
890 =item select_for_update
892 Selects this record with the SQL "FOR UPDATE" command. This can be useful as
897 sub select_for_update {
899 my $primary_key = $self->primary_key;
902 'table' => $self->table,
903 'hashref' => { $primary_key => $self->$primary_key() },
904 'extra_sql' => 'FOR UPDATE',
910 Locks this table with a database-driver specific lock method. This is used
911 as a mutex in order to do a duplicate search.
913 For PostgreSQL, does "LOCK TABLE tablename IN SHARE ROW EXCLUSIVE MODE".
915 For MySQL, does a SELECT FOR UPDATE on the duplicate_lock table.
917 Errors are fatal; no useful return value.
919 Note: To use this method for new tables other than svc_acct and svc_phone,
920 edit freeside-upgrade and add those tables to the duplicate_lock list.
926 my $table = $self->table;
928 warn "$me locking $table table\n" if $DEBUG;
930 if ( driver_name =~ /^Pg/i ) {
932 dbh->do("LOCK TABLE $table IN SHARE ROW EXCLUSIVE MODE")
935 } elsif ( driver_name =~ /^mysql/i ) {
937 dbh->do("SELECT * FROM duplicate_lock
938 WHERE lockname = '$table'
940 ) or die dbh->errstr;
944 die "unknown database ". driver_name. "; don't know how to lock table";
948 warn "$me acquired $table table lock\n" if $DEBUG;
954 Inserts this record to the database. If there is an error, returns the error,
955 otherwise returns false.
963 warn "$self -> insert" if $DEBUG;
965 my $error = $self->check;
966 return $error if $error;
968 #single-field unique keys are given a value if false
969 #(like MySQL's AUTO_INCREMENT or Pg SERIAL)
970 foreach ( $self->dbdef_table->unique_singles) {
971 $self->unique($_) unless $self->getfield($_);
974 #and also the primary key, if the database isn't going to
975 my $primary_key = $self->dbdef_table->primary_key;
977 if ( $primary_key ) {
978 my $col = $self->dbdef_table->column($primary_key);
981 uc($col->type) =~ /^(BIG)?SERIAL\d?/
982 || ( driver_name eq 'Pg'
983 && defined($col->default)
984 && $col->quoted_default =~ /^nextval\(/i
986 || ( driver_name eq 'mysql'
987 && defined($col->local)
988 && $col->local =~ /AUTO_INCREMENT/i
990 $self->unique($primary_key) unless $self->getfield($primary_key) || $db_seq;
993 my $table = $self->table;
995 # Encrypt before the database
996 if ( defined(eval '@FS::'. $table . '::encrypted_fields')
997 && scalar( eval '@FS::'. $table . '::encrypted_fields')
998 && $conf->exists('encryption')
1000 foreach my $field (eval '@FS::'. $table . '::encrypted_fields') {
1001 $self->{'saved'} = $self->getfield($field);
1002 $self->setfield($field, $self->encrypt($self->getfield($field)));
1006 #false laziness w/delete
1008 grep { defined($self->getfield($_)) && $self->getfield($_) ne "" }
1011 my @values = map { _quote( $self->getfield($_), $table, $_) } @real_fields;
1014 my $statement = "INSERT INTO $table ";
1015 if ( @real_fields ) {
1018 join( ', ', @real_fields ).
1020 join( ', ', @values ).
1024 $statement .= 'DEFAULT VALUES';
1026 warn "[debug]$me $statement\n" if $DEBUG > 1;
1027 my $sth = dbh->prepare($statement) or return dbh->errstr;
1029 local $SIG{HUP} = 'IGNORE';
1030 local $SIG{INT} = 'IGNORE';
1031 local $SIG{QUIT} = 'IGNORE';
1032 local $SIG{TERM} = 'IGNORE';
1033 local $SIG{TSTP} = 'IGNORE';
1034 local $SIG{PIPE} = 'IGNORE';
1036 $sth->execute or return $sth->errstr;
1038 # get inserted id from the database, if applicable & needed
1039 if ( $db_seq && ! $self->getfield($primary_key) ) {
1040 warn "[debug]$me retreiving sequence from database\n" if $DEBUG;
1044 if ( driver_name eq 'Pg' ) {
1046 #my $oid = $sth->{'pg_oid_status'};
1047 #my $i_sql = "SELECT $primary_key FROM $table WHERE oid = ?";
1049 my $default = $self->dbdef_table->column($primary_key)->quoted_default;
1050 unless ( $default =~ /^nextval\(\(?'"?([\w\.]+)"?'/i ) {
1051 dbh->rollback if $FS::UID::AutoCommit;
1052 return "can't parse $table.$primary_key default value".
1053 " for sequence name: $default";
1057 my $i_sql = "SELECT currval('$sequence')";
1058 my $i_sth = dbh->prepare($i_sql) or do {
1059 dbh->rollback if $FS::UID::AutoCommit;
1062 $i_sth->execute() or do { #$i_sth->execute($oid)
1063 dbh->rollback if $FS::UID::AutoCommit;
1064 return $i_sth->errstr;
1066 $insertid = $i_sth->fetchrow_arrayref->[0];
1068 } elsif ( driver_name eq 'mysql' ) {
1070 $insertid = dbh->{'mysql_insertid'};
1071 # work around mysql_insertid being null some of the time, ala RT :/
1072 unless ( $insertid ) {
1073 warn "WARNING: DBD::mysql didn't return mysql_insertid; ".
1074 "using SELECT LAST_INSERT_ID();";
1075 my $i_sql = "SELECT LAST_INSERT_ID()";
1076 my $i_sth = dbh->prepare($i_sql) or do {
1077 dbh->rollback if $FS::UID::AutoCommit;
1080 $i_sth->execute or do {
1081 dbh->rollback if $FS::UID::AutoCommit;
1082 return $i_sth->errstr;
1084 $insertid = $i_sth->fetchrow_arrayref->[0];
1089 dbh->rollback if $FS::UID::AutoCommit;
1090 return "don't know how to retreive inserted ids from ". driver_name.
1091 ", try using counterfiles (maybe run dbdef-create?)";
1095 $self->setfield($primary_key, $insertid);
1099 my @virtual_fields =
1100 grep defined($self->getfield($_)) && $self->getfield($_) ne "",
1101 $self->virtual_fields;
1102 if (@virtual_fields) {
1103 my %v_values = map { $_, $self->getfield($_) } @virtual_fields;
1105 my $vfieldpart = $self->vfieldpart_hashref;
1107 my $v_statement = "INSERT INTO virtual_field(recnum, vfieldpart, value) ".
1110 my $v_sth = dbh->prepare($v_statement) or do {
1111 dbh->rollback if $FS::UID::AutoCommit;
1115 foreach (keys(%v_values)) {
1116 $v_sth->execute($self->getfield($primary_key),
1120 dbh->rollback if $FS::UID::AutoCommit;
1121 return $v_sth->errstr;
1128 if ( defined dbdef->table('h_'. $table) ) {
1129 my $h_statement = $self->_h_statement('insert');
1130 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
1131 $h_sth = dbh->prepare($h_statement) or do {
1132 dbh->rollback if $FS::UID::AutoCommit;
1138 $h_sth->execute or return $h_sth->errstr if $h_sth;
1140 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1142 # Now that it has been saved, reset the encrypted fields so that $new
1143 # can still be used.
1144 foreach my $field (keys %{$saved}) {
1145 $self->setfield($field, $saved->{$field});
1153 Depriciated (use insert instead).
1158 cluck "warning: FS::Record::add deprecated!";
1159 insert @_; #call method in this scope
1164 Delete this record from the database. If there is an error, returns the error,
1165 otherwise returns false.
1172 my $statement = "DELETE FROM ". $self->table. " WHERE ". join(' AND ',
1174 $self->getfield($_) eq ''
1175 #? "( $_ IS NULL OR $_ = \"\" )"
1176 ? ( driver_name eq 'Pg'
1178 : "( $_ IS NULL OR $_ = \"\" )"
1180 : "$_ = ". _quote($self->getfield($_),$self->table,$_)
1181 } ( $self->dbdef_table->primary_key )
1182 ? ( $self->dbdef_table->primary_key)
1183 : real_fields($self->table)
1185 warn "[debug]$me $statement\n" if $DEBUG > 1;
1186 my $sth = dbh->prepare($statement) or return dbh->errstr;
1189 if ( defined dbdef->table('h_'. $self->table) ) {
1190 my $h_statement = $self->_h_statement('delete');
1191 warn "[debug]$me $h_statement\n" if $DEBUG > 2;
1192 $h_sth = dbh->prepare($h_statement) or return dbh->errstr;
1197 my $primary_key = $self->dbdef_table->primary_key;
1200 my $vfp = $self->vfieldpart_hashref;
1201 foreach($self->virtual_fields) {
1202 next if $self->getfield($_) eq '';
1203 unless(@del_vfields) {
1204 my $st = "DELETE FROM virtual_field WHERE recnum = ? AND vfieldpart = ?";
1205 $v_sth = dbh->prepare($st) or return dbh->errstr;
1207 push @del_vfields, $_;
1210 local $SIG{HUP} = 'IGNORE';
1211 local $SIG{INT} = 'IGNORE';
1212 local $SIG{QUIT} = 'IGNORE';
1213 local $SIG{TERM} = 'IGNORE';
1214 local $SIG{TSTP} = 'IGNORE';
1215 local $SIG{PIPE} = 'IGNORE';
1217 my $rc = $sth->execute or return $sth->errstr;
1218 #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0";
1219 $h_sth->execute or return $h_sth->errstr if $h_sth;
1220 $v_sth->execute($self->getfield($primary_key), $vfp->{$_})
1221 or return $v_sth->errstr
1222 foreach (@del_vfields);
1224 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1226 #no need to needlessly destoy the data either (causes problems actually)
1227 #undef $self; #no need to keep object!
1234 Depriciated (use delete instead).
1239 cluck "warning: FS::Record::del deprecated!";
1240 &delete(@_); #call method in this scope
1243 =item replace OLD_RECORD
1245 Replace the OLD_RECORD with this one in the database. If there is an error,
1246 returns the error, otherwise returns false.
1251 my ($new, $old) = (shift, shift);
1253 $old = $new->replace_old unless defined($old);
1255 warn "[debug]$me $new ->replace $old\n" if $DEBUG;
1257 if ( $new->can('replace_check') ) {
1258 my $error = $new->replace_check($old);
1259 return $error if $error;
1262 return "Records not in same table!" unless $new->table eq $old->table;
1264 my $primary_key = $old->dbdef_table->primary_key;
1265 return "Can't change primary key $primary_key ".
1266 'from '. $old->getfield($primary_key).
1267 ' to ' . $new->getfield($primary_key)
1269 && ( $old->getfield($primary_key) ne $new->getfield($primary_key) );
1271 my $error = $new->check;
1272 return $error if $error;
1274 # Encrypt for replace
1276 if ( $conf->exists('encryption')
1277 && defined(eval '@FS::'. $new->table . '::encrypted_fields')
1278 && scalar( eval '@FS::'. $new->table . '::encrypted_fields')
1280 foreach my $field (eval '@FS::'. $new->table . '::encrypted_fields') {
1281 $saved->{$field} = $new->getfield($field);
1282 $new->setfield($field, $new->encrypt($new->getfield($field)));
1286 #my @diff = grep $new->getfield($_) ne $old->getfield($_), $old->fields;
1287 my %diff = map { ($new->getfield($_) ne $old->getfield($_))
1288 ? ($_, $new->getfield($_)) : () } $old->fields;
1290 unless (keys(%diff) || $no_update_diff ) {
1291 carp "[warning]$me $new -> replace $old: records identical"
1292 unless $nowarn_identical;
1296 my $statement = "UPDATE ". $old->table. " SET ". join(', ',
1298 "$_ = ". _quote($new->getfield($_),$old->table,$_)
1299 } real_fields($old->table)
1304 if ( $old->getfield($_) eq '' ) {
1306 #false laziness w/qsearch
1307 if ( driver_name eq 'Pg' ) {
1308 my $type = $old->dbdef_table->column($_)->type;
1309 if ( $type =~ /(int|(big)?serial)/i ) {
1312 qq-( $_ IS NULL OR $_ = '' )-;
1315 qq-( $_ IS NULL OR $_ = "" )-;
1319 "$_ = ". _quote($old->getfield($_),$old->table,$_);
1322 } ( $primary_key ? ( $primary_key ) : real_fields($old->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_'. $old->table) ) {
1330 my $h_old_statement = $old->_h_statement('replace_old');
1331 warn "[debug]$me $h_old_statement\n" if $DEBUG > 2;
1332 $h_old_sth = dbh->prepare($h_old_statement) or return dbh->errstr;
1338 if ( defined dbdef->table('h_'. $new->table) ) {
1339 my $h_new_statement = $new->_h_statement('replace_new');
1340 warn "[debug]$me $h_new_statement\n" if $DEBUG > 2;
1341 $h_new_sth = dbh->prepare($h_new_statement) or return dbh->errstr;
1346 # For virtual fields we have three cases with different SQL
1347 # statements: add, replace, delete
1351 my (@add_vfields, @rep_vfields, @del_vfields);
1352 my $vfp = $old->vfieldpart_hashref;
1353 foreach(grep { exists($diff{$_}) } $new->virtual_fields) {
1354 if($diff{$_} eq '') {
1356 unless(@del_vfields) {
1357 my $st = "DELETE FROM virtual_field WHERE recnum = ? ".
1358 "AND vfieldpart = ?";
1359 warn "[debug]$me $st\n" if $DEBUG > 2;
1360 $v_del_sth = dbh->prepare($st) or return dbh->errstr;
1362 push @del_vfields, $_;
1363 } elsif($old->getfield($_) eq '') {
1365 unless(@add_vfields) {
1366 my $st = "INSERT INTO virtual_field (value, recnum, vfieldpart) ".
1368 warn "[debug]$me $st\n" if $DEBUG > 2;
1369 $v_add_sth = dbh->prepare($st) or return dbh->errstr;
1371 push @add_vfields, $_;
1374 unless(@rep_vfields) {
1375 my $st = "UPDATE virtual_field SET value = ? ".
1376 "WHERE recnum = ? AND vfieldpart = ?";
1377 warn "[debug]$me $st\n" if $DEBUG > 2;
1378 $v_rep_sth = dbh->prepare($st) or return dbh->errstr;
1380 push @rep_vfields, $_;
1384 local $SIG{HUP} = 'IGNORE';
1385 local $SIG{INT} = 'IGNORE';
1386 local $SIG{QUIT} = 'IGNORE';
1387 local $SIG{TERM} = 'IGNORE';
1388 local $SIG{TSTP} = 'IGNORE';
1389 local $SIG{PIPE} = 'IGNORE';
1391 my $rc = $sth->execute or return $sth->errstr;
1392 #not portable #return "Record not found (or records identical)." if $rc eq "0E0";
1393 $h_old_sth->execute or return $h_old_sth->errstr if $h_old_sth;
1394 $h_new_sth->execute or return $h_new_sth->errstr if $h_new_sth;
1396 $v_del_sth->execute($old->getfield($primary_key),
1398 or return $v_del_sth->errstr
1399 foreach(@del_vfields);
1401 $v_add_sth->execute($new->getfield($_),
1402 $old->getfield($primary_key),
1404 or return $v_add_sth->errstr
1405 foreach(@add_vfields);
1407 $v_rep_sth->execute($new->getfield($_),
1408 $old->getfield($primary_key),
1410 or return $v_rep_sth->errstr
1411 foreach(@rep_vfields);
1413 dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit;
1415 # Now that it has been saved, reset the encrypted fields so that $new
1416 # can still be used.
1417 foreach my $field (keys %{$saved}) {
1418 $new->setfield($field, $saved->{$field});
1426 my( $self ) = shift;
1427 warn "[$me] replace called with no arguments; autoloading old record\n"
1430 my $primary_key = $self->dbdef_table->primary_key;
1431 if ( $primary_key ) {
1432 $self->by_key( $self->$primary_key() ) #this is what's returned
1433 or croak "can't find ". $self->table. ".$primary_key ".
1434 $self->$primary_key();
1436 croak $self->table. " has no primary key; pass old record as argument";
1443 Depriciated (use replace instead).
1448 cluck "warning: FS::Record::rep deprecated!";
1449 replace @_; #call method in this scope
1454 Checks virtual fields (using check_blocks). Subclasses should still provide
1455 a check method to validate real fields, foreign keys, etc., and call this
1456 method via $self->SUPER::check.
1458 (FIXME: Should this method try to make sure that it I<is> being called from
1459 a subclass's check method, to keep the current semantics as far as possible?)
1464 #confess "FS::Record::check not implemented; supply one in subclass!";
1467 foreach my $field ($self->virtual_fields) {
1468 for ($self->getfield($field)) {
1469 # See notes on check_block in FS::part_virtual_field.
1470 eval $self->pvf($field)->check_block;
1472 #this is bad, probably want to follow the stack backtrace up and see
1474 my $err = "Fatal error checking $field for $self";
1476 return "$err (see log for backtrace): $@";
1479 $self->setfield($field, $_);
1485 =item process_batch_import JOB OPTIONS_HASHREF PARAMS
1487 Processes a batch import as a queued JSRPC job
1489 JOB is an FS::queue entry.
1491 OPTIONS_HASHREF can have the following keys:
1497 Table name (required).
1501 Listref of field names for static fields. They will be given values from the
1502 PARAMS hashref and passed as a "params" hashref to batch_import.
1506 Formats hashref. Keys are field names, values are listrefs that define the
1509 Each listref value can be a column name or a code reference. Coderefs are run
1510 with the row object, data and a FS::Conf object as the three parameters.
1511 For example, this coderef does the same thing as using the "columnname" string:
1514 my( $record, $data, $conf ) = @_;
1515 $record->columnname( $data );
1518 Coderefs are run after all "column name" fields are assigned.
1522 Optional format hashref of types. Keys are field names, values are "csv",
1523 "xls" or "fixedlength". Overrides automatic determination of file type
1526 =item format_headers
1528 Optional format hashref of header lines. Keys are field names, values are 0
1529 for no header, 1 to ignore the first line, or to higher numbers to ignore that
1532 =item format_sep_chars
1534 Optional format hashref of CSV sep_chars. Keys are field names, values are the
1535 CSV separation character.
1537 =item format_fixedlenth_formats
1539 Optional format hashref of fixed length format defintiions. Keys are field
1540 names, values Parse::FixedLength listrefs of field definitions.
1544 Set true to default to CSV file type if the filename does not contain a
1545 recognizable ".csv" or ".xls" extension (and type is not pre-specified by
1550 PARAMS is a base64-encoded Storable string containing the POSTed data as
1551 a hash ref. It normally contains at least one field, "uploaded files",
1552 generated by /elements/file-upload.html and containing the list of uploaded
1553 files. Currently only supports a single file named "file".
1557 use Storable qw(thaw);
1560 sub process_batch_import {
1561 my($job, $opt) = ( shift, shift );
1563 my $table = $opt->{table};
1564 my @pass_params = $opt->{params} ? @{ $opt->{params} } : ();
1565 my %formats = %{ $opt->{formats} };
1567 my $param = thaw(decode_base64(shift));
1568 warn Dumper($param) if $DEBUG;
1570 my $files = $param->{'uploaded_files'}
1571 or die "No files provided.\n";
1573 my (%files) = map { /^(\w+):([\.\w]+)$/ ? ($1,$2):() } split /,/, $files;
1575 my $dir = '%%%FREESIDE_CACHE%%%/cache.'. $FS::UID::datasrc. '/';
1576 my $file = $dir. $files{'file'};
1581 formats => \%formats,
1582 format_types => $opt->{format_types},
1583 format_headers => $opt->{format_headers},
1584 format_sep_chars => $opt->{format_sep_chars},
1585 format_fixedlength_formats => $opt->{format_fixedlength_formats},
1586 format_xml_formats => $opt->{format_xml_formats},
1587 format_row_callbacks => $opt->{format_row_callbacks},
1592 format => $param->{format},
1593 params => { map { $_ => $param->{$_} } @pass_params },
1595 default_csv => $opt->{default_csv},
1596 postinsert_callback => $opt->{postinsert_callback},
1599 if ( $opt->{'batch_namecol'} ) {
1600 $iopt{'batch_namevalue'} = $param->{ $opt->{'batch_namecol'} };
1601 $iopt{$_} = $opt->{$_} foreach qw( batch_keycol batch_table batch_namecol );
1604 my $error = FS::Record::batch_import( \%iopt );
1608 die "$error\n" if $error;
1611 =item batch_import PARAM_HASHREF
1613 Class method for batch imports. Available params:
1619 =item format - usual way to specify import, with this format string selecting data from the formats and format_* info hashes
1625 =item format_headers
1627 =item format_sep_chars
1629 =item format_fixedlength_formats
1631 =item format_row_callbacks
1633 =item fields - Alternate way to specify import, specifying import fields directly as a listref
1635 =item postinsert_callback
1641 FS::queue object, will be updated with progress
1647 csv, xls, fixedlength, xml
1658 warn "$me batch_import call with params: \n". Dumper($param)
1661 my $table = $param->{table};
1663 my $job = $param->{job};
1664 my $file = $param->{file};
1665 my $params = $param->{params} || {};
1667 my( $type, $header, $sep_char, $fixedlength_format,
1668 $xml_format, $row_callback, @fields );
1669 my $postinsert_callback = '';
1670 $postinsert_callback = $param->{'postinsert_callback'}
1671 if $param->{'postinsert_callback'};
1672 if ( $param->{'format'} ) {
1674 my $format = $param->{'format'};
1675 my $formats = $param->{formats};
1676 die "unknown format $format" unless exists $formats->{ $format };
1678 $type = $param->{'format_types'}
1679 ? $param->{'format_types'}{ $format }
1680 : $param->{type} || 'csv';
1683 $header = $param->{'format_headers'}
1684 ? $param->{'format_headers'}{ $param->{'format'} }
1687 $sep_char = $param->{'format_sep_chars'}
1688 ? $param->{'format_sep_chars'}{ $param->{'format'} }
1691 $fixedlength_format =
1692 $param->{'format_fixedlength_formats'}
1693 ? $param->{'format_fixedlength_formats'}{ $param->{'format'} }
1697 $param->{'format_xml_formats'}
1698 ? $param->{'format_xml_formats'}{ $param->{'format'} }
1702 $param->{'format_row_callbacks'}
1703 ? $param->{'format_row_callbacks'}{ $param->{'format'} }
1706 @fields = @{ $formats->{ $format } };
1708 } elsif ( $param->{'fields'} ) {
1710 $type = ''; #infer from filename
1713 $fixedlength_format = '';
1715 @fields = @{ $param->{'fields'} };
1718 die "neither format nor fields specified";
1721 #my $file = $param->{file};
1724 if ( $file =~ /\.(\w+)$/i ) {
1728 warn "can't parse file type from filename $file; defaulting to CSV";
1732 if $param->{'default_csv'} && $type ne 'xls';
1740 if ( $type eq 'csv' || $type eq 'fixedlength' ) {
1742 if ( $type eq 'csv' ) {
1745 $attr{sep_char} = $sep_char if $sep_char;
1746 $parser = new Text::CSV_XS \%attr;
1748 } elsif ( $type eq 'fixedlength' ) {
1750 eval "use Parse::FixedLength;";
1752 $parser = new Parse::FixedLength $fixedlength_format;
1756 die "Unknown file type $type\n";
1759 @buffer = split(/\r?\n/, slurp($file) );
1760 splice(@buffer, 0, ($header || 0) );
1761 $count = scalar(@buffer);
1763 } elsif ( $type eq 'xls' ) {
1765 eval "use Spreadsheet::ParseExcel;";
1768 eval "use DateTime::Format::Excel;";
1769 #for now, just let the error be thrown if it is used, since only CDR
1770 # formats bill_west and troop use it, not other excel-parsing things
1773 my $excel = Spreadsheet::ParseExcel::Workbook->new->Parse($file);
1775 $parser = $excel->{Worksheet}[0]; #first sheet
1777 $count = $parser->{MaxRow} || $parser->{MinRow};
1780 $row = $header || 0;
1781 } elsif ( $type eq 'xml' ) {
1783 eval "use XML::Simple;";
1785 my $xmlrow = $xml_format->{'xmlrow'};
1786 $parser = $xml_format->{'xmlkeys'};
1787 die 'no xmlkeys specified' unless ref $parser eq 'ARRAY';
1788 my $data = XML::Simple::XMLin(
1790 'SuppressEmpty' => '', #sets empty values to ''
1794 $rows = $rows->{$_} foreach @$xmlrow;
1795 $rows = [ $rows ] if ref($rows) ne 'ARRAY';
1796 $count = @buffer = @$rows;
1798 die "Unknown file type $type\n";
1803 local $SIG{HUP} = 'IGNORE';
1804 local $SIG{INT} = 'IGNORE';
1805 local $SIG{QUIT} = 'IGNORE';
1806 local $SIG{TERM} = 'IGNORE';
1807 local $SIG{TSTP} = 'IGNORE';
1808 local $SIG{PIPE} = 'IGNORE';
1810 my $oldAutoCommit = $FS::UID::AutoCommit;
1811 local $FS::UID::AutoCommit = 0;
1814 #my $params = $param->{params} || {};
1815 if ( $param->{'batch_namecol'} && $param->{'batch_namevalue'} ) {
1816 my $batch_col = $param->{'batch_keycol'};
1818 my $batch_class = 'FS::'. $param->{'batch_table'};
1819 my $batch = $batch_class->new({
1820 $param->{'batch_namecol'} => $param->{'batch_namevalue'}
1822 my $error = $batch->insert;
1824 $dbh->rollback if $oldAutoCommit;
1825 return "can't insert batch record: $error";
1827 #primary key via dbdef? (so the column names don't have to match)
1828 my $batch_value = $batch->get( $param->{'batch_keycol'} );
1830 $params->{ $batch_col } = $batch_value;
1833 #my $job = $param->{job};
1836 my( $last, $min_sec ) = ( time, 5 ); #progressbar foo
1840 if ( $type eq 'csv' ) {
1842 last unless scalar(@buffer);
1843 $line = shift(@buffer);
1845 next if $line =~ /^\s*$/; #skip empty lines
1847 $line = &{$row_callback}($line) if $row_callback;
1849 $parser->parse($line) or do {
1850 $dbh->rollback if $oldAutoCommit;
1851 return "can't parse: ". $parser->error_input();
1853 @columns = $parser->fields();
1855 } elsif ( $type eq 'fixedlength' ) {
1857 @columns = $parser->parse($line);
1859 } elsif ( $type eq 'xls' ) {
1861 last if $row > ($parser->{MaxRow} || $parser->{MinRow})
1862 || ! $parser->{Cells}[$row];
1864 my @row = @{ $parser->{Cells}[$row] };
1865 @columns = map $_->{Val}, @row;
1868 #warn $z++. ": $_\n" for @columns;
1870 } elsif ( $type eq 'xml' ) {
1871 # $parser = [ 'Column0Key', 'Column1Key' ... ]
1872 last unless scalar(@buffer);
1873 my $row = shift @buffer;
1874 @columns = @{ $row }{ @$parser };
1876 die "Unknown file type $type\n";
1880 my %hash = %$params;
1882 foreach my $field ( @fields ) {
1884 my $value = shift @columns;
1886 if ( ref($field) eq 'CODE' ) {
1887 #&{$field}(\%hash, $value);
1888 push @later, $field, $value;
1890 #??? $hash{$field} = $value if length($value);
1891 $hash{$field} = $value if defined($value) && length($value);
1896 #my $table = $param->{table};
1897 my $class = "FS::$table";
1899 my $record = $class->new( \%hash );
1902 while ( scalar(@later) ) {
1903 my $sub = shift @later;
1904 my $data = shift @later;
1906 &{$sub}($record, $data, $conf, $param); # $record->&{$sub}($data, $conf)
1909 $dbh->rollback if $oldAutoCommit;
1910 return "can't insert record". ( $line ? " for $line" : '' ). ": $@";
1912 last if exists( $param->{skiprow} );
1914 next if exists( $param->{skiprow} );
1916 my $error = $record->insert;
1919 $dbh->rollback if $oldAutoCommit;
1920 return "can't insert record". ( $line ? " for $line" : '' ). ": $error";
1926 if ( $postinsert_callback ) {
1927 my $error = &{$postinsert_callback}($record, $param);
1929 $dbh->rollback if $oldAutoCommit;
1930 return "postinsert_callback error". ( $line ? " for $line" : '' ).
1935 if ( $job && time - $min_sec > $last ) { #progress bar
1936 $job->update_statustext( int(100 * $imported / $count) );
1942 unless ( $imported || $param->{empty_ok} ) {
1943 $dbh->rollback if $oldAutoCommit;
1944 return "Empty file!";
1947 $dbh->commit or die $dbh->errstr if $oldAutoCommit;;
1954 my( $self, $action, $time ) = @_;
1958 my %nohistory = map { $_=>1 } $self->nohistory_fields;
1961 grep { defined($self->get($_)) && $self->get($_) ne "" && ! $nohistory{$_} }
1962 real_fields($self->table);
1965 # If we're encrypting then don't store the payinfo in the history
1966 if ( $conf && $conf->exists('encryption') ) {
1967 @fields = grep { $_ ne 'payinfo' } @fields;
1970 my @values = map { _quote( $self->getfield($_), $self->table, $_) } @fields;
1972 "INSERT INTO h_". $self->table. " ( ".
1973 join(', ', qw(history_date history_user history_action), @fields ).
1975 join(', ', $time, dbh->quote(getotaker()), dbh->quote($action), @values).
1982 B<Warning>: External use is B<deprecated>.
1984 Replaces COLUMN in record with a unique number, using counters in the
1985 filesystem. Used by the B<insert> method on single-field unique columns
1986 (see L<DBIx::DBSchema::Table>) and also as a fallback for primary keys
1987 that aren't SERIAL (Pg) or AUTO_INCREMENT (mysql).
1989 Returns the new value.
1994 my($self,$field) = @_;
1995 my($table)=$self->table;
1997 croak "Unique called on field $field, but it is ",
1998 $self->getfield($field),
2000 if $self->getfield($field);
2002 #warn "table $table is tainted" if is_tainted($table);
2003 #warn "field $field is tainted" if is_tainted($field);
2005 my($counter) = new File::CounterFile "$table.$field",0;
2007 # getotaker() =~ /^([\w\-]{1,16})$/ or die "Illegal CGI REMOTE_USER!";
2009 # my($counter) = new File::CounterFile "$user/$table.$field",0;
2012 my $index = $counter->inc;
2013 $index = $counter->inc while qsearchs($table, { $field=>$index } );
2015 $index =~ /^(\d*)$/;
2018 $self->setfield($field,$index);
2022 =item ut_float COLUMN
2024 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May not be
2025 null. If there is an error, returns the error, otherwise returns false.
2030 my($self,$field)=@_ ;
2031 ($self->getfield($field) =~ /^\s*(\d+\.\d+)\s*$/ ||
2032 $self->getfield($field) =~ /^\s*(\d+)\s*$/ ||
2033 $self->getfield($field) =~ /^\s*(\d+\.\d+e\d+)\s*$/ ||
2034 $self->getfield($field) =~ /^\s*(\d+e\d+)\s*$/)
2035 or return "Illegal or empty (float) $field: ". $self->getfield($field);
2036 $self->setfield($field,$1);
2039 =item ut_floatn COLUMN
2041 Check/untaint floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
2042 null. If there is an error, returns the error, otherwise returns false.
2046 #false laziness w/ut_ipn
2048 my( $self, $field ) = @_;
2049 if ( $self->getfield($field) =~ /^()$/ ) {
2050 $self->setfield($field,'');
2053 $self->ut_float($field);
2057 =item ut_sfloat COLUMN
2059 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10.
2060 May not be null. If there is an error, returns the error, otherwise returns
2066 my($self,$field)=@_ ;
2067 ($self->getfield($field) =~ /^\s*(-?\d+\.\d+)\s*$/ ||
2068 $self->getfield($field) =~ /^\s*(-?\d+)\s*$/ ||
2069 $self->getfield($field) =~ /^\s*(-?\d+\.\d+[eE]-?\d+)\s*$/ ||
2070 $self->getfield($field) =~ /^\s*(-?\d+[eE]-?\d+)\s*$/)
2071 or return "Illegal or empty (float) $field: ". $self->getfield($field);
2072 $self->setfield($field,$1);
2075 =item ut_sfloatn COLUMN
2077 Check/untaint signed floating point numeric data: 1.1, 1, 1.1e10, 1e10. May be
2078 null. If there is an error, returns the error, otherwise returns false.
2083 my( $self, $field ) = @_;
2084 if ( $self->getfield($field) =~ /^()$/ ) {
2085 $self->setfield($field,'');
2088 $self->ut_sfloat($field);
2092 =item ut_snumber COLUMN
2094 Check/untaint signed numeric data (whole numbers). If there is an error,
2095 returns the error, otherwise returns false.
2100 my($self, $field) = @_;
2101 $self->getfield($field) =~ /^\s*(-?)\s*(\d+)\s*$/
2102 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
2103 $self->setfield($field, "$1$2");
2107 =item ut_snumbern COLUMN
2109 Check/untaint signed numeric data (whole numbers). If there is an error,
2110 returns the error, otherwise returns false.
2115 my($self, $field) = @_;
2116 $self->getfield($field) =~ /^\s*(-?)\s*(\d*)\s*$/
2117 or return "Illegal (numeric) $field: ". $self->getfield($field);
2119 return "Illegal (numeric) $field: ". $self->getfield($field)
2122 $self->setfield($field, "$1$2");
2126 =item ut_number COLUMN
2128 Check/untaint simple numeric data (whole numbers). May not be null. If there
2129 is an error, returns the error, otherwise returns false.
2134 my($self,$field)=@_;
2135 $self->getfield($field) =~ /^\s*(\d+)\s*$/
2136 or return "Illegal or empty (numeric) $field: ". $self->getfield($field);
2137 $self->setfield($field,$1);
2141 =item ut_numbern COLUMN
2143 Check/untaint simple numeric data (whole numbers). May be null. If there is
2144 an error, returns the error, otherwise returns false.
2149 my($self,$field)=@_;
2150 $self->getfield($field) =~ /^\s*(\d*)\s*$/
2151 or return "Illegal (numeric) $field: ". $self->getfield($field);
2152 $self->setfield($field,$1);
2156 =item ut_money COLUMN
2158 Check/untaint monetary numbers. May be negative. Set to 0 if null. If there
2159 is an error, returns the error, otherwise returns false.
2164 my($self,$field)=@_;
2165 $self->setfield($field, 0) if $self->getfield($field) eq '';
2166 $self->getfield($field) =~ /^\s*(\-)?\s*(\d*)(\.\d{2})?\s*$/
2167 or return "Illegal (money) $field: ". $self->getfield($field);
2168 #$self->setfield($field, "$1$2$3" || 0);
2169 $self->setfield($field, ( ($1||''). ($2||''). ($3||'') ) || 0);
2173 =item ut_moneyn COLUMN
2175 Check/untaint monetary numbers. May be negative. If there
2176 is an error, returns the error, otherwise returns false.
2181 my($self,$field)=@_;
2182 if ($self->getfield($field) eq '') {
2183 $self->setfield($field, '');
2186 $self->ut_money($field);
2189 =item ut_text COLUMN
2191 Check/untaint text. Alphanumerics, spaces, and the following punctuation
2192 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? / = [ ] < >
2193 May not be null. If there is an error, returns the error, otherwise returns
2199 my($self,$field)=@_;
2200 #warn "msgcat ". \&msgcat. "\n";
2201 #warn "notexist ". \¬exist. "\n";
2202 #warn "AUTOLOAD ". \&AUTOLOAD. "\n";
2203 $self->getfield($field)
2204 =~ /^([µ_0123456789aAáÁàÀâÂåÅäÄãêæÆbBcCçÇdDðÐeEéÉèÈêÊëËfFgGhHiIíÍìÌîÎïÏjJkKlLmMnNñÑoOóÓòÒôÔöÖõÕøغpPqQrRsSßtTuUúÚùÙûÛüÜvVwWxXyYýÝÿzZþÞ \!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\=\[\]\<\>]+)$/
2205 or return gettext('illegal_or_empty_text'). " $field: ".
2206 $self->getfield($field);
2207 $self->setfield($field,$1);
2211 =item ut_textn COLUMN
2213 Check/untaint text. Alphanumerics, spaces, and the following punctuation
2214 symbols are currently permitted: ! @ # $ % & ( ) - + ; : ' " , . ? /
2215 May be null. If there is an error, returns the error, otherwise returns false.
2220 my($self,$field)=@_;
2221 return $self->setfield($field, '') if $self->getfield($field) =~ /^$/;
2222 $self->ut_text($field);
2225 =item ut_alpha COLUMN
2227 Check/untaint alphanumeric strings (no spaces). May not be null. If there is
2228 an error, returns the error, otherwise returns false.
2233 my($self,$field)=@_;
2234 $self->getfield($field) =~ /^(\w+)$/
2235 or return "Illegal or empty (alphanumeric) $field: ".
2236 $self->getfield($field);
2237 $self->setfield($field,$1);
2241 =item ut_alphan COLUMN
2243 Check/untaint alphanumeric strings (no spaces). May be null. If there is an
2244 error, returns the error, otherwise returns false.
2249 my($self,$field)=@_;
2250 $self->getfield($field) =~ /^(\w*)$/
2251 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
2252 $self->setfield($field,$1);
2256 =item ut_alphasn COLUMN
2258 Check/untaint alphanumeric strings, spaces allowed. May be null. If there is
2259 an error, returns the error, otherwise returns false.
2264 my($self,$field)=@_;
2265 $self->getfield($field) =~ /^([\w ]*)$/
2266 or return "Illegal (alphanumeric) $field: ". $self->getfield($field);
2267 $self->setfield($field,$1);
2272 =item ut_alpha_lower COLUMN
2274 Check/untaint lowercase alphanumeric strings (no spaces). May not be null. If
2275 there is an error, returns the error, otherwise returns false.
2279 sub ut_alpha_lower {
2280 my($self,$field)=@_;
2281 $self->getfield($field) =~ /[[:upper:]]/
2282 and return "Uppercase characters are not permitted in $field";
2283 $self->ut_alpha($field);
2286 =item ut_phonen COLUMN [ COUNTRY ]
2288 Check/untaint phone numbers. May be null. If there is an error, returns
2289 the error, otherwise returns false.
2291 Takes an optional two-letter ISO country code; without it or with unsupported
2292 countries, ut_phonen simply calls ut_alphan.
2297 my( $self, $field, $country ) = @_;
2298 return $self->ut_alphan($field) unless defined $country;
2299 my $phonen = $self->getfield($field);
2300 if ( $phonen eq '' ) {
2301 $self->setfield($field,'');
2302 } elsif ( $country eq 'US' || $country eq 'CA' ) {
2304 $phonen = $conf->config('cust_main-default_areacode').$phonen
2305 if length($phonen)==7 && $conf->config('cust_main-default_areacode');
2306 $phonen =~ /^(\d{3})(\d{3})(\d{4})(\d*)$/
2307 or return gettext('illegal_phone'). " $field: ". $self->getfield($field);
2308 $phonen = "$1-$2-$3";
2309 $phonen .= " x$4" if $4;
2310 $self->setfield($field,$phonen);
2312 warn "warning: don't know how to check phone numbers for country $country";
2313 return $self->ut_textn($field);
2320 Check/untaint hexadecimal values.
2325 my($self, $field) = @_;
2326 $self->getfield($field) =~ /^([\da-fA-F]+)$/
2327 or return "Illegal (hex) $field: ". $self->getfield($field);
2328 $self->setfield($field, uc($1));
2332 =item ut_hexn COLUMN
2334 Check/untaint hexadecimal values. May be null.
2339 my($self, $field) = @_;
2340 $self->getfield($field) =~ /^([\da-fA-F]*)$/
2341 or return "Illegal (hex) $field: ". $self->getfield($field);
2342 $self->setfield($field, uc($1));
2347 Check/untaint ip addresses. IPv4 only for now, though ::1 is auto-translated
2353 my( $self, $field ) = @_;
2354 $self->setfield($field, '127.0.0.1') if $self->getfield($field) eq '::1';
2355 $self->getfield($field) =~ /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/
2356 or return "Illegal (IP address) $field: ". $self->getfield($field);
2357 for ( $1, $2, $3, $4 ) { return "Illegal (IP address) $field" if $_ > 255; }
2358 $self->setfield($field, "$1.$2.$3.$4");
2364 Check/untaint ip addresses. IPv4 only for now, though ::1 is auto-translated
2365 to 127.0.0.1. May be null.
2370 my( $self, $field ) = @_;
2371 if ( $self->getfield($field) =~ /^()$/ ) {
2372 $self->setfield($field,'');
2375 $self->ut_ip($field);
2379 =item ut_coord COLUMN [ LOWER [ UPPER ] ]
2381 Check/untaint coordinates.
2382 Accepts the following forms:
2392 The "DDD MM SS" and "DDD MM MMM" are potentially ambiguous.
2393 The latter form (that is, the MMM are thousands of minutes) is
2394 assumed if the "MMM" is exactly three digits or two digits > 59.
2396 To be safe, just use the DDD.DDDDD form.
2398 If LOWER or UPPER are specified, then the coordinate is checked
2399 for lower and upper bounds, respectively.
2405 my ($self, $field) = (shift, shift);
2407 my $lower = shift if scalar(@_);
2408 my $upper = shift if scalar(@_);
2409 my $coord = $self->getfield($field);
2410 my $neg = $coord =~ s/^(-)//;
2412 my ($d, $m, $s) = (0, 0, 0);
2415 (($d) = ($coord =~ /^(\s*\d{1,3}(?:\.\d+)?)\s*$/)) ||
2416 (($d, $m) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2}(?:\.\d+))\s*$/)) ||
2417 (($d, $m, $s) = ($coord =~ /^(\s*\d{1,3})\s+(\d{1,2})\s+(\d{1,3})\s*$/))
2419 $s = (((($s =~ /^\d{3}$/) or $s > 59) ? ($s / 1000) : ($s / 60)) / 60);
2422 return "Invalid (coordinate with minutes > 59) $field: "
2423 . $self->getfield($field);
2426 $coord = ($neg ? -1 : 1) * sprintf('%.8f', $d + $m + $s);
2428 if (defined($lower) and ($coord < $lower)) {
2429 return "Invalid (coordinate < $lower) $field: "
2430 . $self->getfield($field);;
2433 if (defined($upper) and ($coord > $upper)) {
2434 return "Invalid (coordinate > $upper) $field: "
2435 . $self->getfield($field);;
2438 $self->setfield($field, $coord);
2442 return "Invalid (coordinate) $field: " . $self->getfield($field);
2446 =item ut_coordn COLUMN [ LOWER [ UPPER ] ]
2448 Same as ut_coord, except optionally null.
2454 my ($self, $field) = (shift, shift);
2456 if ($self->getfield($field) =~ /^$/) {
2459 return $self->ut_coord($field, @_);
2465 =item ut_domain COLUMN
2467 Check/untaint host and domain names.
2472 my( $self, $field ) = @_;
2473 #$self->getfield($field) =~/^(\w+\.)*\w+$/
2474 $self->getfield($field) =~/^(([\w\-]+\.)*\w+)$/
2475 or return "Illegal (domain) $field: ". $self->getfield($field);
2476 $self->setfield($field,$1);
2480 =item ut_name COLUMN
2482 Check/untaint proper names; allows alphanumerics, spaces and the following
2483 punctuation: , . - '
2490 my( $self, $field ) = @_;
2491 # warn "ut_name allowed alphanumerics: +(sort grep /\w/, map { chr() } 0..255), "\n";
2492 #$self->getfield($field) =~ /^([\w \,\.\-\']+)$/
2493 $self->getfield($field) =~ /^([µ_0123456789aAáÁàÀâÂåÅäÄãêæÆbBcCçÇdDðÐeEéÉèÈêÊëËfFgGhHiIíÍìÌîÎïÏjJkKlLmMnNñÑoOóÓòÒôÔöÖõÕøغpPqQrRsSßtTuUúÚùÙûÛüÜvVwWxXyYýÝÿzZþÞ \,\.\-\']+)$/
2494 or return gettext('illegal_name'). " $field: ". $self->getfield($field);
2495 $self->setfield($field,$1);
2501 Check/untaint zip codes.
2505 my @zip_reqd_countries = qw( AU CA US ); #CA, US implicit...
2508 my( $self, $field, $country ) = @_;
2510 if ( $country eq 'US' ) {
2512 $self->getfield($field) =~ /^\s*(\d{5}(\-\d{4})?)\s*$/
2513 or return gettext('illegal_zip'). " $field for country $country: ".
2514 $self->getfield($field);
2515 $self->setfield($field, $1);
2517 } elsif ( $country eq 'CA' ) {
2519 $self->getfield($field) =~ /^\s*([A-Z]\d[A-Z])\s*(\d[A-Z]\d)\s*$/i
2520 or return gettext('illegal_zip'). " $field for country $country: ".
2521 $self->getfield($field);
2522 $self->setfield($field, "$1 $2");
2526 if ( $self->getfield($field) =~ /^\s*$/
2527 && ( !$country || ! grep { $_ eq $country } @zip_reqd_countries )
2530 $self->setfield($field,'');
2532 $self->getfield($field) =~ /^\s*(\w[\w\-\s]{2,8}\w)\s*$/
2533 or return gettext('illegal_zip'). " $field: ". $self->getfield($field);
2534 $self->setfield($field,$1);
2542 =item ut_country COLUMN
2544 Check/untaint country codes. Country names are changed to codes, if possible -
2545 see L<Locale::Country>.
2550 my( $self, $field ) = @_;
2551 unless ( $self->getfield($field) =~ /^(\w\w)$/ ) {
2552 if ( $self->getfield($field) =~ /^([\w \,\.\(\)\']+)$/
2553 && country2code($1) ) {
2554 $self->setfield($field,uc(country2code($1)));
2557 $self->getfield($field) =~ /^(\w\w)$/
2558 or return "Illegal (country) $field: ". $self->getfield($field);
2559 $self->setfield($field,uc($1));
2563 =item ut_anything COLUMN
2565 Untaints arbitrary data. Be careful.
2570 my( $self, $field ) = @_;
2571 $self->getfield($field) =~ /^(.*)$/s
2572 or return "Illegal $field: ". $self->getfield($field);
2573 $self->setfield($field,$1);
2577 =item ut_enum COLUMN CHOICES_ARRAYREF
2579 Check/untaint a column, supplying all possible choices, like the "enum" type.
2584 my( $self, $field, $choices ) = @_;
2585 foreach my $choice ( @$choices ) {
2586 if ( $self->getfield($field) eq $choice ) {
2587 $self->setfield($field, $choice);
2591 return "Illegal (enum) field $field: ". $self->getfield($field);
2594 =item ut_enumn COLUMN CHOICES_ARRAYREF
2596 Like ut_enum, except the null value is also allowed.
2601 my( $self, $field, $choices ) = @_;
2602 $self->getfield($field)
2603 ? $self->ut_enum($field, $choices)
2608 =item ut_foreign_key COLUMN FOREIGN_TABLE FOREIGN_COLUMN
2610 Check/untaint a foreign column key. Call a regular ut_ method (like ut_number)
2611 on the column first.
2615 sub ut_foreign_key {
2616 my( $self, $field, $table, $foreign ) = @_;
2617 return '' if $no_check_foreign;
2618 qsearchs($table, { $foreign => $self->getfield($field) })
2619 or return "Can't find ". $self->table. ".$field ". $self->getfield($field).
2620 " in $table.$foreign";
2624 =item ut_foreign_keyn COLUMN FOREIGN_TABLE FOREIGN_COLUMN
2626 Like ut_foreign_key, except the null value is also allowed.
2630 sub ut_foreign_keyn {
2631 my( $self, $field, $table, $foreign ) = @_;
2632 $self->getfield($field)
2633 ? $self->ut_foreign_key($field, $table, $foreign)
2637 =item ut_agentnum_acl COLUMN [ NULL_RIGHT | NULL_RIGHT_LISTREF ]
2639 Checks this column as an agentnum, taking into account the current users's
2640 ACLs. NULL_RIGHT or NULL_RIGHT_LISTREF, if specified, indicates the access
2641 right or rights allowing no agentnum.
2645 sub ut_agentnum_acl {
2646 my( $self, $field ) = (shift, shift);
2647 my $null_acl = scalar(@_) ? shift : [];
2648 $null_acl = [ $null_acl ] unless ref($null_acl);
2650 my $error = $self->ut_foreign_keyn($field, 'agent', 'agentnum');
2651 return "Illegal agentnum: $error" if $error;
2653 my $curuser = $FS::CurrentUser::CurrentUser;
2655 if ( $self->$field() ) {
2657 return "Access denied"
2658 unless $curuser->agentnum($self->$field());
2662 return "Access denied"
2663 unless grep $curuser->access_right($_), @$null_acl;
2671 =item virtual_fields [ TABLE ]
2673 Returns a list of virtual fields defined for the table. This should not
2674 be exported, and should only be called as an instance or class method.
2678 sub virtual_fields {
2681 $table = $self->table or confess "virtual_fields called on non-table";
2683 confess "Unknown table $table" unless dbdef->table($table);
2685 return () unless dbdef->table('part_virtual_field');
2687 unless ( $virtual_fields_cache{$table} ) {
2688 my $query = 'SELECT name from part_virtual_field ' .
2689 "WHERE dbtable = '$table'";
2691 my $result = $dbh->selectcol_arrayref($query);
2692 confess "Error executing virtual fields query: $query: ". $dbh->errstr
2694 $virtual_fields_cache{$table} = $result;
2697 @{$virtual_fields_cache{$table}};
2702 =item fields [ TABLE ]
2704 This is a wrapper for real_fields and virtual_fields. Code that called
2705 fields before should probably continue to call fields.
2710 my $something = shift;
2712 if($something->isa('FS::Record')) {
2713 $table = $something->table;
2715 $table = $something;
2716 $something = "FS::$table";
2718 return (real_fields($table), $something->virtual_fields());
2721 =item pvf FIELD_NAME
2723 Returns the FS::part_virtual_field object corresponding to a field in the
2724 record (specified by FIELD_NAME).
2729 my ($self, $name) = (shift, shift);
2731 if(grep /^$name$/, $self->virtual_fields) {
2732 return qsearchs('part_virtual_field', { dbtable => $self->table,
2738 =item vfieldpart_hashref TABLE
2740 Returns a hashref of virtual field names and vfieldparts applicable to the given
2745 sub vfieldpart_hashref {
2747 my $table = $self->table;
2749 return {} unless dbdef->table('part_virtual_field');
2752 my $statement = "SELECT vfieldpart, name FROM part_virtual_field WHERE ".
2753 "dbtable = '$table'";
2754 my $sth = $dbh->prepare($statement);
2755 $sth->execute or croak "Execution of '$statement' failed: ".$dbh->errstr;
2756 return { map { $_->{name}, $_->{vfieldpart} }
2757 @{$sth->fetchall_arrayref({})} };
2761 =item encrypt($value)
2763 Encrypts the credit card using a combination of PK to encrypt and uuencode to armour.
2765 Returns the encrypted string.
2767 You should generally not have to worry about calling this, as the system handles this for you.
2772 my ($self, $value) = @_;
2775 if ($conf->exists('encryption')) {
2776 if ($self->is_encrypted($value)) {
2777 # Return the original value if it isn't plaintext.
2778 $encrypted = $value;
2781 if (ref($rsa_encrypt) =~ /::RSA/) { # We Can Encrypt
2782 # RSA doesn't like the empty string so let's pack it up
2783 # The database doesn't like the RSA data so uuencode it
2784 my $length = length($value)+1;
2785 $encrypted = pack("u*",$rsa_encrypt->encrypt(pack("Z$length",$value)));
2787 die ("You can't encrypt w/o a valid RSA engine - Check your installation or disable encryption");
2794 =item is_encrypted($value)
2796 Checks to see if the string is encrypted and returns true or false (1/0) to indicate it's status.
2802 my ($self, $value) = @_;
2803 # Possible Bug - Some work may be required here....
2805 if ($value =~ /^M/ && length($value) > 80) {
2812 =item decrypt($value)
2814 Uses the private key to decrypt the string. Returns the decryoted string or undef on failure.
2816 You should generally not have to worry about calling this, as the system handles this for you.
2821 my ($self,$value) = @_;
2822 my $decrypted = $value; # Will return the original value if it isn't encrypted or can't be decrypted.
2823 if ($conf->exists('encryption') && $self->is_encrypted($value)) {
2825 if (ref($rsa_decrypt) =~ /::RSA/) {
2826 my $encrypted = unpack ("u*", $value);
2827 $decrypted = unpack("Z*", eval{$rsa_decrypt->decrypt($encrypted)});
2828 if ($@) {warn "Decryption Failed"};
2836 #Initialize the Module
2837 $rsa_module = 'Crypt::OpenSSL::RSA'; # The Default
2839 if ($conf->exists('encryptionmodule') && $conf->config('encryptionmodule') ne '') {
2840 $rsa_module = $conf->config('encryptionmodule');
2844 eval ("require $rsa_module"); # No need to import the namespace
2847 # Initialize Encryption
2848 if ($conf->exists('encryptionpublickey') && $conf->config('encryptionpublickey') ne '') {
2849 my $public_key = join("\n",$conf->config('encryptionpublickey'));
2850 $rsa_encrypt = $rsa_module->new_public_key($public_key);
2853 # Intitalize Decryption
2854 if ($conf->exists('encryptionprivatekey') && $conf->config('encryptionprivatekey') ne '') {
2855 my $private_key = join("\n",$conf->config('encryptionprivatekey'));
2856 $rsa_decrypt = $rsa_module->new_private_key($private_key);
2860 =item h_search ACTION
2862 Given an ACTION, either "insert", or "delete", returns the appropriate history
2863 record corresponding to this record, if any.
2868 my( $self, $action ) = @_;
2870 my $table = $self->table;
2873 my $primary_key = dbdef->table($table)->primary_key;
2876 'table' => "h_$table",
2877 'hashref' => { $primary_key => $self->$primary_key(),
2878 'history_action' => $action,
2886 Given an ACTION, either "insert", or "delete", returns the timestamp of the
2887 appropriate history record corresponding to this record, if any.
2892 my($self, $action) = @_;
2893 my $h = $self->h_search($action);
2894 $h ? $h->history_date : '';
2897 =item scalar_sql SQL [ PLACEHOLDER, ... ]
2899 A class or object method. Executes the sql statement represented by SQL and
2900 returns a scalar representing the result: the first column of the first row.
2902 Dies on bogus SQL. Returns an empty string if no row is returned.
2904 Typically used for statments which return a single value such as "SELECT
2905 COUNT(*) FROM table WHERE something" OR "SELECT column FROM table WHERE key = ?"
2910 my($self, $sql) = (shift, shift);
2911 my $sth = dbh->prepare($sql) or die dbh->errstr;
2913 or die "Unexpected error executing statement $sql: ". $sth->errstr;
2914 my $row = $sth->fetchrow_arrayref or return '';
2915 my $scalar = $row->[0];
2916 defined($scalar) ? $scalar : '';
2925 =item real_fields [ TABLE ]
2927 Returns a list of the real columns in the specified table. Called only by
2928 fields() and other subroutines elsewhere in FS::Record.
2935 my($table_obj) = dbdef->table($table);
2936 confess "Unknown table $table" unless $table_obj;
2937 $table_obj->columns;
2940 =item _quote VALUE, TABLE, COLUMN
2942 This is an internal function used to construct SQL statements. It returns
2943 VALUE DBI-quoted (see L<DBI/"quote">) unless VALUE is a number and the column
2944 type (see L<DBIx::DBSchema::Column>) does not end in `char' or `binary'.
2949 my($value, $table, $column) = @_;
2950 my $column_obj = dbdef->table($table)->column($column);
2951 my $column_type = $column_obj->type;
2952 my $nullable = $column_obj->null;
2954 warn " $table.$column: $value ($column_type".
2955 ( $nullable ? ' NULL' : ' NOT NULL' ).
2956 ")\n" if $DEBUG > 2;
2958 if ( $value eq '' && $nullable ) {
2960 } elsif ( $value eq '' && $column_type =~ /^(int|numeric)/ ) {
2961 cluck "WARNING: Attempting to set non-null integer $table.$column null; ".
2964 } elsif ( $value =~ /^\d+(\.\d+)?$/ &&
2965 ! $column_type =~ /(char|binary|text)$/i ) {
2967 } elsif (( $column_type =~ /^bytea$/i || $column_type =~ /(blob|varbinary)/i )
2968 && driver_name eq 'Pg'
2972 # dbh->quote($value, { pg_type => PG_BYTEA() }); # doesn't work right
2973 # Pg binary string quoting: convert each character to 3-digit octal prefixed with \\,
2974 # single-quote the whole mess, and put an "E" in front.
2975 return ("E'" . join('', map { sprintf('\\\\%03o', ord($_)) } split(//, $value) ) . "'");
2983 This is deprecated. Don't use it.
2985 It returns a hash-type list with the fields of this record's table set true.
2990 carp "warning: hfields is deprecated";
2993 foreach (fields($table)) {
3002 "$_: ". $self->getfield($_). "|"
3003 } (fields($self->table)) );
3006 sub DESTROY { return; }
3010 # #use Carp qw(cluck);
3011 # #cluck "DESTROYING $self";
3012 # warn "DESTROYING $self";
3016 # return ! eval { join('',@_), kill 0; 1; };
3019 =item str2time_sql [ DRIVER_NAME ]
3021 Returns a function to convert to unix time based on database type, such as
3022 "EXTRACT( EPOCH FROM" for Pg or "UNIX_TIMESTAMP(" for mysql. See
3023 the str2time_sql_closing method to return a closing string rather than just
3024 using a closing parenthesis as previously suggested.
3026 You can pass an optional driver name such as "Pg", "mysql" or
3027 $dbh->{Driver}->{Name} to return a function for that database instead of
3028 the current database.
3033 my $driver = shift || driver_name;
3035 return 'UNIX_TIMESTAMP(' if $driver =~ /^mysql/i;
3036 return 'EXTRACT( EPOCH FROM ' if $driver =~ /^Pg/i;
3038 warn "warning: unknown database type $driver; guessing how to convert ".
3039 "dates to UNIX timestamps";
3040 return 'EXTRACT(EPOCH FROM ';
3044 =item str2time_sql_closing [ DRIVER_NAME ]
3046 Returns the closing suffix of a function to convert to unix time based on
3047 database type, such as ")::integer" for Pg or ")" for mysql.
3049 You can pass an optional driver name such as "Pg", "mysql" or
3050 $dbh->{Driver}->{Name} to return a function for that database instead of
3051 the current database.
3055 sub str2time_sql_closing {
3056 my $driver = shift || driver_name;
3058 return ' )::INTEGER ' if $driver =~ /^Pg/i;
3062 =item regexp_sql [ DRIVER_NAME ]
3064 Returns the operator to do a regular expression comparison based on database
3065 type, such as '~' for Pg or 'REGEXP' for mysql.
3067 You can pass an optional driver name such as "Pg", "mysql" or
3068 $dbh->{Driver}->{Name} to return a function for that database instead of
3069 the current database.
3074 my $driver = shift || driver_name;
3076 return '~' if $driver =~ /^Pg/i;
3077 return 'REGEXP' if $driver =~ /^mysql/i;
3079 die "don't know how to use regular expressions in ". driver_name." databases";
3083 =item not_regexp_sql [ DRIVER_NAME ]
3085 Returns the operator to do a regular expression negation based on database
3086 type, such as '!~' for Pg or 'NOT REGEXP' for mysql.
3088 You can pass an optional driver name such as "Pg", "mysql" or
3089 $dbh->{Driver}->{Name} to return a function for that database instead of
3090 the current database.
3094 sub not_regexp_sql {
3095 my $driver = shift || driver_name;
3097 return '!~' if $driver =~ /^Pg/i;
3098 return 'NOT REGEXP' if $driver =~ /^mysql/i;
3100 die "don't know how to use regular expressions in ". driver_name." databases";
3104 =item concat_sql [ DRIVER_NAME ] ITEMS_ARRAYREF
3106 Returns the items concatendated based on database type, using "CONCAT()" for
3107 mysql and " || " for Pg and other databases.
3109 You can pass an optional driver name such as "Pg", "mysql" or
3110 $dbh->{Driver}->{Name} to return a function for that database instead of
3111 the current database.
3116 my $driver = ref($_[0]) ? driver_name : shift;
3119 if ( $driver =~ /^mysql/i ) {
3120 'CONCAT('. join(',', @$items). ')';
3122 join('||', @$items);
3131 This module should probably be renamed, since much of the functionality is
3132 of general use. It is not completely unlike Adapter::DBI (see below).
3134 Exported qsearch and qsearchs should be deprecated in favor of method calls
3135 (against an FS::Record object like the old search and searchs that qsearch
3136 and qsearchs were on top of.)
3138 The whole fields / hfields mess should be removed.
3140 The various WHERE clauses should be subroutined.
3142 table string should be deprecated in favor of DBIx::DBSchema::Table.
3144 No doubt we could benefit from a Tied hash. Documenting how exists / defined
3145 true maps to the database (and WHERE clauses) would also help.
3147 The ut_ methods should ask the dbdef for a default length.
3149 ut_sqltype (like ut_varchar) should all be defined
3151 A fallback check method should be provided which uses the dbdef.
3153 The ut_money method assumes money has two decimal digits.
3155 The Pg money kludge in the new method only strips `$'.
3157 The ut_phonen method only checks US-style phone numbers.
3159 The _quote function should probably use ut_float instead of a regex.
3161 All the subroutines probably should be methods, here or elsewhere.
3163 Probably should borrow/use some dbdef methods where appropriate (like sub
3166 As of 1.14, DBI fetchall_hashref( {} ) doesn't set fetchrow_hashref NAME_lc,
3167 or allow it to be set. Working around it is ugly any way around - DBI should
3168 be fixed. (only affects RDBMS which return uppercase column names)
3170 ut_zip should take an optional country like ut_phone.
3174 L<DBIx::DBSchema>, L<FS::UID>, L<DBI>
3176 Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.