EV/QueryExecutor.pm

filter: Statementstmt 85.3% 35 / 41 Branchbran 57.1% 8 / 14 Conditioncond 42.8% 3 / 7 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 90.0% 9 / 10 Podpod 50.0% 1 / 2 totaltotal 72.7% 56 / 77 CC 11 SCAR 12.0
CC
11
Cov
74%
_debug_query
6.0
new
4.8
execute
4.1
CRAP 3.3
« EV/Storage.pm ↑ Summary EV/Pool.pm »
1 package DBIO::MySQL::EV::QueryExecutor;
2 # ABSTRACT: Query execution wrapper for DBIO MySQL async storage
3  
4 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11 12 ▶sub new {
Subroutine new: called CC=4 SCAR=15.6
Pod undocumented
12 12 my ($class, %args) = @_;
13 12 ▶ croak 'pool is required' unless $args{pool};
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"pool"} 0 12 12
14  
15 12 ▶ my $debug = $args{debug} // $ENV{DBIO_TRACE} // 0;
Condition40%2/5
($args{"debug"} // $ENV{"DBIO_TRACE"}) // 0
l!l
012
$args{"debug"} // $ENV{"DBIO_TRACE"}
l!l&&r!l&&!r
0012
MC/DC0%0/2
($args{"debug"} // $ENV{"DBIO_TRACE"}) // 0
$args{"debug"} $ENV{"DBIO_TRACE"}
Truth table33%1/3
A // B
A= $args{"debug"}B= $ENV{"DBIO_TRACE"}
ABresult
1X1
011
000
16  
17 bless {
18 pool => $args{pool},
19 12 debug => $debug,
20 }, $class;
21 }
22  
23  
24 48 ▶sub execute {
Subroutine execute: called CC=4 SCAR=14.1
Pod documented
25 48 my ($self, $mdb, $sql, $bind) = @_;
26 48 ▶ $bind //= [];
Condition50%1/2
$bind //= []
l!l
480
MC/DC0%0/1
$bind //= []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
27  
28 48 ▶ $self->_debug_query($sql, $bind) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 48 48
29  
30 48 my $f = Future->new;
31  
32 # EV::MariaDB::query($sql, $cb) does NOT accept bind values — passes a
33 # bind arrayref to it is silently dropped. When the SQL has placeholders
34 # we MUST route through prepare + execute; otherwise the bind values
35 # never reach the server and the query fails with a SQL syntax error on
36 # the '?' placeholders (or, worse, runs with whatever the previous
37 # prepared statement's binds were — silent corruption). Empty binds
38 # stay on the cheap query() path.
39 48 ▶ my $on_result = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
40 48 my ($rows, $err) = @_;
41 48 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 2 46 48
42 2 $f->fail($err);
43 } else {
44 46 ▶ $f->done(ref $rows eq 'ARRAY' ? @$rows : $rows);
Branch: 1 branch
BranchTrueFalseTotal
ref $rows eq 'ARRAY' ? : 46 0 46
45 }
46 48 };
47  
48 48 ▶ if (@$bind) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$bind) { } 45 3 48
49 45 ▶ $mdb->prepare($sql, sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
50 45 my ($stmt, $perr) = @_;
51 45 ▶ if ($perr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($perr) 0 45 45
52 0 $f->fail($perr);
53 0 return;
54 }
55 45 $mdb->execute($stmt, $bind, $on_result);
56 45 });
57 }
58 else {
59 3 $mdb->query($sql, $on_result);
60 }
61  
62 # Mirror the sync storage's bind-clearing contract: the sync path wipes
63 # bind values from its pool once $dbh->execute(@bind) returns. The async
64 # bind arrayref outlives `execute` (it is referenced by the issuing CRUD
65 # frame and, on a real connection, by the in-flight query handle), so
66 # clear it when the future settles — never on the issuing side, which
67 # would corrupt the bind values of a still-pending query. This both frees
68 # the payload (no per-pool retention proportional to total binds issued)
69 # and prevents a re-issued query on the same handle from reading stale
70 # binds while a previous future is pending.
71 48 ▶ $f->on_ready(sub { @$bind = () });
Subroutine __ANON__: called CC=1 SCAR=0.0
72  
73 48 return $f;
74 }
75  
76 0 ▶sub _debug_query {
Subroutine _debug_query: not called CC=2 SCAR=17.9
77 0 my ($self, $sql, $bind) = @_;
78 0 ▶ my $bind_str = join(', ', map { defined $_ ? "'$_'" : 'NULL' } @$bind);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
79 0 warn "$sql: $bind_str\n";
80 }
81  
82 8 1;
83  
84 __END__
85  
86 =pod
87  
88 =encoding UTF-8
89  
90 =head1 NAME
91  
92 DBIO::MySQL::EV::QueryExecutor - Query execution wrapper for DBIO MySQL async storage
93  
94 =head1 VERSION
95  
96 version 0.900001
97  
98 =head1 DESCRIPTION
99  
100 Wraps a connection pool and provides a single C<execute($conn, $sql, $bind)>
101 interface for query execution. Handles bind attribute policy and returns
102 a L<Future> with the result rows.
103  
104 Separated from Storage to allow independent testing of query execution
105 logic from connection pooling and transaction orchestration.
106  
107 =head1 METHODS
108  
109 =head2 execute
110  
111 my $future = $executor->execute($mdb, $sql, $bind);
112  
113 Execute a query on a given L<EV::MariaDB> connection. Returns a L<Future>
114 that resolves with the result rows (array or arrayref).
115  
116 =head1 AUTHOR
117  
118 DBIO Authors
119  
120 =head1 COPYRIGHT AND LICENSE
121  
122 Copyright (C) 2026 DBIO Authors
123  
124 This is free software; you can redistribute it and/or modify it under
125 the same terms as the Perl 5 programming language system itself.
126  
127 =cut
« EV/Storage.pm ↑ Summary EV/Pool.pm »