1 package DBIx::Class::Storage::DBI::Cursor;
3 use base qw/DBIx::Class::Cursor/;
10 DBIx::Class::Storage::DBI::Cursor - Object representing a query cursor on a
15 my $cursor = $schema->resultset('CD')->cursor();
16 my $first_cd = $cursor->next;
20 A Cursor represents a query cursor on a L<DBIx::Class::ResultSet> object. It
21 allows for traversing the result set with L</next>, retrieving all results with
22 L</all> and resetting the cursor with L</reset>.
24 Usually, you would use the cursor methods built into L<DBIx::Class::ResultSet>
25 to traverse it. See L<DBIx::Class::ResultSet/next>,
26 L<DBIx::Class::ResultSet/reset> and L<DBIx::Class::ResultSet/all> for more
33 Returns a new L<DBIx::Class::Storage::DBI::Cursor> object.
38 my ($class, $storage, $args, $attrs) = @_;
39 #use Data::Dumper; warn Dumper(@_);
40 $class = ref $class if ref $class;
46 _dbh_gen => $storage->{_dbh_gen},
49 return bless ($new, $class);
56 =item Arguments: See L<DBIx::Class::ResultSet/as_query>
58 =item Return Value: \[ $sql, @bind ]
62 Returns the SQL statement and bind vars associated with the invocant.
67 my ( $self, $opts ) = @_;
69 $self->throw_exception( "as_query needs a hashref" )
70 if defined $opts and ref $opts ne 'HASH';
72 $opts->{skip_parens} ||= 0;
74 my $storage = $self->{storage};
75 my $sql_maker = $storage->sql_maker;
76 local $sql_maker->{for};
78 my @args = $storage->_select_args(@{$self->{args}});
79 my ($sql, $bind) = $storage->_prep_for_execute(@args[0 .. 2], [@args[4 .. $#args]]);
80 $sql = "($sql)" unless $opts->{skip_parens};
81 return \[ $sql, @$bind ];
90 =item Return Value: \@row_columns
94 Advances the cursor to the next row and returns an array of column
95 values (the result of L<DBI/fetchrow_array> method).
100 my ($storage, $dbh, $self) = @_;
102 $self->_check_dbh_gen;
103 if ($self->{attrs}{rows} && $self->{pos} >= $self->{attrs}{rows}) {
104 $self->{sth}->finish if $self->{sth}->{Active};
108 return if $self->{done};
109 unless ($self->{sth}) {
110 $self->{sth} = ($storage->_select(@{$self->{args}}))[1];
111 if ($self->{attrs}{software_limit}) {
112 if (my $offset = $self->{attrs}{offset}) {
113 $self->{sth}->fetch for 1 .. $offset;
117 my @row = $self->{sth}->fetchrow_array;
129 $self->{storage}->dbh_do($self->can('_dbh_next'), $self);
136 =item Arguments: none
138 =item Return Value: \@row_columns+
142 Returns a list of arrayrefs of column values for all rows in the
143 L<DBIx::Class::ResultSet>.
148 my ($storage, $dbh, $self) = @_;
150 $self->_check_dbh_gen;
151 $self->{sth}->finish if $self->{sth}->{Active};
153 my ($rv, $sth) = $storage->_select(@{$self->{args}});
154 return @{$sth->fetchall_arrayref};
159 if ($self->{attrs}{software_limit}
160 && ($self->{attrs}{offset} || $self->{attrs}{rows})) {
161 return $self->SUPER::all;
163 $self->{storage}->dbh_do($self->can('_dbh_all'), $self);
168 Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
175 # No need to care about failures here
176 eval { $self->{sth}->finish if $self->{sth} && $self->{sth}->{Active} };
184 delete $self->{done};
192 if($self->{_dbh_gen} != $self->{storage}->{_dbh_gen}) {
193 $self->{_dbh_gen} = $self->{storage}->{_dbh_gen};
201 # None of the reasons this would die matter if we're in DESTROY anyways
203 eval { $self->{sth}->finish if $self->{sth} && $self->{sth}->{Active} };