X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2FRecord.pm;h=f88be8b00f80639a980148c0bc774f8662656114;hb=5928d6721ca5328d9a687d914296a2dcc3156a11;hp=18541d2db7aca8de3d7e88d68f1f7f05d0cb8d71;hpb=b90f8cdac9371c219a72dda16f8deecc7c44fc28;p=freeside.git diff --git a/FS/FS/Record.pm b/FS/FS/Record.pm index 18541d2db..f88be8b00 100644 --- a/FS/FS/Record.pm +++ b/FS/FS/Record.pm @@ -6,8 +6,8 @@ use subs qw(reload_dbdef); use Exporter; use Carp qw(carp cluck croak confess); use File::CounterFile; +use DBIx::DBSchema; use FS::UID qw(dbh checkruid swapuid getotaker datasrc driver_name); -use FS::dbdef; @ISA = qw(Exporter); @EXPORT_OK = qw(dbh fields hfields qsearch qsearchs dbdef); @@ -126,7 +126,7 @@ sub new { $hashref->{$field}='' unless defined $hashref->{$field}; #trim the '$' and ',' from money fields for Pg (belong HERE?) #(what about Pg i18n?) - if ( driver_name eq 'Pg' + if ( driver_name =~ /^Pg$/i && $self->dbdef_table->column($field)->type eq 'money' ) { ${$hashref}{$field} =~ s/^\$//; ${$hashref}{$field} =~ s/\,//; @@ -169,6 +169,8 @@ objects. sub qsearch { my($table, $record, $select, $extra_sql ) = @_; + $table =~ /^([\w\_]+)$/ or die "Illegal table: $table"; + $table = $1; $select ||= '*'; my $dbh = dbh; @@ -176,9 +178,9 @@ sub qsearch { my $statement = "SELECT $select FROM $table"; if ( @fields ) { - $statement .= " WHERE ". join(' AND ', map { + $statement .= ' WHERE '. join(' AND ', map { if ( ! defined( $record->{$_} ) || $record->{$_} eq '' ) { - if ( driver_name eq 'Pg' ) { + if ( driver_name =~ /^Pg$/i ) { "$_ IS NULL"; } else { qq-( $_ IS NULL OR $_ = "" )-; @@ -191,11 +193,13 @@ sub qsearch { $statement .= " $extra_sql" if defined($extra_sql); warn $statement if $DEBUG; - my $sth = $dbh->prepare_cached($statement) or croak $dbh->errstr; + my $sth = $dbh->prepare($statement) + or croak "$dbh->errstr doing $statement"; $sth->execute( map $record->{$_}, grep defined( $record->{$_} ) && $record->{$_} ne '', @fields ) or croak $dbh->errstr; + $dbh->commit or croak $dbh->errstr if $FS::UID::AutoCommit; if ( eval 'scalar(@FS::'. $table. '::ISA);' ) { if ( eval 'FS::'. $table. '->can(\'new\')' eq \&new ) { @@ -387,6 +391,7 @@ sub insert { local $SIG{PIPE} = 'IGNORE'; $sth->execute or return $sth->errstr; + dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit; ''; } @@ -416,7 +421,7 @@ sub delete { map { $self->getfield($_) eq '' #? "( $_ IS NULL OR $_ = \"\" )" - ? ( driver_name eq 'Pg' + ? ( driver_name =~ /^Pg$/i ? "$_ IS NULL" : "( $_ IS NULL OR $_ = \"\" )" ) @@ -436,6 +441,7 @@ sub delete { my $rc = $sth->execute or return $sth->errstr; #not portable #return "Record not found, statement:\n$statement" if $rc eq "0E0"; + dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit; undef $self; #no need to keep object! @@ -488,7 +494,7 @@ sub replace { map { $old->getfield($_) eq '' #? "( $_ IS NULL OR $_ = \"\" )" - ? ( driver_name eq 'Pg' + ? ( driver_name =~ /^Pg$/i ? "$_ IS NULL" : "( $_ IS NULL OR $_ = \"\" )" ) @@ -507,6 +513,7 @@ sub replace { my $rc = $sth->execute or return $sth->errstr; #not portable #return "Record not found (or records identical)." if $rc eq "0E0"; + dbh->commit or croak dbh->errstr if $FS::UID::AutoCommit; ''; @@ -536,7 +543,7 @@ sub check { =item unique COLUMN Replaces COLUMN in record with a unique number. Called by the B method -on primary keys and single-field unique columns (see L). +on primary keys and single-field unique columns (see L). Returns the new value. =cut @@ -800,7 +807,7 @@ sub ut_anything { This can be used as both a subroutine and a method call. It returns a list of the columns in this record's table, or an explicitly specified table. -(See L). +(See L). =cut @@ -826,15 +833,15 @@ sub fields { =item reload_dbdef([FILENAME]) -Load a database definition (see L), optionally from a non-default -filename. This command is executed at startup unless -I<$FS::Record::setup_hack> is true. Returns a FS::dbdef object. +Load a database definition (see L), optionally from a +non-default filename. This command is executed at startup unless +I<$FS::Record::setup_hack> is true. Returns a DBIx::DBSchema object. =cut sub reload_dbdef { my $file = shift || $dbdef_file; - $dbdef = load FS::dbdef ($file); + $dbdef = load DBIx::DBSchema $file; } =item dbdef @@ -891,6 +898,8 @@ sub hfields { # } (fields($self->table)) ); #} +sub DESTROY { return; } + #sub DESTROY { # my $self = shift; # #use Carp qw(cluck); @@ -906,7 +915,7 @@ sub hfields { =head1 VERSION -$Id: Record.pm,v 1.10 2000-12-03 20:25:20 ivan Exp $ +$Id: Record.pm,v 1.16 2001-05-07 15:42:02 ivan Exp $ =head1 BUGS @@ -951,7 +960,7 @@ be fixed. (only affects RDBMS which return uppercase column names) =head1 SEE ALSO -L, L, L +L, L, L Adapter::DBI from Ch. 11 of Advanced Perl Programming by Sriram Srinivasan.