1 package DBIx::Class::Storage::DBI::Cursor;
6 use base 'DBIx::Class::Cursor';
8 use Scalar::Util qw(refaddr weaken);
9 use DBIx::Class::_Util qw( detected_reinvoked_destructor dbic_internal_try );
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 )
75 sub DBIx::Class::__DBI_Cursor_iThreads_handler__::CLONE {
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;
86 # Dummy NEXTSTATE ensuring the all temporaries on the stack are garbage
87 # collected before leaving this scope. Depending on the code above, this
88 # may very well be just a preventive measure guarding future modifications
99 =item Return Value: \@row_columns
103 Advances the cursor to the next row and returns an array of column
104 values (the result of L<DBI/fetchrow_array> method).
111 return if $self->{_done};
116 $self->{attrs}{software_limit}
117 && $self->{attrs}{rows}
118 && ($self->{_pos}||0) >= $self->{attrs}{rows}
120 if ($sth = $self->sth) {
121 # explicit finish will issue warnings, unlike the DESTROY below
122 $sth->finish if $sth->FETCH('Active');
128 unless ($sth = $self->sth) {
129 (undef, $sth, undef) = $self->storage->_select( @{$self->{args}} );
131 $self->{_results} = [ (undef) x $sth->FETCH('NUM_OF_FIELDS') ];
132 $sth->bind_columns( \( @{$self->{_results}} ) );
134 if ( $self->{attrs}{software_limit} and $self->{attrs}{offset} ) {
135 $sth->fetch for 1 .. $self->{attrs}{offset};
143 return @{$self->{_results}};
155 =item Arguments: none
157 =item Return Value: \@row_columns+
161 Returns a list of arrayrefs of column values for all rows in the
162 L<DBIx::Class::ResultSet>.
169 # delegate to DBIC::Cursor which will delegate back to next()
170 if ($self->{attrs}{software_limit}
171 && ($self->{attrs}{offset} || $self->{attrs}{rows})) {
172 return $self->next::method(@_);
177 if ($sth = $self->sth) {
178 # explicit finish will issue warnings, unlike the DESTROY below
179 $sth->finish if ( ! $self->{_done} and $sth->FETCH('Active') );
183 (undef, $sth) = $self->storage->_select( @{$self->{args}} );
186 DBIx::Class::_ENV_::SHUFFLE_UNORDERED_RESULTSETS
188 ! $self->{attrs}{order_by}
192 ? List::Util::shuffle( @{$sth->fetchall_arrayref} )
193 : @{$sth->fetchall_arrayref}
201 delete @{$self}{qw/_pos _done _pid _intra_thread/};
203 $self->{sth} = $_[0];
204 $self->{_pid} = $$ if ! DBIx::Class::_ENV_::BROKEN_FORK and $_[0];
206 elsif ($self->{sth} and ! $self->{_done}) {
208 my $invalidate_handle_reason;
210 if (DBIx::Class::_ENV_::HAS_ITHREADS and $self->{_intra_thread} ) {
211 $invalidate_handle_reason = 'Multi-thread';
213 elsif (!DBIx::Class::_ENV_::BROKEN_FORK and $self->{_pid} != $$ ) {
214 $invalidate_handle_reason = 'Multi-process';
217 if ($invalidate_handle_reason) {
218 $self->storage->throw_exception("$invalidate_handle_reason access attempted while cursor in progress (position $self->{_pos})")
221 # reinvokes the reset logic above
231 Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
236 $_[0]->__finish_sth if $_[0]->{sth};
239 # Dummy NEXTSTATE ensuring the all temporaries on the stack are garbage
240 # collected before leaving this scope. Depending on the code above, this
241 # may very well be just a preventive measure guarding future modifications
247 return if &detected_reinvoked_destructor;
249 $_[0]->__finish_sth if $_[0]->{sth};
251 # Dummy NEXTSTATE ensuring the all temporaries on the stack are garbage
252 # collected before leaving this scope. Depending on the code above, this
253 # may very well be just a preventive measure guarding future modifications
258 # It is (sadly) extremely important to finish() handles we are about
259 # to lose (due to reset() or a DESTROY() ). $rs->reset is the closest
260 # thing the user has to getting to the underlying finish() API and some
261 # DBDs mandate this (e.g. DBD::InterBase will segfault, DBD::Sybase
262 # won't start a transaction sanely, etc)
263 # We also can't use the accessor here, as it will trigger a fork/thread
264 # check, and resetting a cursor in a child is perfectly valid
268 # No need to care about failures here
270 local $SIG{__WARN__} = sub {};
275 # weird double-negative to catch the case of ->FETCH throwing
276 # and attempt a finish *anyway*
277 ! dbic_internal_try {
278 ! $self->{sth}->FETCH('Active')
282 # Dummy NEXTSTATE ensuring the all temporaries on the stack are garbage
283 # collected before leaving this scope. Depending on the code above, this
284 # may very well be just a preventive measure guarding future modifications
288 =head1 FURTHER QUESTIONS?
290 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
292 =head1 COPYRIGHT AND LICENSE
294 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
295 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
296 redistribute it and/or modify it under the same terms as the
297 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.