PostgreSQL/Util.pm

filter: Statementstmt 98.6% 218 / 221 Branchbran 93.6% 133 / 142 Conditioncond 79.6% 90 / 113 MC/DCmcdc 66.6% 42 / 63 Subroutinesub 100% 26 / 26 Podpod 100% 18 / 18 totaltotal 90.3% 527 / 583 CC 116 SCAR 22.7
CC
116
Cov
93%
number_placeholders
23.0
quote_search_path_entry
13.7
dsn_names_target
10.1
CRAP 9.7
« PostgreSQL/Diff/Trigger.pm ↑ Summary PostgreSQL/Introspect/Normalize.pm »
1 package DBIO::PostgreSQL::Util;
2 # ABSTRACT: Shared PostgreSQL-specific helper functions for DBIO
3  
4 237 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 237 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 237 our $VERSION = '0.900004';
8  
9 237 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 237 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 237 our @EXPORT_OK = qw(
13 quote_ident quote_qualified quote_literal quote_role quote_channel
14 create_schema_if_missing trim_end
15 mask_detail_values mask_error_details
16 type_takes_modifier type_with_modifier
17 number_placeholders
18 quote_dsn_value dsn_names_target
19 deferrable_clause
20 connection_search_path declared_search_path quote_search_path_entry
21 );
22  
23 # the libpq parameters that name where to connect
24 237 my %TARGET_KEY = map { $_ => 1 } qw(dbname host port hostaddr service);
25  
26  
27  
28 # Every key word PostgreSQL does not class as "unreserved": the reserved ones,
29 # the ones that cannot be a function or type name (col_name) and the ones that
30 # can be a function or type name but nothing else (type_func_name) --
31 # pg_get_keywords() with catcode <> 'U'. quote_ident() on the server quotes
32 # exactly these, so a name that is one of them is never written bare.
33 #
34 # The list is PostgreSQL 18's, read from a 18.6 server. A word that is on it
35 # but is no such key word on an older server is quoted there without need and
36 # names the same object. t/91-ddl-quoting-live.t reads pg_get_keywords() from
37 # the server it runs against and fails for every word missing here.
38 237 my %KEYWORD = map { $_ => 1 } qw(
39 all analyse analyze and any array as asc asymmetric authorization between
40 bigint binary bit boolean both case cast char character check coalesce
41 collate collation column concurrently constraint create cross
42 current_catalog current_date current_role current_schema current_time
43 current_timestamp current_user dec decimal default deferrable desc
44 distinct do else end except exists extract false fetch float for foreign
45 freeze from full grant greatest group grouping having ilike in initially
46 inner inout int integer intersect interval into is isnull join json
47 json_array json_arrayagg json_exists json_object json_objectagg
48 json_query json_scalar json_serialize json_table json_value lateral
49 leading least left like limit localtime localtimestamp merge_action
50 national natural nchar none normalize not notnull null nullif numeric
51 offset on only or order out outer overlaps overlay placing position
52 precision primary real references returning right row select session_user
53 setof similar smallint some substring symmetric system_user table
54 tablesample then time timestamp to trailing treat trim true union unique
55 user using values varchar variadic verbose when where window with
56 xmlattributes xmlconcat xmlelement xmlexists xmlforest xmlnamespaces
57 xmlparse xmlpi xmlroot xmlserialize xmltable
58 );
59  
60  
61 5885 ▶sub quote_ident {
Subroutine quote_ident: called CC=7 SCAR=19.5
Pod documented
62 5885 my ($name) = @_;
63 5885 ▶ croak 'an identifier must be a non-empty string'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and length $name 6 5879 5885
Condition100%6/6
defined $name and not ref $name and length $name
!ll&&!rl&&r
515879
defined $name and not ref $name
!ll&&!rl&&r
415880
MC/DC100%3/3
defined $name and not ref $name and length $name
defined $name not ref $name length $name
Truth table100%4/4
A and not(B) and C
A= defined $nameB= not ref $nameC= length $name
ABCresult
0XX0
10X0
1100
1111
64 unless defined $name && !ref $name && length $name;
65 5879 ▶ croak 'an identifier cannot contain a NUL byte'
Branch: 1 branch
BranchTrueFalseTotal
if index($name, "\0") != -1 1 5878 5879
66 if index($name, "\0") >= 0;
67  
68 5878 ▶ return $name if $name =~ /\A[a-z_][a-z0-9_]*\z/ && !$KEYWORD{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /\A[a-z_][a-z0-9_]*\z/ and not $KEYWORD{$name} 5533 345 5878
Condition100%3/3
$name =~ /\A[a-z_][a-z0-9_]*\z/ and not $KEYWORD{$name}
!ll&&!rl&&r
258875533
MC/DC100%2/2
$name =~ /\A[a-z_][a-z0-9_]*\z/ and not $KEYWORD{$name}
$name =~ /\A[a-z_][a-z0-9_]*\z/ not $KEYWORD{$name}
Truth table100%3/3
A and not(B)
A= $name =~ /\A[a-z_][a-z0-9_]*\z/B= not $KEYWORD{$name}
ABresult
0X0
100
111
69  
70 345 (my $quoted = $name) =~ s/"/""/g;
71 345 return qq{"$quoted"};
72 }
73  
74  
75 1747 ▶sub quote_qualified {
Subroutine quote_qualified: called CC=2 SCAR=6.9
Pod documented
76 1747 my (@parts) = @_;
77 1747 ▶ croak 'a qualified name needs at least one part' unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 1 1746 1747
78 1746 return join '.', map { quote_ident($_) } @parts;
79 }
80  
81  
82 340 ▶sub quote_literal {
Subroutine quote_literal: called CC=4 SCAR=13.9
Pod documented
83 340 my ($value) = @_;
84 340 ▶ croak 'a literal must be a defined, plain value'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value 3 337 340
Condition100%3/3
defined $value and not ref $value
!ll&&!rl&&r
21337
MC/DC100%2/2
defined $value and not ref $value
defined $value not ref $value
Truth table100%3/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
85 unless defined $value && !ref $value;
86 337 ▶ croak 'a literal cannot contain a NUL byte'
Branch: 1 branch
BranchTrueFalseTotal
if index($value, "\0") != -1 1 336 337
87 if index($value, "\0") >= 0;
88  
89 336 (my $quoted = $value) =~ s/'/''/g;
90 336 return qq{'$quoted'};
91 }
92  
93  
94 298 ▶sub trim_end {
Subroutine trim_end: called CC=6 SCAR=17.9
Pod documented
95 298 my ($text, $also) = @_;
96 298 ▶ $also //= '';
Condition100%2/2
$also //= ""
l!l
20593
MC/DC100%1/1
$also //= ""
$also
Truth table100%2/2
A
A= $also
Aresult
11
00
97 298 my $end = length $text;
98 298 ▶ while ($end > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end > 0) 592 13 605
99 592 my $char = substr($text, $end - 1, 1);
100 592 ▶ last unless $char =~ /\s/ || ($also ne '' && index($also, $char) >= 0);
Branch: 1 branch
BranchTrueFalseTotal
unless $char =~ /\s/ or $also ne "" and index($also, $char) != -1 285 307 592
Condition100%6/6
$char =~ /\s/ or $also ne "" and index($also, $char) != -1
l!l&&r!l&&!r
102205285
$also ne "" and index($also, $char) != -1
!ll&&!rl&&r
83202205
MC/DC100%3/3
$char =~ /\s/ or $also ne "" and index($also, $char) != -1
$char =~ /\s/ $also ne "" index($also, $char) != -1
Truth table100%4/4
A or B and C
A= $char =~ /\s/B= $also ne ""C= index($also, $char) != -1
ABCresult
1XX1
0111
00X0
0100
101 307 $end--;
102 }
103 298 return substr($text, 0, $end);
104 }
105  
106  
107 23 ▶sub create_schema_if_missing {
Subroutine create_schema_if_missing: called CC=2 SCAR=6.9
Pod documented
108 23 my ($name) = @_;
109 23 my $body = sprintf
110 "BEGIN\n IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname = %s) THEN\n CREATE SCHEMA %s;\n END IF;\nEND",
111 quote_literal($name), quote_ident($name);
112 23 my ($tag, $n) = ('dbio', 0);
113 23 ▶ $tag = 'dbio' . ++$n while index($body, "\$$tag\$") >= 0;
Branch: 1 branch
BranchTrueFalseTotal
index($body, "\$$tag\$") != -1 and do { $tag = 'dbio' . ++$n } 1 23 24
114 23 return "DO \$$tag\$\n$body\n\$$tag\$";
115 }
116  
117 # What stands in for a value in an error text.
118 237 my $MASK = '***';
119  
120 # The start of a field of the server's message, at the start of a line. A field
121 # runs to the next one or to the end of the text.
122 237 my $FIELD_START = qr/^(?:DETAIL: |HINT: |CONTEXT: |QUERY: |LINE \d+: |STATEMENT:|LOCATION:)/m;
123  
124 # Where the quoted value of a primary text ends: at the end of the text, which
125 # DBI may follow with the place of its caller.
126 237 my $VALUE_END = qr/(?=(?: at \S+ line \d+\.?)?\s*\z)/;
127  
128 # The sentences of the primary text that end in the quoted value; what stands
129 # in front of the quote is kept.
130 237 my @SENTENCE_ENDING_IN_VALUE = (
131 qr/\binvalid input syntax for (?:type )?[^:\n]+: /,
132 qr/\binvalid input value for enum [^\n]*?: /,
133 qr/\bout of range: /,
134 qr/\bmalformed (?:array|record|range|multirange) literal: /,
135 qr/\bin ts(?:vector|query): /,
136 qr/\binvalid (?:hexadecimal|binary) digit: /,
137 qr/\bunrecognized encoding: /,
138 qr/\binvalid (?:destination |source )?encoding name /,
139 qr/\bunrecognized time zone name: /,
140 qr/\binvalid value for parameter "[^"\n]*": /,
141 qr/\binvalid regular expression option: /,
142 qr/\binvalid configuration parameter name /,
143 );
144  
145 237 my $IDENT = qr/[A-Za-z_\x{80}-\x{10FFFF}][\w\$\x{80}-\x{10FFFF}]*/;
146  
147 # "(cols)": a list of plain identifiers, or an expression of names, dots,
148 # colons, commas and balanced parentheses. Anything else (a quoted name, an
149 # "=") could hold a part of the value and is not read.
150 34 ▶sub _readable_columns {
Subroutine _readable_columns: called CC=8 SCAR=20.9
151 34 my ($cols) = @_;
152 34 ▶ return 1 if $cols =~ /\A$IDENT(?:, $IDENT)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $cols =~ /\A$IDENT(?:, $IDENT)*\z/ 30 4 34
153 4 ▶ return 0 unless $cols =~ /\A[\w\$\x{80}-\x{10FFFF} ,:.()]+\z/ && $cols =~ /\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cols =~ /\A[\w\$\x{80}-\x{10FFFF} ,:.()]+\z/ and $cols =~ /\(/ 1 3 4
Condition66%2/3
$cols =~ /\A[\w\$\x{80}-\x{10FFFF} ,:.()]+\z/ and $cols =~ /\(/
!ll&&!rl&&r
103
MC/DC50%1/2
$cols =~ /\A[\w\$\x{80}-\x{10FFFF} ,:.()]+\z/ and $cols =~ /\(/
$cols =~ /\A[\w\$\x{80}-\x{10FFFF} ,:.()]+\z/ $cols =~ /\(/
Truth table66%2/3
A and B
A= $cols =~ /\A[\w\$\x{80}-\x{10FFFF} ,:.()]+\z/B= $cols =~ /\(/
ABresult
0X0
100
111
154 3 my $depth = 0;
155 3 for my $c (split //, $cols) {
156 54 ▶ $depth++ if $c eq '(';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "(" 3 51 54
157 54 ▶ $depth-- if $c eq ')';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq ")" 3 51 54
158 54 ▶ return 0 if $depth < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth < 0 0 54 54
159 }
160 3 return $depth == 0;
161 }
162  
163 # "(cols)=(value)": the columns where they can be read, the value never.
164 34 ▶sub _mask_key_text {
Subroutine _mask_key_text: called CC=3 SCAR=11.0
165 34 my ($key) = @_;
166 34 ▶ return $MASK unless $key =~ /\A\(([^=]*?)\)=\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /\A\(([^=]*?)\)=\(/ 0 34 34
167 34 my $cols = $1;
168 34 ▶ return _readable_columns($cols) ? "($cols)=($MASK)" : $MASK;
Branch: 1 branch
BranchTrueFalseTotal
_readable_columns($cols) ? : 33 1 34
169 }
170  
171 # The sentences of the primary text that hold a value.
172 130 ▶sub _mask_primary {
Subroutine _mask_primary: called CC=2 SCAR=6.9
173 130 my ($text) = @_;
174  
175 130 $text =~ s/($_)"(.*)"$VALUE_END/$1"$MASK"/s for @SENTENCE_ENDING_IN_VALUE;
176  
177 130 $text =~ s/(\bvalue )"(.*)"( is out of range for type )/$1"$MASK"$3/s;
178 130 $text =~ s/(\binvalid value )"(.*)"( for ")/$1"$MASK"$3/s;
179 130 $text =~ s/(?<!\w)"(.*)"( is not a valid (?:binary|hexadecimal) digit)/"$MASK"$2/s;
180 130 $text =~ s/(\btime zone )"(.*)"( not recognized)/$1"$MASK"$3/s;
181 130 $text =~ s/(\bsyntax error at or near )"(.*)"( of jsonpath input)/$1"$MASK"$3/s;
182  
183 # invalid byte sequence for encoding "UTF8": 0xc3 0x28
184 # character with byte sequence 0xe2 0x82 in encoding "UTF8" has no equivalent ...
185 130 $text =~ s/(\bbyte sequence for encoding "[^"\n]*": )0x[0-9A-Fa-f]{2}(?: 0x[0-9A-Fa-f]{2})*/$1$MASK/g;
186 130 $text =~ s/(\bbyte sequence )0x[0-9A-Fa-f]{2}(?: 0x[0-9A-Fa-f]{2})*/$1$MASK/g;
187  
188 130 return $text;
189 }
190  
191 # The lines of the context that hold a value.
192 65 ▶sub _mask_context {
Subroutine _mask_context: called CC=1 SCAR=0.0
193 65 my ($text) = @_;
194  
195 # JSON data, line 1: {"a":x...
196 65 $text =~ s/(\bJSON data, line \d+: )[^\n]*/$1$MASK/g;
197  
198 # COPY t, line 1, column c: "value"
199 65 $text =~ s/(\bCOPY [^\n,]+, line \d+(?:, column [^:\n]+)?: )"(.*)"/$1"$MASK"/s;
200  
201 # unnamed portal with parameters: $1 = 'a''b', $2 = NULL
202 65 $text =~ s/(\$\d+ = )'(?:[^']|'')*'/$1'$MASK'/g;
203  
204 65 return $text;
205 }
206  
207  
208 70 ▶sub mask_detail_values {
Subroutine mask_detail_values: called CC=3 SCAR=11.0
Pod documented
209 70 my ($text) = @_;
210 70 ▶ return $text unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 69 70
211  
212 69 my $tail = qr/ (?:already exists|is duplicated|is not present in table|is still referenced from table|conflicts with existing key)/;
213 69 ▶ unless ($text =~ s/\bKey (\(.*\)=\(.*\))(?=$tail)/'Key ' . _mask_key_text($1)/se) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($text =~ s/\bKey (\(.*\)=\(.*\))(?=$tail)/'Key ' . _mask_key_text($1);/es) 37 32 69
214 0 $text =~ s/\bKey (\(.*\)=\(.*\))(?=[^)]*\z)/'Key ' . _mask_key_text($1)/se;
215 }
216 69 $text =~ s/(\bconflicts with existing key )(\(.*\)=\(.*\))(?=[^)]*\z)/$1 . _mask_key_text($2)/se;
217  
218 69 $text =~ s/(\bFailing row contains \().*\)/$1$MASK)/s;
219 69 $text =~ s/(\bPartition key of the failing row contains \([^=]*?\) = \().*\)/$1$MASK)/s;
220  
221 69 $text =~ s/(\bToken )"(.*)"( is invalid\.)/$1"$MASK"$3/s;
222 69 $text =~ s/(, but found )"(.*)"(\.?)/$1"$MASK"$3/s;
223 69 $text =~ s/(\bEscape sequence )"(.*)"( is invalid\.)/$1"$MASK"$3/s;
224 69 $text =~ s/(\bUnexpected )"(.*)"( character\.)/$1"$MASK"$3/s;
225 69 $text =~ s/(\bCharacter with value )0x[0-9A-Fa-f]+/$1$MASK/g;
226  
227 69 $text =~ s/((?:\A|\bDETAIL: )line \d+: ).*?(\n?)\z/$1$MASK$2/s;
228  
229 69 return $text;
230 }
231  
232  
233 132 ▶sub mask_error_details {
Subroutine mask_error_details: called CC=8 SCAR=20.8
Pod documented
234 132 my ($message) = @_;
235 132 ▶ return $message unless defined $message && length $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and length $message 2 130 132
Condition100%3/3
defined $message and length $message
!ll&&!rl&&r
11130
MC/DC100%2/2
defined $message and length $message
defined $message length $message
Truth table100%3/3
A and B
A= defined $messageB= length $message
ABresult
0X0
100
111
236  
237 130 my $tail = '';
238 130 ▶ ($message, $tail) = ($1, $2) if $message =~ /\A(.*)(\s\[for Statement\b.*)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
if $message =~ /\A(.*)(\s\[for Statement\b.*)\z/s 2 128 130
239  
240 130 my @fields = split /(?=$FIELD_START)/, $message;
241 130 for my $field (@fields) {
242 259 ▶ if ($field =~ /\ADETAIL: /) { $field = mask_detail_values($field) }
Branch: 3 branches
BranchTrueFalseTotal
if ($field =~ /\ADETAIL: /) { } 57 202 259
elsif ($field =~ /\ACONTEXT: /) { } 65 137 202
elsif ($field =~ /$FIELD_START/) { } 7 130 137
243 65 elsif ($field =~ /\ACONTEXT: /) { $field = _mask_context($field) }
244 elsif ($field =~ $FIELD_START) { }
245 130 else { $field = _mask_primary($field) }
246 }
247 130 return join('', @fields) . $tail;
248 }
249  
250  
251 237 my %ROLE_KEYWORD = map { $_ => 1 } qw(
252 public current_user current_role session_user
253 );
254  
255 28 ▶sub quote_role {
Subroutine quote_role: called CC=4 SCAR=13.9
Pod documented
256 28 my ($role) = @_;
257 return $role
258 28 ▶ if defined $role && !ref $role && $ROLE_KEYWORD{lc $role};
Branch: 1 branch
BranchTrueFalseTotal
if defined $role and not ref $role and $ROLE_KEYWORD{lc $role} 13 15 28
Condition83%5/6
defined $role and not ref $role and $ROLE_KEYWORD{lc $role}
!ll&&!rl&&r
11413
defined $role and not ref $role
!ll&&!rl&&r
1027
MC/DC66%2/3
defined $role and not ref $role and $ROLE_KEYWORD{lc $role}
defined $role not ref $role $ROLE_KEYWORD{lc $role}
Truth table75%3/4
A and not(B) and C
A= defined $roleB= not ref $roleC= $ROLE_KEYWORD{lc $role}
ABCresult
0XX0
10X0
1100
1111
259 15 return quote_ident($role);
260 }
261  
262  
263 148 ▶sub deferrable_clause {
Subroutine deferrable_clause: called CC=5 SCAR=16.3
Pod documented
264 148 my ($deferrable, $initially_deferred) = @_;
265 148 ▶ croak "initially_deferred needs deferrable: PostgreSQL has no constraint that is INITIALLY DEFERRED and NOT DEFERRABLE"
Branch: 1 branch
BranchTrueFalseTotal
if $initially_deferred and not $deferrable 0 148 148
Condition66%2/3
$initially_deferred and not $deferrable
!ll&&!rl&&r
138100
MC/DC0%0/2
$initially_deferred and not $deferrable
$initially_deferred not $deferrable
Truth table66%2/3
A and not(B)
A= $initially_deferredB= not $deferrable
ABresult
0X0
100
111
266 if $initially_deferred && !$deferrable;
267 148 ▶ return '' unless $deferrable;
Branch: 1 branch
BranchTrueFalseTotal
unless $deferrable 119 29 148
268 29 ▶ return $initially_deferred ? ' DEFERRABLE INITIALLY DEFERRED' : ' DEFERRABLE INITIALLY IMMEDIATE';
Branch: 1 branch
BranchTrueFalseTotal
$initially_deferred ? : 10 19 29
269 }
270  
271  
272 # Every type that takes a type modifier, under every name it can be declared
273 # by. The built-in ones are the ten of pg_catalog.pg_type with a typmodin --
274 # bpchar, varchar, bit, varbit, numeric, time, timetz, timestamp, timestamptz,
275 # interval -- each under its catalog name, the name format_type() gives it and
276 # the further spellings the grammar has for it. Of the interval forms with
277 # fields, the ones that end in the seconds take the precision. vector, halfvec
278 # and sparsevec are pgvector's.
279 #
280 # Read from a PostgreSQL 18.6 server. t/98-ddl-type-modifier-live.t reads
281 # pg_type from the server it runs against, fails for every built-in type with
282 # a typmodin that is missing here, and has the server take every name below
283 # with a modifier.
284 237 my %TAKES_MODIFIER = map { $_ => 1 } (
285 'character', 'char', 'bpchar', 'nchar', 'national character', 'national char',
286 'character varying', 'char varying', 'varchar', 'nchar varying',
287 'national character varying', 'national char varying',
288 'bit', 'bit varying', 'varbit',
289 'numeric', 'decimal', 'dec',
290 'time', 'time without time zone', 'timetz', 'time with time zone',
291 'timestamp', 'timestamp without time zone', 'timestamptz', 'timestamp with time zone',
292 'interval', 'interval second', 'interval day to second',
293 'interval hour to second', 'interval minute to second',
294 'vector', 'halfvec', 'sparsevec',
295 );
296  
297  
298 700 ▶sub type_takes_modifier {
Subroutine type_takes_modifier: called CC=4 SCAR=13.9
Pod documented
299 700 my ($type) = @_;
300 700 ▶ return 0 unless defined $type && !ref $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and not ref $type 1 699 700
Condition66%2/3
defined $type and not ref $type
!ll&&!rl&&r
10699
MC/DC50%1/2
defined $type and not ref $type
defined $type not ref $type
Truth table66%2/3
A and not(B)
A= defined $typeB= not ref $type
ABresult
0X0
100
111
301 699 (my $name = lc $type) =~ s/\s+/ /g;
302 699 $name =~ s/\A | \z//g;
303 699 ▶ return $TAKES_MODIFIER{$name} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$TAKES_MODIFIER{$name} ? : 154 545 699
304 }
305  
306  
307 1423 ▶sub type_with_modifier {
Subroutine type_with_modifier: called CC=10 SCAR=23.0
Pod documented
308 1423 my ($type, $size, $context) = @_;
309 1423 ▶ my $where = defined $context && length $context ? " for column $context" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $context && length $context ? : 513 910 1423
Condition66%2/3
defined $context and length $context
!ll&&!rl&&r
9100513
MC/DC50%1/2
defined $context and length $context
defined $context length $context
Truth table66%2/3
A and B
A= defined $contextB= length $context
ABresult
0X0
100
111
310 1423 ▶ croak 'a size must be a scalar or an arrayref, got a ' . ref($size) . ' reference' . $where
Branch: 1 branch
BranchTrueFalseTotal
if ref $size and ref $size ne "ARRAY" 6 1417 1423
Condition100%3/3
ref $size and ref $size ne "ARRAY"
!ll&&!rl&&r
1389286
MC/DC100%2/2
ref $size and ref $size ne "ARRAY"
ref $size ref $size ne "ARRAY"
Truth table100%3/3
A and B
A= ref $sizeB= ref $size ne "ARRAY"
ABresult
0X0
100
111
311 if ref $size && ref $size ne 'ARRAY';
312  
313 1417 ▶ my @parts = grep { defined && length } ref $size ? @$size : $size;
Branch: 1 branch
BranchTrueFalseTotal
ref $size ? : 28 1389 1417
Condition100%3/3
defined $_ && length $_
!ll&&!rl&&r
12611178
MC/DC100%2/2
defined $_ && length $_
defined $_ length $_
Truth table100%3/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
314 1417 ▶ return $type unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 1261 156 1417
315  
316 156 my $text = join ',', @parts;
317 156 ▶ croak 'a size must be digits, or two numbers of digits joined by a comma' . $where
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\A\d+(?:,\d+)?\z/ 10 146 156
318 unless $text =~ /\A\d+(?:,\d+)?\z/;
319  
320 146 my $modifier = "($text)";
321 146 ▶ return $1 . $modifier . $2
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /\A(\s*\w+)(\s+with(?:out)?\s+time\s+zone\s*)\z/i 22 124 146
322 if $type =~ /\A(\s*\w+)(\s+with(?:out)?\s+time\s+zone\s*)\z/i;
323 124 return $type . $modifier;
324 }
325  
326  
327 18 ▶sub quote_channel {
Subroutine quote_channel: called CC=6 SCAR=17.9
Pod documented
328 18 my ($channel) = @_;
329 18 ▶ croak 'a LISTEN channel name must be a non-empty string'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $channel and not ref $channel and length $channel 3 15 18
Condition100%6/6
defined $channel and not ref $channel and length $channel
!ll&&!rl&&r
2115
defined $channel and not ref $channel
!ll&&!rl&&r
1116
MC/DC100%3/3
defined $channel and not ref $channel and length $channel
defined $channel not ref $channel length $channel
Truth table100%4/4
A and not(B) and C
A= defined $channelB= not ref $channelC= length $channel
ABCresult
0XX0
10X0
1100
1111
330 unless defined $channel && !ref $channel && length $channel;
331 15 ▶ croak 'a LISTEN channel name cannot contain a NUL byte'
Branch: 1 branch
BranchTrueFalseTotal
if index($channel, "\0") != -1 1 14 15
332 if index($channel, "\0") >= 0;
333  
334 14 my $bytes = $channel;
335 14 utf8::encode($bytes);
336 14 ▶ croak sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if length $bytes > 63 3 11 14
337 "LISTEN channel name '%s' is %d bytes long; PostgreSQL channel names are "
338 . 'identifiers of at most 63 bytes (NAMEDATALEN - 1) -- LISTEN would '
339 . 'truncate it and pg_notify refuses it',
340 $channel, length $bytes,
341 ) if length $bytes > 63;
342  
343 11 (my $quoted = $channel) =~ s/"/""/g;
344 11 return qq{"$quoted"};
345 }
346  
347  
348 # What number_placeholders() takes from PostgreSQL's scanner:
349 # * a stretch of SQL with no '?' in it and nothing left open: characters that
350 # start none of the constructs number_placeholders() looks at, closed '...'
351 # and "..." runs (a doubled quote reads as two runs back to back, which is
352 # the same text), and words. A word -- identifier or keyword -- is taken as
353 # a whole, so a '$' inside it opens no dollar quote; only the word E before
354 # a quote is left standing, for the escape string it opens;
355 # * the delimiter of a dollar quote;
356 # * the whitespace behind a closing quote that continues the literal: a
357 # newline in it, '--' comments allowed.
358 # Each is anchored at the scan position itself and used as a whole pattern:
359 # that way perl takes the compiled pattern as it is on every match.
360 237 my $PG_WORD = qr/[A-Za-z_\x80-\x{10FFFF}][A-Za-z_0-9\$\x80-\x{10FFFF}]*/;
361 237 my $PG_SQL_STRETCH = qr/\G(?:[^A-Za-z_\x80-\x{10FFFF}'"\$?\/-]+|(?![eE]')$PG_WORD|'[^']*+'|"[^"]*+")++/;
362 237 my $PG_DOLLAR_DELIMITER = qr/\G(\$(?:[A-Za-z_\x80-\x{10FFFF}][A-Za-z_0-9\x80-\x{10FFFF}]*)?\$)/;
363 237 my $PG_STRING_CONTINUES = qr/\G(?:[ \t\f\x0B]|--[^\n\r]*)*[\n\r](?:[ \t\n\r\f\x0B]+|--[^\n\r]*[\n\r])*'/;
364  
365  
366 144 ▶sub number_placeholders {
Subroutine number_placeholders: called CC=23 SCAR=31.4
Pod documented
367 144 my ($sql) = @_;
368 144 ▶ croak 'a statement must be a defined, plain value'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sql and not ref $sql 6 138 144
Condition100%3/3
defined $sql and not ref $sql
!ll&&!rl&&r
33138
MC/DC100%2/2
defined $sql and not ref $sql
defined $sql not ref $sql
Truth table100%3/3
A and not(B)
A= defined $sqlB= not ref $sql
ABresult
0X0
100
111
369 unless defined $sql && !ref $sql;
370  
371 138 my $len = length $sql;
372 138 my $out = '';
373 138 my $copied = 0; # $sql up to this offset is in $out
374 138 my $n = 0;
375 138 my $dollar = 0; # a $N written in the statement (outside text)
376  
377 138 pos($sql) = 0;
378 138 ▶ while (pos($sql) < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $sql < $len) 604 138 742
379 604 ▶ if ($sql =~ /$PG_SQL_STRETCH/gc) {
Branch: 8 branches
BranchTrueFalseTotal
if ($sql =~ /$PG_SQL_STRETCH/cg) { } 333 271 604
elsif ($sql =~ /\G\?/cg) { } 189 82 271
elsif ($sql =~ /\G\$[0-9]+/cg) { } 20 62 82
elsif ($sql =~ /\G[eE]'/cg) { } 13 49 62
elsif ($sql =~ /\G--[^\n\r]*/cg) { } 11 38 49
elsif ($sql =~ m[\G/\*]cg) { } 13 25 38
elsif ($sql =~ /$PG_DOLLAR_DELIMITER/cg) { } 15 10 25
elsif ($sql =~ /\G['"]/cg) { } 2 8 10
380 # nothing to number in it
381 }
382 elsif ($sql =~ /\G\?/gc) {
383 189 my $at = pos($sql) - 1;
384 # '\?' is a literal question mark: the backslash goes, no placeholder
385 189 ▶ if ($at > 0 && substr($sql, $at - 1, 1) eq '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($at > 0 and substr($sql, $at - 1, 1) eq "\\") 31 158 189
Condition66%2/3
$at > 0 and substr($sql, $at - 1, 1) eq "\\"
!ll&&!rl&&r
015831
MC/DC50%1/2
$at > 0 and substr($sql, $at - 1, 1) eq "\\"
$at > 0 substr($sql, $at - 1, 1) eq "\\"
Truth table66%2/3
A and B
A= $at > 0B= substr($sql, $at - 1, 1) eq "\\"
ABresult
0X0
100
111
386 31 $out .= substr($sql, $copied, $at - 1 - $copied) . '?';
387 31 $copied = $at + 1;
388 31 next;
389 }
390 158 $out .= substr($sql, $copied, $at - $copied) . '$' . ++$n;
391 158 $copied = $at + 1;
392 }
393 elsif ($sql =~ /\G\$[0-9]+/gc) {
394 20 $dollar++;
395 }
396 elsif ($sql =~ /\G[eE]'/gc) {
397 # an escape string, and what continues it on the following lines
398 13 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ /$PG_STRING_CONTINUES/cg and do { do { $sql =~ /\G(?:[^\\']+|\\.|'')*+'/cgs or pos $sql = $len } } 2 13 15
399 15 ▶ $sql =~ /\G(?:[^\\']+|\\.|'')*+'/gcs or pos($sql) = $len;
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ /\G(?:[^\\']+|\\.|'')*+'/cgs or pos $sql = $len 13 2 15
400 } while ($sql =~ /$PG_STRING_CONTINUES/gc);
401 }
402 elsif ($sql =~ /\G--[^\n\r]*/gc) {
403 # a comment to the end of the line
404 }
405 elsif ($sql =~ m{\G/\*}gc) {
406 13 my $depth = 1;
407 13 ▶ while ($depth && $sql =~ m{\G.*?(/\*|\*/)}gcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth and $sql =~ m[\G.*?(/\*|\*/)]cgs) 25 13 38
Condition100%3/3
$depth and $sql =~ m[\G.*?(/\*|\*/)]cgs
!ll&&!rl&&r
11225
MC/DC100%2/2
$depth and $sql =~ m[\G.*?(/\*|\*/)]cgs
$depth $sql =~ m[\G.*?(/\*|\*/)]cgs
Truth table100%3/3
A and B
A= $depthB= $sql =~ m[\G.*?(/\*|\*/)]cgs
ABresult
0X0
100
111
408 25 ▶ $depth += $1 eq '/*' ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '/*' ? : 7 18 25
409 }
410 13 ▶ pos($sql) = $len if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 2 11 13
411 }
412 elsif ($sql =~ /$PG_DOLLAR_DELIMITER/gc) {
413 15 my $end = index($sql, $1, pos($sql));
414 15 ▶ pos($sql) = $end < 0 ? $len : $end + length $1;
Branch: 1 branch
BranchTrueFalseTotal
$end < 0 ? : 1 14 15
415 }
416 elsif ($sql =~ /\G['"]/gc) {
417 2 pos($sql) = $len; # a quote the stretch did not close: never closed
418 }
419 else {
420 8 pos($sql) = pos($sql) + 1; # a '-', '/' or '$' that opens nothing
421 }
422 }
423  
424 138 ▶ croak 'refusing to number placeholders: the statement mixes $N and ?; '
Branch: 1 branch
BranchTrueFalseTotal
if $dollar and $n 8 130 138
Condition100%3/3
$dollar and $n
!ll&&!rl&&r
12378
MC/DC100%2/2
$dollar and $n
$dollar $n
Truth table100%3/3
A and B
A= $dollarB= $n
ABresult
0X0
100
111
425 . 'write one style, and \\? for a literal question mark'
426 if $dollar && $n;
427  
428 130 return $out . substr($sql, $copied);
429 }
430  
431  
432  
433 15 ▶sub quote_dsn_value {
Subroutine quote_dsn_value: called CC=6 SCAR=20.9
Pod documented
434 15 my ($value) = @_;
435 15 ▶ croak 'a connection parameter value must be a defined, plain value'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value 1 14 15
Condition66%2/3
defined $value and not ref $value
!ll&&!rl&&r
1014
MC/DC50%1/2
defined $value and not ref $value
defined $value not ref $value
Truth table66%2/3
A and not(B)
A= defined $valueB= not ref $value
ABresult
0X0
100
111
436 unless defined $value && !ref $value;
437 14 ▶ croak 'a connection parameter value cannot contain a NUL byte'
Branch: 1 branch
BranchTrueFalseTotal
if index($value, "\0") != -1 0 14 14
438 if index($value, "\0") >= 0;
439  
440 14 ▶ return $value if length $value && $value !~ /[\s;'\\="]/;
Branch: 1 branch
BranchTrueFalseTotal
if length $value and not $value =~ /[\s;'\\="]/ 14 0 14
Condition33%1/3
length $value and not $value =~ /[\s;'\\="]/
!ll&&!rl&&r
0014
MC/DC0%0/2
length $value and not $value =~ /[\s;'\\="]/
length $value not $value =~ /[\s;'\\="]/
Truth table33%1/3
A and not(B)
A= length $valueB= not $value =~ /[\s;'\\="]/
ABresult
0X0
100
111
441  
442 0 (my $quoted = $value) =~ s/(['\\=])/\\$1/g;
443 0 return qq{'$quoted'};
444 }
445  
446  
447 96 ▶sub dsn_names_target {
Subroutine dsn_names_target: called CC=10 SCAR=23.1
Pod documented
448 96 my ($dsn) = @_;
449 96 ▶ croak 'a DSN must be a defined, plain value'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dsn and not ref $dsn 0 96 96
Condition33%1/3
defined $dsn and not ref $dsn
!ll&&!rl&&r
0096
MC/DC0%0/2
defined $dsn and not ref $dsn
defined $dsn not ref $dsn
Truth table33%1/3
A and not(B)
A= defined $dsnB= not ref $dsn
ABresult
0X0
100
111
450 unless defined $dsn && !ref $dsn;
451  
452 96 $dsn =~ s/\Adbi:\w*(?:\([^)]*\))?://i;
453  
454 # libpq takes a URI for its whole connection string; what it names is its
455 # own business, and this does not judge it.
456 96 ▶ return 1 if $dsn =~ m{\A\s*postgres(?:ql)?://}i;
Branch: 1 branch
BranchTrueFalseTotal
if $dsn =~ m[\A\s*postgres(?:ql)?://]i 3 93 96
457  
458 93 ▶ while ($dsn =~ /\G[\s;]*([^\s;='\\]+)[\s;]*=[\s;]*((?:'(?:[^'\\]|\\.)*'|(?:\\.|[^\s;'\\])+)?)/gcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn =~ /\G[\s;]*([^\s;='\\]+)[\s;]*=[\s;]*((?:'(?:[^'\\]|\\.)*'|(?:\\.|[^\s;'\\])+)?)/cgs) 80 56 136
459 80 my ($key, $value) = (lc $1, $2);
460 80 ▶ $key = 'dbname' if $key eq 'db' || $key eq 'database';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "db" or $key eq "database" 4 76 80
Condition100%3/3
$key eq "db" or $key eq "database"
l!l&&r!l&&!r
2276
MC/DC100%2/2
$key eq "db" or $key eq "database"
$key eq "db" $key eq "database"
Truth table100%3/3
A or B
A= $key eq "db"B= $key eq "database"
ABresult
1X1
011
000
461 80 ▶ next unless $TARGET_KEY{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $TARGET_KEY{$key} 28 52 80
462 52 ▶ return 1 if length $value && $value ne q{''};
Branch: 1 branch
BranchTrueFalseTotal
if length $value and $value ne "''" 37 15 52
Condition100%3/3
length $value and $value ne "''"
!ll&&!rl&&r
10537
MC/DC100%2/2
length $value and $value ne "''"
length $value $value ne "''"
Truth table100%3/3
A and B
A= length $valueB= $value ne "''"
ABresult
0X0
100
111
463 }
464  
465 56 return 0;
466 }
467  
468  
469  
470 48 ▶sub connection_search_path {
Subroutine connection_search_path: called CC=8 SCAR=20.8
Pod documented
471 48 my ($namespace, $extensions, $extension_schemas, $activation, $control) = @_;
472 48 ▶ $extension_schemas //= {};
Condition50%1/2
$extension_schemas //= {}
l!l
480
MC/DC0%0/1
$extension_schemas //= {}
$extension_schemas
Truth table50%1/2
A
A= $extension_schemas
Aresult
11
00
473 48 ▶ $control //= {};
Condition100%2/2
$control //= {}
l!l
3810
MC/DC100%1/1
$control //= {}
$control
Truth table100%2/2
A
A= $control
Aresult
11
00
474  
475 50 ▶ my @schemas = map { $extension_schemas->{$_} // $control->{$_} // 'public' }
Condition100%5/5
($extension_schemas->{$_} // $control->{$_}) // "public"
l!l
2228
$extension_schemas->{$_} // $control->{$_}
l!l&&r!l&&!r
19328
MC/DC100%2/2
($extension_schemas->{$_} // $control->{$_}) // "public"
$extension_schemas->{$_} $control->{$_}
Truth table100%3/3
A // B
A= $extension_schemas->{$_}B= $control->{$_}
ABresult
1X1
011
000
476 48 ▶ @{ $extensions // [] };
Condition50%1/2
$extensions // []
l!l
480
MC/DC0%0/1
$extensions // []
$extensions
Truth table50%1/2
A
A= $extensions
Aresult
11
00
477 48 ▶ @schemas = ('public') unless @schemas;
Branch: 1 branch
BranchTrueFalseTotal
unless @schemas 14 34 48
478  
479 48 my %seen;
480 48 ▶ return grep { !$seen{$_}++ } $namespace, @schemas, @{ $activation // [] };
Condition100%2/2
$activation // []
l!l
417
MC/DC100%1/1
$activation // []
$activation
Truth table100%2/2
A
A= $activation
Aresult
11
00
481 }
482  
483  
484 14 ▶sub declared_search_path {
Subroutine declared_search_path: called CC=5 SCAR=16.6
Pod documented
485 14 my ($declared, $extensions, $extension_schemas, $activation) = @_;
486 14 ▶ $extension_schemas //= {};
Condition50%1/2
$extension_schemas //= {}
l!l
140
MC/DC0%0/1
$extension_schemas //= {}
$extension_schemas
Truth table50%1/2
A
A= $extension_schemas
Aresult
11
00
487  
488 14 ▶ my @ext = grep { defined } map { $extension_schemas->{$_} } @{ $extensions // [] };
Condition50%1/2
$extensions // []
l!l
140
MC/DC0%0/1
$extensions // []
$extensions
Truth table50%1/2
A
A= $extensions
Aresult
11
00
489  
490 14 my %seen;
491 14 ▶ return grep { !$seen{$_}++ } @{ $declared // [] }, @ext, @{ $activation // [] };
Condition50%2/4
$declared // []
l!l
140
$activation // []
l!l
140
MC/DC0%0/1
$declared // []
$declared
MC/DC0%0/1
$activation // []
$activation
Truth table50%1/2
A
A= $declared
Aresult
11
00
Truth table50%1/2
A
A= $activation
Aresult
11
00
492 }
493  
494  
495 32 ▶sub quote_search_path_entry {
Subroutine quote_search_path_entry: called CC=10 SCAR=26.2
Pod documented
496 32 my ($entry) = @_;
497 32 ▶ croak sprintf(
Branch: 3 branches
BranchTrueFalseTotal
unless defined $entry and not ref $entry and length $entry and index($entry, "\0") == -1 1 31 32
defined $entry && !ref($entry) ? : 1 0 1
defined $entry ? : 0 0 0
Condition50%6/12
defined $entry and not ref $entry and length $entry and index($entry, "\0") == -1
!ll&&!rl&&r
1031
defined $entry and not ref $entry and length $entry
!ll&&!rl&&r
0131
defined $entry and not ref $entry
!ll&&!rl&&r
0032
defined $entry and not ref $entry
!ll&&!rl&&r
001
MC/DC25%1/4
defined $entry and not ref $entry and length $entry and index($entry, "\0") == -1
defined $entry not ref $entry length $entry index($entry, "\0") == -1
Truth table40%2/5
A and not(B) and C and D
A= defined $entryB= not ref $entryC= length $entryD= index($entry, "\0") == -1
ABCDresult
0XXX0
10XX0
110X0
11100
11111
498 'pg_search_path entry %s is not a name. Entries are schema names; write each as a plain string',
499 defined $entry && !ref $entry ? "'$entry'" : (defined $entry ? 'a reference' : 'undef'))
500 unless defined $entry && !ref $entry && length $entry && index($entry, "\0") < 0;
501 31 ▶ return '"$user"' if $entry eq '$user';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "\$user" 2 29 31
502 29 ▶ croak sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /\A"/ 1 28 29
503 'pg_search_path entry %s is already quoted; write the name without quotes (%s)',
504 $entry, substr($entry, 1, -1))
505 if $entry =~ /\A"/;
506 28 return quote_ident($entry);
507 }
508  
509  
510 237 1;
511  
512 __END__
« PostgreSQL/Diff/Trigger.pm ↑ Summary PostgreSQL/Introspect/Normalize.pm »