blib/lib/DBIx/QuickORM/Iterator.pm

filter: Statementstmt 87.9% 102 / 116 Branchbran 74.3% 55 / 74 Conditioncond 60.9% 25 / 41 MC/DCmcdc 45.8% 11 / 24 Subroutinesub 89.4% 17 / 19 Podpod 81.8% 9 / 11 totaltotal 76.8% 219 / 285 CC 83 SCAR 23.9
CC
83
Cov
79%
init
34.5
init
34.5
DESTROY
7.1
CRAP 10.9
« blib/lib/DBIx/QuickORM/Row.pm ↑ Summary blib/lib/DBIx/QuickORM/LookupTable/Identity.pm »
1 package DBIx::QuickORM::Iterator;
2 259 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 259 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 259 our $VERSION = '0.000033';
6  
7 259 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 259 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 <query
11 <state
12 +index
13 259 };
14  
15 =pod
16  
17 =encoding UTF-8
18  
19 =head1 NAME
20  
21 DBIx::QuickORM::Iterator - Iteration over the rows a query fetched.
22  
23 =head1 DESCRIPTION
24  
25 An iterator walks every row a query returned, fetched at once, with its own
26 position. C<next>, C<first>, C<last>, C<all>, and C<only> hand rows out, and
27 C<reset> rewinds. Each row is built anew through C<build> every time it is
28 handed out (the handle makes row objects there).
29  
30 The rows live in a C<query> hashref that several iterators can share: a
31 handle's iterators all read its one query, each at its own position.
32  
33 A pending iterator (from a handle's C<async>, C<aside>, or C<forked>) starts
34 with a query still running elsewhere and no rows. C<ready> checks on it
35 without waiting. The reading methods wait for its rows first. Once the result
36 is there, the rows are collected at once, for every iterator sharing the
37 query, and the iterator works like any other.
38  
39 Only the process that started a pending query may collect it. A pending
40 query may also refuse to be read for now (C<aside> and C<forked> while a
41 transaction is open): every reading method, C<ready> included, croaks, and the
42 query stays pending. When the last
43 iterator over it is freed before it was collected, in that process, the query
44 is cancelled (a failed cancel warns).
45  
46 When collecting fails (the query failed, or was cancelled when its
47 transaction was rolled back) the error is thrown, and every reading method of
48 every iterator sharing the query throws it again, every time. C<reset> and the
49 attribute accessors neither wait nor throw.
50  
51 The C<state> says how many rows the query can rightly return. C<single> (a
52 foreign key followed to a primary or unique key) croaks when a second row is
53 asked for. C<multi> forbids C<only>, and undef allows anything.
54  
55 =head1 SYNOPSIS
56  
57 my $it = $orm->handle('users')->order_by('name')->iter;
58  
59 my @names;
60 while (my $user = $it->next) {
61 push @names => $user->field('name');
62 }
63  
64 $it->reset; # back to the first row, without running the query again
65 my @all = $it->all;
66 my $first = $it->first;
67  
68 # The one row, or undef. Croaks when there is more than one.
69 my $bob = $orm->handle('users')
70 ->where({email => 'bob@example.com'})
71 ->iter
72 ->only;
73  
74 my $later = $orm->handle('users')->async->iter;
75 do_something_else() until $later->ready;
76 my @rows = $later->all;
77  
78 =head1 ATTRIBUTES
79  
80 A constructor argument of the wrong shape croaks.
81  
82 =over 4
83  
84 =item \%query = $it->query
85  
86 Required: the internal state a handle's iterators share (its rows, pending
87 query, and error). It is not an interface.
88  
89 =item $state = $it->state
90  
91 C<single>, C<multi>, or undef (the default).
92  
93 =item \@rows = $it->rows
94  
95 =item $code = $it->build
96  
97 The query's C<rows> and C<build>.
98  
99 =back
100  
101 =cut
102  
103 # The query hashref, shared by every iterator a handle gives out:
104 # rows => \@rows, the row data in order. Required unless pending,
105 # undef until a pending query is collected.
106 # build => \&build, called with each row's data as it is handed out.
107 # Rows go out as they are without it. Set on collection.
108 # pending => {ready => \&ready, collect => \&collect, cancel => \&cancel},
109 # a query still running: ready checks without waiting, collect
110 # (given the reading call's caller(0) frame) waits and returns
111 # (\@rows, \&build), cancel gives up. At most
112 # one of collect and cancel is called, once, never in global
113 # destruction. An optional blocked returns why the query
114 # cannot be read now, or undef.
115 # frame => while a handle reader collects, that reader's caller(0)
116 # frame, passed to collect in place of the iterator's own
117 # pid => the process that may collect the pending query
118 # error => a failed collection's error, rethrown on every read
119 # iterators => how many live iterators hold it. The last one freed cancels
120 # a pending query.
121 361 ▶sub init {
Subroutine init: called CC=12 SCAR=35.4
Pod undocumented
122 0 ▶ my $self = shift;
Subroutine init: not called CC=12 SCAR=35.4
Pod undocumented
123 0 my $query = $self->{+QUERY};
124 0 ▶ croak "'query' must be a hashref" unless ref($query) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $query eq "HASH" 1 360 361
unless ref $query eq "HASH" 0 0 0
125  
126 0 ▶ if (my $pending = $query->{pending}) {
Branch: 4 branches
BranchTrueFalseTotal
if (my $pending = $query->{'pending'}) { } 40 320 360
elsif (not defined $query->{'error'}) { } 320 0 320
if (my $pending = $query->{'pending'}) { } 0 0 0
elsif (not defined $query->{'error'}) { } 0 0 0
127 0 ▶ $query->{pid} //= $$;
Condition33%2/6
$query->{"pid"} //= $$
l!l&&r!l&&!r
16240
$query->{"pid"} //= $$
l!l&&r!l&&!r
000
MC/DC0%0/2
$query->{"pid"} //= $$
$query->{"pid"} $$
MC/DC0%0/2
$query->{"pid"} //= $$
$query->{"pid"} $$
Truth table66%2/3
A //= B
A= $query->{"pid"}B= $$
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $query->{"pid"}B= $$
ABresult
1X1
011
000
128 0 ▶ croak "'pending' must be a hashref of ready, collect, and cancel code references" if ref($pending) ne 'HASH' || grep { ref($pending->{$_}) ne 'CODE' } qw/ready collect cancel/;
Branch: 2 branches
BranchTrueFalseTotal
if ref $pending ne "HASH" or grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel" 1 39 40
if ref $pending ne "HASH" or grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel" 0 0 0
Condition33%2/6
ref $pending ne "HASH" or grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel"
l!l&&r!l&&!r
0139
ref $pending ne "HASH" or grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel"
l!l&&r!l&&!r
000
MC/DC50%1/2
ref $pending ne "HASH" or grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel"
ref $pending ne "HASH" grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel"
MC/DC0%0/2
ref $pending ne "HASH" or grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel"
ref $pending ne "HASH" grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel"
Truth table66%2/3
A or B
A= ref $pending ne "HASH"B= grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel"
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= ref $pending ne "HASH"B= grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel"
ABresult
1X1
011
000
129 }
130 elsif (!defined $query->{error}) {
131 0 ▶ croak "'rows' must be an arrayref" unless ref($query->{rows}) eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $query->{"rows"} eq "ARRAY" 1 319 320
unless ref $query->{"rows"} eq "ARRAY" 0 0 0
132 }
133 0 ▶ croak "'build' must be a code reference" if defined($query->{build}) && ref($query->{build}) ne 'CODE';
Branch: 2 branches
BranchTrueFalseTotal
if defined $query->{"build"} and ref $query->{"build"} ne "CODE" 1 357 358
if defined $query->{"build"} and ref $query->{"build"} ne "CODE" 0 0 0
Condition50%3/6
defined $query->{"build"} and ref $query->{"build"} ne "CODE"
!ll&&!rl&&r
2291281
defined $query->{"build"} and ref $query->{"build"} ne "CODE"
!ll&&!rl&&r
000
MC/DC100%2/2
defined $query->{"build"} and ref $query->{"build"} ne "CODE"
defined $query->{"build"} ref $query->{"build"} ne "CODE"
MC/DC0%0/2
defined $query->{"build"} and ref $query->{"build"} ne "CODE"
defined $query->{"build"} ref $query->{"build"} ne "CODE"
Truth table100%3/3
A and B
A= defined $query->{"build"}B= ref $query->{"build"} ne "CODE"
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $query->{"build"}B= ref $query->{"build"} ne "CODE"
ABresult
0X0
100
111
134  
135 0 my $state = $self->{+STATE};
136 0 ▶ croak "'state' must be 'single', 'multi', or undef" if defined($state) && $state !~ m/^(?:single|multi)\z/;
Branch: 2 branches
BranchTrueFalseTotal
if defined $state and not $state =~ /^(?:single|multi)\z/ 1 356 357
if defined $state and not $state =~ /^(?:single|multi)\z/ 0 0 0
Condition50%3/6
defined $state and not $state =~ /^(?:single|multi)\z/
!ll&&!rl&&r
314421
defined $state and not $state =~ /^(?:single|multi)\z/
!ll&&!rl&&r
000
MC/DC100%2/2
defined $state and not $state =~ /^(?:single|multi)\z/
defined $state not $state =~ /^(?:single|multi)\z/
MC/DC0%0/2
defined $state and not $state =~ /^(?:single|multi)\z/
defined $state not $state =~ /^(?:single|multi)\z/
Truth table100%3/3
A and not(B)
A= defined $stateB= not $state =~ /^(?:single|multi)\z/
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= defined $stateB= not $state =~ /^(?:single|multi)\z/
ABresult
0X0
100
111
137  
138 # Counted only once init succeeds, so DESTROY knows this one holds the query.
139 0 $query->{iterators}++;
140 0 $self->{+INDEX} = 0;
141 }
142  
143 2 ▶sub rows { $_[0]->{+QUERY}->{rows} }
Subroutine rows: called CC=1 SCAR=0.0
Pod documented
144 0 ▶sub build { $_[0]->{+QUERY}->{build} }
Subroutine build: not called CC=1 SCAR=6.9
Pod documented
145  
146 =head1 PUBLIC METHODS
147  
148 =over 4
149  
150 =item $bool = $it->ready
151  
152 True when the rows are there.
153  
154 Always true for an iterator that is not pending. For a pending one it checks
155 without waiting and collects the rows once the result has arrived, so the
156 query's error is thrown here. After a failed collection it is true, and the
157 reading methods throw the error.
158  
159 =back
160  
161 =cut
162  
163 14 ▶sub ready {
Subroutine ready: called CC=3 SCAR=11.0
Pod documented
164 14 my $self = shift;
165 14 ▶ my $pending = $self->{+QUERY}->{pending} or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $pending = $self->{"query"}{"pending"} or return 1 7 7 14
166 7 $self->_check_pid;
167 6 ▶ return 0 unless $pending->{ready}->();
Branch: 1 branch
BranchTrueFalseTotal
unless $pending->{"ready"}->() 3 3 6
168 3 $self->_collect;
169 3 return 1;
170 }
171  
172 =pod
173  
174 =over 4
175  
176 =item $it->reset
177  
178 Rewinds the position to the first row. It does not wait for a pending
179 iterator's rows, and does not throw a failed collection's error.
180  
181 =back
182  
183 =cut
184  
185 5 ▶sub reset { $_[0]->{+INDEX} = 0; return }
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
186  
187 =pod
188  
189 =over 4
190  
191 =item $row = $it->next
192  
193 The next row, or undef when there are no more.
194  
195 =back
196  
197 =cut
198  
199 41 ▶sub next {
Subroutine next: called CC=2 SCAR=6.9
Pod documented
200 41 my $self = shift;
201 41 $self->_collect;
202 39 my $i = $self->{+INDEX};
203 39 ▶ return undef unless $i < @{$self->{+QUERY}->{rows}};
Branch: 1 branch
BranchTrueFalseTotal
unless $i < @{$self->{"query"}{"rows"};} 5 34 39
204 34 $self->{+INDEX} = $i + 1;
205 34 return $self->_row($i);
206 }
207  
208 =pod
209  
210 =over 4
211  
212 =item $row = $it->first
213  
214 =item $row = $it->last
215  
216 The first or last row, or undef when there are none. The position is left
217 alone.
218  
219 =back
220  
221 =cut
222  
223 132 ▶sub first {
Subroutine first: called CC=2 SCAR=6.9
Pod documented
224 132 my $self = shift;
225 132 $self->_collect;
226 130 ▶ return @{$self->{+QUERY}->{rows}} ? $self->_row(0) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'query'}{'rows'};} ? : 124 6 130
227 }
228  
229 8 ▶sub last {
Subroutine last: called CC=2 SCAR=6.9
Pod documented
230 8 my $self = shift;
231 8 $self->_collect;
232 6 my $rows = $self->{+QUERY}->{rows};
233 6 ▶ return @$rows ? $self->_row($#$rows) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$rows ? : 5 1 6
234 }
235  
236 =pod
237  
238 =over 4
239  
240 =item @rows = $it->all
241  
242 Every row. The position is left alone.
243  
244 =back
245  
246 =cut
247  
248 196 ▶sub all {
Subroutine all: called CC=5 SCAR=16.1
Pod documented
249 196 my $self = shift;
250 196 $self->_collect;
251  
252 # Checks single once, then builds every row without asking again.
253 189 my ($rows, $build) = @{$self->{+QUERY}}{qw/rows build/};
254 189 ▶ $self->_row(1) if @$rows > 1 && ($self->{+STATE} // '') eq 'single'; # croaks
Branch: 1 branch
BranchTrueFalseTotal
if @$rows > 1 and ($self->{"state"} // "") eq "single" 1 188 189
Condition100%5/5
@$rows > 1 and ($self->{"state"} // "") eq "single"
!ll&&!rl&&r
116721
$self->{"state"} // ""
l!l
568
MC/DC100%2/2
@$rows > 1 and ($self->{"state"} // "") eq "single"
@$rows > 1 $self->{"state"}
Truth table100%3/3
A and B
A= @$rows > 1B= $self->{"state"}
ABresult
0X0
100
111
255 188 ▶ return @$rows unless $build;
Branch: 1 branch
BranchTrueFalseTotal
unless $build 130 58 188
256 58 return map { $build->($_) } @$rows;
257 }
258  
259 =pod
260  
261 =over 4
262  
263 =item $row = $it->only
264  
265 The one row, or undef when there are none. Croaks when there are more, and
266 always under C<multi>.
267  
268 =back
269  
270 =cut
271  
272 89 ▶sub only {
Subroutine only: called CC=4 SCAR=13.9
Pod documented
273 89 my $self = shift;
274 89 $self->_collect;
275 87 ▶ croak "only() cannot be used on a multi-row iterator" if ($self->{+STATE} // '') eq 'multi';
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} // "") eq "multi" 2 85 87
Condition100%2/2
$self->{"state"} // ""
l!l
3354
MC/DC100%1/1
$self->{"state"} // ""
$self->{"state"}
Truth table100%2/2
A
A= $self->{"state"}
Aresult
11
00
276 85 ▶ croak "only() found more than one row" if @{$self->{+QUERY}->{rows}} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"query"}{"rows"};} > 1 1 84 85
277 84 return $self->first;
278 }
279  
280 =head1 PRIVATE METHODS
281  
282 =over 4
283  
284 =item $it->_collect
285  
286 Waits for a pending query's result and stores its rows and C<build> in the
287 shared query, for every iterator over it. Does nothing for one that is not
288 pending. An error thrown while collecting is stored there and thrown again on
289 every later call.
290  
291 =back
292  
293 =cut
294  
295 469 ▶sub _collect {
Subroutine _collect: called CC=6 SCAR=18.0
296 469 my $self = shift;
297 469 my $query = $self->{+QUERY};
298 469 ▶ die $query->{error} if defined $query->{error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $query->{"error"} 6 463 469
299 463 ▶ return unless $query->{pending};
Branch: 1 branch
BranchTrueFalseTotal
unless $query->{"pending"} 438 25 463
300 25 $self->_check_pid;
301 19 my $pending = delete $query->{pending};
302  
303 # The public call the rows arrive under: a handle reader's frame when one is reading, else the iterator reader
304 # that called this, one frame up.
305 19 ▶ my $frame = $query->{frame} // ($query->{debug} ? [(caller(1))[0 .. 3]] : undef);
Branch: 1 branch
BranchTrueFalseTotal
$query->{'debug'} ? : 0 19 19
Condition50%1/2
$query->{"frame"} // ($query->{"debug"} ? [(caller 1)[0..3]] : undef)
l!l
019
MC/DC0%0/1
$query->{"frame"} // ($query->{"debug"} ? [(caller 1)[0..3]] : undef)
$query->{"frame"}
Truth table50%1/2
A
A= $query->{"frame"}
Aresult
11
00
306 19 ▶ return if eval { @{$query}{qw/rows build/} = $pending->{collect}->($frame); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { @{$query;}{"rows", "build"} = $pending->{"collect"}->($frame); 1 } } 16 3 19
307 3 die($query->{error} = $@);
308 }
309  
310 =pod
311  
312 =over 4
313  
314 =item $it->_check_pid
315  
316 Croaks unless this process started the query, and with the pending query's
317 C<blocked> reason when it gives one, before anything is read.
318  
319 =back
320  
321 =cut
322  
323 32 ▶sub _check_pid {
Subroutine _check_pid: called CC=4 SCAR=13.9
324 32 my $self = shift;
325 32 my $pid = $self->{+QUERY}->{pid};
326 32 ▶ croak "This iterator's query was started in process $pid; it cannot be read in process $$" unless $pid == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $pid == $$ 0 32 32
327 32 ▶ my $blocked = $self->{+QUERY}->{pending}->{blocked} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $blocked = $self->{"query"}{"pending"}{"blocked"} or return 24 8 32
328 24 ▶ my $why = $blocked->() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $why = &$blocked() or return 7 17 24
329 7 croak $why;
330 }
331  
332 =pod
333  
334 =over 4
335  
336 =item $row = $it->_row($i)
337  
338 The row at the index, through C<build>. Croaks for any row but the first
339 under C<single>.
340  
341 =back
342  
343 =cut
344  
345 164 ▶sub _row {
Subroutine _row: called CC=5 SCAR=16.1
346 164 my $self = shift;
347 164 my ($i) = @_;
348 164 ▶ croak "This iterator is for a single row (a primary key or unique key lookup), but the query found more than one" if $i && ($self->{+STATE} // '') eq 'single';
Branch: 1 branch
BranchTrueFalseTotal
if $i and ($self->{"state"} // "") eq "single" 3 161 164
Condition100%5/5
$i and ($self->{"state"} // "") eq "single"
!ll&&!rl&&r
143183
$self->{"state"} // ""
l!l
318
MC/DC100%2/2
$i and ($self->{"state"} // "") eq "single"
$i $self->{"state"}
Truth table100%3/3
A and B
A= $iB= $self->{"state"}
ABresult
0X0
100
111
349  
350 161 my ($rows, $build) = @{$self->{+QUERY}}{qw/rows build/};
351 161 my $data = $rows->[$i];
352 161 ▶ return $data unless $build;
Branch: 1 branch
BranchTrueFalseTotal
unless $build 71 90 161
353 90 return $build->($data);
354 }
355  
356 361 ▶sub DESTROY {
Subroutine DESTROY: called CC=7 SCAR=19.6
357 361 my $self = shift;
358 361 ▶ return unless defined $self->{+INDEX}; # init failed: never counted
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"index"} 5 356 361
359 356 my $query = $self->{+QUERY};
360 356 ▶ return if --$query->{iterators}; # another iterator still reads it
Branch: 1 branch
BranchTrueFalseTotal
if --$query->{"iterators"} 32 324 356
361 324 ▶ return unless $query->{pending} && $query->{pid} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $query->{"pending"} and $query->{"pid"} == $$ 320 4 324
Condition66%2/3
$query->{"pending"} and $query->{"pid"} == $$
!ll&&!rl&&r
32004
MC/DC50%1/2
$query->{"pending"} and $query->{"pid"} == $$
$query->{"pending"} $query->{"pid"} == $$
Truth table66%2/3
A and B
A= $query->{"pending"}B= $query->{"pid"} == $$
ABresult
0X0
100
111
362 4 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 4 4
363  
364 4 my $pending = delete $query->{pending};
365 4 local $@;
366 4 ▶ eval { $pending->{cancel}->(); 1 } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $pending->{"cancel"}->(); 1 } } or warn $@ 4 0 4
367 }
368  
369 259 1;
370  
371 __END__
« blib/lib/DBIx/QuickORM/Row.pm ↑ Summary blib/lib/DBIx/QuickORM/LookupTable/Identity.pm »