| 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 //= {}
MC/DC0%0/1
$params //= {}
$params
Truth table50%1/2
A
A= $params
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | 12 | ▶ | return ($sql, []) unless %$params; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
not $in_string and $char eq "'" || $char eq "\""
$char eq "'" or $char eq "\""
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 "\""
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 41 | 6 | $result .= $char; | |||||||||||||||||||||||||||||||||||||||||
| 42 | 6 | ▶ | if ($char eq $string_char) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | # Check for escaped quote | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 1 | ▶ | if ($i + 1 < $len && substr($sql, $i + 1, 1) eq $string_char) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$i + 1 < $len and substr($sql, $i + 1, 1) eq $string_char
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$char eq ":" and $i + 1 < $len and substr($sql, $i + 1, 1) eq ":"
$char eq ":" and $i + 1 < $len
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 ":"
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$j < $len and substr($sql, $j, 1) =~ /[a-zA-Z0-9_]/
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_]/
| |||||||||||||||||||||||||||||||||||||||||||
| 68 | 47 | $name .= substr($sql, $j, 1); | |||||||||||||||||||||||||||||||||||||||||
| 69 | 47 | $j++; | |||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 15 | ▶ | if (length($name) && exists $params->{$name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $name and exists $params->{$name}
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 73 | 15 | ▶ | if (!exists $seen{$name}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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"
MC/DC0%0/1
$host //= "localhost"
$host
Truth table50%1/2
A
A= $host
| |||||||||||||||||||||||||||||||||||||||||||
| 112 | 6 | ▶ | $port //= 5432; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$port //= 5432
MC/DC100%1/1
$port //= 5432
$port
Truth table100%2/2
A
A= $port
| |||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | 6 | ▶ | my @parts = ("dbname=$db") if $db; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 115 | 6 | ▶ | push @parts, "host=$host" if $host; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 116 | 6 | ▶ | push @parts, "port=$port" if $port; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | # Parse query string options | ||||||||||||||||||||||||||||||||||||||||||
| 119 | 6 | ▶ | if ($options) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||