blib/lib/DBIO/PostgreSQL/EV/Storage.pm

filter: Statementstmt 97.8% 762 / 779 Branchbran 85.8% 359 / 418 Conditioncond 62.7% 167 / 266 MC/DCmcdc 41.6% 62 / 149 Subroutinesub 100% 121 / 121 Podpod 100% 7 / 7 totaltotal 84.9% 1478 / 1740 CC 306 SCAR 22.3
CC
306
Cov
88%
_pg_read_connect_info
38.5
__ANON__
15.4
_pg_server_fields
14.9
CRAP 9.3
« blib/lib/DBIO/PostgreSQL/EV/ConnectInfo.pm ↑ Summary blib/lib/DBIO/PostgreSQL/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.900007';
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 4177 ▶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 966 ▶sub connect_info {
Subroutine connect_info: called CC=10 SCAR=23.0
Pod documented
71 966 my ($self, $info) = @_;
72 966 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 966 0 966
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 966 ▶ if ($self->{pool} || $self->{_listen}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'pool'} or $self->{'_listen'}) { } 74 892 966
Condition66%2/3
$self->{"pool"} or $self->{"_listen"}
l!l&&r!l&&!r
740892
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 892 $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 966 @{$self}{qw( connect_info _conninfo _pool_size _opts )} = ();
93 966 $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 966 my $stripped = $self->_strip_private_connect_attrs($info);
102 966 my $beside = $self->_access_broker_call_options($stripped);
103 964 ▶ $info = $stripped if $beside;
Branch: 1 branch
BranchTrueFalseTotal
if $beside 126 838 964
104  
105 964 ▶ if ($beside || $self->_is_access_broker_connect_info($info)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($beside or $self->_is_access_broker_connect_info($info)) { } 353 611 964
Condition100%3/3
$beside or $self->_is_access_broker_connect_info($info)
l!l&&r!l&&!r
126227611
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 611 $self->_clear_access_broker;
141 611 my $read = $self->_pg_read_connect_info($info);
142 502 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 502 ▶ $conninfo //= '';
Condition100%2/2
$conninfo //= ""
l!l
5011
MC/DC100%1/1
$conninfo //= ""
$conninfo
Truth table100%2/2
A
A= $conninfo
Aresult
11
00
145 502 $self->{connect_info} = $read;
146 502 $self->{_conninfo} = $conninfo;
147 502 $self->{_pool_size} = $pool_size;
148 502 $self->{_opts} = $opts;
149 }
150 }
151 676 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 1919 ▶sub _pg_is_dbi_attribute {
Subroutine _pg_is_dbi_attribute: called CC=1 SCAR=0.0
175 1919 my ($self, $name) = @_;
176 1919 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 1200 ▶sub _pg_read_connect_info {
Subroutine _pg_read_connect_info: called CC=38 SCAR=36.5
234 1200 my ($self, $info, %from) = @_;
235 1200 my ($pair, $shape);
236  
237 1200 ▶ $info = [ $info, {} ] if $from{broker} && ref $info eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $from{"broker"} and ref $info eq "HASH" 0 1200 1200
Condition66%2/3
$from{"broker"} and ref $info eq "HASH"
!ll&&!rl&&r
6115890
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 1200 ▶ if (ref $info eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $info eq "ARRAY") 1179 21 1200
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 1179 my $stripped = do {
246 1179 local $self->{_storage_options};
247 1179 $self->_strip_private_connect_attrs($info);
248 };
249 1179 my ($first, $opts) = @$stripped;
250 1179 ▶ my $paired = @$info <= 2 && (!defined $opts || ref $opts eq 'HASH');
Condition100%6/6
@$info <= 2 && (!defined($opts) || ref $opts eq 'HASH')
!ll&&!rl&&r
3458826
not defined $opts or ref $opts eq "HASH"
l!l&&r!l&&!r
2335938
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 1179 ▶ if (defined $first && !ref $first && $first =~ /^dbi:/i) {
Branch: 5 branches
BranchTrueFalseTotal
if (defined $first and not ref $first and $first =~ /^dbi:/i) { } 385 794 1179
elsif (ref $first eq 'CODE') { } 17 777 794
elsif (not $paired) { } 21 756 777
elsif (ref $first eq 'HASH') { } 734 22 756
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
76826385
defined $first and not ref $first
!ll&&!rl&&r
5763411
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 734 my %hash = %$first;
265 my $is_dsn = exists $hash{dsn} ? 'dsn'
266 734 ▶ : defined $hash{dbname} && !ref $hash{dbname} && $hash{dbname} =~ /^dbi:/i ? 'dbname'
Branch: 2 branches
BranchTrueFalseTotal
exists $hash{'dsn'} ? : 103 631 734
defined $hash{'dbname'} && !ref($hash{'dbname'}) && $hash{'dbname'} =~ /^dbi:/i ? : 11 620 631
Condition83%5/6
defined $hash{"dbname"} and not ref $hash{"dbname"} and $hash{"dbname"} =~ /^dbi:/i
!ll&&!rl&&r
3158911
defined $hash{"dbname"} and not ref $hash{"dbname"}
!ll&&!rl&&r
310600
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 734 ▶ if (delete $hash{dbh_maker}) {
Branch: 2 branches
BranchTrueFalseTotal
if (delete $hash{'dbh_maker'}) { } 5 729 734
elsif (defined $is_dsn) { } 112 617 729
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 617 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 617 ▶ 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 617 delete @hash{@attributes};
303 617 my ($ref_key) = grep { ref $hash{$_} } sort keys %hash;
304 617 ▶ if (@cased) {
Branch: 2 branches
BranchTrueFalseTotal
if (@cased) { } 4 613 617
elsif (defined $ref_key) { } 8 605 613
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 605 $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 1065 ▶ if ($pair) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pair) 917 148 1065
326 917 ▶ return $pair unless $from{broker};
Branch: 1 branch
BranchTrueFalseTotal
unless $from{"broker"} 502 415 917
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 2557 ▶sub pool {
Subroutine pool: called CC=3 SCAR=11.0
Pod documented
432 2557 my $self = shift;
433 2557 ▶ $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
18247330
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 733 );
440  
441 733 ▶ if (my $provider = $self->_conninfo_provider) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $provider = $self->_conninfo_provider) { } 214 519 733
442 214 $args{conninfo_provider} = $provider;
443 }
444 else {
445 519 $args{conninfo} = $self->_conninfo_string;
446 }
447  
448 733 DBIO::PostgreSQL::EV::Pool->new(%args);
449 };
450 }
451  
452  
453 667 ▶sub _conninfo_string {
Subroutine _conninfo_string: called CC=5 SCAR=16.1
454 667 my ($self, $ci) = @_;
455 667 ▶ if (! defined $ci) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ci) 667 0 667
456 667 my $provider = $self->_conninfo_provider;
457 667 ▶ $ci = $provider ? $provider->() : $self->{_conninfo};
Branch: 1 branch
BranchTrueFalseTotal
$provider ? : 12 655 667
458 }
459 663 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 663 ▶ return defined $string && !length $string ? ' ' : $string;
Branch: 1 branch
BranchTrueFalseTotal
defined $string && !length($string) ? : 9 654 663
Condition100%3/3
defined $string and not length $string
!ll&&!rl&&r
1654899
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 1078 ▶sub _transform_sql {
Subroutine _transform_sql: called CC=1 SCAR=0.0
493 1078 my ($self, $sql) = @_;
494 return DBIO::PostgreSQL::EV::Storage::_Refusal::of_util(
495 1078 ▶ sub { DBIO::PostgreSQL::Util::number_placeholders($sql) },
Subroutine __ANON__: called CC=1 SCAR=0.0
496 1078 );
497 }
498  
499 # --- Async query execution (transport seams) ----------------------------------
500  
501  
502 576 ▶sub _query_async {
Subroutine _query_async: called CC=4 SCAR=13.9
503 576 my ($self, $sql, $bind, $bind_attrs) = @_;
504 576 ▶ $bind //= [];
Condition50%1/2
$bind //= []
l!l
5760
MC/DC0%0/1
$bind //= []
$bind
Truth table50%1/2
A
A= $bind
Aresult
11
00
505 576 $sql = $self->_transform_sql($sql);
506  
507 574 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 574 my $pool = $self->pool;
512 541 ▶ my $f = $pool->acquire->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
513 541 my $pg = shift;
514 541 return $self->_dispatch_query($pg, $sql, $bind, $pool, $pool->_lease($pg));
515 574 });
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 574 ▶ $f->retain if $f->can('retain');
Branch: 1 branch
BranchTrueFalseTotal
if $f->can("retain") 574 0 574
521 574 ▶ $f->on_ready($trace_end) if $trace_end;
Branch: 1 branch
BranchTrueFalseTotal
if $trace_end 8 566 574
522 574 return $f;
523 }
524  
525  
526 544 ▶sub _dispatch_query {
Subroutine _dispatch_query: called CC=3 SCAR=11.1
527 544 my ($self, $pg, $sql, $bind, $pool, $lease) = @_;
528 544 ▶ croak '_dispatch_query needs the pool the connection was acquired from' unless $pool;
Branch: 1 branch
BranchTrueFalseTotal
unless $pool 1 543 544
529 543 my $f = $self->future_class->new;
530 543 my $pid = $$;
531 543 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 543 my $values = _pg_error_values(@$bind);
535 540 ▶ my $on_result = sub {
Subroutine __ANON__: called CC=8 SCAR=22.2
536 540 my ($rows, $err) = @_;
537 540 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 540 540
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 540 ▶ 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 540 540
Condition33%3/9
defined $self and defined $pg and defined $f and defined $pool
!ll&&!rl&&r
00540
defined $self and defined $pg and defined $f
!ll&&!rl&&r
00540
defined $self and defined $pg
!ll&&!rl&&r
00540
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 540 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
542 540 $guard->{answered} = 1;
543 # Here, before anything is deferred: error_fields is this statement's only now (karr k74).
544 540 $err = $self->_pg_server_error($pg, $err, $values);
545 540 ▶ my $settle = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
546 540 ▶ $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 42 540
547 540 };
548 # A cancel request is out: the connection goes back when it is answered (karr k75).
549 540 ▶ if ($guard->{cancelling} && $pg->is_connected) {
Branch: 1 branch
BranchTrueFalseTotal
if ($guard->{"cancelling"} and $pg->is_connected) 4 536 540
Condition66%2/3
$guard->{"cancelling"} and $pg->is_connected
!ll&&!rl&&r
53604
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 536 $settle->();
557 543 };
558 # EV::Pg croaks before it queues $on_result, so that never runs: release
559 # here, once (karr k46).
560 543 ▶ 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 537 543
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 543 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 1129 ▶ sub of_util {
Subroutine of_util: called CC=4 SCAR=14.0
624 1129 my ($code) = @_;
625 1129 my ($answer, $ok, $err);
626 {
627 1129 local $@;
628 1129 $ok = eval { local $Carp::Verbose = 0; $answer = $code->(); 1 };
629 1129 $err = $@;
630 }
631 1129 ▶ return $answer if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1099 30 1129
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.900007-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.900007-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.900007-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.900007-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.900007-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=5 SCAR=16.1
639 907 my ($self, $pg, $err, $values) = @_;
640 907 ▶ return $err if ref $err or not defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if ref $err or not defined $err 777 130 907
Condition100%3/3
ref $err or not defined $err
l!l&&r!l&&!r
1776130
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 130 my $fields = $self->_pg_server_fields($pg, $err);
642 130 ▶ my $sqlstate = $fields ? $fields->{sqlstate} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$fields ? : 25 105 130
643 130 my $text = $self->_pg_mask_error_text($err, $values, $sqlstate);
644 130 ▶ return $text unless $fields;
Branch: 1 branch
BranchTrueFalseTotal
unless $fields 105 25 130
645 return DBIO::Exception::Server->new(
646 message => $text,
647 sqlstate => $sqlstate,
648 constraint => $fields->{constraint},
649 table => $fields->{table},
650 25 detail => $self->_pg_mask_error_detail($fields->{detail}, $values, $sqlstate),
651 );
652 }
653  
654 # The error_fields of $pg when $err is the server's error they belong to, else
655 # undef: the fields have a severity and a primary and the text begins with
656 # them, as libpq renders an error (see _pg_server_error). The only place the
657 # SQLSTATE of an error is taken from, so that no text of a lost connection
658 # gets the state of an earlier statement.
659 136 ▶sub _pg_server_fields {
Subroutine _pg_server_fields: called CC=13 SCAR=27.0
660 136 my ($self, $pg, $err) = @_;
661 136 ▶ return undef unless defined $err && !ref $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err and not ref $err 0 136 136
Condition33%1/3
defined $err and not ref $err
!ll&&!rl&&r
00136
MC/DC0%0/2
defined $err and not ref $err
defined $err not ref $err
Truth table33%1/3
A and not(B)
A= defined $errB= not ref $err
ABresult
0X0
100
111
662 136 ▶ return undef unless Scalar::Util::blessed($pg) && $pg->can('error_fields');
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($pg) and $pg->can("error_fields") 94 42 136
Condition100%3/3
builtin::blessed($pg) and $pg->can("error_fields")
!ll&&!rl&&r
19342
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
663 42 my $fields = $pg->error_fields;
664 42 ▶ return undef unless ref $fields eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $fields eq "HASH" 9 33 42
665 33 my ($severity, $primary, $sqlstate) = @$fields{qw( severity primary sqlstate )};
666 33 ▶ return undef unless defined $severity && length $severity
Branch: 1 branch
BranchTrueFalseTotal
unless defined $severity and length $severity and defined $primary and length $primary 1 32 33
Condition66%6/9
defined $severity and length $severity and defined $primary and length $primary
!ll&&!rl&&r
1032
defined $severity and length $severity and defined $primary
!ll&&!rl&&r
1032
defined $severity and length $severity
!ll&&!rl&&r
1032
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
667 && defined $primary && length $primary;
668 32 ▶ my $verbose = defined $sqlstate && length $sqlstate ? qr/(?:\Q$sqlstate\E: )?/ : qr//;
Branch: 1 branch
BranchTrueFalseTotal
defined $sqlstate && length $sqlstate ? : 32 0 32
Condition33%1/3
defined $sqlstate and length $sqlstate
!ll&&!rl&&r
0032
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
669 32 ▶ return undef 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 25 32
670 25 return $fields;
671 }
672  
673 # The SQLSTATE of $err when it is the server's error on $pg, else undef.
674 6 ▶sub _pg_sqlstate_of {
Subroutine _pg_sqlstate_of: called CC=2 SCAR=7.1
675 6 my ($self, $pg, $err) = @_;
676 6 my $fields = $self->_pg_server_fields($pg, $err);
677 6 ▶ return $fields ? $fields->{sqlstate} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$fields ? : 0 6 6
678 }
679  
680  
681 144 ▶sub _pg_mask_error_text {
Subroutine _pg_mask_error_text: called CC=4 SCAR=13.9
682 144 my ($self, $text, $values, $sqlstate) = @_;
683 144 ▶ return $text if not defined $text or ref $text;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $text or ref $text 2 142 144
Condition100%3/3
not defined $text or ref $text
l!l&&r!l&&!r
11142
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
684 142 my $masked = eval {
685 142 DBIO::PostgreSQL::Util::mask_error_details(
686 DBIO::Util::mask_bound_values($text, $values), $sqlstate
687 );
688 };
689 142 ▶ return defined $masked ? $masked : $text;
Branch: 1 branch
BranchTrueFalseTotal
defined $masked ? : 142 0 142
690 }
691  
692 # The detail field of the server with the values taken out: the bound values,
693 # then the values of its forms (Key (name)=(***), Failing row contains (***)),
694 # in the language of the SQLSTATE when it is given.
695 25 ▶sub _pg_mask_error_detail {
Subroutine _pg_mask_error_detail: called CC=4 SCAR=14.0
696 25 my ($self, $detail, $values, $sqlstate) = @_;
697 25 ▶ return $detail if not defined $detail or ref $detail;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $detail or ref $detail 15 10 25
Condition66%2/3
not defined $detail or ref $detail
l!l&&r!l&&!r
15010
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
698 10 my $masked = eval {
699 10 DBIO::PostgreSQL::Util::mask_detail_values(
700 DBIO::Util::mask_bound_values($detail, $values), $sqlstate
701 );
702 };
703 10 ▶ return defined $masked ? $masked : $detail;
Branch: 1 branch
BranchTrueFalseTotal
defined $masked ? : 10 0 10
704 }
705  
706 # The values of a statement's binds as the net takes them, read the way core's
707 # DBIO::Storage::DBI::_plain_error_values reads a bind list: undef and a
708 # reference that is no plain value fall away, an object with a stringification
709 # is its string. A new list of copies -- what a callback keeps for the flight
710 # of its statement in place of the bind array (t/05).
711 843 ▶sub _pg_error_values {
Subroutine _pg_error_values: called CC=6 SCAR=17.9
712 843 my @plain;
713 843 for my $v (@_) {
714 487 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 12 475 487
715 475 ▶ if (length ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (length ref $v) { } 6 469 475
716 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
717 2 ▶ push @plain, "$$p" if defined $$p;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$p 2 0 2
718 }
719 else {
720 469 push @plain, $v;
721 }
722 }
723 843 return \@plain;
724 }
725  
726 # How a callback this class hands to EV::Pg ends a pooled statement --
727 # _dispatch_query, _dispatch_notify, copy_in: settle $f -- fail it with $err if
728 # there is one, else done with @result -- and only then hand $pg back to
729 # $pool, the pool it was acquired from, with $lease, the lease of the borrow
730 # (a stale release then takes nothing, karr k89). Settle first: the Future's then callbacks run with $pg still checked
731 # out (karr k44, k51), and the release runs inside the caller's delivery frame.
732 # An on_done / on_ready / on_fail that dies escapes done / fail, so the release
733 # must not depend on it returning; the die goes on to EV::Pg after the release,
734 # which warns it (karr k50).
735 #
736 # "There is one" is a defined $err, here and in every callback of this class:
737 # EV::Pg calls a statement's callback with ($result) when it went through and
738 # with (undef, $message) when it did not, the message a string that is never
739 # empty -- libpq's, or a text of its own where libpq has none ("connection
740 # lost", "pipeline aborted", ...; Pg.xs, 0.09).
741 #
742 # $pool is handed in by whoever acquired $pg, and is the only pool this talks
743 # to -- never $self->pool (karr k88, this transport's half of karr dbio-async
744 # k46). The settling runs the caller's callbacks, and one of them may
745 # disconnect the storage: the slot pool() fills is empty then, and asked here
746 # it built a new pool on the disconnected storage, to release into it a
747 # connection that pool had never held -- or died, where the storage had
748 # nothing left to connect from. $pool is shut down by then, and takes the
749 # report and the release as no-ops.
750 705 ▶sub _pg_settle_and_release {
Subroutine _pg_settle_and_release: called CC=4 SCAR=13.9
751 705 my ($self, $pool, $pg, $lease, $f, $err, @result) = @_;
752 705 my $settled = eval {
753 705 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) { } 132 573 705
754 132 $f->fail($err);
755 } else {
756 573 $f->done(@result);
757 }
758 659 1;
759 };
760 705 my $settle_err = $@;
761 705 ▶ $self->_pg_report_if_dead($pool, $pg) if defined $err; # before the release (karr k49)
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 132 573 705
762 705 $pool->release($pg, $lease);
763 705 ▶ die $settle_err unless $settled;
Branch: 1 branch
BranchTrueFalseTotal
unless $settled 46 659 705
764 659 return;
765 }
766  
767  
768 147 ▶sub _pg_report_if_dead {
Subroutine _pg_report_if_dead: called CC=2 SCAR=6.9
769 147 my ($self, $pool, $pg) = @_;
770 147 ▶ $pool->_mark_connection_dead($pg) unless $pg->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $pg->is_connected 44 103 147
771 147 return;
772 }
773  
774  
775 93 ▶sub _reset_pool_connection {
Subroutine _reset_pool_connection: called CC=2 SCAR=7.2
776 93 my ($self, $pg) = @_;
777 93 my $f = $self->future_class->new;
778 93 my $pid = $$;
779  
780 88 ▶ my $on_result = sub {
Subroutine __ANON__: called CC=7 SCAR=21.0
781 88 my (undef, $err) = @_;
782 88 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 88 88
783 # Global destruction: EV::Pg cancels the statements still waiting and calls
784 # back after the captured connection and Future are gone; nothing to deliver.
785 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
786 88 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
787 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
788 64 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 2 62 64
789 2 $f->fail("the session of a pool connection could not be reset: DISCARD ALL failed: "
790 . $self->_pg_mask_error_text($err, undef, $self->_pg_sqlstate_of($pg, $err)));
791 2 return;
792 }
793 # The connect actions run the EV loop to the end of each statement
794 # (_run_pool_connect_statement), which EV::Pg does not take inside a
795 # callback of the very connection they run on: it would hand the answer of
796 # the first statement to this callback, still running. So they run from a
797 # timer, after this callback has returned.
798 62 my $timer;
799 62 ▶ $timer = EV::timer(0, 0, sub {
Subroutine __ANON__: called CC=7 SCAR=21.8
800 62 undef $timer;
801 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
802 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
803 2 $f->fail("the session of a pool connection could not be reset: "
804 . "its connect actions could not be run again\n");
805 2 return;
806 }
807 60 $f->done;
808 62 });
809 62 return;
810 93 };
811  
812 # EV::Pg croaks before it queues $on_result ("not connected"): the Future
813 # fails and the pool retires the connection.
814 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
815 1 $f->fail($self->_pg_dispatch_error($@));
816 }
817 93 return $f;
818 }
819  
820 # --- The pinned path's one-statement-at-a-time queue (karr k48) ---------------
821 # See _query_async_pinned. The state is kept per connection, by refaddr, in
822 # records of { pid, conn } -- the process that made the record (karr k45) and
823 # the connection, weakly. A record of another process, or whose connection has
824 # gone, is stale and counts as absent, so it can neither outlive its connection
825 # nor land on a later one at the same address (as Pool.pm's %PG_OPENED).
826 #
827 # %PG_PINNED a record exists while a statement of the pinned path is in
828 # flight on the connection outside pipeline mode, plus
829 # queue the statements waiting behind it, [ $sql, $bind, $f ]
830 # ending 'COMMIT' / 'ROLLBACK' once one of those is queued
831 # or in flight; a statement issued then is refused.
832 # Core ends the transaction context at that moment
833 # too (core k237), so what still meets this refusal
834 # is a caller past the context, on txn_conn
835 # %PG_PIPELINE a record exists between _pipeline_enter and _pipeline_exit
836 65 my %PG_PINNED;
837 my %PG_PIPELINE;
838  
839 # A statement in flight whose Future is cancelled asks the server to stop it
840 # (karr k75): EV::Pg cancel_async, libpq 17+, never the blocking cancel. Returns
841 # the statement's guard, which the statement's completion callback keeps:
842 # answered the statement's result is in
843 # cancelling a cancel request is out and not answered
844 # then what the result's callback left undone while it was
845 # The callback hands the rest of its work to `then` when it finds a request
846 # out, and the request's answer runs it: the connection goes on -- released,
847 # its next statement sent -- only when result and answer are both in, else the
848 # request reaches the next statement of the connection. A request that ends
849 # with an error marks the connection dead for its pool ($pool, else the
850 # storage's, never built here: karr k88). The guard stays empty, and the cancel
851 # a no-op, without the capability, for a Future that is not pending, and in
852 # pipeline mode.
853 768 ▶sub _pg_cancel_guard {
Subroutine _pg_cancel_guard: called CC=3 SCAR=11.4
854 768 my ($self, $f, $pg, $pool) = @_;
855 768 my $guard = {};
856 768 ▶ 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") 686 82 768
Condition100%3/3
$f->is_ready or not $pg->can("cancel_async")
l!l&&r!l&&!r
268482
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
857 82 my $pid = $$;
858 82 Scalar::Util::weaken(my $weak_pg = $pg);
859 18 ▶ $f->on_cancel(sub {
Subroutine __ANON__: called CC=9 SCAR=23.8
860 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
861 18 ▶ my $pg = $weak_pg or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pg = $weak_pg or return 18 0 18
862 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
863 # A retirement has sent the request already (Pool::_shutdown_connection).
864 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
865 10 $guard->{cancelling} = 1;
866 10 DBIO::PostgreSQL::EV::Pool::_pg_cancel_begin($pg);
867 10 my $sent = eval {
868 10 ▶ $pg->cancel_async(sub {
Subroutine __ANON__: called CC=8 SCAR=21.2
869 10 my ($ok, $err) = @_;
870 10 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 10 10
871 10 $guard->{cancelling} = 0;
872 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
873 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
874 4 ▶ $owner->_mark_connection_dead($conn) if $owner;
Branch: 1 branch
BranchTrueFalseTotal
if $owner 4 0 4
875 }
876 10 my $then = delete $guard->{then};
877 10 ▶ my $ran = eval { $then->() if $then; 1 };
Branch: 1 branch
BranchTrueFalseTotal
if $then 6 4 10
878 10 my $died = $@;
879 # A retirement that waited for this answer closes the connection now.
880 10 DBIO::PostgreSQL::EV::Pool::_pg_cancel_end($weak_pg, $ok, $err);
881 10 ▶ die $died unless $ran;
Branch: 1 branch
BranchTrueFalseTotal
unless $ran 0 10 10
882 10 });
883 10 1;
884 };
885 10 ▶ unless ($sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sent) 0 10 10
886 0 $guard->{cancelling} = 0;
887 0 DBIO::PostgreSQL::EV::Pool::_pg_cancel_end($pg);
888 }
889 82 });
890 82 return $guard;
891 }
892  
893  
894 256 ▶sub _query_async_pinned {
Subroutine _query_async_pinned: called CC=12 SCAR=25.1
895 256 my ($self, $pg, $sql, $bind, $bind_attrs) = @_;
896 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
897 256 $sql = $self->_transform_sql($sql);
898  
899 256 my $trace_end = $self->_debug_query($sql, $bind, $bind_attrs);
900  
901 256 my $f = $self->future_class->new;
902 256 ▶ $f->on_ready($trace_end) if $trace_end;
Branch: 1 branch
BranchTrueFalseTotal
if $trace_end 5 251 256
903  
904 # Pipeline mode: libpq takes the statement at once (no callbacks on $f yet).
905 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
906 # Behind the sync point the server does not answer a statement until the
907 # next sync (karr k88): it waits here for pipeline mode to be left.
908 25 ▶ if ($pipeline->{synced}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pipeline->{"synced"}) 0 25 25
909 0 push @{ $pipeline->{queue} }, [ $sql, $bind, $f ];
910 0 return $f;
911 }
912 25 my $err = $self->_pg_send_pinned($pg, $sql, $bind, $f, 0);
913 25 ▶ $f->fail($err) if defined $err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 0 25 25
914 25 return $f;
915 }
916  
917 231 my $st = _pg_conn_record(\%PG_PINNED, $pg);
918 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
919 4 $f->fail(
920 "Statement refused: this transaction's $st->{ending} has already been "
921 . 'issued on its connection, and the statement would run after it, '
922 . 'outside the transaction. Chain every statement of a txn_do coderef '
923 . "into the Future the coderef returns.\n"
924 );
925 4 return $f;
926 }
927  
928 227 ▶ my $in_flight = $st ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$st ? : 36 191 227
929 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
930 227 ▶ $st->{ending} = $sql if $sql =~ /\A(?:COMMIT|ROLLBACK)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $sql =~ /\A(?:COMMIT|ROLLBACK)\z/ 69 158 227
931 227 push @{ $st->{queue} }, [ $sql, $bind, $f ];
932 227 ▶ $self->_pg_pinned_next($pg) unless $in_flight;
Branch: 1 branch
BranchTrueFalseTotal
unless $in_flight 191 36 227
933 227 return $f;
934 }
935  
936 1152 ▶sub _pg_conn_record {
Subroutine _pg_conn_record: called CC=5 SCAR=16.2
937 1152 my ($table, $pg, $create) = @_;
938 1152 my $addr = Scalar::Util::refaddr($pg);
939 1152 my $rec = $table->{$addr};
940 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
941 653 delete $table->{$addr};
942 653 ▶ return unless $create;
Branch: 1 branch
BranchTrueFalseTotal
unless $create 446 207 653
943 207 $rec = $table->{$addr} = { pid => $$, conn => $pg, queue => [] };
944 207 Scalar::Util::weaken($rec->{conn});
945 207 return $rec;
946 }
947  
948 # Nothing of the pinned path is in flight on $pg: send what waits in its queue,
949 # up to the first statement EV::Pg takes -- one cancelled in the queue is
950 # skipped, bar COMMIT and ROLLBACK (karr k75). When none is left, the record goes --
951 # the connection is idle. A statement EV::Pg refuses fails only once that is
952 # decided, so a callback on it finds the queue consistent; a die out of such a
953 # callback is returned, for the caller to rethrow once it has settled what it
954 # owes (the karr k50 order).
955 406 ▶sub _pg_pinned_next {
Subroutine _pg_pinned_next: called CC=10 SCAR=23.3
956 406 my ($self, $pg) = @_;
957 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
958 406 my ($sent, @refused);
959 406 ▶ while (my $next = shift @{ $st->{queue} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $next = shift @{$$st{"queue"};}) 227 191 418
960 227 my ($sql, $bind, $f) = @$next;
961 # Cancelled while it waited: not sent (karr k75). Core's COMMIT and
962 # ROLLBACK are not skipped -- the queue knows them as the end ('ending'),
963 # and the transaction would stay open, with its locks.
964 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
965 225 my $err = $self->_pg_send_pinned($pg, $sql, $bind, $f, 1);
966 225 ▶ if (!defined $err) { $sent = 1; last }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $err) 215 10 225
967 10 push @refused, [ $f, $err ];
968 }
969 406 ▶ delete $PG_PINNED{ Scalar::Util::refaddr($pg) } unless $sent;
Branch: 1 branch
BranchTrueFalseTotal
unless $sent 191 215 406
970  
971 406 my $died;
972 406 for my $refused (@refused) {
973 10 my ($f, $err) = @$refused;
974 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
975 }
976 406 return $died;
977 }
978  
979 # Send one statement of the pinned path; undef once EV::Pg has taken it, else
980 # EV::Pg's error -- it croaks before it queues the callback (karr k48, the k46
981 # pattern). Named sub, like _dispatch_query (karr k29): the completion callback
982 # is created here and does not hold $bind. The callback of a $queued statement
983 # sends the next one waiting before it settles $f.
984 250 ▶sub _pg_send_pinned {
Subroutine _pg_send_pinned: called CC=3 SCAR=11.1
985 250 my ($self, $pg, $sql, $bind, $f, $queued) = @_;
986 250 my $pid = $$;
987 # Pipeline mode (not $queued) sends no cancel request (karr k75).
988 250 ▶ my $guard = $queued ? $self->_pg_cancel_guard($f, $pg) : {};
Branch: 1 branch
BranchTrueFalseTotal
$queued ? : 225 25 250
989 250 my $values = _pg_error_values(@$bind); # as in _dispatch_query: copies, not $bind
990 240 ▶ my $on_result = sub {
Subroutine __ANON__: called CC=7 SCAR=20.0
991 240 my ($rows, $err) = @_;
992 240 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 240 240
993 # Global destruction: EV::Pg cancels the statements still waiting and calls
994 # back after the captured connection and Future are gone; nothing to deliver.
995 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
996 240 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
997 240 $guard->{answered} = 1;
998 240 $err = $self->_pg_server_error($pg, $err, $values); # karr k74
999 240 ▶ my $settle = sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
1000 # libpq is idle again -- EV::Pg delivers after the statement's last result
1001 # -- so the next statement goes out before $f's callbacks can issue more.
1002 240 ▶ my $next_died = $queued ? $self->_pg_pinned_next($pg) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$queued ? : 215 25 240
1003 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
1004 240 ▶ die $next_died if defined $next_died;
Branch: 1 branch
BranchTrueFalseTotal
if defined $next_died 0 240 240
1005 # Do NOT release -- the connection is pinned for the duration of the txn
1006 240 };
1007 # A cancel request is out: the next statement goes when it is answered (karr k75).
1008 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
1009 2 ▶ $guard->{then} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1010 2 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
1011 2 $settle->();
1012 2 };
1013 2 return;
1014 }
1015 238 $settle->();
1016 250 };
1017 250 my $sent = eval { $pg->query_params($sql, $bind, $on_result); 1 };
1018 250 ▶ return $sent ? undef : $self->_pg_dispatch_error($@);
Branch: 1 branch
BranchTrueFalseTotal
$sent ? : 240 10 250
1019 }
1020  
1021 # --- Pool connect-action replay seam (core k68) -------------------------------
1022  
1023  
1024 40 ▶sub _run_pool_connect_statement {
Subroutine _run_pool_connect_statement: called CC=7 SCAR=19.5
1025 40 my ($self, $conn, $sql, $attrs, @bind) = @_;
1026 40 require EV;
1027  
1028 # The statement's result is always waited for on the EV loop: refuse before
1029 # anything is sent (karr k52).
1030 40 $self->_pg_refuse_connect_wait;
1031  
1032 # Whatever the loop runs from here on runs above the spawn that opens $conn:
1033 # the pool counts the connection and hands it out only after this has
1034 # returned (karr k88).
1035 38 my $replaying = DBIO::PostgreSQL::EV::Future->_replaying($conn);
1036  
1037 # The connection was just spawned; its async connect may still be in flight.
1038 # Block the EV loop until it is ready (the core seam's synchronous contract).
1039 # Prefer the pool's per-connection readiness Future
1040 # (DBIO::Storage::PoolBase::_connection_ready_future, core k75), which is
1041 # FAILED on a connect error -- so a bad connect surfaces as a thrown error
1042 # here rather than busy-spinning on is_connected forever. Fall back to
1043 # is_connected for a pool that does not expose the readiness seam (e.g. a
1044 # bare test double that is not a PoolBase subclass at all).
1045 38 my $pool = $self->pool;
1046 38 ▶ if ($pool->can('_connection_ready_future')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pool->can('_connection_ready_future')) { } 28 10 38
1047 28 my $ready = $pool->_connection_ready_future($conn);
1048 28 ▶ $self->_pg_connect_wait(sub { $ready->is_ready });
Subroutine __ANON__: called CC=1 SCAR=0.0
1049 28 $ready->get; # rethrows a connect error
1050 }
1051 else {
1052 10 ▶ $self->_pg_connect_wait(sub { $conn->is_connected });
Subroutine __ANON__: called CC=1 SCAR=0.0
1053 }
1054  
1055 38 my $done = 0;
1056 38 my ($res, $err);
1057 38 ▶ my $cb = sub { ($res, $err) = @_; $done = 1 };
Subroutine __ANON__: called CC=1 SCAR=0.0
1058 38 ▶ if (@bind) {
Branch: 1 branch
BranchTrueFalseTotal
if (@bind) { } 3 35 38
1059 3 $conn->query_params($self->_transform_sql($sql), [@bind], $cb);
1060 }
1061 else {
1062 35 $conn->query($sql, $cb);
1063 }
1064 38 ▶ $self->_pg_connect_wait(sub { $done });
Subroutine __ANON__: called CC=1 SCAR=0.0
1065  
1066 37 ▶ croak 'pool connect statement failed: '
Branch: 1 branch
BranchTrueFalseTotal
if defined $err 4 33 37
1067 . $self->_pg_mask_error_text($err, _pg_error_values(@bind), $self->_pg_sqlstate_of($conn, $err))
1068 if defined $err;
1069  
1070 # dbio_rows (core k358): hand back the rows, as selectall_arrayref does. A
1071 # statement that answers no rows (SET) has a command tag, not an array.
1072 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
1073 3 ▶ return ref $res eq 'ARRAY' ? $res : [];
Branch: 1 branch
BranchTrueFalseTotal
ref $res eq 'ARRAY' ? : 2 1 3
1074 }
1075 30 return;
1076 }
1077  
1078 # Run the EV loop until $ready->() is true, refused before every iteration.
1079 76 ▶sub _pg_connect_wait {
Subroutine _pg_connect_wait: called CC=2 SCAR=6.9
1080 76 my ($self, $ready) = @_;
1081 76 ▶ until ($ready->()) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$ready()) 51 75 126
1082 51 $self->_pg_refuse_connect_wait;
1083 50 EV::run(EV::RUN_ONCE());
1084 }
1085 75 return;
1086 }
1087  
1088 # Die while one of this transport's callbacks runs for a connection with more
1089 # results due: a loop run now would read them (karr k52; the await refusal of
1090 # karr k44).
1091 91 ▶sub _pg_refuse_connect_wait {
Subroutine _pg_refuse_connect_wait: called CC=2 SCAR=6.9
1092 91 my ($self) = @_;
1093 91 ▶ 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 88 91
1094 3 croak 'Blocking wait refused: the pool is opening a new connection and '
1095 . 'replaying on_connect_do / on_connect_call on it, which runs the EV '
1096 . 'loop, inside a callback of an EV::Pg connection that has '
1097 . ($pg->pending_count - 1) . ' more result(s) due; EV::Pg would hand '
1098 . 'the next of them to the callback that is still running, then spin. '
1099 . 'Chain this call with ->then onto the statement in flight, or run it '
1100 . 'on the transaction context ($ctx->select_async, ...)';
1101 }
1102  
1103 # --- Pipeline mode seams ------------------------------------------------------
1104 # The inherited DBIO::Storage::Async::pipeline scaffold acquires a connection,
1105 # brackets the batch with these three seams and releases the connection. EV::Pg
1106 # expresses pipelining natively, so the base scaffold fits exactly -- no
1107 # parallel pipeline() is needed (WP4 decision).
1108  
1109  
1110 17 ▶sub _pipeline_enter {
Subroutine _pipeline_enter: called CC=2 SCAR=6.9
1111 17 my ($self, $pg) = @_;
1112 # EV::Pg's croak names the line here that called it (karr k88)
1113 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
1114 16 _pg_conn_record(\%PG_PIPELINE, $pg, 1);
1115 16 return;
1116 }
1117  
1118  
1119 17 ▶sub _pipeline_sync {
Subroutine _pipeline_sync: called CC=2 SCAR=7.0
1120 17 my ($self, $pg) = @_;
1121 17 my $f = $self->future_class->new;
1122 17 my $pid = $$;
1123 17 my $pipeline = _pg_conn_record(\%PG_PIPELINE, $pg);
1124 19 ▶ $pg->pipeline_sync(sub {
Subroutine __ANON__: called CC=7 SCAR=20.2
1125 19 my (undef, $err) = @_;
1126 19 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 1 18 19
1127 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
1128 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
1129 17 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
1130 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
1131 17 });
1132 17 ▶ $pipeline->{synced} = 1 if $pipeline;
Branch: 1 branch
BranchTrueFalseTotal
if $pipeline 12 5 17
1133 17 return $f;
1134 }
1135  
1136  
1137 17 ▶sub _pipeline_exit {
Subroutine _pipeline_exit: called CC=8 SCAR=26.9
1138 17 my ($self, $pg) = @_;
1139 17 my $pipeline = _pg_conn_record(\%PG_PIPELINE, $pg);
1140 17 my $exited = eval { $pg->exit_pipeline; 1 };
1141 17 my $error = $self->_pg_dispatch_error($@); # without the place (karr k88)
1142 17 ▶ my @waiting = $pipeline ? splice @{ $pipeline->{queue} } : ();
Branch: 1 branch
BranchTrueFalseTotal
$pipeline ? : 16 1 17
1143  
1144 17 ▶ if (!$exited) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($exited) 5 12 17
1145 # Still in pipeline mode: what waited for the exit never goes out.
1146 5 for my $waiting (@waiting) {
1147 0 eval { $waiting->[2]->fail($error); 1 };
1148 }
1149 5 die $error;
1150 }
1151  
1152 12 delete $PG_PIPELINE{ Scalar::Util::refaddr($pg) };
1153 12 ▶ return unless @waiting;
Branch: 1 branch
BranchTrueFalseTotal
unless @waiting 12 0 12
1154  
1155 # The statements a transaction issued behind the sync, its ROLLBACK among
1156 # them, go out now, one at a time, as on any pinned connection.
1157 0 my $st = _pg_conn_record(\%PG_PINNED, $pg, 1);
1158 0 for my $waiting (@waiting) {
1159 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
1160 0 push @{ $st->{queue} }, $waiting;
1161 }
1162 0 my $died = $self->_pg_pinned_next($pg);
1163 0 ▶ die $died if defined $died;
Branch: 1 branch
BranchTrueFalseTotal
if defined $died 0 0 0
1164 0 return;
1165 }
1166  
1167 # --- LISTEN/NOTIFY seams (core ADR 0056) --------------------------------------
1168 #
1169 # Core DBIO::Storage::Async owns the public listen / unlisten / notify /
1170 # listen_alive / listen_reconnect, the hooks, the backoff and the machinery
1171 # behind them -- including the queue that runs one LISTEN / UNLISTEN at a time
1172 # on the listen connection, which replaced this class's own serialiser (karr
1173 # k15, k43). What is left is EV::Pg's. Private helpers are named _pg_*, never
1174 # _listen_*: they share an inheritance chain with core's private _listen_*
1175 # helpers, and a same-named method appearing there later would be shadowed.
1176 #
1177 # The listen connection is a hashref, opaque to core:
1178 #
1179 # pg the EV::Pg handle, a DBIO::PostgreSQL::EV::Pool::_Connection; gone
1180 # once closed
1181 # state what the handle's handlers do with what EV::Pg reports:
1182 # opening connecting; an error fails the open
1183 # open connected, not watched yet; notifications are held
1184 # watching notifications go to core, an error is a lost connection
1185 # quiet unwatched or closed; nothing reaches core any more
1186 # held the notifications held while 'open', in arrival order
1187  
1188  
1189 285 ▶sub _listen_open {
Subroutine _listen_open: called CC=4 SCAR=13.9
1190 285 my ($self) = @_;
1191 285 my $provider = $self->_conninfo_provider;
1192 285 ▶ my $conninfo = $provider ? conninfo_string($provider->()) : $self->_conninfo_string;
Branch: 1 branch
BranchTrueFalseTotal
$provider ? : 156 129 285
1193 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
1194 unless defined $conninfo && length $conninfo;
1195  
1196 111 my $f = $self->future_class->new;
1197 111 my $conn = { state => 'opening', held => [] };
1198  
1199 # Nothing else holds the connection before core receives it: the open's
1200 # Future does, until it is ready.
1201 111 my $hold = $conn;
1202 111 ▶ $f->on_ready(sub { undef $hold });
Subroutine __ANON__: called CC=1 SCAR=0.0
1203  
1204 111 $conn->{pg} = DBIO::PostgreSQL::EV::Pool->_new_connection(
1205 conninfo => $conninfo,
1206 keep_alive => 1,
1207 $self->_pg_listen_handlers($f, $conn),
1208 );
1209 111 return $f;
1210 }
1211  
1212 # The handle's on_connect / on_error / on_notify. Built in a named sub, so they
1213 # close over nothing but these arguments: the storage and the connection weakly
1214 # -- the handle keeps its handlers for its lifetime, and the connection holds
1215 # the handle -- and the open's Future only until the connect settles. Every one
1216 # returns at once in a process other than the opener (karr k45).
1217 111 ▶sub _pg_listen_handlers {
Subroutine _pg_listen_handlers: called CC=1 SCAR=0.2
1218 111 my ($self, $f, $conn) = @_;
1219 111 Scalar::Util::weaken(my $weak_self = $self);
1220 111 Scalar::Util::weaken(my $weak_conn = $conn);
1221 111 my $opening = $f;
1222 111 my $pid = $$;
1223  
1224 100 ▶ my $on_connect = sub {
Subroutine __ANON__: called CC=5 SCAR=18.1
1225 100 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 100 100
1226 100 my ($f, $conn) = ($opening, $weak_conn);
1227 100 undef $opening;
1228 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
1229 100 $conn->{state} = 'open';
1230 100 $f->done($conn);
1231 100 return;
1232 111 };
1233  
1234 2 ▶ my $on_error = sub {
Subroutine __ANON__: called CC=8 SCAR=22.7
1235 2 my ($err) = @_;
1236 2 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 2 2
1237 2 ▶ chomp($err = defined $err ? "$err" : 'unknown error');
Branch: 1 branch
BranchTrueFalseTotal
defined $err ? : 2 0 2
1238 2 ▶ if (my $f = $opening) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $opening) 1 1 2
1239 1 undef $opening;
1240 1 ▶ $f->fail("connecting the listen connection failed: $err") unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 1 0 1
1241 1 return;
1242 }
1243 1 my ($self, $conn) = ($weak_self, $weak_conn);
1244 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
1245 1 $self->_listen_lost($conn, "the listen connection was lost: $err");
1246 1 return;
1247 111 };
1248  
1249 17 ▶ my $on_notify = sub {
Subroutine __ANON__: called CC=6 SCAR=18.8
1250 17 my ($channel, $payload, $sender_pid) = @_;
1251 17 ▶ return if $$ != $pid;
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 17 17
1252 17 my ($self, $conn) = ($weak_self, $weak_conn);
1253 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
1254 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
1255 13 $self->_listen_dispatch($channel, $payload, $sender_pid);
1256 }
1257 elsif ($conn->{state} eq 'open') {
1258 4 push @{ $conn->{held} }, [ $channel, $payload, $sender_pid ];
1259 }
1260 17 return;
1261 111 };
1262  
1263 return (
1264 111 on_connect => $on_connect,
1265 on_error => $on_error,
1266 on_notify => $on_notify,
1267 );
1268 }
1269  
1270  
1271 100 ▶sub _listen_close {
Subroutine _listen_close: called CC=2 SCAR=7.0
1272 100 my ($self, $conn) = @_;
1273 100 $self->_pg_listen_quiet($conn);
1274 100 ▶ my $pg = delete $conn->{pg} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pg = delete $conn->{"pg"} or return 100 0 100
1275 100 $pg->finish;
1276 100 return;
1277 }
1278  
1279  
1280 34 ▶sub _listen_subscribe {
Subroutine _listen_subscribe: called CC=1 SCAR=0.0
1281 34 my ($self, $conn, $channel) = @_;
1282 34 return $self->_pg_listen_command($conn, LISTEN => $channel);
1283 }
1284  
1285  
1286 4 ▶sub _listen_unsubscribe {
Subroutine _listen_unsubscribe: called CC=1 SCAR=0.0
1287 4 my ($self, $conn, $channel) = @_;
1288 4 return $self->_pg_listen_command($conn, UNLISTEN => $channel);
1289 }
1290  
1291 # One LISTEN / UNLISTEN on the listen connection. Named sub, like
1292 # _dispatch_query (karr k29): the completion callback is created here.
1293 38 ▶sub _pg_listen_command {
Subroutine _pg_listen_command: called CC=3 SCAR=11.3
1294 38 my ($self, $conn, $verb, $channel) = @_;
1295 38 my $sql = "$verb " . $self->_pg_quote_channel($channel);
1296 my $pg = $conn->{pg}
1297 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
1298  
1299 31 my $f = $self->future_class->new;
1300 31 my $pid = $$;
1301 30 ▶ my $on_result = sub {
Subroutine __ANON__: called CC=7 SCAR=22.4
1302 30 my (undef, $err) = @_;
1303 30 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 30 30
1304 # Closing the connection from inside this callback -- the last channel
1305 # unlistened by a callback on this Future -- makes EV::Pg call it again
1306 # with "connection finished" while it runs (see _pipeline_sync).
1307 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
1308 30 ▶ return if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 2 28 30
1309 28 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
1310 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
1311 31 };
1312  
1313 # EV::Pg croaks before it queues $on_result, so that never runs: fail here
1314 # (karr k46).
1315 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
1316 3 $f->fail($self->_pg_dispatch_error($@));
1317 }
1318 31 return $f;
1319 }
1320  
1321  
1322 51 ▶sub _pg_quote_channel {
Subroutine _pg_quote_channel: called CC=1 SCAR=0.0
1323 51 my ($self, $channel) = @_;
1324 return DBIO::PostgreSQL::EV::Storage::_Refusal::of_util(
1325 51 ▶ sub { DBIO::PostgreSQL::Util::quote_channel($channel) },
Subroutine __ANON__: called CC=1 SCAR=0.0
1326 51 );
1327 }
1328  
1329  
1330 17 ▶sub _listen_watch {
Subroutine _listen_watch: called CC=5 SCAR=16.1
1331 17 my ($self, $conn) = @_;
1332 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
1333 16 ▶ _pg_refuse('the listen connection is not connected') unless $pg->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $pg->is_connected 2 14 16
1334  
1335 14 $conn->{state} = 'watching';
1336 14 ▶ while (my $n = shift @{ $conn->{held} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = shift @{$$conn{"held"};}) 3 13 16
1337 3 $self->_listen_dispatch(@$n);
1338 3 ▶ last unless $conn->{state} eq 'watching'; # a callback ended it
Branch: 1 branch
BranchTrueFalseTotal
unless $conn->{"state"} eq "watching" 1 2 3
1339 }
1340 14 return;
1341 }
1342  
1343  
1344 14 ▶sub _listen_unwatch {
Subroutine _listen_unwatch: called CC=1 SCAR=0.0
1345 14 my ($self, $conn) = @_;
1346 14 $self->_pg_listen_quiet($conn);
1347 14 return;
1348 }
1349  
1350 # Nothing the handle reports reaches core any more. Safe from inside one of
1351 # the handlers: EV::Pg holds a reference to the handler it is running.
1352 114 ▶sub _pg_listen_quiet {
Subroutine _pg_listen_quiet: called CC=3 SCAR=11.0
1353 114 my ($self, $conn) = @_;
1354 114 $conn->{state} = 'quiet';
1355 114 @{ $conn->{held} } = ();
1356 114 ▶ my $pg = $conn->{pg} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $pg = $conn->{"pg"} or return 114 0 114
1357 114 $pg->$_(undef) for qw(on_connect on_error on_notify);
1358 114 return;
1359 }
1360  
1361  
1362 20 ▶sub _listen_probe {
Subroutine _listen_probe: called CC=4 SCAR=13.9
1363 20 my ($self, $conn) = @_;
1364 20 ▶ my $pg = $conn->{pg} or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $pg = $conn->{"pg"} or return 0 19 1 20
1365 19 ▶ return 0 unless $pg->is_connected;
Branch: 1 branch
BranchTrueFalseTotal
unless $pg->is_connected 4 15 19
1366 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
1367 }
1368  
1369  
1370 808 ▶sub _listen_delay {
Subroutine _listen_delay: called CC=1 SCAR=0.0
1371 808 my ($self, $seconds) = @_;
1372 808 my $f = $self->future_class->new;
1373 808 my $timer;
1374 808 EV::now_update();
1375 8 ▶ $timer = EV::timer($seconds, 0, sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
1376 8 undef $timer;
1377 8 ▶ $f->done unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 8 0 8
1378 808 });
1379 808 ▶ $f->on_cancel(sub { undef $timer });
Subroutine __ANON__: called CC=1 SCAR=0.0
1380 808 return $f;
1381 }
1382  
1383  
1384 44 ▶sub _notify_send {
Subroutine _notify_send: called CC=2 SCAR=7.0
1385 44 my ($self, $channel, $payload) = @_;
1386 # Released to the pool it was acquired from (karr k88, as in _query_async).
1387 44 my $pool = $self->pool;
1388 44 ▶ my $f = $pool->acquire->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1389 44 my $pg = shift;
1390 44 return $self->_dispatch_notify($pg, $channel, $payload, $pool, $pool->_lease($pg));
1391 44 });
1392 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
1393 44 return $f;
1394 }
1395  
1396  
1397 46 ▶sub _dispatch_notify {
Subroutine _dispatch_notify: called CC=4 SCAR=14.2
1398 46 my ($self, $pg, $channel, $payload, $pool, $lease) = @_;
1399 46 ▶ croak '_dispatch_notify needs the pool the connection was acquired from' unless $pool;
Branch: 1 branch
BranchTrueFalseTotal
unless $pool 1 45 46
1400 45 my $f = $self->future_class->new;
1401 45 my $pid = $$;
1402 45 my $values = _pg_error_values($channel, $payload);
1403  
1404 40 ▶ my $on_result = sub {
Subroutine __ANON__: called CC=6 SCAR=21.6
1405 40 my ($res, $err) = @_;
1406 40 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 40 40
1407 # Global destruction: EV::Pg cancels the statements still waiting and calls
1408 # back after the captured connection and Future are gone; nothing to deliver.
1409 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
1410 40 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
1411 40 $err = $self->_pg_server_error($pg, $err, $values); # karr k74
1412 40 $self->_pg_settle_and_release($pool, $pg, $lease, $f, $err); # done void
1413 45 };
1414  
1415 # pg_notify() with bind params -- NOTIFY itself takes no placeholders,
1416 # and inlining the payload as a string literal invites quoting bugs.
1417 # EV::Pg croaks before it queues $on_result: release here (karr k46).
1418 45 my $sent = eval {
1419 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
1420 40 1;
1421 };
1422 45 ▶ unless ($sent) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sent) 5 40 45
1423 5 my $err = $self->_pg_dispatch_error($@);
1424 5 $self->_pg_report_if_dead($pool, $pg);
1425 5 $pool->release($pg, $lease);
1426 5 $f->fail($err);
1427 }
1428  
1429 45 return $f;
1430 }
1431  
1432 # --- COPY ---------------------------------------------------------------------
1433  
1434 # One value in PostgreSQL's COPY text format: the backslash is the escape
1435 # character; TAB and LF delimit fields and rows, CR is a line terminator the
1436 # server knows too. One pass, so an escape is never escaped again (karr k98).
1437 65 my %COPY_TEXT_ESCAPE = ( "\\" => '\\\\', "\t" => '\\t', "\n" => '\\n', "\r" => '\\r' );
1438 361 ▶sub _copy_text_escape {
Subroutine _copy_text_escape: called CC=1 SCAR=0.0
1439 361 my ($value) = @_;
1440 361 $value =~ s/([\\\t\n\r])/$COPY_TEXT_ESCAPE{$1}/g;
1441 361 return $value;
1442 }
1443  
1444  
1445 129 ▶sub copy_in {
Subroutine copy_in: called CC=2 SCAR=6.9
Pod documented
1446 129 my ($self, $table, $columns, $coderef) = @_;
1447  
1448 129 my $col_list = join(', ', map { DBIO::PostgreSQL::Util::quote_ident($_) } @$columns);
1449 129 my $quoted_table = DBIO::PostgreSQL::Util::quote_qualified(split /\./, $table);
1450 129 my $sql = "COPY $quoted_table ($col_list) FROM STDIN";
1451  
1452 # The pool this COPY begins on is the one it ends on: the release, the report
1453 # of a dead connection and the retirement of an open stream all go to $pool,
1454 # never to $self->pool asked again (karr k88, as in _query_async). The
1455 # coderef runs inside the COPY's delivery, as the callbacks on its Future do.
1456 129 my $pool = $self->pool;
1457 129 ▶ my $seq = $pool->acquire->then(sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
1458 129 my $pg = shift;
1459 129 my $lease = $pool->_lease($pg); # every release below passes it (karr k89)
1460 129 my $f = $self->future_class->new;
1461  
1462 # EV::Pg fires the COPY query callback TWICE (see EV::Pg::Pg POD,
1463 # "A COPY command runs in two phases"): first with the string tag
1464 # "COPY_IN" when libpq has accepted the COPY command and the stream
1465 # is ready, then a second time with the final cmd_tuples result when
1466 # libpq has consumed the EOF marker, applied every row, and sent
1467 # CommandComplete + ReadyForQuery. The connection is NOT safe to
1468 # hand back to the pool until that second firing -- releasing after
1469 # the first lets the next acquire race libpq's still-pending reply
1470 # and fail with "another command is already in progress" (karr k13).
1471 #
1472 # $phase is where the COPY stands:
1473 # sent the COPY command is out, the server has not answered it
1474 # open the stream is open: the coderef runs, then put_copy_end
1475 # closing libpq has taken the end of the stream, the server's answer
1476 # to it is due -- the second firing
1477 # ended over, however it ended
1478 129 my $phase = 'sent';
1479 129 my $pid = $$;
1480 129 my $coderef_err; # die from the user's coderef, captured to forward
1481 # into the Future failure verbatim -- libpq wraps
1482 # whatever we pass to put_copy_end into its own
1483 # "ERROR: COPY from stdin failed: ..." envelope, so
1484 # callers would see the wrapped string instead of
1485 # their original die message unless we keep the
1486 # raw $@. See t/18 "dying coderef" assertion.
1487 my $write_err; # why the writer could not put a row, from the first
1488 # one on: the COPY fails with it (karr k65)
1489  
1490 # How the COPY ends: here, once, however it ends -- settled before
1491 # released, through _pg_settle_and_release (karr k51), and with the
1492 # coderef's error before any other. Ended is the COPY's own state, not
1493 # $f->is_ready: a Future the caller cancelled is ready, and its
1494 # connection still has to go back.
1495 #
1496 # A COPY that ends while its stream is open -- no put_copy_end libpq and
1497 # EV::Pg both took -- leaves a connection the pool cannot use again:
1498 # gone, or open with libpq still in the COPY, where every statement is
1499 # refused. It is reported dead before it is released, so the pool retires
1500 # it instead of idling it (karr k65). The server's answer leaves the
1501 # connection ready, or gone, which _pg_settle_and_release sees (karr k49).
1502 125 ▶ my $end = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
1503 125 my ($err, @result) = @_;
1504 125 my $stream_open = $phase eq 'open';
1505 125 $phase = 'ended';
1506 125 ▶ $pool->_mark_connection_dead($pg) if $stream_open;
Branch: 1 branch
BranchTrueFalseTotal
if $stream_open 52 73 125
1507 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
1508 129 };
1509 197 ▶ my $on_copy = sub {
Subroutine __ANON__: called CC=15 SCAR=27.3
1510 197 my ($status, $err) = @_;
1511 197 ▶ return if $$ != $pid; # a forked child's copy (karr k45)
Branch: 1 branch
BranchTrueFalseTotal
if $$ != $pid 0 197 197
1512 # A call after the COPY ended touches neither the Future nor the pool,
1513 # as in _pipeline_sync. EV::Pg keeps this callback queued when
1514 # put_copy_end answers -1 (Pg.xs put_copy_end, 0.02 and 0.07 alike) and
1515 # calls it again -- with "COPY_IN" on a read while libpq has the COPY
1516 # open, with "connection lost" or "connection finished" when the
1517 # connection goes. By then the connection is not this COPY's: released
1518 # again, it would have two holders (karr k57).
1519 197 ▶ return if $phase eq 'ended';
Branch: 1 branch
BranchTrueFalseTotal
if $phase eq "ended" 12 185 197
1520 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
1521 185 my $delivering = DBIO::PostgreSQL::EV::Future->_delivering($pg);
1522  
1523 # Second firing: the server's answer to the end of the stream. The
1524 # connection is back at ReadyForQuery and safe to reuse.
1525 185 ▶ if ($phase eq 'closing') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq "closing") 60 125 185
1526 60 $end->($self->_pg_server_error($pg, $err), $status);
1527 48 return;
1528 }
1529  
1530 # First firing. With an error the server refused the COPY, or the
1531 # connection went before it answered: no stream was opened.
1532 125 ▶ if (defined $err) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $err) 13 112 125
1533 13 $end->($self->_pg_server_error($pg, $err));
1534 9 return;
1535 }
1536 112 $phase = 'open';
1537  
1538 # The writer. A row it cannot put is the end of the COPY (karr k65):
1539 # put_copy_data answers 0 when libpq cannot grow its output buffer for
1540 # the row -- there is no waiting for room in here, the EV loop does not
1541 # run inside this callback -- and -1 on an error. Its croak, "not
1542 # connected" once EV::Pg has lost the connection, goes through
1543 # without its place (_pg_dispatch_error): put_copy_end croaks the same
1544 # then. Good only while the coderef
1545 # runs: after that $pg is the pool's, or in another caller's COPY, and
1546 # a call is refused (_pg_refuse). The writer has a name: core's
1547 # exception finds the caller's place past named frames only, and an
1548 # anonymous one would leave it the whole stack (karr k88).
1549 187 ▶ my $put = Sub::Util::set_subname 'DBIO::PostgreSQL::EV::Storage::copy_in_writer' => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
1550 187 my $row = shift;
1551 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
1552 185 ▶ croak $write_err if defined $write_err;
Branch: 1 branch
BranchTrueFalseTotal
if defined $write_err 2 183 185
1553 183 ▶ my $line = join("\t", map { defined $_ ? _copy_text_escape($_) : '\N' } @$row) . "\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 361 4 365
1554 183 my $rc = eval { $pg->put_copy_data($line) };
1555 183 ▶ die $self->_pg_dispatch_error($@) unless defined $rc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rc 9 174 183
1556 174 ▶ return $rc if $rc == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $rc == 1 170 4 174
1557 4 croak $write_err = $self->_pg_copy_failure($pg, 'put_copy_data');
1558 112 };
1559  
1560 112 my $ran = eval { $coderef->($put); 1 };
1561 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
1562  
1563 # End the stream. An error goes to libpq, so the server throws the
1564 # COPY away; the Future fails with the raw $coderef_err, not with
1565 # libpq's wrapped "ERROR: COPY from stdin failed: ..." envelope.
1566 112 my $rc = eval {
1567 112 ▶ defined $coderef_err ? $pg->put_copy_end($coderef_err) : $pg->put_copy_end;
Branch: 1 branch
BranchTrueFalseTotal
defined $coderef_err ? : 36 76 112
1568 };
1569 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
1570 # put_copy_end croaked "not connected": EV::Pg lost the connection
1571 # during the COPY and dropped this callback on the way, so no second
1572 # firing will come (karr k46).
1573 19 $end->($self->_pg_dispatch_error($@));
1574 }
1575 elsif ($rc < 0) {
1576 # PQputCopyEnd failed, and EV::Pg leaves it at that: this callback
1577 # stays queued, and libpq is still in the COPY if it could not queue
1578 # the end of the stream. No answer of the server's is due.
1579 29 $end->($self->_pg_copy_failure($pg, 'put_copy_end'));
1580 }
1581 elsif (!$pg->is_connected) {
1582 # libpq took the end of the stream, and EV::Pg lost the connection
1583 # in the flush behind it: libpq had dropped it in an earlier write
1584 # (PQflush answers -1 on such a connection). EV::Pg drops the
1585 # callback it is delivering to without a second firing, so this is
1586 # the only place to end; "connection lost" is what it tells a
1587 # callback it does call.
1588 4 $end->('connection lost');
1589 }
1590 else {
1591 # The second firing completes $f and releases the connection.
1592 60 $phase = 'closing';
1593 }
1594 96 return;
1595 129 };
1596  
1597 # EV::Pg croaks before it queues $on_copy ("not connected", "another
1598 # command is already in progress"), so neither firing will come:
1599 # release here, once (karr k46).
1600 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
1601 4 my $err = $self->_pg_dispatch_error($@);
1602 4 $self->_pg_report_if_dead($pool, $pg);
1603 4 $pool->release($pg, $lease);
1604 4 $f->fail($err);
1605 }
1606  
1607 129 return $f;
1608 129 });
1609 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
1610 129 return $seq;
1611 }
1612  
1613 # What a failed put_copy_data / put_copy_end of copy_in says: the call, and
1614 # libpq's reason for it when it has one -- "cannot allocate memory for output
1615 # buffer", "server closed the connection unexpectedly" -- which is the
1616 # connection's error message at that moment. That is libpq's text: masked,
1617 # like every text of libpq's that is passed on here (karr k78), and without
1618 # its trailing newline -- the writer croaks with it.
1619 33 ▶sub _pg_copy_failure {
Subroutine _pg_copy_failure: called CC=3 SCAR=11.0
1620 33 my ($self, $pg, $call) = @_;
1621 33 my $why = $pg->error_message;
1622 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
1623 21 $why =~ s/\s+\z//;
1624 21 return "$call failed: " . DBIO::Util::mask_connect_secrets($why);
1625 }
1626  
1627 # --- Async Deploy -------------------------------------------------------------
1628 # deploy_async / _execute_ddl_async / _drop_statements_for / the sync deploy
1629 # fallback are all INHERITED from DBIO::Storage::Async (core karr k73): the
1630 # generic implementation already does exactly what this transport needs --
1631 # generate DDL, probe _use_transactional_ddl, run it through the
1632 # _query_async_pinned seam this class implements for CRUD. Filling _ddl_class
1633 # is the only seam a Model-B backend has to provide (karr k25; precedent
1634 # dbio-mysql-ev k21, commit d86446e).
1635  
1636  
1637 12 ▶sub _ddl_class { 'DBIO::PostgreSQL::DDL' }
Subroutine _ddl_class: called CC=1 SCAR=0.0
1638  
1639  
1640 10 ▶sub deploy_async {
Subroutine deploy_async: called CC=3 SCAR=11.0
Pod documented
1641 10 my ($self, $schema, @rest) = @_;
1642  
1643 10 my @declared = DBIO::PostgreSQL::DDL::_schema_decl($schema, 'pg_extensions');
1644 10 ▶ return $self->SUPER::deploy_async($schema, @rest) unless @declared;
Branch: 1 branch
BranchTrueFalseTotal
unless @declared 6 4 10
1645  
1646 my $f = $self->_seam_future(_query_async =>
1647 DBIO::PostgreSQL::DDL::control_schemas_sql(@declared), [],
1648 4 ▶ )->then(sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1649 4 my %fixed = map { $_->[0] => $_->[1] } @_;
1650 4 DBIO::PostgreSQL::DDL::check_control_schemas($schema, \%fixed);
1651 3 return $self->_deploy_async_with_control_schemas($schema, \%fixed, @rest);
1652 4 });
1653 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
1654 4 return $f;
1655 }
1656  
1657 # The schemas a control file fixes are handed to _install_ddl for the length
1658 # of the synchronous call core's deploy_async makes to it, and no longer: a
1659 # second deploy on the same storage reads its own.
1660 3 ▶sub _deploy_async_with_control_schemas {
Subroutine _deploy_async_with_control_schemas: called CC=1 SCAR=0.0
1661 3 my ($self, $schema, $fixed, @rest) = @_;
1662 3 local $self->{_extension_control_schemas} = $fixed;
1663 3 return $self->SUPER::deploy_async($schema, @rest);
1664 }
1665  
1666  
1667 9 ▶sub _install_ddl {
Subroutine _install_ddl: called CC=2 SCAR=6.9
1668 9 my ($self, $schema) = @_;
1669 return $self->_ddl_class->install_ddl($schema,
1670 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
1671 }
1672  
1673 # --- Schema Integration -------------------------------------------------------
1674 # schema / debug / connected / disconnect / DESTROY and the sync
1675 # CRUD/txn/deploy ->get fallbacks are inherited from DBIO::Storage::Async.
1676 # disconnect was overridden here until karr k43, only to finish the dedicated
1677 # LISTEN connection this class kept itself; core's disconnect rotates the
1678 # context-owner token, ends listening (core ADR 0056) and shuts the pool down.
1679  
1680 65 1;
1681  
1682 __END__
« blib/lib/DBIO/PostgreSQL/EV/ConnectInfo.pm ↑ Summary blib/lib/DBIO/PostgreSQL/EV/Pool.pm »