DBIx/Class/Storage/DBI/Cursor.pm

filter: Statementstmt 89.8% 80 / 89 Branchbran 71.4% 30 / 42 Conditioncond 65.5% 19 / 29 MC/DCmcdc 50.0% 8 / 16 Subroutinesub 88.8% 16 / 18 Podpod 80.0% 4 / 5 totaltotal 78.8% 157 / 199 CC 34 SCAR 20.5
CC
34
Cov
81%
next
13.0
CLONE
12.0
all
10.7
CRAP 7.8
« DBIx/Class/DB.pm ↑ Summary DBIx/Class/Storage/TxnScopeGuard.pm »
1 package DBIx::Class::Storage::DBI::Cursor;
2  
3 146 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 146 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 146 ▶use base 'DBIx::Class::Cursor';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 146 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 146 ▶use Scalar::Util qw(refaddr weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 146 ▶use DBIx::Class::_Util 'detected_reinvoked_destructor';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 146 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 146 __PACKAGE__->mk_group_accessors('simple' =>
14 qw/storage args attrs/
15 );
16  
17 =head1 NAME
18  
19 DBIx::Class::Storage::DBI::Cursor - Object representing a query cursor on a
20 resultset.
21  
22 =head1 SYNOPSIS
23  
24 my $cursor = $schema->resultset('CD')->cursor();
25  
26 # raw values off the database handle in resultset columns/select order
27 my @next_cd_column_values = $cursor->next;
28  
29 # list of all raw values as arrayrefs
30 my @all_cds_column_values = $cursor->all;
31  
32 =head1 DESCRIPTION
33  
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>.
37  
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
41 information.
42  
43 =head1 METHODS
44  
45 =head2 new
46  
47 Returns a new L<DBIx::Class::Storage::DBI::Cursor> object.
48  
49 =cut
50  
51 {
52 146 my %cursor_registry;
53  
54 3780 ▶ sub new {
Subroutine new: called CC=2 SCAR=7.4
Pod documented
55 3780 my ($class, $storage, $args, $attrs) = @_;
56  
57 3780 ▶ my $self = bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
037800
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
58 storage => $storage,
59 args => $args,
60 attrs => $attrs,
61 }, ref $class || $class;
62  
63 3780 if (DBIx::Class::_ENV_::HAS_ITHREADS) {
64  
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;
69  
70 weaken( $cursor_registry{ refaddr($self) } = $self )
71 }
72  
73 3780 return $self;
74 }
75  
76 0 ▶ sub CLONE {
Subroutine CLONE: not called CC=3 SCAR=24.8
77 0 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 0 ▶ my $self = delete $cursor_registry{$_}
Branch: 1 branch
BranchTrueFalseTotal
my $self = delete $cursor_registry{$_} or next 0 0 0
82 or next;
83  
84 0 $self->{_intra_thread} = 1;
85 }
86 }
87 }
88  
89 =head2 next
90  
91 =over 4
92  
93 =item Arguments: none
94  
95 =item Return Value: \@row_columns
96  
97 =back
98  
99 Advances the cursor to the next row and returns an array of column
100 values (the result of L<DBI/fetchrow_array> method).
101  
102 =cut
103  
104 146 ▶sub next {
Subroutine next: called CC=13 SCAR=25.7
Pod documented
105 6141 my $self = shift;
106  
107 6141 ▶ return if $self->{_done};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_done"} 17 6124 6141
108  
109 6124 my $sth;
110  
111 6124 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"rows"} and ($self->{"_pos"} || 0) >= $self->{"attrs"}{"rows"}) 2 6122 6124
Condition100%8/8
$self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"rows"} and ($self->{"_pos"} || 0) >= $self->{"attrs"}{"rows"}
!ll&&!rl&&r
611662
$self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"rows"}
!ll&&!rl&&r
611338
$self->{'_pos'} || 0
l!l
53
MC/DC100%3/3
$self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"rows"} and ($self->{"_pos"} || 0) >= $self->{"attrs"}{"rows"}
$self->{"attrs"}{"software_limit"} $self->{"attrs"}{"rows"} $self->{'_pos'}
Truth table100%4/4
A and B and C
A= $self->{"attrs"}{"software_limit"}B= $self->{"attrs"}{"rows"}C= $self->{'_pos'}
ABCresult
0XX0
10X0
1100
1111
112 $self->{attrs}{software_limit}
113 && $self->{attrs}{rows}
114 && ($self->{_pos}||0) >= $self->{attrs}{rows}
115 ) {
116 2 ▶ if ($sth = $self->sth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth = $self->sth) 2 0 2
117 # explicit finish will issue warnings, unlike the DESTROY below
118 2 ▶ $sth->finish if $sth->FETCH('Active');
Branch: 1 branch
BranchTrueFalseTotal
if $sth->FETCH("Active") 2 0 2
119 }
120 2 $self->{_done} = 1;
121 2 return;
122 }
123  
124 6122 ▶ unless ($sth = $self->sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth = $self->sth) 2708 3414 6122
125 2708 (undef, $sth, undef) = $self->storage->_select( @{$self->{args}} );
126  
127 2697 $self->{_results} = [ (undef) x $sth->FETCH('NUM_OF_FIELDS') ];
128 2697 $sth->bind_columns( \( @{$self->{_results}} ) );
129  
130 2697 ▶ if ( $self->{attrs}{software_limit} and $self->{attrs}{offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"offset"}) 2 2695 2697
Condition100%3/3
$self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"offset"}
!ll&&!rl&&r
269142
MC/DC100%2/2
$self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"offset"}
$self->{"attrs"}{"software_limit"} $self->{"attrs"}{"offset"}
Truth table100%3/3
A and B
A= $self->{"attrs"}{"software_limit"}B= $self->{"attrs"}{"offset"}
ABresult
0X0
100
111
131 2 $sth->fetch for 1 .. $self->{attrs}{offset};
132 }
133  
134 2697 $self->sth($sth);
135 }
136  
137 6111 ▶ if ($sth->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->fetch) { } 5268 843 6111
138 5268 $self->{_pos}++;
139 5268 return @{$self->{_results}};
140 } else {
141 843 $self->{_done} = 1;
142 843 return ();
143 }
144 }
145  
146  
147 =head2 all
148  
149 =over 4
150  
151 =item Arguments: none
152  
153 =item Return Value: \@row_columns+
154  
155 =back
156  
157 Returns a list of arrayrefs of column values for all rows in the
158 L<DBIx::Class::ResultSet>.
159  
160 =cut
161  
162 1677 ▶sub all {
Subroutine all: called CC=7 SCAR=23.7
Pod documented
163 1677 my $self = shift;
164  
165 # delegate to DBIC::Cursor which will delegate back to next()
166 1677 ▶ if ($self->{attrs}{software_limit}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"offset"} || $self->{"attrs"}{"rows"}) 1 1676 1677
Condition50%3/6
$self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"offset"} || $self->{"attrs"}{"rows"}
!ll&&!rl&&r
167601
$self->{"attrs"}{"offset"} or $self->{"attrs"}{"rows"}
l!l&&r!l&&!r
100
MC/DC33%1/3
$self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"offset"} || $self->{"attrs"}{"rows"}
$self->{"attrs"}{"software_limit"} $self->{"attrs"}{"offset"} $self->{"attrs"}{"rows"}
Truth table50%2/4
A and B or C
A= $self->{"attrs"}{"software_limit"}B= $self->{"attrs"}{"offset"}C= $self->{"attrs"}{"rows"}
ABCresult
0XX0
1000
11X1
1011
167 && ($self->{attrs}{offset} || $self->{attrs}{rows})) {
168 1 return $self->next::method(@_);
169 }
170  
171 1676 my $sth;
172  
173 1676 ▶ if ($sth = $self->sth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth = $self->sth) 0 1676 1676
174 # explicit finish will issue warnings, unlike the DESTROY below
175 0 ▶ $sth->finish if ( ! $self->{_done} and $sth->FETCH('Active') );
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"_done"} and $sth->FETCH("Active") 0 0 0
Condition0%0/3
not $self->{"_done"} and $sth->FETCH("Active")
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->{"_done"} and $sth->FETCH("Active")
not $self->{"_done"} $sth->FETCH("Active")
Truth table0%0/3
not(A) and B
A= not $self->{"_done"}B= $sth->FETCH("Active")
ABresult
0X0
100
111
176 0 $self->sth(undef);
177 }
178  
179 1676 (undef, $sth) = $self->storage->_select( @{$self->{args}} );
180  
181 (
182 DBIx::Class::_ENV_::SHUFFLE_UNORDERED_RESULTSETS
183 and
184 ! $self->{attrs}{order_by}
185 and
186 require List::Util
187 )
188 ? List::Util::shuffle( @{$sth->fetchall_arrayref} )
189 1667 : @{$sth->fetchall_arrayref}
190 ;
191 }
192  
193 13634 ▶sub sth {
Subroutine sth: called CC=8 SCAR=22.9
Pod undocumented
194 13634 my $self = shift;
195  
196 13634 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 5834 7800 13634
elsif ($self->{'sth'} and not $self->{'_done'}) { } 3416 8768 12184
197 5834 delete @{$self}{qw/_pos _done _pid _intra_thread/};
198  
199 5834 $self->{sth} = $_[0];
200 5834 ▶ $self->{_pid} = $$ if ! DBIx::Class::_ENV_::BROKEN_FORK and $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 2697 3137 5834
201 }
202 ▶ elsif ($self->{sth} and ! $self->{_done}) {
Condition66%2/3
$self->{"sth"} and not $self->{"_done"}
!ll&&!rl&&r
438403416
MC/DC50%1/2
$self->{"sth"} and not $self->{"_done"}
$self->{"sth"} not $self->{"_done"}
Truth table66%2/3
A and not(B)
A= $self->{"sth"}B= not $self->{"_done"}
ABresult
0X0
100
111
203  
204 3416 my $invalidate_handle_reason;
205  
206 3416 ▶ if (DBIx::Class::_ENV_::HAS_ITHREADS and $self->{_intra_thread} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { $self->{"_pid"} != $$ }) 0 3416 3416
207 $invalidate_handle_reason = 'Multi-thread';
208 }
209 0 elsif (!DBIx::Class::_ENV_::BROKEN_FORK and $self->{_pid} != $$ ) {
210 0 $invalidate_handle_reason = 'Multi-process';
211 }
212  
213 3416 ▶ if ($invalidate_handle_reason) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invalidate_handle_reason) 0 3416 3416
214 $self->storage->throw_exception("$invalidate_handle_reason access attempted while cursor in progress (position $self->{_pos})")
215 0 ▶ if $self->{_pos};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_pos"} 0 0 0
216  
217 # reinvokes the reset logic above
218 0 $self->sth(undef);
219 }
220 }
221  
222 13634 return $self->{sth};
223 }
224  
225 =head2 reset
226  
227 Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
228  
229 =cut
230  
231 3137 ▶sub reset {
Subroutine reset: called CC=2 SCAR=6.9
Pod documented
232 3137 ▶ $_[0]->__finish_sth if $_[0]->{sth};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"sth"} 616 2521 3137
233 3137 $_[0]->sth(undef);
234 }
235  
236  
237 3780 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.1
238 3780 ▶ return if &detected_reinvoked_destructor;
Branch: 1 branch
BranchTrueFalseTotal
if &detected_reinvoked_destructor 0 3780 3780
239  
240 3780 ▶ $_[0]->__finish_sth if $_[0]->{sth};
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"sth"} 2081 1699 3780
241 }
242  
243 2697 ▶sub __finish_sth {
Subroutine __finish_sth: called CC=3 SCAR=11.0
244 # It is (sadly) extremely important to finish() handles we are about
245 # to lose (due to reset() or a DESTROY() ). $rs->reset is the closest
246 # thing the user has to getting to the underlying finish() API and some
247 # DBDs mandate this (e.g. DBD::InterBase will segfault, DBD::Sybase
248 # won't start a transaction sanely, etc)
249 # We also can't use the accessor here, as it will trigger a fork/thread
250 # check, and resetting a cursor in a child is perfectly valid
251  
252 2697 my $self = shift;
253  
254 # No need to care about failures here
255 1852 ▶ try { local $SIG{__WARN__} = sub {}; $self->{sth}->finish } if (
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: not called CC=1 SCAR=0.0
256 2697 ▶ $self->{sth} and ! try { ! $self->{sth}->FETCH('Active') }
Subroutine __ANON__: called CC=1 SCAR=0.0
257 2697 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sth"} and not &try(sub { not $self->{"sth"}->FETCH("Active"); } ) 1852 845 2697
Condition66%2/3
$self->{"sth"} and not &try(sub { not $self->{"sth"}->FETCH("Active"); } )
!ll&&!rl&&r
08451852
MC/DC50%1/2
$self->{"sth"} and not &try(sub { not $self->{"sth"}->FETCH("Active"); } )
$self->{"sth"} not &try(sub { not $self->{"sth"}->FETCH("Active"); } )
Truth table66%2/3
A and not(B)
A= $self->{"sth"}B= not &try(sub { not $self->{"sth"}->FETCH("Active"); } )
ABresult
0X0
100
111
258 }
259  
260 =head1 FURTHER QUESTIONS?
261  
262 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
263  
264 =head1 COPYRIGHT AND LICENSE
265  
266 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
267 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
268 redistribute it and/or modify it under the same terms as the
269 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
270  
271 =cut
272  
273 146 1;
« DBIx/Class/DB.pm ↑ Summary DBIx/Class/Storage/TxnScopeGuard.pm »