Async/Storage.pm

filter: Statementstmt 37.7% 89 / 236 Branchbran 18.7% 12 / 64 Conditioncond 19.2% 5 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 45.0% 32 / 71 Podpod 100% 24 / 24 totaltotal 37.0% 162 / 437 CC 43 SCAR 21.6
CC
43
Cov
33%
_normalize_async_connect_info
42.0
_current_async_connect_info
30.0
__ANON__
30.0
CRAP 8.7
↑ Summary Async/QueryExecutor.pm »
1 package DBIO::MySQL::Async::Storage;
2 # ABSTRACT: Async MySQL/MariaDB storage driver using EV::MariaDB
3  
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use base 'DBIO::Storage::Async';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use DBIO::MySQL::SQLMaker;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 4 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 4 my $PREPARED_COUNTER = 0;
15  
16 sub new {
17 10 ▶ my ($class, $schema, $args) = @_;
Subroutine new: called CC=2 SCAR=7.1
Pod documented
18 my $self = bless {
19 schema => $schema,
20 pool => undef,
21 executor => undef,
22 connect_info => undef,
23 sql_maker => undef,
24 sql_maker_class => 'DBIO::MySQL::SQLMaker',
25 _prepared_cache => {},
26 _conninfo_provider => undef,
27 _last_insert_id => undef,
28 10 ▶ debug => $ENV{DBIO_TRACE} || 0,
Condition50%1/2
$ENV{'DBIO_TRACE'} || 0
l!l
010
MC/DC0%0/1
$ENV{'DBIO_TRACE'} || 0
$ENV{'DBIO_TRACE'}
Truth table50%1/2
A
A= $ENV{'DBIO_TRACE'}
Aresult
11
00
29 debugobj => undef,
30 }, $class;
31 10 $self;
32 }
33  
34  
35 1 ▶sub future_class { 'Future' }
Subroutine future_class: called CC=1 SCAR=0.0
Pod documented
36  
37  
38 sub connect_info {
39 0 ▶ my ($self, $info) = @_;
Subroutine connect_info: not called CC=4 SCAR=30.0
Pod documented
40 0 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 0 0 0
41 0 ▶ $self->disconnect if $self->{pool};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pool"} 0 0 0
42 0 $self->{connect_info} = $info;
43  
44 0 ▶ if ($self->_is_access_broker_connect_info($info)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_access_broker_connect_info($info)) { } 0 0 0
45 0 $self->_setup_access_broker($info->[0]);
46 0 my ($conninfo, $pool_size, $opts) = $self->_normalize_async_connect_info(
47 $self->_current_async_connect_info($self->access_broker_mode)
48 );
49 0 $self->{_conninfo} = $conninfo;
50 0 $self->{_pool_size} = $pool_size;
51 0 $self->{_opts} = $opts;
52 }
53 else {
54 0 $self->_clear_access_broker;
55 0 my ($conninfo, $pool_size, $opts) = $self->_normalize_async_connect_info($info);
56 0 $self->{_conninfo} = $conninfo;
57 0 $self->{_pool_size} = $pool_size;
58 0 $self->{_opts} = $opts;
59 }
60 }
61 0 return $self->{connect_info};
62 }
63  
64 sub _current_async_connect_info {
65 0 ▶ my ($self, $mode) = @_;
Subroutine _current_async_connect_info: not called CC=5 SCAR=34.0
66  
67 0 my $connect_info = $self->current_access_broker_connect_info($mode);
68 0 ▶ return [$connect_info, {}] if $connect_info && ref $connect_info eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $connect_info and ref $connect_info eq "HASH" 0 0 0
Condition0%0/3
$connect_info and ref $connect_info eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$connect_info and ref $connect_info eq "HASH"
$connect_info ref $connect_info eq "HASH"
Truth table0%0/3
A and B
A= $connect_infoB= ref $connect_info eq "HASH"
ABresult
0X0
100
111
69 0 ▶ return $connect_info if $connect_info;
Branch: 1 branch
BranchTrueFalseTotal
if $connect_info 0 0 0
70  
71 0 ▶ return [ $self->{_conninfo}, $self->{_opts} || {} ];
Condition0%0/2
$self->{'_opts'} || {}
l!l
00
MC/DC0%0/1
$self->{'_opts'} || {}
$self->{'_opts'}
Truth table0%0/2
A
A= $self->{'_opts'}
Aresult
11
00
72 }
73  
74 # Driver seam hook for the inherited async AccessBroker mechanics
75 # (DBIO::Storage::Async::_setup_access_broker installs a conninfo_provider
76 # that invokes this once per pool spawn). Returns ONE fresh, normalized,
77 # EV::MariaDB-native conninfo hashref — the shape the pool's identity
78 # _transform_conninfo passes straight to _create_connection.
79 sub _async_broker_conninfo {
80 0 ▶ my ($self, $mode) = @_;
Subroutine _async_broker_conninfo: not called CC=1 SCAR=6.9
81  
82 0 my ($conninfo) = $self->_normalize_async_connect_info(
83 $self->_current_async_connect_info($mode)
84 );
85 0 return $conninfo;
86 }
87  
88 sub _normalize_async_connect_info {
89 0 ▶ my ($self, $info) = @_;
Subroutine _normalize_async_connect_info: not called CC=6 SCAR=37.4
90  
91 0 my $conninfo = $info->[0];
92 0 ▶ $conninfo = ref($conninfo) eq 'HASH' ? { %$conninfo } : $conninfo;
Branch: 1 branch
BranchTrueFalseTotal
ref $conninfo eq 'HASH' ? : 0 0 0
93  
94 0 my $opts = $info->[1];
95 0 ▶ $opts = ref($opts) eq 'HASH' ? { %$opts } : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $opts eq 'HASH' ? : 0 0 0
96  
97 0 my $pool_size = 5;
98 0 ▶ if (ref($conninfo) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $conninfo eq "HASH") 0 0 0
99 0 ▶ $pool_size = delete $conninfo->{pool_size} // 5;
Condition0%0/2
delete $conninfo->{"pool_size"} // 5
l!l
00
MC/DC0%0/1
delete $conninfo->{"pool_size"} // 5
delete $conninfo->{"pool_size"}
Truth table0%0/2
A
A= delete $conninfo->{"pool_size"}
Aresult
11
00
100 # MySQL uses 'database', not 'dbname'
101 0 ▶ $conninfo->{database} = delete $conninfo->{dbname} if exists $conninfo->{dbname};
Branch: 1 branch
BranchTrueFalseTotal
if exists $conninfo->{"dbname"} 0 0 0
102 }
103  
104 0 return ($conninfo, $pool_size, $opts);
105 }
106  
107  
108 sub pool {
109 16 ▶ my $self = shift;
Subroutine pool: called CC=3 SCAR=20.6
Pod documented
110 16 ▶ $self->{pool} ||= do {
Condition33%1/3
$self->{"pool"} ||= do {
l!l&&r!l&&!r
1600
MC/DC0%0/2
$self->{"pool"} ||= do { require DBIO::MySQL::Async::Pool; my(%args) = ("size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::MySQL::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; my $pool = "DBIO::MySQL::Async::Pool"->new(%args); $self->{"executor"} = "DBIO::MySQL::Async::QueryExecutor"->new("pool", $pool, "debug", $self->{"debug"}); $pool }
$self->{"pool"} do { require DBIO::MySQL::Async::Pool; my(%args) = ("size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::MySQL::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; my $pool = "DBIO::MySQL::Async::Pool"->new(%args); $self->{"executor"} = "DBIO::MySQL::Async::QueryExecutor"->new("pool", $pool, "debug", $self->{"debug"}); $pool }
Truth table33%1/3
A ||= B
A= $self->{"pool"}B= do { require DBIO::MySQL::Async::Pool; my(%args) = ("size", $self->{"_pool_size"}, "on_error", sub { warn "DBIO::MySQL::Async pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->{"_conninfo"}; } ; my $pool = "DBIO::MySQL::Async::Pool"->new(%args); $self->{"executor"} = "DBIO::MySQL::Async::QueryExecutor"->new("pool", $pool, "debug", $self->{"debug"}); $pool }
ABresult
1X1
011
000
111 0 require DBIO::MySQL::Async::Pool;
112 my %args = (
113 size => $self->{_pool_size},
114 0 ▶ on_error => sub { warn "DBIO::MySQL::Async pool error: $_[0]\n" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 );
116  
117 0 ▶ if (my $provider = $self->_conninfo_provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $provider = $self->_conninfo_provider) { } 0 0 0
118 0 $args{conninfo_provider} = $provider;
119 }
120 else {
121 0 $args{conninfo} = $self->{_conninfo};
122 }
123  
124 0 my $pool = DBIO::MySQL::Async::Pool->new(%args);
125 $self->{executor} = DBIO::MySQL::Async::QueryExecutor->new(
126 pool => $pool,
127 debug => $self->{debug},
128 0 );
129 0 $pool;
130 };
131 }
132  
133 sub _conninfo_hash {
134 0 ▶ my ($self, $ci) = @_;
Subroutine _conninfo_hash: not called CC=4 SCAR=30.0
135 # Use already-normalized stored conninfo; only do fallback resolution
136 # when called in contexts where _conninfo is not yet set
137 0 ▶ my $conninfo = defined($ci) ? $ci : $self->{_conninfo};
Branch: 1 branch
BranchTrueFalseTotal
defined $ci ? : 0 0 0
138 0 ▶ return unless defined $conninfo;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conninfo 0 0 0
139 0 ▶ return $conninfo if ref $conninfo ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $conninfo ne "HASH" 0 0 0
140 # Already normalized by _normalize_async_connect_info; return as-is
141 0 return $conninfo;
142 }
143  
144 # --- SQL Generation ---
145  
146  
147 sub sql_maker {
148 10 ▶ my $self = shift;
Subroutine sql_maker: called CC=2 SCAR=7.4
Pod documented
149 10 ▶ $self->{sql_maker} ||= do {
Condition33%1/3
$self->{"sql_maker"} ||= do {
l!l&&r!l&&!r
0100
MC/DC0%0/2
$self->{"sql_maker"} ||= do { my $class = $self->{"sql_maker_class"}; $class->new("quote_char", "`", "name_sep", ".") }
$self->{"sql_maker"} do { my $class = $self->{"sql_maker_class"}; $class->new("quote_char", "`", "name_sep", ".") }
Truth table33%1/3
A ||= B
A= $self->{"sql_maker"}B= do { my $class = $self->{"sql_maker_class"}; $class->new("quote_char", "`", "name_sep", ".") }
ABresult
1X1
011
000
150 10 my $class = $self->{sql_maker_class};
151 10 $class->new(
152 quote_char => '`',
153 name_sep => '.',
154 );
155 };
156 }
157  
158 sub _generate_sql {
159 0 ▶ my ($self, $op, @args) = @_;
Subroutine _generate_sql: not called CC=2 SCAR=17.9
160 0 my $sm = $self->sql_maker;
161 my $method = {
162 select => 'select',
163 select_single => 'select',
164 insert => 'insert',
165 update => 'update',
166 delete => 'delete',
167 0 ▶ }->{$op} or croak "Unknown operation: $op";
Branch: 1 branch
BranchTrueFalseTotal
my $method = {"select", "select", "select_single", "select", "insert", "insert", "update", "update", "delete", "delete"}->{$op} or croak("Unknown operation: $op") 0 0 0
168  
169 0 return $sm->$method(@args);
170 }
171  
172 # --- Async Query Execution ---
173  
174  
175 sub select_async {
176 4 ▶ my $self = shift;
Subroutine select_async: called CC=1 SCAR=0.0
Pod documented
177 4 return $self->_run_crud('select', undef, @_);
178 }
179  
180  
181 sub select_single_async {
182 1 ▶ my $self = shift;
Subroutine select_single_async: called CC=1 SCAR=0.0
Pod documented
183 1 return $self->_run_crud('select_single', undef, @_);
184 }
185  
186  
187 sub insert_async {
188 1 ▶ my $self = shift;
Subroutine insert_async: called CC=1 SCAR=0.0
Pod documented
189 1 return $self->_run_crud('insert', undef, @_);
190 }
191  
192  
193 sub update_async {
194 1 ▶ my $self = shift;
Subroutine update_async: called CC=1 SCAR=0.0
Pod documented
195 1 return $self->_run_crud('update', undef, @_);
196 }
197  
198  
199 sub delete_async {
200 1 ▶ my $self = shift;
Subroutine delete_async: called CC=1 SCAR=0.0
Pod documented
201 1 return $self->_run_crud('delete', undef, @_);
202 }
203  
204 # Build SQL for a CRUD op and run it on a connection. When $mdb is
205 # undef, a pooled connection is acquired and released around the query;
206 # when $mdb is given, the query runs pinned on that connection (no
207 # release) — used by the transaction context so all CRUD inside a
208 # txn_do_async hits the BEGIN/COMMIT connection.
209 sub _run_crud {
210 8 ▶ my ($self, $op, $mdb, @args) = @_;
Subroutine _run_crud: called CC=4 SCAR=14.0
211  
212 8 ▶ if ($op eq 'select_single') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "select_single") 1 7 8
213 1 my ($sql, @bind) = $self->sql_maker->select(@args);
214 return $self->_run_on_conn($mdb, sub {
215 1 ▶ my $conn = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
216 1 $self->_query_on($conn, $sql, \@bind);
217 })->then(sub {
218 1 ▶ my @rows = @_;
Subroutine __ANON__: called CC=2 SCAR=7.2
219 1 ▶ return @rows ? $rows[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
@rows ? : 1 0 1
220 1 });
221 }
222  
223 7 ▶ if ($op eq 'insert') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "insert") 1 6 7
224 1 my ($sql, @bind) = $self->sql_maker->insert(@args);
225 return $self->_run_on_conn($mdb, sub {
226 1 ▶ my $conn = shift;
Subroutine __ANON__: called CC=1 SCAR=0.2
227 $self->_query_on($conn, $sql, \@bind)->then(sub {
228 # LAST_INSERT_ID() must run on the SAME connection as the INSERT.
229 1 ▶ my $lii_f = $self->_query_on($conn, 'SELECT LAST_INSERT_ID()', []);
Subroutine __ANON__: called CC=1 SCAR=0.3
230 $lii_f->on_done(sub {
231 1 ▶ my @rows = @_;
Subroutine __ANON__: called CC=3 SCAR=13.1
232 1 ▶ $self->{_last_insert_id} = $rows[0]->[0] if @rows && defined $rows[0]->[0];
Branch: 1 branch
BranchTrueFalseTotal
if @rows and defined $rows[0][0] 1 0 1
Condition33%1/3
@rows and defined $rows[0][0]
!ll&&!rl&&r
001
MC/DC0%0/2
@rows and defined $rows[0][0]
@rows defined $rows[0][0]
Truth table33%1/3
A and B
A= @rowsB= defined $rows[0][0]
ABresult
0X0
100
111
233 1 });
234 1 return $lii_f;
235 1 });
236 1 });
237 }
238  
239 6 ▶ my $method = { select => 'select', update => 'update', delete => 'delete' }->{$op}
Branch: 1 branch
BranchTrueFalseTotal
my $method = {"select", "select", "update", "update", "delete", "delete"}->{$op} or croak("Unknown CRUD operation: $op") 0 6 6
240 or croak "Unknown CRUD operation: $op";
241 6 my ($sql, @bind) = $self->sql_maker->$method(@args);
242 return $self->_run_on_conn($mdb, sub {
243 6 ▶ my $conn = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
244 6 $self->_query_on($conn, $sql, \@bind);
245 6 });
246 }
247  
248 # Resolve a connection and run $cb->($conn) on it. When $mdb is given the
249 # connection is pinned: $cb runs on it and it is NOT released (the txn
250 # owner manages its lifecycle). When $mdb is undef a pooled connection is
251 # acquired via acquire->then — honoring the pool's waiter queue when the
252 # pool is full — and released once $cb's Future is ready, on success or
253 # failure, so connections never leak.
254 sub _run_on_conn {
255 8 ▶ my ($self, $mdb, $cb) = @_;
Subroutine _run_on_conn: called CC=2 SCAR=7.2
256  
257 8 ▶ if (defined $mdb) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mdb) 0 8 8
258 0 ▶ return Future->call(sub { $cb->($mdb) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
259 }
260  
261 return $self->pool->acquire->then(sub {
262 8 ▶ my $conn = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
263 8 ▶ my $f = Future->call(sub { $cb->($conn) });
Subroutine __ANON__: called CC=1 SCAR=0.0
264 8 ▶ $f->on_ready(sub { $self->pool->release($conn) });
Subroutine __ANON__: called CC=1 SCAR=0.0
265 8 return $f;
266 8 });
267 }
268  
269 # Run a single query on a concrete connection. No acquire, no release.
270 sub _query_on {
271 9 ▶ my ($self, $mdb, $sql, $bind) = @_;
Subroutine _query_on: called CC=3 SCAR=11.4
272 9 ▶ $bind //= [];
Condition50%1/2
$bind //= []
l!l
90
MC/DC0%0/1
$bind //= []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
273 9 ▶ $self->_debug_query($sql, $bind) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 9 9
274 9 return $self->{executor}->execute($mdb, $sql, $bind);
275 }
276  
277 # Low-level async query dispatch on a freshly-acquired pooled connection,
278 # released once ready. Retained for callers that already hold raw SQL.
279 sub _query_async {
280 0 ▶ my ($self, $sql, $bind) = @_;
Subroutine _query_async: not called CC=1 SCAR=6.9
281 return $self->_run_on_conn(undef, sub {
282 0 ▶ my $conn = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
283 0 $self->_query_on($conn, $sql, $bind);
284 0 });
285 }
286  
287 # Run a query on a specific connection without releasing it back to the
288 # pool. Used by the transaction context for the pinned connection and
289 # for sub-queries that must share a connection (e.g. LAST_INSERT_ID).
290 sub _query_async_pinned {
291 0 ▶ my ($self, $mdb, $sql, $bind) = @_;
Subroutine _query_async_pinned: not called CC=1 SCAR=6.9
292 0 return $self->_query_on($mdb, $sql, $bind);
293 }
294  
295 # Build SQL and run a CRUD op pinned on a given connection. Entry point
296 # used by TransactionContext so CRUD inside a txn hits the same
297 # connection BEGIN/COMMIT ran on, without duplicating SQL generation.
298 sub _run_crud_pinned {
299 0 ▶ my ($self, $op, $mdb, @args) = @_;
Subroutine _run_crud_pinned: not called CC=1 SCAR=6.9
300 0 return $self->_run_crud($op, $mdb, @args);
301 }
302  
303 sub _debug_query {
304 0 ▶ my ($self, $sql, $bind) = @_;
Subroutine _debug_query: not called CC=2 SCAR=17.9
305 0 ▶ my $bind_str = join(', ', map { defined $_ ? "'$_'" : 'NULL' } @$bind);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
306 0 warn "$sql: $bind_str\n";
307 }
308  
309 # --- Transactions ---
310  
311  
312 sub txn_do_async {
313 0 ▶ my ($self, $coderef, @args) = @_;
Subroutine txn_do_async: not called CC=1 SCAR=6.9
Pod documented
314  
315 0 require DBIO::MySQL::Async::TransactionContext;
316  
317 return $self->pool->acquire_txn->then(sub {
318 0 ▶ my $mdb = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
319  
320 0 my $txn_ctx = DBIO::MySQL::Async::TransactionContext->new(
321 storage => $self,
322 mdb => $mdb,
323 );
324  
325 0 my $f = Future->new;
326  
327 $mdb->query('BEGIN', sub {
328 0 ▶ my (undef, $err) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
329 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
330 0 $self->pool->release($mdb);
331 0 $f->fail("BEGIN failed: $err");
332 0 return;
333 }
334  
335 0 my $inner = eval { $coderef->($txn_ctx, @args) };
336 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
337 0 my $error = $@;
338 $mdb->query('ROLLBACK', sub {
339 0 ▶ $self->pool->release($mdb);
Subroutine __ANON__: not called CC=1 SCAR=6.9
340 0 $f->fail($error);
341 0 });
342 0 return;
343 }
344  
345 # If coderef returned a Future, chain COMMIT/ROLLBACK
346 0 ▶ if (ref $inner && $inner->can('then')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $inner and $inner->can('then')) { } 0 0 0
Condition0%0/3
ref $inner and $inner->can("then")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $inner and $inner->can("then")
ref $inner $inner->can("then")
Truth table0%0/3
A and B
A= ref $innerB= $inner->can("then")
ABresult
0X0
100
111
347 $inner->then(sub {
348 0 ▶ my @result = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
349 0 my $commit_f = Future->new;
350 $mdb->query('COMMIT', sub {
351 0 ▶ my (undef, $cerr) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
352 0 $self->pool->release($mdb);
353 0 ▶ if ($cerr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cerr) { } 0 0 0
354 0 $commit_f->fail("COMMIT failed: $cerr");
355 } else {
356 0 $commit_f->done(@result);
357 }
358 0 });
359 0 return $commit_f;
360 })->catch(sub {
361 0 ▶ my $error = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
362 0 my $rb_f = Future->new;
363 $mdb->query('ROLLBACK', sub {
364 0 ▶ $self->pool->release($mdb);
Subroutine __ANON__: not called CC=1 SCAR=6.9
365 0 $rb_f->fail($error);
366 0 });
367 0 return $rb_f;
368 0 ▶ })->on_done(sub { $f->done(@_) })
Subroutine __ANON__: not called CC=1 SCAR=6.9
369 0 ▶ ->on_fail(sub { $f->fail(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
370 } else {
371 # Coderef returned a plain value — commit immediately
372 $mdb->query('COMMIT', sub {
373 0 ▶ my (undef, $cerr) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
374 0 $self->pool->release($mdb);
375 0 ▶ if ($cerr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cerr) { } 0 0 0
376 0 $f->fail("COMMIT failed: $cerr");
377 } else {
378 0 $f->done($inner);
379 }
380 0 });
381 }
382 0 });
383  
384 0 return $f;
385 0 });
386 }
387  
388 # --- Pipeline Mode ---
389  
390  
391 sub pipeline {
392 0 ▶ my ($self, $coderef) = @_;
Subroutine pipeline: not called CC=1 SCAR=6.9
Pod documented
393  
394 return $self->pool->acquire->then(sub {
395 0 ▶ my $mdb = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
396 0 $mdb->query('SET autocommit=0'); # MariaDB pipelining needs explicit tx mode
397  
398 0 my $result = eval { $coderef->($self) };
399 0 my $err = $@;
400  
401 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
402 0 ▶ $mdb->query('ROLLBACK', sub {}); # we don't care about the result here
Subroutine __ANON__: not called CC=1 SCAR=6.9
403 0 $self->pool->release($mdb);
404 0 return Future->fail($err);
405 }
406  
407 # Sync the pipeline — callback fires when all results are in
408 0 my $f = Future->new;
409 $mdb->query('COMMIT', sub {
410 0 ▶ $mdb->query('SET autocommit=1', sub {}); # reset
Subroutine __ANON__: not called CC=3 SCAR=24.8
411 0 $self->pool->release($mdb);
412 0 ▶ if (ref $result && $result->can('then')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $result and $result->can('then')) { } 0 0 0
Condition0%0/3
ref $result and $result->can("then")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $result and $result->can("then")
ref $result $result->can("then")
Truth table0%0/3
A and B
A= ref $resultB= $result->can("then")
ABresult
0X0
100
111
413 0 ▶ $result->on_done(sub { $f->done(@_) })
Subroutine __ANON__: not called CC=1 SCAR=6.9
414 0 ▶ ->on_fail(sub { $f->fail(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
415 } else {
416 0 $f->done($result);
417 }
418 0 });
419  
420 0 return $f;
421 0 });
422 }
423  
424 # --- Sync Fallbacks ---
425 # These allow sync methods (->all, ->first etc.) to work
426 # by blocking the event loop. Useful for scripts/migrations.
427  
428 sub select {
429 1 ▶ my $self = shift;
Subroutine select: called CC=1 SCAR=0.0
Pod documented
430 1 return $self->select_async(@_)->get;
431 }
432  
433 sub select_single {
434 0 ▶ my $self = shift;
Subroutine select_single: not called CC=1 SCAR=6.9
Pod documented
435 0 return $self->select_single_async(@_)->get;
436 }
437  
438 sub insert {
439 0 ▶ my $self = shift;
Subroutine insert: not called CC=1 SCAR=6.9
Pod documented
440 0 return $self->insert_async(@_)->get;
441 }
442  
443 sub update {
444 0 ▶ my $self = shift;
Subroutine update: not called CC=1 SCAR=6.9
Pod documented
445 0 return $self->update_async(@_)->get;
446 }
447  
448 sub delete {
449 0 ▶ my $self = shift;
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
450 0 return $self->delete_async(@_)->get;
451 }
452  
453 sub txn_do {
454 0 ▶ my $self = shift;
Subroutine txn_do: not called CC=1 SCAR=6.9
Pod documented
455 0 return $self->txn_do_async(@_)->get;
456 }
457  
458 # --- Schema Integration ---
459  
460 0 ▶sub schema { $_[0]->{schema} }
Subroutine schema: not called CC=1 SCAR=6.9
Pod documented
461 0 ▶sub debug { $_[0]->{debug} }
Subroutine debug: not called CC=1 SCAR=6.9
Pod documented
462  
463 0 ▶sub connected { defined $_[0]->{pool} && $_[0]->pool->available > 0 }
Subroutine connected: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0]{"pool"} and $_[0]->pool->available > 0 0 0 0
464  
465  
466 0 ▶sub in_txn { 0 }
Subroutine in_txn: not called CC=1 SCAR=6.9
Pod documented
467  
468  
469 sub last_insert_id {
470 1 ▶ my ($self, $source, @cols) = @_;
Subroutine last_insert_id: called CC=1 SCAR=0.0
Pod documented
471 1 return $self->{_last_insert_id};
472 }
473  
474 sub disconnect {
475 8 ▶ my $self = shift;
Subroutine disconnect: called CC=2 SCAR=7.0
Pod documented
476 8 ▶ if ($self->{pool}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"pool"}) 8 0 8
477 8 $self->{pool}->shutdown;
478 8 $self->{pool} = undef;
479 }
480 }
481  
482 sub DESTROY {
483 10 ▶ my $self = shift;
Subroutine DESTROY: called CC=2 SCAR=6.9
484 10 ▶ $self->disconnect if $self->{pool};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"pool"} 8 2 10
485 }
486  
487 4 1;
488  
489 __END__
490  
491 =pod
492  
493 =encoding UTF-8
494  
495 =head1 NAME
496  
497 DBIO::MySQL::Async::Storage - Async MySQL/MariaDB storage driver using EV::MariaDB
498  
499 =head1 VERSION
500  
501 version 0.900000
502  
503 =head1 DESCRIPTION
504  
505 Implements L<DBIO::Storage::Async> using L<EV::MariaDB> — a non-blocking
506 MariaDB/MySQL client that speaks the MariaDB C client library directly.
507 No DBI, no DBD::MySQL, just raw MariaDB performance.
508  
509 Features:
510  
511 =over 4
512  
513 =item * Pipeline mode — batch queries in a single network round-trip
514  
515 =item * Prepared statement caching
516  
517 =item * Connection pooling with transaction pinning
518  
519 =back
520  
521 =head1 METHODS
522  
523 =head2 future_class
524  
525 Returns C<'Future'> — uses L<Future.pm|Future> from CPAN.
526  
527 =head2 connect_info
528  
529 $storage->connect_info([ \%conninfo, \%opts ]);
530  
531 Set connection parameters. C<%conninfo> is passed directly to
532 L<EV::MariaDB> as connection parameters (host, user, password, database, etc.).
533  
534 =head2 pool
535  
536 Returns the L<DBIO::MySQL::Async::Pool> connection pool.
537 Created lazily on first access.
538  
539 =head2 sql_maker
540  
541 Returns the L<DBIO::MySQL::SQLMaker> instance, configured for MySQL
542 (backtick quoting, MySQL C<LIMIT offset, rows> pagination).
543  
544 =head2 select_async
545  
546 my $future = $storage->select_async($source, $select, $where, $attrs);
547  
548 Execute a SELECT query asynchronously. Returns a L<Future> that
549 resolves with the result rows (arrayrefs).
550  
551 =head2 select_single_async
552  
553 Like L</select_async> but returns only the first row.
554  
555 =head2 insert_async
556  
557 my $future = $storage->insert_async($source, \%vals);
558  
559 =head2 update_async
560  
561 my $future = $storage->update_async($source, \%vals, \%where);
562  
563 =head2 delete_async
564  
565 my $future = $storage->delete_async($source, \%where);
566  
567 =head2 txn_do_async
568  
569 my $future = $storage->txn_do_async(sub {
570 my ($storage) = @_;
571 # All queries in here use the same connection
572 $storage->insert_async(...)->then(sub { ... });
573 });
574  
575 Acquires a connection from the pool, issues BEGIN, executes the
576 coderef, and issues COMMIT on success or ROLLBACK on Future failure.
577  
578 =head2 pipeline
579  
580 my $future = $storage->pipeline(sub {
581 my ($storage) = @_;
582 my @futures;
583 push @futures, $storage->insert_async('artist', { name => $_ })
584 for @names;
585 return Future->needs_all(@futures);
586 });
587  
588 Execute multiple queries in pipeline mode. All queries are batched
589 and sent in a single network round-trip for maximum throughput.
590 EV::MariaDB supports up to 64 pipelined queries.
591  
592 =head2 in_txn
593  
594 Returns true when invoked on a L<DBIO::MySQL::Async::TransactionContext>
595 (the coderef inside L</txn_do_async>), false on the storage itself.
596 Used by sub-queries that need to know whether the current connection
597 is pinned.
598  
599 =head2 last_insert_id
600  
601 my $id = $storage->last_insert_id;
602  
603 Returns the auto-increment value from the last INSERT performed
604 on this storage instance. Valid after L<insert_async|/insert_async>
605 or L<insert|/insert> succeeds.
606  
607 =head1 AUTHOR
608  
609 DBIO Authors
610  
611 =head1 COPYRIGHT AND LICENSE
612  
613 Copyright (C) 2026 DBIO Authors
614  
615 This is free software; you can redistribute it and/or modify it under
616 the same terms as the Perl 5 programming language system itself.
617  
618 =cut
↑ Summary Async/QueryExecutor.pm »