File Coverage

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


line stmt bran cond sub pod time code
1             package DBIx::Class::Storage::DBI::Cursor;
2              
3 146     146   97760 use strict;
  146         260  
  146         4449  
4 146     146   614 use warnings;
  146         216  
  146         4607  
5              
6 146     146   644 use base 'DBIx::Class::Cursor';
  146         228  
  146         71071  
7              
8 146     146   786 use Try::Tiny;
  146         225  
  146         9693  
9 146     146   724 use Scalar::Util qw(refaddr weaken);
  146         226  
  146         7697  
10 146     146   727 use List::Util 'shuffle';
  146         240  
  146         8299  
11 146     146   711 use DBIx::Class::_Util 'detected_reinvoked_destructor';
  146         232  
  146         6339  
12 146     146   724 use namespace::clean;
  146         224  
  146         1269  
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 object. It
36             allows for traversing the result set with L, retrieving all results with
37             L and resetting the cursor with L.
38              
39             Usually, you would use the cursor methods built into L
40             to traverse it. See L,
41             L and L for more
42             information.
43              
44             =head1 METHODS
45              
46             =head2 new
47              
48             Returns a new L object.
49              
50             =cut
51              
52             {
53             my %cursor_registry;
54              
55             sub new {
56 3788     3788 1 6359 my ($class, $storage, $args, $attrs) = @_;
57              
58 3788   33     23965 my $self = bless {
59             storage => $storage,
60             args => $args,
61             attrs => $attrs,
62             }, ref $class || $class;
63              
64 3788         4384 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 3788         22587 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 method).
102              
103             =cut
104              
105             sub next {
106 6142     6142 1 6388 my $self = shift;
107              
108 6142 100       11656 return if $self->{_done};
109              
110 6125         5642 my $sth;
111              
112 6125 100 66     12364 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       23 $sth->finish if $sth->FETCH('Active');
120             }
121 2         5 $self->{_done} = 1;
122 2         8 return;
123             }
124              
125 6123 100       10086 unless ($sth = $self->sth) {
126 2709         5582 (undef, $sth, undef) = $self->storage->_select( @{$self->{args}} );
  2709         34634  
127              
128 2698         88844 $self->{_results} = [ (undef) x $sth->FETCH('NUM_OF_FIELDS') ];
129 2698         4535 $sth->bind_columns( \( @{$self->{_results}} ) );
  2698         16369  
130              
131 2698 100 66     69992 if ( $self->{attrs}{software_limit} and $self->{attrs}{offset} ) {
132 2         24 $sth->fetch for 1 .. $self->{attrs}{offset};
133             }
134              
135 2698         6114 $self->sth($sth);
136             }
137              
138 6112 100       59622 if ($sth->fetch) {
139 5270         8183 $self->{_pos}++;
140 5270         5330 return @{$self->{_results}};
  5270         37017  
141             } else {
142 842         1694 $self->{_done} = 1;
143 842         3949 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.
160              
161             =cut
162              
163             sub all {
164 1684     1684 1 2075 my $self = shift;
165              
166             # delegate to DBIC::Cursor which will delegate back to next()
167 1684 50 33     4865 if ($self->{attrs}{software_limit}
      66        
168             && ($self->{attrs}{offset} || $self->{attrs}{rows})) {
169 1         4 return $self->next::method(@_);
170             }
171              
172 1683         1893 my $sth;
173              
174 1683 50       3026 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 1683         3906 (undef, $sth) = $self->storage->_select( @{$self->{args}} );
  1683         14554  
181              
182             return (
183             DBIx::Class::_ENV_::SHUFFLE_UNORDERED_RESULTSETS
184             and
185             ! $self->{attrs}{order_by}
186             )
187             ? shuffle @{$sth->fetchall_arrayref}
188 1674         39704 : @{$sth->fetchall_arrayref}
  1674         40162  
189             ;
190             }
191              
192             sub sth {
193 13643     13643 0 12584 my $self = shift;
194              
195 13643 100 66     42098 if (@_) {
    100          
196 5835         6546 delete @{$self}{qw/_pos _done _pid _intra_thread/};
  5835         11500  
197              
198 5835         8449 $self->{sth} = $_[0];
199 5835 100       15863 $self->{_pid} = $$ if ! DBIx::Class::_ENV_::BROKEN_FORK and $_[0];
200             }
201             elsif ($self->{sth} and ! $self->{_done}) {
202              
203 3416         2729 my $invalidate_handle_reason;
204              
205 3416 50       7981 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       5800 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 13643         26105 return $self->{sth};
222             }
223              
224             =head2 reset
225              
226             Resets the cursor to the beginning of the L.
227              
228             =cut
229              
230             sub reset {
231 3137 100   3137 1 9480 $_[0]->__finish_sth if $_[0]->{sth};
232 3137         14912 $_[0]->sth(undef);
233             }
234              
235              
236             sub DESTROY {
237 3788 50   3788   8568 return if &detected_reinvoked_destructor;
238              
239 3788 100       39856 $_[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 2698     2698   3430 my $self = shift;
252              
253             # No need to care about failures here
254 1854     1854   64766 try { local $SIG{__WARN__} = sub {}; $self->{sth}->finish } if (
  1854         17363  
255 2698     2698   84417 $self->{sth} and ! try { ! $self->{sth}->FETCH('Active') }
256 2698 100 66     19508 );
257             }
258              
259             =head1 FURTHER QUESTIONS?
260              
261             Check the list of L.
262              
263             =head1 COPYRIGHT AND LICENSE
264              
265             This module is free software L
266             by the L. You can
267             redistribute it and/or modify it under the same terms as the
268             L.
269              
270             =cut
271              
272             1;