| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | if (my $pending = $query->{pending}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | $query->{pid} //= $$; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
$query->{"pid"} //= $$
$query->{"pid"} //= $$
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= $$
Truth table0%0/3
A //= B
A= $query->{"pid"}B= $$
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%2/6
ref $pending ne "HASH" or grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel"
ref $pending ne "HASH" or grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel"
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"
Truth table0%0/3
A or B
A= ref $pending ne "HASH"B= grep {ref $pending->{$_} ne "CODE";} "ready", "collect", "cancel"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | elsif (!defined $query->{error}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | croak "'rows' must be an arrayref" unless ref($query->{rows}) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | croak "'build' must be a code reference" if defined($query->{build}) && ref($query->{build}) ne 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
defined $query->{"build"} and ref $query->{"build"} ne "CODE"
defined $query->{"build"} and ref $query->{"build"} ne "CODE"
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"
Truth table0%0/3
A and B
A= defined $query->{"build"}B= ref $query->{"build"} ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
defined $state and not $state =~ /^(?:single|multi)\z/
defined $state and not $state =~ /^(?:single|multi)\z/
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/
Truth table0%0/3
A and not(B)
A= defined $stateB= not $state =~ /^(?:single|multi)\z/
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 7 | $self->_check_pid; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 6 | ▶ | return 0 unless $pending->{ready}->(); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%5/5
@$rows > 1 and ($self->{"state"} // "") eq "single"
$self->{"state"} // ""
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | 188 | ▶ | return @$rows unless $build; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%2/2
$self->{"state"} // ""
MC/DC100%1/1
$self->{"state"} // ""
$self->{"state"}
Truth table100%2/2
A
A= $self->{"state"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 85 | ▶ | croak "only() found more than one row" if @{$self->{+QUERY}->{rows}} > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | 463 | ▶ | return unless $query->{pending}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%1/2
$query->{"frame"} // ($query->{"debug"} ? [(caller 1)[0..3]] : undef)
MC/DC0%0/1
$query->{"frame"} // ($query->{"debug"} ? [(caller 1)[0..3]] : undef)
$query->{"frame"}
Truth table50%1/2
A
A= $query->{"frame"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | 19 | ▶ | return if eval { @{$query}{qw/rows build/} = $pending->{collect}->($frame); 1 }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 32 | ▶ | my $blocked = $self->{+QUERY}->{pending}->{blocked} or return; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 24 | ▶ | my $why = $blocked->() or return; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%5/5
$i and ($self->{"state"} // "") eq "single"
$self->{"state"} // ""
MC/DC100%2/2
$i and ($self->{"state"} // "") eq "single"
$i
$self->{"state"}
Truth table100%3/3
A and B
A= $iB= $self->{"state"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | 356 | my $query = $self->{+QUERY}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 356 | ▶ | return if --$query->{iterators}; # another iterator still reads it | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | 324 | ▶ | return unless $query->{pending} && $query->{pid} == $$; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$query->{"pending"} and $query->{"pid"} == $$
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"} == $$
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | 4 | ▶ | return if ${^GLOBAL_PHASE} eq 'DESTRUCT'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | 4 | my $pending = delete $query->{pending}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | 4 | local $@; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 4 | ▶ | eval { $pending->{cancel}->(); 1 } or warn $@; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | 259 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||