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';
13 __PACKAGE__->mk_group_accessors('simple' =>
14 qw/storage args attrs/
19 DBIx::Class::Storage::DBI::Cursor - Object representing a query cursor on a
24 my $cursor = $schema->resultset('CD')->cursor();
26 # raw values off the database handle in resultset columns/select order
27 my @next_cd_column_values = $cursor->next;
29 # list of all raw values as arrayrefs
30 my @all_cds_column_values = $cursor->all;
34 A Cursor represents a query cursor on a L<DBIx::Class::ResultSet> object. It
35 allows for traversing the result set with L</next>, retrieving all results with
36 L</all> and resetting the cursor with L</reset>.
38 Usually, you would use the cursor methods built into L<DBIx::Class::ResultSet>
39 to traverse it. See L<DBIx::Class::ResultSet/next>,
40 L<DBIx::Class::ResultSet/reset> and L<DBIx::Class::ResultSet/all> for more
47 Returns a new L<DBIx::Class::Storage::DBI::Cursor> object.
55 my ($class, $storage, $args, $attrs) = @_;
61 }, ref $class || $class;
63 if (DBIx::Class::_ENV_::HAS_ITHREADS) {
65 # quick "garbage collection" pass - prevents the registry
66 # from slowly growing with a bunch of undef-valued keys
67 defined $cursor_registry{$_} or delete $cursor_registry{$_}
68 for keys %cursor_registry;
70 weaken( $cursor_registry{ refaddr($self) } = $self )
77 for (keys %cursor_registry) {
78 # once marked we no longer care about them, hence no
79 # need to keep in the registry, left alone renumber the
80 # keys (all addresses are now different)
81 my $self = delete $cursor_registry{$_}
84 $self->{_intra_thread} = 1;
95 =item Return Value: \@row_columns
99 Advances the cursor to the next row and returns an array of column
100 values (the result of L<DBI/fetchrow_array> method).
107 return if $self->{_done};
112 $self->{attrs}{software_limit}
113 && $self->{attrs}{rows}
114 && ($self->{_pos}||0) >= $self->{attrs}{rows}
116 if ($sth = $self->sth) {
117 # explicit finish will issue warnings, unlike the DESTROY below
118 $sth->finish if $sth->FETCH('Active');
124 unless ($sth = $self->sth) {
125 (undef, $sth, undef) = $self->storage->_select( @{$self->{args}} );
127 $self->{_results} = [ (undef) x $sth->FETCH('NUM_OF_FIELDS') ];
128 $sth->bind_columns( \( @{$self->{_results}} ) );
130 if ( $self->{attrs}{software_limit} and $self->{attrs}{offset} ) {
131 $sth->fetch for 1 .. $self->{attrs}{offset};
139 return @{$self->{_results}};
151 =item Arguments: none
153 =item Return Value: \@row_columns+
157 Returns a list of arrayrefs of column values for all rows in the
158 L<DBIx::Class::ResultSet>.
165 # delegate to DBIC::Cursor which will delegate back to next()
166 if ($self->{attrs}{software_limit}
167 && ($self->{attrs}{offset} || $self->{attrs}{rows})) {
168 return $self->next::method(@_);
173 if ($sth = $self->sth) {
174 # explicit finish will issue warnings, unlike the DESTROY below
175 $sth->finish if ( ! $self->{_done} and $sth->FETCH('Active') );
179 (undef, $sth) = $self->storage->_select( @{$self->{args}} );
182 DBIx::Class::_ENV_::SHUFFLE_UNORDERED_RESULTSETS
184 ! $self->{attrs}{order_by}
186 ? shuffle @{$sth->fetchall_arrayref}
187 : @{$sth->fetchall_arrayref}
195 delete @{$self}{qw/_pos _done _pid _intra_thread/};
197 $self->{sth} = $_[0];
198 $self->{_pid} = $$ if ! DBIx::Class::_ENV_::BROKEN_FORK and $_[0];
200 elsif ($self->{sth} and ! $self->{_done}) {
202 my $invalidate_handle_reason;
204 if (DBIx::Class::_ENV_::HAS_ITHREADS and $self->{_intra_thread} ) {
205 $invalidate_handle_reason = 'Multi-thread';
207 elsif (!DBIx::Class::_ENV_::BROKEN_FORK and $self->{_pid} != $$ ) {
208 $invalidate_handle_reason = 'Multi-process';
211 if ($invalidate_handle_reason) {
212 $self->storage->throw_exception("$invalidate_handle_reason access attempted while cursor in progress (position $self->{_pos})")
215 # reinvokes the reset logic above
225 Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
230 $_[0]->__finish_sth if $_[0]->{sth};
236 $_[0]->__finish_sth if $_[0]->{sth};
240 # It is (sadly) extremely important to finish() handles we are about
241 # to lose (due to reset() or a DESTROY() ). $rs->reset is the closest
242 # thing the user has to getting to the underlying finish() API and some
243 # DBDs mandate this (e.g. DBD::InterBase will segfault, DBD::Sybase
244 # won't start a transaction sanely, etc)
245 # We also can't use the accessor here, as it will trigger a fork/thread
246 # check, and resetting a cursor in a child is perfectly valid
250 # No need to care about failures here
251 try { local $SIG{__WARN__} = sub {}; $self->{sth}->finish } if (
252 $self->{sth} and ! try { ! $self->{sth}->FETCH('Active') }
256 =head1 FURTHER QUESTIONS?
258 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
260 =head1 COPYRIGHT AND LICENSE
262 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
263 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
264 redistribute it and/or modify it under the same terms as the
265 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.