1 package DBIx::Class::Storage::DBI::Cursor;
6 use base qw/DBIx::Class::Cursor/;
9 use Scalar::Util qw/refaddr weaken/;
12 __PACKAGE__->mk_group_accessors('simple' =>
13 qw/storage args attrs/
18 DBIx::Class::Storage::DBI::Cursor - Object representing a query cursor on a
23 my $cursor = $schema->resultset('CD')->cursor();
25 # raw values off the database handle in resultset columns/select order
26 my @next_cd_column_values = $cursor->next;
28 # list of all raw values as arrayrefs
29 my @all_cds_column_values = $cursor->all;
33 A Cursor represents a query cursor on a L<DBIx::Class::ResultSet> object. It
34 allows for traversing the result set with L</next>, retrieving all results with
35 L</all> and resetting the cursor with L</reset>.
37 Usually, you would use the cursor methods built into L<DBIx::Class::ResultSet>
38 to traverse it. See L<DBIx::Class::ResultSet/next>,
39 L<DBIx::Class::ResultSet/reset> and L<DBIx::Class::ResultSet/all> for more
46 Returns a new L<DBIx::Class::Storage::DBI::Cursor> object.
54 my ($class, $storage, $args, $attrs) = @_;
60 }, ref $class || $class;
62 if (DBIx::Class::_ENV_::HAS_ITHREADS) {
64 # quick "garbage collection" pass - prevents the registry
65 # from slowly growing with a bunch of undef-valued keys
66 defined $cursor_registry{$_} or delete $cursor_registry{$_}
67 for keys %cursor_registry;
69 weaken( $cursor_registry{ refaddr($self) } = $self )
76 for (keys %cursor_registry) {
77 # once marked we no longer care about them, hence no
78 # need to keep in the registry, left alone renumber the
79 # keys (all addresses are now different)
80 my $self = delete $cursor_registry{$_}
83 $self->{_intra_thread} = 1;
94 =item Return Value: \@row_columns
98 Advances the cursor to the next row and returns an array of column
99 values (the result of L<DBI/fetchrow_array> method).
106 return if $self->{_done};
111 $self->{attrs}{software_limit}
112 && $self->{attrs}{rows}
113 && ($self->{_pos}||0) >= $self->{attrs}{rows}
115 if ($sth = $self->sth) {
116 # explicit finish will issue warnings, unlike the DESTROY below
117 $sth->finish if $sth->FETCH('Active');
123 unless ($sth = $self->sth) {
124 (undef, $sth, undef) = $self->storage->_select( @{$self->{args}} );
126 $self->{_results} = [ (undef) x $sth->FETCH('NUM_OF_FIELDS') ];
127 $sth->bind_columns( \( @{$self->{_results}} ) );
129 if ( $self->{attrs}{software_limit} and $self->{attrs}{offset} ) {
130 $sth->fetch for 1 .. $self->{attrs}{offset};
138 return @{$self->{_results}};
150 =item Arguments: none
152 =item Return Value: \@row_columns+
156 Returns a list of arrayrefs of column values for all rows in the
157 L<DBIx::Class::ResultSet>.
164 # delegate to DBIC::Cursor which will delegate back to next()
165 if ($self->{attrs}{software_limit}
166 && ($self->{attrs}{offset} || $self->{attrs}{rows})) {
167 return $self->next::method(@_);
172 if ($sth = $self->sth) {
173 # explicit finish will issue warnings, unlike the DESTROY below
174 $sth->finish if ( ! $self->{_done} and $sth->FETCH('Active') );
178 (undef, $sth) = $self->storage->_select( @{$self->{args}} );
180 return @{$sth->fetchall_arrayref};
187 delete @{$self}{qw/_pos _done _pid _intra_thread/};
189 $self->{sth} = $_[0];
190 $self->{_pid} = $$ if ! DBIx::Class::_ENV_::BROKEN_FORK and $_[0];
192 elsif ($self->{sth} and ! $self->{_done}) {
194 my $invalidate_handle_reason;
196 if (DBIx::Class::_ENV_::HAS_ITHREADS and $self->{_intra_thread} ) {
197 $invalidate_handle_reason = 'Multi-thread';
199 elsif (!DBIx::Class::_ENV_::BROKEN_FORK and $self->{_pid} != $$ ) {
200 $invalidate_handle_reason = 'Multi-process';
203 if ($invalidate_handle_reason) {
204 $self->storage->throw_exception("$invalidate_handle_reason access attempted while cursor in progress (position $self->{_pos})")
207 # reinvokes the reset logic above
217 Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
222 $_[0]->__finish_sth if $_[0]->{sth};
228 $_[0]->__finish_sth if $_[0]->{sth};
232 # It is (sadly) extremely important to finish() handles we are about
233 # to lose (due to reset() or a DESTROY() ). $rs->reset is the closest
234 # thing the user has to getting to the underlying finish() API and some
235 # DBDs mandate this (e.g. DBD::InterBase will segfault, DBD::Sybase
236 # won't start a transaction sanely, etc)
237 # We also can't use the accessor here, as it will trigger a fork/thread
238 # check, and resetting a cursor in a child is perfectly valid
242 # No need to care about failures here
243 try { local $SIG{__WARN__} = sub {}; $self->{sth}->finish } if (
244 $self->{sth} and ! try { ! $self->{sth}->FETCH('Active') }