| 1 | package DBIO::PostgreSQL::EV::ConnectInfo; | ||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: PostgreSQL connection string utilities for DBIO async driver | ||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 12 | our @EXPORT_OK = qw( escape_conninfo conninfo_string dbi_to_conninfo ); | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||
| 11 | 9 | ▶ | sub escape_conninfo { | ||||||||||||||||||||||||||||||
|
Subroutine
escape_conninfo: called CC=4 SCAR=18.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 12 | 9 | my $val = shift; | |||||||||||||||||||||||||||||||
| 13 | 9 | ▶ | return "''" unless defined $val && length $val; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $val and length $val
MC/DC0%0/2
defined $val and length $val
defined $val
length $val
Truth table33%1/3
A and B
A= defined $valB= length $val
| |||||||||||||||||||||||||||||||||
| 14 | 9 | ▶ | return $val unless $val =~ /[\s'\\]/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 15 | 0 | $val =~ s/\\/\\\\/g; | |||||||||||||||||||||||||||||||
| 16 | 0 | $val =~ s/'/\\'/g; | |||||||||||||||||||||||||||||||
| 17 | 0 | return "'$val'"; | |||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | 4 | ▶ | sub conninfo_string { | ||||||||||||||||||||||||||||||
|
Subroutine
conninfo_string: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 22 | 4 | my ($ci) = @_; | |||||||||||||||||||||||||||||||
| 23 | 4 | ▶ | return $ci unless ref $ci; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||
| 25 | 9 | return join(' ', map { "$_=" . escape_conninfo($ci->{$_}) } | |||||||||||||||||||||||||||||||
| 26 | 3 | grep { defined $ci->{$_} } keys %$ci); | |||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | 0 | ▶ | sub dbi_to_conninfo { | ||||||||||||||||||||||||||||||
|
Subroutine
dbi_to_conninfo: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 31 | 0 | my ($info) = @_; | |||||||||||||||||||||||||||||||
| 32 | 0 | my ($dsn, $user, $pass, $attrs) = @$info; | |||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | die "expected dbi:Pg:... DSN, got: " . (defined $dsn ? $dsn : '(undef)') . "\n" | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
defined $dsn and $dsn =~ s/^dbi:Pg:(.+)$/$1/i
MC/DC0%0/2
defined $dsn and $dsn =~ s/^dbi:Pg:(.+)$/$1/i
defined $dsn
$dsn =~ s/^dbi:Pg:(.+)$/$1/i
Truth table0%0/3
A and B
A= defined $dsnB= $dsn =~ s/^dbi:Pg:(.+)$/$1/i
| |||||||||||||||||||||||||||||||||
| 34 | unless defined $dsn && $dsn =~ s{^dbi:Pg:(.+)$}{$1}i; | ||||||||||||||||||||||||||||||||
| 35 | 0 | my $params = $1; | |||||||||||||||||||||||||||||||
| 36 | 0 | my %h; | |||||||||||||||||||||||||||||||
| 37 | 0 | for my $kv (split /;/, $params) { | |||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | next unless $kv =~ /\S/; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 39 | 0 | my ($k, $v) = split /=/, $kv, 2; | |||||||||||||||||||||||||||||||
| 40 | 0 | $k = lc $k; | |||||||||||||||||||||||||||||||
| 41 | 0 | ▶ | $k = 'dbname' if $k eq 'database'; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 42 | 0 | $h{$k} = $v; | |||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||
| 44 | 0 | ▶ | $h{user} //= $user if defined $user; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$h{"user"} //= $user
MC/DC0%0/2
$h{"user"} //= $user
$h{"user"}
$user
Truth table0%0/3
A //= B
A= $h{"user"}B= $user
| |||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | $h{password} //= $pass if defined $pass; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$h{"password"} //= $pass
MC/DC0%0/2
$h{"password"} //= $pass
$h{"password"}
$pass
Truth table0%0/3
A //= B
A= $h{"password"}B= $pass
| |||||||||||||||||||||||||||||||||
| 46 | 0 | my %opts; | |||||||||||||||||||||||||||||||
| 47 | 0 | ▶ | $h{pool_size} = $attrs->{pool_size} if ref $attrs eq 'HASH' && $attrs->{pool_size}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
ref $attrs eq "HASH" and $attrs->{"pool_size"}
MC/DC0%0/2
ref $attrs eq "HASH" and $attrs->{"pool_size"}
ref $attrs eq "HASH"
$attrs->{"pool_size"}
Truth table0%0/3
A and B
A= ref $attrs eq "HASH"B= $attrs->{"pool_size"}
| |||||||||||||||||||||||||||||||||
| 48 | 0 | return [ \%h, \%opts ]; | |||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||
| 51 | 12 | 1; | |||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||
| 53 | __END__ | ||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||
| 55 | =pod | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | DBIO::PostgreSQL::EV::ConnectInfo - PostgreSQL connection string utilities for DBIO async driver | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | =head1 VERSION | ||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||
| 65 | version 0.900001 | ||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||
| 67 | =func escape_conninfo | ||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||
| 69 | my $escaped = escape_conninfo($value); | ||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | Escape a single connection parameter value for use in a libpq conninfo string. | ||||||||||||||||||||||||||||||||
| 72 | Handles whitespace, single quotes, and backslashes. | ||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | =func conninfo_string | ||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||
| 76 | my $str = conninfo_string({ host => 'localhost', port => 5432 }); | ||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||
| 78 | Convert a hashref of connection parameters into a libpq connection string. | ||||||||||||||||||||||||||||||||
| 79 | Skips undefined values. | ||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||
| 81 | =func dbi_to_conninfo | ||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||
| 83 | my $async_info = dbi_to_conninfo([ 'dbi:Pg:dbname=test;host=localhost', $user, $pass, \%attrs ]); | ||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | Translate a DBI-style connect-info arrayref (C<[$dsn, $user, $pass, \%attrs]>, | ||||||||||||||||||||||||||||||||
| 86 | as produced by the sync L<DBIO::PostgreSQL::Storage>) into the async | ||||||||||||||||||||||||||||||||
| 87 | C<[ \%conninfo, \%opts ]> shape consumed by L<DBIO::PostgreSQL::EV::Storage>. | ||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||
| 89 | The C<dbi:Pg:> DSN is parsed into named libpq parameters (C<dbname>, C<host>, | ||||||||||||||||||||||||||||||||
| 90 | C<port>, ...); C<database> is normalised to C<dbname>. C<$user> / C<$pass> fill | ||||||||||||||||||||||||||||||||
| 91 | in C<user> / C<password> when not already present. A C<pool_size> attribute, if | ||||||||||||||||||||||||||||||||
| 92 | given, is carried into the conninfo hash. Dies unless the DSN is in C<dbi:Pg:> | ||||||||||||||||||||||||||||||||
| 93 | form, since libpq does not understand it. | ||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||
| 95 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||
| 97 | DBIO Authors | ||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||
| 99 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||
| 101 | Copyright (C) 2026 DBIO Authors | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||
| 104 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||
| 106 | =cut | ||||||||||||||||||||||||||||||||