1 package DBIx::Class::Storage::DBI::Cursor;
6 use base 'DBIx::Class::Cursor';
9 use Scalar::Util qw(refaddr weaken);
10 use List::Util 'shuffle';
11 use DBIx::Class::_Util 'detect_reinvoked_destructor';
14 __PACKAGE__->mk_group_accessors('simple' =>
15 qw/storage args attrs/
20 DBIx::Class::Storage::DBI::Cursor - Object representing a query cursor on a
25 my $cursor = $schema->resultset('CD')->cursor();
27 # raw values off the database handle in resultset columns/select order
28 my @next_cd_column_values = $cursor->next;
30 # list of all raw values as arrayrefs
31 my @all_cds_column_values = $cursor->all;
35 A Cursor represents a query cursor on a L<DBIx::Class::ResultSet> object. It
36 allows for traversing the result set with L</next>, retrieving all results with
37 L</all> and resetting the cursor with L</reset>.
39 Usually, you would use the cursor methods built into L<DBIx::Class::ResultSet>
40 to traverse it. See L<DBIx::Class::ResultSet/next>,
41 L<DBIx::Class::ResultSet/reset> and L<DBIx::Class::ResultSet/all> for more
48 Returns a new L<DBIx::Class::Storage::DBI::Cursor> object.
56 my ($class, $storage, $args, $attrs) = @_;
62 }, ref $class || $class;
64 if (DBIx::Class::_ENV_::HAS_ITHREADS) {
66 # quick "garbage collection" pass - prevents the registry
67 # from slowly growing with a bunch of undef-valued keys
68 defined $cursor_registry{$_} or delete $cursor_registry{$_}
69 for keys %cursor_registry;
71 weaken( $cursor_registry{ refaddr($self) } = $self )
78 for (keys %cursor_registry) {
79 # once marked we no longer care about them, hence no
80 # need to keep in the registry, left alone renumber the
81 # keys (all addresses are now different)
82 my $self = delete $cursor_registry{$_}
85 $self->{_intra_thread} = 1;
96 =item Return Value: \@row_columns
100 Advances the cursor to the next row and returns an array of column
101 values (the result of L<DBI/fetchrow_array> method).
108 return if $self->{_done};
113 $self->{attrs}{software_limit}
114 && $self->{attrs}{rows}
115 && ($self->{_pos}||0) >= $self->{attrs}{rows}
117 if ($sth = $self->sth) {
118 # explicit finish will issue warnings, unlike the DESTROY below
119 $sth->finish if $sth->FETCH('Active');
125 unless ($sth = $self->sth) {
126 (undef, $sth, undef) = $self->storage->_select( @{$self->{args}} );
128 $self->{_results} = [ (undef) x $sth->FETCH('NUM_OF_FIELDS') ];
129 $sth->bind_columns( \( @{$self->{_results}} ) );
131 if ( $self->{attrs}{software_limit} and $self->{attrs}{offset} ) {
132 $sth->fetch for 1 .. $self->{attrs}{offset};
140 return @{$self->{_results}};
152 =item Arguments: none
154 =item Return Value: \@row_columns+
158 Returns a list of arrayrefs of column values for all rows in the
159 L<DBIx::Class::ResultSet>.
166 # delegate to DBIC::Cursor which will delegate back to next()
167 if ($self->{attrs}{software_limit}
168 && ($self->{attrs}{offset} || $self->{attrs}{rows})) {
169 return $self->next::method(@_);
174 if ($sth = $self->sth) {
175 # explicit finish will issue warnings, unlike the DESTROY below
176 $sth->finish if ( ! $self->{_done} and $sth->FETCH('Active') );
180 (undef, $sth) = $self->storage->_select( @{$self->{args}} );
183 DBIx::Class::_ENV_::SHUFFLE_UNORDERED_RESULTSETS
185 ! $self->{attrs}{order_by}
187 ? shuffle @{$sth->fetchall_arrayref}
188 : @{$sth->fetchall_arrayref}
196 delete @{$self}{qw/_pos _done _pid _intra_thread/};
198 $self->{sth} = $_[0];
199 $self->{_pid} = $$ if ! DBIx::Class::_ENV_::BROKEN_FORK and $_[0];
201 elsif ($self->{sth} and ! $self->{_done}) {
203 my $invalidate_handle_reason;
205 if (DBIx::Class::_ENV_::HAS_ITHREADS and $self->{_intra_thread} ) {
206 $invalidate_handle_reason = 'Multi-thread';
208 elsif (!DBIx::Class::_ENV_::BROKEN_FORK and $self->{_pid} != $$ ) {
209 $invalidate_handle_reason = 'Multi-process';
212 if ($invalidate_handle_reason) {
213 $self->storage->throw_exception("$invalidate_handle_reason access attempted while cursor in progress (position $self->{_pos})")
216 # reinvokes the reset logic above
226 Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
231 $_[0]->__finish_sth if $_[0]->{sth};
237 return if &detect_reinvoked_destructor;
239 $_[0]->__finish_sth if $_[0]->{sth};
243 # It is (sadly) extremely important to finish() handles we are about
244 # to lose (due to reset() or a DESTROY() ). $rs->reset is the closest
245 # thing the user has to getting to the underlying finish() API and some
246 # DBDs mandate this (e.g. DBD::InterBase will segfault, DBD::Sybase
247 # won't start a transaction sanely, etc)
248 # We also can't use the accessor here, as it will trigger a fork/thread
249 # check, and resetting a cursor in a child is perfectly valid
253 # No need to care about failures here
254 try { local $SIG{__WARN__} = sub {}; $self->{sth}->finish } if (
255 $self->{sth} and ! try { ! $self->{sth}->FETCH('Active') }
259 =head1 FURTHER QUESTIONS?
261 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
263 =head1 COPYRIGHT AND LICENSE
265 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
266 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
267 redistribute it and/or modify it under the same terms as the
268 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.