blib/lib/DBIO/MySQL/Storage/Async.pm

filter: Statementstmt 99.5% 404 / 406 Branchbran 79.1% 133 / 168 Conditioncond 63.4% 99 / 156 MC/DCmcdc 38.3% 33 / 86 Subroutinesub 100% 82 / 82 Podpod 100% 4 / 4 totaltotal 83.7% 755 / 902 CC 147 SCAR 20.0
CC
147
Cov
87%
_binary_binds
26.5
_auto_increment_column
16.3
_normalize_conninfo
14.4
CRAP 7.4
« blib/lib/DBIO/MySQL/SQLMaker.pm ↑ Summary blib/lib/DBIO/MySQL/Introspect.pm »
1 package DBIO::MySQL::Storage::Async;
2 # ABSTRACT: future_io async MySQL transport over the DBD driver's native async binding
3  
4 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 our $VERSION = '0.900004';
8  
9 27 ▶use base 'DBIO::Async::Storage';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 27 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 27 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 27 ▶use Future::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 27 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 27 ▶use DBIO::MySQL::SQLMaker;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 27 ▶use DBIO::MySQL::Storage ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 27 ▶use DBIO::MySQL::Storage::Async::Binary ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 27 ▶use DBIO::MySQL::MariaDBForkGuard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 27 ▶use DBIO::MySQL::DDL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 27 ▶use DBIO::MySQL::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 27 ▶use DBIO::MySQL::Identifier ();
Subroutine BEGIN: called CC=1 SCAR=0.0
23 27 ▶use DBIO::MySQL::NotifyTable;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 27 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26  
27 # --- Transport capabilities -------------------------------------------------
28  
29  
30 238 ▶sub transport_capabilities {
Subroutine transport_capabilities: called CC=1 SCAR=0.0
Pod documented
31 238 my $self = shift;
32 238 return ($self->next::method(@_), qw(listen notify broker_connect_options));
33 }
34  
35 # --- LISTEN / NOTIFY configuration ------------------------------------------
36  
37  
38 27 __PACKAGE__->mk_group_accessors(inherited => qw(
39 notify_table notify_poll_interval notify_lookback notify_retention
40 ));
41 {
42 27 my %defaults = DBIO::MySQL::NotifyTable->defaults;
43 27 __PACKAGE__->notify_table($defaults{table});
44 27 __PACKAGE__->notify_poll_interval($defaults{poll_interval});
45 27 __PACKAGE__->notify_lookback($defaults{lookback});
46 27 __PACKAGE__->notify_retention($defaults{retention});
47 }
48  
49 # --- SQL shaping seams ------------------------------------------------------
50  
51  
52 24 ▶sub sql_maker_class { 'DBIO::MySQL::SQLMaker' }
Subroutine sql_maker_class: called CC=1 SCAR=0.0
Pod documented
53  
54  
55 23 ▶sub _sql_maker_args {
Subroutine _sql_maker_args: called CC=1 SCAR=0.0
56 return (
57 23 quote_char => '`',
58 name_sep => '.',
59 );
60 }
61  
62  
63 59 ▶sub _run_crud {
Subroutine _run_crud: called CC=2 SCAR=7.0
64 59 my ($self, $op, $runner, @args) = @_;
65  
66 59 my $ident = $args[0];
67 57 ▶ my $typed = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 57 my ($sql, $bind) = @_;
69 57 return $runner->($sql, $self->_binary_binds($ident, $bind));
70 59 };
71  
72 # Only while the statement is built: the continuations the builder returns
73 # do not touch the maker.
74 local $self->{sql_maker} = $self->{_typed_sql_maker} ||=
75 59 ▶ $self->sql_maker_class->new($self->_sql_maker_args, bindtype => 'columns');
Condition66%2/3
$self->{"_typed_sql_maker"} ||= $self->sql_maker_class->new($self->_sql_maker_args, "bindtype", "columns")
l!l&&r!l&&!r
36230
MC/DC0%0/2
$self->{"_typed_sql_maker"} ||= $self->sql_maker_class->new($self->_sql_maker_args, "bindtype", "columns")
$self->{"_typed_sql_maker"} $self->sql_maker_class->new($self->_sql_maker_args, "bindtype", "columns")
Truth table66%2/3
A ||= B
A= $self->{"_typed_sql_maker"}B= $self->sql_maker_class->new($self->_sql_maker_args, "bindtype", "columns")
ABresult
1X1
011
000
76 59 return $self->SUPER::_run_crud($op, $typed, @args);
77 }
78  
79 # [ $column => $value ] pairs of the typed maker become the value, wrapped when
80 # the column is binary; anything else core already took apart stays as it is.
81 57 ▶sub _binary_binds {
Subroutine _binary_binds: called CC=14 SCAR=32.8
82 57 my ($self, $ident, $bind) = @_;
83  
84 57 ▶ my $owner = $self->can('_effective_owner_storage')
Branch: 1 branch
BranchTrueFalseTotal
$self->can('_effective_owner_storage') ? : 57 0 57
85 ? $self->_effective_owner_storage
86 : $self->_owner_storage;
87 57 my $colinfos;
88 my @out;
89 57 for my $b (@$bind) {
90 63 ▶ if (ref $b eq 'ARRAY' && @$b == 2 && defined $b->[0] && !ref $b->[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $b eq 'ARRAY' and @$b == 2 and defined $b->[0] and not ref $b->[0]) { } 63 0 63
Condition33%3/9
ref $b eq "ARRAY" and @$b == 2 and defined $b->[0] and not ref $b->[0]
!ll&&!rl&&r
0063
ref $b eq "ARRAY" and @$b == 2 and defined $b->[0]
!ll&&!rl&&r
0063
ref $b eq "ARRAY" and @$b == 2
!ll&&!rl&&r
0063
MC/DC0%0/4
ref $b eq "ARRAY" and @$b == 2 and defined $b->[0] and not ref $b->[0]
ref $b eq "ARRAY" @$b == 2 defined $b->[0] not ref $b->[0]
Truth table20%1/5
A and B and C and not(D)
A= ref $b eq "ARRAY"B= @$b == 2C= defined $b->[0]D= not ref $b->[0]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
91 63 my ($col, $value) = @$b;
92 63 my $type;
93 63 ▶ if (defined $value && !ref $value && $owner && ref $ident) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value and not ref $value and $owner and ref $ident) 63 0 63
Condition33%3/9
defined $value and not ref $value and $owner and ref $ident
!ll&&!rl&&r
0063
defined $value and not ref $value and $owner
!ll&&!rl&&r
0063
defined $value and not ref $value
!ll&&!rl&&r
0063
MC/DC0%0/4
defined $value and not ref $value and $owner and ref $ident
defined $value not ref $value $owner ref $ident
Truth table20%1/5
A and not(B) and C and D
A= defined $valueB= not ref $valueC= $ownerD= ref $ident
ABCDresult
0XXX0
10XX0
110X0
11100
11111
94 63 ▶ $colinfos ||= $owner->_resolve_column_info($ident);
Condition66%2/3
$colinfos ||= $owner->_resolve_column_info($ident)
l!l&&r!l&&!r
6570
MC/DC0%0/2
$colinfos ||= $owner->_resolve_column_info($ident)
$colinfos $owner->_resolve_column_info($ident)
Truth table66%2/3
A ||= B
A= $colinfosB= $owner->_resolve_column_info($ident)
ABresult
1X1
011
000
95 63 $type = $colinfos->{$col}{data_type};
96 }
97 63 ▶ push @out, (defined $type && DBIO::MySQL::Storage->bind_attribute_by_data_type($type))
Branch: 1 branch
BranchTrueFalseTotal
defined $type && 'DBIO::MySQL::Storage'->bind_attribute_by_data_type($type) ? : 3 60 63
Condition66%2/3
defined $type and "DBIO::MySQL::Storage"->bind_attribute_by_data_type($type)
!ll&&!rl&&r
0603
MC/DC50%1/2
defined $type and "DBIO::MySQL::Storage"->bind_attribute_by_data_type($type)
defined $type "DBIO::MySQL::Storage"->bind_attribute_by_data_type($type)
Truth table66%2/3
A and B
A= defined $typeB= "DBIO::MySQL::Storage"->bind_attribute_by_data_type($type)
ABresult
0X0
100
111
98 ? DBIO::MySQL::Storage::Async::Binary->new($value)
99 : $value;
100 }
101 else {
102 0 push @out, $b;
103 }
104 }
105 57 return \@out;
106 }
107  
108  
109 493 ▶sub _transform_sql { $_[1] }
Subroutine _transform_sql: called CC=1 SCAR=0.0
110  
111  
112 56 ▶sub _post_insert_sql { '' }
Subroutine _post_insert_sql: called CC=1 SCAR=0.0
113  
114 # --- INSERT returned-columns (last_insert_id, no RETURNING) ------------------
115  
116  
117 53 ▶sub _insert_returned_columns {
Subroutine _insert_returned_columns: called CC=5 SCAR=16.2
118 53 my ($self, $source, $to_insert, $affected) = @_;
119  
120 53 ▶ my %returned = %{ $to_insert || {} };
Condition50%1/2
$to_insert || {}
l!l
530
MC/DC0%0/1
$to_insert || {}
$to_insert
Truth table50%1/2
A
A= $to_insert
Aresult
11
00
121  
122 53 my $id = $self->{_statement_insert_id};
123 53 ▶ return \%returned unless defined $id && $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id and $id 16 37 53
Condition100%3/3
defined $id and $id
!ll&&!rl&&r
11537
MC/DC100%2/2
defined $id and $id
defined $id $id
Truth table100%3/3
A and B
A= defined $idB= $id
ABresult
0X0
100
111
124  
125 37 my $col = $self->_auto_increment_column($source, $to_insert);
126 37 ▶ return \%returned unless defined $col;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $col 0 37 37
127  
128 37 $returned{$col} = $id;
129 37 return \%returned;
130 }
131  
132 # Find the column the auto-increment id belongs in, matching the sync
133 # DBIO::Storage::DBI heuristic:
134 # - the first column flagged is_auto_increment, OR
135 # - the first PK column that was not supplied in $to_insert (no
136 # is_auto_increment flag on the schema, but MySQL still hands back an id
137 # because the PK is auto_increment server-side).
138 # Returns undef when neither applies (e.g. a table with no PK), leaving the
139 # hashref as the supplied insert data only.
140 37 ▶sub _auto_increment_column {
Subroutine _auto_increment_column: called CC=13 SCAR=27.9
141 37 my ($self, $source, $to_insert) = @_;
142 37 ▶ return unless Scalar::Util::blessed($source) && $source->can('columns');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($source) and $source->can("columns") 0 37 37
Condition33%1/3
builtin::blessed($source) and $source->can("columns")
!ll&&!rl&&r
0037
MC/DC0%0/2
builtin::blessed($source) and $source->can("columns")
builtin::blessed($source) $source->can("columns")
Truth table33%1/3
A and B
A= builtin::blessed($source)B= $source->can("columns")
ABresult
0X0
100
111
143  
144 37 ▶ my $col_infos = eval { $source->columns_info } || {};
Condition50%1/2
eval { do { $source->columns_info } } || {}
l!l
370
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
145 37 ▶ my @pks = $source->can('primary_columns') ? $source->primary_columns : ();
Branch: 1 branch
BranchTrueFalseTotal
$source->can('primary_columns') ? : 37 0 37
146 37 my %is_pk = map { $_ => 1 } @pks;
147  
148 37 my $first_autoinc;
149 my $first_pk_unsupplied;
150 37 for my $col ($source->columns) {
151 147 ▶ if (!defined $first_autoinc
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $first_autoinc and $col_infos->{$col}{"is_auto_increment"} // 0) 37 110 147
Condition60%3/5
not defined $first_autoinc and $col_infos->{$col}{"is_auto_increment"} // 0
!ll&&!rl&&r
110037
$col_infos->{$col}{"is_auto_increment"} // 0
l!l
370
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
152 && ($col_infos->{$col}{is_auto_increment} // 0)) {
153 37 $first_autoinc = $col;
154 }
155 147 ▶ if ($is_pk{$col}
Branch: 1 branch
BranchTrueFalseTotal
if ($is_pk{$col} and not defined $first_pk_unsupplied and not exists $to_insert->{$col}) 37 110 147
Condition66%4/6
$is_pk{$col} and not defined $first_pk_unsupplied and not exists $to_insert->{$col}
!ll&&!rl&&r
110037
$is_pk{$col} and not defined $first_pk_unsupplied
!ll&&!rl&&r
110037
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
156 && !defined $first_pk_unsupplied
157 && !exists $to_insert->{$col}) {
158 37 $first_pk_unsupplied = $col;
159 }
160 }
161  
162 37 ▶ return $first_autoinc if defined $first_autoinc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $first_autoinc 37 0 37
163 0 return $first_pk_unsupplied;
164 }
165  
166  
167 16 ▶sub last_insert_id { $_[0]->{_last_insert_id} }
Subroutine last_insert_id: called CC=1 SCAR=0.0
Pod documented
168  
169 # --- Connect-info seam ------------------------------------------------------
170  
171  
172 552 ▶sub _normalize_conninfo {
Subroutine _normalize_conninfo: called CC=14 SCAR=26.7
173 552 my ($self, $info) = @_;
174  
175 # DBI-form: [ 'dbi:mysql:...', $user, $pass, \%attrs, \%extra ] -> our
176 # conninfo shape. Both attribute hashes, the later winning, as
177 # DBIO::Storage::DBI::_normalize_connect_info merges them (karr k64): read
178 # from slot [3] alone, what the caller put in the fifth element reached the
179 # owner's connection and no pooled one.
180 552 ▶ if (ref $info eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info eq "ARRAY" and defined $info->[0] and not ref $info->[0] and $info->[0] =~ /^dbi:/i) 342 210 552
Condition55%5/9
ref $info eq "ARRAY" and defined $info->[0] and not ref $info->[0] and $info->[0] =~ /^dbi:/i
!ll&&!rl&&r
2100342
ref $info eq "ARRAY" and defined $info->[0] and not ref $info->[0]
!ll&&!rl&&r
0210342
ref $info eq "ARRAY" and defined $info->[0]
!ll&&!rl&&r
00552
MC/DC25%1/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 table40%2/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
181 && defined $info->[0] && !ref $info->[0] && $info->[0] =~ /^dbi:/i) {
182 342 my ($dsn, $user, $pass) = @$info;
183 342 my %dbd_attrs = map { %$_ } grep { ref $_ eq 'HASH' } @{$info}[3, 4];
184 # DBIO-private connect attributes (async, ignore_version, SQLMaker options) are
185 # already stripped centrally by core DBIO::Storage::Async::connect_info before
186 # this seam runs (core #66), from both hashes, so they never reach
187 # %dbd_attrs here.
188 342 my %conninfo = (
189 dsn => $dsn,
190 user => $user,
191 password => $pass,
192 attrs => \%dbd_attrs,
193 );
194 $conninfo{pool_size} = delete $conninfo{attrs}{pool_size}
195 342 ▶ if defined $conninfo{attrs}{pool_size};
Branch: 1 branch
BranchTrueFalseTotal
if defined $conninfo{"attrs"}{"pool_size"} 120 222 342
196 342 return [ \%conninfo, {} ];
197 }
198  
199 # Single-hash form (DBIx::Class heritage): [ { dsn, user, password, <DBD
200 # attributes>, pool_size } ], no second element. Every key but those is a
201 # DBD attribute, as the owning sync storage reads it (karr k66). The
202 # transport's own shape always has a second element ([ \%conninfo, \%opts ],
203 # a broker answer is wrapped that way), so a lone hash is the caller's.
204 210 ▶ if (ref $info eq 'ARRAY' && @$info == 1
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info eq "ARRAY" and @$info == 1 and ref $info->[0] eq "HASH" and not builtin::blessed($info->[0]) and defined $info->[0]{"dsn"} and not exists $info->[0]{"dbh_maker"}) 6 204 210
Condition80%12/15
ref $info eq "ARRAY" and @$info == 1 and ref $info->[0] eq "HASH" and not builtin::blessed($info->[0]) and defined $info->[0]{"dsn"} and not exists $info->[0]{"dbh_maker"}
!ll&&!rl&&r
20406
ref $info eq "ARRAY" and @$info == 1 and ref $info->[0] eq "HASH" and not builtin::blessed($info->[0]) and defined $info->[0]{"dsn"}
!ll&&!rl&&r
194106
ref $info eq "ARRAY" and @$info == 1 and ref $info->[0] eq "HASH" and not builtin::blessed($info->[0])
!ll&&!rl&&r
194016
ref $info eq "ARRAY" and @$info == 1 and ref $info->[0] eq "HASH"
!ll&&!rl&&r
6313116
ref $info eq "ARRAY" and @$info == 1
!ll&&!rl&&r
063147
MC/DC50%3/6
ref $info eq "ARRAY" and @$info == 1 and ref $info->[0] eq "HASH" and not builtin::blessed($info->[0]) and defined $info->[0]{"dsn"} and not exists $info->[0]{"dbh_maker"}
ref $info eq "ARRAY" @$info == 1 ref $info->[0] eq "HASH" not builtin::blessed($info->[0]) defined $info->[0]{"dsn"} not exists $info->[0]{"dbh_maker"}
Truth table57%4/7
A and B and C and not(D) and E and not(F)
A= ref $info eq "ARRAY"B= @$info == 1C= ref $info->[0] eq "HASH"D= not builtin::blessed($info->[0])E= defined $info->[0]{"dsn"}F= not exists $info->[0]{"dbh_maker"}
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
205 && ref $info->[0] eq 'HASH' && !Scalar::Util::blessed($info->[0])
206 && defined $info->[0]{dsn} && !exists $info->[0]{dbh_maker}) {
207 6 my %conninfo = %{ $info->[0] };
208 6 ▶ my %dbd_attrs = ( %{ delete $conninfo{attrs} || {} } );
Condition100%2/2
delete $conninfo{'attrs'} || {}
l!l
24
MC/DC100%1/1
delete $conninfo{'attrs'} || {}
delete $conninfo{'attrs'}
Truth table100%2/2
A
A= delete $conninfo{'attrs'}
Aresult
11
00
209 6 my %flat;
210 6 for my $key (grep { !/^(?:dsn|user|password|pool_size)$/ } keys %conninfo) {
211 8 $flat{$key} = delete $conninfo{$key};
212 }
213 6 $conninfo{attrs} = { %flat, %dbd_attrs };
214 6 return [ \%conninfo, {} ];
215 }
216  
217 # Broker arrayref [ $broker ], or an already-normalised [ \%conninfo, \%opts ]
218 # pair -- leave for the inherited broker detection / normalisation.
219 204 return $info;
220 }
221  
222 # --- Pool connection lifecycle ----------------------------------------------
223  
224  
225 313 ▶sub _create_pool_connection {
Subroutine _create_pool_connection: called CC=1 SCAR=0.0
226 313 my ($self, $conninfo) = @_;
227 313 return $self->_mysql_connect($conninfo);
228 }
229  
230 # The one DBI->connect of this transport: a pool connection
231 # (_create_pool_connection) and the dedicated listen connection (_listen_open,
232 # which passes $force_no_reconnect). The forced attributes win over the
233 # caller's.
234 #
235 # The auto-reconnect attribute is named here and nowhere earlier, for the DBD
236 # of the DSN this connection is opened with (karr k60): this is the first point
237 # where that DSN is known -- with an AccessBroker it is the broker's answer for
238 # this one connection. Off unless the connect attributes set it under that
239 # DBD's name; off regardless when forced.
240 498 ▶sub _mysql_connect {
Subroutine _mysql_connect: called CC=10 SCAR=23.1
241 498 my ($self, $conninfo, $force_no_reconnect) = @_;
242  
243 croak 'MySQL async conninfo must be a hashref with a dsn'
244 498 ▶ unless ref $conninfo eq 'HASH' && defined $conninfo->{dsn};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $conninfo eq "HASH" and defined $conninfo->{"dsn"} 52 446 498
Condition66%2/3
ref $conninfo eq "HASH" and defined $conninfo->{"dsn"}
!ll&&!rl&&r
520446
MC/DC50%1/2
ref $conninfo eq "HASH" and defined $conninfo->{"dsn"}
ref $conninfo eq "HASH" defined $conninfo->{"dsn"}
Truth table66%2/3
A and B
A= ref $conninfo eq "HASH"B= defined $conninfo->{"dsn"}
ABresult
0X0
100
111
245  
246 446 ▶ my %attrs = %{ $conninfo->{attrs} || {} };
Condition100%2/2
$conninfo->{'attrs'} || {}
l!l
4379
MC/DC100%1/1
$conninfo->{'attrs'} || {}
$conninfo->{'attrs'}
Truth table100%2/2
A
A= $conninfo->{'attrs'}
Aresult
11
00
247 446 my $reconnect = $self->_mysql_auto_reconnect_attr($conninfo->{dsn});
248 440 ▶ $attrs{$reconnect} = 0 if $force_no_reconnect || !exists $attrs{$reconnect};
Branch: 1 branch
BranchTrueFalseTotal
if $force_no_reconnect or not exists $attrs{$reconnect} 424 16 440
Condition100%3/3
$force_no_reconnect or not exists $attrs{$reconnect}
l!l&&r!l&&!r
13129316
MC/DC100%2/2
$force_no_reconnect or not exists $attrs{$reconnect}
$force_no_reconnect not exists $attrs{$reconnect}
Truth table100%3/3
A or not(B)
A= $force_no_reconnectB= not exists $attrs{$reconnect}
ABresult
1X1
011
000
249  
250 my @info = (
251 $conninfo->{dsn},
252 $conninfo->{user},
253 $conninfo->{password},
254 {
255 440 %attrs,
256 AutoCommit => 1,
257 RaiseError => 1,
258 PrintError => 0,
259 AutoInactiveDestroy => 1,
260 },
261 );
262  
263 # Never the password in what a failed connect says (karr k76, core k297).
264 # Under RaiseError DBI dies itself, quoting the DSN it was given, which may
265 # carry password=...; DBI's message goes on as it came, its location
266 # included, hence die. The croak is left for a connect that answered false:
267 # a HandleError among the attributes took the error.
268 440 my ($dbh, $ok, $err);
269 {
270 440 local $@;
271 440 $ok = eval { $dbh = DBI->connect(@info); 1 };
272 440 $err = $@;
273 }
274 440 ▶ die DBIO::Util::mask_connect_secrets($err, \@info) unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 440 440
275 440 ▶ croak DBIO::Util::mask_connect_secrets("DBI connect failed: $DBI::errstr", \@info)
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 0 440 440
276 unless $dbh;
277  
278 # The server's version string, kept once while the connection is healthy:
279 # _conn_error_is_fatal needs the product for 4031 and must not ask a dead
280 # connection anything. The handshake delivered it, so this is no round trip.
281 # A forked child's exit must not close this process's connections
282 # (karr k56, ADR 0009).
283 440 ▶ DBIO::MySQL::MariaDBForkGuard->install if ($dbh->{Driver}{Name} // '') eq 'MariaDB';
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh->{"Driver"}{"Name"} // "") eq "MariaDB" 193 247 440
Condition100%2/2
$dbh->{"Driver"}{"Name"} // ""
l!l
40139
MC/DC100%1/1
$dbh->{"Driver"}{"Name"} // ""
$dbh->{"Driver"}{"Name"}
Truth table100%2/2
A
A= $dbh->{"Driver"}{"Name"}
Aresult
11
00
284  
285 # The session of every connection of this transport, the listen one
286 # included, is the one the synchronous storage gives its own
287 # (STRICT_ALL_TABLES; karr k121). Before the pool replays on_connect_do and
288 # on_connect_call, as the synchronous storage does.
289 440 DBIO::MySQL::Storage::session_setup($dbh);
290  
291 440 my $conn = { dbh => $dbh };
292 440 $self->_mysql_serverinfo($conn);
293  
294 440 return $conn;
295 }
296  
297  
298 194 ▶sub _shutdown_pool_connection {
Subroutine _shutdown_pool_connection: called CC=4 SCAR=14.0
299 194 my ($self, $conn) = @_;
300 194 ▶ my $dbh = $conn->{dbh} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $conn->{"dbh"} or return 194 0 194
301 194 ▶ if ($self->_conn_known_dead($conn)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_conn_known_dead($conn)) 33 65 98
302 33 $dbh->{Warn} = 0;
303 33 $self->_silence_statement($conn->{sth});
304 }
305 98 ▶ $dbh->disconnect if $dbh->{Active};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"Active"} 98 0 98
306 }
307  
308 # A statement handle of a connection known to be dead: its DESTROY could only
309 # report the loss once more, as "(in cleanup) ... st DESTROY failed".
310 43 ▶sub _silence_statement {
Subroutine _silence_statement: called CC=2 SCAR=6.9
311 43 my ($self, $sth) = @_;
312 43 ▶ return unless $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 8 35 43
313 35 $sth->{RaiseError} = 0;
314 35 $sth->{PrintError} = 0;
315 35 return;
316 }
317  
318 # --- DBD-specific async primitives ------------------------------------------
319 # These are the ONLY places the transport asks for a DBD driver's async
320 # binding (the five below, plus _mysql_auto_reconnect_attr for the pool and
321 # listen connections). None of them spells a name: each asks
322 # DBIO::MySQL::Storage's table for the DBD of the connection in hand -- the
323 # connection, not this class, is what has a DBD (karr k60). Everything else in
324 # the transport (submit / collect control flow, pooling, txn) is DBD-agnostic.
325  
326 # The name $conn's DBD gives $what. The DBD is the handle's own, read once and
327 # kept on $conn. _submit_query asks first, before the connection's first
328 # statement, so no handle attribute is fetched with a statement in flight.
329 3509 ▶sub _mysql_binding {
Subroutine _mysql_binding: called CC=3 SCAR=11.0
330 3509 my ($self, $conn, $what) = @_;
331 3509 ▶ $conn->{_mysql_dbd} //= do {
Condition100%3/3
$conn->{"_mysql_dbd"} //= do { my $dbh = $conn->{"dbh"}; $dbh and $dbh->{"Driver"}{"Name"} }
l!l&&r!l&&!r
295751339
MC/DC100%2/2
$conn->{"_mysql_dbd"} //= do { my $dbh = $conn->{"dbh"}; $dbh and $dbh->{"Driver"}{"Name"} }
$conn->{"_mysql_dbd"} do { my $dbh = $conn->{"dbh"}; $dbh and $dbh->{"Driver"}{"Name"} }
Truth table100%3/3
A //= B
A= $conn->{"_mysql_dbd"}B= do { my $dbh = $conn->{"dbh"}; $dbh and $dbh->{"Driver"}{"Name"} }
ABresult
1X1
011
000
332 552 my $dbh = $conn->{dbh};
333 552 ▶ $dbh && $dbh->{Driver}{Name};
Condition66%2/3
$dbh && $dbh->{'Driver'}{'Name'}
!ll&&!rl&&r
039513
MC/DC50%1/2
$dbh && $dbh->{'Driver'}{'Name'}
$dbh $dbh->{'Driver'}{'Name'}
Truth table66%2/3
A && B
A= $dbhB= $dbh->{'Driver'}{'Name'}
ABresult
0X0
100
111
334 };
335 3509 return DBIO::MySQL::Storage->_dbd_attribute($conn->{_mysql_dbd}, $what);
336 }
337  
338 # The version string the server sent in its handshake (mysql_serverinfo /
339 # mariadb_serverinfo), read once and kept on $conn. A client-side string: no
340 # statement, no round trip, safe on a dead connection too. It carries the
341 # product ("11.8.8-MariaDB-ubu2404" vs "8.0.30"), the same string
342 # SELECT VERSION() answers. undef when the handle cannot say.
343 472 ▶sub _mysql_serverinfo {
Subroutine _mysql_serverinfo: called CC=3 SCAR=11.2
344 472 my ($self, $conn) = @_;
345 472 ▶ return $conn->{_mysql_serverinfo} if exists $conn->{_mysql_serverinfo};
Branch: 1 branch
BranchTrueFalseTotal
if exists $conn->{"_mysql_serverinfo"} 0 472 472
346 472 ▶ my $dbh = $conn->{dbh} or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $conn->{"dbh"} or return undef 472 0 472
347 472 my $info = eval { $dbh->{ $self->_mysql_binding($conn, 'serverinfo') } };
348 472 return $conn->{_mysql_serverinfo} = $info;
349 }
350  
351  
352 497 ▶sub _async_prepare_attrs {
Subroutine _async_prepare_attrs: called CC=1 SCAR=0.0
353 497 my ($self, $conn) = @_;
354 497 return { $self->_mysql_binding($conn, 'async') => 1 };
355 }
356  
357  
358 1217 ▶sub _conn_socket_fd {
Subroutine _conn_socket_fd: called CC=1 SCAR=0.0
359 1217 my ($self, $conn) = @_;
360 1217 my $method = $self->_mysql_binding($conn, 'socket_fd');
361 1217 return $conn->{dbh}->$method;
362 }
363  
364  
365 471 ▶sub _async_ready {
Subroutine _async_ready: called CC=1 SCAR=0.0
366 471 my ($self, $conn) = @_;
367 471 my $method = $self->_mysql_binding($conn, 'async_ready');
368 471 return $conn->{sth}->$method;
369 }
370  
371  
372 460 ▶sub _async_result {
Subroutine _async_result: called CC=1 SCAR=0.0
373 460 my ($self, $conn) = @_;
374 460 my $method = $self->_mysql_binding($conn, 'async_result');
375 460 return $conn->{sth}->$method;
376 }
377  
378  
379 392 ▶sub _async_insertid {
Subroutine _async_insertid: called CC=1 SCAR=0.0
380 392 my ($self, $conn) = @_;
381 392 return $conn->{sth}{ $self->_mysql_binding($conn, 'insertid') };
382 }
383  
384  
385 454 ▶sub _mysql_auto_reconnect_attr {
Subroutine _mysql_auto_reconnect_attr: called CC=3 SCAR=11.1
386 454 my ($self, $dsn) = @_;
387 454 ▶ my ($dbd) = ($dsn // '') =~ /^dbi:(\w*)/i;
Condition50%1/2
$dsn // ""
l!l
4540
MC/DC0%0/1
$dsn // ""
$dsn
Truth table50%1/2
A
A= $dsn
Aresult
11
00
388 # A DSN without a driver part names no DBD: core refuses it (karr k340),
389 # and DBI_DRIVER does not stand in. _dbd_attribute throws for undef.
390 454 ▶ return DBIO::MySQL::Storage->_dbd_attribute(
Branch: 1 branch
BranchTrueFalseTotal
length $dbd ? : 450 4 454
391 ( length $dbd ? $dbd : undef ), 'auto_reconnect',
392 );
393 }
394  
395 # --- Readiness / socket seams -----------------------------------------------
396  
397  
398 422 ▶sub _conn_ready {
Subroutine _conn_ready: called CC=3 SCAR=13.1
399 422 my ($self, $conn) = @_;
400 422 ▶ return $conn->{dbh} && $conn->{dbh}{Active} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$conn->{'dbh'} && $conn->{'dbh'}{'Active'} ? : 422 0 422
Condition33%1/3
$conn->{"dbh"} and $conn->{"dbh"}{"Active"}
!ll&&!rl&&r
00422
MC/DC0%0/2
$conn->{"dbh"} and $conn->{"dbh"}{"Active"}
$conn->{"dbh"} $conn->{"dbh"}{"Active"}
Truth table33%1/3
A and B
A= $conn->{"dbh"}B= $conn->{"dbh"}{"Active"}
ABresult
0X0
100
111
401 }
402  
403  
404 225 ▶sub _conn_fileno {
Subroutine _conn_fileno: called CC=1 SCAR=0.0
405 225 my ($self, $conn) = @_;
406 225 return $self->_conn_socket_fd($conn);
407 }
408  
409 # --- Query submit / collect -------------------------------------------------
410  
411  
412 497 ▶sub _submit_query {
Subroutine _submit_query: called CC=7 SCAR=19.5
413 497 my ($self, $conn, $sql, $bind) = @_;
414 497 ▶ $bind //= [];
Condition50%1/2
$bind //= []
l!l
4970
MC/DC0%0/1
$bind //= []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
415  
416 497 my $dbh = $conn->{dbh};
417  
418 497 my $old = delete $conn->{sth};
419 497 ▶ $self->_silence_statement($old) if $old && $self->_conn_known_dead($conn);
Branch: 1 branch
BranchTrueFalseTotal
if $old and $self->_conn_known_dead($conn) 1 496 497
Condition100%3/3
$old and $self->_conn_known_dead($conn)
!ll&&!rl&&r
2422541
MC/DC100%2/2
$old and $self->_conn_known_dead($conn)
$old $self->_conn_known_dead($conn)
Truth table100%3/3
A and B
A= $oldB= $self->_conn_known_dead($conn)
ABresult
0X0
100
111
420 497 undef $old;
421  
422 497 delete @$conn{qw(_error_binds _error_sql)};
423  
424 497 my $values = $self->_error_values($bind);
425 497 my $sth = eval { $dbh->prepare($sql, $self->_async_prepare_attrs($conn)) };
426 497 ▶ unless ($sth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth) 7 490 497
427 7 my $error = $@;
428 7 die $self->_masked_error($error, $values, $sql);
429 }
430 490 ▶ unless (eval { $self->_bind_and_execute($dbh, $sth, $bind); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $self->_bind_and_execute($dbh, $sth, $bind); 1 } }) 14 476 490
431 14 my $error = $@;
432 14 ▶ $self->_silence_statement($sth) if $self->_conn_known_dead($conn);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_conn_known_dead($conn) 9 5 14
433 14 die $self->_masked_error($error, $values, $sql);
434 }
435 476 $conn->{sth} = $sth;
436  
437 # What the collector needs to take the values out of an error: plain copies,
438 # dropped when the result is finalised.
439 476 @$conn{qw(_error_binds _error_sql)} = ($values, $sql);
440  
441 476 return;
442 }
443  
444  
445 493 ▶sub _bind_and_execute {
Subroutine _bind_and_execute: called CC=7 SCAR=19.5
446 493 my ($self, $dbh, $sth, $bind) = @_;
447  
448 493 my (@attrs, @pairs);
449 493 for my $value (@$bind) {
450 484 ▶ my $binary = Scalar::Util::blessed($value)
Condition66%2/3
builtin::blessed($value) && $value->isa('DBIO::MySQL::Storage::Async::Binary')
!ll&&!rl&&r
48202
MC/DC50%1/2
builtin::blessed($value) && $value->isa('DBIO::MySQL::Storage::Async::Binary')
builtin::blessed($value) $value->isa('DBIO::MySQL::Storage::Async::Binary')
Truth table66%2/3
A && B
A= builtin::blessed($value)B= $value->isa('DBIO::MySQL::Storage::Async::Binary')
ABresult
0X0
100
111
451 && $value->isa('DBIO::MySQL::Storage::Async::Binary');
452 484 ▶ push @attrs, $binary ? { TYPE => DBI::SQL_BINARY() } : undef;
Branch: 1 branch
BranchTrueFalseTotal
$binary ? : 2 482 484
453 484 ▶ push @pairs, [ {}, $binary ? $value->value : $value ];
Branch: 1 branch
BranchTrueFalseTotal
$binary ? : 2 482 484
454 }
455 493 my $adjusted = DBIO::MySQL::Storage::text_binds($dbh, \@pairs, \@attrs);
456  
457 # A type is a bind_param; without one the values go with the execute, as
458 # they always did.
459 493 ▶ return $sth->execute(map { $_->[1] } @$adjusted) unless grep { $_ } @attrs;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_;} @attrs 491 2 493
460  
461 2 $sth->bind_param($_ + 1, $adjusted->[$_][1], $attrs[$_]) for 0 .. $#pairs;
462 2 return $sth->execute;
463 }
464  
465  
466 471 ▶sub _collect_result {
Subroutine _collect_result: called CC=7 SCAR=19.5
467 471 my ($self, $conn) = @_;
468  
469 471 my $sth = $conn->{sth};
470  
471 # The result may not have fully arrived on the first readable event.
472 # _async_ready reports whether _async_result will not block; if not, wait for
473 # more and re-check rather than blocking.
474 471 ▶ unless ($self->_async_ready($conn)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_async_ready($conn)) 11 460 471
475 11 ▶ return $self->_await_readable($conn)->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
476 11 return $self->_collect_result($conn);
477 11 });
478 }
479  
480 # The values and the statement of this one query; with them gone nothing
481 # pins the caller's binds past the result.
482 460 my ($values, $sql) = delete @$conn{qw(_error_binds _error_sql)};
483  
484 460 my @out;
485 460 my $ok = eval {
486 460 my $rv = $self->_async_result($conn); # finalise; dies (RaiseError) on error
487  
488 # The insertid of THIS statement, for insert_async's returned-columns hashref
489 # (MySQL has no RETURNING). Taken for every statement, zero included: a
490 # statement that generated no id must not leave an earlier one's in place for
491 # _insert_returned_columns to pick up (karr k69). last_insert_id is the other
492 # value -- the last id a statement generated -- so there a zero is skipped.
493 392 my $iid = $self->_async_insertid($conn);
494 392 $self->{_statement_insert_id} = $iid;
495 392 ▶ $self->{_last_insert_id} = $iid if $iid;
Branch: 1 branch
BranchTrueFalseTotal
if $iid 44 348 392
496  
497 392 my @rows;
498 392 ▶ my $has_rows = ($sth->{NUM_OF_FIELDS} || 0) > 0;
Condition100%2/2
$sth->{'NUM_OF_FIELDS'} || 0
l!l
275117
MC/DC100%1/1
$sth->{'NUM_OF_FIELDS'} || 0
$sth->{'NUM_OF_FIELDS'}
Truth table100%2/2
A
A= $sth->{'NUM_OF_FIELDS'}
Aresult
11
00
499 392 ▶ @rows = @{ $sth->fetchall_arrayref } if $has_rows;
Branch: 1 branch
BranchTrueFalseTotal
if $has_rows 275 117 392
500  
501 # A statement may have produced more result sets (a CALL). The client
502 # library refuses every further statement on the connection while they are
503 # unread; read them out, discarded (karr k73).
504 392 $self->_drain_more_results($conn);
505  
506 391 ▶ @out = $has_rows ? @rows : ($rv);
Branch: 1 branch
BranchTrueFalseTotal
$has_rows ? : 274 117 391
507 391 1;
508 };
509 460 ▶ unless ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 69 391 460
510 69 my $error = $@;
511 69 die $self->_masked_error($error, $values, $sql);
512 }
513  
514 391 return $self->future_class->done(@out);
515 }
516  
517  
518 392 ▶sub _drain_more_results {
Subroutine _drain_more_results: called CC=4 SCAR=13.9
519 392 my ($self, $conn) = @_;
520 392 my $sth = $conn->{sth};
521 392 ▶ my $ok = eval { 1 while $sth->more_results; 1 };
Branch: 1 branch
BranchTrueFalseTotal
$sth->more_results and do { '???' } 3 391 394
522 392 ▶ return if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 391 1 392
523 1 ▶ my $error = $@ || 'reading the remaining result sets failed';
Condition50%1/2
$@ || 'reading the remaining result sets failed'
l!l
10
MC/DC0%0/1
$@ || 'reading the remaining result sets failed'
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
524 1 $conn->{_unread_results_failed} = 1;
525 1 die $error;
526 }
527  
528  
529 90 ▶sub _mask_error_values {
Subroutine _mask_error_values: called CC=4 SCAR=14.0
530 90 my ($self, $text, $values, $statement) = @_;
531  
532 90 ▶ return $text unless defined $text and !ref $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and not ref $text 2 88 90
Condition66%2/3
defined $text and not ref $text
!ll&&!rl&&r
0288
MC/DC50%1/2
defined $text and not ref $text
defined $text not ref $text
Truth table66%2/3
A and not(B)
A= defined $textB= not ref $text
ABresult
0X0
100
111
533  
534 88 my $masked = eval {
535 88 DBIO::Util::mask_bound_values(
536 DBIO::MySQL::Util::mask_error_details($text, $values, $statement),
537 $values,
538 );
539 };
540  
541 88 ▶ return defined $masked ? $masked : $text;
Branch: 1 branch
BranchTrueFalseTotal
defined $masked ? : 88 0 88
542 }
543  
544 # The values of a bind list as the hook takes them: a binary one is its value,
545 # undef and a reference are left out.
546 497 ▶sub _error_values {
Subroutine _error_values: called CC=7 SCAR=22.5
547 497 my ($self, $bind) = @_;
548  
549 497 my @plain;
550 497 ▶ for my $given (@{ $bind || [] }) {
Condition50%1/2
$bind || []
l!l
4970
MC/DC0%0/1
$bind || []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
551 500 ▶ my $value = Scalar::Util::blessed($given) && $given->isa('DBIO::MySQL::Storage::Async::Binary')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($given) && $given->isa('DBIO::MySQL::Storage::Async::Binary') ? : 0 500 500
Condition33%1/3
builtin::blessed($given) and $given->isa("DBIO::MySQL::Storage::Async::Binary")
!ll&&!rl&&r
50000
MC/DC0%0/2
builtin::blessed($given) and $given->isa("DBIO::MySQL::Storage::Async::Binary")
builtin::blessed($given) $given->isa("DBIO::MySQL::Storage::Async::Binary")
Truth table33%1/3
A and B
A= builtin::blessed($given)B= $given->isa("DBIO::MySQL::Storage::Async::Binary")
ABresult
0X0
100
111
552 ? $given->value
553 : $given;
554 500 ▶ push @plain, "$value" if defined $value && !ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value and not ref $value 500 0 500
Condition33%1/3
defined $value and not ref $value
!ll&&!rl&&r
00500
MC/DC0%0/2
defined $value and not ref $value
defined $value not ref $value
Truth table33%1/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
555 }
556  
557 497 return \@plain;
558 }
559  
560 # An error as it leaves the transport: text masked, an object as it is.
561 90 ▶sub _masked_error {
Subroutine _masked_error: called CC=1 SCAR=0.0
562 90 my ($self, $error, $values, $sql) = @_;
563 90 return $self->_mask_error_values($error, $values, $sql);
564 }
565  
566 # --- Dead-connection seams --------------------------------------------------
567  
568  
569 # The client library's numbers for a lost connection (errmsg.h, the same in
570 # MySQL's libmysqlclient and in MariaDB Connector/C). Both DBDs pass the
571 # library's mysql_errno() through as the handle's err -- do_error() in
572 # DBD::mysql 4.055, mariadb_dr_do_error() in DBD::MariaDB 1.24, from
573 # mysql_db_async_result() / mariadb_db_async_result() and from execute -- and
574 # test the first two themselves before an auto-reconnect (mysql_db_reconnect(),
575 # mariadb_db_reconnect()).
576 #
577 # Add a number only with a measurement, or a vendor statement, that the
578 # connection is unusable after it; the POD above says what was measured, and
579 # what is missing for 1053, 1927 and 2014. 4031 is answered by product, below.
580 27 my %CONN_LOST_ERR = map { $_ => 1 } (
581 2006, # CR_SERVER_GONE_ERROR
582 2013, # CR_SERVER_LOST
583 2026, # CR_SSL_CONNECTION_ERROR: the same loss, over TLS (measured, karr k65)
584 2055, # CR_SERVER_LOST_EXTENDED (text only in both libraries, number 2013)
585 );
586  
587 187 ▶sub _conn_error_is_fatal {
Subroutine _conn_error_is_fatal: called CC=1 SCAR=0.0
588 187 my ($self, $conn) = @_;
589 187 return $self->_conn_known_dead($conn);
590 }
591  
592 # The answer of _conn_error_is_fatal, from memory. The quiet-drop paths ask it
593 # here, so that the seam itself is asked by the base alone (karr k74).
594 650 ▶sub _conn_known_dead {
Subroutine _conn_known_dead: called CC=11 SCAR=24.0
595 650 my ($self, $conn) = @_;
596  
597 # No handle: nothing to send a statement on. Answered, not thrown -- the
598 # base would count a throw as dead too, but report it as a broken seam.
599 650 ▶ my $dbh = ref $conn eq 'HASH' ? $conn->{dbh} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $conn eq 'HASH' ? : 645 5 650
600 650 ▶ return 1 unless $dbh;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh 9 641 650
601  
602 # Result sets that could not be read out leave the protocol in an unknown
603 # state (karr k73).
604 641 ▶ return 1 if $conn->{_unread_results_failed};
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"_unread_results_failed"} 1 640 641
605  
606 640 my $err = $dbh->err;
607 544 ▶ return 0 unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 329 215 544
608 215 ▶ return 1 if $CONN_LOST_ERR{$err};
Branch: 1 branch
BranchTrueFalseTotal
if $CONN_LOST_ERR{$err} 105 110 215
609  
610 # 4031 is the server closing an idle session on MySQL 8.0.24+ only.
611 110 ▶ if ($err == 4031) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err == 4031) 32 78 110
612 32 my $info = $self->_mysql_serverinfo($conn);
613 32 ▶ return 0 unless defined $info && $info !~ /mariadb/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $info and not $info =~ /mariadb/i 12 20 32
Condition100%3/3
defined $info and not $info =~ /mariadb/i
!ll&&!rl&&r
4820
MC/DC100%2/2
defined $info and not $info =~ /mariadb/i
defined $info not $info =~ /mariadb/i
Truth table100%3/3
A and not(B)
A= defined $infoB= not $info =~ /mariadb/i
ABresult
0X0
100
111
614 20 ▶ my ($maj, $min, $pat) = $info =~ /^(\d+)\.(\d+)\.(\d+)/ or return 0;
Branch: 1 branch
BranchTrueFalseTotal
() = $info =~ /^(\d+)\.(\d+)\.(\d+)/ or return 0 20 0 20
615 20 ▶ return ($maj * 1_000_000 + $min * 1_000 + $pat) >= 8_000_024 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$maj * 1000000 + $min * 1000 + $pat >= 8000024 ? : 12 8 20
616 }
617  
618 78 return 0;
619 }
620  
621  
622 1004 ▶sub _idle_connection_alive {
Subroutine _idle_connection_alive: called CC=9 SCAR=22.0
623 1004 my ($self, $conn) = @_;
624  
625 1004 ▶ my $dbh = ref $conn eq 'HASH' ? $conn->{dbh} : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $conn eq 'HASH' ? : 1000 4 1004
626 1004 ▶ return 0 unless $dbh && $dbh->{Active};
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh and $dbh->{"Active"} 12 992 1004
Condition100%3/3
$dbh and $dbh->{"Active"}
!ll&&!rl&&r
84992
MC/DC100%2/2
$dbh and $dbh->{"Active"}
$dbh $dbh->{"Active"}
Truth table100%3/3
A and B
A= $dbhB= $dbh->{"Active"}
ABresult
0X0
100
111
627  
628 # undef once the client library has no socket (pmysql->net.fd of -1 in
629 # DBD::mysql 4.055, imp_dbh->sock_fd below zero in DBD::MariaDB 1.24).
630 # Guarded before vec, which warns about undef and dies on a negative offset.
631 992 my $fd = $self->_conn_socket_fd($conn);
632 992 ▶ return 0 unless defined $fd && $fd =~ /\A[0-9]+\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fd and $fd =~ /\A[0-9]+\z/ 12 980 992
Condition100%3/3
defined $fd and $fd =~ /\A[0-9]+\z/
!ll&&!rl&&r
48980
MC/DC100%2/2
defined $fd and $fd =~ /\A[0-9]+\z/
defined $fd $fd =~ /\A[0-9]+\z/
Truth table100%3/3
A and B
A= defined $fdB= $fd =~ /\A[0-9]+\z/
ABresult
0X0
100
111
633  
634 # select only reports: what the server sent stays on the socket. A zero
635 # timeout cannot wait, but it can be interrupted -- the kernel answers EINTR
636 # for a pending signal when nothing is readable -- which says nothing about
637 # the socket, so ask again.
638 980 my $rin = '';
639 980 vec($rin, $fd, 1) = 1;
640 980 my $found;
641 996 do { $found = select(my $rout = $rin, undef, undef, 0) }
642 980 ▶ while $found < 0 && $!{EINTR};
Branch: 1 branch
BranchTrueFalseTotal
$found < 0 and $!{"EINTR"} and do { do { $found = select(my $rout = $rin, undef, undef, 0) } } 16 980 996
Condition100%3/3
$found < 0 and $!{"EINTR"}
!ll&&!rl&&r
972816
MC/DC100%2/2
$found < 0 and $!{"EINTR"}
$found < 0 $!{"EINTR"}
Truth table100%3/3
A and B
A= $found < 0B= $!{"EINTR"}
ABresult
0X0
100
111
643  
644 # Readable (> 0) is dead; so is a select that failed (< 0, the fd is not
645 # open). Only "nothing to read" is alive.
646 980 ▶ return $found == 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$found == 0 ? : 920 60 980
647 }
648  
649 # --- LISTEN / NOTIFY seams (emulated) ---------------------------------------
650 #
651 # Core DBIO::Storage::Async owns the contract and the machinery (ADR 0056);
652 # DBIO::Async::Storage owns the Future::IO watch loop, the timer and the pooled
653 # _notify_send (dbio-async ADR 0005). What is left is MySQL's: the dedicated
654 # handle and the notify-table emulation, whose SQL and state are
655 # DBIO::MySQL::NotifyTable's. Private helpers are named _mysql_*, never
656 # _listen_* or _watch_*: they share an inheritance chain with core's and
657 # dbio-async's privates, and a same-named method appearing there later would be
658 # shadowed.
659 #
660 # One async statement per MySQL connection: a second one, or a ping, while one
661 # is in flight dies in both DBDs ("Calling a synchronous function on an
662 # asynchronous handle"). Every statement on the listen connection therefore
663 # goes through _mysql_listen_start, which records the one in flight in
664 # {_mysql_busy}: a drain skips its tick while a subscribe's statement runs, a
665 # subscribe waits for the drain in flight, and the probe does not ping over a
666 # statement in flight.
667  
668  
669 367 ▶sub _listen_open {
Subroutine _listen_open: called CC=2 SCAR=6.9
670 367 my ($self) = @_;
671 367 my $notify = $self->_mysql_notify_table;
672 366 my $provider = $self->_conninfo_provider;
673 366 ▶ my $conninfo = $provider ? $provider->() : $self->{_conninfo};
Branch: 1 branch
BranchTrueFalseTotal
$provider ? : 286 80 366
674 182 my $conn = $self->_mysql_connect($conninfo, 1); # auto-reconnect forced off
675 130 $conn->{_mysql_notify} = $notify;
676 130 return $self->future_class->done($conn);
677 }
678  
679  
680 61 ▶sub _listen_close {
Subroutine _listen_close: called CC=4 SCAR=14.0
681 61 my ($self, $conn) = @_;
682 61 ▶ my $dbh = delete $conn->{dbh} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = delete $conn->{"dbh"} or return 61 0 61
683 61 $dbh->{Warn} = 0; # an abandoned poll is an active statement handle
684 61 ▶ $dbh->disconnect if $dbh->{Active};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"Active"} 61 0 61
685 # The last statement handle goes only now, and quietly: on a dead
686 # connection its DESTROY would report the loss once more, as a warning.
687 61 ▶ if (my $sth = delete $conn->{sth}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sth = delete $conn->{"sth"}) 56 5 61
688 56 $sth->{RaiseError} = 0;
689 56 $sth->{PrintError} = 0;
690 }
691 61 return;
692 }
693  
694  
695 66 ▶sub _listen_subscribe {
Subroutine _listen_subscribe: called CC=2 SCAR=7.0
696 66 my ($self, $conn, $channel) = @_;
697 my $notify = $conn->{_mysql_notify}
698 66 ▶ or croak "LISTEN on channel '$channel': the listen connection is closed";
Branch: 1 branch
BranchTrueFalseTotal
my $notify = $conn->{"_mysql_notify"} or croak("LISTEN on channel '${channel}': the listen connection is closed") 66 0 66
699 66 my ($sql, $bind) = $notify->subscribe_sql($channel);
700 66 return $self->_mysql_listen_statement($conn, $sql, $bind)->then(
701 _mysql_subscribed_cb($self->future_class, $notify, $channel),
702 _mysql_subscribe_failed_cb($self->future_class, $notify),
703 );
704 }
705  
706 66 ▶sub _mysql_subscribed_cb {
Subroutine _mysql_subscribed_cb: called CC=1 SCAR=0.0
707 66 my ($fc, $notify, $channel) = @_;
708 65 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
709 65 $notify->subscribed($channel, [@_]);
710 65 return $fc->done;
711 66 };
712 }
713  
714 66 ▶sub _mysql_subscribe_failed_cb {
Subroutine _mysql_subscribe_failed_cb: called CC=1 SCAR=0.3
715 66 my ($fc, $notify) = @_;
716 1 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.5
717 1 my ($err) = @_;
718 1 ▶ $err = sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if defined $err and $err =~ /doesn't exist/ 1 0 1
Condition33%1/3
defined $err and $err =~ /doesn't exist/
!ll&&!rl&&r
001
MC/DC0%0/2
defined $err and $err =~ /doesn't exist/
defined $err $err =~ /doesn't exist/
Truth table33%1/3
A and B
A= defined $errB= $err =~ /doesn't exist/
ABresult
0X0
100
111
719 'the notify table %s does not exist -- MySQL has no LISTEN/NOTIFY, and '
720 . 'its emulation needs that table: deploy it once with deploy_notify_table, '
721 . 'or run DBIO::MySQL::NotifyTable->create_table_sql from a migration (%s)',
722 $notify->quoted_table, $err,
723 ) if defined $err && $err =~ /doesn't exist/;
724 1 return $fc->fail($err);
725 66 };
726 }
727  
728  
729 3 ▶sub _listen_unsubscribe {
Subroutine _listen_unsubscribe: called CC=2 SCAR=7.0
730 3 my ($self, $conn, $channel) = @_;
731 3 my $notify = $conn->{_mysql_notify};
732 3 ▶ $notify->unsubscribe($channel) if $notify;
Branch: 1 branch
BranchTrueFalseTotal
if $notify 3 0 3
733 3 return $self->future_class->done;
734 }
735  
736  
737 60 ▶sub _listen_poll_interval { $_[0]->notify_poll_interval }
Subroutine _listen_poll_interval: called CC=1 SCAR=0.0
738  
739  
740 70 ▶sub _listen_drain {
Subroutine _listen_drain: called CC=6 SCAR=19.9
741 70 my ($self, $conn) = @_;
742 70 my $notify = $conn->{_mysql_notify};
743 70 ▶ croak 'the listen connection is closed' unless $notify && $conn->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $notify and $conn->{"dbh"} 0 70 70
Condition33%1/3
$notify and $conn->{"dbh"}
!ll&&!rl&&r
0070
MC/DC0%0/2
$notify and $conn->{"dbh"}
$notify $conn->{"dbh"}
Truth table33%1/3
A and B
A= $notifyB= $conn->{"dbh"}
ABresult
0X0
100
111
744 croak 'the listen connection was abandoned with a statement in flight'
745 70 ▶ if $conn->{_mysql_abandoned};
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"_mysql_abandoned"} 0 70 70
746 70 ▶ return [] if $conn->{_mysql_busy};
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"_mysql_busy"} 0 70 70
747  
748 70 ▶ my ($sql, $bind, $token) = $notify->poll_sql or return [];
Branch: 1 branch
BranchTrueFalseTotal
() = $notify->poll_sql or return [] 70 0 70
749 70 return $self->_mysql_listen_start($conn, $sql, $bind)
750 ->then($self->_mysql_polled_cb($conn, $notify, $token));
751 }
752  
753 70 ▶sub _mysql_polled_cb {
Subroutine _mysql_polled_cb: called CC=1 SCAR=0.0
754 70 my ($self, $conn, $notify, $token) = @_;
755 70 my $fc = $self->future_class;
756 70 Scalar::Util::weaken(my $weak_self = $self);
757 70 Scalar::Util::weaken(my $weak_conn = $conn);
758 63 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.4
759 63 my $notifications = $notify->polled($token, [@_]);
760 63 my ($self, $conn) = ($weak_self, $weak_conn);
761 63 my ($sql, $bind) = $notify->cleanup_sql;
762 return $fc->done($notifications)
763 63 ▶ unless defined $sql && $self && $conn && $conn->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sql and $self and $conn and $conn->{"dbh"} 37 26 63
Condition66%6/9
defined $sql and $self and $conn and $conn->{"dbh"}
!ll&&!rl&&r
37026
defined $sql and $self and $conn
!ll&&!rl&&r
37026
defined $sql and $self
!ll&&!rl&&r
37026
MC/DC25%1/4
defined $sql and $self and $conn and $conn->{"dbh"}
defined $sql $self $conn $conn->{"dbh"}
Truth table40%2/5
A and B and C and D
A= defined $sqlB= $selfC= $connD= $conn->{"dbh"}
ABCDresult
0XXX0
10XX0
110X0
11100
11111
764 26 return $self->_mysql_listen_start($conn, $sql, $bind)->then(
765 $self->_mysql_cleaned_cb($notify, $notifications, 1),
766 $self->_mysql_cleaned_cb($notify, $notifications, 0),
767 );
768 70 };
769 }
770  
771 52 ▶sub _mysql_cleaned_cb {
Subroutine _mysql_cleaned_cb: called CC=1 SCAR=0.0
772 52 my ($self, $notify, $notifications, $ok) = @_;
773 52 my $fc = $self->future_class;
774 52 Scalar::Util::weaken(my $weak_self = $self);
775 14 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
776 14 my ($result) = @_;
777 14 ▶ $notify->cleaned($ok ? $result : undef);
Branch: 1 branch
BranchTrueFalseTotal
$ok ? : 8 6 14
778 14 ▶ $weak_self->_listen_error(
Branch: 1 branch
BranchTrueFalseTotal
if not $ok and $weak_self 6 8 14
Condition60%3/5
not $ok and $weak_self
!ll&&!rl&&r
806
$result // "unknown error"
l!l
60
MC/DC50%1/2
not $ok and $weak_self
not $ok $weak_self
MC/DC0%0/1
$result // "unknown error"
$result
Truth table66%2/3
not(A) and B
A= not $okB= $weak_self
ABresult
0X0
100
111
Truth table50%1/2
A
A= $result
Aresult
11
00
779 'cleaning up the notify table ' . $notify->quoted_table . ' failed: '
780 . ($result // 'unknown error')
781 ) if !$ok && $weak_self;
782 14 return $fc->done($notifications);
783 52 };
784 }
785  
786  
787 7 ▶sub _listen_probe {
Subroutine _listen_probe: called CC=5 SCAR=16.7
788 7 my ($self, $conn) = @_;
789 7 ▶ my $dbh = $conn->{dbh} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $conn->{"dbh"} or return 0 7 0 7
790 7 ▶ return 0 if $conn->{_mysql_abandoned};
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"_mysql_abandoned"} 0 7 7
791 7 ▶ return 1 if $conn->{_mysql_busy};
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"_mysql_busy"} 1 6 7
792 6 ▶ return $dbh->ping ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->ping ? : 6 0 6
793 }
794  
795  
796 5 ▶sub _notify_sql {
Subroutine _notify_sql: called CC=1 SCAR=0.0
797 5 my ($self, $channel, $payload) = @_;
798 5 return $self->_mysql_notify_table->notify_sql($channel, $payload);
799 }
800  
801  
802 1 ▶sub deploy_notify_table {
Subroutine deploy_notify_table: called CC=1 SCAR=0.0
Pod documented
803 1 my ($self) = @_;
804 1 ▶ return $self->future_class->call(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
805 1 $self->_query_async($self->_mysql_notify_table->create_table_sql, []);
806 1 });
807 }
808  
809 # The notify table of the current settings; croaks on invalid ones.
810 373 ▶sub _mysql_notify_table {
Subroutine _mysql_notify_table: called CC=1 SCAR=0.0
811 373 my ($self) = @_;
812 373 return DBIO::MySQL::NotifyTable->new(
813 table => $self->notify_table,
814 poll_interval => $self->notify_poll_interval,
815 lookback => $self->notify_lookback,
816 retention => $self->notify_retention,
817 );
818 }
819  
820 # Run a statement on the listen connection once the one in flight is done,
821 # whether that succeeded or not. Re-checks after the wait: the drain it waited
822 # for may have started its cleanup.
823 67 ▶sub _mysql_listen_statement {
Subroutine _mysql_listen_statement: called CC=2 SCAR=6.9
824 67 my ($self, $conn, $sql, $bind) = @_;
825 my $busy = $conn->{_mysql_busy}
826 67 ▶ or return $self->_mysql_listen_start($conn, $sql, $bind);
Branch: 1 branch
BranchTrueFalseTotal
my $busy = $conn->{"_mysql_busy"} or return $self->_mysql_listen_start($conn, $sql, $bind) 1 66 67
827 1 return $busy->followed_by($self->_mysql_listen_retry_cb($conn, $sql, $bind));
828 }
829  
830 1 ▶sub _mysql_listen_retry_cb {
Subroutine _mysql_listen_retry_cb: called CC=1 SCAR=0.1
831 1 my ($self, $conn, $sql, $bind) = @_;
832 1 my $fc = $self->future_class;
833 1 Scalar::Util::weaken(my $weak_self = $self);
834 1 Scalar::Util::weaken(my $weak_conn = $conn);
835 1 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=12.0
836 1 my ($self, $conn) = ($weak_self, $weak_conn);
837 1 ▶ return $fc->fail('the listen connection is gone') unless $self && $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self and $conn 0 1 1
Condition33%1/3
$self and $conn
!ll&&!rl&&r
001
MC/DC0%0/2
$self and $conn
$self $conn
Truth table33%1/3
A and B
A= $selfB= $conn
ABresult
0X0
100
111
838 1 ▶ return $fc->call(sub { $self->_mysql_listen_statement($conn, $sql, $bind) });
Subroutine __ANON__: called CC=1 SCAR=0.0
839 1 };
840 }
841  
842 # Start a statement on the listen connection and record it as the one in
843 # flight until it settles. A cancelled one -- the watch was stopped mid-poll --
844 # leaves the handle with an unread result, so the connection is marked
845 # abandoned: nothing runs on it again, and core closes it next.
846 162 ▶sub _mysql_listen_start {
Subroutine _mysql_listen_start: called CC=3 SCAR=11.2
847 162 my ($self, $conn, $sql, $bind) = @_;
848 162 ▶ croak 'the listen connection is closed' unless $conn->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $conn->{"dbh"} 0 162 162
849 croak 'the listen connection was abandoned with a statement in flight'
850 162 ▶ if $conn->{_mysql_abandoned};
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"_mysql_abandoned"} 0 162 162
851  
852 162 my $f = $self->_query_async_pinned($conn, $sql, $bind);
853 162 $conn->{_mysql_busy} = $f;
854 162 $f->on_ready(_mysql_listen_idle_cb($conn, $f));
855 162 return $f;
856 }
857  
858 162 ▶sub _mysql_listen_idle_cb {
Subroutine _mysql_listen_idle_cb: called CC=1 SCAR=0.1
859 162 my ($conn, $f) = @_;
860 162 Scalar::Util::weaken(my $weak_conn = $conn);
861 162 my $id = Scalar::Util::refaddr($f);
862 162 ▶ return sub {
Subroutine __ANON__: called CC=5 SCAR=17.5
863 162 ▶ my $conn = $weak_conn or return;
Branch: 1 branch
BranchTrueFalseTotal
my $conn = $weak_conn or return 162 0 162
864 162 ▶ $conn->{_mysql_abandoned} = 1 if $_[0]->is_cancelled;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->is_cancelled 14 148 162
865 delete $conn->{_mysql_busy}
866 162 ▶ if $conn->{_mysql_busy} && Scalar::Util::refaddr($conn->{_mysql_busy}) == $id;
Branch: 1 branch
BranchTrueFalseTotal
if $conn->{"_mysql_busy"} and builtin::refaddr($conn->{"_mysql_busy"}) == $id 162 0 162
Condition33%1/3
$conn->{"_mysql_busy"} and builtin::refaddr($conn->{"_mysql_busy"}) == $id
!ll&&!rl&&r
00162
MC/DC0%0/2
$conn->{"_mysql_busy"} and builtin::refaddr($conn->{"_mysql_busy"}) == $id
$conn->{"_mysql_busy"} builtin::refaddr($conn->{"_mysql_busy"}) == $id
Truth table33%1/3
A and B
A= $conn->{"_mysql_busy"}B= builtin::refaddr($conn->{"_mysql_busy"}) == $id
ABresult
0X0
100
111
867 162 return;
868 162 };
869 }
870  
871 # --- Async deploy seam ------------------------------------------------------
872  
873  
874  
875 30 ▶sub _ddl_quote_ident {
Subroutine _ddl_quote_ident: called CC=1 SCAR=0.0
876 30 my ($self, $name) = @_;
877 30 return DBIO::MySQL::Identifier::quote_ident($name);
878 }
879  
880 8 ▶sub _ddl_class { 'DBIO::MySQL::DDL' }
Subroutine _ddl_class: called CC=1 SCAR=0.0
881  
882 27 1;
883  
884 __END__
« blib/lib/DBIO/MySQL/SQLMaker.pm ↑ Summary blib/lib/DBIO/MySQL/Introspect.pm »