EV/Storage.pm

filter: Statementstmt 97.7% 752 / 769 Branchbran 86.1% 353 / 410 Conditioncond 63.1% 166 / 263 MC/DCmcdc 42.1% 62 / 147 Subroutinesub 100% 119 / 119 Podpod 100% 7 / 7 totaltotal 85.0% 1459 / 1715 CC 301 SCAR 22.3
CC
301
Cov
88%
_pg_read_connect_info
38.5
__ANON__
15.4
_pipeline_exit
14.7
CRAP 9.3
« EV/ConnectInfo.pm ↑ Summary EV/Pool.pm »
1 package DBIO::PostgreSQL::EV::Storage;
2 # ABSTRACT: Async PostgreSQL storage driver using EV::Pg
3  
4 65 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 65 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 65 our $VERSION = '0.900004';
8  
9 65 ▶use base 'DBIO::Storage::Async';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 65 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 65 ▶use EV ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 65 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 65 ▶use DBIO::Storage::DBI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 65 ▶use Sub::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 65 ▶use DBIO::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 65 ▶use DBIO::Exception ();
Subroutine BEGIN: called CC=1 SCAR=0.0
18 65 ▶use DBIO::Exception::Server ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 65 ▶use DBIO::PostgreSQL::SQLMaker;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 65 ▶use DBIO::PostgreSQL::DDL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 65 ▶use DBIO::PostgreSQL::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 65 ▶use DBIO::PostgreSQL::EV::ConnectInfo qw( conninfo_string is_libpq_parameter );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 65 ▶use DBIO::PostgreSQL::EV::Future;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 65 ▶use DBIO::PostgreSQL::EV::Pool;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 65 ▶use DBIO::PostgreSQL::EV::TransactionContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 65 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28  
29 # --- SQLMaker seams -----------------------------------------------------------
30 # The inherited DBIO::Storage::Async::sql_maker builds the maker from these.
31  
32  
33 250 ▶sub sql_maker_class { 'DBIO::PostgreSQL::SQLMaker' }
Subroutine sql_maker_class: called CC=1 SCAR=0.0
Pod documented
34  
35  
36 247 ▶sub _sql_maker_args {
Subroutine _sql_maker_args: called CC=4 SCAR=13.9
37 247 my ($self) = @_;
38 # _owner_storage, not _effective_owner_storage: the private sync sibling of
39 # a backend without an owner shares this backend's maker, so asking it here
40 # would build this maker from itself.
41 247 ▶ my $owner = ref $self ? $self->_owner_storage : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 247 0 247
42  
43 247 my @quoting = (quote_char => '"', name_sep => '.');
44 247 ▶ if ($owner) {
Branch: 1 branch
BranchTrueFalseTotal
if ($owner) 13 234 247
45 13 my $sync = $owner->sql_maker;
46 @quoting = (
47 (defined $sync->{quote_char} ? (quote_char => $sync->{quote_char}) : ()),
48 name_sep => $sync->{name_sep},
49 13 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
defined $sync->{'quote_char'} ? : 11 2 13
50 }
51  
52 return (
53 247 @quoting,
54 limit_dialect => 'LimitOffset',
55 );
56 }
57  
58  
59 65 ▶sub _post_insert_sql { ' RETURNING *' }
Subroutine _post_insert_sql: called CC=1 SCAR=0.0
60  
61  
62 4172 ▶sub future_class { 'DBIO::PostgreSQL::EV::Future' }
Subroutine future_class: called CC=1 SCAR=0.0
Pod documented
63  
64  
65 222 ▶sub transport_capabilities {
Subroutine transport_capabilities: called CC=1 SCAR=0.0
Pod documented
66 222 qw(on_connect_replay listen notify copy pipeline broker_connect_options)
67 }
68  
69  
70 965 ▶sub connect_info {
Subroutine connect_info: called CC=10 SCAR=23.0
Pod documented
71 965 my ($self, $info) = @_;
72 965 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 965 0 965
73 # A connect_info setter always opens a new ownership generation (core k196 /
74 # this repo k38). When a pool or listen state exists, disconnect performs
75 # the rotation (it rotates before teardown) and ends listening (core ADR
76 # 0056); with neither materialised we still rotate here, so no context of
77 # the outgoing generation survives a reconfigure. Matches
78 # DBIO::Storage::Async::connect_info.
79 965 ▶ if ($self->{pool} || $self->{_listen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'pool'} or $self->{'_listen'}) { } 74 891 965
Condition66%2/3
$self->{"pool"} or $self->{"_listen"}
l!l&&r!l&&!r
740891
MC/DC50%1/2
$self->{"pool"} or $self->{"_listen"}
$self->{"pool"} $self->{"_listen"}
Truth table66%2/3
A or B
A= $self->{"pool"}B= $self->{"_listen"}
ABresult
1X1
011
000
80 74 $self->disconnect;
81 }
82 else {
83 891 $self->_rotate_context_owner_token;
84 }
85 # Nothing the connect_info before this one stored outlives this call --
86 # dropped here, before anything is read, because reading can throw: the
87 # broker itself, core's refusal of an answer of nothing, this transport's
88 # refusal of what it cannot connect from, two pool sizes that disagree.
89 # The conninfo must not be left for a connection opened later (core k252
90 # and k265, karr k63 and k67), and the pool size must not size a pool
91 # built after the refusal (karr k66).
92 965 @{$self}{qw( connect_info _conninfo _pool_size _opts )} = ();
93 965 $self->{_access_broker_call_attributes} = {};
94  
95 # [ $broker, \%options ]: the connect options the owning sync storage was
96 # given beside the broker, handed on because this transport declares
97 # broker_connect_options (core k243, karr k60). Read as core's connect_info
98 # reads them: what the owner applies itself (async, the storage and SQL
99 # maker options) is stripped, the rest is split into pool_size and the DBD
100 # attributes. Undef for every other form.
101 965 my $stripped = $self->_strip_private_connect_attrs($info);
102 965 my $beside = $self->_access_broker_call_options($stripped);
103 963 ▶ $info = $stripped if $beside;
Branch: 1 branch
BranchTrueFalseTotal
if $beside 126 837 963
104  
105 963 ▶ if ($beside || $self->_is_access_broker_connect_info($info)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($beside or $self->_is_access_broker_connect_info($info)) { } 353 610 963
Condition100%3/3
$beside or $self->_is_access_broker_connect_info($info)
l!l&&r!l&&!r
126227610
MC/DC100%2/2
$beside or $self->_is_access_broker_connect_info($info)
$beside $self->_is_access_broker_connect_info($info)
Truth table100%3/3
A or B
A= $besideB= $self->_is_access_broker_connect_info($info)
ABresult
1X1
011
000
106 353 $self->{connect_info} = $info;
107 # No conninfo is kept with a broker attached, as in core's connect_info
108 # (core k252, karr k63): every connection asks the broker anew
109 # (_conninfo_provider), so a stored copy could only ever be an earlier
110 # answer -- password included -- which nothing may use.
111 353 $self->_setup_access_broker($info->[0]);
112 # What stands beside the broker, kept where core's accessor answers for
113 # it and never applied: this transport drops DBD attributes, as
114 # dbi_to_conninfo does for a DSN. A connection parameter among them is
115 # not dropped but refused (karr k76) -- here, before the broker is asked,
116 # and for every connection after (_async_broker_conninfo), which is why
117 # they are kept before anything can throw: a storage whose connect_info
118 # was refused opens no connection without the parameter.
119 353 ▶ $self->{_access_broker_call_attributes} = $beside->{attributes} if $beside;
Branch: 1 branch
BranchTrueFalseTotal
if $beside 126 227 353
120 353 $self->_pg_refuse_parameters_beside_broker;
121 307 my $read = $self->_pg_read_connect_info(
122 $self->_current_async_connect_info($self->access_broker_mode), broker => 1,
123 );
124 190 my (undef, $pool_size, $opts) = $self->_normalize_async_connect_info($read);
125 # One rule for the pool size wherever it is given -- the answer's hash,
126 # an attribute of a DBI-form answer, beside the broker; the answer and
127 # the options beside the broker must agree, or this throws (core k245).
128 # Core's rule, asked of the answer as it was read: the reader has every
129 # place an answer gives a pool size in, the attributes in front of a
130 # DSN among them.
131 190 my $given_pool_size = $self->_access_broker_pool_size($read, $beside);
132 174 ▶ $pool_size = $given_pool_size if defined $given_pool_size;
Branch: 1 branch
BranchTrueFalseTotal
if defined $given_pool_size 63 111 174
133 174 $self->{_pool_size} = $pool_size;
134 174 $self->{_opts} = $opts;
135 }
136 else {
137 # Static connect info, in whichever form the owning sync storage reads
138 # one: the DBI form it hands on is translated, a form nobody can connect
139 # from is refused here and not by the first pool (karr k64, k66).
140 610 $self->_clear_access_broker;
141 610 my $read = $self->_pg_read_connect_info($info);
142 501 my ($conninfo, $pool_size, $opts) = $self->_normalize_async_connect_info($read);
143 # Nothing at all is the empty conninfo, left to libpq (karr k66).
144 501 ▶ $conninfo //= '';
Condition100%2/2
$conninfo //= ""
l!l
5001
MC/DC100%1/1
$conninfo //= ""
$conninfo
Truth table100%2/2
A
A= $conninfo
Aresult
11
00
145 501 $self->{connect_info} = $read;
146 501 $self->{_conninfo} = $conninfo;
147 501 $self->{_pool_size} = $pool_size;
148 501 $self->{_opts} = $opts;
149 }
150 }
151 675 return $self->{connect_info};
152 }
153  
154  
155 361 ▶sub _async_broker_conninfo {
Subroutine _async_broker_conninfo: called CC=2 SCAR=6.9
156 361 my ($self, $mode) = @_;
157 # Without a broker core's seam answers what connect_info stored, and that is
158 # no broker's answer: nothing since karr k63, before it the earlier answer of
159 # the broker that was detached, its password included.
160 361 ▶ _pg_refuse('PostgreSQL ev has no AccessBroker attached to ask for connect info')
Branch: 1 branch
BranchTrueFalseTotal
unless $self->access_broker 2 359 361
161 unless $self->access_broker;
162 359 $self->_pg_refuse_parameters_beside_broker;
163 329 my ($conninfo) = $self->_normalize_async_connect_info(
164 $self->_pg_read_connect_info($self->_current_async_connect_info($mode), broker => 1)
165 );
166 196 return $conninfo;
167 }
168  
169 # True for the name of a DBI attribute, by core's measure for a key of a hash
170 # (core k298, DBIO::Storage::DBI::AccessBroker::_access_broker_unapplied_parameters):
171 # it starts with an uppercase letter, with private_, dbi_ or dbd_, or with the
172 # prefix of the DBD -- pg_ here, the one DBD this transport stands beside. No
173 # libpq parameter has such a name.
174 1917 ▶sub _pg_is_dbi_attribute {
Subroutine _pg_is_dbi_attribute: called CC=1 SCAR=0.0
175 1917 my ($self, $name) = @_;
176 1917 return $name =~ /^(?:[A-Z]|private_|dbi_|dbd_|pg_)/;
177 }
178  
179 # Croaks when a connection parameter stands beside the broker (karr k76): a
180 # key of what connect_info kept from [ $broker, \%options ] that is set to a
181 # value and is, by its name, no DBI attribute (_pg_is_dbi_attribute, the
182 # measure of core's _assert_access_broker_parameters_apply for a broker's hash
183 # answer); undef and the empty string name no parameter. What the owner applies
184 # itself and pool_size are gone from the hash by now. No list of libpq
185 # parameters is used for a lowercase name: one that is no DBI attribute cannot
186 # be told from one. A name spelled with a capital is told by the list of
187 # ConnectInfo::is_libpq_parameter (karr k80). Names only, sorted -- a value may be a secret (sslpassword), and
188 # nothing is said of the broker's answer, which has not been asked for.
189 712 ▶sub _pg_refuse_parameters_beside_broker {
Subroutine _pg_refuse_parameters_beside_broker: called CC=6 SCAR=17.9
190 712 my ($self) = @_;
191 712 my $beside = $self->_access_broker_call_attributes;
192 # A name that is a DBI attribute's by its case -- Sslmode -- and a libpq
193 # parameter's lower-cased is refused as well (karr k80): the owner's DBI
194 # takes it for an attribute, and the pool would connect without it.
195 my @unapplied = sort grep {
196 712 ▶ defined $beside->{$_} && length $beside->{$_}
Condition100%9/9
defined $beside->{$_} && length $beside->{$_} && (!$self->_pg_is_dbi_attribute($_) || is_libpq_parameter($_))
!ll&&!rl&&r
109680
defined $beside->{$_} and length $beside->{$_}
!ll&&!rl&&r
73176
not $self->_pg_is_dbi_attribute($_) or is_libpq_parameter($_)
l!l&&r!l&&!r
77396
MC/DC100%4/4
defined $beside->{$_} && length $beside->{$_} && (!$self->_pg_is_dbi_attribute($_) || is_libpq_parameter($_))
defined $beside->{$_} length $beside->{$_} not $self->_pg_is_dbi_attribute($_) is_libpq_parameter($_)
Truth table100%5/5
A and B && not(C) or D
A= defined $beside->{$_}B= length $beside->{$_}C= not $self->_pg_is_dbi_attribute($_)D= is_libpq_parameter($_)
ABCDresult
0XXX0
10XX0
11000
111X1
11011
197 && (!$self->_pg_is_dbi_attribute($_) || is_libpq_parameter($_))
198 } keys %$beside;
199 712 ▶ return unless @unapplied;
Branch: 1 branch
BranchTrueFalseTotal
unless @unapplied 636 76 712
200  
201 76 ▶ _pg_refuse(sprintf
Branch: 1 branch
BranchTrueFalseTotal
@unapplied > 1 ? : 3 73 76
202 'PostgreSQL ev does not apply connection parameter(s) given beside an '
203 . 'AccessBroker (%s): its pooled connections and the listen connection are '
204 . "opened from the broker's answer alone, and would be opened without %s. "
205 . "Give %s in the broker's answer: the DBI form "
206 . "[ 'dbi:Pg:...;<parameter>=...', \$user, \$password ] with the parameter in "
207 . 'the DSN, or a hash of libpq connection parameters answered to this '
208 . 'transport (connect_info_for_storage). A libpq parameter spelled with a '
209 . 'capital (Sslmode) is refused as well: DBI takes it for an attribute. '
210 . 'Beside the broker it takes '
211 . 'pool_size, and DBI attributes, which are for the DBI connection of the '
212 . 'owning storage and dropped here',
213 join(', ', @unapplied), (@unapplied > 1 ? 'them' : 'it') x 2);
214 }
215  
216 # Connect info, in any form connect_info's POD lists, as the
217 # [ $conninfo, \%opts ] pair core's _normalize_async_connect_info splits. The
218 # one place this transport reads connect info, whatever it comes from (karr
219 # k61, k64): what connect_info is given, and what a broker answers -- at
220 # attach and for every connection -- when called with broker => 1. So a form
221 # is read the same from both, and what is refused here never reaches EV::Pg.
222 #
223 # The measure is the owning sync storage, which opens its dbh from the same
224 # connect info (karr k58): a form it reads as one connection
225 # (DBIO::Storage::DBI::_normalize_connect_info) is read as that connection
226 # here, and what it connects from in a way this transport cannot follow -- a
227 # code reference -- is refused. Croaks naming the form, never a value.
228 #
229 # Two rules hang on where the connect info comes from. A hash that stands in
230 # no array is a broker's answer only. And connect info that renders to the
231 # empty conninfo is refused from a broker (karr k61) and taken as static
232 # connect info, where what becomes of it is open (karr k66).
233 1199 ▶sub _pg_read_connect_info {
Subroutine _pg_read_connect_info: called CC=38 SCAR=36.5
234 1199 my ($self, $info, %from) = @_;
235 1199 my ($pair, $shape);
236  
237 1199 ▶ $info = [ $info, {} ] if $from{broker} && ref $info eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $from{"broker"} and ref $info eq "HASH" 0 1199 1199
Condition66%2/3
$from{"broker"} and ref $info eq "HASH"
!ll&&!rl&&r
6105890
MC/DC0%0/2
$from{"broker"} and ref $info eq "HASH"
$from{"broker"} ref $info eq "HASH"
Truth table66%2/3
A and B
A= $from{"broker"}B= ref $info eq "HASH"
ABresult
0X0
100
111
238  
239 1199 ▶ if (ref $info eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info eq "ARRAY") 1178 21 1199
240 # What the owning storage applies itself -- async, its storage and SQL
241 # maker options -- is no part of a connection: taken out by core, of the
242 # attribute hashes of whichever form (a broker's answer comes unstripped).
243 # Core keeps what it took out for the storage; that is connect_info's to
244 # set, not every reading's.
245 1178 my $stripped = do {
246 1178 local $self->{_storage_options};
247 1178 $self->_strip_private_connect_attrs($info);
248 };
249 1178 my ($first, $opts) = @$stripped;
250 1178 ▶ my $paired = @$info <= 2 && (!defined $opts || ref $opts eq 'HASH');
Condition100%6/6
@$info <= 2 && (!defined($opts) || ref $opts eq 'HASH')
!ll&&!rl&&r
3458825
not defined $opts or ref $opts eq "HASH"
l!l&&r!l&&!r
2325938
MC/DC100%3/3
@$info <= 2 && (!defined($opts) || ref $opts eq 'HASH')
@$info <= 2 not defined $opts ref $opts eq "HASH"
Truth table100%4/4
A && not(B) or C
A= @$info <= 2B= not defined $optsC= ref $opts eq "HASH"
ABCresult
0XX0
1000
11X1
1011
251  
252 1178 ▶ if (defined $first && !ref $first && $first =~ /^dbi:/i) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined $first and not ref $first and $first =~ /^dbi:/i) { } 385 793 1178
elsif (ref $first eq 'CODE') { } 17 776 793
elsif (not $paired) { } 21 755 776
elsif (ref $first eq 'HASH') { } 733 22 755
elsif (not ref $first and $from{'broker'} ? defined $first && length $first : 1) { } 16 12 28
Condition100%6/6
defined $first and not ref $first and $first =~ /^dbi:/i
!ll&&!rl&&r
76726385
defined $first and not ref $first
!ll&&!rl&&r
5762411
MC/DC100%3/3
defined $first and not ref $first and $first =~ /^dbi:/i
defined $first not ref $first $first =~ /^dbi:/i
Truth table100%4/4
A and not(B) and C
A= defined $firstB= not ref $firstC= $first =~ /^dbi:/i
ABCresult
0XX0
10X0
1100
1111
253 385 ($pair, $shape) = $self->_pg_read_dbi_form($stripped, 'a DBI-form arrayref', $from{broker});
254 }
255 elsif (ref $first eq 'CODE') {
256 17 $shape = $self->_pg_code_shape(undef, $from{broker});
257 }
258 elsif (!$paired) {
259 # a libpq conninfo string followed by a user and password, which would
260 # be dropped; anything else nobody reads: named by its elements below
261 }
262 elsif (ref $first eq 'HASH') {
263 # The single hash, as the owner reads it.
264 733 my %hash = %$first;
265 my $is_dsn = exists $hash{dsn} ? 'dsn'
266 733 ▶ : defined $hash{dbname} && !ref $hash{dbname} && $hash{dbname} =~ /^dbi:/i ? 'dbname'
Branch: 2 branches
BranchTrueFalseTotal
exists $hash{'dsn'} ? : 103 630 733
defined $hash{'dbname'} && !ref($hash{'dbname'}) && $hash{'dbname'} =~ /^dbi:/i ? : 11 619 630
Condition83%5/6
defined $hash{"dbname"} and not ref $hash{"dbname"} and $hash{"dbname"} =~ /^dbi:/i
!ll&&!rl&&r
3158811
defined $hash{"dbname"} and not ref $hash{"dbname"}
!ll&&!rl&&r
310599
MC/DC66%2/3
defined $hash{"dbname"} and not ref $hash{"dbname"} and $hash{"dbname"} =~ /^dbi:/i
defined $hash{"dbname"} not ref $hash{"dbname"} $hash{"dbname"} =~ /^dbi:/i
Truth table75%3/4
A and not(B) and C
A= defined $hash{"dbname"}B= not ref $hash{"dbname"}C= $hash{"dbname"} =~ /^dbi:/i
ABCresult
0XX0
10X0
1100
1111
267 : undef;
268  
269 733 ▶ if (delete $hash{dbh_maker}) {
Branch: 2 branches
BranchTrueFalseTotal
if (delete $hash{'dbh_maker'}) { } 5 728 733
elsif (defined $is_dsn) { } 112 616 728
270 5 $shape = $self->_pg_code_shape('dbh_maker', $from{broker});
271 }
272 elsif (defined $is_dsn) {
273 # { dsn => $dsn, user => ..., password => ..., %attributes } is
274 # [ $dsn, $user, $password, \%attributes ]. A dbname that is a whole
275 # DSN is the dsn where there is none; host and port cannot go into
276 # it, which the owner refuses too, and empty ones are none.
277 my @beside = $is_dsn eq 'dbname'
278 112 ▶ ? grep { defined $hash{$_} && length $hash{$_} } qw( host port ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$is_dsn eq 'dbname' ? : 11 101 112
Condition66%2/3
defined $hash{$_} && length $hash{$_}
!ll&&!rl&&r
1903
MC/DC50%1/2
defined $hash{$_} && length $hash{$_}
defined $hash{$_} length $hash{$_}
Truth table66%2/3
A && B
A= defined $hash{$_}B= length $hash{$_}
ABresult
0X0
100
111
279 112 ▶ if (@beside) {
Branch: 1 branch
BranchTrueFalseTotal
if (@beside) { } 3 109 112
280 3 $shape = 'a hashref whose dbname is a whole DSN, with ' . join(' and ', @beside) . ' beside it';
281 }
282 else {
283 ($pair, $shape) = $self->_pg_read_dbi_form(
284 [ delete $hash{$is_dsn}, delete @hash{qw( user password )}, \%hash ],
285 $is_dsn eq 'dsn' ? 'a hashref with a dsn key' : 'a hashref with a DSN for its dbname',
286 $from{broker},
287 109 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$is_dsn eq 'dsn' ? : 101 8 109
288 65 ▶ $pair->[1] = $opts if $pair && $opts;
Branch: 1 branch
BranchTrueFalseTotal
if $pair and $opts 32 33 65
Condition100%3/3
$pair and $opts
!ll&&!rl&&r
221132
MC/DC100%2/2
$pair and $opts
$pair $opts
Truth table100%3/3
A and B
A= $pairB= $opts
ABresult
0X0
100
111
289 }
290 }
291 else {
292 # A hash of libpq connection parameters, this transport's own form.
293 # A key that is a DBI attribute's by its name is the owner's, for its
294 # dbh, and dropped as the attributes of the DBI form are.
295 616 my @attributes = grep { $self->_pg_is_dbi_attribute($_) } keys %hash;
296 # ... unless it is a libpq parameter by another case (karr k80,
297 # Sslmode): dropped as an attribute it would leave the connection
298 # without it, silently. Refused naming the key, never a value.
299 my @cased = sort grep {
300 616 ▶ is_libpq_parameter($_) && defined $hash{$_} && length $hash{$_}
Condition83%5/6
is_libpq_parameter($_) && defined $hash{$_} && length $hash{$_}
!ll&&!rl&&r
3806
is_libpq_parameter($_) and defined $hash{$_}
!ll&&!rl&&r
3626
MC/DC66%2/3
is_libpq_parameter($_) && defined $hash{$_} && length $hash{$_}
is_libpq_parameter($_) defined $hash{$_} length $hash{$_}
Truth table75%3/4
A and B && C
A= is_libpq_parameter($_)B= defined $hash{$_}C= length $hash{$_}
ABCresult
0XX0
10X0
1100
1111
301 } @attributes;
302 616 delete @hash{@attributes};
303 616 my ($ref_key) = grep { ref $hash{$_} } sort keys %hash;
304 616 ▶ if (@cased) {
Branch: 2 branches
BranchTrueFalseTotal
if (@cased) { } 4 612 616
elsif (defined $ref_key) { } 8 604 612
305 4 $shape = sprintf 'a hashref of libpq connection parameters whose '
306 . 'key(s) (%s) are libpq parameters spelled with a capital: DBI '
307 . 'takes that for an attribute, which this transport drops, and '
308 . 'the connection would be opened without the parameter -- '
309 . 'libpq parameters are lower case', join(', ', @cased);
310 }
311 elsif (defined $ref_key) {
312 8 $shape = "its libpq connection parameter '$ref_key' is a reference, not a value";
313 }
314 else {
315 604 $pair = [ \%hash, $opts ];
316 }
317 }
318 }
319 ▶ elsif (!ref $first && ($from{broker} ? defined $first && length $first : 1)) {
Branch: 1 branch
BranchTrueFalseTotal
$from{'broker'} ? : 13 3 16
Condition50%3/6
not ref $first and $from{"broker"} ? defined $first && length $first : 1
!ll&&!rl&&r
6016
defined $first and length $first
!ll&&!rl&&r
0013
MC/DC50%1/2
not ref $first and $from{"broker"} ? defined $first && length $first : 1
not ref $first $from{"broker"} ? defined $first && length $first : 1
MC/DC0%0/2
defined $first and length $first
defined $first length $first
Truth table66%2/3
not(A) and B
A= not ref $firstB= $from{"broker"} ? defined $first && length $first : 1
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $firstB= length $first
ABresult
0X0
100
111
320 # A libpq conninfo string. Static connect info may give none at all.
321 16 $pair = [ $first, $opts ];
322 }
323 }
324  
325 1064 ▶ if ($pair) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair) 916 148 1064
326 916 ▶ return $pair unless $from{broker};
Branch: 1 branch
BranchTrueFalseTotal
unless $from{"broker"} 501 415 916
327 # Whatever its shape, an answer that renders to the empty conninfo is
328 # refused (karr k61): libpq fills an empty conninfo from its environment
329 # and defaults (PGHOST, PGUSER, ...), so the connection would go where the
330 # process environment points instead of where the broker said. Rendered as
331 # the pool and the listen connection render it, so the two cannot differ.
332 415 my ($conninfo) = $self->_normalize_async_connect_info($pair);
333 415 ▶ return $pair if conninfo_string($conninfo) =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
if conninfo_string($conninfo) =~ /\S/ 386 28 414
334 28 ▶ $shape = sprintf
Branch: 1 branch
BranchTrueFalseTotal
ref $info->[0] eq 'HASH' ? : 25 3 28
335 'no libpq connection parameter with a value in %s -- '
336 . 'libpq would fill an empty conninfo from its environment and defaults',
337 $self->_pg_describe_shape(ref $info->[0] eq 'HASH' ? $info->[0] : $info);
338 }
339  
340 _pg_refuse(sprintf(
341 'PostgreSQL ev cannot connect from %s: %s. '
342 . "It takes a DBI-form arrayref [ 'dbi:Pg:...', \$user, \$password, \\%%attrs ], "
343 . "a hashref with that DSN for its dsn ({ dsn => 'dbi:Pg:...', user => ..., password => ... }), "
344 . 'a hashref of libpq connection parameters (dbname, host, port, user, password, ...), '
345 . 'or an arrayref of such a hashref or a libpq conninfo string, optionally '
346 . 'followed by an options hashref',
347 176 ▶ ($from{broker} ? 'what the AccessBroker returned' : 'this connect info'),
Branch: 1 branch
BranchTrueFalseTotal
$from{'broker'} ? : 100 76 176
Condition66%2/3
$shape // $self->_pg_describe_shape($info)
l!l&&r!l&&!r
128480
MC/DC0%0/2
$shape // $self->_pg_describe_shape($info)
$shape $self->_pg_describe_shape($info)
Truth table66%2/3
A // B
A= $shapeB= $self->_pg_describe_shape($info)
ABresult
1X1
011
000
348 $shape // $self->_pg_describe_shape($info)),
349 _pg_answer_connect_info($info));
350 }
351  
352 # The DBI form [ $dsn, $user, $password, \%attrs, \%extra_attributes ], read
353 # for _pg_read_connect_info: the pair dbi_to_conninfo makes of it, or else
354 # (undef, $shape) -- what is wrong with it, for a message. $form names what
355 # the DSN came in.
356 494 ▶sub _pg_read_dbi_form {
Subroutine _pg_read_dbi_form: called CC=9 SCAR=22.0
357 494 my ($self, $dbi, $form, $from_broker) = @_;
358 494 my $dsn = $dbi->[0];
359 # DBI_USER and DBI_PASS are read for static connect info only (karr k66).
360 494 ▶ my $env = $from_broker ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$from_broker ? : 285 209 494
361  
362 # dbi_to_conninfo's own test, made here: it dies quoting the DSN -- its
363 # passwords masked since karr k78, the rest as it came -- and this message
364 # names the form of connect info and nothing that was in it.
365 494 my ($driver) = DBIO::PostgreSQL::EV::ConnectInfo::dbi_dsn_parts($dsn);
366 494 ▶ if (defined $driver && $driver =~ /\APg\z/i) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $driver and $driver =~ /\APg\z/i) 472 22 494
Condition100%3/3
defined $driver and $driver =~ /\APg\z/i
!ll&&!rl&&r
616472
MC/DC100%2/2
defined $driver and $driver =~ /\APg\z/i
defined $driver $driver =~ /\APg\z/i
Truth table100%3/3
A and B
A= defined $driverB= $driver =~ /\APg\z/i
ABresult
0X0
100
111
367 472 my $pair = eval { DBIO::PostgreSQL::EV::ConnectInfo::dbi_to_conninfo($dbi, env => $env) };
368 472 ▶ if ($pair) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair) 431 41 472
369 # A DSN that names no target would have libpq take it from PGHOST,
370 # PGDATABASE, ... -- refused with the words of the sync storage and of
371 # future_io (karr k95; DBIO::PostgreSQL::Util::dsn_names_target).
372 431 ▶ _pg_refuse(
Branch: 1 branch
BranchTrueFalseTotal
unless DBIO::PostgreSQL::Util::dsn_names_target($dsn) 135 296 431
373 'Cannot connect with a DBD::Pg DSN that names no dbname, host or port, '
374 . 'so libpq would take the target from its environment (PGHOST, '
375 . 'PGDATABASE, ...). Give at least one of dbname, host or port (or '
376 . 'service, hostaddr) in the DSN.',
377 $dbi,
378 ) unless DBIO::PostgreSQL::Util::dsn_names_target($dsn);
379 296 return $pair;
380 }
381 # What is left for it to die of is a DSN libpq cannot read (karr k58),
382 # and that complaint holds no word of the DSN.
383 41 return (undef, "$form -- " . $@ =~ s/\s+\z//r);
384 }
385  
386 # The driver's name alone. What stands between 'dbi:' and the next colon is
387 # more than that: DBI takes attributes there, Password among them
388 # ('dbi:Pg(Password=>...):'), and a DSN gone wrong may have its parameters
389 # there (karr k78).
390 22 ▶ my ($named) = defined $dsn && !ref $dsn ? $dsn =~ /^dbi:(\w*)/i : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $dsn && !ref($dsn) ? : 20 2 22
Condition66%2/3
defined $dsn and not ref $dsn
!ll&&!rl&&r
2020
MC/DC50%1/2
defined $dsn and not ref $dsn
defined $dsn not ref $dsn
Truth table66%2/3
A and not(B)
A= defined $dsnB= not ref $dsn
ABresult
0X0
100
111
391 22 ▶ return (undef, "$form whose DSN is not dbi:Pg:<parameters>"
Branch: 1 branch
BranchTrueFalseTotal
defined $named ? : 18 4 22
392 . (defined $named ? " (DBI driver '$named')" : ''));
393 }
394  
395 # What a code reference in connect info is, for an error message: the owner's
396 # way to a dbh -- a connection coderef, or a hash's dbh_maker -- and nothing
397 # this transport can open an EV::Pg connection from.
398 22 ▶sub _pg_code_shape {
Subroutine _pg_code_shape: called CC=6 SCAR=18.5
399 22 my ($self, $key, $from_broker) = @_;
400 # Beside a broker answering code, core refuses every key it does not
401 # consume itself, a DBI attribute as much as a client parameter (core
402 # k310; karr k80): named here too, never a value.
403 22 my %consumed = map { $_ => 1 }
404 qw( dsn user password dbh_maker dbname host port pool_size ignore_version );
405 22 ▶ my $given = $from_broker ? $self->_access_broker_call_attributes : {};
Branch: 1 branch
BranchTrueFalseTotal
$from_broker ? : 4 18 22
406 my @beside = sort grep {
407 22 ▶ !$consumed{$_} && defined $given->{$_} && length $given->{$_}
Condition33%2/6
!$consumed{$_} && defined $given->{$_} && length $given->{$_}
!ll&&!rl&&r
001
not $consumed{$_} and defined $given->{$_}
!ll&&!rl&&r
001
MC/DC0%0/3
!$consumed{$_} && defined $given->{$_} && length $given->{$_}
not $consumed{$_} defined $given->{$_} length $given->{$_}
Truth table25%1/4
not(A) and B && C
A= not $consumed{$_}B= defined $given->{$_}C= length $given->{$_}
ABCresult
0XX0
10X0
1100
1111
408 } keys %$given;
409 22 ▶ return 'a code reference that makes a DBI handle' . (defined $key ? " ($key)" : '')
Branch: 2 branches
BranchTrueFalseTotal
defined $key ? : 5 17 22
@beside ? : 1 21 22
410 . ' -- this transport opens its own connections, from libpq parameters'
411 . (@beside ? sprintf ', and %s beside the broker would be dropped with it: %s',
412 join(', ', @beside), 'the code opens the connection and nothing beside the broker goes to it'
413 : '');
414 }
415  
416 # The shape of connect info, for an error message: types and hash keys, never
417 # values -- they carry the password.
418 76 ▶sub _pg_describe_shape {
Subroutine _pg_describe_shape: called CC=8 SCAR=20.8
419 76 my ($self, $value) = @_;
420 76 ▶ return 'a plain string' unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 19 57 76
421 57 ▶ return 'an empty hashref' if ref $value eq 'HASH' && !%$value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" and not %$value 6 51 57
Condition100%3/3
ref $value eq "HASH" and not %$value
!ll&&!rl&&r
31206
MC/DC100%2/2
ref $value eq "HASH" and not %$value
ref $value eq "HASH" not %$value
Truth table100%3/3
A and not(B)
A= ref $value eq "HASH"B= not %$value
ABresult
0X0
100
111
422 51 ▶ return sprintf 'a hashref with keys (%s)', join ', ', sort keys %$value
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq "HASH" 20 31 51
423 if ref $value eq 'HASH';
424 return sprintf 'an arrayref of (%s)', join ', ',
425 31 ▶ map { !defined $_ ? 'undef' : ref $_ ? ref($_) . ' ref' : 'string' } @$value
Branch: 3 branches
BranchTrueFalseTotal
if ref $value eq "ARRAY" 30 1 31
!defined($_) ? : 6 64 70
ref $_ ? : 16 48 64
426 if ref $value eq 'ARRAY';
427 1 return 'a ' . ref($value) . ' ref';
428 }
429  
430  
431 2550 ▶sub pool {
Subroutine pool: called CC=3 SCAR=11.0
Pod documented
432 2550 my $self = shift;
433 2550 ▶ $self->{pool} ||= do {
Condition66%2/3
$self->{"pool"} ||= do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "future_class", $self->future_class, "on_error", sub { warn "DBIO::PostgreSQL::EV pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->_conninfo_string; } ; "DBIO::PostgreSQL::EV::Pool"->new(%args) }
l!l&&r!l&&!r
18187320
MC/DC0%0/2
$self->{"pool"} ||= do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "future_class", $self->future_class, "on_error", sub { warn "DBIO::PostgreSQL::EV pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->_conninfo_string; } ; "DBIO::PostgreSQL::EV::Pool"->new(%args) }
$self->{"pool"} do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "future_class", $self->future_class, "on_error", sub { warn "DBIO::PostgreSQL::EV pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->_conninfo_string; } ; "DBIO::PostgreSQL::EV::Pool"->new(%args) }
Truth table66%2/3
A ||= B
A= $self->{"pool"}B= do { my(%args) = ("storage", $self, "size", $self->{"_pool_size"}, "future_class", $self->future_class, "on_error", sub { warn "DBIO::PostgreSQL::EV pool error: $_[0]\n"; } ); if (my $provider = $self->_conninfo_provider) { $args{"conninfo_provider"} = $provider; } else { $args{"conninfo"} = $self->_conninfo_string; } ; "DBIO::PostgreSQL::EV::Pool"->new(%args) }
ABresult
1X1
011
000
434 my %args = (
435 storage => $self,
436 size => $self->{_pool_size},
437 future_class => $self->future_class,
438 39 ▶ on_error => sub { warn "DBIO::PostgreSQL::EV pool error: $_[0]\n" },
Subroutine __ANON__: called CC=1 SCAR=0.0
439 732 );
440  
441 732 ▶ if (my $provider = $self->_conninfo_provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $provider = $self->_conninfo_provider) { } 214 518 732
442 214 $args{conninfo_provider} = $provider;
443 }
444 else {
445 518 $args{conninfo} = $self->_conninfo_string;
446 }
447  
448 732 DBIO::PostgreSQL::EV::Pool->new(%args);
449 };
450 }
451  
452  
453 666 ▶sub _conninfo_string {
Subroutine _conninfo_string: called CC=5 SCAR=16.1
454 666 my ($self, $ci) = @_;
455 666 ▶ if (! defined $ci) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ci) 666 0 666
456 666 my $provider = $self->_conninfo_provider;
457 666 ▶ $ci = $provider ? $provider->() : $self->{_conninfo};
Branch: 1 branch
BranchTrueFalseTotal
$provider ? : 12 654 666
458 }
459 662 my $string = conninfo_string($ci);
460 # An empty conninfo is libpq's own environment and defaults (karr k66); a
461 # blank, where the pool takes none that is false.
462 662 ▶ return defined $string && !length $string ? ' ' : $string;
Branch: 1 branch
BranchTrueFalseTotal
defined $string && !length($string) ? : 9 653 662
Condition100%3/3
defined $string and not length $string
!ll&&!rl&&r
1654889
MC/DC100%2/2
defined $string and not length $string
defined $string not length $string
Truth table100%3/3
A and not(B)
A= defined $stringB= not length $string
ABresult
0X0
100
111
463 }
464  
465 # --- Transaction context seams ------------------------------------------------
466  
467  
468 80 ▶sub _txn_context_class { 'DBIO::PostgreSQL::EV::TransactionContext' }
Subroutine _txn_context_class: called CC=1 SCAR=0.0
469  
470  
471 80 ▶sub _txn_conn_accessor { 'txn_conn' }
Subroutine _txn_conn_accessor: called CC=1 SCAR=0.0
472  
473  
474 29 ▶sub _txn_commit_rolled_back {
Subroutine _txn_commit_rolled_back: called CC=5 SCAR=16.3
475 29 my ($self, $pg) = @_;
476 29 ▶ return 0 unless $pg->can('result_meta');
Branch: 1 branch
BranchTrueFalseTotal
unless $pg->can("result_meta") 8 21 29
477 21 my $meta = $pg->result_meta;
478 21 ▶ return $meta && ($meta->{cmd_status} // '') eq 'ROLLBACK' ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$meta && ($meta->{'cmd_status'} // '') eq 'ROLLBACK' ? : 1 20 21
Condition60%3/5
$meta and ($meta->{"cmd_status"} // "") eq "ROLLBACK"
!ll&&!rl&&r
0201
$meta->{"cmd_status"} // ""
l!l
210
MC/DC0%0/2
$meta and ($meta->{"cmd_status"} // "") eq "ROLLBACK"
$meta $meta->{"cmd_status"}
Truth table33%1/3
A and B
A= $metaB= $meta->{"cmd_status"}
ABresult
0X0
100
111
479 }
480  
481  
482 61 ▶sub _txn_inner_future {
Subroutine _txn_inner_future: called CC=3 SCAR=11.0
483 61 my ($self, $txn_ctx, $conn, $inner) = @_;
484 61 ▶ $inner->_refuse_queued
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($inner) and $inner->can("_refuse_queued") 60 1 61
Condition66%2/3
builtin::blessed($inner) and $inner->can("_refuse_queued")
!ll&&!rl&&r
0160
MC/DC50%1/2
builtin::blessed($inner) and $inner->can("_refuse_queued")
builtin::blessed($inner) $inner->can("_refuse_queued")
Truth table66%2/3
A and B
A= builtin::blessed($inner)B= $inner->can("_refuse_queued")
ABresult
0X0
100
111
485 if Scalar::Util::blessed($inner) && $inner->can('_refuse_queued');
486 61 return $self->next::method($txn_ctx, $conn, $inner);
487 }
488  
489 # --- SQL shaping seam ---------------------------------------------------------
490  
491  
492 1077 ▶sub _transform_sql {
Subroutine _transform_sql: called CC=1 SCAR=0.0
493 1077 my ($self, $sql) = @_;
494 return DBIO::PostgreSQL::EV::Storage::_Refusal::of_util(
495 1077 ▶ sub { DBIO::PostgreSQL::Util::number_placeholders($sql) },
Subroutine __ANON__: called CC=1 SCAR=0.0
496 1077 );
497 }
498  
499 # --- Async query execution (transport seams) ----------------------------------
500  
501  
502 575 ▶sub _query_async {
Subroutine _query_async: called CC=4 SCAR=13.9
503 575 my ($self, $sql, $bind, $bind_attrs) = @_;
504 575 ▶ $bind //= [];
Condition50%1/2
$bind //= []
l!l
5750
MC/DC0%0/1
$bind //= []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
505 575 $sql = $self->_transform_sql($sql);
506  
507 573 my $trace_end = $self->_debug_query($sql, $bind, $bind_attrs);
508  
509 # The pool this statement begins on is the one it ends on: its completion
510 # releases to $pool, never to $self->pool asked again (karr k88).
511 573 my $pool = $self->pool;
512 540 ▶ my $f = $pool->acquire->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
513 540 my $pg = shift;
514 540 return $self->_dispatch_query($pg, $sql, $bind, $pool, $pool->_lease($pg));
515 573 });
516 # Future.pm holds a then-sequence only weakly from the Future it waits on: a
517 # caller that drops $f while the pool is full takes the chain with it, the
518 # body never runs and the connection it would borrow never comes back (core
519 # k407, as in DBIO::Async::Storage, karr k46).
520 573 ▶ $f->retain if $f->can('retain');
Branch: 1 branch
BranchTrueFalseTotal
if $f->can("retain") 573 0 573
521 573 ▶ $f->on_ready($trace_end) if $trace_end;
Branch: 1 branch
BranchTrueFalseTotal
if $trace_end 8 565 573
522 573 return $f;
523 }
524  
525  
526 543 ▶sub _dispatch_query {
Subroutine _dispatch_query: called CC=3 SCAR=11.1
527 543 my ($self, $pg, $sql, $bind, $pool, $lease) = @_;
528 543 ▶ croak '_dispatch_query needs the pool the connection was acquired from' unless $pool;
Branch: 1 branch
BranchTrueFalseTotal
unless $pool 1 542 543
529 542 my $f = $self->future_class->new;
530 542 my $pid = $$;
531 542 my $guard = $self->_pg_cancel_guard($f, $pg, $pool);
532 # The values for the text of an error: copies of the plain values, so the
533 # callback holds them for the statement's flight and never $bind itself.
534 542 my $values = _pg_error_values(@$bind);
535 539 ▶ my $on_result = sub {
Subroutine __ANON__: called CC=8 SCAR=22.2
536 539 my ($rows, $err) = @_;
537 539 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 539 539
538 # Global destruction: EV::Pg cancels the statements still waiting and calls
539 # back after the captured connection and Future are gone; nothing to deliver.
540 539 ▶ return unless defined $self && defined $pg && defined $f && defined $pool;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self and defined $pg and defined $f and defined $pool 0 539 539
Condition33%3/9
defined $self and defined $pg and defined $f and defined $pool
!ll&&!rl&&r
00539
defined $self and defined $pg and defined $f
!ll&&!rl&&r
00539
defined $self and defined $pg
!ll&&!rl&&r
00539
MC/DC0%0/4
defined $self and defined $pg and defined $f and defined $pool
defined $self defined $pg defined $f defined $pool
Truth table20%1/5
A and B and C and D
A= defined $selfB= defined $pgC= defined $fD= defined $pool
ABCDresult
0XXX0
10XX0
110X0
11100
11111
541 539 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
542 539 $guard->{answered} = 1;
543 # Here, before anything is deferred: error_fields is this statement's only now (karr k74).
544 539 $err = $self->_pg_server_error($pg, $err, $values);
545 539 ▶ my $settle = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
546 539 ▶ $self->_pg_settle_and_release($pool, $pg, $lease, $f, $err, ref $rows eq 'ARRAY' ? @$rows : $rows);
Branch: 1 branch
BranchTrueFalseTotal
ref $rows eq 'ARRAY' ? : 498 41 539
547 539 };
548 # A cancel request is out: the connection goes back when it is answered (karr k75).
549 539 ▶ if ($guard->{cancelling} && $pg->is_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($guard->{"cancelling"} and $pg->is_connected) 4 535 539
Condition66%2/3
$guard->{"cancelling"} and $pg->is_connected
!ll&&!rl&&r
53504
MC/DC50%1/2
$guard->{"cancelling"} and $pg->is_connected
$guard->{"cancelling"} $pg->is_connected
Truth table66%2/3
A and B
A= $guard->{"cancelling"}B= $pg->is_connected
ABresult
0X0
100
111
550 4 ▶ $guard->{then} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
551 4 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
552 4 $settle->();
553 4 };
554 4 return;
555 }
556 535 $settle->();
557 542 };
558 # EV::Pg croaks before it queues $on_result, so that never runs: release
559 # here, once (karr k46).
560 542 ▶ unless (eval { $pg->query_params($sql, $bind, $on_result); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $pg->query_params($sql, $bind, $on_result); 1 } }) 6 536 542
561 6 my $err = $self->_pg_dispatch_error($@);
562 6 $self->_pg_report_if_dead($pool, $pg);
563 6 $pool->release($pg, $lease);
564 6 $f->fail($err);
565 }
566 542 return $f;
567 }
568  
569 # EV::Pg's own croak -- "not connected" -- is raised from XS and carries the
570 # place of the perl statement that called it: a line of this file. That says
571 # where the library is installed and a line that moves with every release,
572 # neither about the statement (karr k88; as dbio-postgresql karr k120 did for
573 # the future_io transport). The text and its newline are what a caller gets.
574 # libpq's own messages end in a newline and carry no place; they pass as they
575 # are, and so does anything that is not a plain string.
576 82 ▶sub _pg_dispatch_error {
Subroutine _pg_dispatch_error: called CC=3 SCAR=11.0
577 82 my ($self, $err) = @_;
578 82 ▶ return $err if ref $err or not defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if ref $err or not defined $err 2 80 82
Condition100%3/3
ref $err or not defined $err
l!l&&r!l&&!r
1180
MC/DC100%2/2
ref $err or not defined $err
ref $err not defined $err
Truth table100%3/3
A or not(B)
A= ref $errB= not defined $err
ABresult
1X1
011
000
579 80 $err =~ s/ at (?:(?! at )[^\n])* line \d+\.\n\z/\n/;
580 80 return $err;
581 }
582  
583  
584 485 ▶sub _pg_refuse { goto &DBIO::PostgreSQL::EV::Storage::_Refusal::raise }
Subroutine _pg_refuse: called CC=1 SCAR=0.0
585  
586 # The connect info a refusal is about, in the DBI form the trace masks
587 # (DBIO::Util::masked_longmess): from a hash of connection parameters or with a
588 # dsn, a DBI-form arrayref, or the hash of a [ \%hash, \%opts ] pair. Nothing
589 # for anything else.
590 176 ▶sub _pg_answer_connect_info {
Subroutine _pg_answer_connect_info: called CC=5 SCAR=16.1
591 176 my ($info) = @_;
592 176 ▶ $info = $info->[0] if ref $info eq 'ARRAY' && ref $info->[0] eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $info eq "ARRAY" and ref $info->[0] eq "HASH" 73 103 176
Condition100%3/3
ref $info eq "ARRAY" and ref $info->[0] eq "HASH"
!ll&&!rl&&r
218273
MC/DC100%2/2
ref $info eq "ARRAY" and ref $info->[0] eq "HASH"
ref $info eq "ARRAY" ref $info->[0] eq "HASH"
Truth table100%3/3
A and B
A= ref $info eq "ARRAY"B= ref $info->[0] eq "HASH"
ABresult
0X0
100
111
593 176 ▶ return [ @{$info}{qw( dsn user password )}, { Password => $info->{Password} } ]
Branch: 1 branch
BranchTrueFalseTotal
if ref $info eq "HASH" 74 102 176
594 if ref $info eq 'HASH';
595 102 ▶ return [ @{$info}[0 .. 3] ] if ref $info eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $info eq "ARRAY" 82 20 102
596 20 return undef;
597 }
598  
599 # Where a refusal is thrown from. DBIO::Exception->throw names the first frame
600 # that is not this package's, nor of a package the patterns of
601 # _skip_namespace_frames name (DBIO::Carp): DBIO's own, and here Future's and
602 # EV's as well -- a refusal reached in a callback of the loop would otherwise
603 # name Future's code. %Carp::Internal keeps these frames out of the origin the
604 # exception names (the transport's method that refused) and out of the start
605 # of a stack trace.
606 {
607 package # not a class of its own: the place a refusal is thrown from
608 DBIO::PostgreSQL::EV::Storage::_Refusal;
609  
610 65 $Carp::Internal{ (__PACKAGE__) }++;
611  
612 543 ▶ sub _skip_namespace_frames { '^Future\b|^EV\b' }
Subroutine _skip_namespace_frames: called CC=1 SCAR=0.0
613  
614 515 ▶ sub raise {
Subroutine raise: called CC=1 SCAR=0.0
615 515 my ($message, $connect_info) = @_;
616 515 DBIO::Exception->throw($message, $Carp::Verbose, $connect_info);
617 }
618  
619 # A refusal of a DBIO::PostgreSQL::Util rule, raised again as this
620 # transport's. The rule croaks, naming its caller -- the code ref, in this
621 # file; Carp's short form is asked for, so that is what comes back, and the
622 # refusal is raised again in the rule's words without that place.
623 1128 ▶ sub of_util {
Subroutine of_util: called CC=4 SCAR=14.0
624 1128 my ($code) = @_;
625 1128 my ($answer, $ok, $err);
626 {
627 1128 local $@;
628 1128 $ok = eval { local $Carp::Verbose = 0; $answer = $code->(); 1 };
629 1128 $err = $@;
630 }
631 1128 ▶ return $answer if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1098 30 1128
632 30 ▶ die $err unless !ref $err && $err =~ s/ at \Q${\ __FILE__}\E line \d+[^\n]*\n\z//;
Branch: 1 branch
BranchTrueFalseTotal
unless not ref $err and $err =~ s[ at \Q${\"/home/cpancover/.cpan/build/DBIO-PostgreSQL-EV-0.900004-0/blib/lib/DBIO/PostgreSQL/EV/Storage.pm";}\E line \d+[^\n]*\n\z][] 0 30 30
Condition33%1/3
not ref $err and $err =~ s[ at \Q${\"/home/cpancover/.cpan/build/DBIO-PostgreSQL-EV-0.900004-0/blib/lib/DBIO/PostgreSQL/EV/Storage.pm";}\E line \d+[^\n]*\n\z][]
!ll&&!rl&&r
0030
MC/DC0%0/2
not ref $err and $err =~ s[ at \Q${\"/home/cpancover/.cpan/build/DBIO-PostgreSQL-EV-0.900004-0/blib/lib/DBIO/PostgreSQL/EV/Storage.pm";}\E line \d+[^\n]*\n\z][]
not ref $err $err =~ s[ at \Q${\"/home/cpancover/.cpan/build/DBIO-PostgreSQL-EV-0.900004-0/blib/lib/DBIO/PostgreSQL/EV/Storage.pm";}\E line \d+[^\n]*\n\z][]
Truth table33%1/3
not(A) and B
A= not ref $errB= $err =~ s[ at \Q${\"/home/cpancover/.cpan/build/DBIO-PostgreSQL-EV-0.900004-0/blib/lib/DBIO/PostgreSQL/EV/Storage.pm";}\E line \d+[^\n]*\n\z][]
ABresult
0X0
100
111
633 30 raise($err);
634 }
635 }
636  
637  
638 65 ▶sub _pg_server_error {
Subroutine _pg_server_error: called CC=13 SCAR=26.0
639 906 my ($self, $pg, $err, $values) = @_;
640 906 ▶ return $err if ref $err or not defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if ref $err or not defined $err 777 129 906
Condition100%3/3
ref $err or not defined $err
l!l&&r!l&&!r
1776129
MC/DC100%2/2
ref $err or not defined $err
ref $err not defined $err
Truth table100%3/3
A or not(B)
A= ref $errB= not defined $err
ABresult
1X1
011
000
641 129 my $text = $self->_pg_mask_error_text($err, $values);
642 129 ▶ return $text unless Scalar::Util::blessed($pg) && $pg->can('error_fields');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($pg) and $pg->can("error_fields") 93 36 129
Condition100%3/3
builtin::blessed($pg) and $pg->can("error_fields")
!ll&&!rl&&r
19236
MC/DC100%2/2
builtin::blessed($pg) and $pg->can("error_fields")
builtin::blessed($pg) $pg->can("error_fields")
Truth table100%3/3
A and B
A= builtin::blessed($pg)B= $pg->can("error_fields")
ABresult
0X0
100
111
643 36 my $fields = $pg->error_fields;
644 36 ▶ return $text unless ref $fields eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fields eq "HASH" 4 32 36
645 32 my ($severity, $primary, $sqlstate) = @$fields{qw( severity primary sqlstate )};
646 32 ▶ return $text unless defined $severity && length $severity
Branch: 1 branch
BranchTrueFalseTotal
unless defined $severity and length $severity and defined $primary and length $primary 1 31 32
Condition66%6/9
defined $severity and length $severity and defined $primary and length $primary
!ll&&!rl&&r
1031
defined $severity and length $severity and defined $primary
!ll&&!rl&&r
1031
defined $severity and length $severity
!ll&&!rl&&r
1031
MC/DC25%1/4
defined $severity and length $severity and defined $primary and length $primary
defined $severity length $severity defined $primary length $primary
Truth table40%2/5
A and B and C and D
A= defined $severityB= length $severityC= defined $primaryD= length $primary
ABCDresult
0XXX0
10XX0
110X0
11100
11111
647 && defined $primary && length $primary;
648 31 ▶ my $verbose = defined $sqlstate && length $sqlstate ? qr/(?:\Q$sqlstate\E: )?/ : qr//;
Branch: 1 branch
BranchTrueFalseTotal
defined $sqlstate && length $sqlstate ? : 31 0 31
Condition33%1/3
defined $sqlstate and length $sqlstate
!ll&&!rl&&r
0031
MC/DC0%0/2
defined $sqlstate and length $sqlstate
defined $sqlstate length $sqlstate
Truth table33%1/3
A and B
A= defined $sqlstateB= length $sqlstate
ABresult
0X0
100
111
649 31 ▶ return $text unless $err =~ /\A\Q$severity\E: $verbose\Q$primary\E/;
Branch: 1 branch
BranchTrueFalseTotal
unless $err =~ /\A\Q$severity\E: $verbose\Q$primary\E/ 7 24 31
650 return DBIO::Exception::Server->new(
651 message => $text,
652 sqlstate => $sqlstate,
653 constraint => $fields->{constraint},
654 table => $fields->{table},
655 24 detail => $self->_pg_mask_error_detail($fields->{detail}, $values),
656 );
657 }
658  
659  
660 141 ▶sub _pg_mask_error_text {
Subroutine _pg_mask_error_text: called CC=4 SCAR=13.9
661 141 my ($self, $text, $values) = @_;
662 141 ▶ return $text if not defined $text or ref $text;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $text or ref $text 2 139 141
Condition100%3/3
not defined $text or ref $text
l!l&&r!l&&!r
11139
MC/DC100%2/2
not defined $text or ref $text
not defined $text ref $text
Truth table100%3/3
not(A) or B
A= not defined $textB= ref $text
ABresult
1X1
011
000
663 139 my $masked = eval {
664 139 DBIO::PostgreSQL::Util::mask_error_details(
665 DBIO::Util::mask_bound_values($text, $values)
666 );
667 };
668 139 ▶ return defined $masked ? $masked : $text;
Branch: 1 branch
BranchTrueFalseTotal
defined $masked ? : 139 0 139
669 }
670  
671 # The detail field of the server with the values taken out: the bound values,
672 # then the values of its forms (Key (name)=(***), Failing row contains (***)).
673 24 ▶sub _pg_mask_error_detail {
Subroutine _pg_mask_error_detail: called CC=4 SCAR=14.0
674 24 my ($self, $detail, $values) = @_;
675 24 ▶ return $detail if not defined $detail or ref $detail;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $detail or ref $detail 15 9 24
Condition66%2/3
not defined $detail or ref $detail
l!l&&r!l&&!r
1509
MC/DC50%1/2
not defined $detail or ref $detail
not defined $detail ref $detail
Truth table66%2/3
not(A) or B
A= not defined $detailB= ref $detail
ABresult
1X1
011
000
676 9 my $masked = eval {
677 9 DBIO::PostgreSQL::Util::mask_detail_values(
678 DBIO::Util::mask_bound_values($detail, $values)
679 );
680 };
681 9 ▶ return defined $masked ? $masked : $detail;
Branch: 1 branch
BranchTrueFalseTotal
defined $masked ? : 9 0 9
682 }
683  
684 # The values of a statement's binds as the net takes them, read the way core's
685 # DBIO::Storage::DBI::_plain_error_values reads a bind list: undef and a
686 # reference that is no plain value fall away, an object with a stringification
687 # is its string. A new list of copies -- what a callback keeps for the flight
688 # of its statement in place of the bind array (t/05).
689 842 ▶sub _pg_error_values {
Subroutine _pg_error_values: called CC=6 SCAR=17.9
690 842 my @plain;
691 842 for my $v (@_) {
692 486 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 12 474 486
693 474 ▶ if (length ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (length ref $v) { } 6 468 474
694 6 ▶ my $p = DBIO::Util::is_plain_value($v) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $p = &DBIO::Util::is_plain_value($v) or next 2 4 6
695 2 ▶ push @plain, "$$p" if defined $$p;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$p 2 0 2
696 }
697 else {
698 468 push @plain, $v;
699 }
700 }
701 842 return \@plain;
702 }
703  
704 # How a callback this class hands to EV::Pg ends a pooled statement --
705 # _dispatch_query, _dispatch_notify, copy_in: settle $f -- fail it with $err if
706 # there is one, else done with @result -- and only then hand $pg back to
707 # $pool, the pool it was acquired from, with $lease, the lease of the borrow
708 # (a stale release then takes nothing, karr k89). Settle first: the Future's then callbacks run with $pg still checked
709 # out (karr k44, k51), and the release runs inside the caller's delivery frame.
710 # An on_done / on_ready / on_fail that dies escapes done / fail, so the release
711 # must not depend on it returning; the die goes on to EV::Pg after the release,
712 # which warns it (karr k50).
713 #
714 # "There is one" is a defined $err, here and in every callback of this class:
715 # EV::Pg calls a statement's callback with ($result) when it went through and
716 # with (undef, $message) when it did not, the message a string that is never
717 # empty -- libpq's, or a text of its own where libpq has none ("connection
718 # lost", "pipeline aborted", ...; Pg.xs, 0.02 and 0.07 alike).
719 #
720 # $pool is handed in by whoever acquired $pg, and is the only pool this talks
721 # to -- never $self->pool (karr k88, this transport's half of karr dbio-async
722 # k46). The settling runs the caller's callbacks, and one of them may
723 # disconnect the storage: the slot pool() fills is empty then, and asked here
724 # it built a new pool on the disconnected storage, to release into it a
725 # connection that pool had never held -- or died, where the storage had
726 # nothing left to connect from. $pool is shut down by then, and takes the
727 # report and the release as no-ops.
728 704 ▶sub _pg_settle_and_release {
Subroutine _pg_settle_and_release: called CC=4 SCAR=13.9
729 704 my ($self, $pool, $pg, $lease, $f, $err, @result) = @_;
730 704 my $settled = eval {
731 704 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) { } 131 573 704
732 131 $f->fail($err);
733 } else {
734 573 $f->done(@result);
735 }
736 658 1;
737 };
738 704 my $settle_err = $@;
739 704 ▶ $self->_pg_report_if_dead($pool, $pg) if defined $err; # before the release (karr k49)
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 131 573 704
740 704 $pool->release($pg, $lease);
741 704 ▶ die $settle_err unless $settled;
Branch: 1 branch
BranchTrueFalseTotal
unless $settled 46 658 704
742 658 return;
743 }
744  
745  
746 146 ▶sub _pg_report_if_dead {
Subroutine _pg_report_if_dead: called CC=2 SCAR=6.9
747 146 my ($self, $pool, $pg) = @_;
748 146 ▶ $pool->_mark_connection_dead($pg) unless $pg->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $pg->is_connected 44 102 146
749 146 return;
750 }
751  
752  
753 93 ▶sub _reset_pool_connection {
Subroutine _reset_pool_connection: called CC=2 SCAR=7.2
754 93 my ($self, $pg) = @_;
755 93 my $f = $self->future_class->new;
756 93 my $pid = $$;
757  
758 88 ▶ my $on_result = sub {
Subroutine __ANON__: called CC=7 SCAR=21.0
759 88 my (undef, $err) = @_;
760 88 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 88 88
761 # Global destruction: EV::Pg cancels the statements still waiting and calls
762 # back after the captured connection and Future are gone; nothing to deliver.
763 88 ▶ return unless defined $self && defined $pg && defined $f;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self and defined $pg and defined $f 0 88 88
Condition33%2/6
defined $self and defined $pg and defined $f
!ll&&!rl&&r
0088
defined $self and defined $pg
!ll&&!rl&&r
0088
MC/DC0%0/3
defined $self and defined $pg and defined $f
defined $self defined $pg defined $f
Truth table25%1/4
A and B and C
A= defined $selfB= defined $pgC= defined $f
ABCresult
0XX0
10X0
1100
1111
764 88 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
765 88 ▶ return if $f->is_ready; # cancelled by the pool: it retired the connection
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 24 64 88
766 64 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 2 62 64
767 2 $f->fail("the session of a pool connection could not be reset: DISCARD ALL failed: "
768 . $self->_pg_mask_error_text($err));
769 2 return;
770 }
771 # The connect actions run the EV loop to the end of each statement
772 # (_run_pool_connect_statement), which EV::Pg does not take inside a
773 # callback of the very connection they run on: it would hand the answer of
774 # the first statement to this callback, still running. So they run from a
775 # timer, after this callback has returned.
776 62 my $timer;
777 62 ▶ $timer = EV::timer(0, 0, sub {
Subroutine __ANON__: called CC=7 SCAR=21.8
778 62 undef $timer;
779 62 ▶ return if $$ != $pid || !defined $f || !defined $pg || $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid or not defined $f or not defined $pg or $f->is_ready 0 62 62
Condition33%3/9
$$ != $pid or not defined $f or not defined $pg or $f->is_ready
l!l&&r!l&&!r
0062
$$ != $pid or not defined $f or not defined $pg
l!l&&r!l&&!r
0062
$$ != $pid or not defined $f
l!l&&r!l&&!r
0062
MC/DC0%0/4
$$ != $pid or not defined $f or not defined $pg or $f->is_ready
$$ != $pid not defined $f not defined $pg $f->is_ready
Truth table20%1/5
A or not(B) or not(C) or D
A= $$ != $pidB= not defined $fC= not defined $pgD= $f->is_ready
ABCDresult
1XXX1
01XX1
001X1
00011
00000
780 62 ▶ unless ($pg->is_connected && eval { $self->_setup_pool_connection($pg); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($pg->is_connected and eval { do { $self->_setup_pool_connection($pg); 1 } }) 2 60 62
Condition66%2/3
$pg->is_connected and eval { do { $self->_setup_pool_connection($pg); 1 } }
!ll&&!rl&&r
0260
MC/DC50%1/2
$pg->is_connected and eval { do { $self->_setup_pool_connection($pg); 1 } }
$pg->is_connected eval { do { $self->_setup_pool_connection($pg); 1 } }
Truth table66%2/3
A and B
A= $pg->is_connectedB= eval { do { $self->_setup_pool_connection($pg); 1 } }
ABresult
0X0
100
111
781 2 $f->fail("the session of a pool connection could not be reset: "
782 . "its connect actions could not be run again\n");
783 2 return;
784 }
785 60 $f->done;
786 62 });
787 62 return;
788 93 };
789  
790 # EV::Pg croaks before it queues $on_result ("not connected"): the Future
791 # fails and the pool retires the connection.
792 93 ▶ unless (eval { $pg->query('DISCARD ALL', $on_result); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $pg->query("DISCARD ALL", $on_result); 1 } }) 1 92 93
793 1 $f->fail($self->_pg_dispatch_error($@));
794 }
795 93 return $f;
796 }
797  
798 # --- The pinned path's one-statement-at-a-time queue (karr k48) ---------------
799 # See _query_async_pinned. The state is kept per connection, by refaddr, in
800 # records of { pid, conn } -- the process that made the record (karr k45) and
801 # the connection, weakly. A record of another process, or whose connection has
802 # gone, is stale and counts as absent, so it can neither outlive its connection
803 # nor land on a later one at the same address (as Pool.pm's %PG_OPENED).
804 #
805 # %PG_PINNED a record exists while a statement of the pinned path is in
806 # flight on the connection outside pipeline mode, plus
807 # queue the statements waiting behind it, [ $sql, $bind, $f ]
808 # ending 'COMMIT' / 'ROLLBACK' once one of those is queued
809 # or in flight; a statement issued then is refused.
810 # Core ends the transaction context at that moment
811 # too (core k237), so what still meets this refusal
812 # is a caller past the context, on txn_conn
813 # %PG_PIPELINE a record exists between _pipeline_enter and _pipeline_exit
814 65 my %PG_PINNED;
815 my %PG_PIPELINE;
816  
817 # A statement in flight whose Future is cancelled asks the server to stop it
818 # (karr k75): EV::Pg cancel_async, libpq 17+, never the blocking cancel. Returns
819 # the statement's guard, which the statement's completion callback keeps:
820 # answered the statement's result is in
821 # cancelling a cancel request is out and not answered
822 # then what the result's callback left undone while it was
823 # The callback hands the rest of its work to `then` when it finds a request
824 # out, and the request's answer runs it: the connection goes on -- released,
825 # its next statement sent -- only when result and answer are both in, else the
826 # request reaches the next statement of the connection. A request that ends
827 # with an error marks the connection dead for its pool ($pool, else the
828 # storage's, never built here: karr k88). The guard stays empty, and the cancel
829 # a no-op, without the capability, for a Future that is not pending, and in
830 # pipeline mode.
831 767 ▶sub _pg_cancel_guard {
Subroutine _pg_cancel_guard: called CC=3 SCAR=11.4
832 767 my ($self, $f, $pg, $pool) = @_;
833 767 my $guard = {};
834 767 ▶ return $guard if $f->is_ready or !$pg->can('cancel_async');
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready or not $pg->can("cancel_async") 685 82 767
Condition100%3/3
$f->is_ready or not $pg->can("cancel_async")
l!l&&r!l&&!r
268382
MC/DC100%2/2
$f->is_ready or not $pg->can("cancel_async")
$f->is_ready not $pg->can("cancel_async")
Truth table100%3/3
A or not(B)
A= $f->is_readyB= not $pg->can("cancel_async")
ABresult
1X1
011
000
835 82 my $pid = $$;
836 82 Scalar::Util::weaken(my $weak_pg = $pg);
837 18 ▶ $f->on_cancel(sub {
Subroutine __ANON__: called CC=9 SCAR=23.8
838 18 ▶ return if $$ != $pid or $guard->{answered} or $guard->{cancelling};
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid or $guard->{"answered"} or $guard->{"cancelling"} 0 18 18
Condition33%2/6
$$ != $pid or $guard->{"answered"} or $guard->{"cancelling"}
l!l&&r!l&&!r
0018
$$ != $pid or $guard->{"answered"}
l!l&&r!l&&!r
0018
MC/DC0%0/3
$$ != $pid or $guard->{"answered"} or $guard->{"cancelling"}
$$ != $pid $guard->{"answered"} $guard->{"cancelling"}
Truth table25%1/4
A or B or C
A= $$ != $pidB= $guard->{"answered"}C= $guard->{"cancelling"}
ABCresult
1XX1
01X1
0011
0000
839 18 ▶ my $pg = $weak_pg or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pg = $weak_pg or return 18 0 18
840 18 ▶ return if _pg_conn_record(\%PG_PIPELINE, $pg) or !$pg->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
if _pg_conn_record(\%PG_PIPELINE, $pg) or not $pg->is_connected 0 18 18
Condition33%1/3
_pg_conn_record(\%PG_PIPELINE, $pg) or not $pg->is_connected
l!l&&r!l&&!r
0018
MC/DC0%0/2
_pg_conn_record(\%PG_PIPELINE, $pg) or not $pg->is_connected
_pg_conn_record(\%PG_PIPELINE, $pg) not $pg->is_connected
Truth table33%1/3
A or not(B)
A= _pg_conn_record(\%PG_PIPELINE, $pg)B= not $pg->is_connected
ABresult
1X1
011
000
841 # A retirement has sent the request already (Pool::_shutdown_connection).
842 18 ▶ return if DBIO::PostgreSQL::EV::Pool::_pg_cancel_is_out($pg);
Branch: 1 branch
BranchTrueFalseTotal
if DBIO::PostgreSQL::EV::Pool::_pg_cancel_is_out($pg) 8 10 18
843 10 $guard->{cancelling} = 1;
844 10 DBIO::PostgreSQL::EV::Pool::_pg_cancel_begin($pg);
845 10 my $sent = eval {
846 10 ▶ $pg->cancel_async(sub {
Subroutine __ANON__: called CC=8 SCAR=21.2
847 10 my ($ok, $err) = @_;
848 10 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 10 10
849 10 $guard->{cancelling} = 0;
850 10 ▶ if (!$ok and my $conn = $weak_pg) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok and my $conn = $weak_pg) 4 6 10
Condition66%2/3
not $ok and my $conn = $weak_pg
!ll&&!rl&&r
604
MC/DC50%1/2
not $ok and my $conn = $weak_pg
not $ok my $conn = $weak_pg
Truth table66%2/3
not(A) and B
A= not $okB= my $conn = $weak_pg
ABresult
0X0
100
111
851 4 ▶ my $owner = $pool || $self->{pool};
Condition66%2/3
$pool || $self->{'pool'}
l!l&&r!l&&!r
310
MC/DC0%0/2
$pool || $self->{'pool'}
$pool $self->{'pool'}
Truth table66%2/3
A || B
A= $poolB= $self->{'pool'}
ABresult
1X1
011
000
852 4 ▶ $owner->_mark_connection_dead($conn) if $owner;
Branch: 1 branch
BranchTrueFalseTotal
if $owner 4 0 4
853 }
854 10 my $then = delete $guard->{then};
855 10 ▶ my $ran = eval { $then->() if $then; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if $then 6 4 10
856 10 my $died = $@;
857 # A retirement that waited for this answer closes the connection now.
858 10 DBIO::PostgreSQL::EV::Pool::_pg_cancel_end($weak_pg, $ok, $err);
859 10 ▶ die $died unless $ran;
Branch: 1 branch
BranchTrueFalseTotal
unless $ran 0 10 10
860 10 });
861 10 1;
862 };
863 10 ▶ unless ($sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sent) 0 10 10
864 0 $guard->{cancelling} = 0;
865 0 DBIO::PostgreSQL::EV::Pool::_pg_cancel_end($pg);
866 }
867 82 });
868 82 return $guard;
869 }
870  
871  
872 256 ▶sub _query_async_pinned {
Subroutine _query_async_pinned: called CC=12 SCAR=25.1
873 256 my ($self, $pg, $sql, $bind, $bind_attrs) = @_;
874 256 ▶ $bind //= [];
Condition100%2/2
$bind //= []
l!l
2542
MC/DC100%1/1
$bind //= []
$bind
Truth table100%2/2
A
A= $bind
Aresult
11
00
875 256 $sql = $self->_transform_sql($sql);
876  
877 256 my $trace_end = $self->_debug_query($sql, $bind, $bind_attrs);
878  
879 256 my $f = $self->future_class->new;
880 256 ▶ $f->on_ready($trace_end) if $trace_end;
Branch: 1 branch
BranchTrueFalseTotal
if $trace_end 5 251 256
881  
882 # Pipeline mode: libpq takes the statement at once (no callbacks on $f yet).
883 256 ▶ if (my $pipeline = _pg_conn_record(\%PG_PIPELINE, $pg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pipeline = _pg_conn_record(\%PG_PIPELINE, $pg)) 25 231 256
884 # Behind the sync point the server does not answer a statement until the
885 # next sync (karr k88): it waits here for pipeline mode to be left.
886 25 ▶ if ($pipeline->{synced}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pipeline->{"synced"}) 0 25 25
887 0 push @{ $pipeline->{queue} }, [ $sql, $bind, $f ];
888 0 return $f;
889 }
890 25 my $err = $self->_pg_send_pinned($pg, $sql, $bind, $f, 0);
891 25 ▶ $f->fail($err) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 25 25
892 25 return $f;
893 }
894  
895 231 my $st = _pg_conn_record(\%PG_PINNED, $pg);
896 231 ▶ if ($st && $st->{ending}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($st and $st->{"ending"}) 4 227 231
Condition100%3/3
$st and $st->{"ending"}
!ll&&!rl&&r
191364
MC/DC100%2/2
$st and $st->{"ending"}
$st $st->{"ending"}
Truth table100%3/3
A and B
A= $stB= $st->{"ending"}
ABresult
0X0
100
111
897 4 $f->fail(
898 "Statement refused: this transaction's $st->{ending} has already been "
899 . 'issued on its connection, and the statement would run after it, '
900 . 'outside the transaction. Chain every statement of a txn_do coderef '
901 . "into the Future the coderef returns.\n"
902 );
903 4 return $f;
904 }
905  
906 227 ▶ my $in_flight = $st ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$st ? : 36 191 227
907 227 ▶ $st ||= _pg_conn_record(\%PG_PINNED, $pg, 1);
Condition66%2/3
$st ||= _pg_conn_record(\%PG_PINNED, $pg, 1)
l!l&&r!l&&!r
361910
MC/DC0%0/2
$st ||= _pg_conn_record(\%PG_PINNED, $pg, 1)
$st _pg_conn_record(\%PG_PINNED, $pg, 1)
Truth table66%2/3
A ||= B
A= $stB= _pg_conn_record(\%PG_PINNED, $pg, 1)
ABresult
1X1
011
000
908 227 ▶ $st->{ending} = $sql if $sql =~ /\A(?:COMMIT|ROLLBACK)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /\A(?:COMMIT|ROLLBACK)\z/ 69 158 227
909 227 push @{ $st->{queue} }, [ $sql, $bind, $f ];
910 227 ▶ $self->_pg_pinned_next($pg) unless $in_flight;
Branch: 1 branch
BranchTrueFalseTotal
unless $in_flight 191 36 227
911 227 return $f;
912 }
913  
914 1152 ▶sub _pg_conn_record {
Subroutine _pg_conn_record: called CC=5 SCAR=16.2
915 1152 my ($table, $pg, $create) = @_;
916 1152 my $addr = Scalar::Util::refaddr($pg);
917 1152 my $rec = $table->{$addr};
918 1152 ▶ return $rec if $rec && $rec->{pid} == $$ && defined $rec->{conn};
Branch: 1 branch
BranchTrueFalseTotal
if $rec and $rec->{"pid"} == $$ and defined $rec->{"conn"} 499 653 1152
Condition66%4/6
$rec and $rec->{"pid"} == $$ and defined $rec->{"conn"}
!ll&&!rl&&r
6530499
$rec and $rec->{"pid"} == $$
!ll&&!rl&&r
6530499
MC/DC33%1/3
$rec and $rec->{"pid"} == $$ and defined $rec->{"conn"}
$rec $rec->{"pid"} == $$ defined $rec->{"conn"}
Truth table50%2/4
A and B and C
A= $recB= $rec->{"pid"} == $$C= defined $rec->{"conn"}
ABCresult
0XX0
10X0
1100
1111
919 653 delete $table->{$addr};
920 653 ▶ return unless $create;
Branch: 1 branch
BranchTrueFalseTotal
unless $create 446 207 653
921 207 $rec = $table->{$addr} = { pid => $$, conn => $pg, queue => [] };
922 207 Scalar::Util::weaken($rec->{conn});
923 207 return $rec;
924 }
925  
926 # Nothing of the pinned path is in flight on $pg: send what waits in its queue,
927 # up to the first statement EV::Pg takes -- one cancelled in the queue is
928 # skipped, bar COMMIT and ROLLBACK (karr k75). When none is left, the record goes --
929 # the connection is idle. A statement EV::Pg refuses fails only once that is
930 # decided, so a callback on it finds the queue consistent; a die out of such a
931 # callback is returned, for the caller to rethrow once it has settled what it
932 # owes (the karr k50 order).
933 406 ▶sub _pg_pinned_next {
Subroutine _pg_pinned_next: called CC=10 SCAR=23.3
934 406 my ($self, $pg) = @_;
935 406 ▶ my $st = _pg_conn_record(\%PG_PINNED, $pg) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $st = _pg_conn_record(\%PG_PINNED, $pg) or return 406 0 406
936 406 my ($sent, @refused);
937 406 ▶ while (my $next = shift @{ $st->{queue} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = shift @{$$st{"queue"};}) 227 191 418
938 227 my ($sql, $bind, $f) = @$next;
939 # Cancelled while it waited: not sent (karr k75). Core's COMMIT and
940 # ROLLBACK are not skipped -- the queue knows them as the end ('ending'),
941 # and the transaction would stay open, with its locks.
942 227 ▶ next if $f->is_cancelled && $sql !~ /\A(?:COMMIT|ROLLBACK)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_cancelled and not $sql =~ /\A(?:COMMIT|ROLLBACK)\z/ 2 225 227
Condition100%3/3
$f->is_cancelled and not $sql =~ /\A(?:COMMIT|ROLLBACK)\z/
!ll&&!rl&&r
22322
MC/DC100%2/2
$f->is_cancelled and not $sql =~ /\A(?:COMMIT|ROLLBACK)\z/
$f->is_cancelled not $sql =~ /\A(?:COMMIT|ROLLBACK)\z/
Truth table100%3/3
A and not(B)
A= $f->is_cancelledB= not $sql =~ /\A(?:COMMIT|ROLLBACK)\z/
ABresult
0X0
100
111
943 225 my $err = $self->_pg_send_pinned($pg, $sql, $bind, $f, 1);
944 225 ▶ if (!defined $err) { $sent = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $err) 215 10 225
945 10 push @refused, [ $f, $err ];
946 }
947 406 ▶ delete $PG_PINNED{ Scalar::Util::refaddr($pg) } unless $sent;
Branch: 1 branch
BranchTrueFalseTotal
unless $sent 191 215 406
948  
949 406 my $died;
950 406 for my $refused (@refused) {
951 10 my ($f, $err) = @$refused;
952 10 ▶ eval { $f->fail($err); 1 } or $died //= $@;
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $f->fail($err); 1 } } or $died //= $@ 10 0 10
Condition0%0/3
$died //= $@
l!l&&r!l&&!r
000
MC/DC0%0/2
$died //= $@
$died $@
Truth table0%0/3
A //= B
A= $diedB= $@
ABresult
1X1
011
000
953 }
954 406 return $died;
955 }
956  
957 # Send one statement of the pinned path; undef once EV::Pg has taken it, else
958 # EV::Pg's error -- it croaks before it queues the callback (karr k48, the k46
959 # pattern). Named sub, like _dispatch_query (karr k29): the completion callback
960 # is created here and does not hold $bind. The callback of a $queued statement
961 # sends the next one waiting before it settles $f.
962 250 ▶sub _pg_send_pinned {
Subroutine _pg_send_pinned: called CC=3 SCAR=11.1
963 250 my ($self, $pg, $sql, $bind, $f, $queued) = @_;
964 250 my $pid = $$;
965 # Pipeline mode (not $queued) sends no cancel request (karr k75).
966 250 ▶ my $guard = $queued ? $self->_pg_cancel_guard($f, $pg) : {};
Branch: 1 branch
BranchTrueFalseTotal
$queued ? : 225 25 250
967 250 my $values = _pg_error_values(@$bind); # as in _dispatch_query: copies, not $bind
968 240 ▶ my $on_result = sub {
Subroutine __ANON__: called CC=7 SCAR=20.0
969 240 my ($rows, $err) = @_;
970 240 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 240 240
971 # Global destruction: EV::Pg cancels the statements still waiting and calls
972 # back after the captured connection and Future are gone; nothing to deliver.
973 240 ▶ return unless defined $self && defined $pg && defined $f;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self and defined $pg and defined $f 0 240 240
Condition33%2/6
defined $self and defined $pg and defined $f
!ll&&!rl&&r
00240
defined $self and defined $pg
!ll&&!rl&&r
00240
MC/DC0%0/3
defined $self and defined $pg and defined $f
defined $self defined $pg defined $f
Truth table25%1/4
A and B and C
A= defined $selfB= defined $pgC= defined $f
ABCresult
0XX0
10X0
1100
1111
974 240 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
975 240 $guard->{answered} = 1;
976 240 $err = $self->_pg_server_error($pg, $err, $values); # karr k74
977 240 ▶ my $settle = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
978 # libpq is idle again -- EV::Pg delivers after the statement's last result
979 # -- so the next statement goes out before $f's callbacks can issue more.
980 240 ▶ my $next_died = $queued ? $self->_pg_pinned_next($pg) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$queued ? : 215 25 240
981 240 ▶ if (defined $err) { $f->fail($err) } else { $f->done(ref $rows eq 'ARRAY' ? @$rows : $rows) }
Branch: 2 branches
BranchTrueFalseTotal
if (defined $err) { } 38 202 240
ref $rows eq 'ARRAY' ? : 98 104 202
982 240 ▶ die $next_died if defined $next_died;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next_died 0 240 240
983 # Do NOT release -- the connection is pinned for the duration of the txn
984 240 };
985 # A cancel request is out: the next statement goes when it is answered (karr k75).
986 240 ▶ if ($guard->{cancelling} && $pg->is_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($guard->{"cancelling"} and $pg->is_connected) 2 238 240
Condition66%2/3
$guard->{"cancelling"} and $pg->is_connected
!ll&&!rl&&r
23802
MC/DC50%1/2
$guard->{"cancelling"} and $pg->is_connected
$guard->{"cancelling"} $pg->is_connected
Truth table66%2/3
A and B
A= $guard->{"cancelling"}B= $pg->is_connected
ABresult
0X0
100
111
987 2 ▶ $guard->{then} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
988 2 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
989 2 $settle->();
990 2 };
991 2 return;
992 }
993 238 $settle->();
994 250 };
995 250 my $sent = eval { $pg->query_params($sql, $bind, $on_result); 1 };
996 250 ▶ return $sent ? undef : $self->_pg_dispatch_error($@);
Branch: 1 branch
BranchTrueFalseTotal
$sent ? : 240 10 250
997 }
998  
999 # --- Pool connect-action replay seam (core k68) -------------------------------
1000  
1001  
1002 40 ▶sub _run_pool_connect_statement {
Subroutine _run_pool_connect_statement: called CC=7 SCAR=19.5
1003 40 my ($self, $conn, $sql, $attrs, @bind) = @_;
1004 40 require EV;
1005  
1006 # The statement's result is always waited for on the EV loop: refuse before
1007 # anything is sent (karr k52).
1008 40 $self->_pg_refuse_connect_wait;
1009  
1010 # Whatever the loop runs from here on runs above the spawn that opens $conn:
1011 # the pool counts the connection and hands it out only after this has
1012 # returned (karr k88).
1013 38 my $replaying = DBIO::PostgreSQL::EV::Future->_replaying($conn);
1014  
1015 # The connection was just spawned; its async connect may still be in flight.
1016 # Block the EV loop until it is ready (the core seam's synchronous contract).
1017 # Prefer the pool's per-connection readiness Future
1018 # (DBIO::Storage::PoolBase::_connection_ready_future, core k75), which is
1019 # FAILED on a connect error -- so a bad connect surfaces as a thrown error
1020 # here rather than busy-spinning on is_connected forever. Fall back to
1021 # is_connected for a pool that does not expose the readiness seam (e.g. a
1022 # bare test double that is not a PoolBase subclass at all).
1023 38 my $pool = $self->pool;
1024 38 ▶ if ($pool->can('_connection_ready_future')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pool->can('_connection_ready_future')) { } 28 10 38
1025 28 my $ready = $pool->_connection_ready_future($conn);
1026 28 ▶ $self->_pg_connect_wait(sub { $ready->is_ready });
Subroutine __ANON__: called CC=1 SCAR=0.0
1027 28 $ready->get; # rethrows a connect error
1028 }
1029 else {
1030 10 ▶ $self->_pg_connect_wait(sub { $conn->is_connected });
Subroutine __ANON__: called CC=1 SCAR=0.0
1031 }
1032  
1033 38 my $done = 0;
1034 38 my ($res, $err);
1035 38 ▶ my $cb = sub { ($res, $err) = @_; $done = 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
1036 38 ▶ if (@bind) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bind) { } 3 35 38
1037 3 $conn->query_params($self->_transform_sql($sql), [@bind], $cb);
1038 }
1039 else {
1040 35 $conn->query($sql, $cb);
1041 }
1042 38 ▶ $self->_pg_connect_wait(sub { $done });
Subroutine __ANON__: called CC=1 SCAR=0.0
1043  
1044 37 ▶ croak 'pool connect statement failed: '
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 4 33 37
1045 . $self->_pg_mask_error_text($err, _pg_error_values(@bind))
1046 if defined $err;
1047  
1048 # dbio_rows (core k358): hand back the rows, as selectall_arrayref does. A
1049 # statement that answers no rows (SET) has a command tag, not an array.
1050 33 ▶ if (ref $attrs eq 'HASH' and $attrs->{dbio_rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrs eq "HASH" and $attrs->{"dbio_rows"}) 3 30 33
Condition100%3/3
ref $attrs eq "HASH" and $attrs->{"dbio_rows"}
!ll&&!rl&&r
2913
MC/DC100%2/2
ref $attrs eq "HASH" and $attrs->{"dbio_rows"}
ref $attrs eq "HASH" $attrs->{"dbio_rows"}
Truth table100%3/3
A and B
A= ref $attrs eq "HASH"B= $attrs->{"dbio_rows"}
ABresult
0X0
100
111
1051 3 ▶ return ref $res eq 'ARRAY' ? $res : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $res eq 'ARRAY' ? : 2 1 3
1052 }
1053 30 return;
1054 }
1055  
1056 # Run the EV loop until $ready->() is true, refused before every iteration.
1057 76 ▶sub _pg_connect_wait {
Subroutine _pg_connect_wait: called CC=2 SCAR=6.9
1058 76 my ($self, $ready) = @_;
1059 76 ▶ until ($ready->()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$ready()) 48 75 123
1060 48 $self->_pg_refuse_connect_wait;
1061 47 EV::run(EV::RUN_ONCE());
1062 }
1063 75 return;
1064 }
1065  
1066 # Die while one of this transport's callbacks runs for a connection with more
1067 # results due: a loop run now would read them (karr k52; the await refusal of
1068 # karr k44).
1069 88 ▶sub _pg_refuse_connect_wait {
Subroutine _pg_refuse_connect_wait: called CC=2 SCAR=6.9
1070 88 my ($self) = @_;
1071 88 ▶ my $pg = DBIO::PostgreSQL::EV::Future::_delivering_with_more_due() or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pg = DBIO::PostgreSQL::EV::Future::_delivering_with_more_due() or return 3 85 88
1072 3 croak 'Blocking wait refused: the pool is opening a new connection and '
1073 . 'replaying on_connect_do / on_connect_call on it, which runs the EV '
1074 . 'loop, inside a callback of an EV::Pg connection that has '
1075 . ($pg->pending_count - 1) . ' more result(s) due; EV::Pg would hand '
1076 . 'the next of them to the callback that is still running, then spin. '
1077 . 'Chain this call with ->then onto the statement in flight, or run it '
1078 . 'on the transaction context ($ctx->select_async, ...)';
1079 }
1080  
1081 # --- Pipeline mode seams ------------------------------------------------------
1082 # The inherited DBIO::Storage::Async::pipeline scaffold acquires a connection,
1083 # brackets the batch with these three seams and releases the connection. EV::Pg
1084 # expresses pipelining natively, so the base scaffold fits exactly -- no
1085 # parallel pipeline() is needed (WP4 decision).
1086  
1087  
1088 17 ▶sub _pipeline_enter {
Subroutine _pipeline_enter: called CC=2 SCAR=6.9
1089 17 my ($self, $pg) = @_;
1090 # EV::Pg's croak names the line here that called it (karr k88)
1091 17 ▶ eval { $pg->enter_pipeline; 1 } or die $self->_pg_dispatch_error($@);
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $pg->enter_pipeline; 1 } } or die $self->_pg_dispatch_error($@) 16 1 17
1092 16 _pg_conn_record(\%PG_PIPELINE, $pg, 1);
1093 16 return;
1094 }
1095  
1096  
1097 17 ▶sub _pipeline_sync {
Subroutine _pipeline_sync: called CC=2 SCAR=7.0
1098 17 my ($self, $pg) = @_;
1099 17 my $f = $self->future_class->new;
1100 17 my $pid = $$;
1101 17 my $pipeline = _pg_conn_record(\%PG_PIPELINE, $pg);
1102 19 ▶ $pg->pipeline_sync(sub {
Subroutine __ANON__: called CC=7 SCAR=20.2
1103 19 my (undef, $err) = @_;
1104 19 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 1 18 19
1105 18 ▶ return unless defined $self && defined $pg && defined $f; # global destruction
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self and defined $pg and defined $f 0 18 18
Condition33%2/6
defined $self and defined $pg and defined $f
!ll&&!rl&&r
0018
defined $self and defined $pg
!ll&&!rl&&r
0018
MC/DC0%0/3
defined $self and defined $pg and defined $f
defined $self defined $pg defined $f
Truth table25%1/4
A and B and C
A= defined $selfB= defined $pgC= defined $f
ABCresult
0XX0
10X0
1100
1111
1106 18 ▶ return if $f->is_ready; # EV::Pg's second call after an error (karr k47)
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 1 17 18
1107 17 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
1108 17 ▶ if (defined $err) { $f->fail($self->_pg_server_error($pg, $err)) } else { $f->done }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) { } 4 13 17
1109 17 });
1110 17 ▶ $pipeline->{synced} = 1 if $pipeline;
Branch: 1 branch
BranchTrueFalseTotal
if $pipeline 12 5 17
1111 17 return $f;
1112 }
1113  
1114  
1115 17 ▶sub _pipeline_exit {
Subroutine _pipeline_exit: called CC=8 SCAR=26.9
1116 17 my ($self, $pg) = @_;
1117 17 my $pipeline = _pg_conn_record(\%PG_PIPELINE, $pg);
1118 17 my $exited = eval { $pg->exit_pipeline; 1 };
1119 17 my $error = $self->_pg_dispatch_error($@); # without the place (karr k88)
1120 17 ▶ my @waiting = $pipeline ? splice @{ $pipeline->{queue} } : ();
Branch: 1 branch
BranchTrueFalseTotal
$pipeline ? : 16 1 17
1121  
1122 17 ▶ if (!$exited) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exited) 5 12 17
1123 # Still in pipeline mode: what waited for the exit never goes out.
1124 5 for my $waiting (@waiting) {
1125 0 eval { $waiting->[2]->fail($error); 1 };
1126 }
1127 5 die $error;
1128 }
1129  
1130 12 delete $PG_PIPELINE{ Scalar::Util::refaddr($pg) };
1131 12 ▶ return unless @waiting;
Branch: 1 branch
BranchTrueFalseTotal
unless @waiting 12 0 12
1132  
1133 # The statements a transaction issued behind the sync, its ROLLBACK among
1134 # them, go out now, one at a time, as on any pinned connection.
1135 0 my $st = _pg_conn_record(\%PG_PINNED, $pg, 1);
1136 0 for my $waiting (@waiting) {
1137 0 ▶ $st->{ending} = $waiting->[0] if $waiting->[0] =~ /\A(?:COMMIT|ROLLBACK)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $waiting->[0] =~ /\A(?:COMMIT|ROLLBACK)\z/ 0 0 0
1138 0 push @{ $st->{queue} }, $waiting;
1139 }
1140 0 my $died = $self->_pg_pinned_next($pg);
1141 0 ▶ die $died if defined $died;
Branch: 1 branch
BranchTrueFalseTotal
if defined $died 0 0 0
1142 0 return;
1143 }
1144  
1145 # --- LISTEN/NOTIFY seams (core ADR 0056) --------------------------------------
1146 #
1147 # Core DBIO::Storage::Async owns the public listen / unlisten / notify /
1148 # listen_alive / listen_reconnect, the hooks, the backoff and the machinery
1149 # behind them -- including the queue that runs one LISTEN / UNLISTEN at a time
1150 # on the listen connection, which replaced this class's own serialiser (karr
1151 # k15, k43). What is left is EV::Pg's. Private helpers are named _pg_*, never
1152 # _listen_*: they share an inheritance chain with core's private _listen_*
1153 # helpers, and a same-named method appearing there later would be shadowed.
1154 #
1155 # The listen connection is a hashref, opaque to core:
1156 #
1157 # pg the EV::Pg handle, a DBIO::PostgreSQL::EV::Pool::_Connection; gone
1158 # once closed
1159 # state what the handle's handlers do with what EV::Pg reports:
1160 # opening connecting; an error fails the open
1161 # open connected, not watched yet; notifications are held
1162 # watching notifications go to core, an error is a lost connection
1163 # quiet unwatched or closed; nothing reaches core any more
1164 # held the notifications held while 'open', in arrival order
1165  
1166  
1167 285 ▶sub _listen_open {
Subroutine _listen_open: called CC=4 SCAR=13.9
1168 285 my ($self) = @_;
1169 285 my $provider = $self->_conninfo_provider;
1170 285 ▶ my $conninfo = $provider ? conninfo_string($provider->()) : $self->_conninfo_string;
Branch: 1 branch
BranchTrueFalseTotal
$provider ? : 156 129 285
1171 192 ▶ _pg_refuse('the listen connection has no connect info -- call connect_info first')
Branch: 1 branch
BranchTrueFalseTotal
unless defined $conninfo and length $conninfo 81 111 192
Condition66%2/3
defined $conninfo and length $conninfo
!ll&&!rl&&r
810111
MC/DC50%1/2
defined $conninfo and length $conninfo
defined $conninfo length $conninfo
Truth table66%2/3
A and B
A= defined $conninfoB= length $conninfo
ABresult
0X0
100
111
1172 unless defined $conninfo && length $conninfo;
1173  
1174 111 my $f = $self->future_class->new;
1175 111 my $conn = { state => 'opening', held => [] };
1176  
1177 # Nothing else holds the connection before core receives it: the open's
1178 # Future does, until it is ready.
1179 111 my $hold = $conn;
1180 111 ▶ $f->on_ready(sub { undef $hold });
Subroutine __ANON__: called CC=1 SCAR=0.0
1181  
1182 111 $conn->{pg} = DBIO::PostgreSQL::EV::Pool->_new_connection(
1183 conninfo => $conninfo,
1184 keep_alive => 1,
1185 $self->_pg_listen_handlers($f, $conn),
1186 );
1187 111 return $f;
1188 }
1189  
1190 # The handle's on_connect / on_error / on_notify. Built in a named sub, so they
1191 # close over nothing but these arguments: the storage and the connection weakly
1192 # -- the handle keeps its handlers for its lifetime, and the connection holds
1193 # the handle -- and the open's Future only until the connect settles. Every one
1194 # returns at once in a process other than the opener (karr k45).
1195 111 ▶sub _pg_listen_handlers {
Subroutine _pg_listen_handlers: called CC=1 SCAR=0.2
1196 111 my ($self, $f, $conn) = @_;
1197 111 Scalar::Util::weaken(my $weak_self = $self);
1198 111 Scalar::Util::weaken(my $weak_conn = $conn);
1199 111 my $opening = $f;
1200 111 my $pid = $$;
1201  
1202 100 ▶ my $on_connect = sub {
Subroutine __ANON__: called CC=5 SCAR=18.1
1203 100 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 100 100
1204 100 my ($f, $conn) = ($opening, $weak_conn);
1205 100 undef $opening;
1206 100 ▶ return unless $f && $conn && !$f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f and $conn and not $f->is_ready 0 100 100
Condition33%2/6
$f and $conn and not $f->is_ready
!ll&&!rl&&r
00100
$f and $conn
!ll&&!rl&&r
00100
MC/DC0%0/3
$f and $conn and not $f->is_ready
$f $conn not $f->is_ready
Truth table25%1/4
A and B and not(C)
A= $fB= $connC= not $f->is_ready
ABCresult
0XX0
10X0
1100
1111
1207 100 $conn->{state} = 'open';
1208 100 $f->done($conn);
1209 100 return;
1210 111 };
1211  
1212 2 ▶ my $on_error = sub {
Subroutine __ANON__: called CC=8 SCAR=22.7
1213 2 my ($err) = @_;
1214 2 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 2 2
1215 2 ▶ chomp($err = defined $err ? "$err" : 'unknown error');
Branch: 1 branch
BranchTrueFalseTotal
defined $err ? : 2 0 2
1216 2 ▶ if (my $f = $opening) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $opening) 1 1 2
1217 1 undef $opening;
1218 1 ▶ $f->fail("connecting the listen connection failed: $err") unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 1 0 1
1219 1 return;
1220 }
1221 1 my ($self, $conn) = ($weak_self, $weak_conn);
1222 1 ▶ return unless $self && $conn && $conn->{state} ne 'quiet';
Branch: 1 branch
BranchTrueFalseTotal
unless $self and $conn and $conn->{"state"} ne "quiet" 0 1 1
Condition33%2/6
$self and $conn and $conn->{"state"} ne "quiet"
!ll&&!rl&&r
001
$self and $conn
!ll&&!rl&&r
001
MC/DC0%0/3
$self and $conn and $conn->{"state"} ne "quiet"
$self $conn $conn->{"state"} ne "quiet"
Truth table25%1/4
A and B and C
A= $selfB= $connC= $conn->{"state"} ne "quiet"
ABCresult
0XX0
10X0
1100
1111
1223 1 $self->_listen_lost($conn, "the listen connection was lost: $err");
1224 1 return;
1225 111 };
1226  
1227 17 ▶ my $on_notify = sub {
Subroutine __ANON__: called CC=6 SCAR=18.8
1228 17 my ($channel, $payload, $sender_pid) = @_;
1229 17 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 17 17
1230 17 my ($self, $conn) = ($weak_self, $weak_conn);
1231 17 ▶ return unless $self && $conn;
Branch: 1 branch
BranchTrueFalseTotal
unless $self and $conn 0 17 17
Condition33%1/3
$self and $conn
!ll&&!rl&&r
0017
MC/DC0%0/2
$self and $conn
$self $conn
Truth table33%1/3
A and B
A= $selfB= $conn
ABresult
0X0
100
111
1232 17 ▶ if ($conn->{state} eq 'watching') {
Branch: 2 branches
BranchTrueFalseTotal
if ($conn->{'state'} eq 'watching') { } 13 4 17
elsif ($conn->{'state'} eq 'open') { } 4 0 4
1233 13 $self->_listen_dispatch($channel, $payload, $sender_pid);
1234 }
1235 elsif ($conn->{state} eq 'open') {
1236 4 push @{ $conn->{held} }, [ $channel, $payload, $sender_pid ];
1237 }
1238 17 return;
1239 111 };
1240  
1241 return (
1242 111 on_connect => $on_connect,
1243 on_error => $on_error,
1244 on_notify => $on_notify,
1245 );
1246 }
1247  
1248  
1249 100 ▶sub _listen_close {
Subroutine _listen_close: called CC=2 SCAR=7.0
1250 100 my ($self, $conn) = @_;
1251 100 $self->_pg_listen_quiet($conn);
1252 100 ▶ my $pg = delete $conn->{pg} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pg = delete $conn->{"pg"} or return 100 0 100
1253 100 $pg->finish;
1254 100 return;
1255 }
1256  
1257  
1258 34 ▶sub _listen_subscribe {
Subroutine _listen_subscribe: called CC=1 SCAR=0.0
1259 34 my ($self, $conn, $channel) = @_;
1260 34 return $self->_pg_listen_command($conn, LISTEN => $channel);
1261 }
1262  
1263  
1264 4 ▶sub _listen_unsubscribe {
Subroutine _listen_unsubscribe: called CC=1 SCAR=0.0
1265 4 my ($self, $conn, $channel) = @_;
1266 4 return $self->_pg_listen_command($conn, UNLISTEN => $channel);
1267 }
1268  
1269 # One LISTEN / UNLISTEN on the listen connection. Named sub, like
1270 # _dispatch_query (karr k29): the completion callback is created here.
1271 38 ▶sub _pg_listen_command {
Subroutine _pg_listen_command: called CC=3 SCAR=11.3
1272 38 my ($self, $conn, $verb, $channel) = @_;
1273 38 my $sql = "$verb " . $self->_pg_quote_channel($channel);
1274 my $pg = $conn->{pg}
1275 33 ▶ or _pg_refuse("$verb on channel '$channel': the listen connection is closed");
Branch: 1 branch
BranchTrueFalseTotal
my $pg = $conn->{"pg"} or _pg_refuse("$verb on channel '${channel}': the listen connection is closed") 31 2 33
1276  
1277 31 my $f = $self->future_class->new;
1278 31 my $pid = $$;
1279 30 ▶ my $on_result = sub {
Subroutine __ANON__: called CC=7 SCAR=22.4
1280 30 my (undef, $err) = @_;
1281 30 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 30 30
1282 # Closing the connection from inside this callback -- the last channel
1283 # unlistened by a callback on this Future -- makes EV::Pg call it again
1284 # with "connection finished" while it runs (see _pipeline_sync).
1285 30 ▶ return unless defined $self && defined $pg && defined $f; # global destruction
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self and defined $pg and defined $f 0 30 30
Condition33%2/6
defined $self and defined $pg and defined $f
!ll&&!rl&&r
0030
defined $self and defined $pg
!ll&&!rl&&r
0030
MC/DC0%0/3
defined $self and defined $pg and defined $f
defined $self defined $pg defined $f
Truth table25%1/4
A and B and C
A= defined $selfB= defined $pgC= defined $f
ABCresult
0XX0
10X0
1100
1111
1286 30 ▶ return if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 2 28 30
1287 28 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
1288 0 ▶ if (defined $err) { $f->fail($self->_pg_server_error($pg, $err)) } else { $f->done }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) { } 0 28 28
1289 31 };
1290  
1291 # EV::Pg croaks before it queues $on_result, so that never runs: fail here
1292 # (karr k46).
1293 31 ▶ unless (eval { $pg->query($sql, $on_result); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $pg->query($sql, $on_result); 1 } }) 3 28 31
1294 3 $f->fail($self->_pg_dispatch_error($@));
1295 }
1296 31 return $f;
1297 }
1298  
1299  
1300 51 ▶sub _pg_quote_channel {
Subroutine _pg_quote_channel: called CC=1 SCAR=0.0
1301 51 my ($self, $channel) = @_;
1302 return DBIO::PostgreSQL::EV::Storage::_Refusal::of_util(
1303 51 ▶ sub { DBIO::PostgreSQL::Util::quote_channel($channel) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1304 51 );
1305 }
1306  
1307  
1308 17 ▶sub _listen_watch {
Subroutine _listen_watch: called CC=5 SCAR=16.1
1309 17 my ($self, $conn) = @_;
1310 17 ▶ my $pg = $conn->{pg} or _pg_refuse('the listen connection is closed');
Branch: 1 branch
BranchTrueFalseTotal
my $pg = $conn->{"pg"} or _pg_refuse("the listen connection is closed") 16 1 17
1311 16 ▶ _pg_refuse('the listen connection is not connected') unless $pg->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $pg->is_connected 2 14 16
1312  
1313 14 $conn->{state} = 'watching';
1314 14 ▶ while (my $n = shift @{ $conn->{held} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = shift @{$$conn{"held"};}) 3 13 16
1315 3 $self->_listen_dispatch(@$n);
1316 3 ▶ last unless $conn->{state} eq 'watching'; # a callback ended it
Branch: 1 branch
BranchTrueFalseTotal
unless $conn->{"state"} eq "watching" 1 2 3
1317 }
1318 14 return;
1319 }
1320  
1321  
1322 14 ▶sub _listen_unwatch {
Subroutine _listen_unwatch: called CC=1 SCAR=0.0
1323 14 my ($self, $conn) = @_;
1324 14 $self->_pg_listen_quiet($conn);
1325 14 return;
1326 }
1327  
1328 # Nothing the handle reports reaches core any more. Safe from inside one of
1329 # the handlers: EV::Pg holds a reference to the handler it is running.
1330 114 ▶sub _pg_listen_quiet {
Subroutine _pg_listen_quiet: called CC=3 SCAR=11.0
1331 114 my ($self, $conn) = @_;
1332 114 $conn->{state} = 'quiet';
1333 114 @{ $conn->{held} } = ();
1334 114 ▶ my $pg = $conn->{pg} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pg = $conn->{"pg"} or return 114 0 114
1335 114 $pg->$_(undef) for qw(on_connect on_error on_notify);
1336 114 return;
1337 }
1338  
1339  
1340 20 ▶sub _listen_probe {
Subroutine _listen_probe: called CC=4 SCAR=13.9
1341 20 my ($self, $conn) = @_;
1342 20 ▶ my $pg = $conn->{pg} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $pg = $conn->{"pg"} or return 0 19 1 20
1343 19 ▶ return 0 unless $pg->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $pg->is_connected 4 15 19
1344 15 ▶ return DBIO::PostgreSQL::EV::Pool->_pg_peer_closed($pg) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
'DBIO::PostgreSQL::EV::Pool'->_pg_peer_closed($pg) ? : 2 13 15
1345 }
1346  
1347  
1348 806 ▶sub _listen_delay {
Subroutine _listen_delay: called CC=1 SCAR=0.0
1349 806 my ($self, $seconds) = @_;
1350 806 my $f = $self->future_class->new;
1351 806 my $timer;
1352 7 ▶ $timer = EV::timer($seconds, 0, sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1353 7 undef $timer;
1354 7 ▶ $f->done unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 7 0 7
1355 806 });
1356 806 ▶ $f->on_cancel(sub { undef $timer });
Subroutine __ANON__: called CC=1 SCAR=0.0
1357 806 return $f;
1358 }
1359  
1360  
1361 44 ▶sub _notify_send {
Subroutine _notify_send: called CC=2 SCAR=7.0
1362 44 my ($self, $channel, $payload) = @_;
1363 # Released to the pool it was acquired from (karr k88, as in _query_async).
1364 44 my $pool = $self->pool;
1365 44 ▶ my $f = $pool->acquire->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1366 44 my $pg = shift;
1367 44 return $self->_dispatch_notify($pg, $channel, $payload, $pool, $pool->_lease($pg));
1368 44 });
1369 44 ▶ $f->retain if $f->can('retain'); # held until ready, as in _query_async (core k407)
Branch: 1 branch
BranchTrueFalseTotal
if $f->can("retain") 44 0 44
1370 44 return $f;
1371 }
1372  
1373  
1374 46 ▶sub _dispatch_notify {
Subroutine _dispatch_notify: called CC=4 SCAR=14.2
1375 46 my ($self, $pg, $channel, $payload, $pool, $lease) = @_;
1376 46 ▶ croak '_dispatch_notify needs the pool the connection was acquired from' unless $pool;
Branch: 1 branch
BranchTrueFalseTotal
unless $pool 1 45 46
1377 45 my $f = $self->future_class->new;
1378 45 my $pid = $$;
1379 45 my $values = _pg_error_values($channel, $payload);
1380  
1381 40 ▶ my $on_result = sub {
Subroutine __ANON__: called CC=6 SCAR=21.6
1382 40 my ($res, $err) = @_;
1383 40 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 40 40
1384 # Global destruction: EV::Pg cancels the statements still waiting and calls
1385 # back after the captured connection and Future are gone; nothing to deliver.
1386 40 ▶ return unless defined $self && defined $pg && defined $f && defined $pool;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self and defined $pg and defined $f and defined $pool 0 40 40
Condition33%3/9
defined $self and defined $pg and defined $f and defined $pool
!ll&&!rl&&r
0040
defined $self and defined $pg and defined $f
!ll&&!rl&&r
0040
defined $self and defined $pg
!ll&&!rl&&r
0040
MC/DC0%0/4
defined $self and defined $pg and defined $f and defined $pool
defined $self defined $pg defined $f defined $pool
Truth table20%1/5
A and B and C and D
A= defined $selfB= defined $pgC= defined $fD= defined $pool
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1387 40 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
1388 40 $err = $self->_pg_server_error($pg, $err, $values); # karr k74
1389 40 $self->_pg_settle_and_release($pool, $pg, $lease, $f, $err); # done void
1390 45 };
1391  
1392 # pg_notify() with bind params -- NOTIFY itself takes no placeholders,
1393 # and inlining the payload as a string literal invites quoting bugs.
1394 # EV::Pg croaks before it queues $on_result: release here (karr k46).
1395 45 my $sent = eval {
1396 45 ▶ $pg->query_params('SELECT pg_notify($1, $2)', [ $channel, $payload // '' ], $on_result);
Condition100%2/2
$payload // ""
l!l
432
MC/DC100%1/1
$payload // ""
$payload
Truth table100%2/2
A
A= $payload
Aresult
11
00
1397 40 1;
1398 };
1399 45 ▶ unless ($sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sent) 5 40 45
1400 5 my $err = $self->_pg_dispatch_error($@);
1401 5 $self->_pg_report_if_dead($pool, $pg);
1402 5 $pool->release($pg, $lease);
1403 5 $f->fail($err);
1404 }
1405  
1406 45 return $f;
1407 }
1408  
1409 # --- COPY ---------------------------------------------------------------------
1410  
1411 # One value in PostgreSQL's COPY text format: the backslash is the escape
1412 # character; TAB and LF delimit fields and rows, CR is a line terminator the
1413 # server knows too. One pass, so an escape is never escaped again (karr k98).
1414 65 my %COPY_TEXT_ESCAPE = ( "\\" => '\\\\', "\t" => '\\t', "\n" => '\\n', "\r" => '\\r' );
1415 361 ▶sub _copy_text_escape {
Subroutine _copy_text_escape: called CC=1 SCAR=0.0
1416 361 my ($value) = @_;
1417 361 $value =~ s/([\\\t\n\r])/$COPY_TEXT_ESCAPE{$1}/g;
1418 361 return $value;
1419 }
1420  
1421  
1422 129 ▶sub copy_in {
Subroutine copy_in: called CC=2 SCAR=6.9
Pod documented
1423 129 my ($self, $table, $columns, $coderef) = @_;
1424  
1425 129 my $col_list = join(', ', map { DBIO::PostgreSQL::Util::quote_ident($_) } @$columns);
1426 129 my $quoted_table = DBIO::PostgreSQL::Util::quote_qualified(split /\./, $table);
1427 129 my $sql = "COPY $quoted_table ($col_list) FROM STDIN";
1428  
1429 # The pool this COPY begins on is the one it ends on: the release, the report
1430 # of a dead connection and the retirement of an open stream all go to $pool,
1431 # never to $self->pool asked again (karr k88, as in _query_async). The
1432 # coderef runs inside the COPY's delivery, as the callbacks on its Future do.
1433 129 my $pool = $self->pool;
1434 129 ▶ my $seq = $pool->acquire->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1435 129 my $pg = shift;
1436 129 my $lease = $pool->_lease($pg); # every release below passes it (karr k89)
1437 129 my $f = $self->future_class->new;
1438  
1439 # EV::Pg fires the COPY query callback TWICE (see EV::Pg::Pg POD,
1440 # "A COPY command runs in two phases"): first with the string tag
1441 # "COPY_IN" when libpq has accepted the COPY command and the stream
1442 # is ready, then a second time with the final cmd_tuples result when
1443 # libpq has consumed the EOF marker, applied every row, and sent
1444 # CommandComplete + ReadyForQuery. The connection is NOT safe to
1445 # hand back to the pool until that second firing -- releasing after
1446 # the first lets the next acquire race libpq's still-pending reply
1447 # and fail with "another command is already in progress" (karr k13).
1448 #
1449 # $phase is where the COPY stands:
1450 # sent the COPY command is out, the server has not answered it
1451 # open the stream is open: the coderef runs, then put_copy_end
1452 # closing libpq has taken the end of the stream, the server's answer
1453 # to it is due -- the second firing
1454 # ended over, however it ended
1455 129 my $phase = 'sent';
1456 129 my $pid = $$;
1457 129 my $coderef_err; # die from the user's coderef, captured to forward
1458 # into the Future failure verbatim -- libpq wraps
1459 # whatever we pass to put_copy_end into its own
1460 # "ERROR: COPY from stdin failed: ..." envelope, so
1461 # callers would see the wrapped string instead of
1462 # their original die message unless we keep the
1463 # raw $@. See t/18 "dying coderef" assertion.
1464 my $write_err; # why the writer could not put a row, from the first
1465 # one on: the COPY fails with it (karr k65)
1466  
1467 # How the COPY ends: here, once, however it ends -- settled before
1468 # released, through _pg_settle_and_release (karr k51), and with the
1469 # coderef's error before any other. Ended is the COPY's own state, not
1470 # $f->is_ready: a Future the caller cancelled is ready, and its
1471 # connection still has to go back.
1472 #
1473 # A COPY that ends while its stream is open -- no put_copy_end libpq and
1474 # EV::Pg both took -- leaves a connection the pool cannot use again:
1475 # gone, or open with libpq still in the COPY, where every statement is
1476 # refused. It is reported dead before it is released, so the pool retires
1477 # it instead of idling it (karr k65). The server's answer leaves the
1478 # connection ready, or gone, which _pg_settle_and_release sees (karr k49).
1479 125 ▶ my $end = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1480 125 my ($err, @result) = @_;
1481 125 my $stream_open = $phase eq 'open';
1482 125 $phase = 'ended';
1483 125 ▶ $pool->_mark_connection_dead($pg) if $stream_open;
Branch: 1 branch
BranchTrueFalseTotal
if $stream_open 52 73 125
1484 125 ▶ $self->_pg_settle_and_release($pool, $pg, $lease, $f, $coderef_err // $err, @result);
Condition100%3/3
$coderef_err // $err
l!l&&r!l&&!r
364742
MC/DC100%2/2
$coderef_err // $err
$coderef_err $err
Truth table100%3/3
A // B
A= $coderef_errB= $err
ABresult
1X1
011
000
1485 129 };
1486 197 ▶ my $on_copy = sub {
Subroutine __ANON__: called CC=15 SCAR=27.3
1487 197 my ($status, $err) = @_;
1488 197 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 197 197
1489 # A call after the COPY ended touches neither the Future nor the pool,
1490 # as in _pipeline_sync. EV::Pg keeps this callback queued when
1491 # put_copy_end answers -1 (Pg.xs put_copy_end, 0.02 and 0.07 alike) and
1492 # calls it again -- with "COPY_IN" on a read while libpq has the COPY
1493 # open, with "connection lost" or "connection finished" when the
1494 # connection goes. By then the connection is not this COPY's: released
1495 # again, it would have two holders (karr k57).
1496 197 ▶ return if $phase eq 'ended';
Branch: 1 branch
BranchTrueFalseTotal
if $phase eq "ended" 12 185 197
1497 185 ▶ return unless defined $self && defined $pg && defined $f && defined $pool; # global destruction
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self and defined $pg and defined $f and defined $pool 0 185 185
Condition33%3/9
defined $self and defined $pg and defined $f and defined $pool
!ll&&!rl&&r
00185
defined $self and defined $pg and defined $f
!ll&&!rl&&r
00185
defined $self and defined $pg
!ll&&!rl&&r
00185
MC/DC0%0/4
defined $self and defined $pg and defined $f and defined $pool
defined $self defined $pg defined $f defined $pool
Truth table20%1/5
A and B and C and D
A= defined $selfB= defined $pgC= defined $fD= defined $pool
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1498 185 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
1499  
1500 # Second firing: the server's answer to the end of the stream. The
1501 # connection is back at ReadyForQuery and safe to reuse.
1502 185 ▶ if ($phase eq 'closing') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq "closing") 60 125 185
1503 60 $end->($self->_pg_server_error($pg, $err), $status);
1504 48 return;
1505 }
1506  
1507 # First firing. With an error the server refused the COPY, or the
1508 # connection went before it answered: no stream was opened.
1509 125 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 13 112 125
1510 13 $end->($self->_pg_server_error($pg, $err));
1511 9 return;
1512 }
1513 112 $phase = 'open';
1514  
1515 # The writer. A row it cannot put is the end of the COPY (karr k65):
1516 # put_copy_data answers 0 when libpq cannot grow its output buffer for
1517 # the row -- there is no waiting for room in here, the EV loop does not
1518 # run inside this callback -- and -1 on an error. Its croak, "not
1519 # connected" once EV::Pg has lost the connection, goes through
1520 # without its place (_pg_dispatch_error): put_copy_end croaks the same
1521 # then. Good only while the coderef
1522 # runs: after that $pg is the pool's, or in another caller's COPY, and
1523 # a call is refused (_pg_refuse). The writer has a name: core's
1524 # exception finds the caller's place past named frames only, and an
1525 # anonymous one would leave it the whole stack (karr k88).
1526 187 ▶ my $put = Sub::Util::set_subname 'DBIO::PostgreSQL::EV::Storage::copy_in_writer' => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
1527 187 my $row = shift;
1528 187 ▶ _pg_refuse('copy_in: the writer was called outside its COPY') unless $phase eq 'open';
Branch: 1 branch
BranchTrueFalseTotal
unless $phase eq "open" 2 185 187
1529 185 ▶ croak $write_err if defined $write_err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $write_err 2 183 185
1530 183 ▶ my $line = join("\t", map { defined $_ ? _copy_text_escape($_) : '\N' } @$row) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 361 4 365
1531 183 my $rc = eval { $pg->put_copy_data($line) };
1532 183 ▶ die $self->_pg_dispatch_error($@) unless defined $rc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rc 9 174 183
1533 174 ▶ return $rc if $rc == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rc == 1 170 4 174
1534 4 croak $write_err = $self->_pg_copy_failure($pg, 'put_copy_data');
1535 112 };
1536  
1537 112 my $ran = eval { $coderef->($put); 1 };
1538 112 ▶ $coderef_err = $ran ? $write_err : ($@ || 'copy_in: the callback died');
Branch: 1 branch
BranchTrueFalseTotal
$ran ? : 78 34 112
Condition50%1/2
$@ or "copy_in: the callback died"
l!l
340
MC/DC0%0/1
$@ or "copy_in: the callback died"
$@
Truth table50%1/2
A
A= $@
Aresult
11
00
1539  
1540 # End the stream. An error goes to libpq, so the server throws the
1541 # COPY away; the Future fails with the raw $coderef_err, not with
1542 # libpq's wrapped "ERROR: COPY from stdin failed: ..." envelope.
1543 112 my $rc = eval {
1544 112 ▶ defined $coderef_err ? $pg->put_copy_end($coderef_err) : $pg->put_copy_end;
Branch: 1 branch
BranchTrueFalseTotal
defined $coderef_err ? : 36 76 112
1545 };
1546 112 ▶ if (!defined $rc) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $rc) { } 19 93 112
elsif ($rc < 0) { } 29 64 93
elsif (not $pg->is_connected) { } 4 60 64
1547 # put_copy_end croaked "not connected": EV::Pg lost the connection
1548 # during the COPY and dropped this callback on the way, so no second
1549 # firing will come (karr k46).
1550 19 $end->($self->_pg_dispatch_error($@));
1551 }
1552 elsif ($rc < 0) {
1553 # PQputCopyEnd failed, and EV::Pg leaves it at that: this callback
1554 # stays queued, and libpq is still in the COPY if it could not queue
1555 # the end of the stream. No answer of the server's is due.
1556 29 $end->($self->_pg_copy_failure($pg, 'put_copy_end'));
1557 }
1558 elsif (!$pg->is_connected) {
1559 # libpq took the end of the stream, and EV::Pg lost the connection
1560 # in the flush behind it: libpq had dropped it in an earlier write
1561 # (PQflush answers -1 on such a connection). EV::Pg drops the
1562 # callback it is delivering to without a second firing, so this is
1563 # the only place to end; "connection lost" is what it tells a
1564 # callback it does call.
1565 4 $end->('connection lost');
1566 }
1567 else {
1568 # The second firing completes $f and releases the connection.
1569 60 $phase = 'closing';
1570 }
1571 96 return;
1572 129 };
1573  
1574 # EV::Pg croaks before it queues $on_copy ("not connected", "another
1575 # command is already in progress"), so neither firing will come:
1576 # release here, once (karr k46).
1577 129 ▶ unless (eval { $pg->query($sql, $on_copy); 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { $pg->query($sql, $on_copy); 1 } }) 4 125 129
1578 4 my $err = $self->_pg_dispatch_error($@);
1579 4 $self->_pg_report_if_dead($pool, $pg);
1580 4 $pool->release($pg, $lease);
1581 4 $f->fail($err);
1582 }
1583  
1584 129 return $f;
1585 129 });
1586 129 ▶ $seq->retain if $seq->can('retain'); # held until ready, as in _query_async (core k407)
Branch: 1 branch
BranchTrueFalseTotal
if $seq->can("retain") 129 0 129
1587 129 return $seq;
1588 }
1589  
1590 # What a failed put_copy_data / put_copy_end of copy_in says: the call, and
1591 # libpq's reason for it when it has one -- "cannot allocate memory for output
1592 # buffer", "server closed the connection unexpectedly" -- which is the
1593 # connection's error message at that moment. That is libpq's text: masked,
1594 # like every text of libpq's that is passed on here (karr k78), and without
1595 # its trailing newline -- the writer croaks with it.
1596 33 ▶sub _pg_copy_failure {
Subroutine _pg_copy_failure: called CC=3 SCAR=11.0
1597 33 my ($self, $pg, $call) = @_;
1598 33 my $why = $pg->error_message;
1599 33 ▶ return "$call failed" unless defined $why && $why =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $why and $why =~ /\S/ 12 21 33
Condition66%2/3
defined $why and $why =~ /\S/
!ll&&!rl&&r
12021
MC/DC50%1/2
defined $why and $why =~ /\S/
defined $why $why =~ /\S/
Truth table66%2/3
A and B
A= defined $whyB= $why =~ /\S/
ABresult
0X0
100
111
1600 21 $why =~ s/\s+\z//;
1601 21 return "$call failed: " . DBIO::Util::mask_connect_secrets($why);
1602 }
1603  
1604 # --- Async Deploy -------------------------------------------------------------
1605 # deploy_async / _execute_ddl_async / _drop_statements_for / the sync deploy
1606 # fallback are all INHERITED from DBIO::Storage::Async (core karr k73): the
1607 # generic implementation already does exactly what this transport needs --
1608 # generate DDL, probe _use_transactional_ddl, run it through the
1609 # _query_async_pinned seam this class implements for CRUD. Filling _ddl_class
1610 # is the only seam a Model-B backend has to provide (karr k25; precedent
1611 # dbio-mysql-ev k21, commit d86446e).
1612  
1613  
1614 12 ▶sub _ddl_class { 'DBIO::PostgreSQL::DDL' }
Subroutine _ddl_class: called CC=1 SCAR=0.0
1615  
1616  
1617 10 ▶sub deploy_async {
Subroutine deploy_async: called CC=3 SCAR=11.0
Pod documented
1618 10 my ($self, $schema, @rest) = @_;
1619  
1620 10 my @declared = DBIO::PostgreSQL::DDL::_schema_decl($schema, 'pg_extensions');
1621 10 ▶ return $self->SUPER::deploy_async($schema, @rest) unless @declared;
Branch: 1 branch
BranchTrueFalseTotal
unless @declared 6 4 10
1622  
1623 my $f = $self->_seam_future(_query_async =>
1624 DBIO::PostgreSQL::DDL::control_schemas_sql(@declared), [],
1625 4 ▶ )->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1626 4 my %fixed = map { $_->[0] => $_->[1] } @_;
1627 4 DBIO::PostgreSQL::DDL::check_control_schemas($schema, \%fixed);
1628 3 return $self->_deploy_async_with_control_schemas($schema, \%fixed, @rest);
1629 4 });
1630 4 ▶ $f->retain if $f->can('retain'); # held until ready, as in _query_async (core k407)
Branch: 1 branch
BranchTrueFalseTotal
if $f->can("retain") 4 0 4
1631 4 return $f;
1632 }
1633  
1634 # The schemas a control file fixes are handed to _install_ddl for the length
1635 # of the synchronous call core's deploy_async makes to it, and no longer: a
1636 # second deploy on the same storage reads its own.
1637 3 ▶sub _deploy_async_with_control_schemas {
Subroutine _deploy_async_with_control_schemas: called CC=1 SCAR=0.0
1638 3 my ($self, $schema, $fixed, @rest) = @_;
1639 3 local $self->{_extension_control_schemas} = $fixed;
1640 3 return $self->SUPER::deploy_async($schema, @rest);
1641 }
1642  
1643  
1644 9 ▶sub _install_ddl {
Subroutine _install_ddl: called CC=2 SCAR=6.9
1645 9 my ($self, $schema) = @_;
1646 return $self->_ddl_class->install_ddl($schema,
1647 9 ▶ extension_control_schemas => $self->{_extension_control_schemas} // {});
Condition100%2/2
$self->{"_extension_control_schemas"} // {}
l!l
36
MC/DC100%1/1
$self->{"_extension_control_schemas"} // {}
$self->{"_extension_control_schemas"}
Truth table100%2/2
A
A= $self->{"_extension_control_schemas"}
Aresult
11
00
1648 }
1649  
1650 # --- Schema Integration -------------------------------------------------------
1651 # schema / debug / connected / disconnect / DESTROY and the sync
1652 # CRUD/txn/deploy ->get fallbacks are inherited from DBIO::Storage::Async.
1653 # disconnect was overridden here until karr k43, only to finish the dedicated
1654 # LISTEN connection this class kept itself; core's disconnect rotates the
1655 # context-owner token, ends listening (core ADR 0056) and shuts the pool down.
1656  
1657 65 1;
1658  
1659 __END__
« EV/ConnectInfo.pm ↑ Summary EV/Pool.pm »