Pg/Util.pm

filter: Statementstmt 96.3% 78 / 81 Branchbran 82.3% 28 / 34 Conditioncond 70.3% 19 / 27 MC/DCmcdc 46.6% 7 / 15 Subroutinesub 100% 6 / 6 Podpod 100% 3 / 3 totaltotal 84.9% 141 / 166 CC 29 SCAR 27.3
CC
29
Cov
88%
convert_placeholders
20.8
parse_dsn
10.3
BEGIN
1.0
CRAP 15.4
« Pg/PubSub.pm ↑ Summary Pg/Cursor.pm »
1 package IO::Async::Pg::Util;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our @EXPORT_OK = qw(convert_placeholders parse_dsn safe_dsn);
8  
9 # Convert named placeholders (:name) to positional ($1, $2, ...)
10 # Uses order of first appearance in SQL for deterministic ordering
11 12 ▶sub convert_placeholders {
Subroutine convert_placeholders: called CC=20 SCAR=30.4
Pod documented
12 12 my ($sql, $params) = @_;
13 12 ▶ $params //= {};
Condition50%1/2
$params //= {}
l!l
120
MC/DC0%0/1
$params //= {}
$params
Truth table50%1/2
A
A= $params
Aresult
11
00
14  
15 12 ▶ return ($sql, []) unless %$params;
Branch: 1 branch
BranchTrueFalseTotal
unless %$params 3 9 12
16  
17 9 my %seen; # name => position number
18 my @bind; # values in order
19 9 my $pos = 0;
20  
21 # Match :name but not inside strings or ::cast
22 9 my $result = '';
23 9 my $in_string = 0;
24 9 my $string_char = '';
25 9 my $i = 0;
26 9 my $len = length($sql);
27  
28 9 ▶ while ($i < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $len) 331 9 340
29 331 my $char = substr($sql, $i, 1);
30  
31 # Handle string literals
32 331 ▶ if (!$in_string && ($char eq "'" || $char eq '"')) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in_string and $char eq "'" || $char eq "\"") 1 330 331
Condition83%5/6
not $in_string and $char eq "'" || $char eq "\""
!ll&&!rl&&r
63241
$char eq "'" or $char eq "\""
l!l&&r!l&&!r
10324
MC/DC66%2/3
not $in_string and $char eq "'" || $char eq "\""
not $in_string $char eq "'" $char eq "\""
Truth table75%3/4
not(A) and B or C
A= not $in_stringB= $char eq "'"C= $char eq "\""
ABCresult
0XX0
1000
11X1
1011
33 1 $in_string = 1;
34 1 $string_char = $char;
35 1 $result .= $char;
36 1 $i++;
37 1 next;
38 }
39  
40 330 ▶ if ($in_string) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in_string) 6 324 330
41 6 $result .= $char;
42 6 ▶ if ($char eq $string_char) {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq $string_char) 1 5 6
43 # Check for escaped quote
44 1 ▶ if ($i + 1 < $len && substr($sql, $i + 1, 1) eq $string_char) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 < $len and substr($sql, $i + 1, 1) eq $string_char) 0 1 1
Condition33%1/3
$i + 1 < $len and substr($sql, $i + 1, 1) eq $string_char
!ll&&!rl&&r
010
MC/DC0%0/2
$i + 1 < $len and substr($sql, $i + 1, 1) eq $string_char
$i + 1 < $len substr($sql, $i + 1, 1) eq $string_char
Truth table33%1/3
A and B
A= $i + 1 < $lenB= substr($sql, $i + 1, 1) eq $string_char
ABresult
0X0
100
111
45 0 $result .= substr($sql, $i + 1, 1);
46 0 $i += 2;
47 0 next;
48 }
49 1 $in_string = 0;
50 }
51 6 $i++;
52 6 next;
53 }
54  
55 # Check for :: (PostgreSQL cast) - skip both colons
56 324 ▶ if ($char eq ':' && $i + 1 < $len && substr($sql, $i + 1, 1) eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq ":" and $i + 1 < $len and substr($sql, $i + 1, 1) eq ":") 1 323 324
Condition83%5/6
$char eq ":" and $i + 1 < $len and substr($sql, $i + 1, 1) eq ":"
!ll&&!rl&&r
308151
$char eq ":" and $i + 1 < $len
!ll&&!rl&&r
308016
MC/DC66%2/3
$char eq ":" and $i + 1 < $len and substr($sql, $i + 1, 1) eq ":"
$char eq ":" $i + 1 < $len substr($sql, $i + 1, 1) eq ":"
Truth table75%3/4
A and B and C
A= $char eq ":"B= $i + 1 < $lenC= substr($sql, $i + 1, 1) eq ":"
ABCresult
0XX0
10X0
1100
1111
57 1 $result .= '::';
58 1 $i += 2;
59 1 next;
60 }
61  
62 # Check for :name placeholder
63 323 ▶ if ($char eq ':') {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq ":") 15 308 323
64 # Look for valid identifier characters
65 15 my $name = '';
66 15 my $j = $i + 1;
67 15 ▶ while ($j < $len && substr($sql, $j, 1) =~ /[a-zA-Z0-9_]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $len and substr($sql, $j, 1) =~ /[a-zA-Z0-9_]/) 47 15 62
Condition100%3/3
$j < $len and substr($sql, $j, 1) =~ /[a-zA-Z0-9_]/
!ll&&!rl&&r
7847
MC/DC100%2/2
$j < $len and substr($sql, $j, 1) =~ /[a-zA-Z0-9_]/
$j < $len substr($sql, $j, 1) =~ /[a-zA-Z0-9_]/
Truth table100%3/3
A and B
A= $j < $lenB= substr($sql, $j, 1) =~ /[a-zA-Z0-9_]/
ABresult
0X0
100
111
68 47 $name .= substr($sql, $j, 1);
69 47 $j++;
70 }
71  
72 15 ▶ if (length($name) && exists $params->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $name and exists $params->{$name}) 15 0 15
Condition33%1/3
length $name and exists $params->{$name}
!ll&&!rl&&r
0015
MC/DC0%0/2
length $name and exists $params->{$name}
length $name exists $params->{$name}
Truth table33%1/3
A and B
A= length $nameB= exists $params->{$name}
ABresult
0X0
100
111
73 15 ▶ if (!exists $seen{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $seen{$name}) 14 1 15
74 14 $pos++;
75 14 $seen{$name} = $pos;
76 14 push @bind, $params->{$name};
77 }
78 15 $result .= '$' . $seen{$name};
79 15 $i = $j;
80 15 next;
81 }
82 }
83  
84 308 $result .= $char;
85 308 $i++;
86 }
87  
88 9 return ($result, \@bind);
89 }
90  
91 # Parse PostgreSQL URI to DBI components
92 7 ▶sub parse_dsn {
Subroutine parse_dsn: called CC=10 SCAR=23.3
Pod documented
93 7 my ($uri) = @_;
94  
95 # postgresql://user:pass@host:port/dbname?options
96 7 my $parsed = {
97 dbi_dsn => '',
98 user => undef,
99 password => undef,
100 };
101  
102 7 ▶ if ($uri =~ m{^postgres(?:ql)?://
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ m[^postgres(?:ql)?:// (?:([^:\@/]+)(?::([^\@/]*))?@)? # user:pass@ ([^:/?]+)? # host (?::(\d+))? # :port (?:/([^?]+))? # /dbname (?:\?(.+))? # ?options ]x) { } 6 1 7
103 (?:([^:@/]+)(?::([^@/]*))?@)? # user:pass@
104 ([^:/?]+)? # host
105 (?::(\d+))? # :port
106 (?:/([^?]+))? # /dbname
107 (?:\?(.+))? # ?options
108 }x) {
109 6 my ($user, $pass, $host, $port, $db, $options) = ($1, $2, $3, $4, $5, $6);
110  
111 6 ▶ $host //= 'localhost';
Condition50%1/2
$host //= "localhost"
l!l
60
MC/DC0%0/1
$host //= "localhost"
$host
Truth table50%1/2
A
A= $host
Aresult
11
00
112 6 ▶ $port //= 5432;
Condition100%2/2
$port //= 5432
l!l
24
MC/DC100%1/1
$port //= 5432
$port
Truth table100%2/2
A
A= $port
Aresult
11
00
113  
114 6 ▶ my @parts = ("dbname=$db") if $db;
Branch: 1 branch
BranchTrueFalseTotal
if $db 6 0 6
115 6 ▶ push @parts, "host=$host" if $host;
Branch: 1 branch
BranchTrueFalseTotal
if $host 6 0 6
116 6 ▶ push @parts, "port=$port" if $port;
Branch: 1 branch
BranchTrueFalseTotal
if $port 6 0 6
117  
118 # Parse query string options
119 6 ▶ if ($options) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options) 2 4 6
120 2 for my $opt (split /&/, $options) {
121 2 my ($key, $val) = split /=/, $opt, 2;
122 2 ▶ push @parts, "$key=$val" if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 2 0 2
123 }
124 }
125  
126 6 $parsed->{dbi_dsn} = 'dbi:Pg:' . join(';', @parts);
127 6 $parsed->{user} = $user;
128 6 $parsed->{password} = $pass;
129 }
130 else {
131 1 die "Cannot parse DSN: $uri";
132 }
133  
134 6 return $parsed;
135 }
136  
137 # Return DSN with password masked
138 2 ▶sub safe_dsn {
Subroutine safe_dsn: called CC=1 SCAR=0.0
Pod documented
139 2 my ($uri) = @_;
140 2 $uri =~ s{://([^:]+):[^@]+@}{://$1:***@};
141 2 return $uri;
142 }
143  
144 2 1;
145  
146 __END__
« Pg/PubSub.pm ↑ Summary Pg/Cursor.pm »