File Coverage

blib/lib/DBIx/Class/Storage/DBI/Cursor.pm
Criterion Covered Total %
statement 79 88 89.7
branch 29 42 69.0
condition 19 29 65.5
subroutine 17 18 94.4
pod 4 5 80.0
total 148 182 81.3


line stmt bran cond sub pod time code
1             package DBIx::Class::Storage::DBI::Cursor;
2              
3 148     148   112872 use strict;
  148         386  
  148         5444  
4 148     148   931 use warnings;
  148         384  
  148         6895  
5              
6 148     148   1019 use base 'DBIx::Class::Cursor';
  148         332  
  148         77798  
7              
8 148     148   1117 use Try::Tiny;
  148         342  
  148         10997  
9 148     148   1026 use Scalar::Util qw(refaddr weaken);
  148         367  
  148         8114  
10 148     148   982 use List::Util 'shuffle';
  148         349  
  148         8612  
11 148     148   1026 use DBIx::Class::_Util 'detected_reinvoked_destructor';
  148         337  
  148         6594  
12 148     148   1010 use namespace::clean;
  148         409  
  148         1562  
13              
14             __PACKAGE__->mk_group_accessors('simple' =>
15             qw/storage args attrs/
16             );
17              
18             =head1 NAME
19              
20             DBIx::Class::Storage::DBI::Cursor - Object representing a query cursor on a
21             resultset.
22              
23             =head1 SYNOPSIS
24              
25             my $cursor = $schema->resultset('CD')->cursor();
26              
27             # raw values off the database handle in resultset columns/select order
28             my @next_cd_column_values = $cursor->next;
29              
30             # list of all raw values as arrayrefs
31             my @all_cds_column_values = $cursor->all;
32              
33             =head1 DESCRIPTION
34              
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>.
38              
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
42             information.
43              
44             =head1 METHODS
45              
46             =head2 new
47              
48             Returns a new L<DBIx::Class::Storage::DBI::Cursor> object.
49              
50             =cut
51              
52             {
53             my %cursor_registry;
54              
55             sub new {
56 3804     3804 1 10224 my ($class, $storage, $args, $attrs) = @_;
57              
58 3804   33     21904 my $self = bless {
59             storage => $storage,
60             args => $args,
61             attrs => $attrs,
62             }, ref $class || $class;
63              
64 3804         7038 if (DBIx::Class::_ENV_::HAS_ITHREADS) {
65              
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;
70              
71             weaken( $cursor_registry{ refaddr($self) } = $self )
72             }
73              
74 3804         25259 return $self;
75             }
76              
77             sub CLONE {
78 0     0   0 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 0 0       0 my $self = delete $cursor_registry{$_}
83             or next;
84              
85 0         0 $self->{_intra_thread} = 1;
86             }
87             }
88             }
89              
90             =head2 next
91              
92             =over 4
93              
94             =item Arguments: none
95              
96             =item Return Value: \@row_columns
97              
98             =back
99              
100             Advances the cursor to the next row and returns an array of column
101             values (the result of L<DBI/fetchrow_array> method).
102              
103             =cut
104              
105             sub next {
106 6148     6148 1 10739 my $self = shift;
107              
108 6148 100       14418 return if $self->{_done};
109              
110 6131         9470 my $sth;
111              
112 6131 100 100     14475 if (
      100        
      100        
113             $self->{attrs}{software_limit}
114             && $self->{attrs}{rows}
115             && ($self->{_pos}||0) >= $self->{attrs}{rows}
116             ) {
117 2 50       6 if ($sth = $self->sth) {
118             # explicit finish will issue warnings, unlike the DESTROY below
119 2 50       24 $sth->finish if $sth->FETCH('Active');
120             }
121 2         8 $self->{_done} = 1;
122 2         7 return;
123             }
124              
125 6129 100       13894 unless ($sth = $self->sth) {
126 2715         7009 (undef, $sth, undef) = $self->storage->_select( @{$self->{args}} );
  2715         45361  
127              
128 2704         92802 $self->{_results} = [ (undef) x $sth->FETCH('NUM_OF_FIELDS') ];
129 2704         7882 $sth->bind_columns( \( @{$self->{_results}} ) );
  2704         17931  
130              
131 2704 100 100     78647 if ( $self->{attrs}{software_limit} and $self->{attrs}{offset} ) {
132 2         32 $sth->fetch for 1 .. $self->{attrs}{offset};
133             }
134              
135 2704         7793 $self->sth($sth);
136             }
137              
138 6118 100       60836 if ($sth->fetch) {
139 5278         13652 $self->{_pos}++;
140 5278         8012 return @{$self->{_results}};
  5278         31065  
141             } else {
142 840         2219 $self->{_done} = 1;
143 840         3875 return ();
144             }
145             }
146              
147              
148             =head2 all
149              
150             =over 4
151              
152             =item Arguments: none
153              
154             =item Return Value: \@row_columns+
155              
156             =back
157              
158             Returns a list of arrayrefs of column values for all rows in the
159             L<DBIx::Class::ResultSet>.
160              
161             =cut
162              
163             sub all {
164 1695     1695 1 3236 my $self = shift;
165              
166             # delegate to DBIC::Cursor which will delegate back to next()
167 1695 50 33     5568 if ($self->{attrs}{software_limit}
      66        
168             && ($self->{attrs}{offset} || $self->{attrs}{rows})) {
169 1         5 return $self->next::method(@_);
170             }
171              
172 1694         2811 my $sth;
173              
174 1694 50       3599 if ($sth = $self->sth) {
175             # explicit finish will issue warnings, unlike the DESTROY below
176 0 0 0     0 $sth->finish if ( ! $self->{_done} and $sth->FETCH('Active') );
177 0         0 $self->sth(undef);
178             }
179              
180 1694         4442 (undef, $sth) = $self->storage->_select( @{$self->{args}} );
  1694         17431  
181              
182             return (
183             DBIx::Class::_ENV_::SHUFFLE_UNORDERED_RESULTSETS
184             and
185             ! $self->{attrs}{order_by}
186             )
187             ? shuffle @{$sth->fetchall_arrayref}
188 1685         39492 : @{$sth->fetchall_arrayref}
  1685         35992  
189             ;
190             }
191              
192             sub sth {
193 13676     13676 0 21836 my $self = shift;
194              
195 13676 100 66     44215 if (@_) {
    100          
196 5851         10519 delete @{$self}{qw/_pos _done _pid _intra_thread/};
  5851         15173  
197              
198 5851         11865 $self->{sth} = $_[0];
199 5851 100       19091 $self->{_pid} = $$ if ! DBIx::Class::_ENV_::BROKEN_FORK and $_[0];
200             }
201             elsif ($self->{sth} and ! $self->{_done}) {
202              
203 3416         5022 my $invalidate_handle_reason;
204              
205 3416 50       10040 if (DBIx::Class::_ENV_::HAS_ITHREADS and $self->{_intra_thread} ) {
206             $invalidate_handle_reason = 'Multi-thread';
207             }
208 0         0 elsif (!DBIx::Class::_ENV_::BROKEN_FORK and $self->{_pid} != $$ ) {
209 0         0 $invalidate_handle_reason = 'Multi-process';
210             }
211              
212 3416 50       7280 if ($invalidate_handle_reason) {
213             $self->storage->throw_exception("$invalidate_handle_reason access attempted while cursor in progress (position $self->{_pos})")
214 0 0       0 if $self->{_pos};
215              
216             # reinvokes the reset logic above
217 0         0 $self->sth(undef);
218             }
219             }
220              
221 13676         33648 return $self->{sth};
222             }
223              
224             =head2 reset
225              
226             Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
227              
228             =cut
229              
230             sub reset {
231 3147 100   3147 1 10803 $_[0]->__finish_sth if $_[0]->{sth};
232 3147         17148 $_[0]->sth(undef);
233             }
234              
235              
236             sub DESTROY {
237 3804 50   3804   10467 return if &detected_reinvoked_destructor;
238              
239 3804 100       39054 $_[0]->__finish_sth if $_[0]->{sth};
240             }
241              
242             sub __finish_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
250              
251 2704     2704   5314 my $self = shift;
252              
253             # No need to care about failures here
254 1862     1862   89517 try { local $SIG{__WARN__} = sub {}; $self->{sth}->finish } if (
  1862         17269  
255 2704     2704   115123 $self->{sth} and ! try { ! $self->{sth}->FETCH('Active') }
256 2704 100 66     19086 );
257             }
258              
259             =head1 FURTHER QUESTIONS?
260              
261             Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
262              
263             =head1 COPYRIGHT AND LICENSE
264              
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>.
269              
270             =cut
271              
272             1;