X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=FS%2FFS%2FCursor.pm;h=ec7af93c1c166068d151e72c794ff2116f4a7990;hb=2ea729e55651a288bbf3826c888f430806aab04c;hp=f3bc1e23d5d83713cbe5947707a38f4c03a37966;hpb=1d41419b8faaf6ffb0fc51ba4fbea15bd452e30e;p=freeside.git diff --git a/FS/FS/Cursor.pm b/FS/FS/Cursor.pm index f3bc1e23d..ec7af93c1 100644 --- a/FS/FS/Cursor.pm +++ b/FS/FS/Cursor.pm @@ -2,12 +2,11 @@ package FS::Cursor; use strict; use vars qw($DEBUG $buffer); -use base qw( Exporter ); -use FS::Record qw(qsearch dbdef dbh); -use Data::Dumper; +use FS::Record qw(dbh); use Scalar::Util qw(refaddr); $DEBUG = 0; + # this might become a parameter at some point, but right now, you can # "local $FS::Cursor::buffer = X;" $buffer = 200; @@ -50,12 +49,14 @@ sub new { # the class of record object to return $self->{class} = "FS::".($q->{table} || 'Record'); + # save for later, so forked children will not destroy me when they exit + $self->{pid} = $$; + $self->{id} = sprintf('cursor%08x', refaddr($self)); my $statement = "DECLARE ".$self->{id}." CURSOR FOR ".$q->{statement}; - my $dbh = dbh; - my $sth = $dbh->prepare($statement) - or die $dbh->errstr; + my $sth = dbh->prepare($statement) + or die dbh->errstr; my $bind = 0; foreach my $value ( @{ $q->{value} } ) { my $bind_type = shift @{ $q->{bind_type} }; @@ -64,7 +65,7 @@ sub new { $sth->execute or die $sth->errstr; - $self->{fetch} = $dbh->prepare("FETCH FORWARD $buffer FROM ".$self->{id}); + $self->{fetch} = dbh->prepare("FETCH FORWARD $buffer FROM ".$self->{id}); $self; } @@ -101,6 +102,12 @@ sub refill { scalar @$result; } +sub DESTROY { + my $self = shift; + return unless $self->{pid} eq $$; + dbh->do('CLOSE '. $self->{id}) or die dbh->errstr; # clean-up the cursor in Pg +} + =back =head1 TO DO @@ -111,6 +118,12 @@ Replace all uses of qsearch with this. Doesn't support MySQL. +The cursor will close prematurely if any code issues a rollback/commit. If +you need protection against this use qsearch or fork and get a new dbh +handle. +Normally this issue will represent itself this message. +ERROR: cursor "cursorXXXXXXX" does not exist. + =head1 SEE ALSO L