EV/Storage.pm

filter: Statementstmt 95.9% 164 / 171 Branchbran 67.2% 39 / 58 Conditioncond 49.0% 25 / 51 MC/DCmcdc 14.8% 4 / 27 Subroutinesub 97.6% 42 / 43 Podpod 100% 7 / 7 totaltotal 78.7% 281 / 357 CC 50 SCAR 16.9
CC
50
Cov
81%
_auto_increment_column
14.8
connect_info
9.5
_insert_returned_columns
8.2
CRAP 5.4
« EV/TestHarness.pm ↑ Summary EV/QueryExecutor.pm »
1 package DBIO::MySQL::EV::Storage;
2 # ABSTRACT: Async MySQL/MariaDB storage driver using EV::MariaDB
3  
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use base 'DBIO::Storage::Async';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 7 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use DBIO::MySQL::SQLMaker;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use DBIO::MySQL::EV::ConnectInfo ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use DBIO::MySQL::EV::Pool;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use DBIO::MySQL::EV::QueryExecutor;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use DBIO::MySQL::EV::TransactionContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18  
19 # --- SQLMaker seams -----------------------------------------------------------
20 # The inherited DBIO::Storage::Async::sql_maker builds the maker from these.
21  
22  
23 15 ▶sub sql_maker_class { 'DBIO::MySQL::SQLMaker' }
Subroutine sql_maker_class: called CC=1 SCAR=0.0
Pod documented
24  
25  
26 14 ▶sub _sql_maker_args {
Subroutine _sql_maker_args: called CC=1 SCAR=0.0
27 return (
28 14 quote_char => '`',
29 name_sep => '.',
30 );
31 }
32  
33  
34 1 ▶sub _post_insert_sql { '' }
Subroutine _post_insert_sql: called CC=1 SCAR=0.0
35  
36  
37 2 ▶sub future_class { 'Future' }
Subroutine future_class: called CC=1 SCAR=0.0
Pod documented
38  
39  
40 1 ▶sub transport_capabilities { qw(on_connect_replay) }
Subroutine transport_capabilities: called CC=1 SCAR=0.0
Pod documented
41  
42  
43 4 ▶sub connect_info {
Subroutine connect_info: called CC=8 SCAR=22.5
Pod documented
44 4 my ($self, $info) = @_;
45 4 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 4 0 4
46 # Embedded path: the sync storage feeds its own DBI-form connect_info
47 # (['dbi:MariaDB:...', $user, $pass, \%attrs]). Translate it into the async
48 # [ \%conninfo, \%opts ] shape before the normal broker/normalize logic, so
49 # EV::MariaDB gets named params instead of a DSN string it cannot parse.
50 4 ▶ if (ref $info eq 'ARRAY' && defined $info->[0] && !ref $info->[0] && $info->[0] =~ /^dbi:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info eq "ARRAY" and defined $info->[0] and not ref $info->[0] and $info->[0] =~ /^dbi:/i) 0 4 4
Condition33%3/9
ref $info eq "ARRAY" and defined $info->[0] and not ref $info->[0] and $info->[0] =~ /^dbi:/i
!ll&&!rl&&r
400
ref $info eq "ARRAY" and defined $info->[0] and not ref $info->[0]
!ll&&!rl&&r
040
ref $info eq "ARRAY" and defined $info->[0]
!ll&&!rl&&r
004
MC/DC0%0/4
ref $info eq "ARRAY" and defined $info->[0] and not ref $info->[0] and $info->[0] =~ /^dbi:/i
ref $info eq "ARRAY" defined $info->[0] not ref $info->[0] $info->[0] =~ /^dbi:/i
Truth table20%1/5
A and B and not(C) and D
A= ref $info eq "ARRAY"B= defined $info->[0]C= not ref $info->[0]D= $info->[0] =~ /^dbi:/i
ABCDresult
0XXX0
10XX0
110X0
11100
11111
51 0 $info = DBIO::MySQL::EV::ConnectInfo::dbi_to_conninfo($info);
52 }
53 4 ▶ $self->disconnect if $self->{pool};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pool"} 0 4 4
54 4 $self->{connect_info} = $info;
55  
56 4 ▶ if ($self->_is_access_broker_connect_info($info)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_access_broker_connect_info($info)) { } 3 1 4
57 3 $self->_setup_access_broker($info->[0]);
58 3 my ($conninfo, $pool_size, $opts) = $self->_normalize_async_connect_info(
59 $self->_current_async_connect_info($self->access_broker_mode)
60 );
61 3 $self->{_conninfo} = $conninfo;
62 3 $self->{_pool_size} = $pool_size;
63 3 $self->{_opts} = $opts;
64 }
65 else {
66 1 $self->_clear_access_broker;
67 1 my ($conninfo, $pool_size, $opts) = $self->_normalize_async_connect_info($info);
68 1 $self->{_conninfo} = $conninfo;
69 1 $self->{_pool_size} = $pool_size;
70 1 $self->{_opts} = $opts;
71 }
72 }
73 4 return $self->{connect_info};
74 }
75  
76  
77 3 ▶sub _async_broker_conninfo {
Subroutine _async_broker_conninfo: called CC=1 SCAR=0.0
78 3 my ($self, $mode) = @_;
79  
80 3 my ($conninfo) = $self->_normalize_async_connect_info(
81 $self->_current_async_connect_info($mode)
82 );
83 3 return $conninfo;
84 }
85  
86  
87 7 ▶sub _normalize_async_connect_info {
Subroutine _normalize_async_connect_info: called CC=6 SCAR=18.1
88 7 my ($self, $info) = @_;
89  
90 7 my $conninfo = $info->[0];
91 7 ▶ $conninfo = ref($conninfo) eq 'HASH' ? { %$conninfo } : $conninfo;
Branch: 1 branch
BranchTrueFalseTotal
ref $conninfo eq 'HASH' ? : 7 0 7
92  
93 7 my $opts = $info->[1];
94 7 ▶ $opts = ref($opts) eq 'HASH' ? { %$opts } : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $opts eq 'HASH' ? : 6 1 7
95  
96 7 my $pool_size = 5;
97 7 ▶ if (ref($conninfo) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conninfo eq "HASH") 7 0 7
98 7 ▶ $pool_size = delete $conninfo->{pool_size} // 5;
Condition50%1/2
delete $conninfo->{"pool_size"} // 5
l!l
07
MC/DC0%0/1
delete $conninfo->{"pool_size"} // 5
delete $conninfo->{"pool_size"}
Truth table50%1/2
A
A= delete $conninfo->{"pool_size"}
Aresult
11
00
99 # MySQL uses 'database', not 'dbname'
100 7 ▶ $conninfo->{database} = delete $conninfo->{dbname} if exists $conninfo->{dbname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $conninfo->{"dbname"} 1 6 7
101 }
102  
103 7 return ($conninfo, $pool_size, $opts);
104 }
105  
106  
107 1 ▶sub _conninfo_hash {
Subroutine _conninfo_hash: called CC=4 SCAR=14.6
108 1 my ($self, $ci) = @_;
109 1 ▶ my $conninfo = defined($ci) ? $ci : $self->{_conninfo};
Branch: 1 branch
BranchTrueFalseTotal
defined $ci ? : 0 1 1
110 1 ▶ return unless defined $conninfo;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conninfo 0 1 1
111 1 ▶ return $conninfo if ref $conninfo ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $conninfo ne "HASH" 0 1 1
112 1 return $conninfo;
113 }
114  
115  
116 23 ▶sub pool {
Subroutine pool: called CC=3 SCAR=11.8
Pod documented
117 23 my $self = shift;
118 23 ▶ $self->{pool} ||= do {
Condition66%2/3
$self->{"pool"} ||= do {
l!l&&r!l&&!r
2210
MC/DC0%0/2
$self->{"pool"} ||= do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::MySQL::EV pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; "DBIO::MySQL::EV::Pool"->new(%args) }
$self->{"pool"} do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::MySQL::EV pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; "DBIO::MySQL::EV::Pool"->new(%args) }
Truth table66%2/3
A ||= B
A= $self->{"pool"}B= do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::MySQL::EV pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; "DBIO::MySQL::EV::Pool"->new(%args) }
ABresult
1X1
011
000
119 my %args = (
120 storage => $self,
121 size => $self->{_pool_size},
122 0 ▶ on_error => sub { warn "DBIO::MySQL::EV pool error: $_[0]\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 1 );
124  
125 1 ▶ if (my $provider = $self->_conninfo_provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $provider = $self->_conninfo_provider) { } 0 1 1
126 0 $args{conninfo_provider} = $provider;
127 }
128 else {
129 1 $args{conninfo} = $self->{_conninfo};
130 }
131  
132 1 DBIO::MySQL::EV::Pool->new(%args);
133 };
134 }
135  
136 # Lazily-built query executor bound to the pool. The executor is the MySQL wire
137 # seam: it hides EV::MariaDB's query-vs-prepare/execute split (query() takes no
138 # binds) and the async bind-clearing contract (karr #11). Held on the storage so
139 # a caller that pre-wires { pool, executor } (the offline facade tests) keeps its
140 # own instance.
141 14 ▶sub _executor {
Subroutine _executor: called CC=2 SCAR=8.1
142 14 my $self = shift;
143 return $self->{executor} ||= DBIO::MySQL::EV::QueryExecutor->new(
144 pool => $self->{pool},
145 debug => $self->{debug},
146 14 ▶ );
Condition33%1/3
$self->{"executor"} ||= "DBIO::MySQL::EV::QueryExecutor"->new("pool", $self->{"pool"}, "debug", $self->{"debug"})
l!l&&r!l&&!r
1400
MC/DC0%0/2
$self->{"executor"} ||= "DBIO::MySQL::EV::QueryExecutor"->new("pool", $self->{"pool"}, "debug", $self->{"debug"})
$self->{"executor"} "DBIO::MySQL::EV::QueryExecutor"->new("pool", $self->{"pool"}, "debug", $self->{"debug"})
Truth table33%1/3
A ||= B
A= $self->{"executor"}B= "DBIO::MySQL::EV::QueryExecutor"->new("pool", $self->{"pool"}, "debug", $self->{"debug"})
ABresult
1X1
011
000
147 }
148  
149 # --- Transaction context seams ------------------------------------------------
150  
151  
152 1 ▶sub _txn_context_class { 'DBIO::MySQL::EV::TransactionContext' }
Subroutine _txn_context_class: called CC=1 SCAR=0.0
153  
154  
155 1 ▶sub _txn_conn_accessor { 'mdb' }
Subroutine _txn_conn_accessor: called CC=1 SCAR=0.0
156  
157 # --- SQL shaping seam ---------------------------------------------------------
158  
159  
160 21 ▶sub _transform_sql { $_[1] }
Subroutine _transform_sql: called CC=1 SCAR=0.0
161  
162 # --- Async query execution (transport seams) ----------------------------------
163  
164  
165 8 ▶sub _query_async {
Subroutine _query_async: called CC=2 SCAR=6.9
166 8 my ($self, $sql, $bind) = @_;
167 8 ▶ $bind //= [];
Condition50%1/2
$bind //= []
l!l
80
MC/DC0%0/1
$bind //= []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
168 8 $sql = $self->_transform_sql($sql);
169  
170 8 ▶ return $self->pool->acquire->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
171 8 my $conn = shift;
172 8 my $f = $self->_executor->execute($conn, $sql, $bind);
173 8 ▶ $f->on_ready(sub { $self->pool->release($conn) });
Subroutine __ANON__: called CC=1 SCAR=0.0
174 8 return $f;
175 8 });
176 }
177  
178  
179 6 ▶sub _query_async_pinned {
Subroutine _query_async_pinned: called CC=2 SCAR=7.0
180 6 my ($self, $conn, $sql, $bind) = @_;
181 6 ▶ $bind //= [];
Condition50%1/2
$bind //= []
l!l
60
MC/DC0%0/1
$bind //= []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
182 6 $sql = $self->_transform_sql($sql);
183 6 return $self->_executor->execute($conn, $sql, $bind);
184 }
185  
186 # --- INSERT: LAST_INSERT_ID instead of RETURNING (MySQL value-add) ------------
187  
188  
189 3 ▶sub insert_async {
Subroutine insert_async: called CC=1 SCAR=0.0
Pod documented
190 3 my ($self, @args) = @_;
191  
192 # INSERT + LAST_INSERT_ID() must ride the SAME session, so pin the whole
193 # insert on one acquired connection and release it once the Future settles.
194 3 ▶ return $self->pool->acquire->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
195 3 my $conn = shift;
196 3 my $f = $self->_run_crud_pinned('insert', $conn, @args);
197 3 ▶ $f->on_ready(sub { $self->pool->release($conn) });
Subroutine __ANON__: called CC=1 SCAR=0.0
198 3 return $f;
199 3 });
200 }
201  
202  
203 3 ▶sub _run_crud_pinned {
Subroutine _run_crud_pinned: called CC=4 SCAR=14.3
204 3 my ($self, $op, $conn, @args) = @_;
205 3 ▶ return $self->SUPER::_run_crud_pinned($op, $conn, @args) unless $op eq 'insert';
Branch: 1 branch
BranchTrueFalseTotal
unless $op eq "insert" 0 3 3
206  
207 # The CRUD contract is insert_async($source, \%rowdata) (ADR 0031 §3).
208 # SQLMaker wants a plain table name, so unwrap a blessed source to its
209 # ->name; a bare string passes through.
210 3 my ($source, @rest) = @args;
211 3 my $to_insert = $rest[0];
212 3 ▶ my $table = (Scalar::Util::blessed($source) && $source->can('name'))
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($source) && $source->can('name') ? : 1 2 3
Condition66%2/3
builtin::blessed($source) and $source->can("name")
!ll&&!rl&&r
201
MC/DC50%1/2
builtin::blessed($source) and $source->can("name")
builtin::blessed($source) $source->can("name")
Truth table66%2/3
A and B
A= builtin::blessed($source)B= $source->can("name")
ABresult
0X0
100
111
213 ? $source->name
214 : $source;
215  
216 3 my ($sql, @bind) = $self->sql_maker->insert($table, @rest);
217  
218 # LAST_INSERT_ID() must run on the SAME connection as the INSERT, otherwise
219 # the session-scope LII is undefined / from a prior session. Both run pinned
220 # on $conn, so the SELECT never hits a foreign session's LII.
221 3 ▶ return $self->_query_async_pinned($conn, $sql, \@bind)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
222 3 return $self->_query_async_pinned($conn, 'SELECT LAST_INSERT_ID()', []);
223 3 ▶ })->then(sub {
Subroutine __ANON__: called CC=4 SCAR=15.6
224 3 my $lii_row = shift;
225 3 ▶ my $lii_id = (ref $lii_row eq 'ARRAY' && @$lii_row) ? $lii_row->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $lii_row eq 'ARRAY' && @$lii_row ? : 3 0 3
Condition33%1/3
ref $lii_row eq "ARRAY" and @$lii_row
!ll&&!rl&&r
003
MC/DC0%0/2
ref $lii_row eq "ARRAY" and @$lii_row
ref $lii_row eq "ARRAY" @$lii_row
Truth table33%1/3
A and B
A= ref $lii_row eq "ARRAY"B= @$lii_row
ABresult
0X0
100
111
226 # Capture for legacy ->last_insert_id callers (e.g. scripts, tests).
227 3 ▶ $self->{_last_insert_id} = $lii_id if defined $lii_id;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lii_id 3 0 3
228 3 return $self->_insert_returned_columns($source, $to_insert, $lii_row);
229 3 });
230 }
231  
232  
233 3 ▶sub _insert_returned_columns {
Subroutine _insert_returned_columns: called CC=7 SCAR=21.1
234 3 my ($self, $source, $to_insert, $lii_row) = @_;
235  
236 3 ▶ my %returned = %{ $to_insert || {} };
Condition50%1/2
$to_insert || {}
l!l
30
MC/DC0%0/1
$to_insert || {}
$to_insert
Truth table50%1/2
A
A= $to_insert
Aresult
11
00
237 3 ▶ return \%returned unless defined $lii_row && ref $lii_row eq 'ARRAY' && @$lii_row;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lii_row and ref $lii_row eq "ARRAY" and @$lii_row 0 3 3
Condition33%2/6
defined $lii_row and ref $lii_row eq "ARRAY" and @$lii_row
!ll&&!rl&&r
003
defined $lii_row and ref $lii_row eq "ARRAY"
!ll&&!rl&&r
003
MC/DC0%0/3
defined $lii_row and ref $lii_row eq "ARRAY" and @$lii_row
defined $lii_row ref $lii_row eq "ARRAY" @$lii_row
Truth table25%1/4
A and B and C
A= defined $lii_rowB= ref $lii_row eq "ARRAY"C= @$lii_row
ABCresult
0XX0
10X0
1100
1111
238 3 my $lii_id = $lii_row->[0];
239 3 ▶ return \%returned unless defined $lii_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lii_id 0 3 3
240  
241 3 my $col = $self->_auto_increment_column($source, $to_insert);
242 3 ▶ return \%returned unless defined $col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col 2 1 3
243 1 $returned{$col} = $lii_id;
244  
245 1 return \%returned;
246 }
247  
248 # Find the column a $storage->insert should drop the LAST_INSERT_ID() value
249 # into, matching the sync DBIO::Storage::DBI heuristic:
250 # - the first column with is_auto_increment, OR
251 # - the first PK column that wasn't supplied in $to_insert (no
252 # is_auto_increment flag on the schema, MySQL still hands back an
253 # LII because the PK is auto_increment server-side).
254 # Returns undef when neither applies (e.g. a table with no PK), in which
255 # case the hashref is left as the supplied insert data only.
256 3 ▶sub _auto_increment_column {
Subroutine _auto_increment_column: called CC=13 SCAR=26.9
257 3 my ($self, $source, $to_insert) = @_;
258 3 ▶ return unless Scalar::Util::blessed($source) && $source->can('columns');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($source) and $source->can("columns") 2 1 3
Condition66%2/3
builtin::blessed($source) and $source->can("columns")
!ll&&!rl&&r
201
MC/DC50%1/2
builtin::blessed($source) and $source->can("columns")
builtin::blessed($source) $source->can("columns")
Truth table66%2/3
A and B
A= builtin::blessed($source)B= $source->can("columns")
ABresult
0X0
100
111
259  
260 1 ▶ my $col_infos = eval { $source->columns_info } || {};
Condition50%1/2
eval { || {}
l!l
10
MC/DC0%0/1
eval { do { $source->columns_info } } || {}
eval { do { $source->columns_info } }
Truth table50%1/2
A
A= eval { do { $source->columns_info } }
Aresult
11
00
261 1 ▶ my @pks = $source->can('primary_columns') ? $source->primary_columns : ();
Branch: 1 branch
BranchTrueFalseTotal
$source->can('primary_columns') ? : 1 0 1
262 1 my %is_pk = map { $_ => 1 } @pks;
263  
264 1 my $first_autoinc;
265 my $first_pk_unsupplied;
266 1 for my $col ($source->columns) {
267 2 ▶ if (!defined $first_autoinc
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $first_autoinc and $col_infos->{$col}{"is_auto_increment"} // 0) 1 1 2
Condition60%3/5
not defined $first_autoinc and $col_infos->{$col}{"is_auto_increment"} // 0
!ll&&!rl&&r
101
$col_infos->{$col}{"is_auto_increment"} // 0
l!l
10
MC/DC50%1/2
not defined $first_autoinc and $col_infos->{$col}{"is_auto_increment"} // 0
not defined $first_autoinc $col_infos->{$col}{"is_auto_increment"}
Truth table66%2/3
not(A) and B
A= not defined $first_autoincB= $col_infos->{$col}{"is_auto_increment"}
ABresult
0X0
100
111
268 && ($col_infos->{$col}{is_auto_increment} // 0)) {
269 1 $first_autoinc = $col;
270 }
271 2 ▶ if ($is_pk{$col}
Branch: 1 branch
BranchTrueFalseTotal
if ($is_pk{$col} and not defined $first_pk_unsupplied and not exists $to_insert->{$col}) 1 1 2
Condition66%4/6
$is_pk{$col} and not defined $first_pk_unsupplied and not exists $to_insert->{$col}
!ll&&!rl&&r
101
$is_pk{$col} and not defined $first_pk_unsupplied
!ll&&!rl&&r
101
MC/DC33%1/3
$is_pk{$col} and not defined $first_pk_unsupplied and not exists $to_insert->{$col}
$is_pk{$col} not defined $first_pk_unsupplied not exists $to_insert->{$col}
Truth table50%2/4
A and not(B) and not(C)
A= $is_pk{$col}B= not defined $first_pk_unsuppliedC= not exists $to_insert->{$col}
ABCresult
0XX0
10X0
1100
1111
272 && !defined $first_pk_unsupplied
273 && !exists $to_insert->{$col}) {
274 1 $first_pk_unsupplied = $col;
275 }
276 }
277  
278 1 ▶ return $first_autoinc if defined $first_autoinc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $first_autoinc 1 0 1
279 0 return $first_pk_unsupplied;
280 }
281  
282 # --- Pool connect-action replay seam (karr #68 / #18) -------------------------
283  
284  
285 5 ▶sub _run_pool_connect_statement {
Subroutine _run_pool_connect_statement: called CC=5 SCAR=16.5
286 5 my ($self, $conn, $sql, $attrs, @bind) = @_;
287 5 require EV;
288  
289 # The connection was just spawned; its async connect may still be in flight.
290 # Block the EV loop until it is ready (the core seam's synchronous contract)
291 # -- prepare/execute are exclusive ops and need an idle, connected handle.
292 5 ▶ EV::run(EV::RUN_ONCE()) until $conn->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
$conn->is_connected or do { EV::run(EV::RUN_ONCE()) } 5 0 5
293  
294 5 $sql = $self->_transform_sql($sql);
295  
296 5 my $done = 0;
297 5 my $err;
298 5 ▶ my $cb = sub { (undef, $err) = @_; $done = 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
299  
300 5 ▶ if (@bind) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bind) { } 1 4 5
301 # EV::MariaDB->query() takes no bind values; a bound connect statement must
302 # go through prepare + execute. (Connect actions are almost always bindless
303 # SET statements, but honour binds for completeness.)
304 1 ▶ $conn->prepare($sql, sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
305 1 my ($stmt, $perr) = @_;
306 1 ▶ if ($perr) { $err = $perr; $done = 1; return }
Branch: 1 branch
BranchTrueFalseTotal
if ($perr) 0 1 1
307 1 $conn->execute($stmt, [@bind], $cb);
308 1 });
309 }
310 else {
311 4 $conn->query($sql, $cb);
312 }
313 5 ▶ EV::run(EV::RUN_ONCE()) until $done;
Branch: 1 branch
BranchTrueFalseTotal
$done or do { EV::run(EV::RUN_ONCE()) } 5 0 5
314  
315 5 ▶ croak "pool connect statement failed: $err" if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 1 4 5
316 4 return;
317 }
318  
319 # --- Schema integration -------------------------------------------------------
320 # schema / debug / connected / in_txn / disconnect / DESTROY and the sync
321 # CRUD/txn ->get fallbacks are inherited from DBIO::Storage::Async.
322  
323  
324 2 ▶sub last_insert_id {
Subroutine last_insert_id: called CC=1 SCAR=0.0
Pod documented
325 2 my ($self, $source, @cols) = @_;
326 2 return $self->{_last_insert_id};
327 }
328  
329 7 1;
330  
331 __END__
332  
333 =pod
334  
335 =encoding UTF-8
336  
337 =head1 NAME
338  
339 DBIO::MySQL::EV::Storage - Async MySQL/MariaDB storage driver using EV::MariaDB
340  
341 =head1 VERSION
342  
343 version 0.900001
344  
345 =head1 DESCRIPTION
346  
347 The native C<ev> async transport for MySQL/MariaDB (core ADR 0030 / 0031): a
348 B<thin transport> over L<EV::MariaDB>, a non-blocking client that speaks the
349 MariaDB C client library directly. No DBI, no DBD::MySQL, just raw MariaDB
350 performance.
351  
352 It resolves as the C<ev> async mode of L<DBIO::MySQL::Storage> (registered
353 there via C<< register_async_mode( ev => ... ) >>), so a schema connected with
354 C<< { async => 'ev' } >> answers the six C<*_async> storage methods over
355 EV::MariaDB while the same schema keeps serving synchronous queries over
356 DBI/DBD::MySQL.
357  
358 =head2 Thin transport: shared machinery is inherited
359  
360 The B<Model-B orchestration> (ADR 0030 §4) is inherited unchanged from
361 L<DBIO::Storage::Async>: the CRUD runner (L<DBIO::Storage::Async/_run_crud>
362 with its pooled / pinned runners), L<DBIO::Storage::Async/txn_do_async>
363 bracketing, the C<sql_maker> plumbing and the sync C<< ->get >> fallbacks. This
364 class fills B<only> the loop-/wire-specific seams and the genuine MySQL
365 value-add:
366  
367 =over 4
368  
369 =item * L</sql_maker_class> / L</_sql_maker_args> -- the MySQL SQLMaker
370 (backtick quoting, C<LIMIT offset, rows> pagination)
371  
372 =item * L</_transform_sql> -- B<identity>. MySQL/DBD speaks C<?> natively, so
373 there is no placeholder rewrite; the seam is kept only to honour the uniform
374 core #70 / ADR 0032 C<?>-in contract
375  
376 =item * L</_query_async> / L</_query_async_pinned> -- native EV::MariaDB query
377 dispatch (pooled and pinned), routed through L<DBIO::MySQL::EV::QueryExecutor>
378  
379 =item * L</insert_async> + L</_run_crud_pinned> + L</_insert_returned_columns>
380 -- MySQL has B<no RETURNING clause>, so INSERT reads C<SELECT LAST_INSERT_ID()>
381 on the B<same> pinned connection and folds the auto-increment id into the
382 returned-columns hashref (ADR 0031 §3). This is the one CRUD path core's
383 generic RETURNING-shaped runner cannot serve, so it is overridden here
384  
385 =item * L</_run_pool_connect_statement> -- synchronous connect-action replay on
386 a freshly-spawned EV::MariaDB pool connection (karr #68 / karr #18 seam)
387  
388 =item * L</connect_info> / L</_normalize_async_connect_info> / L</_conninfo_hash>
389 -- the EV::MariaDB named-parameter conninfo transport shape (incl.
390 C<dbname>-E<gt>C<database>)
391  
392 =item * L</_txn_context_class> / L</_txn_conn_accessor> -- the pinned-connection
393 transaction context
394  
395 =back
396  
397 =head2 Transport value-add
398  
399 MySQL/MariaDB has B<no> LISTEN/NOTIFY and B<no> COPY, so -- unlike the
400 PostgreSQL EV transport -- this transport carries no such wire value-add and
401 declares none. EV::MariaDB pipelines queries B<automatically> at the wire level
402 (consecutive C<query> calls are batched, up to 64 in flight) with B<no> explicit
403 pipeline-mode API to bracket; the core C<_pipeline_enter>/C<_pipeline_sync>/
404 C<_pipeline_exit> seam therefore does not apply and the C<pipeline> capability
405 is B<not> declared (see L</transport_capabilities>). The only capability this
406 transport provides is C<on_connect_replay>.
407  
408 =head1 METHODS
409  
410 =head2 sql_maker_class
411  
412 The L<DBIO::MySQL::SQLMaker> subclass used to generate SQL. Shared with the sync
413 driver, so the maker keeps emitting C<?> placeholders -- which MySQL/DBD
414 understands natively (L</_transform_sql> is identity).
415  
416 =head2 _sql_maker_args
417  
418 MySQL SQLMaker construction args: backtick identifier quoting, C<.> name
419 separator. (The MySQL C<LIMIT offset, rows> pagination dialect is baked into
420 L<DBIO::MySQL::SQLMaker> itself.)
421  
422 =head2 _post_insert_sql
423  
424 Empty string -- MySQL has B<no> RETURNING clause. The INSERT path
425 (L</_run_crud_pinned>) reads C<SELECT LAST_INSERT_ID()> on the pinned connection
426 instead of appending a RETURNING suffix, so core's generic
427 C<_run_crud> insert branch (which consumes this seam) is never reached for this
428 transport; the seam is defined only to satisfy the base contract.
429  
430 =head2 future_class
431  
432 Returns C<'Future'> -- uses L<Future.pm|Future> from CPAN. Plain L<Future>
433 C<< ->then >> callbacks auto-wrap a non-Future return value into a resolved
434 Future (ADR 0031 §4), so ResultSet/Row C<< *_async >> callbacks that return
435 plain values resolve without an explicit C<< Future->done(...) >> wrap.
436  
437 =head2 transport_capabilities
438  
439 my @caps = DBIO::MySQL::EV::Storage->transport_capabilities;
440  
441 Class method (see L<DBIO::Storage::Async/transport_capabilities>). Declares the
442 wire capabilities this transport really provides, so
443 L<DBIO::Storage::DBI/_async_storage> lets an async extension layer that requires
444 any of them compose onto it (and croaks naming the gap otherwise, rather than
445 silently dropping a feature). This transport provides B<only>:
446  
447 =over 4
448  
449 =item * C<on_connect_replay> -- its pool (L<DBIO::MySQL::EV::Pool>, a
450 L<DBIO::Storage::PoolBase>) drives core's
451 L<DBIO::Storage::Async/_setup_pool_connection> on every freshly-spawned
452 connection, and L</_run_pool_connect_statement> replays the owning sync
453 storage's C<on_connect_do>/C<on_connect_call> against it (karr #68 / #18).
454  
455 =back
456  
457 It does B<not> declare C<listen>, C<notify> or C<copy> (MySQL has neither
458 LISTEN/NOTIFY nor COPY), nor C<pipeline> (EV::MariaDB pipelines automatically
459 at the wire level; there is no explicit pipeline-mode API for the core seam to
460 bracket).
461  
462 =head2 connect_info
463  
464 $storage->connect_info([ \%conninfo, \%opts ]);
465  
466 Set connection parameters. C<%conninfo> is passed to L<EV::MariaDB> as named
467 connection parameters (host, user, password, database, ...). When the embedding
468 sync storage feeds its own DBI-form connect info
469 (C<< ['dbi:MariaDB:...', $user, $pass, \%attrs] >>) it is translated into the
470 async C<< [ \%conninfo, \%opts ] >> shape first (EV::MariaDB cannot parse a DSN
471 string). AccessBroker connect info (C<< [ $broker ] >>) is detected and wired to
472 the per-spawn credential provider (the inherited broker seam), exactly as the
473 sync path does.
474  
475 This override (rather than the inherited base C<connect_info>) is retained
476 because the EV transport folds the DBI-form DSN into EV::MariaDB named params
477 inline via L<DBIO::MySQL::EV::ConnectInfo/dbi_to_conninfo>.
478  
479 =head2 _async_broker_conninfo
480  
481 my $conninfo = $storage->_async_broker_conninfo($mode);
482  
483 AccessBroker seam (see L<DBIO::Storage::Async/ACCESSBROKER CONSUMPTION>): return
484 one fresh, storage-native EV::MariaDB conninfo hash for a single new pool
485 connection, built from the current broker credentials via the inherited
486 normalisation.
487  
488 =head2 _normalize_async_connect_info
489  
490 my ($conninfo, $pool_size, $opts) = $storage->_normalize_async_connect_info($info);
491  
492 MySQL-specific override of the base normaliser: besides stripping C<pool_size>
493 out of the conninfo, it maps C<dbname> to C<database> (EV::MariaDB's spelling)
494 so a hash-form connect that used the DBI-style C<dbname> key still reaches the
495 native client correctly. Works on copies so the caller's structures are never
496 mutated.
497  
498 =head2 _conninfo_hash
499  
500 my $conninfo = $storage->_conninfo_hash;
501  
502 Returns the already-normalised stored EV::MariaDB conninfo hashref (or a passed
503 override). Normalisation (C<dbname>-E<gt>C<database>, C<pool_size> extraction)
504 is done once in L</_normalize_async_connect_info>, so this returns it as-is.
505  
506 =head2 pool
507  
508 Returns the L<DBIO::MySQL::EV::Pool> connection pool, created lazily on first
509 access. The pool is wired with C<< storage => $self >> so its core-shared spawn
510 path (L<DBIO::Storage::PoolBase/_spawn_connection>) replays the owning sync
511 storage's connect actions on each new connection via
512 L</_run_pool_connect_statement> (karr #68 / #18). Fed the per-spawn
513 C<conninfo_provider> when an AccessBroker is attached, otherwise the static
514 conninfo hash.
515  
516 =head2 _txn_context_class
517  
518 The pinned-connection transaction context the inherited
519 L<DBIO::Storage::Async/txn_do_async> hands to its coderef:
520 L<DBIO::MySQL::EV::TransactionContext>.
521  
522 =head2 _txn_conn_accessor
523  
524 The constructor key the pinned connection is passed under -- C<mdb>, matching
525 L<DBIO::MySQL::EV::TransactionContext>.
526  
527 =head2 _transform_sql
528  
529 Transport-internal SQL shaping (core #70 / ADR 0032). B<Identity> for MySQL:
530 MySQL/DBD speaks the C<?> placeholder dialect natively, so the C<sql_maker>
531 output needs no rewrite. The seam is retained (and applied first by
532 L</_query_async> / L</_query_async_pinned>) purely to keep the transport seam
533 contract uniform across drivers -- do B<not> reintroduce a dialect rewrite here.
534  
535 =head2 _query_async
536  
537 Transport seam. Execute a query on a freshly-acquired pooled connection,
538 releasing it once the Future is ready. Returns a L<Future> of the raw result
539 rows (list of column arrayrefs for a result set, or the affected-row count for
540 plain DML -- ADR 0031 §3), exactly what the inherited C<_run_crud> expects.
541  
542 Receives SQL in the C<sql_maker> C<?>-placeholder dialect and shapes it via
543 L</_transform_sql> (identity for MySQL) before it reaches the executor -- the
544 core #70 / ADR 0032 seam contract.
545  
546 =head2 _query_async_pinned
547  
548 Transport seam. Like L</_query_async> but runs on the supplied pinned connection
549 and does B<not> release it -- used for queries inside a pinned transaction and
550 for the INSERT + C<LAST_INSERT_ID()> pair (which must ride the same session).
551 Shapes the incoming C<?>-dialect SQL via L</_transform_sql> exactly as
552 L</_query_async> does.
553  
554 =head2 insert_async
555  
556 my $future = $storage->insert_async($source, \%vals);
557  
558 Resolve the returned-columns I<hashref> -- the supplied insert data overlaid
559 with the auto-increment PK the engine populated (ADR 0031 §3). MySQL has no
560 RETURNING clause, so the INSERT and a following C<SELECT LAST_INSERT_ID()> must
561 run on the B<same> session: this override acquires one pooled connection, runs
562 the whole insert pinned on it via L</_run_crud_pinned>, and releases it when the
563 Future settles. (The base C<insert_async> uses the pooled runner, which would
564 acquire a fresh connection per query and read a foreign session's LII.)
565  
566 =head2 _run_crud_pinned
567  
568 my $future = $storage->_run_crud_pinned($op, $conn, @args);
569  
570 Build SQL and run a CRUD op on a pinned connection (the entry point the
571 L<DBIO::MySQL::EV::TransactionContext> and L</insert_async> use). For every op
572 except C<insert> this delegates to the inherited runner. The C<insert> op is the
573 MySQL value-add: it runs the INSERT and then C<SELECT LAST_INSERT_ID()> on the
574 B<same> C<$conn> (so the session-scoped LII is the one the INSERT produced),
575 captures it for legacy L</last_insert_id> callers, and folds it into the
576 returned-columns hashref via L</_insert_returned_columns>.
577  
578 =head2 _insert_returned_columns
579  
580 $hashref = $storage->_insert_returned_columns($source, \%to_insert, $lii_row);
581  
582 Assemble the returned-columns hashref from a C<SELECT LAST_INSERT_ID()> row (ADR
583 0031 §3). MySQL has no RETURNING clause, so the INSERT path captures the LII on
584 the pinned connection and hands the single-column row to this method. The
585 hashref is C<< { %$to_insert, $col => $lii_id } >>, where C<$col> is the source's
586 first C<is_auto_increment> column (falling back to the first PK column whose
587 value was not supplied, matching the sync L<DBIO::Storage::DBI/insert>
588 heuristic). Overrides the inherited default that maps a positional RETURNING row
589 onto a column list.
590  
591 =head2 _run_pool_connect_statement
592  
593 $storage->_run_pool_connect_statement($conn, $sql, $attrs, @bind);
594  
595 Native-backend override of the core connect-action runner seam
596 (L<DBIO::Storage::Async/_run_pool_connect_statement>). The base default drives a
597 blocking DBI C<do> and croaks on a non-DBI connection; an EV::MariaDB handle is
598 neither a C<< { dbh => $dbh } >> wrapper nor a do-capable DBI handle, so this
599 override drives the statement B<synchronously to completion> on that connection
600 over the EV loop.
601  
602 Called once per freshly-spawned pool connection from
603 L<DBIO::Storage::Async/_setup_pool_connection>, so the owning sync storage's
604 C<on_connect_do> / C<on_connect_call> replay on every new EV pool connection,
605 identical to the DBD-based pools (karr #68 / #18). The freshly-spawned
606 EV::MariaDB handle connects asynchronously, so we first drive the loop until it
607 is up, then run the statement and drive the loop until it completes. Bindless
608 statements (the usual C<SET ...> connect actions) go on the queueable C<query>
609 path; a bound statement is routed through C<prepare> + C<execute>. C<$attrs>
610 (the DBI attribute hashref) has no EV::MariaDB analogue and is ignored.
611  
612 =head2 last_insert_id
613  
614 my $id = $storage->last_insert_id;
615  
616 Returns the auto-increment value from the last INSERT performed on this storage
617 instance (captured from C<SELECT LAST_INSERT_ID()> by L</_run_crud_pinned>).
618 Valid after L<insert_async|/insert_async> or the sync C<insert> succeeds.
619  
620 =head1 AUTHOR
621  
622 DBIO Authors
623  
624 =head1 COPYRIGHT AND LICENSE
625  
626 Copyright (C) 2026 DBIO Authors
627  
628 This is free software; you can redistribute it and/or modify it under
629 the same terms as the Perl 5 programming language system itself.
630  
631 =cut
« EV/TestHarness.pm ↑ Summary EV/QueryExecutor.pm »