EV/QueryExecutor.pm

filter: Statementstmt 100% 226 / 226 Branchbran 83.6% 87 / 104 Conditioncond 73.5% 75 / 102 MC/DCmcdc 49.0% 27 / 55 Subroutinesub 100% 42 / 42 Podpod 100% 6 / 6 totaltotal 86.5% 463 / 535 CC 91 SCAR 17.5
CC
91
Cov
90%
_server_sqlstate
15.8
execute
10.0
__ANON__
8.0
CRAP 5.7
« EV/Storage.pm ↑ Summary EV/Future.pm »
1 package DBIO::MySQL::EV::QueryExecutor;
2 # ABSTRACT: Query execution wrapper for DBIO MySQL async storage
3  
4 58 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 58 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 58 our $VERSION = '0.900004';
8  
9 58 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 58 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 58 ▶use DBIO::Exception::Server ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 58 ▶use DBIO::MySQL::EV::Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 # Per connection (refaddr of the handle), the settling of one of its Futures
16 # that runs right now (_drain) -- the innermost one when they nest:
17 # { active => 1 } while it runs. What it calls is a then chain or a txn_do
18 # coderef, which holds the connection's transaction until it returns
19 # (_waits_for_callback, karr k53). Not EV::MariaDB's callback_depth: those
20 # callbacks only prepare, execute, close and hand the answer on (karr k51).
21 58 my %FRAME;
22  
23 # Per connection, how often it was forgotten (forget): the settling of a
24 # statement that was answered before its handle was forgotten leaves the counts
25 # alone, they belong to the next handle at that address.
26 my %GEN;
27  
28 # Per connection (refaddr of the handle), the number of statements dispatched
29 # on it whose Future has not settled: counted up once EV::MariaDB has taken the
30 # statement, down where the Future is settled. A count that is never taken
31 # down -- a handle that goes away without calling back, or a forked child,
32 # where the callbacks return at once -- reads as work in flight, which errs on
33 # the side of waiting.
34 58 my %IN_FLIGHT;
35  
36 # Per connection, how many of those are exclusive operations (prepare, execute,
37 # close_stmt): a statement with bind values or a CALL. While one is in flight
38 # EV::MariaDB refuses a query (karr k81).
39 58 my %EXCL;
40  
41 # Per connection, the statements that wait for every statement in flight on it
42 # to settle (execute_when_idle): coderefs, oldest first, each started once the
43 # count above is back at zero: { future => the Future the caller waits on,
44 # run => the coderef that starts it }. They hold the handle weakly; forget
45 # fails the futures.
46 58 my %AFTER;
47  
48 # The executor traces nothing: it is no storage. The statement trace is the
49 # core's (DBIO::Storage::Async/_debug_query, ADR 0025), called by
50 # DBIO::MySQL::EV::Storage once per statement.
51 167 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
52 167 my ($class, %args) = @_;
53 bless {
54 future_class => $args{future_class} // 'DBIO::MySQL::EV::Future',
55 error_masker => $args{error_masker},
56 167 ▶ }, $class;
Condition100%2/2
$args{"future_class"} // "DBIO::MySQL::EV::Future"
l!l
14225
MC/DC100%1/1
$args{"future_class"} // "DBIO::MySQL::EV::Future"
$args{"future_class"}
Truth table100%2/2
A
A= $args{"future_class"}
Aresult
11
00
57 }
58  
59  
60  
61 681 ▶sub execute {
Subroutine execute: called CC=10 SCAR=23.1
Pod documented
62 681 my ($self, $mdb, $sql, $bind) = @_;
63 681 ▶ $bind //= [];
Condition50%1/2
$bind //= []
l!l
6810
MC/DC0%0/1
$bind //= []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
64  
65 681 my $f = $self->{future_class}->new;
66 681 my $pid = $$;
67 681 my $key = Scalar::Util::refaddr($mdb);
68 681 ▶ my $exclusive = (@$bind || _is_call($sql)) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@$bind || _is_call($sql) ? : 287 394 681
Condition100%3/3
@$bind or _is_call($sql)
l!l&&r!l&&!r
2807394
MC/DC100%2/2
@$bind or _is_call($sql)
@$bind _is_call($sql)
Truth table100%3/3
A or B
A= @$bindB= _is_call($sql)
ABresult
1X1
011
000
69 681 my $settle = _settle_cb($f, $pid, $key, $exclusive);
70  
71 # EV::MariaDB::query($sql, $cb) does NOT accept bind values — passes a
72 # bind arrayref to it is silently dropped. When the SQL has placeholders
73 # we MUST route through prepare + execute; otherwise the bind values
74 # never reach the server and the query fails with a SQL syntax error on
75 # the '?' placeholders (or, worse, runs with whatever the previous
76 # prepared statement's binds were — silent corruption). Empty binds
77 # stay on the cheap query() path.
78 #
79 # A CALL without bind values goes the same way (karr k84). EV::MariaDB's
80 # query() calls back with the first result set and reads the rest of the
81 # answer -- the closing OK of the CALL, further result sets -- only after the
82 # callback returned, with nothing on the handle that says so; a statement
83 # with bind values that follows on the connection is then refused. Prepared
84 # and executed, the whole answer is read before the callback runs.
85 #
86 # An error text is masked (ADR 0075 of the core) by $mask, built here from
87 # the bound values and the statement; a dispatch EV::MariaDB refuses dies
88 # with its text masked as well.
89 681 my $mask = _error_mask($self->{error_masker}, $bind, $sql);
90 {
91 681 local $@;
92 681 my $dispatched = eval {
93 681 ▶ if (@$bind || _is_call($sql)) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$bind or _is_call($sql)) { } 287 394 681
Condition100%3/3
@$bind or _is_call($sql)
l!l&&r!l&&!r
2807394
MC/DC100%2/2
@$bind or _is_call($sql)
@$bind _is_call($sql)
Truth table100%3/3
A or B
A= @$bindB= _is_call($sql)
ABresult
1X1
011
000
94 287 $mdb->prepare($sql, _prepared_cb($mdb, $bind, $settle, $pid, $mask));
95 }
96 else {
97 394 $mdb->query($sql, _queried_cb($mdb, $settle, $pid, $mask));
98 }
99 670 1;
100 };
101 681 ▶ unless ($dispatched) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dispatched) 11 670 681
102 11 my $died = $@;
103 11 die _masked($mask, $died);
104 }
105 }
106  
107 # Dispatched -- a croak above never gets here. A handle that called back
108 # before it returned has settled the Future already and left the count at
109 # -1, so both sides drop the entry at zero.
110 670 ▶ delete $IN_FLIGHT{$key} unless ++$IN_FLIGHT{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless ++$IN_FLIGHT{$key} 1 669 670
111 670 ▶ delete $EXCL{$key} if $exclusive && !++$EXCL{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $exclusive and not ++$EXCL{$key} 0 670 670
Condition66%2/3
$exclusive and not ++$EXCL{$key}
!ll&&!rl&&r
3902800
MC/DC0%0/2
$exclusive and not ++$EXCL{$key}
$exclusive not ++$EXCL{$key}
Truth table66%2/3
A and not(B)
A= $exclusiveB= not ++$EXCL{$key}
ABresult
0X0
100
111
112  
113 670 return $f;
114 }
115  
116  
117 274 ▶sub reset_connection {
Subroutine reset_connection: called CC=3 SCAR=11.1
Pod documented
118 274 my ($self, $mdb) = @_;
119  
120 274 my $f = $self->{future_class}->new;
121 274 my $pid = $$;
122 274 my $key = Scalar::Util::refaddr($mdb);
123 274 my $settle = _settle_cb($f, $pid, $key, 1);
124  
125 274 my $mask = _error_mask($self->{error_masker}, [], undef);
126 274 $mdb->reset_connection(_reset_cb($settle, $mask));
127  
128 272 ▶ delete $IN_FLIGHT{$key} unless ++$IN_FLIGHT{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless ++$IN_FLIGHT{$key} 0 272 272
129 272 ▶ delete $EXCL{$key} if !++$EXCL{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless ++$EXCL{$key} 0 272 272
130  
131 272 return $f;
132 }
133  
134  
135 49 ▶sub execute_when_idle {
Subroutine execute_when_idle: called CC=2 SCAR=6.9
Pod documented
136 49 my ($self, $mdb, $sql, $bind, $timeout) = @_;
137  
138 49 my $key = Scalar::Util::refaddr($mdb);
139 49 ▶ return $self->execute($mdb, $sql, $bind) unless $IN_FLIGHT{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $IN_FLIGHT{$key} 14 35 49
140 35 return $self->_enqueue($mdb, $sql, $bind, $timeout);
141 }
142  
143  
144 283 ▶sub execute_after_exclusive {
Subroutine execute_after_exclusive: called CC=4 SCAR=14.0
Pod documented
145 283 my ($self, $mdb, $sql, $bind) = @_;
146 283 my $key = Scalar::Util::refaddr($mdb);
147 283 ▶ return $self->execute($mdb, $sql, $bind) unless $EXCL{$key} || ($AFTER{$key} && $IN_FLIGHT{$key});
Branch: 1 branch
BranchTrueFalseTotal
unless $EXCL{$key} or $AFTER{$key} and $IN_FLIGHT{$key} 281 2 283
Condition66%4/6
$EXCL{$key} or $AFTER{$key} and $IN_FLIGHT{$key}
l!l&&r!l&&!r
20281
$AFTER{$key} and $IN_FLIGHT{$key}
!ll&&!rl&&r
27920
MC/DC33%1/3
$EXCL{$key} or $AFTER{$key} and $IN_FLIGHT{$key}
$EXCL{$key} $AFTER{$key} $IN_FLIGHT{$key}
Truth table75%3/4
A or B and C
A= $EXCL{$key}B= $AFTER{$key}C= $IN_FLIGHT{$key}
ABCresult
1XX1
0111
00X0
0100
148 2 return $self->_enqueue($mdb, $sql, $bind);
149 }
150  
151 # Queue a statement behind the ones in flight on $mdb. With a $timeout (seconds,
152 # a positive number) its Future fails with a DBIO::Exception naming the wait
153 # when the statement has not been started by then (karr k81).
154 37 ▶sub _enqueue {
Subroutine _enqueue: called CC=3 SCAR=11.0
155 37 my ($self, $mdb, $sql, $bind, $timeout) = @_;
156 37 my $key = Scalar::Util::refaddr($mdb);
157  
158 37 my $outer = $self->{future_class}->new;
159 37 my $timer;
160 37 ▶ if ($timeout && $timeout > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($timeout and $timeout > 0) 12 25 37
Condition66%2/3
$timeout and $timeout > 0
!ll&&!rl&&r
25012
MC/DC50%1/2
$timeout and $timeout > 0
$timeout $timeout > 0
Truth table66%2/3
A and B
A= $timeoutB= $timeout > 0
ABresult
0X0
100
111
161 12 Scalar::Util::weaken(my $weak_outer = $outer);
162 2 ▶ $timer = EV::timer($timeout, 0, sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
163 2 undef $timer;
164 2 ▶ my $f = $weak_outer or return;
Branch: 1 branch
BranchTrueFalseTotal
my $f = $weak_outer or return 2 0 2
165 2 ▶ return if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 2 2
166 2 local $@;
167 2 eval { DBIO::Exception->throw(
168 "statement not sent: the statement in flight on the connection did not answer within ${timeout}s", 0) };
169 2 $f->fail($@);
170 12 });
171 12 ▶ $outer->on_ready(sub { undef $timer });
Subroutine __ANON__: called CC=1 SCAR=0.0
172 }
173 37 Scalar::Util::weaken(my $weak_mdb = $mdb);
174 37 ▶ push @{ $AFTER{$key} }, { future => $outer, run => sub {
Subroutine __ANON__: called CC=5 SCAR=16.2
175 25 undef $timer;
176 25 ▶ return if $outer->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $outer->is_ready 4 21 25
177 21 ▶ my $handle = $weak_mdb
Branch: 1 branch
BranchTrueFalseTotal
my $handle = $weak_mdb or return $outer->fail("connection is gone before the statement could be sent") 21 0 21
178 or return $outer->fail('connection is gone before the statement could be sent');
179 21 my $inner;
180 {
181 21 local $@;
182 21 ▶ unless (eval { $inner = $self->execute($handle, $sql, $bind); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $inner = $self->execute($handle, $sql, $bind); 1 } }) 4 17 21
183 4 ▶ return $outer->fail($@ || 'statement could not be sent');
Condition50%1/2
$@ || 'statement could not be sent'
l!l
40
MC/DC0%0/1
$@ || 'statement could not be sent'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
184 }
185 }
186 17 ▶ $inner->on_done(sub { $outer->done(@_) unless $outer->is_ready });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless $outer->is_ready 16 0 16
187 17 ▶ $inner->on_fail(sub { $outer->fail(@_) unless $outer->is_ready });
Subroutine __ANON__: called CC=2 SCAR=7.2
Branch: 1 branch
BranchTrueFalseTotal
unless $outer->is_ready 1 0 1
188 17 return;
189 37 } };
190 37 return $outer;
191 }
192  
193 # Start the statements that wait on $key, for as long as nothing is in flight
194 # on it. One that was dispatched is in flight itself, and the one behind it
195 # starts when that one settles. One that sent nothing -- its Future was
196 # cancelled while it waited, its handle is gone, EV::MariaDB refused it --
197 # leaves nothing in flight, and no settling would ever come back here for the
198 # ones behind it: so the next one is started right away, until one is in flight
199 # or none is left.
200 923 ▶sub _start_next {
Subroutine _start_next: called CC=5 SCAR=16.1
201 923 my ($key) = @_;
202 923 ▶ until ($IN_FLIGHT{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($IN_FLIGHT{$key}) 388 560 948
203 388 ▶ my $queue = $AFTER{$key} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $queue = $AFTER{$key} or return 25 363 388
204 25 my $next = shift @$queue;
205 25 ▶ delete $AFTER{$key} unless @$queue;
Branch: 1 branch
BranchTrueFalseTotal
unless @$queue 16 9 25
206 25 ▶ $next->{run}->() if $next;
Branch: 1 branch
BranchTrueFalseTotal
if $next 25 0 25
207 }
208 560 return;
209 }
210  
211  
212 80 ▶sub _waits_for_callback {
Subroutine _waits_for_callback: called CC=3 SCAR=11.0
213 80 my ($class, $mdb) = @_;
214 80 my $key = Scalar::Util::refaddr($mdb);
215 80 ▶ return $FRAME{$key} && !$IN_FLIGHT{$key} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$FRAME{$key} && !$IN_FLIGHT{$key} ? : 8 72 80
Condition100%3/3
$FRAME{$key} and not $IN_FLIGHT{$key}
!ll&&!rl&&r
47258
MC/DC100%2/2
$FRAME{$key} and not $IN_FLIGHT{$key}
$FRAME{$key} not $IN_FLIGHT{$key}
Truth table100%3/3
A and not(B)
A= $FRAME{$key}B= not $IN_FLIGHT{$key}
ABresult
0X0
100
111
216 }
217  
218  
219 280 ▶sub forget {
Subroutine forget: called CC=2 SCAR=6.9
Pod documented
220 280 my ($class, $mdb) = @_;
221 280 ▶ return unless defined $mdb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mdb 1 279 280
222 279 my $key = Scalar::Util::refaddr($mdb);
223 279 delete $FRAME{$key};
224 279 delete $IN_FLIGHT{$key};
225 279 delete $EXCL{$key};
226 279 $GEN{$key}++;
227 279 $class->_fail_waiting($mdb);
228 279 return;
229 }
230  
231  
232 552 ▶sub _fail_waiting {
Subroutine _fail_waiting: called CC=5 SCAR=16.1
233 552 my ($class, $mdb) = @_;
234 552 ▶ return unless defined $mdb;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mdb 0 552 552
235 552 my $waiting = delete $AFTER{ Scalar::Util::refaddr($mdb) };
236 552 ▶ for my $w (@{ $waiting || [] }) {
Condition100%2/2
$waiting || []
l!l
9543
MC/DC100%1/1
$waiting || []
$waiting
Truth table100%2/2
A
A= $waiting
Aresult
11
00
237 12 my $f = $w->{future};
238 12 ▶ next if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 2 10 12
239 10 local $@;
240 10 eval { DBIO::Exception->throw('connection torn down, statement not sent', 0) };
241 10 $f->fail($@);
242 }
243 552 return;
244 }
245  
246 # The callbacks below are named subs, not closures inside execute(): each
247 # closes over exactly what it is handed. The bind arrayref reaches the prepare
248 # callback, which hands it to execute -- the dispatch where its lifetime ends
249 # (ADR 0044) -- and no later callback holds it.
250 #
251 # Each returns at once outside $pid, the process that called execute(): in a
252 # forked child they run when the pool's fork guard finishes the child's copy of
253 # the handle (karr k46), and what they would do next -- close, settle, release,
254 # the caller's chain -- is the parent's.
255  
256 # Settles $f with an EV::MariaDB result ($rows, $err). Runs at most once: a
257 # close_stmt that croaked after calling back must not settle twice.
258 955 ▶sub _settle_cb {
Subroutine _settle_cb: called CC=1 SCAR=0.0
259 955 my ($f, $pid, $key, $exclusive) = @_;
260 955 my $settled;
261 927 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
262 927 my ($rows, $err) = @_;
263 927 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 927 927
264 927 ▶ return if $settled++;
Branch: 1 branch
BranchTrueFalseTotal
if $settled++ 1 926 927
265 # The Future settles from an idle watcher of the loop, not from inside this
266 # callback of the handle (karr k51): what its callbacks run -- a then, the
267 # coderef of a txn_do -- is outside every callback of the handle, so a
268 # statement it sends goes out at once and a blocking wait for it can end.
269 926 ▶ my $gen = $GEN{$key} // 0;
Condition100%2/2
$GEN{$key} // 0
l!l
10916
MC/DC100%1/1
$GEN{$key} // 0
$GEN{$key}
Truth table100%2/2
A
A= $GEN{$key}
Aresult
11
00
270 924 ▶ _defer($pid, $key, sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
271 # Before the Future is settled: what that runs sees the statement answered.
272 924 ▶ if (($GEN{$key} // 0) == $gen) {
Branch: 1 branch
BranchTrueFalseTotal
if (($GEN{$key} // 0) == $gen) 924 0 924
Condition100%2/2
$GEN{$key} // 0
l!l
10914
MC/DC100%1/1
$GEN{$key} // 0
$GEN{$key}
Truth table100%2/2
A
A= $GEN{$key}
Aresult
11
00
273 924 ▶ delete $IN_FLIGHT{$key} unless --$IN_FLIGHT{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless --$IN_FLIGHT{$key} 881 43 924
274 924 ▶ delete $EXCL{$key} if $exclusive && !--$EXCL{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $exclusive and not --$EXCL{$key} 512 412 924
Condition100%3/3
$exclusive and not --$EXCL{$key}
!ll&&!rl&&r
38032512
MC/DC100%2/2
$exclusive and not --$EXCL{$key}
$exclusive not --$EXCL{$key}
Truth table100%3/3
A and not(B)
A= $exclusiveB= not --$EXCL{$key}
ABresult
0X0
100
111
275 }
276 924 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 47 877 924
277 47 $f->fail($err);
278 } else {
279 877 ▶ $f->done(ref $rows eq 'ARRAY' ? @$rows : $rows);
Branch: 1 branch
BranchTrueFalseTotal
ref $rows eq 'ARRAY' ? : 403 474 877
280 }
281 # What waits for the connection to have nothing in flight starts now
282 # (execute_when_idle), after what the settling above ran.
283 923 _start_next($key);
284 926 });
285 926 return;
286 955 };
287 }
288  
289 # The settlings the idle watcher has still to run, oldest first:
290 # [ $pid, $key, $code ]. One watcher for all connections, so Futures settle in
291 # the order the handles answered.
292 58 my @DEFERRED;
293 58 my $IDLE;
294  
295 # Queue $code, to run from the loop's idle watcher. It runs as a callback of
296 # connection $key (the frame k53 asks about): a transaction's coderef, run by
297 # the settling of its BEGIN, holds its connection until it returns.
298 926 ▶sub _defer {
Subroutine _defer: called CC=2 SCAR=7.0
299 926 my ($pid, $key, $code) = @_;
300 926 push @DEFERRED, [ $pid, $key, $code ];
301 926 ▶ $IDLE ||= EV::idle_ns(\&_drain);
Condition66%2/3
$IDLE ||= &EV::idle_ns(\&DBIO::MySQL::EV::QueryExecutor::_drain)
l!l&&r!l&&!r
906200
MC/DC0%0/2
$IDLE ||= &EV::idle_ns(\&DBIO::MySQL::EV::QueryExecutor::_drain)
$IDLE &EV::idle_ns(\&DBIO::MySQL::EV::QueryExecutor::_drain)
Truth table66%2/3
A ||= B
A= $IDLEB= &EV::idle_ns(\&DBIO::MySQL::EV::QueryExecutor::_drain)
ABresult
1X1
011
000
302 926 $IDLE->start;
303 926 return;
304 }
305  
306 # The idle watcher. Entries taken one at a time and before they run, so a
307 # blocking wait inside one of them -- a nested EV::run, which runs this watcher
308 # again -- carries on with the ones behind it and none runs twice. An entry
309 # that dies is warned about, like EV::MariaDB does for its callbacks, and does
310 # not stop the ones behind it. Entries of another process (a forked child
311 # inherits the parent's) are dropped: the parent runs them.
312 888 ▶sub _drain {
Subroutine _drain: called CC=6 SCAR=18.2
313 888 ▶ while (my $entry = shift @DEFERRED) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $entry = shift @DEFERRED) 924 888 1812
314 924 my ($pid, $key, $code) = @$entry;
315 924 ▶ next if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 924 924
316 924 my $frame = {};
317 924 local $frame->{active} = 1;
318 924 local $FRAME{$key} = $frame;
319 924 local $@;
320 924 ▶ eval { $code->(); 1 } or warn "DBIO::MySQL::EV: exception in callback: $@";
Branch: 1 branch
BranchTrueFalseTotal
eval { do { &$code(); 1 } } or warn "DBIO::MySQL::EV: exception in callback: $@" 923 1 924
321 }
322 888 ▶ $IDLE->stop if $IDLE && !@DEFERRED;
Branch: 1 branch
BranchTrueFalseTotal
if $IDLE and not @DEFERRED 888 0 888
Condition33%1/3
$IDLE and not @DEFERRED
!ll&&!rl&&r
00888
MC/DC0%0/2
$IDLE and not @DEFERRED
$IDLE not @DEFERRED
Truth table33%1/3
A and not(B)
A= $IDLEB= not @DEFERRED
ABresult
0X0
100
111
323 888 return;
324 }
325  
326 287 ▶sub _prepared_cb {
Subroutine _prepared_cb: called CC=1 SCAR=0.1
327 287 my ($mdb, $bind, $settle, $pid, $mask) = @_;
328 280 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
329 280 my ($stmt, $perr) = @_;
330 280 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 280 280
331 # No statement id, nothing to close.
332 280 ▶ return $settle->(undef, _server_error($mdb, $perr, $mask) || 'prepare failed') if $perr || !defined $stmt;
Branch: 1 branch
BranchTrueFalseTotal
if $perr or not defined $stmt 10 270 280
Condition60%3/5
$perr or not defined $stmt
l!l&&r!l&&!r
100270
_server_error($mdb, $perr, $mask) || 'prepare failed'
l!l
100
MC/DC50%1/2
$perr or not defined $stmt
$perr not defined $stmt
MC/DC0%0/1
_server_error($mdb, $perr, $mask) || 'prepare failed'
_server_error($mdb, $perr, $mask)
Truth table66%2/3
A or not(B)
A= $perrB= not defined $stmt
ABresult
1X1
011
000
Truth table50%1/2
A
A= _server_error($mdb, $perr, $mask)
Aresult
11
00
333 270 local $@;
334 270 ▶ eval { $mdb->execute($stmt, $bind, _executed_cb($mdb, $stmt, $settle, $pid, $mask)); 1 }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $mdb->execute($stmt, $bind, _executed_cb($mdb, $stmt, $settle, $pid, $mask)); 1 } } or _close_and_settle($mdb, $stmt, $settle, undef, _masked($mask, $@ || "execute failed")) 266 4 270
Condition50%1/2
$@ || 'execute failed'
l!l
40
MC/DC0%0/1
$@ || 'execute failed'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
335 or _close_and_settle($mdb, $stmt, $settle, undef, _masked($mask, $@ || 'execute failed'));
336 270 return;
337 287 };
338 }
339  
340 270 ▶sub _executed_cb {
Subroutine _executed_cb: called CC=1 SCAR=0.0
341 270 my ($mdb, $stmt, $settle, $pid, $mask) = @_;
342 266 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.0
343 266 my ($rows, $err) = @_;
344 266 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 266 266
345 # Read before the close: it is the execute's error the handle holds now.
346 266 _close_and_settle($mdb, $stmt, $settle, $rows, _server_error($mdb, $err, $mask));
347 266 return;
348 270 };
349 }
350  
351 394 ▶sub _queried_cb {
Subroutine _queried_cb: called CC=1 SCAR=0.0
352 394 my ($mdb, $settle, $pid, $mask) = @_;
353 381 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
354 381 my ($rows, $err) = @_;
355 381 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 381 381
356 381 return $settle->($rows, _server_error($mdb, $err, $mask));
357 394 };
358 }
359  
360 274 ▶sub _reset_cb {
Subroutine _reset_cb: called CC=1 SCAR=0.0
361 274 my ($settle, $mask) = @_;
362 265 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
363 265 my ($ok, $err) = @_;
364 265 ▶ $settle->($err ? (undef, _masked($mask, $err)) : (1, undef));
Branch: 1 branch
BranchTrueFalseTotal
$err ? : 2 263 265
365 274 };
366 }
367  
368 # The mask of one statement: a coderef text -> text with the values out, or
369 # undef without a masker. It holds a copy of the bound values that can be found
370 # in a text (defined, not a reference, four characters or more, as strings) and
371 # the statement -- never the caller's bind arrayref (ADR 0044).
372 955 ▶sub _error_mask {
Subroutine _error_mask: called CC=5 SCAR=16.2
373 955 my ($masker, $bind, $sql) = @_;
374 955 ▶ return unless $masker;
Branch: 1 branch
BranchTrueFalseTotal
unless $masker 88 867 955
375 867 ▶ my @values = map { "$_" } grep { defined && !ref && length >= 4 } @{ $bind || [] };
Condition75%6/8
defined $_ && !ref($_) && length $_ >= 4
!ll&&!rl&&r
1210407
defined $_ and not ref $_
!ll&&!rl&&r
10617
$bind || []
l!l
8670
MC/DC66%2/3
defined $_ && !ref($_) && length $_ >= 4
defined $_ not ref $_ length $_ >= 4
MC/DC0%0/1
$bind || []
$bind
Truth table75%3/4
A and not(B) && C
A= defined $_B= not ref $_C= length $_ >= 4
ABCresult
0XX0
10X0
1100
1111
Truth table50%1/2
A
A= $bind
Aresult
11
00
376 867 ▶ return sub { $masker->($_[0], \@values, $sql) };
Subroutine __ANON__: called CC=1 SCAR=0.0
377 }
378  
379 # An error text through the mask; an object, undef and an empty text as they are.
380 65 ▶sub _masked {
Subroutine _masked: called CC=5 SCAR=16.2
381 65 my ($mask, $err) = @_;
382 65 ▶ return $err unless $mask && defined $err && !ref $err && length $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $mask and defined $err and not ref $err and length $err 14 51 65
Condition77%7/9
$mask and defined $err and not ref $err and length $err
!ll&&!rl&&r
14051
$mask and defined $err and not ref $err
!ll&&!rl&&r
13151
$mask and defined $err
!ll&&!rl&&r
13052
MC/DC50%2/4
$mask and defined $err and not ref $err and length $err
$mask defined $err not ref $err length $err
Truth table60%3/5
A and B and not(C) and D
A= $maskB= defined $errC= not ref $errD= length $err
ABCDresult
0XXX0
10XX0
110X0
11100
11111
383 51 return $mask->($err);
384 }
385  
386  
387 665 ▶sub _server_error {
Subroutine _server_error: called CC=5 SCAR=16.1
388 665 my ($mdb, $err, $mask) = @_;
389 665 ▶ return $err if ref $err or not defined $err or not length $err;
Branch: 1 branch
BranchTrueFalseTotal
if ref $err or not defined $err or not length $err 617 48 665
Condition83%5/6
ref $err or not defined $err or not length $err
l!l&&r!l&&!r
617048
ref $err or not defined $err
l!l&&r!l&&!r
161648
MC/DC66%2/3
ref $err or not defined $err or not length $err
ref $err not defined $err not length $err
Truth table75%3/4
A or not(B) or not(C)
A= ref $errB= not defined $errC= not length $err
ABCresult
1XX1
01X1
0011
0000
390 48 ▶ my $state = _server_sqlstate($mdb, $err)
Branch: 1 branch
BranchTrueFalseTotal
my $state = _server_sqlstate($mdb, $err) or return _masked($mask, $err) 10 38 48
391 or return _masked($mask, $err);
392 10 return DBIO::Exception::Server->new(message => _masked($mask, $err), sqlstate => $state);
393 }
394  
395 # The SQLSTATE of the handle when $err is a refusal by the server, else undef.
396 48 ▶sub _server_sqlstate {
Subroutine _server_sqlstate: called CC=14 SCAR=27.6
397 48 my ($mdb, $err) = @_;
398 48 ▶ return unless Scalar::Util::blessed($mdb)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($mdb) and $mdb->can("sqlstate") and $mdb->can("error_number") and $mdb->can("error_message") 34 14 48
Condition66%6/9
builtin::blessed($mdb) and $mdb->can("sqlstate") and $mdb->can("error_number") and $mdb->can("error_message")
!ll&&!rl&&r
34014
builtin::blessed($mdb) and $mdb->can("sqlstate") and $mdb->can("error_number")
!ll&&!rl&&r
34014
builtin::blessed($mdb) and $mdb->can("sqlstate")
!ll&&!rl&&r
03414
MC/DC25%1/4
builtin::blessed($mdb) and $mdb->can("sqlstate") and $mdb->can("error_number") and $mdb->can("error_message")
builtin::blessed($mdb) $mdb->can("sqlstate") $mdb->can("error_number") $mdb->can("error_message")
Truth table40%2/5
A and B and C and D
A= builtin::blessed($mdb)B= $mdb->can("sqlstate")C= $mdb->can("error_number")D= $mdb->can("error_message")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
399 && $mdb->can('sqlstate') && $mdb->can('error_number') && $mdb->can('error_message');
400 14 my ($state, $number, $message) = eval { ($mdb->sqlstate, $mdb->error_number, $mdb->error_message) };
401 14 ▶ return unless defined $state && $state =~ /\A[0-9A-Z]{5}\z/ && $state ne '00000';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state and $state =~ /\A[0-9A-Z]{5}\z/ and $state ne "00000" 1 13 14
Condition50%3/6
defined $state and $state =~ /\A[0-9A-Z]{5}\z/ and $state ne "00000"
!ll&&!rl&&r
0113
defined $state and $state =~ /\A[0-9A-Z]{5}\z/
!ll&&!rl&&r
0014
MC/DC33%1/3
defined $state and $state =~ /\A[0-9A-Z]{5}\z/ and $state ne "00000"
defined $state $state =~ /\A[0-9A-Z]{5}\z/ $state ne "00000"
Truth table50%2/4
A and B and C
A= defined $stateB= $state =~ /\A[0-9A-Z]{5}\z/C= $state ne "00000"
ABCresult
0XX0
10X0
1100
1111
402 13 ▶ return unless defined $number && ($number >= 1000 && $number < 2000 || $number >= 3000);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number and $number >= 1000 && $number < 2000 || $number >= 3000 2 11 13
Condition77%7/9
defined $number and $number >= 1000 && $number < 2000 || $number >= 3000
!ll&&!rl&&r
0211
$number >= 1000 and $number < 2000 or $number >= 3000
l!l&&r!l&&!r
1012
$number >= 1000 and $number < 2000
!ll&&!rl&&r
0310
MC/DC50%2/4
defined $number and $number >= 1000 && $number < 2000 || $number >= 3000
defined $number $number >= 1000 $number < 2000 $number >= 3000
Truth table50%3/6
A and B and C or D
A= defined $numberB= $number >= 1000C= $number < 2000D= $number >= 3000
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
403 11 ▶ return unless defined $message && $message eq $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and $message eq $err 1 10 11
Condition66%2/3
defined $message and $message eq $err
!ll&&!rl&&r
0110
MC/DC50%1/2
defined $message and $message eq $err
defined $message $message eq $err
Truth table66%2/3
A and B
A= defined $messageB= $message eq $err
ABresult
0X0
100
111
404 10 return $state;
405 }
406  
407 # Close the statement, then settle with the execute's outcome. The close's own
408 # result does not change that outcome; a close that croaks settles at once.
409 270 ▶sub _close_and_settle {
Subroutine _close_and_settle: called CC=2 SCAR=6.9
410 270 my ($mdb, $stmt, $settle, $rows, $err) = @_;
411 270 my $closing;
412 {
413 270 local $@;
414 270 ▶ $closing = eval { $mdb->close_stmt($stmt, sub { $settle->($rows, $err) }); 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
415 }
416 270 ▶ $settle->($rows, $err) unless $closing;
Branch: 1 branch
BranchTrueFalseTotal
unless $closing 4 266 270
417 270 return;
418 }
419  
420 # True for a statement that begins with CALL, behind leading whitespace and
421 # comments (-- and # to the end of the line, /* ... */), in any case.
422 802 ▶sub _is_call {
Subroutine _is_call: called CC=3 SCAR=11.1
423 802 my ($sql) = @_;
424 802 ▶ return defined $sql && $sql =~ m{\A(?:\s+|--[ \t][^\n]*(?:\n|\z)|\#[^\n]*(?:\n|\z)|/\*.*?\*/)*CALL\b}is ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $sql && $sql =~ m[\A(?:\s+|--[ \t][^\n]*(?:\n|\z)|\#[^\n]*(?:\n|\z)|/\*.*?\*/)*CALL\b]is ? : 14 788 802
Condition66%2/3
defined $sql and $sql =~ m[\A(?:\s+|--[ \t][^\n]*(?:\n|\z)|\#[^\n]*(?:\n|\z)|/\*.*?\*/)*CALL\b]is
!ll&&!rl&&r
078814
MC/DC50%1/2
defined $sql and $sql =~ m[\A(?:\s+|--[ \t][^\n]*(?:\n|\z)|\#[^\n]*(?:\n|\z)|/\*.*?\*/)*CALL\b]is
defined $sql $sql =~ m[\A(?:\s+|--[ \t][^\n]*(?:\n|\z)|\#[^\n]*(?:\n|\z)|/\*.*?\*/)*CALL\b]is
Truth table66%2/3
A and B
A= defined $sqlB= $sql =~ m[\A(?:\s+|--[ \t][^\n]*(?:\n|\z)|\#[^\n]*(?:\n|\z)|/\*.*?\*/)*CALL\b]is
ABresult
0X0
100
111
425 }
426  
427 58 1;
428  
429 __END__
« EV/Storage.pm ↑ Summary EV/Future.pm »