PostgreSQL.pm

filter: Statementstmt 25.7% 121 / 470 Branchbran 13.1% 19 / 144 Conditioncond 19.6% 20 / 102 MC/DCmcdc 1.7% 1 / 57 Subroutinesub 30.9% 34 / 110 Podpod 52.6% 20 / 38 totaltotal 23.3% 215 / 921 CC 118 SCAR 36.6
CC
118
Cov
22%
_remove_from_loop
240.0
connect
210.0
negotiate_ssl
56.0
CRAP 38.9
↑ Summary PostgreSQL/DDL.pm »
1 package Database::Async::Engine::PostgreSQL;
2 # ABSTRACT: PostgreSQL support for Database::Async
3  
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 7 our $VERSION = '1.005';
10  
11 7 ▶use parent qw(Database::Async::Engine);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =encoding utf8
14  
15 =head1 NAME
16  
17 Database::Async::Engine::PostgreSQL - support for PostgreSQL databases in L<Database::Async>
18  
19 =head1 DESCRIPTION
20  
21 Provide a C<postgresql://> URI when instantiating L<Database::Async> to use this engine.
22  
23 $loop->add(
24 my $dbh = Database::Async->new(
25 uri => 'postgresql://localhost'
26 )
27 );
28  
29 Connection can also be made using a service definition, as described in L<https://www.postgresql.org/docs/current/libpq-pgservice.html>.
30  
31 $loop->add(
32 my $dbh = Database::Async->new(
33 type => 'postgresql',
34 engine => {
35 service => 'example',
36 }
37 )
38 );
39  
40 If neither URI nor service are provided, the C<PGSERVICE> environment variable is attempted, and will fall back
41 to localhost (similar to C<psql -h localhost> behaviour).
42  
43 $loop->add(
44 my $dbh = Database::Async->new(
45 type => 'postgresql',
46 )
47 );
48  
49  
50 =cut
51  
52 7 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 7 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 7 ▶use Ryu::Async;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 7 ▶use Ryu::Observable;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 7 ▶use curry;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 7 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
58 7 ▶use URI::postgres;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 7 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 7 ▶use Future::AsyncAwait;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 7 ▶use Database::Async::Query;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 7 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 7 ▶use Config::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 7 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
65 7 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
66 7 ▶use Bytes::Random::Secure ();
Subroutine BEGIN: called CC=1 SCAR=0.0
67 7 ▶use Unicode::UTF8;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 7 ▶use Crypt::Digest::SHA256 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
69 7 ▶use Crypt::Mac::HMAC ();
Subroutine BEGIN: called CC=1 SCAR=0.0
70  
71 7 ▶use Protocol::Database::PostgreSQL::Client 2.000;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 7 ▶use Protocol::Database::PostgreSQL::Constants qw(:v1);
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 7 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 use overload
77 0 ▶ '""' => sub { ref(shift) },
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=1 SCAR=6.9
78 0 ▶ bool => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
79 7 fallback => 1;
80  
81 7 Database::Async::Engine->register_class(
82 postgresql => __PACKAGE__
83 );
84  
85 =head1 METHODS
86  
87 =head2 configure
88  
89 =cut
90  
91 4 ▶sub configure {
Subroutine configure: called CC=3 SCAR=11.1
Pod documented
92 4 my ($self, %args) = @_;
93 4 for (qw(service encoding application_name)) {
94 12 ▶ $self->{$_} = delete $args{$_} if exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$_} 0 12 12
95 }
96 4 return $self->next::method(%args);
97 }
98  
99 0 ▶sub encoding { shift->{encoding} }
Subroutine encoding: not called CC=1 SCAR=6.9
Pod undocumented
100  
101 =head2 connection
102  
103 Returns a L<Future> representing the database connection,
104 and will attempt to connect if we are not already connected.
105  
106 =cut
107  
108 0 ▶sub connection {
Subroutine connection: not called CC=2 SCAR=17.9
Pod documented
109 0 my ($self) = @_;
110 0 ▶ $self->{connection} //= $self->connect;
Condition0%0/3
$self->{"connection"} //= $self->connect
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"connection"} //= $self->connect
$self->{"connection"} $self->connect
Truth table0%0/3
A //= B
A= $self->{"connection"}B= $self->connect
ABresult
1X1
011
000
111 }
112  
113 =head2 ssl
114  
115 Whether to try SSL or not, expected to be one of the following
116 values from L<Protocol::Database::PostgreSQL::Constants>:
117  
118 =over 4
119  
120 =item * C<SSL_REQUIRE>
121  
122 =item * C<SSL_PREFER>
123  
124 =item * C<SSL_DISABLE>
125  
126 =back
127  
128 =cut
129  
130 0 ▶sub ssl { shift->{ssl} }
Subroutine ssl: not called CC=1 SCAR=6.9
Pod documented
131  
132 =head2 read_len
133  
134 Buffer read length. Higher values mean we will attempt to read more
135 data for each I/O loop iteration.
136  
137 Defaults to 2 megabytes.
138  
139 =cut
140  
141 0 ▶sub read_len { shift->{read_len} //= 2 * 1024 * 1024 }
Subroutine read_len: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"read_len"} //= 2097152
l!l
00
MC/DC0%0/1
+(shift())->{"read_len"} //= 2097152
+(shift())->{"read_len"}
Truth table0%0/2
A
A= +(shift())->{"read_len"}
Aresult
11
00
142  
143 =head2 write_len
144  
145 Buffer write length. Higher values mean we will attempt to write more
146 data for each I/O loop iteration.
147  
148 Defaults to 2 megabytes.
149  
150 =cut
151  
152 0 ▶sub write_len { shift->{write_len} //= 2 * 1024 * 1024 }
Subroutine write_len: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
+(shift())->{"write_len"} //= 2097152
l!l
00
MC/DC0%0/1
+(shift())->{"write_len"} //= 2097152
+(shift())->{"write_len"}
Truth table0%0/2
A
A= +(shift())->{"write_len"}
Aresult
11
00
153  
154 =head2 connect
155  
156 Establish a connection to the server.
157  
158 Returns a L<Future> which resolves to the L<IO::Async::Stream>
159 once ready.
160  
161 =cut
162  
163 0 ▶async sub connect {
Subroutine connect: not called CC=14 SCAR=53.5
Pod documented
164 0 my ($self) = @_;
165 0 my $loop = $self->loop;
166  
167 0 my $connected = $self->connected;
168 0 ▶ die 'We think we are already connected, and that is bad' if $connected->as_numeric;
Branch: 1 branch
BranchTrueFalseTotal
if $connected->as_numeric 0 0 0
169  
170 # Initial connection is made directly through the URI
171 # parameters. Eventually we also want to support UNIX
172 # socket and other types.
173 0 ▶ $self->{uri} ||= $self->uri_for_service($self->service) if $self->service;
Branch: 1 branch
BranchTrueFalseTotal
if $self->service 0 0 0
Condition0%0/3
$self->{"uri"} ||= $self->uri_for_service($self->service)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"uri"} ||= $self->uri_for_service($self->service)
$self->{"uri"} $self->uri_for_service($self->service)
Truth table0%0/3
A ||= B
A= $self->{"uri"}B= $self->uri_for_service($self->service)
ABresult
1X1
011
000
174 0 my $uri = $self->uri;
175 0 ▶ die 'bad URI' unless ref $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $uri 0 0 0
176 0 $log->tracef('URI for connection is %s', "$uri");
177 0 my $endpoint = join ':', $uri->host, $uri->port;
178  
179 0 $log->tracef('Will connect to %s', $endpoint);
180 0 $self->{ssl} = do {
181 0 ▶ my $mode = $uri->query_param('sslmode') // 'prefer';
Condition0%0/2
$uri->query_param("sslmode") // "prefer"
l!l
00
MC/DC0%0/1
$uri->query_param("sslmode") // "prefer"
$uri->query_param("sslmode")
Truth table0%0/2
A
A= $uri->query_param("sslmode")
Aresult
11
00
182 0 ▶ $Protocol::Database::PostgreSQL::Constants::SSL_NAME_MAP{$mode} // die 'unknown SSL mode ' . $mode;
Condition0%0/2
$Protocol::Database::PostgreSQL::Constants::SSL_NAME_MAP{$mode} // die('unknown SSL mode ' . $mode)
l!l
00
MC/DC0%0/1
$Protocol::Database::PostgreSQL::Constants::SSL_NAME_MAP{$mode} // die('unknown SSL mode ' . $mode)
$Protocol::Database::PostgreSQL::Constants::SSL_NAME_MAP{$mode}
Truth table0%0/2
A
A= $Protocol::Database::PostgreSQL::Constants::SSL_NAME_MAP{$mode}
Aresult
11
00
183 };
184  
185 # We're assuming TCP (either v4 or v6) here, but there's not really any reason we couldn't have
186 # UNIX sockets or other transport layers here other than lack of demand so far.
187 0 my @connect_params;
188 0 ▶ if ($uri->host and not $uri->host =~ m!^[/@]!) {
Branch: 2 branches
BranchTrueFalseTotal
if ($uri->host and not $uri->host =~ m[^[/\@]]) { } 0 0 0
elsif ($uri->host eq '') { } 0 0 0
Condition0%0/3
$uri->host and not $uri->host =~ m[^[/\@]]
!ll&&!rl&&r
000
MC/DC0%0/2
$uri->host and not $uri->host =~ m[^[/\@]]
$uri->host not $uri->host =~ m[^[/\@]]
Truth table0%0/3
A and not(B)
A= $uri->hostB= not $uri->host =~ m[^[/\@]]
ABresult
0X0
100
111
189 0 @connect_params = (
190 service => $uri->port,
191 host => $uri->host,
192 socktype => 'stream',
193 );
194 } elsif ($uri->host eq '') {
195 0 @connect_params = (
196 addr => {
197 family => 'unix',
198 socktype => 'stream',
199 path => '/var/run/postgresql/.s.PGSQL.'.$uri->port,
200 }
201 );
202 } else {
203 0 @connect_params = (
204 addr => {
205 family => 'unix',
206 socktype => 'stream',
207 path => $uri->host.'/.s.PGSQL.'.$uri->port,
208 }
209 );
210 }
211 0 my $sock = await $loop->connect(@connect_params);
212  
213 0 ▶ if ($sock->sockdomain == Socket::PF_INET or $sock->sockdomain == Socket::PF_INET6) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sock->sockdomain == 2 or $sock->sockdomain == 10) { } 0 0 0
elsif ($sock->sockdomain == 1) { } 0 0 0
Condition0%0/3
$sock->sockdomain == 2 or $sock->sockdomain == 10
l!l&&r!l&&!r
000
MC/DC0%0/2
$sock->sockdomain == 2 or $sock->sockdomain == 10
$sock->sockdomain == 2 $sock->sockdomain == 10
Truth table0%0/3
A or B
A= $sock->sockdomain == 2B= $sock->sockdomain == 10
ABresult
1X1
011
000
214 0 my $local = join ':', $sock->sockhost_service(1);
215 0 my $remote = join ':', $sock->peerhost_service(1);
216 0 $log->tracef('Connected to %s as %s from %s', $endpoint, $remote, $local);
217 } elsif ($sock->sockdomain == Socket::PF_UNIX) {
218 0 $log->tracef('Connected to %s as %s', $endpoint, $sock->peerpath);
219 }
220  
221 # We start with a null handler for read, because our behaviour varies depending on
222 # whether we want to go through the SSL dance or not.
223 $self->add_child(
224 my $stream = IO::Async::Stream->new(
225 handle => $sock,
226 0 ▶ on_read => sub { 0 }
Subroutine __ANON__: not called CC=1 SCAR=6.9
227 )
228 0 );
229  
230 # SSL is conveniently simple: a prefix exchange before the real session starts,
231 # and the user can decide whether SSL is mandatory or optional.
232 0 $stream = await $self->negotiate_ssl(
233 stream => $stream,
234 );
235  
236 0 Scalar::Util::weaken($self->{stream} = $stream);
237 0 ▶ $self->outgoing->each(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
238 0 $log->tracef('Write bytes [%v02x]', $_);
239 0 $self->ready_for_query->set_string('');
240 0 $self->stream->write("$_");
241 0 return;
242 0 });
243 0 $stream->configure(
244 on_read => $self->curry::weak::on_read,
245 read_len => $self->read_len,
246 write_len => $self->write_len,
247 autoflush => 0,
248 );
249  
250 0 $log->tracef('Send initial request with user %s', $uri->user);
251  
252 # This is where the extensible options for initial connection are applied:
253 # we have already handled SSL by this point, so we exclude this from the
254 # list and pass everything else directly to the startup packet.
255 0 my %qp = $uri->query_params;
256 0 delete $qp{sslmode};
257  
258 0 ▶ $qp{application_name} //= $self->application_name;
Condition0%0/3
$qp{"application_name"} //= $self->application_name
l!l&&r!l&&!r
000
MC/DC0%0/2
$qp{"application_name"} //= $self->application_name
$qp{"application_name"} $self->application_name
Truth table0%0/3
A //= B
A= $qp{"application_name"}B= $self->application_name
ABresult
1X1
011
000
259 0 $self->protocol->send_startup_request(
260 database => $self->database_name,
261 user => $self->database_user,
262 %qp
263 );
264 0 $connected->set_numeric(1);
265 0 return $stream;
266 7 }
267  
268 =head2 service_conf_path
269  
270 Return the expected location for the pg_service.conf file.
271  
272 =cut
273  
274 8 ▶sub service_conf_path {
Subroutine service_conf_path: called CC=4 SCAR=13.9
Pod documented
275 8 my ($class) = @_;
276 8 ▶ return $ENV{PGSERVICEFILE} if exists $ENV{PGSERVICEFILE};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"PGSERVICEFILE"} 4 4 8
277 4 ▶ return $ENV{PGSYSCONFDIR} . '/pg_service.conf' if exists $ENV{PGSYSCONFDIR};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"PGSYSCONFDIR"} 2 2 4
278 2 my $path = File::HomeDir->my_home . '/.pg_service.conf';
279 2 ▶ return $path if -r $path;
Branch: 1 branch
BranchTrueFalseTotal
if -r $path 1 1 2
280 1 return '/etc/pg_service.conf';
281 }
282  
283 3 ▶sub service_parse {
Subroutine service_parse: called CC=1 SCAR=0.0
Pod undocumented
284 3 my ($class, $path) = @_;
285 3 return Config::Tiny->read($path, 'encoding(UTF-8)');
286 }
287  
288 3 ▶sub find_service {
Subroutine find_service: called CC=2 SCAR=6.9
Pod undocumented
289 3 my ($class, $srv) = @_;
290 3 my $data = $class->service_parse(
291 $class->service_conf_path
292 );
293 3 ▶ die 'service ' . $srv . ' not found in config' unless $data->{$srv};
Branch: 1 branch
BranchTrueFalseTotal
unless $data->{$srv} 1 2 3
294 2 return $data->{$srv};
295 }
296  
297 0 ▶sub service { shift->{service} //= $ENV{PGSERVICE} }
Subroutine service: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/3
+(shift())->{"service"} //= $ENV{"PGSERVICE"}
l!l&&r!l&&!r
000
MC/DC0%0/2
+(shift())->{"service"} //= $ENV{"PGSERVICE"}
+(shift())->{"service"} $ENV{"PGSERVICE"}
Truth table0%0/3
A //= B
A= +(shift())->{"service"}B= $ENV{"PGSERVICE"}
ABresult
1X1
011
000
298  
299 8 ▶sub database_name {
Subroutine database_name: called CC=3 SCAR=13.1
Pod undocumented
300 8 my $uri = shift->uri;
301 8 ▶ return $uri->dbname // $uri->user // 'postgres'
Condition40%2/5
($uri->dbname // $uri->user) // "postgres"
l!l
80
$uri->dbname // $uri->user
l!l&&r!l&&!r
800
MC/DC0%0/2
($uri->dbname // $uri->user) // "postgres"
$uri->dbname $uri->user
Truth table33%1/3
A // B
A= $uri->dbnameB= $uri->user
ABresult
1X1
011
000
302 }
303  
304 8 ▶sub database_user {
Subroutine database_user: called CC=2 SCAR=7.2
Pod undocumented
305 8 my $uri = shift->uri;
306 8 ▶ return $uri->user // 'postgres'
Condition50%1/2
$uri->user // "postgres"
l!l
80
MC/DC0%0/1
$uri->user // "postgres"
$uri->user
Truth table50%1/2
A
A= $uri->user
Aresult
11
00
307 }
308  
309 18 ▶sub password_from_file {
Subroutine password_from_file: called CC=19 SCAR=35.6
Pod undocumented
310 18 my $self = shift;
311 18 ▶ my $pwfile = $ENV{PGPASSFILE} || File::HomeDir->my_home . '/.pgpass';
Condition100%2/2
$ENV{'PGPASSFILE'} || 'File::HomeDir'->my_home . '/.pgpass'
l!l
216
MC/DC100%1/1
$ENV{'PGPASSFILE'} || 'File::HomeDir'->my_home . '/.pgpass'
$ENV{'PGPASSFILE'}
Truth table100%2/2
A
A= $ENV{'PGPASSFILE'}
Aresult
11
00
312  
313 18 ▶ unless ($^O eq 'MSWin32') { # same as libpq
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O eq "MSWin32") 18 0 18
314 # libpq also does stat here instead of lstat. So, pgpass can be
315 # a logical link.
316 18 ▶ my (undef, undef, $mode) = stat $pwfile or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my(undef, undef, $mode) = stat $pwfile or return undef 18 0 18
317 18 ▶ unless (-f _) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f _) 0 18 18
318 0 $log->warnf("WARNING: password file \"%s\" is not a plain file\n", $pwfile);
319 0 return undef;
320 }
321  
322 18 ▶ if ($mode & 077) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode & 63) 1 17 18
323 1 $log->warnf("WARNING: password file \"%s\" has group or world access; permissions should be u=rw (0600) or less", $pwfile);
324 1 return undef;
325 }
326 # libpq has the same race condition of stat versus open.
327 }
328  
329 # It's not an error for this file to be missing: it might not
330 # be readable for various reasons, but for now we ignore that case as well
331 # (we've already checked for overly-lax permissions above)
332 17 ▶ open my $fh, '<', $pwfile or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $pwfile or return undef 17 0 17
333  
334 17 ▶ while (defined(my $line = readline $fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 17 0 17
335 17 ▶ next if $line =~ '^#';
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 0 17 17
336 17 chomp $line;
337 17 ▶ my ($host, $port, $db, $user, $pw) = ($line =~ /((?:\\.|[^:])*)(?::|$)/g)
Branch: 1 branch
BranchTrueFalseTotal
() = $line =~ /((?:\\.|[^:])*)(?::|$)/g or next 17 0 17
338 or next;
339 17 s/\\(.)/$1/g for ($host, $port, $db, $user, $pw);
340  
341 17 ▶ return $pw if (
Branch: 1 branch
BranchTrueFalseTotal
if $host eq "*" || $host eq $self->uri->host and $port eq "*" || $port eq $self->uri->port and $user eq "*" || $user eq $self->database_user and $db eq "*" || $db eq $self->database_name 17 0 17
Condition52%11/21
$host eq "*" || $host eq $self->uri->host and $port eq "*" || $port eq $self->uri->port and $user eq "*" || $user eq $self->database_user and $db eq "*" || $db eq $self->database_name
!ll&&!rl&&r
0017
$host eq "*" || $host eq $self->uri->host and $port eq "*" || $port eq $self->uri->port and $user eq "*" || $user eq $self->database_user
!ll&&!rl&&r
0017
$host eq "*" || $host eq $self->uri->host and $port eq "*" || $port eq $self->uri->port
!ll&&!rl&&r
0017
$host eq "*" or $host eq $self->uri->host
l!l&&r!l&&!r
980
$port eq "*" or $port eq $self->uri->port
l!l&&r!l&&!r
980
$user eq "*" or $user eq $self->database_user
l!l&&r!l&&!r
980
$db eq "*" or $db eq $self->database_name
l!l&&r!l&&!r
980
MC/DC0%0/8
$host eq "*" || $host eq $self->uri->host and $port eq "*" || $port eq $self->uri->port and $user eq "*" || $user eq $self->database_user and $db eq "*" || $db eq $self->database_name
$host eq "*" $host eq $self->uri->host $port eq "*" $port eq $self->uri->port $user eq "*" $user eq $self->database_user $db eq "*" $db eq $self->database_name
Truth table51%16/31
A or B and C or D and E or F and G or H
A= $host eq "*"B= $host eq $self->uri->hostC= $port eq "*"D= $port eq $self->uri->portE= $user eq "*"F= $user eq $self->database_userG= $db eq "*"H= $db eq $self->database_name
ABCDEFGHresult
00XXXXXX0
1X00XXXX0
0100XXXX0
1X1X00XX0
1X0100XX0
011X00XX0
010100XX0
1X1X1X000
1X1X01000
1X011X000
1X0101000
011X1X000
011X01000
01011X000
010101000
1X1X1X1X1
1X1X1X011
1X1X011X1
1X1X01011
1X011X1X1
1X011X011
1X01011X1
1X0101011
011X1X1X1
011X1X011
011X011X1
011X01011
01011X1X1
01011X011
0101011X1
010101011
342 $host eq '*' || $host eq $self->uri->host and
343 $port eq '*' || $port eq $self->uri->port and
344 $user eq '*' || $user eq $self->database_user and
345 $db eq '*' || $db eq $self->database_name
346 );
347 }
348  
349 0 return undef;
350 }
351  
352 20 ▶sub database_password {
Subroutine database_password: called CC=3 SCAR=11.4
Pod undocumented
353 20 my $self = shift;
354 20 ▶ return $self->uri->password // $ENV{PGPASSWORD} || $self->password_from_file
Condition66%4/6
($self->uri->password // $ENV{'PGPASSWORD'}) || $self->password_from_file
l!l&&r!l&&!r
2180
$self->uri->password // $ENV{"PGPASSWORD"}
l!l&&r!l&&!r
1190
MC/DC0%0/3
($self->uri->password // $ENV{'PGPASSWORD'}) || $self->password_from_file
$self->uri->password $ENV{"PGPASSWORD"} $self->password_from_file
Truth table50%2/4
A // B || C
A= $self->uri->passwordB= $ENV{"PGPASSWORD"}C= $self->password_from_file
ABCresult
1XX1
01X1
0011
0000
355 }
356  
357 =head2 negotiate_ssl
358  
359 Apply SSL negotiation.
360  
361 =cut
362  
363 0 ▶async sub negotiate_ssl {
Subroutine negotiate_ssl: not called CC=7 SCAR=40.3
Pod documented
364 0 my ($self, %args) = @_;
365 0 my $stream = delete $args{stream};
366  
367 # If SSL is disabled entirely, just return the same stream as-is
368 0 ▶ my $ssl = $self->ssl
Branch: 1 branch
BranchTrueFalseTotal
my $ssl = $self->ssl or return $stream 0 0 0
369 or return $stream;
370  
371 0 require IO::Async::SSL;
372 0 require IO::Socket::SSL;
373  
374 0 $log->tracef('Attempting to negotiate SSL');
375 0 await $stream->write($self->protocol->ssl_request);
376  
377 0 $log->tracef('Waiting for response');
378 0 my ($resp, $eof) = await $stream->read_exactly(1);
379  
380 0 ▶ $log->tracef('Read %v02x from server for SSL response (EOF is %s)', $resp, $eof ? 'true' : 'false');
Branch: 1 branch
BranchTrueFalseTotal
$eof ? : 0 0 0
381 0 ▶ die 'Server closed connection' if $eof;
Branch: 1 branch
BranchTrueFalseTotal
if $eof 0 0 0
382  
383 0 ▶ if($resp eq 'S') {
Branch: 2 branches
BranchTrueFalseTotal
if ($resp eq 'S') { } 0 0 0
elsif ($resp eq 'N') { } 0 0 0
384 # S for SSL...
385 0 $log->tracef('This is SSL, let us upgrade');
386 $stream = await $self->loop->SSL_upgrade(
387 handle => $stream,
388 # SSL defaults...
389 SSL_server => 0,
390 SSL_hostname => $self->uri->host,
391 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
392 # Pass through anything SSL-related unchanged, the user knows
393 # better than we do
394 0 (map {; $_ => $self->{$_} } grep { /^SSL_/ } keys %$self)
395 );
396 0 $log->tracef('Upgrade complete');
397 } elsif($resp eq 'N') {
398 # N for "no SSL"...
399 0 $log->tracef('No to SSL');
400 0 ▶ die 'Server does not support SSL' if $self->ssl == SSL_REQUIRE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->ssl == 2 0 0 0
401 } else {
402 # anything else is unexpected
403 0 die 'Unknown response to SSL request';
404 }
405 0 return $stream;
406 7 }
407  
408 0 ▶sub is_replication { shift->{is_replication} //= 0 }
Subroutine is_replication: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
+(shift())->{"is_replication"} //= 0
l!l
00
MC/DC0%0/1
+(shift())->{"is_replication"} //= 0
+(shift())->{"is_replication"}
Truth table0%0/2
A
A= +(shift())->{"is_replication"}
Aresult
11
00
409 0 ▶sub application_name { shift->{application_name} //= 'perl' }
Subroutine application_name: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
+(shift())->{"application_name"} //= "perl"
l!l
00
MC/DC0%0/1
+(shift())->{"application_name"} //= "perl"
+(shift())->{"application_name"}
Truth table0%0/2
A
A= +(shift())->{"application_name"}
Aresult
11
00
410  
411 =head2 uri_for_dsn
412  
413 Returns a L<URI> corresponding to the given L<database source name|https://en.wikipedia.org/wiki/Data_source_name>.
414  
415 May throw an exception if we don't have a valid string.
416  
417 =cut
418  
419 0 ▶sub uri_for_dsn {
Subroutine uri_for_dsn: not called CC=3 SCAR=24.8
Pod documented
420 0 my ($class, $dsn) = @_;
421 0 ▶ die 'invalid DSN, expecting DBI:Pg:...' unless $dsn =~ s/^DBI:Pg://i;
Branch: 1 branch
BranchTrueFalseTotal
unless $dsn =~ s/^DBI:Pg://i 0 0 0
422 0 my %args = split /[=;]/, $dsn;
423 0 my $uri = URI->new('postgresql://postgres@localhost/postgres');
424 0 $uri->$_(delete $args{$_}) for grep exists $args{$_}, qw(host port user password dbname);
425 0 $uri
426 }
427  
428 0 ▶sub uri_for_service {
Subroutine uri_for_service: not called CC=4 SCAR=30.0
Pod undocumented
429 0 my ($class, $service) = @_;
430 0 my $cfg = $class->find_service($service);
431  
432 # Start with common default values (i.e. follow libpq behaviour unless there's a strong reason not to)
433 0 my $uri = URI->new('postgresql://postgres@localhost/postgres');
434  
435 # Standard fields supported by URI::pg
436 0 $uri->$_(delete $cfg->{$_}) for grep exists $cfg->{$_}, qw(host port user password dbname);
437 # ... note that `hostaddr` takes precedence over plain `host`
438 0 ▶ $uri->host(delete $cfg->{hostaddr}) if exists $cfg->{hostaddr};
Branch: 1 branch
BranchTrueFalseTotal
if exists $cfg->{"hostaddr"} 0 0 0
439  
440 # Everything else is handled via query parameters, this list is non-exhaustive and likely to be
441 # extended in future (e.g. text/binary protocol mode)
442 0 $uri->query_param($_ => delete $cfg->{$_}) for grep exists $cfg->{$_}, qw(
443 application_name
444 fallback_application_name
445 keepalives
446 options
447 sslmode
448 replication
449 );
450 0 $uri
451 }
452  
453 =head2 stream
454  
455 The L<IO::Async::Stream> representing the database connection.
456  
457 =cut
458  
459 0 ▶sub stream { shift->{stream} }
Subroutine stream: not called CC=1 SCAR=6.9
Pod documented
460  
461 =head2 on_read
462  
463 Process incoming database packets.
464  
465 Expects the following parameters:
466  
467 =over 4
468  
469 =item * C<$stream> - the L<IO::Async::Stream> we are receiving data on
470  
471 =item * C<$buffref> - a scalar reference to the current input data buffer
472  
473 =item * C<$eof> - true if we have reached the end of input
474  
475 =back
476  
477 =cut
478  
479 0 ▶sub on_read {
Subroutine on_read: not called CC=2 SCAR=17.9
Pod documented
480 0 my ($self, $stream, $buffref, $eof) = @_;
481  
482 try {
483 0 $log->tracef('Have server message of length %d', length $$buffref);
484 0 ▶ while(my $msg = $self->protocol->extract_message($buffref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $self->protocol->extract_message($buffref)) 0 0 0
485 0 $log->tracef('Message: %s', $msg);
486 0 $self->incoming->emit($msg);
487 }
488 } catch($e) {
489 # This really shouldn't happen, but since we can't trust our current state we should drop
490 # the connection ASAP, and avoid any chance of barrelling through to a COMMIT or other
491 # risky operation.
492 0 $log->errorf('Failed to handle read, connection is no longer in a valid state: %s', $e);
493 0 $stream->close_now;
494 } finally {
495 $self->connected->set_numeric(0) if $eof;
496 }
497 0 return 0;
498 }
499  
500 =head2 ryu
501  
502 Provides a L<Ryu::Async> instance.
503  
504 =cut
505  
506 0 ▶sub ryu {
Subroutine ryu: not called CC=2 SCAR=17.9
Pod documented
507 0 my ($self) = @_;
508 0 ▶ $self->{ryu} //= do {
Condition0%0/3
$self->{"ryu"} //= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"ryu"} //= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
$self->{"ryu"} do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
Truth table0%0/3
A //= B
A= $self->{"ryu"}B= do { $self->add_child(my $ryu = "Ryu::Async"->new); $ryu }
ABresult
1X1
011
000
509 0 $self->add_child(
510 my $ryu = Ryu::Async->new
511 );
512 0 $ryu
513 }
514 }
515  
516 =head2 outgoing
517  
518 L<Ryu::Source> representing outgoing packets for the current database connection.
519  
520 =cut
521  
522 0 ▶sub outgoing {
Subroutine outgoing: not called CC=2 SCAR=17.9
Pod documented
523 0 my ($self) = @_;
524 0 ▶ $self->{outgoing} //= $self->ryu->source;
Condition0%0/3
$self->{"outgoing"} //= $self->ryu->source
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"outgoing"} //= $self->ryu->source
$self->{"outgoing"} $self->ryu->source
Truth table0%0/3
A //= B
A= $self->{"outgoing"}B= $self->ryu->source
ABresult
1X1
011
000
525 }
526  
527 =head2 incoming
528  
529 L<Ryu::Source> representing incoming packets for the current database connection.
530  
531 =cut
532  
533 0 ▶sub incoming {
Subroutine incoming: not called CC=2 SCAR=17.9
Pod documented
534 0 my ($self) = @_;
535 0 ▶ $self->{incoming} //= $self->ryu->source;
Condition0%0/3
$self->{"incoming"} //= $self->ryu->source
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"incoming"} //= $self->ryu->source
$self->{"incoming"} $self->ryu->source
Truth table0%0/3
A //= B
A= $self->{"incoming"}B= $self->ryu->source
ABresult
1X1
011
000
536 }
537  
538 =head2 connected
539  
540 A L<Ryu::Observable> which will be 1 while the connection is in a valid state,
541 and 0 if we're disconnected.
542  
543 =cut
544  
545 0 ▶sub connected {
Subroutine connected: not called CC=2 SCAR=17.9
Pod documented
546 0 my ($self) = @_;
547 0 ▶ $self->{connected} //= do {
Condition0%0/3
$self->{"connected"} //= do { my $obs = "Ryu::Observable"->new(0); $obs->subscribe($self->$curry::weak(sub { my($self, $v) = @_; return if $v; if (my $query = delete $self->{"active_query"}) { $query->completed->fail("disconnected") unless $query->completed->is_ready; } ; if (my $db = $self->db) { $db->engine_disconnected($self); } ; } )); $obs }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"connected"} //= do { my $obs = "Ryu::Observable"->new(0); $obs->subscribe($self->$curry::weak(sub { my($self, $v) = @_; return if $v; if (my $query = delete $self->{"active_query"}) { $query->completed->fail("disconnected") unless $query->completed->is_ready; } ; if (my $db = $self->db) { $db->engine_disconnected($self); } ; } )); $obs }
$self->{"connected"} do { my $obs = "Ryu::Observable"->new(0); $obs->subscribe($self->$curry::weak(sub { my($self, $v) = @_; return if $v; if (my $query = delete $self->{"active_query"}) { $query->completed->fail("disconnected") unless $query->completed->is_ready; } ; if (my $db = $self->db) { $db->engine_disconnected($self); } ; } )); $obs }
Truth table0%0/3
A //= B
A= $self->{"connected"}B= do { my $obs = "Ryu::Observable"->new(0); $obs->subscribe($self->$curry::weak(sub { my($self, $v) = @_; return if $v; if (my $query = delete $self->{"active_query"}) { $query->completed->fail("disconnected") unless $query->completed->is_ready; } ; if (my $db = $self->db) { $db->engine_disconnected($self); } ; } )); $obs }
ABresult
1X1
011
000
548 0 my $obs = Ryu::Observable->new(0);
549 $obs->subscribe(
550 0 ▶ $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
551 0 my ($self, $v) = @_;
552 # We only care about disconnection events
553 0 ▶ return if $v;
Branch: 1 branch
BranchTrueFalseTotal
if $v 0 0 0
554  
555 # If we were doing something, then it didn't work
556 0 ▶ if(my $query = delete $self->{active_query}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $query = delete $self->{"active_query"}) 0 0 0
557 0 ▶ $query->completed->fail('disconnected') unless $query->completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $query->completed->is_ready 0 0 0
558 }
559  
560 # Tell the database pool management that we're no longer useful
561 0 ▶ if(my $db = $self->db) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $db = $self->db) 0 0 0
562 0 $db->engine_disconnected($self);
563 }
564 })
565 0 );
566 0 $obs
567 };
568 }
569  
570 =head2 authenticated
571  
572 Resolves once database authentication is complete.
573  
574 =cut
575  
576 0 ▶sub authenticated {
Subroutine authenticated: not called CC=2 SCAR=17.9
Pod documented
577 0 my ($self) = @_;
578 0 ▶ $self->{authenticated} //= $self->loop->new_future;
Condition0%0/3
$self->{"authenticated"} //= $self->loop->new_future
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"authenticated"} //= $self->loop->new_future
$self->{"authenticated"} $self->loop->new_future
Truth table0%0/3
A //= B
A= $self->{"authenticated"}B= $self->loop->new_future
ABresult
1X1
011
000
579 }
580  
581 # Handlers for authentication messages from backend.
582 our %AUTH_HANDLER = (
583 0 ▶ AuthenticationOk => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
584 0 my ($self, $msg) = @_;
585 0 $self->authenticated->done;
586 },
587 0 ▶ AuthenticationKerberosV5 => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
588 0 my ($self, $msg) = @_;
589 0 die "Not yet implemented";
590 },
591 0 ▶ AuthenticationCleartextPassword => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
592 0 my ($self, $msg) = @_;
593 0 $self->protocol->send_message(
594 'PasswordMessage',
595 user => $self->encode_text($self->uri->user),
596 password_type => 'plain',
597 password => $self->encode_text($self->database_password),
598 );
599 },
600 0 ▶ AuthenticationMD5Password => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
601 0 my ($self, $msg) = @_;
602 0 $self->protocol->send_message(
603 'PasswordMessage',
604 user => $self->encode_text($self->uri->user),
605 password_type => 'md5',
606 password_salt => $msg->password_salt,
607 password => $self->encode_text($self->database_password),
608 );
609 },
610 0 ▶ AuthenticationSCMCredential => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
611 0 my ($self, $msg) = @_;
612 0 die "Not yet implemented";
613 },
614 0 ▶ AuthenticationGSS => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
615 0 my ($self, $msg) = @_;
616 0 die "Not yet implemented";
617 },
618 0 ▶ AuthenticationSSPI => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
619 0 my ($self, $msg) = @_;
620 0 die "Not yet implemented";
621 },
622 0 ▶ AuthenticationGSSContinue => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
623 0 my ($self, $msg) = @_;
624 0 die "Not yet implemented";
625 },
626 0 ▶ AuthenticationSASL => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
627 0 my ($self, $msg) = @_;
628 0 $log->tracef('SASL starts');
629 0 my $nonce = MIME::Base64::encode_base64(Bytes::Random::Secure::random_string_from(join('', ('a'..'z'), ('A'..'Z'), ('0'..'9')), 18), '');
630 0 $self->{client_first_message} = 'n,,n=,r=' . $nonce;
631 0 $self->protocol->send_message(
632 'SASLInitialResponse',
633 mechanism => 'SCRAM-SHA-256',
634 nonce => $nonce,
635 );
636 },
637 0 ▶ AuthenticationSASLContinue => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
638 0 my ($self, $msg) = @_;
639 0 $log->tracef('Have msg %s', $msg);
640  
641 0 ▶ my $rounds = $msg->password_rounds or die 'need iteration count';
Branch: 1 branch
BranchTrueFalseTotal
my $rounds = $msg->password_rounds or die "need iteration count" 0 0 0
642  
643 0 my $server_first_message = $msg->server_first_message;
644 0 my $pass = Unicode::UTF8::encode_utf8($self->database_password);
645 0 my $salted_password = do {
646 0 my $hash = Crypt::Mac::HMAC::hmac('SHA256', $pass, MIME::Base64::decode_base64($msg->password_salt), pack('N1', 1));
647 0 my $out = $hash;
648 # Skip the first round - that's our original $hash value - and recursively re-hash
649 # for the remainder, incrementally building our bitwise XOR result
650 0 for my $idx (1..$rounds-1) {
651 0 $hash = Crypt::Mac::HMAC::hmac('SHA256', $pass, $hash);
652 0 $out = "$out" ^ "$hash";
653 }
654 $out
655 0 };
656 # The client key uses the literal string 'Client Key' as the base - for the server, it'd be 'Server Key'
657 0 my $client_key = Crypt::Mac::HMAC::hmac('SHA256', $salted_password, "Client Key");
658 0 my $server_key = Crypt::Mac::HMAC::hmac('SHA256', $salted_password, "Server Key");
659 # Then we hash this to get the stored key, which will be used for the signature
660 0 my $stored_key = Crypt::Digest::SHA256::sha256($client_key);
661  
662 0 my $client_first_message = $self->{client_first_message};
663 # Strip out the channel-binding GS2 header
664 0 my $header = 'n,,';
665 0 $client_first_message =~ s{^\Q$header}{};
666 # ... but we _do_ want the header in the final-message c= GS2 component
667 0 my $client_final_message = 'c=' . MIME::Base64::encode_base64($header, '') . ',r=' . $msg->password_nonce;
668  
669 # this is what we want to sign!
670 0 my $auth_message = join ',', $client_first_message, $server_first_message, $client_final_message;
671 0 $log->tracef('Auth message = %s', $auth_message);
672  
673 0 my $client_signature = Crypt::Mac::HMAC::hmac('SHA256', $stored_key, $auth_message);
674 0 my $client_proof = "$client_key" ^ "$client_signature";
675 0 $log->tracef('Client proof is %s', $client_proof);
676 0 my $server_signature = Crypt::Mac::HMAC::hmac('SHA256', $server_key, $auth_message);
677 0 $self->{expected_server_signature} = $server_signature;
678 0 $self->protocol->send_message(
679 'SASLResponse',
680 header => $header,
681 nonce => $msg->password_nonce,
682 proof => $client_proof,
683 );
684 },
685 0 ▶ AuthenticationSASLFinal => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
686 0 my ($self, $msg) = @_;
687 0 my $expected = MIME::Base64::encode_base64($self->{expected_server_signature}, '');
688 0 ▶ die 'invalid server signature ' . $msg->server_signature . ', expected ' . $expected unless $msg->server_signature eq $expected;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->server_signature eq $expected 0 0 0
689 0 $log->tracef('Server signature seems fine, continue with auth');
690 # No further action required, we'll get an AuthenticationOk immediately after this
691 }
692 7 );
693  
694 =head2 protocol
695  
696 Returns the L<Protocol::Database::PostgreSQL> instance, creating it
697 and setting up event handlers if necessary.
698  
699 =cut
700  
701 0 ▶sub protocol {
Subroutine protocol: not called CC=2 SCAR=17.9
Pod documented
702 0 my ($self) = @_;
703 0 ▶ $self->{protocol} //= do {
Condition0%0/3
$self->{"protocol"} //= do { my $pg = "Protocol::Database::PostgreSQL::Client"->new("database", $self->database_name, "outgoing", $self->outgoing); $self->incoming->switch_str(sub { $_->type; } , "authentication_request", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Auth request received: %s", $msg); my $code = $AUTH_HANDLER{$msg->auth_type} or $Database::Async::Engine::PostgreSQL::log->errorf("unknown auth type %s", $msg->auth_type); $self->$code($msg); } ), "password", $self->$curry::weak(sub { my($self, %args) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Auth request received: %s", \%args); $self->protocol->{"user"} = $self->uri->user; $self->protocol->send_message("PasswordMessage", "password", $self->encode_text($self->database_password)); } ), "parameter_status", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Parameter received: %s", $msg); $self->set_parameter(map($self->decode_text($_), $msg->key, $msg->value)); } ), "row_description", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Row description %s", $msg); $Database::Async::Engine::PostgreSQL::log->errorf("No active query?") unless my $q = $self->active_query; $q->row_description($msg->description); } ), "data_row", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Have row data %s", $msg); $self->{"fc"} ||= do { my $src = $self->active_query->row_data; $self->stream->want_readready($src->is_paused ? 0 : 1) if $self->stream; $src->flow_control->each($self->$curry::weak(sub { my($self) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Flow control event - will %s stream", $_ ? "resume" : "pause"); $self->stream->want_readready($_) if $self->stream; } )) }; $self->active_query->row([map($self->decode_text($_), $msg->fields)]); } ), "command_complete", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; my $query = delete $self->{"active_query"} or do { $Database::Async::Engine::PostgreSQL::log->warnf("Command complete but no query"); return }; $Database::Async::Engine::PostgreSQL::log->tracef("Completed query %s with result %s", $query, $msg->result); $query->done unless $query->completed->is_ready; } ), "no_data", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Completed query %s with no data", $self->active_query); } ), "send_request", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Send request for %s", $msg); $self->stream->write($msg); } ), "ready_for_query", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Ready for query, state is %s", $msg->state); $self->ready_for_query->set_string($msg->state); $self->db->engine_ready($self) if $self->db; } ), "backend_key_data", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Backend key data: pid %d, key 0x%08x", $msg->pid, $msg->key); } ), "parse_complete", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Parsing complete for query %s", $self->active_query); } ), "bind_complete", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Bind complete for query %s", $self->active_query); } ), "close_complete", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Close complete for query %s", $self->active_query); } ), "empty_query_response", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Query returned no results for %s", $self->active_query); } ), "error_response", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; if (my $query = delete $self->{"active_query"}) { $Database::Async::Engine::PostgreSQL::log->warnf("Query returned error %s for %s", $msg->error, $self->active_query); my $f = $query->completed; $f->fail($msg->error) unless $f->is_ready; } else { $Database::Async::Engine::PostgreSQL::log->errorf("Received error %s with no active query", $msg->error); } ; } ), "copy_in_response", $self->$curry::weak(sub { my($self, $msg) = @_; my $query = $self->active_query; $Database::Async::Engine::PostgreSQL::log->tracef("Ready to copy data for %s", $query); my $proto = $self->protocol; { my $src = $query->streaming_input; $src->completed->on_ready(sub { my($f) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Sending copy done notification, stream status was %s", $f->state); $proto->send_message("CopyDone", "data", ""); $proto->send_message("Close", "portal", "", "statement", ""); $proto->send_message("Sync", "portal", "", "statement", ""); } ); $src->each(sub { $Database::Async::Engine::PostgreSQL::log->tracef("Sending %s", $_); $proto->send_copy_data($_); } ); } ; $query->ready_to_stream->done unless $query->ready_to_stream->is_ready; } ), "copy_out_response", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("copy out starts %s", $msg); } ), "copy_data", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Have copy data %s", $msg); my $query = $self->active_query or do { $Database::Async::Engine::PostgreSQL::log->warnf("No active query for copy data"); return }; $query->row([map($self->decode_text($_), @$_)]) foreach ($msg->rows); } ), "copy_done", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Copy done - %s", $msg); } ), "notification_response", $self->$curry::weak(sub { my($self, $msg) = @_; my($chan, $data) = @{$msg;}{"channel", "data"}; $Database::Async::Engine::PostgreSQL::log->tracef("Notification on channel %s containing %s", $chan, $data); $self->db->notification($self, map($self->decode_text($_), $chan, $data)); } ), sub { $Database::Async::Engine::PostgreSQL::log->errorf("Unknown message %s (type %s)", $_, $_->type); } ); $pg }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"protocol"} //= do { my $pg = "Protocol::Database::PostgreSQL::Client"->new("database", $self->database_name, "outgoing", $self->outgoing); $self->incoming->switch_str(sub { $_->type; } , "authentication_request", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Auth request received: %s", $msg); my $code = $AUTH_HANDLER{$msg->auth_type} or $Database::Async::Engine::PostgreSQL::log->errorf("unknown auth type %s", $msg->auth_type); $self->$code($msg); } ), "password", $self->$curry::weak(sub { my($self, %args) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Auth request received: %s", \%args); $self->protocol->{"user"} = $self->uri->user; $self->protocol->send_message("PasswordMessage", "password", $self->encode_text($self->database_password)); } ), "parameter_status", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Parameter received: %s", $msg); $self->set_parameter(map($self->decode_text($_), $msg->key, $msg->value)); } ), "row_description", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Row description %s", $msg); $Database::Async::Engine::PostgreSQL::log->errorf("No active query?") unless my $q = $self->active_query; $q->row_description($msg->description); } ), "data_row", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Have row data %s", $msg); $self->{"fc"} ||= do { my $src = $self->active_query->row_data; $self->stream->want_readready($src->is_paused ? 0 : 1) if $self->stream; $src->flow_control->each($self->$curry::weak(sub { my($self) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Flow control event - will %s stream", $_ ? "resume" : "pause"); $self->stream->want_readready($_) if $self->stream; } )) }; $self->active_query->row([map($self->decode_text($_), $msg->fields)]); } ), "command_complete", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; my $query = delete $self->{"active_query"} or do { $Database::Async::Engine::PostgreSQL::log->warnf("Command complete but no query"); return }; $Database::Async::Engine::PostgreSQL::log->tracef("Completed query %s with result %s", $query, $msg->result); $query->done unless $query->completed->is_ready; } ), "no_data", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Completed query %s with no data", $self->active_query); } ), "send_request", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Send request for %s", $msg); $self->stream->write($msg); } ), "ready_for_query", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Ready for query, state is %s", $msg->state); $self->ready_for_query->set_string($msg->state); $self->db->engine_ready($self) if $self->db; } ), "backend_key_data", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Backend key data: pid %d, key 0x%08x", $msg->pid, $msg->key); } ), "parse_complete", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Parsing complete for query %s", $self->active_query); } ), "bind_complete", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Bind complete for query %s", $self->active_query); } ), "close_complete", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Close complete for query %s", $self->active_query); } ), "empty_query_response", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Query returned no results for %s", $self->active_query); } ), "error_response", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; if (my $query = delete $self->{"active_query"}) { $Database::Async::Engine::PostgreSQL::log->warnf("Query returned error %s for %s", $msg->error, $self->active_query); my $f = $query->completed; $f->fail($msg->error) unless $f->is_ready; } else { $Database::Async::Engine::PostgreSQL::log->errorf("Received error %s with no active query", $msg->error); } ; } ), "copy_in_response", $self->$curry::weak(sub { my($self, $msg) = @_; my $query = $self->active_query; $Database::Async::Engine::PostgreSQL::log->tracef("Ready to copy data for %s", $query); my $proto = $self->protocol; { my $src = $query->streaming_input; $src->completed->on_ready(sub { my($f) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Sending copy done notification, stream status was %s", $f->state); $proto->send_message("CopyDone", "data", ""); $proto->send_message("Close", "portal", "", "statement", ""); $proto->send_message("Sync", "portal", "", "statement", ""); } ); $src->each(sub { $Database::Async::Engine::PostgreSQL::log->tracef("Sending %s", $_); $proto->send_copy_data($_); } ); } ; $query->ready_to_stream->done unless $query->ready_to_stream->is_ready; } ), "copy_out_response", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("copy out starts %s", $msg); } ), "copy_data", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Have copy data %s", $msg); my $query = $self->active_query or do { $Database::Async::Engine::PostgreSQL::log->warnf("No active query for copy data"); return }; $query->row([map($self->decode_text($_), @$_)]) foreach ($msg->rows); } ), "copy_done", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Copy done - %s", $msg); } ), "notification_response", $self->$curry::weak(sub { my($self, $msg) = @_; my($chan, $data) = @{$msg;}{"channel", "data"}; $Database::Async::Engine::PostgreSQL::log->tracef("Notification on channel %s containing %s", $chan, $data); $self->db->notification($self, map($self->decode_text($_), $chan, $data)); } ), sub { $Database::Async::Engine::PostgreSQL::log->errorf("Unknown message %s (type %s)", $_, $_->type); } ); $pg }
$self->{"protocol"} do { my $pg = "Protocol::Database::PostgreSQL::Client"->new("database", $self->database_name, "outgoing", $self->outgoing); $self->incoming->switch_str(sub { $_->type; } , "authentication_request", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Auth request received: %s", $msg); my $code = $AUTH_HANDLER{$msg->auth_type} or $Database::Async::Engine::PostgreSQL::log->errorf("unknown auth type %s", $msg->auth_type); $self->$code($msg); } ), "password", $self->$curry::weak(sub { my($self, %args) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Auth request received: %s", \%args); $self->protocol->{"user"} = $self->uri->user; $self->protocol->send_message("PasswordMessage", "password", $self->encode_text($self->database_password)); } ), "parameter_status", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Parameter received: %s", $msg); $self->set_parameter(map($self->decode_text($_), $msg->key, $msg->value)); } ), "row_description", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Row description %s", $msg); $Database::Async::Engine::PostgreSQL::log->errorf("No active query?") unless my $q = $self->active_query; $q->row_description($msg->description); } ), "data_row", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Have row data %s", $msg); $self->{"fc"} ||= do { my $src = $self->active_query->row_data; $self->stream->want_readready($src->is_paused ? 0 : 1) if $self->stream; $src->flow_control->each($self->$curry::weak(sub { my($self) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Flow control event - will %s stream", $_ ? "resume" : "pause"); $self->stream->want_readready($_) if $self->stream; } )) }; $self->active_query->row([map($self->decode_text($_), $msg->fields)]); } ), "command_complete", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; my $query = delete $self->{"active_query"} or do { $Database::Async::Engine::PostgreSQL::log->warnf("Command complete but no query"); return }; $Database::Async::Engine::PostgreSQL::log->tracef("Completed query %s with result %s", $query, $msg->result); $query->done unless $query->completed->is_ready; } ), "no_data", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Completed query %s with no data", $self->active_query); } ), "send_request", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Send request for %s", $msg); $self->stream->write($msg); } ), "ready_for_query", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Ready for query, state is %s", $msg->state); $self->ready_for_query->set_string($msg->state); $self->db->engine_ready($self) if $self->db; } ), "backend_key_data", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Backend key data: pid %d, key 0x%08x", $msg->pid, $msg->key); } ), "parse_complete", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Parsing complete for query %s", $self->active_query); } ), "bind_complete", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Bind complete for query %s", $self->active_query); } ), "close_complete", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Close complete for query %s", $self->active_query); } ), "empty_query_response", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Query returned no results for %s", $self->active_query); } ), "error_response", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; if (my $query = delete $self->{"active_query"}) { $Database::Async::Engine::PostgreSQL::log->warnf("Query returned error %s for %s", $msg->error, $self->active_query); my $f = $query->completed; $f->fail($msg->error) unless $f->is_ready; } else { $Database::Async::Engine::PostgreSQL::log->errorf("Received error %s with no active query", $msg->error); } ; } ), "copy_in_response", $self->$curry::weak(sub { my($self, $msg) = @_; my $query = $self->active_query; $Database::Async::Engine::PostgreSQL::log->tracef("Ready to copy data for %s", $query); my $proto = $self->protocol; { my $src = $query->streaming_input; $src->completed->on_ready(sub { my($f) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Sending copy done notification, stream status was %s", $f->state); $proto->send_message("CopyDone", "data", ""); $proto->send_message("Close", "portal", "", "statement", ""); $proto->send_message("Sync", "portal", "", "statement", ""); } ); $src->each(sub { $Database::Async::Engine::PostgreSQL::log->tracef("Sending %s", $_); $proto->send_copy_data($_); } ); } ; $query->ready_to_stream->done unless $query->ready_to_stream->is_ready; } ), "copy_out_response", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("copy out starts %s", $msg); } ), "copy_data", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Have copy data %s", $msg); my $query = $self->active_query or do { $Database::Async::Engine::PostgreSQL::log->warnf("No active query for copy data"); return }; $query->row([map($self->decode_text($_), @$_)]) foreach ($msg->rows); } ), "copy_done", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Copy done - %s", $msg); } ), "notification_response", $self->$curry::weak(sub { my($self, $msg) = @_; my($chan, $data) = @{$msg;}{"channel", "data"}; $Database::Async::Engine::PostgreSQL::log->tracef("Notification on channel %s containing %s", $chan, $data); $self->db->notification($self, map($self->decode_text($_), $chan, $data)); } ), sub { $Database::Async::Engine::PostgreSQL::log->errorf("Unknown message %s (type %s)", $_, $_->type); } ); $pg }
Truth table0%0/3
A //= B
A= $self->{"protocol"}B= do { my $pg = "Protocol::Database::PostgreSQL::Client"->new("database", $self->database_name, "outgoing", $self->outgoing); $self->incoming->switch_str(sub { $_->type; } , "authentication_request", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Auth request received: %s", $msg); my $code = $AUTH_HANDLER{$msg->auth_type} or $Database::Async::Engine::PostgreSQL::log->errorf("unknown auth type %s", $msg->auth_type); $self->$code($msg); } ), "password", $self->$curry::weak(sub { my($self, %args) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Auth request received: %s", \%args); $self->protocol->{"user"} = $self->uri->user; $self->protocol->send_message("PasswordMessage", "password", $self->encode_text($self->database_password)); } ), "parameter_status", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Parameter received: %s", $msg); $self->set_parameter(map($self->decode_text($_), $msg->key, $msg->value)); } ), "row_description", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Row description %s", $msg); $Database::Async::Engine::PostgreSQL::log->errorf("No active query?") unless my $q = $self->active_query; $q->row_description($msg->description); } ), "data_row", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Have row data %s", $msg); $self->{"fc"} ||= do { my $src = $self->active_query->row_data; $self->stream->want_readready($src->is_paused ? 0 : 1) if $self->stream; $src->flow_control->each($self->$curry::weak(sub { my($self) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Flow control event - will %s stream", $_ ? "resume" : "pause"); $self->stream->want_readready($_) if $self->stream; } )) }; $self->active_query->row([map($self->decode_text($_), $msg->fields)]); } ), "command_complete", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; my $query = delete $self->{"active_query"} or do { $Database::Async::Engine::PostgreSQL::log->warnf("Command complete but no query"); return }; $Database::Async::Engine::PostgreSQL::log->tracef("Completed query %s with result %s", $query, $msg->result); $query->done unless $query->completed->is_ready; } ), "no_data", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Completed query %s with no data", $self->active_query); } ), "send_request", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Send request for %s", $msg); $self->stream->write($msg); } ), "ready_for_query", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Ready for query, state is %s", $msg->state); $self->ready_for_query->set_string($msg->state); $self->db->engine_ready($self) if $self->db; } ), "backend_key_data", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Backend key data: pid %d, key 0x%08x", $msg->pid, $msg->key); } ), "parse_complete", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Parsing complete for query %s", $self->active_query); } ), "bind_complete", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Bind complete for query %s", $self->active_query); } ), "close_complete", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Close complete for query %s", $self->active_query); } ), "empty_query_response", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Query returned no results for %s", $self->active_query); } ), "error_response", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; if (my $query = delete $self->{"active_query"}) { $Database::Async::Engine::PostgreSQL::log->warnf("Query returned error %s for %s", $msg->error, $self->active_query); my $f = $query->completed; $f->fail($msg->error) unless $f->is_ready; } else { $Database::Async::Engine::PostgreSQL::log->errorf("Received error %s with no active query", $msg->error); } ; } ), "copy_in_response", $self->$curry::weak(sub { my($self, $msg) = @_; my $query = $self->active_query; $Database::Async::Engine::PostgreSQL::log->tracef("Ready to copy data for %s", $query); my $proto = $self->protocol; { my $src = $query->streaming_input; $src->completed->on_ready(sub { my($f) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Sending copy done notification, stream status was %s", $f->state); $proto->send_message("CopyDone", "data", ""); $proto->send_message("Close", "portal", "", "statement", ""); $proto->send_message("Sync", "portal", "", "statement", ""); } ); $src->each(sub { $Database::Async::Engine::PostgreSQL::log->tracef("Sending %s", $_); $proto->send_copy_data($_); } ); } ; $query->ready_to_stream->done unless $query->ready_to_stream->is_ready; } ), "copy_out_response", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("copy out starts %s", $msg); } ), "copy_data", $self->$curry::weak(sub { my($self, $msg) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Have copy data %s", $msg); my $query = $self->active_query or do { $Database::Async::Engine::PostgreSQL::log->warnf("No active query for copy data"); return }; $query->row([map($self->decode_text($_), @$_)]) foreach ($msg->rows); } ), "copy_done", $self->$curry::weak(sub { my($self, $msg) = @_; delete $self->{"fc"}; $Database::Async::Engine::PostgreSQL::log->tracef("Copy done - %s", $msg); } ), "notification_response", $self->$curry::weak(sub { my($self, $msg) = @_; my($chan, $data) = @{$msg;}{"channel", "data"}; $Database::Async::Engine::PostgreSQL::log->tracef("Notification on channel %s containing %s", $chan, $data); $self->db->notification($self, map($self->decode_text($_), $chan, $data)); } ), sub { $Database::Async::Engine::PostgreSQL::log->errorf("Unknown message %s (type %s)", $_, $_->type); } ); $pg }
ABresult
1X1
011
000
704 0 my $pg = Protocol::Database::PostgreSQL::Client->new(
705 database => $self->database_name,
706 outgoing => $self->outgoing,
707 );
708 $self->incoming
709 ->switch_str(
710 0 ▶ sub { $_->type },
Subroutine __ANON__: not called CC=1 SCAR=6.9
711 0 ▶ authentication_request => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
712 0 my ($self, $msg) = @_;
713 0 $log->tracef('Auth request received: %s', $msg);
714 0 ▶ my $code = $AUTH_HANDLER{$msg->auth_type}
Branch: 1 branch
BranchTrueFalseTotal
my $code = $Database::Async::Engine::PostgreSQL::AUTH_HANDLER{$msg->auth_type} or $Database::Async::Engine::PostgreSQL::log->errorf("unknown auth type %s", $msg->auth_type) 0 0 0
715 or $log->errorf('unknown auth type %s', $msg->auth_type);
716 0 $self->$code($msg);
717 }),
718 0 ▶ password => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
719 0 my ($self, %args) = @_;
720 0 $log->tracef('Auth request received: %s', \%args);
721 0 $self->protocol->{user} = $self->uri->user;
722 0 $self->protocol->send_message('PasswordMessage', password => $self->encode_text($self->database_password));
723 }),
724 0 ▶ parameter_status => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
725 0 my ($self, $msg) = @_;
726 0 $log->tracef('Parameter received: %s', $msg);
727 0 $self->set_parameter(map $self->decode_text($_), $msg->key => $msg->value);
728 }),
729 0 ▶ row_description => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
730 0 my ($self, $msg) = @_;
731 0 $log->tracef('Row description %s', $msg);
732 0 ▶ $log->errorf('No active query?') unless my $q = $self->active_query;
Branch: 1 branch
BranchTrueFalseTotal
unless my $q = $self->active_query 0 0 0
733 0 $q->row_description($msg->description);
734 }),
735 0 ▶ data_row => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
736 0 my ($self, $msg) = @_;
737 0 $log->tracef('Have row data %s', $msg);
738 0 ▶ $self->{fc} ||= do {
Condition0%0/3
$self->{"fc"} ||= do { my $src = $self->active_query->row_data; $self->stream->want_readready($src->is_paused ? 0 : 1) if $self->stream; $src->flow_control->each($self->$curry::weak(sub { my($self) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Flow control event - will %s stream", $_ ? "resume" : "pause"); $self->stream->want_readready($_) if $self->stream; } )) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"fc"} ||= do { my $src = $self->active_query->row_data; $self->stream->want_readready($src->is_paused ? 0 : 1) if $self->stream; $src->flow_control->each($self->$curry::weak(sub { my($self) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Flow control event - will %s stream", $_ ? "resume" : "pause"); $self->stream->want_readready($_) if $self->stream; } )) }
$self->{"fc"} do { my $src = $self->active_query->row_data; $self->stream->want_readready($src->is_paused ? 0 : 1) if $self->stream; $src->flow_control->each($self->$curry::weak(sub { my($self) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Flow control event - will %s stream", $_ ? "resume" : "pause"); $self->stream->want_readready($_) if $self->stream; } )) }
Truth table0%0/3
A ||= B
A= $self->{"fc"}B= do { my $src = $self->active_query->row_data; $self->stream->want_readready($src->is_paused ? 0 : 1) if $self->stream; $src->flow_control->each($self->$curry::weak(sub { my($self) = @_; $Database::Async::Engine::PostgreSQL::log->tracef("Flow control event - will %s stream", $_ ? "resume" : "pause"); $self->stream->want_readready($_) if $self->stream; } )) }
ABresult
1X1
011
000
739 0 my $src = $self->active_query->row_data;
740 0 ▶ $self->stream->want_readready($src->is_paused ? 0 : 1) if $self->stream;
Branch: 2 branches
BranchTrueFalseTotal
if $self->stream 0 0 0
$src->is_paused ? : 0 0 0
741 0 ▶ $src->flow_control->each($self->$curry::weak(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
742 0 my ($self) = @_;
743 0 ▶ $log->tracef('Flow control event - will %s stream', $_ ? 'resume' : 'pause');
Branch: 1 branch
BranchTrueFalseTotal
$_ ? : 0 0 0
744 0 ▶ $self->stream->want_readready($_) if $self->stream;
Branch: 1 branch
BranchTrueFalseTotal
if $self->stream 0 0 0
745 0 }));
746 };
747 0 $self->active_query->row([ map $self->decode_text($_), $msg->fields ]);
748 }),
749 0 ▶ command_complete => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
750 0 my ($self, $msg) = @_;
751 # Flow control is only valid for the current stream, so we discard it here
752 0 delete $self->{fc};
753 0 ▶ my $query = delete $self->{active_query} or do {
Branch: 1 branch
BranchTrueFalseTotal
my $query = delete $self->{"active_query"} or do { $Database::Async::Engine::PostgreSQL::log->warnf("Command complete but no query"); return } 0 0 0
754 0 $log->warnf('Command complete but no query');
755 0 return;
756 };
757 0 $log->tracef('Completed query %s with result %s', $query, $msg->result);
758 0 ▶ $query->done unless $query->completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $query->completed->is_ready 0 0 0
759 }),
760 0 ▶ no_data => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
761 0 my ($self, $msg) = @_;
762 # Flow control is only valid for the current stream, so we discard it here
763 0 delete $self->{fc};
764 0 $log->tracef('Completed query %s with no data', $self->active_query);
765 # my $query = delete $self->{active_query};
766 # $query->done if $query;
767 }),
768 0 ▶ send_request => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
769 0 my ($self, $msg) = @_;
770 0 $log->tracef('Send request for %s', $msg);
771 0 $self->stream->write($msg);
772 }),
773 0 ▶ ready_for_query => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
774 0 my ($self, $msg) = @_;
775 # Flow control is only valid for the current stream, so we discard it here
776 0 delete $self->{fc};
777 0 $log->tracef('Ready for query, state is %s', $msg->state);
778 0 $self->ready_for_query->set_string($msg->state);
779 0 ▶ $self->db->engine_ready($self) if $self->db;
Branch: 1 branch
BranchTrueFalseTotal
if $self->db 0 0 0
780 }),
781 0 ▶ backend_key_data => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
782 0 my ($self, $msg) = @_;
783 0 $log->tracef('Backend key data: pid %d, key 0x%08x', $msg->pid, $msg->key);
784 }),
785 0 ▶ parse_complete => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
786 0 my ($self, $msg) = @_;
787 0 $log->tracef('Parsing complete for query %s', $self->active_query);
788 }),
789 0 ▶ bind_complete => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
790 0 my ($self, $msg) = @_;
791 0 $log->tracef('Bind complete for query %s', $self->active_query);
792 }),
793 0 ▶ close_complete => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
794 0 my ($self, $msg) = @_;
795 # Flow control is only valid for the current stream, so we discard it here
796 0 delete $self->{fc};
797 0 $log->tracef('Close complete for query %s', $self->active_query);
798 }),
799 0 ▶ empty_query_response => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
800 0 my ($self, $msg) = @_;
801 # Flow control is only valid for the current stream, so we discard it here
802 0 delete $self->{fc};
803 0 $log->tracef('Query returned no results for %s', $self->active_query);
804 }),
805 0 ▶ error_response => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
806 0 my ($self, $msg) = @_;
807 # Flow control is only valid for the current stream, so we discard it here
808 0 delete $self->{fc};
809 0 ▶ if(my $query = delete $self->{active_query}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $query = delete $self->{'active_query'}) { } 0 0 0
810 0 $log->warnf('Query returned error %s for %s', $msg->error, $self->active_query);
811 0 my $f = $query->completed;
812 0 ▶ $f->fail($msg->error) unless $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $f->is_ready 0 0 0
813 } else {
814 0 $log->errorf('Received error %s with no active query', $msg->error);
815 }
816 }),
817 0 ▶ copy_in_response => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
818 0 my ($self, $msg) = @_;
819 0 my $query = $self->active_query;
820 0 $log->tracef('Ready to copy data for %s', $query);
821 0 my $proto = $self->protocol;
822 {
823 0 my $src = $query->streaming_input;
824 $src->completed
825 0 ▶ ->on_ready(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
826 0 my ($f) = @_;
827 0 $log->tracef('Sending copy done notification, stream status was %s', $f->state);
828 0 $proto->send_message(
829 'CopyDone',
830 data => '',
831 );
832 0 $proto->send_message(
833 'Close',
834 portal => '',
835 statement => '',
836 );
837 0 $proto->send_message(
838 'Sync',
839 portal => '',
840 statement => '',
841 );
842 0 });
843 0 ▶ $src->each(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
844 0 $log->tracef('Sending %s', $_);
845 0 $proto->send_copy_data($_);
846 0 });
847 }
848 0 ▶ $query->ready_to_stream->done unless $query->ready_to_stream->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $query->ready_to_stream->is_ready 0 0 0
849 }),
850 0 ▶ copy_out_response => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
851 0 my ($self, $msg) = @_;
852 0 $log->tracef('copy out starts %s', $msg);
853 # $self->active_query->row([ $msg->fields ]);
854 }),
855 0 ▶ copy_data => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
856 0 my ($self, $msg) = @_;
857 0 $log->tracef('Have copy data %s', $msg);
858 0 ▶ my $query = $self->active_query or do {
Branch: 1 branch
BranchTrueFalseTotal
my $query = $self->active_query or do { $Database::Async::Engine::PostgreSQL::log->warnf("No active query for copy data"); return } 0 0 0
859 0 $log->warnf('No active query for copy data');
860 0 return;
861 };
862 0 $query->row([ map $self->decode_text($_), @$_ ]) for $msg->rows;
863 }),
864 0 ▶ copy_done => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
865 0 my ($self, $msg) = @_;
866 0 delete $self->{fc};
867 0 $log->tracef('Copy done - %s', $msg);
868 }),
869 0 ▶ notification_response => $self->$curry::weak(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
870 0 my ($self, $msg) = @_;
871 0 my ($chan, $data) = @{$msg}{qw(channel data)};
872 0 $log->tracef('Notification on channel %s containing %s', $chan, $data);
873 0 $self->db->notification($self, map $self->decode_text($_), $chan, $data);
874 }),
875 0 ▶ sub { $log->errorf('Unknown message %s (type %s)', $_, $_->type) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
876 0 );
877 0 $pg
878 }
879 }
880  
881 0 ▶sub stream_from {
Subroutine stream_from: not called CC=1 SCAR=6.9
Pod undocumented
882 0 my ($self, $src) = @_;
883 0 my $proto = $self->proto;
884 0 ▶ $src->each(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
885 0 $log->tracef('Sending %s', $_);
886 # This is already UTF-8 encoded in the protocol handler,
887 # since it's a text-based protocol
888 0 $proto->send_copy_data($_);
889 })
890 0 }
891  
892 =head2 set_parameter
893  
894 Marks a parameter update from the server.
895  
896 =cut
897  
898 0 ▶sub set_parameter {
Subroutine set_parameter: not called CC=2 SCAR=17.9
Pod documented
899 0 my ($self, $k, $v) = @_;
900 0 ▶ if(my $param = $self->{parameter}{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $param = $self->{'parameter'}{$k}) { } 0 0 0
901 0 $param->set_string($v);
902 } else {
903 0 $self->{parameter}{$k} = Ryu::Observable->new($v);
904 }
905 0 $self
906 }
907  
908 =head2 idle
909  
910 Resolves when we are idle and ready to process the next request.
911  
912 =cut
913  
914 0 ▶sub idle {
Subroutine idle: not called CC=2 SCAR=17.9
Pod documented
915 0 my ($self) = @_;
916 0 ▶ $self->{idle} //= $self->loop->new_future->on_ready(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
917 delete $self->{idle}
918 0 ▶ });
Condition0%0/3
$self->{"idle"} //= $self->loop->new_future->on_ready(sub { delete $self->{"idle"}; } )
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"idle"} //= $self->loop->new_future->on_ready(sub { delete $self->{"idle"}; } )
$self->{"idle"} $self->loop->new_future->on_ready(sub { delete $self->{"idle"}; } )
Truth table0%0/3
A //= B
A= $self->{"idle"}B= $self->loop->new_future->on_ready(sub { delete $self->{"idle"}; } )
ABresult
1X1
011
000
919 }
920  
921 0 ▶sub ready_for_query {
Subroutine ready_for_query: not called CC=2 SCAR=17.9
Pod undocumented
922 0 my ($self) = @_;
923 0 ▶ $self->{ready_for_query} //= do {
Condition0%0/3
$self->{"ready_for_query"} //= do { "Ryu::Observable"->new(0)->subscribe($self->$curry::weak(sub { my($self, $v) = @_; return unless my $idle = $self->{"idle"} and $v; $idle->done unless $idle->is_ready; } )) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"ready_for_query"} //= do { "Ryu::Observable"->new(0)->subscribe($self->$curry::weak(sub { my($self, $v) = @_; return unless my $idle = $self->{"idle"} and $v; $idle->done unless $idle->is_ready; } )) }
$self->{"ready_for_query"} do { "Ryu::Observable"->new(0)->subscribe($self->$curry::weak(sub { my($self, $v) = @_; return unless my $idle = $self->{"idle"} and $v; $idle->done unless $idle->is_ready; } )) }
Truth table0%0/3
A //= B
A= $self->{"ready_for_query"}B= do { "Ryu::Observable"->new(0)->subscribe($self->$curry::weak(sub { my($self, $v) = @_; return unless my $idle = $self->{"idle"} and $v; $idle->done unless $idle->is_ready; } )) }
ABresult
1X1
011
000
924 0 ▶ Ryu::Observable->new(0)->subscribe($self->$curry::weak(sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
925 0 my ($self, $v) = @_;
926 0 ▶ return unless my $idle = $self->{idle} and $v;
Branch: 1 branch
BranchTrueFalseTotal
unless my $idle = $self->{"idle"} and $v 0 0 0
Condition0%0/3
my $idle = $self->{"idle"} and $v
!ll&&!rl&&r
000
MC/DC0%0/2
my $idle = $self->{"idle"} and $v
my $idle = $self->{"idle"} $v
Truth table0%0/3
A and B
A= my $idle = $self->{"idle"}B= $v
ABresult
0X0
100
111
927 0 ▶ $idle->done unless $idle->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $idle->is_ready 0 0 0
928 0 }))
929 }
930 }
931  
932 0 ▶sub simple_query {
Subroutine simple_query: not called CC=2 SCAR=17.9
Pod undocumented
933 0 my ($self, $sql) = @_;
934 0 ▶ die 'already have active query' if $self->{active_query};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_query"} 0 0 0
935 0 $self->{active_query} = my $query = Database::Async::Query->new(
936 sql => $sql,
937 db => $self->db,
938 row_data => my $src = $self->ryu->source
939 );
940 0 ▶ $query->completed->on_ready(sub { $src->finish });
Subroutine __ANON__: not called CC=1 SCAR=6.9
941 0 $self->protocol->simple_query($self->encode_text($query->sql));
942 0 return $src;
943 }
944  
945 0 ▶sub encode_text {
Subroutine encode_text: not called CC=4 SCAR=30.0
Pod undocumented
946 0 my ($self, $txt) = @_;
947 0 ▶ return $txt unless defined $txt and my $encoding = $self->encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $txt and my $encoding = $self->encoding 0 0 0
Condition0%0/3
defined $txt and my $encoding = $self->encoding
!ll&&!rl&&r
000
MC/DC0%0/2
defined $txt and my $encoding = $self->encoding
defined $txt my $encoding = $self->encoding
Truth table0%0/3
A and B
A= defined $txtB= my $encoding = $self->encoding
ABresult
0X0
100
111
948 0 ▶ return Unicode::UTF8::encode_utf8($txt) if $encoding eq 'UTF-8';
Branch: 1 branch
BranchTrueFalseTotal
if $encoding eq "UTF-8" 0 0 0
949 0 return Encode::encode($encoding, $txt, Encode::FB_CROAK);
950 }
951  
952 0 ▶sub decode_text {
Subroutine decode_text: not called CC=4 SCAR=30.0
Pod undocumented
953 0 my ($self, $txt) = @_;
954 0 ▶ return $txt unless defined $txt and my $encoding = $self->encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $txt and my $encoding = $self->encoding 0 0 0
Condition0%0/3
defined $txt and my $encoding = $self->encoding
!ll&&!rl&&r
000
MC/DC0%0/2
defined $txt and my $encoding = $self->encoding
defined $txt my $encoding = $self->encoding
Truth table0%0/3
A and B
A= defined $txtB= my $encoding = $self->encoding
ABresult
0X0
100
111
955 0 ▶ return Unicode::UTF8::decode_utf8($txt) if $encoding eq 'UTF-8';
Branch: 1 branch
BranchTrueFalseTotal
if $encoding eq "UTF-8" 0 0 0
956 0 return Encode::decode($encoding, $txt, Encode::FB_CROAK);
957 }
958  
959 0 ▶sub handle_query {
Subroutine handle_query: not called CC=3 SCAR=24.8
Pod undocumented
960 0 my ($self, $query) = @_;
961 0 ▶ die 'already have active query' if $self->{active_query};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"active_query"} 0 0 0
962 0 $self->{active_query} = $query;
963 0 my $proto = $self->protocol;
964 0 $proto->send_message(
965 'Parse',
966 sql => $self->encode_text($query->sql),
967 statement => '',
968 );
969 0 $proto->send_message(
970 'Bind',
971 portal => '',
972 statement => '',
973 param => [ map $self->encode_text($_), $query->bind ],
974 );
975 0 $proto->send_message(
976 'Describe',
977 portal => '',
978 statement => '',
979 );
980 0 $proto->send_message(
981 'Execute',
982 portal => '',
983 statement => '',
984 );
985 0 ▶ unless($query->{in}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($query->{"in"}) 0 0 0
986 0 $proto->send_message(
987 'Close',
988 portal => '',
989 statement => '',
990 );
991 0 $proto->send_message(
992 'Sync',
993 portal => '',
994 statement => '',
995 );
996 }
997 Future->done
998 0 }
999  
1000 0 ▶sub query { die 'use handle_query instead'; }
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
1001  
1002 0 ▶sub active_query { shift->{active_query} }
Subroutine active_query: not called CC=1 SCAR=6.9
Pod undocumented
1003  
1004 =head2 _remove_from_loop
1005  
1006 Called when this engine instance is removed from the main event loop, usually just before the instance
1007 is destroyed.
1008  
1009 Since we could be in various states of authentication or query processing, we potentially have many
1010 different elements to clean up here. We do these explicitly so that we can apply some ordering to the events:
1011 clear out things that relate to queries before dropping the connection, for example.
1012  
1013 =cut
1014  
1015 0 ▶sub _remove_from_loop {
Subroutine _remove_from_loop: not called CC=15 SCAR=54.8
1016 0 my ($self, $loop) = @_;
1017 0 ▶ if(my $query = delete $self->{active_query}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $query = delete $self->{"active_query"}) 0 0 0
1018 0 ▶ $query->fail('disconnected') unless $query->completed->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $query->completed->is_ready 0 0 0
1019 }
1020 0 ▶ if(my $idle = delete $self->{idle}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $idle = delete $self->{"idle"}) 0 0 0
1021 0 $idle->cancel;
1022 }
1023 0 ▶ if(my $auth = delete $self->{authenticated}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $auth = delete $self->{"authenticated"}) 0 0 0
1024 0 $auth->cancel;
1025 }
1026 0 ▶ if(my $connected = delete $self->{connected}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $connected = delete $self->{"connected"}) 0 0 0
1027 0 $connected->finish;
1028 }
1029 0 ▶ if(my $outgoing = delete $self->{outgoing}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $outgoing = delete $self->{"outgoing"}) 0 0 0
1030 0 ▶ $outgoing->finish unless $outgoing->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $outgoing->is_ready 0 0 0
1031 }
1032 0 ▶ if(my $incoming = delete $self->{incoming}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $incoming = delete $self->{"incoming"}) 0 0 0
1033 0 ▶ $incoming->finish unless $incoming->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
unless $incoming->is_ready 0 0 0
1034 }
1035 0 ▶ if(my $stream = delete $self->{stream}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $stream = delete $self->{"stream"}) 0 0 0
1036 0 $stream->close_now;
1037 0 $stream->remove_from_parent;
1038 }
1039 0 ▶ if(my $conn = delete $self->{connection}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $conn = delete $self->{"connection"}) 0 0 0
1040 0 $conn->cancel;
1041 }
1042 # Observable connection parameters - signal that no further updates are expected
1043 0 for my $k (keys %{$self->{parameter}}) {
1044 0 my $param = delete $self->{parameter}{$k};
1045 0 ▶ $param->finish if $param;
Branch: 1 branch
BranchTrueFalseTotal
if $param 0 0 0
1046 }
1047 0 ▶ if(my $ryu = delete $self->{ryu}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ryu = delete $self->{"ryu"}) 0 0 0
1048 0 $ryu->remove_from_parent;
1049 }
1050 0 delete $self->{protocol};
1051 0 return $self->next::method($loop);
1052 }
1053  
1054 7 1;
1055  
1056 __END__
↑ Summary PostgreSQL/DDL.pm »