X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FRecord.pm;h=65bd8270f475835f1977e5cebce6063d4b208008;hb=784853b8a9d5ba5a7d91bcc58d64ce81303336f3;hp=e2d0a0fc5645f68796a8b56d69b0c04804c4a0eb;hpb=3b35ccbf226efe00c94f3a72dd1c7ed64d926a7c;p=freeside.git diff --git a/FS/FS/Record.pm b/FS/FS/Record.pm index e2d0a0fc5..65bd8270f 100644 --- a/FS/FS/Record.pm +++ b/FS/FS/Record.pm @@ -3,9 +3,12 @@ package FS::Record; use strict; use vars qw( $AUTOLOAD @ISA @EXPORT_OK $DEBUG $conf $me - %virtual_fields_cache $nowarn_identical $no_update_diff ); + %virtual_fields_cache + $nowarn_identical $no_update_diff $no_check_foreign + ); use Exporter; use Carp qw(carp cluck croak confess); +use Scalar::Util qw( blessed ); use File::CounterFile; use Locale::Country; use DBI qw(:sql_types); @@ -15,7 +18,7 @@ use FS::CurrentUser; use FS::Schema qw(dbdef); use FS::SearchCache; use FS::Msgcat qw(gettext); -use FS::Conf; +#use FS::Conf; #dependency loop bs, in install_callback below instead use FS::part_virtual_field; @@ -24,13 +27,15 @@ use Tie::IxHash; @ISA = qw(Exporter); #export dbdef for now... everything else expects to find it here -@EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch str2time_sql); +@EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef jsearch + str2time_sql str2time_sql_closing ); $DEBUG = 0; $me = '[FS::Record]'; $nowarn_identical = 0; $no_update_diff = 0; +$no_check_foreign = 0; my $rsa_module; my $rsa_loaded; @@ -38,7 +43,9 @@ my $rsa_encrypt; my $rsa_decrypt; FS::UID->install_callback( sub { - $conf = new FS::Conf; + eval "use FS::Conf;"; + die $@ if $@; + $conf = FS::Conf->new; $File::CounterFile::DEFAULT_DIR = $conf->base_dir . "/counters.". datasrc; } ); @@ -234,6 +241,8 @@ fine in the common case where there are only two parameters: =cut +my %TYPE = (); #for debugging + sub qsearch { my($stable, $record, $select, $extra_sql, $order_by, $cache, $addl_from ); my $debug = ''; @@ -294,21 +303,33 @@ sub qsearch { foreach my $field ( grep defined( $record->{$_} ) && $record->{$_} ne '', @real_fields ) { - if ( $record->{$field} =~ /^\d+(\.\d+)?$/ - && dbdef->table($table)->column($field)->type =~ /(int|(big)?serial)/i - ) { - $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_INTEGER } ); - }elsif ( $record->{$field} =~ /^[+-]?\d+(\.\d+)?$/ - && dbdef->table($table)->column($field)->type =~ /(numeric)/i - ) { - $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_FLOAT } ); - }elsif ( $record->{$field} =~ /[-+]?\d*\.?\d+([eE][-+]?\d+)?/ - && dbdef->table($table)->column($field)->type =~ /(float4)/i - ) { - $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_FLOAT } ); - } else { - $sth->bind_param($bind++, $record->{$field}, { TYPE => SQL_VARCHAR } ); + + my $value = $record->{$field}; + $value = $value->{'value'} if ref($value); + my $type = dbdef->table($table)->column($field)->type; + + my $TYPE = SQL_VARCHAR; + if ( $type =~ /(int|(big)?serial)/i && $value =~ /^\d+(\.\d+)?$/ ) { + $TYPE = SQL_INTEGER; + + #DBD::Pg 1.49: Cannot bind ... unknown sql_type 6 + #} elsif ( ( $type =~ /(numeric)/i && $value =~ /^[+-]?\d+(\.\d+)?$/) + # || ( $type =~ /(real|float4)/i + # && $value =~ /[-+]?\d*\.?\d+([eE][-+]?\d+)?/ + # ) + # ) { + # $TYPE = SQL_FLOAT; + } + + if ( $DEBUG > 2 ) { + no strict 'refs'; + %TYPE = map { &{"DBI::$_"}() => $_ } @{ $DBI::EXPORT_TAGS{sql_types} } + unless keys %TYPE; + warn " bind_param $bind (for field $field), $value, TYPE $TYPE{$TYPE}\n"; } + + $sth->bind_param($bind++, $value, { TYPE => $TYPE } ); + } # $sth->execute( map $record->{$_}, @@ -662,11 +683,11 @@ sub AUTOLOAD { $field =~ s/.*://; if ( defined($value) ) { confess "errant AUTOLOAD $field for $self (arg $value)" - unless ref($self) && $self->can('setfield'); + unless blessed($self) && $self->can('setfield'); $self->setfield($field,$value); } else { confess "errant AUTOLOAD $field for $self (no args)" - unless ref($self) && $self->can('getfield'); + unless blessed($self) && $self->can('getfield'); $self->getfield($field); } } @@ -785,18 +806,18 @@ sub insert { } my $table = $self->table; - # Encrypt before the database - my $conf = new FS::Conf; - if ($conf->exists('encryption') && defined(eval '@FS::'. $table . '::encrypted_fields')) { + if ( defined(eval '@FS::'. $table . '::encrypted_fields') + && scalar( eval '@FS::'. $table . '::encrypted_fields') + && $conf->exists('encryption') + ) { foreach my $field (eval '@FS::'. $table . '::encrypted_fields') { $self->{'saved'} = $self->getfield($field); $self->setfield($field, $self->encrypt($self->getfield($field))); } } - #false laziness w/delete my @real_fields = grep { defined($self->getfield($_)) && $self->getfield($_) ne "" } @@ -1066,7 +1087,6 @@ sub replace { return $error if $error; # Encrypt for replace - my $conf = new FS::Conf; my $saved = {}; if ($conf->exists('encryption') && defined(eval '@FS::'. $new->table . '::encrypted_fields')) { foreach my $field (eval '@FS::'. $new->table . '::encrypted_fields') { @@ -1286,7 +1306,6 @@ sub _h_statement { # If we're encrypting then don't ever store the payinfo or CVV2 in the history.... # You can see if it changed by the paymask... - my $conf = new FS::Conf; if ($conf->exists('encryption') ) { @fields = grep $_ ne 'payinfo' && $_ ne 'cvv2', @fields; } @@ -1887,6 +1906,7 @@ on the column first. sub ut_foreign_key { my( $self, $field, $table, $foreign ) = @_; + return '' if $no_check_foreign; qsearchs($table, { $foreign => $self->getfield($field) }) or return "Can't find ". $self->table. ".$field ". $self->getfield($field). " in $table.$foreign"; @@ -2041,7 +2061,6 @@ sub encrypt { my ($self, $value) = @_; my $encrypted; - my $conf = new FS::Conf; if ($conf->exists('encryption')) { if ($self->is_encrypted($value)) { # Return the original value if it isn't plaintext. @@ -2090,7 +2109,6 @@ You should generally not have to worry about calling this, as the system handles sub decrypt { my ($self,$value) = @_; my $decrypted = $value; # Will return the original value if it isn't encrypted or can't be decrypted. - my $conf = new FS::Conf; if ($conf->exists('encryption') && $self->is_encrypted($value)) { $self->loadRSA; if (ref($rsa_decrypt) =~ /::RSA/) { @@ -2107,7 +2125,6 @@ sub loadRSA { #Initialize the Module $rsa_module = 'Crypt::OpenSSL::RSA'; # The Default - my $conf = new FS::Conf; if ($conf->exists('encryptionmodule') && $conf->config_binary('encryptionmodule') ne '') { $rsa_module = $conf->config('encryptionmodule'); }