blib/lib/DBIx/QuickORM/Iterator.pm

filter: Statementstmt 87.8% 101 / 115 Branchbran 75.0% 54 / 72 Conditioncond 61.5% 24 / 39 MC/DCmcdc 47.8% 11 / 23 Subroutinesub 89.4% 17 / 19 Podpod 81.8% 9 / 11 totaltotal 77.4% 216 / 279 CC 79 SCAR 23.9
CC
79
Cov
79%
init
34.5
init
34.5
DESTROY
7.1
CRAP 11.0
« blib/lib/DBIx/QuickORM.pm ↑ Summary blib/lib/DBIx/QuickORM/ORM.pm »
1 package DBIx::QuickORM::Iterator;
2 258 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 258 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 258 our $VERSION = '0.000031';
6  
7 258 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 258 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
10 <query
11 <state
12 +index
13 258 };
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 # waits and returns (\@rows, \&build), cancel gives up. At most
111 # one of collect and cancel is called, once, never in global
112 # destruction. An optional blocked returns why the query
113 # cannot be read now, or undef.
114 # pid => the process that may collect the pending query
115 # error => a failed collection's error, rethrown on every read
116 # iterators => how many live iterators hold it. The last one freed cancels
117 # a pending query.
118 272 ▶sub init {
Subroutine init: called CC=12 SCAR=35.4
Pod undocumented
119 0 ▶ my $self = shift;
Subroutine init: not called CC=12 SCAR=35.4
Pod undocumented
120 0 my $query = $self->{+QUERY};
121 0 ▶ croak "'query' must be a hashref" unless ref($query) eq 'HASH';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $query eq "HASH" 1 271 272
unless ref $query eq "HASH" 0 0 0
122  
123 0 ▶ if (my $pending = $query->{pending}) {
Branch: 4 branches
BranchTrueFalseTotal
if (my $pending = $query->{'pending'}) { } 40 231 271
elsif (not defined $query->{'error'}) { } 231 0 231
if (my $pending = $query->{'pending'}) { } 0 0 0
elsif (not defined $query->{'error'}) { } 0 0 0
124 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
125 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
126 }
127 elsif (!defined $query->{error}) {
128 0 ▶ croak "'rows' must be an arrayref" unless ref($query->{rows}) eq 'ARRAY';
Branch: 2 branches
BranchTrueFalseTotal
unless ref $query->{"rows"} eq "ARRAY" 1 230 231
unless ref $query->{"rows"} eq "ARRAY" 0 0 0
129 }
130 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 268 269
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
173951
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
131  
132 0 my $state = $self->{+STATE};
133 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 267 268
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
230371
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
134  
135 # Counted only once init succeeds, so DESTROY knows this one holds the query.
136 0 $query->{iterators}++;
137 0 $self->{+INDEX} = 0;
138 }
139  
140 2 ▶sub rows { $_[0]->{+QUERY}->{rows} }
Subroutine rows: called CC=1 SCAR=0.0
Pod documented
141 0 ▶sub build { $_[0]->{+QUERY}->{build} }
Subroutine build: not called CC=1 SCAR=6.9
Pod documented
142  
143 =head1 PUBLIC METHODS
144  
145 =over 4
146  
147 =item $bool = $it->ready
148  
149 True when the rows are there.
150  
151 Always true for an iterator that is not pending. For a pending one it checks
152 without waiting and collects the rows once the result has arrived, so the
153 query's error is thrown here. After a failed collection it is true, and the
154 reading methods throw the error.
155  
156 =back
157  
158 =cut
159  
160 14 ▶sub ready {
Subroutine ready: called CC=3 SCAR=11.0
Pod documented
161 14 my $self = shift;
162 14 ▶ my $pending = $self->{+QUERY}->{pending} or return 1;
Branch: 1 branch
BranchTrueFalseTotal
my $pending = $self->{"query"}{"pending"} or return 1 6 8 14
163 6 $self->_check_pid;
164 5 ▶ return 0 unless $pending->{ready}->();
Branch: 1 branch
BranchTrueFalseTotal
unless $pending->{"ready"}->() 2 3 5
165 3 $self->_collect;
166 3 return 1;
167 }
168  
169 =pod
170  
171 =over 4
172  
173 =item $it->reset
174  
175 Rewinds the position to the first row. It does not wait for a pending
176 iterator's rows, and does not throw a failed collection's error.
177  
178 =back
179  
180 =cut
181  
182 5 ▶sub reset { $_[0]->{+INDEX} = 0; return }
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
183  
184 =pod
185  
186 =over 4
187  
188 =item $row = $it->next
189  
190 The next row, or undef when there are no more.
191  
192 =back
193  
194 =cut
195  
196 41 ▶sub next {
Subroutine next: called CC=2 SCAR=6.9
Pod documented
197 41 my $self = shift;
198 41 $self->_collect;
199 39 my $i = $self->{+INDEX};
200 39 ▶ return undef unless $i < @{$self->{+QUERY}->{rows}};
Branch: 1 branch
BranchTrueFalseTotal
unless $i < @{$self->{"query"}{"rows"};} 5 34 39
201 34 $self->{+INDEX} = $i + 1;
202 34 return $self->_row($i);
203 }
204  
205 =pod
206  
207 =over 4
208  
209 =item $row = $it->first
210  
211 =item $row = $it->last
212  
213 The first or last row, or undef when there are none. The position is left
214 alone.
215  
216 =back
217  
218 =cut
219  
220 92 ▶sub first {
Subroutine first: called CC=2 SCAR=6.9
Pod documented
221 92 my $self = shift;
222 92 $self->_collect;
223 90 ▶ return @{$self->{+QUERY}->{rows}} ? $self->_row(0) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@{$self->{'query'}{'rows'};} ? : 84 6 90
224 }
225  
226 8 ▶sub last {
Subroutine last: called CC=2 SCAR=6.9
Pod documented
227 8 my $self = shift;
228 8 $self->_collect;
229 6 my $rows = $self->{+QUERY}->{rows};
230 6 ▶ return @$rows ? $self->_row($#$rows) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@$rows ? : 5 1 6
231 }
232  
233 =pod
234  
235 =over 4
236  
237 =item @rows = $it->all
238  
239 Every row. The position is left alone.
240  
241 =back
242  
243 =cut
244  
245 146 ▶sub all {
Subroutine all: called CC=5 SCAR=16.1
Pod documented
246 146 my $self = shift;
247 146 $self->_collect;
248  
249 # Checks single once, then builds every row without asking again.
250 139 my ($rows, $build) = @{$self->{+QUERY}}{qw/rows build/};
251 139 ▶ $self->_row(1) if @$rows > 1 && ($self->{+STATE} // '') eq 'single'; # croaks
Branch: 1 branch
BranchTrueFalseTotal
if @$rows > 1 and ($self->{"state"} // "") eq "single" 1 138 139
Condition100%5/5
@$rows > 1 and ($self->{"state"} // "") eq "single"
!ll&&!rl&&r
77611
$self->{"state"} // ""
l!l
557
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
252 138 ▶ return @$rows unless $build;
Branch: 1 branch
BranchTrueFalseTotal
unless $build 87 51 138
253 51 return map { $build->($_) } @$rows;
254 }
255  
256 =pod
257  
258 =over 4
259  
260 =item $row = $it->only
261  
262 The one row, or undef when there are none. Croaks when there are more, and
263 always under C<multi>.
264  
265 =back
266  
267 =cut
268  
269 61 ▶sub only {
Subroutine only: called CC=4 SCAR=13.9
Pod documented
270 61 my $self = shift;
271 61 $self->_collect;
272 59 ▶ 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 57 59
Condition100%2/2
$self->{"state"} // ""
l!l
2831
MC/DC100%1/1
$self->{"state"} // ""
$self->{"state"}
Truth table100%2/2
A
A= $self->{"state"}
Aresult
11
00
273 57 ▶ croak "only() found more than one row" if @{$self->{+QUERY}->{rows}} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"query"}{"rows"};} > 1 1 56 57
274 56 return $self->first;
275 }
276  
277 =head1 PRIVATE METHODS
278  
279 =over 4
280  
281 =item $it->_collect
282  
283 Waits for a pending query's result and stores its rows and C<build> in the
284 shared query, for every iterator over it. Does nothing for one that is not
285 pending. An error thrown while collecting is stored there and thrown again on
286 every later call.
287  
288 =back
289  
290 =cut
291  
292 351 ▶sub _collect {
Subroutine _collect: called CC=4 SCAR=13.9
293 351 my $self = shift;
294 351 my $query = $self->{+QUERY};
295 351 ▶ die $query->{error} if defined $query->{error};
Branch: 1 branch
BranchTrueFalseTotal
if defined $query->{"error"} 6 345 351
296 345 ▶ return unless $query->{pending};
Branch: 1 branch
BranchTrueFalseTotal
unless $query->{"pending"} 320 25 345
297 25 $self->_check_pid;
298 19 my $pending = delete $query->{pending};
299 19 ▶ return if eval { @{$query}{qw/rows build/} = $pending->{collect}->(); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { @{$query;}{"rows", "build"} = $pending->{"collect"}->(); 1 } } 16 3 19
300 3 die($query->{error} = $@);
301 }
302  
303 =pod
304  
305 =over 4
306  
307 =item $it->_check_pid
308  
309 Croaks unless this process started the query, and with the pending query's
310 C<blocked> reason when it gives one, before anything is read.
311  
312 =back
313  
314 =cut
315  
316 31 ▶sub _check_pid {
Subroutine _check_pid: called CC=4 SCAR=13.9
317 31 my $self = shift;
318 31 my $pid = $self->{+QUERY}->{pid};
319 31 ▶ 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 31 31
320 31 ▶ my $blocked = $self->{+QUERY}->{pending}->{blocked} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $blocked = $self->{"query"}{"pending"}{"blocked"} or return 23 8 31
321 23 ▶ my $why = $blocked->() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $why = &$blocked() or return 7 16 23
322 7 croak $why;
323 }
324  
325 =pod
326  
327 =over 4
328  
329 =item $row = $it->_row($i)
330  
331 The row at the index, through C<build>. Croaks for any row but the first
332 under C<single>.
333  
334 =back
335  
336 =cut
337  
338 124 ▶sub _row {
Subroutine _row: called CC=5 SCAR=16.1
339 124 my $self = shift;
340 124 my ($i) = @_;
341 124 ▶ 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 121 124
Condition100%5/5
$i and ($self->{"state"} // "") eq "single"
!ll&&!rl&&r
103183
$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
342  
343 121 my ($rows, $build) = @{$self->{+QUERY}}{qw/rows build/};
344 121 my $data = $rows->[$i];
345 121 ▶ return $data unless $build;
Branch: 1 branch
BranchTrueFalseTotal
unless $build 58 63 121
346 63 return $build->($data);
347 }
348  
349 272 ▶sub DESTROY {
Subroutine DESTROY: called CC=7 SCAR=19.6
350 272 my $self = shift;
351 272 ▶ return unless defined $self->{+INDEX}; # init failed: never counted
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"index"} 5 267 272
352 267 my $query = $self->{+QUERY};
353 267 ▶ return if --$query->{iterators}; # another iterator still reads it
Branch: 1 branch
BranchTrueFalseTotal
if --$query->{"iterators"} 33 234 267
354 234 ▶ return unless $query->{pending} && $query->{pid} == $$;
Branch: 1 branch
BranchTrueFalseTotal
unless $query->{"pending"} and $query->{"pid"} == $$ 230 4 234
Condition66%2/3
$query->{"pending"} and $query->{"pid"} == $$
!ll&&!rl&&r
23004
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
355 4 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 4 4
356  
357 4 my $pending = delete $query->{pending};
358 4 local $@;
359 4 ▶ eval { $pending->{cancel}->(); 1 } or warn $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $pending->{"cancel"}->(); 1 } } or warn $@ 4 0 4
360 }
361  
362 258 1;
363  
364 __END__
« blib/lib/DBIx/QuickORM.pm ↑ Summary blib/lib/DBIx/QuickORM/ORM.pm »