4 use vars qw($DEBUG $buffer);
6 use FS::UID qw(myconnect driver_name);
7 use Scalar::Util qw(refaddr);
11 # this might become a parameter at some point, but right now, you can
12 # "local $FS::Cursor::buffer = X;"
17 FS::Cursor - Iterator for querying large data sets
23 my $search = FS::Cursor->new('table', { field => 'value' ... });
24 while ( my $row = $search->fetch ) {
34 Constructs a cursored search. Accepts all the same arguments as qsearch,
35 and returns an FS::Cursor object to fetch the rows one at a time.
41 my $q = FS::Record::_query(@_); # builds the statement and parameter list
46 class => 'FS::' . ($q->{table} || 'Record'),
48 position => 0, # for mysql
52 # the class of record object to return
53 $self->{class} = "FS::".($q->{table} || 'Record');
55 # save for later, so forked children will not destroy me when they exit
58 $self->{id} = sprintf('cursor%08x', refaddr($self));
61 if ( driver_name() eq 'Pg' ) {
62 $self->{dbh} = $dbh = myconnect();
63 $statement = "DECLARE ".$self->{id}." CURSOR FOR ".$q->{statement};
64 } elsif ( driver_name() eq 'mysql' ) {
65 # build a cursor from scratch
68 # there are problems doing it this way, and we don't have time to resolve
69 # them all right now...
70 #$statement = "CREATE TEMPORARY TABLE $self->{id}
71 # (rownum INT AUTO_INCREMENT, PRIMARY KEY (rownum))
74 # one of those problems is locking, so keep everything on the main session
75 $self->{dbh} = $dbh = FS::UID::dbh();
76 $statement = $q->{statement};
79 my $sth = $dbh->prepare($statement)
82 foreach my $value ( @{ $q->{value} } ) {
83 my $bind_type = shift @{ $q->{bind_type} };
84 $sth->bind_param($bind++, $value, $bind_type );
87 $sth->execute or die $sth->errstr;
89 if ( driver_name() eq 'Pg' ) {
90 $self->{fetch} = $dbh->prepare("FETCH FORWARD $buffer FROM ".$self->{id});
91 } elsif ( driver_name() eq 'mysql' ) {
92 # make sure we're not holding any locks on the tables mentioned
94 #$dbh->commit if driver_name() eq 'mysql';
95 #$self->{fetch} = $dbh->prepare("SELECT * FROM $self->{id} ORDER BY rownum LIMIT ?, $buffer");
97 # instead, fetch all the rows at once
98 $self->{buffer} = $sth->fetchall_arrayref( {} );
112 Fetch the next row from the search results.
117 # might be a little more efficient to do a FETCH NEXT 1000 or something
118 # and buffer them locally, but the semantics are simpler this way
120 if (@{ $self->{buffer} } == 0) {
121 my $rows = $self->refill;
122 return undef if !$rows;
124 $self->{class}->new(shift @{ $self->{buffer} });
129 if (driver_name() eq 'Pg') {
130 my $sth = $self->{fetch};
131 $sth->bind_param(1, $self->{position}) if driver_name() eq 'mysql';
132 $sth->execute or die $sth->errstr;
133 my $result = $self->{fetch}->fetchall_arrayref( {} );
134 $self->{buffer} = $result;
135 $self->{position} += $sth->rows;
137 } # mysql can't be refilled, since everything is buffered from the start
142 return if driver_name() eq 'mysql';
144 return unless $self->{pid} eq $$;
145 $self->{dbh}->do('CLOSE '. $self->{id})
146 or die $self->{dbh}->errstr; # clean-up the cursor in Pg
147 $self->{dbh}->rollback;
148 $self->{dbh}->disconnect;
155 Replace all uses of qsearch with this.
159 Still doesn't really support MySQL, but it pretends it does, by simply
160 running the query and returning records one at a time.
162 The cursor will close prematurely if any code issues a rollback/commit. If
163 you need protection against this use qsearch or fork and get a new dbh
165 Normally this issue will represent itself this message.
166 ERROR: cursor "cursorXXXXXXX" does not exist.