DBIx/Class/Storage/DBI/Cursor.pm

filter: Statementstmt 89.4% 76 / 85 Branchbran 72.5% 29 / 40 Conditioncond 65.5% 19 / 29 MC/DCmcdc 43.7% 7 / 16 Subroutinesub 93.7% 15 / 16 Podpod 80.0% 4 / 5 totaltotal 78.5% 150 / 191 CC 33 SCAR 20.9
CC
33
Cov
81%
next
13.0
CLONE
12.0
all
10.7
CRAP 8.1
« DBIx/Class/Storage/DBI/ACCESS.pm ↑ Summary DBIx/Class/DB.pm »
1 package DBIx::Class::Storage::DBI::Cursor;
2  
3 139 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 139 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 139 ▶use base qw/DBIx::Class::Cursor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 139 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 139 ▶use Scalar::Util qw/refaddr weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 139 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 139 __PACKAGE__->mk_group_accessors('simple' =>
13 qw/storage args attrs/
14 );
15  
16 =head1 NAME
17  
18 DBIx::Class::Storage::DBI::Cursor - Object representing a query cursor on a
19 resultset.
20  
21 =head1 SYNOPSIS
22  
23 my $cursor = $schema->resultset('CD')->cursor();
24  
25 # raw values off the database handle in resultset columns/select order
26 my @next_cd_column_values = $cursor->next;
27  
28 # list of all raw values as arrayrefs
29 my @all_cds_column_values = $cursor->all;
30  
31 =head1 DESCRIPTION
32  
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>.
36  
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
40 information.
41  
42 =head1 METHODS
43  
44 =head2 new
45  
46 Returns a new L<DBIx::Class::Storage::DBI::Cursor> object.
47  
48 =cut
49  
50 {
51 139 my %cursor_registry;
52  
53 sub new {
54 3514 ▶ my ($class, $storage, $args, $attrs) = @_;
Subroutine new: called CC=2 SCAR=7.4
Pod documented
55  
56 3514 ▶ my $self = bless {
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
035140
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
57 storage => $storage,
58 args => $args,
59 attrs => $attrs,
60 }, ref $class || $class;
61  
62 3514 if (DBIx::Class::_ENV_::HAS_ITHREADS) {
63  
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;
68  
69 weaken( $cursor_registry{ refaddr($self) } = $self )
70 }
71  
72 3514 return $self;
73 }
74  
75 sub CLONE {
76 0 ▶ for (keys %cursor_registry) {
Subroutine CLONE: not called CC=3 SCAR=24.8
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 0 ▶ my $self = delete $cursor_registry{$_}
Branch: 1 branch
BranchTrueFalseTotal
my $self = delete $cursor_registry{$_} or next 0 0 0
81 or next;
82  
83 0 $self->{_intra_thread} = 1;
84 }
85 }
86 }
87  
88 =head2 next
89  
90 =over 4
91  
92 =item Arguments: none
93  
94 =item Return Value: \@row_columns
95  
96 =back
97  
98 Advances the cursor to the next row and returns an array of column
99 values (the result of L<DBI/fetchrow_array> method).
100  
101 =cut
102  
103 139 sub next {
104 6023 ▶ my $self = shift;
Subroutine next: called CC=13 SCAR=25.7
Pod documented
105  
106 6023 ▶ return if $self->{_done};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_done"} 13 6010 6023
107  
108 6010 my $sth;
109  
110 6010 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"rows"} and ($self->{"_pos"} || 0) >= $self->{"attrs"}{"rows"}) 2 6008 6010
Condition100%8/8
$self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"rows"} and ($self->{"_pos"} || 0) >= $self->{"attrs"}{"rows"}
!ll&&!rl&&r
600262
$self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"rows"}
!ll&&!rl&&r
599938
$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
111 $self->{attrs}{software_limit}
112 && $self->{attrs}{rows}
113 && ($self->{_pos}||0) >= $self->{attrs}{rows}
114 ) {
115 2 ▶ if ($sth = $self->sth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth = $self->sth) 2 0 2
116 # explicit finish will issue warnings, unlike the DESTROY below
117 2 ▶ $sth->finish if $sth->FETCH('Active');
Branch: 1 branch
BranchTrueFalseTotal
if $sth->FETCH("Active") 2 0 2
118 }
119 2 $self->{_done} = 1;
120 2 return;
121 }
122  
123 6008 ▶ unless ($sth = $self->sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth = $self->sth) 2616 3392 6008
124 2616 (undef, $sth, undef) = $self->storage->_select( @{$self->{args}} );
125  
126 2600 $self->{_results} = [ (undef) x $sth->FETCH('NUM_OF_FIELDS') ];
127 2600 $sth->bind_columns( \( @{$self->{_results}} ) );
128  
129 2600 ▶ if ( $self->{attrs}{software_limit} and $self->{attrs}{offset} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"offset"}) 2 2598 2600
Condition100%3/3
$self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"offset"}
!ll&&!rl&&r
259442
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
130 2 $sth->fetch for 1 .. $self->{attrs}{offset};
131 }
132  
133 2600 $self->sth($sth);
134 }
135  
136 5992 ▶ if ($sth->fetch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth->fetch) { } 5172 820 5992
137 5172 $self->{_pos}++;
138 5172 return @{$self->{_results}};
139 } else {
140 820 $self->{_done} = 1;
141 820 return ();
142 }
143 }
144  
145  
146 =head2 all
147  
148 =over 4
149  
150 =item Arguments: none
151  
152 =item Return Value: \@row_columns+
153  
154 =back
155  
156 Returns a list of arrayrefs of column values for all rows in the
157 L<DBIx::Class::ResultSet>.
158  
159 =cut
160  
161 sub all {
162 1501 ▶ my $self = shift;
Subroutine all: called CC=7 SCAR=23.7
Pod documented
163  
164 # delegate to DBIC::Cursor which will delegate back to next()
165 1501 ▶ if ($self->{attrs}{software_limit}
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"offset"} || $self->{"attrs"}{"rows"}) 1 1500 1501
Condition50%3/6
$self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"offset"} || $self->{"attrs"}{"rows"}
!ll&&!rl&&r
150010
$self->{"attrs"}{"offset"} or $self->{"attrs"}{"rows"}
l!l&&r!l&&!r
100
MC/DC0%0/3
$self->{"attrs"}{"software_limit"} and $self->{"attrs"}{"offset"} || $self->{"attrs"}{"rows"}
$self->{"attrs"}{"software_limit"} $self->{"attrs"}{"offset"} $self->{"attrs"}{"rows"}
Truth table25%1/4
A and B or C
A= $self->{"attrs"}{"software_limit"}B= $self->{"attrs"}{"offset"}C= $self->{"attrs"}{"rows"}
ABCresult
0XX0
1000
11X1
1011
166 && ($self->{attrs}{offset} || $self->{attrs}{rows})) {
167 1 return $self->next::method(@_);
168 }
169  
170 1500 my $sth;
171  
172 1500 ▶ if ($sth = $self->sth) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sth = $self->sth) 0 1500 1500
173 # explicit finish will issue warnings, unlike the DESTROY below
174 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
175 0 $self->sth(undef);
176 }
177  
178 1500 (undef, $sth) = $self->storage->_select( @{$self->{args}} );
179  
180 1491 return @{$sth->fetchall_arrayref};
181 }
182  
183 sub sth {
184 13073 ▶ my $self = shift;
Subroutine sth: called CC=8 SCAR=22.9
Pod undocumented
185  
186 13073 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 5563 7510 13073
elsif ($self->{'sth'} and not $self->{'_done'}) { } 3394 8232 11626
187 5563 delete @{$self}{qw/_pos _done _pid _intra_thread/};
188  
189 5563 $self->{sth} = $_[0];
190 5563 ▶ $self->{_pid} = $$ if ! DBIx::Class::_ENV_::BROKEN_FORK and $_[0];
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 2600 2963 5563
191 }
192 ▶ elsif ($self->{sth} and ! $self->{_done}) {
Condition66%2/3
$self->{"sth"} and not $self->{"_done"}
!ll&&!rl&&r
411603394
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
193  
194 3394 my $invalidate_handle_reason;
195  
196 3394 ▶ if (DBIx::Class::_ENV_::HAS_ITHREADS and $self->{_intra_thread} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { $self->{"_pid"} != $$ }) 0 3394 3394
197 $invalidate_handle_reason = 'Multi-thread';
198 }
199 0 elsif (!DBIx::Class::_ENV_::BROKEN_FORK and $self->{_pid} != $$ ) {
200 0 $invalidate_handle_reason = 'Multi-process';
201 }
202  
203 3394 ▶ if ($invalidate_handle_reason) {
Branch: 1 branch
BranchTrueFalseTotal
if ($invalidate_handle_reason) 0 3394 3394
204 $self->storage->throw_exception("$invalidate_handle_reason access attempted while cursor in progress (position $self->{_pos})")
205 0 ▶ if $self->{_pos};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_pos"} 0 0 0
206  
207 # reinvokes the reset logic above
208 0 $self->sth(undef);
209 }
210 }
211  
212 13073 return $self->{sth};
213 }
214  
215 =head2 reset
216  
217 Resets the cursor to the beginning of the L<DBIx::Class::ResultSet>.
218  
219 =cut
220  
221 sub reset {
222 2963 ▶ $_[0]->__finish_sth if $_[0]->{sth};
Subroutine reset: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"sth"} 615 2348 2963
223 2963 $_[0]->sth(undef);
224 }
225  
226  
227 sub DESTROY {
228 3514 ▶ $_[0]->__finish_sth if $_[0]->{sth};
Subroutine DESTROY: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]{"sth"} 1985 1529 3514
229 }
230  
231 sub __finish_sth {
232 # It is (sadly) extremely important to finish() handles we are about
233 # to lose (due to reset() or a DESTROY() ). $rs->reset is the closest
234 # thing the user has to getting to the underlying finish() API and some
235 # DBDs mandate this (e.g. DBD::InterBase will segfault, DBD::Sybase
236 # won't start a transaction sanely, etc)
237 # We also can't use the accessor here, as it will trigger a fork/thread
238 # check, and resetting a cursor in a child is perfectly valid
239  
240 2600 ▶ my $self = shift;
Subroutine __finish_sth: called CC=3 SCAR=11.0
241  
242 # No need to care about failures here
243 1778 ▶ try { local $SIG{__WARN__} = sub {}; $self->{sth}->finish } if (
Subroutine __ANON__: called CC=1 SCAR=0.0
244 2600 ▶ $self->{sth} and ! try { ! $self->{sth}->FETCH('Active') }
Subroutine __ANON__: called CC=1 SCAR=0.0
245 2600 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sth"} and not &try(sub { not $self->{"sth"}->FETCH("Active"); } ) 1778 822 2600
Condition66%2/3
$self->{"sth"} and not &try(sub { not $self->{"sth"}->FETCH("Active"); } )
!ll&&!rl&&r
08221778
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
246 }
247  
248 139 1;
« DBIx/Class/Storage/DBI/ACCESS.pm ↑ Summary DBIx/Class/DB.pm »