blib/lib/DBIO/PostgreSQL/Util.pm

filter: Statementstmt 99.0% 327 / 330 Branchbran 92.7% 193 / 208 Conditioncond 84.5% 148 / 175 MC/DCmcdc 74.4% 73 / 98 Subroutinesub 100% 42 / 42 Podpod 100% 19 / 19 totaltotal 91.9% 802 / 872 CC 172 SCAR 21.8
CC
172
Cov
94%
number_placeholders
23.0
quote_search_path_entry
13.7
_mask_error_details
13.0
CRAP 8.9
« blib/lib/DBIO/PostgreSQL/Diff/Sequence.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Introspect/Parse.pm »
1 package DBIO::PostgreSQL::Util;
2 # ABSTRACT: Shared PostgreSQL-specific helper functions for DBIO
3  
4 217 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 217 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 217 our $VERSION = '0.900007';
8  
9 217 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 217 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 217 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 217 our @EXPORT_OK = qw(
14 quote_ident quote_qualified quote_literal quote_role quote_channel
15 create_schema_if_missing trim_end
16 mask_detail_values mask_error_details error_sqlstate
17 type_takes_modifier type_with_modifier
18 number_placeholders
19 quote_dsn_value dsn_names_target
20 deferrable_clause
21 connection_search_path declared_search_path quote_search_path_entry
22 );
23  
24 # the libpq parameters that name where to connect
25 217 my %TARGET_KEY = map { $_ => 1 } qw(dbname host port hostaddr service);
26  
27  
28  
29 # Every key word PostgreSQL does not class as "unreserved": the reserved ones,
30 # the ones that cannot be a function or type name (col_name) and the ones that
31 # can be a function or type name but nothing else (type_func_name) --
32 # pg_get_keywords() with catcode <> 'U'. quote_ident() on the server quotes
33 # exactly these, so a name that is one of them is never written bare.
34 #
35 # The list is PostgreSQL 18's, read from a 18.6 server. A word that is on it
36 # but is no such key word on an older server is quoted there without need and
37 # names the same object. t/91-ddl-quoting-live.t reads pg_get_keywords() from
38 # the server it runs against and fails for every word missing here.
39 217 my %KEYWORD = map { $_ => 1 } qw(
40 all analyse analyze and any array as asc asymmetric authorization between
41 bigint binary bit boolean both case cast char character check coalesce
42 collate collation column concurrently constraint create cross
43 current_catalog current_date current_role current_schema current_time
44 current_timestamp current_user dec decimal default deferrable desc
45 distinct do else end except exists extract false fetch float for foreign
46 freeze from full grant greatest group grouping having ilike in initially
47 inner inout int integer intersect interval into is isnull join json
48 json_array json_arrayagg json_exists json_object json_objectagg
49 json_query json_scalar json_serialize json_table json_value lateral
50 leading least left like limit localtime localtimestamp merge_action
51 national natural nchar none normalize not notnull null nullif numeric
52 offset on only or order out outer overlaps overlay placing position
53 precision primary real references returning right row select session_user
54 setof similar smallint some substring symmetric system_user table
55 tablesample then time timestamp to trailing treat trim true union unique
56 user using values varchar variadic verbose when where window with
57 xmlattributes xmlconcat xmlelement xmlexists xmlforest xmlnamespaces
58 xmlparse xmlpi xmlroot xmlserialize xmltable
59 );
60  
61  
62 12605 ▶sub quote_ident {
Subroutine quote_ident: called CC=7 SCAR=19.5
Pod documented
63 12605 my ($name) = @_;
64 12605 ▶ croak 'an identifier must be a non-empty string'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and not ref $name and length $name 6 12599 12605
Condition100%6/6
defined $name and not ref $name and length $name
!ll&&!rl&&r
5112599
defined $name and not ref $name
!ll&&!rl&&r
4112600
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
65 unless defined $name && !ref $name && length $name;
66 12599 ▶ croak 'an identifier cannot contain a NUL byte'
Branch: 1 branch
BranchTrueFalseTotal
if index($name, "\0") != -1 1 12598 12599
67 if index($name, "\0") >= 0;
68  
69 12598 ▶ 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} 12253 345 12598
Condition100%3/3
$name =~ /\A[a-z_][a-z0-9_]*\z/ and not $KEYWORD{$name}
!ll&&!rl&&r
2588712253
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
70  
71 345 (my $quoted = $name) =~ s/"/""/g;
72 345 return qq{"$quoted"};
73 }
74  
75  
76 5107 ▶sub quote_qualified {
Subroutine quote_qualified: called CC=2 SCAR=6.9
Pod documented
77 5107 my (@parts) = @_;
78 5107 ▶ croak 'a qualified name needs at least one part' unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 1 5106 5107
79 5106 return join '.', map { quote_ident($_) } @parts;
80 }
81  
82  
83 34825 ▶sub quote_literal {
Subroutine quote_literal: called CC=4 SCAR=13.9
Pod documented
84 34825 my ($value) = @_;
85 34825 ▶ croak 'a literal must be a defined, plain value'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value 3 34822 34825
Condition100%3/3
defined $value and not ref $value
!ll&&!rl&&r
2134822
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
86 unless defined $value && !ref $value;
87 34822 ▶ croak 'a literal cannot contain a NUL byte'
Branch: 1 branch
BranchTrueFalseTotal
if index($value, "\0") != -1 1 34821 34822
88 if index($value, "\0") >= 0;
89  
90 34821 (my $quoted = $value) =~ s/'/''/g;
91 34821 return qq{'$quoted'};
92 }
93  
94  
95 378577 ▶sub trim_end {
Subroutine trim_end: called CC=6 SCAR=17.9
Pod documented
96 378577 my ($text, $also) = @_;
97 378577 ▶ $also //= '';
Condition100%2/2
$also //= ""
l!l
6925371652
MC/DC100%1/1
$also //= ""
$also
Truth table100%2/2
A
A= $also
Aresult
11
00
98 378577 my $end = length $text;
99 378577 ▶ while ($end > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end > 0) 385591 13 385604
100 385591 my $char = substr($text, $end - 1, 1);
101 385591 ▶ 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 378564 7027 385591
Condition100%6/6
$char =~ /\s/ or $also ne "" and index($also, $char) != -1
l!l&&r!l&&!r
1026925378564
$also ne "" and index($also, $char) != -1
!ll&&!rl&&r
37164269226925
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
102 7027 $end--;
103 }
104 378577 return substr($text, 0, $end);
105 }
106  
107  
108 23 ▶sub create_schema_if_missing {
Subroutine create_schema_if_missing: called CC=2 SCAR=6.9
Pod documented
109 23 my ($name) = @_;
110 23 my $body = sprintf
111 "BEGIN\n IF NOT EXISTS (SELECT 1 FROM pg_catalog.pg_namespace WHERE nspname = %s) THEN\n CREATE SCHEMA %s;\n END IF;\nEND",
112 quote_literal($name), quote_ident($name);
113 23 my ($tag, $n) = ('dbio', 0);
114 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
115 23 return "DO \$$tag\$\n$body\n\$$tag\$";
116 }
117  
118 # What stands in for a value in an error text.
119 217 my $MASK = '***';
120  
121 # The start of a field of the server's message, at the start of a line. A field
122 # runs to the next one or to the end of the text.
123 217 my $FIELD_START = qr/^(?:DETAIL: |HINT: |CONTEXT: |QUERY: |LINE \d+: |STATEMENT:|LOCATION:)/m;
124  
125 # The same for any language of the server (lc_messages): a label is capital
126 # letters, optionally a number (the statement excerpt, "LINE 1: ", "ZEILE 1: ")
127 # and a space before the colon ("DETAIL : " in French). Used only where the
128 # SQLSTATE says the text may hold a value (mask_error_details).
129 217 my $FIELD_ANY = qr/^(?:\p{Lu}+ \d+ ?: |\p{Lu}+ ?: |STATEMENT:|LOCATION:)/m;
130  
131 # SQLSTATE class of the errors whose text holds a value: 22 data exception
132 # (the value of a cast, a range, a literal), 23 integrity constraint violation
133 # (the key or the row in DETAIL).
134 447 ▶sub _state_class {
Subroutine _state_class: called CC=4 SCAR=13.9
135 447 my ($state) = @_;
136 447 ▶ return '' unless defined $state && !ref $state && $state =~ /\A([0-9A-Z]{2})/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $state and not ref $state and $state =~ /\A([0-9A-Z]{2})/ 285 162 447
Condition83%5/6
defined $state and not ref $state and $state =~ /\A([0-9A-Z]{2})/
!ll&&!rl&&r
2841162
defined $state and not ref $state
!ll&&!rl&&r
2840163
MC/DC66%2/3
defined $state and not ref $state and $state =~ /\A([0-9A-Z]{2})/
defined $state not ref $state $state =~ /\A([0-9A-Z]{2})/
Truth table75%3/4
A and not(B) and C
A= defined $stateB= not ref $stateC= $state =~ /\A([0-9A-Z]{2})/
ABCresult
0XX0
10X0
1100
1111
137 162 return $1;
138 }
139  
140 # Where the quoted value of a primary text ends: at the end of the text, which
141 # DBI may follow with the place of its caller.
142 217 my $VALUE_END = qr/(?=(?: at \S+ line \d+\.?)?\s*\z)/;
143  
144 # The sentences of the primary text that end in the quoted value; what stands
145 # in front of the quote is kept.
146 217 my @SENTENCE_ENDING_IN_VALUE = (
147 qr/\binvalid input syntax for (?:type )?[^:\n]+: /,
148 qr/\binvalid input value for enum [^\n]*?: /,
149 qr/\bout of range: /,
150 qr/\bmalformed (?:array|record|range|multirange) literal: /,
151 qr/\bin ts(?:vector|query): /,
152 qr/\binvalid (?:hexadecimal|binary) digit: /,
153 qr/\bunrecognized encoding: /,
154 qr/\binvalid (?:destination |source )?encoding name /,
155 qr/\bunrecognized time zone name: /,
156 qr/\binvalid value for parameter "[^"\n]*": /,
157 qr/\binvalid regular expression option: /,
158 qr/\binvalid configuration parameter name /,
159 );
160  
161 217 my $IDENT = qr/[A-Za-z_\x{80}-\x{10FFFF}][\w\$\x{80}-\x{10FFFF}]*/;
162  
163 # "(cols)": a list of plain identifiers, or an expression of names, dots,
164 # colons, commas and balanced parentheses. Anything else (a quoted name, an
165 # "=") could hold a part of the value and is not read.
166 55 ▶sub _readable_columns {
Subroutine _readable_columns: called CC=8 SCAR=20.9
167 55 my ($cols) = @_;
168 55 ▶ return 1 if $cols =~ /\A$IDENT(?:, $IDENT)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $cols =~ /\A$IDENT(?:, $IDENT)*\z/ 49 6 55
169 6 ▶ 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 5 6
Condition66%2/3
$cols =~ /\A[\w\$\x{80}-\x{10FFFF} ,:.()]+\z/ and $cols =~ /\(/
!ll&&!rl&&r
105
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
170 5 my $depth = 0;
171 5 for my $c (split //, $cols) {
172 76 ▶ $depth++ if $c eq '(';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq "(" 5 71 76
173 76 ▶ $depth-- if $c eq ')';
Branch: 1 branch
BranchTrueFalseTotal
if $c eq ")" 5 71 76
174 76 ▶ return 0 if $depth < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $depth < 0 0 76 76
175 }
176 5 return $depth == 0;
177 }
178  
179 # "(cols)=(value)": the columns where they can be read, the value never.
180 56 ▶sub _mask_key_text {
Subroutine _mask_key_text: called CC=3 SCAR=11.0
181 56 my ($key) = @_;
182 56 ▶ return $MASK unless $key =~ /\A\(([^=]*?)\)=\(/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /\A\(([^=]*?)\)=\(/ 1 55 56
183 55 my $cols = $1;
184 55 ▶ return _readable_columns($cols) ? "($cols)=($MASK)" : $MASK;
Branch: 1 branch
BranchTrueFalseTotal
_readable_columns($cols) ? : 54 1 55
185 }
186  
187 # The sentences of the primary text that hold a value, in the words of the
188 # server's English. Answers the text and whether a rule found its form.
189 202 ▶sub _mask_primary_en {
Subroutine _mask_primary_en: called CC=2 SCAR=6.9
190 202 my ($text) = @_;
191 202 my $hit = 0;
192  
193 202 $hit += ($text =~ s/($_)"(.*)"$VALUE_END/$1"$MASK"/s) for @SENTENCE_ENDING_IN_VALUE;
194  
195 202 $hit += ($text =~ s/(\bvalue )"(.*)"( is out of range for type )/$1"$MASK"$3/s);
196 202 $hit += ($text =~ s/(\binvalid value )"(.*)"( for ")/$1"$MASK"$3/s);
197 202 $hit += ($text =~ s/(?<!\w)"(.*)"( is not a valid (?:binary|hexadecimal) digit)/"$MASK"$2/s);
198 202 $hit += ($text =~ s/(\btime zone )"(.*)"( not recognized)/$1"$MASK"$3/s);
199 202 $hit += ($text =~ s/(\bsyntax error at or near )"(.*)"( of jsonpath input)/$1"$MASK"$3/s);
200  
201 # invalid byte sequence for encoding "UTF8": 0xc3 0x28
202 # character with byte sequence 0xe2 0x82 in encoding "UTF8" has no equivalent ...
203 202 $hit += ($text =~ s/(\bbyte sequence for encoding "[^"\n]*": )0x[0-9A-Fa-f]{2}(?: 0x[0-9A-Fa-f]{2})*/$1$MASK/g);
204 202 $hit += ($text =~ s/(\bbyte sequence )0x[0-9A-Fa-f]{2}(?: 0x[0-9A-Fa-f]{2})*/$1$MASK/g);
205  
206 202 return ($text, $hit);
207 }
208  
209 # The pairs of marks a message of any language quotes a value in: the opening
210 # and the closing one.
211 217 my @QUOTE_PAIRS = (
212 [ '"', '"' ],
213 [ "\x{BB}", "\x{AB}" ], # German
214 [ "\x{AB}", "\x{BB}" ], # French
215 [ "\x{201E}", "\x{201C}" ],
216 [ "\x{201C}", "\x{201D}" ],
217 [ "\x{300C}", "\x{300D}" ],
218 );
219 217 my $QUOTE_CHARS = qr/["\x{AB}\x{BB}\x{201C}\x{201D}\x{201E}\x{300C}\x{300D}]/;
220 217 my $PAD = qr/[\s\x{A0}\x{202F}]*/;
221  
222 # The text with the span from the first opening mark to the last closing mark
223 # replaced by the mark, *** and the mark (the padding a language puts inside
224 # its marks stays). Without a span the text, undef as the second answer.
225 43 ▶sub _mask_quoted_span {
Subroutine _mask_quoted_span: called CC=7 SCAR=19.5
226 43 my ($text) = @_;
227 43 my ($best_start, $pair);
228 43 my $best_end;
229 43 for my $p (@QUOTE_PAIRS) {
230 258 my ($o, $c) = @$p;
231 258 my $start = index($text, $o);
232 258 ▶ next if $start < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start < 0 203 55 258
233 55 my $end = rindex($text, $c);
234 55 ▶ next if $end <= $start;
Branch: 1 branch
BranchTrueFalseTotal
if $end <= $start 26 29 55
235 29 ▶ ($best_start, $best_end, $pair) = ($start, $end, $p)
Branch: 1 branch
BranchTrueFalseTotal
if not defined $best_start or $start < $best_start 27 2 29
Condition66%2/3
not defined $best_start or $start < $best_start
l!l&&r!l&&!r
2702
MC/DC50%1/2
not defined $best_start or $start < $best_start
not defined $best_start $start < $best_start
Truth table66%2/3
not(A) or B
A= not defined $best_startB= $start < $best_start
ABresult
1X1
011
000
236 if !defined $best_start || $start < $best_start;
237 }
238 43 ▶ return ($text, undef) unless defined $best_start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $best_start 16 27 43
239 27 my ($o, $c) = @$pair;
240 27 my $inner = substr($text, $best_start + length $o, $best_end - $best_start - length $o);
241 27 my ($pre) = $inner =~ /\A($PAD)/;
242 27 my ($post) = $inner =~ /($PAD)\z/;
243 27 substr($text, $best_start, $best_end + length($c) - $best_start) = $o . $pre . $MASK . $post . $c;
244 27 return ($text, 1);
245 }
246  
247 # The primary text of a server that does not speak English: what stands
248 # between the marks it quotes a value in; else what stands after the last colon.
249 # The place DBI adds behind the text is no part of it.
250 34 ▶sub _mask_primary_foreign {
Subroutine _mask_primary_foreign: called CC=6 SCAR=18.0
251 34 my ($text) = @_;
252 34 my $tail = '';
253 34 ▶ ($text, $tail) = ($1, $2) if $text =~ /\A(.*?)((?: at \S+ line \d+\.?)?\s*)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A(.*?)((?: at \S+ line \d+\.?)?\s*)\z/s 34 0 34
254  
255 34 my ($masked, $span) = _mask_quoted_span($text);
256 34 ▶ return $masked . $tail if $span;
Branch: 1 branch
BranchTrueFalseTotal
if $span 20 14 34
257  
258 # the prefix of DBI and the severity label hold colons of their own
259 14 my ($head, $rest) = ('', $text);
260 14 ▶ ($head, $rest) = ($1, $2) if $text =~ /\A((?:DBD::\w+::\w+ \w+ failed: )?(?:\p{Lu}+ ?: )?)(.*)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\A((?:DBD::\w+::\w+ \w+ failed: )?(?:\p{Lu}+ ?: )?)(.*)\z/s 14 0 14
261 14 ▶ if ($rest =~ /\A(.*[:\x{FF1A}]$PAD)(.+)\z/s && $2 ne $MASK) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest =~ /\A(.*[:\x{FF1A}]$PAD)(.+)\z/s and $2 ne $MASK) 1 13 14
Condition100%3/3
$rest =~ /\A(.*[:\x{FF1A}]$PAD)(.+)\z/s and $2 ne $MASK
!ll&&!rl&&r
1211
MC/DC100%2/2
$rest =~ /\A(.*[:\x{FF1A}]$PAD)(.+)\z/s and $2 ne $MASK
$rest =~ /\A(.*[:\x{FF1A}]$PAD)(.+)\z/s $2 ne $MASK
Truth table100%3/3
A and B
A= $rest =~ /\A(.*[:\x{FF1A}]$PAD)(.+)\z/sB= $2 ne $MASK
ABresult
0X0
100
111
262 1 return $head . $1 . $MASK . $tail;
263 }
264 13 return $text . $tail;
265 }
266  
267 202 ▶sub _mask_primary {
Subroutine _mask_primary: called CC=6 SCAR=17.9
268 202 my ($text, $state) = @_;
269 202 my ($masked, $hit) = _mask_primary_en($text);
270  
271 # An invalid or untranslatable byte sequence (22021, 22P05): the bytes go in
272 # any language, the name of the encoding stays.
273 202 ▶ if (!$hit && defined $state && $state =~ /\A22(?:021|P05)\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hit and defined $state and $state =~ /\A22(?:021|P05)\z/) 8 194 202
Condition100%6/6
not $hit and defined $state and $state =~ /\A22(?:021|P05)\z/
!ll&&!rl&&r
140548
not $hit and defined $state
!ll&&!rl&&r
756562
MC/DC100%3/3
not $hit and defined $state and $state =~ /\A22(?:021|P05)\z/
not $hit defined $state $state =~ /\A22(?:021|P05)\z/
Truth table100%4/4
not(A) and B and C
A= not $hitB= defined $stateC= $state =~ /\A22(?:021|P05)\z/
ABCresult
0XX0
10X0
1100
1111
274 8 $hit += ($masked =~ s/\b0x[0-9A-Fa-f]{2}(?: 0x[0-9A-Fa-f]{2})*/$MASK/g);
275 }
276  
277 202 ▶ $masked = _mask_primary_foreign($masked)
Branch: 1 branch
BranchTrueFalseTotal
if not $hit and _state_class($state) eq 22 34 168 202
Condition100%3/3
not $hit and _state_class($state) eq 22
!ll&&!rl&&r
838534
MC/DC100%2/2
not $hit and _state_class($state) eq 22
not $hit _state_class($state) eq 22
Truth table100%3/3
not(A) and B
A= not $hitB= _state_class($state) eq 22
ABresult
0X0
100
111
278 if !$hit && _state_class($state) eq '22';
279 202 return $masked;
280 }
281  
282 # A text that arrives as UTF-8 bytes (no utf8 flag) is masked as characters, so
283 # that no mark or padding character is cut through; the answer is bytes again.
284 # A text that is a string already, or is no UTF-8, goes to the code as it is.
285 313 ▶sub _as_characters {
Subroutine _as_characters: called CC=4 SCAR=13.9
286 313 my ($text, $code) = @_;
287 313 ▶ if (!utf8::is_utf8($text) && $text =~ /[\x80-\xFF]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not utf8::is_utf8($text) and $text =~ /[\x80-\xFF]/) 8 305 313
Condition100%3/3
not utf8::is_utf8($text) and $text =~ /[\x80-\xFF]/
!ll&&!rl&&r
942118
MC/DC100%2/2
not utf8::is_utf8($text) and $text =~ /[\x80-\xFF]/
not utf8::is_utf8($text) $text =~ /[\x80-\xFF]/
Truth table100%3/3
not(A) and B
A= not utf8::is_utf8($text)B= $text =~ /[\x80-\xFF]/
ABresult
0X0
100
111
288 8 my $chars = $text;
289 8 ▶ if (utf8::decode($chars)) {
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::decode($chars)) 8 0 8
290 8 my $masked = $code->($chars);
291 8 utf8::encode($masked);
292 8 return $masked;
293 }
294 }
295 305 return $code->($text);
296 }
297  
298 # The parameters of a portal ($1 = 'a''b') stand in the same words in every
299 # language, in a field of any label.
300 175 ▶sub _mask_portal_parameters {
Subroutine _mask_portal_parameters: called CC=1 SCAR=0.0
301 175 my ($text) = @_;
302 175 $text =~ s/(\$\d+ = )'(?:[^']|'')*'/$1'$MASK'/g;
303 175 return $text;
304 }
305  
306 # The lines of the context that hold a value. With $state (a SQLSTATE of
307 # class 22 or 23: a value may stand in the text) a line of the form
308 # "words 1: rest" that no rule knows is a line of the server in another
309 # language -- JSON data, line 1: {"a":x... -- and keeps its words, the rest is ***.
310 75 ▶sub _mask_context {
Subroutine _mask_context: called CC=4 SCAR=13.9
311 75 my ($text, $state) = @_;
312  
313 # JSON data, line 1: {"a":x...
314 75 $text =~ s/(\bJSON data, line \d+: )[^\n]*/$1$MASK/g;
315  
316 # COPY t, line 1, column c: "value"
317 75 $text =~ s/(\bCOPY [^\n,]+, line \d+(?:, column [^:\n]+)?: )"(.*)"/$1"$MASK"/s;
318  
319 # unnamed portal with parameters: $1 = 'a''b', $2 = NULL
320 75 $text = _mask_portal_parameters($text);
321  
322 75 ▶ if (_state_class($state) =~ /\A2[23]\z/) {
Branch: 1 branch
BranchTrueFalseTotal
if (_state_class($state) =~ /\A2[23]\z/) 8 67 75
323 8 ▶ my ($label, $body) = $text =~ /\A(\p{Lu}+ ?: )(.*)\z/s ? ($1, $2) : ('', $text);
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\A(\p{Lu}+ ?: )(.*)\z/s ? : 8 0 8
324 8 ▶ $body =~ s/^([^:\n]*\d[^:\n]*: )(.+)$/index($2, $MASK) >= 0 ? "$1$2" : "$1$MASK"/gme;
Branch: 1 branch
BranchTrueFalseTotal
index($2, $MASK) != -1 ? : 3 3 6
325 8 $text = $label . $body;
326 }
327  
328 75 return $text;
329 }
330  
331  
332 112 ▶sub mask_detail_values {
Subroutine mask_detail_values: called CC=2 SCAR=6.9
Pod documented
333 112 my ($text, $state) = @_;
334 112 ▶ return $text unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 1 111 112
335 111 ▶ return _as_characters($text, sub { _mask_detail_values($_[0], $state) });
Subroutine __ANON__: called CC=1 SCAR=0.0
336 }
337  
338 111 ▶sub _mask_detail_values {
Subroutine _mask_detail_values: called CC=4 SCAR=13.9
339 111 my ($text, $state) = @_;
340  
341 111 my $hit = 0;
342 111 my $tail = qr/ (?:already exists|is duplicated|is not present in table|is still referenced from table|conflicts with existing key)/;
343 111 my $key = $text =~ s/\bKey (\(.*\)=\(.*\))(?=$tail)/'Key ' . _mask_key_text($1)/se;
344 0 ▶ $key ||= $text =~ s/\bKey (\(.*\)=\(.*\))(?=[^)]*\z)/'Key ' . _mask_key_text($1)/se;
Condition66%2/3
$key ||= $text =~ s/\bKey (\(.*\)=\(.*\))(?=[^)]*\z)/'Key ' . _mask_key_text($1);/es
l!l&&r!l&&!r
36075
MC/DC50%1/2
$key ||= $text =~ s/\bKey (\(.*\)=\(.*\))(?=[^)]*\z)/'Key ' . _mask_key_text($1);/es
$key $text =~ s/\bKey (\(.*\)=\(.*\))(?=[^)]*\z)/'Key ' . _mask_key_text($1);/es
Truth table66%2/3
A ||= B
A= $keyB= $text =~ s/\bKey (\(.*\)=\(.*\))(?=[^)]*\z)/'Key ' . _mask_key_text($1);/es
ABresult
1X1
011
000
345 111 $hit += $key;
346 111 $hit += ($text =~ s/(\bconflicts with existing key )(\(.*\)=\(.*\))(?=[^)]*\z)/$1 . _mask_key_text($2)/se);
347  
348 111 $hit += ($text =~ s/(\bFailing row contains \().*\)/$1$MASK)/s);
349 111 $hit += ($text =~ s/(\bPartition key of the failing row contains \([^=]*?\) = \().*\)/$1$MASK)/s);
350  
351 111 $hit += ($text =~ s/(\bToken )"(.*)"( is invalid\.)/$1"$MASK"$3/s);
352 111 $hit += ($text =~ s/(, but found )"(.*)"(\.?)/$1"$MASK"$3/s);
353 111 $hit += ($text =~ s/(\bEscape sequence )"(.*)"( is invalid\.)/$1"$MASK"$3/s);
354 111 $hit += ($text =~ s/(\bUnexpected )"(.*)"( character\.)/$1"$MASK"$3/s);
355 111 $hit += ($text =~ s/(\bCharacter with value )0x[0-9A-Fa-f]+/$1$MASK/g);
356  
357 111 $hit += ($text =~ s/((?:\A|\bDETAIL: )line \d+: ).*?(\n?)\z/$1$MASK$2/s);
358  
359 111 ▶ $text = _mask_detail_foreign($text)
Branch: 1 branch
BranchTrueFalseTotal
if not $hit and _state_class($state) =~ /\A2[23]\z/ 31 80 111
Condition100%3/3
not $hit and _state_class($state) =~ /\A2[23]\z/
!ll&&!rl&&r
602031
MC/DC100%2/2
not $hit and _state_class($state) =~ /\A2[23]\z/
not $hit _state_class($state) =~ /\A2[23]\z/
Truth table100%3/3
not(A) and B
A= not $hitB= _state_class($state) =~ /\A2[23]\z/
ABresult
0X0
100
111
360 if !$hit && _state_class($state) =~ /\A2[23]\z/;
361  
362 111 return $text;
363 }
364  
365 # The detail of a server that does not speak English (or the form of which no
366 # rule above knows), for an error whose SQLSTATE says the text holds a value.
367 # What is kept is what stands outside of the value: the label, the words, the
368 # columns of a key. The text goes whole when it cannot be told where the value
369 # ends.
370 31 ▶sub _mask_detail_foreign {
Subroutine _mask_detail_foreign: called CC=10 SCAR=23.0
371 31 my ($text) = @_;
372  
373 31 ▶ my ($label, $body) = $text =~ /\A(\p{Lu}+ ?: )(.*)\z/s ? ($1, $2) : ('', $text);
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\A(\p{Lu}+ ?: )(.*)\z/s ? : 28 3 31
374 31 my $trail = '';
375 31 ▶ ($body, $trail) = ($1, $2) if $body =~ /\A(.*?)(\s*)\z/s;
Branch: 1 branch
BranchTrueFalseTotal
if $body =~ /\A(.*?)(\s*)\z/s 31 0 31
376  
377 31 my $first = index($body, '(');
378 31 my $last = rindex($body, ')');
379 31 ▶ my $bracketed = $first >= 0 && $last > $first;
Condition100%3/3
$first >= 0 && $last > $first
!ll&&!rl&&r
8122
MC/DC100%2/2
$first >= 0 && $last > $first
$first >= 0 $last > $first
Truth table100%3/3
A && B
A= $first >= 0B= $last > $first
ABresult
0X0
100
111
380  
381 31 ▶ if ($bracketed) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bracketed) { } 22 9 31
382 # Schluessel (cols)=(value) ... / Failing row contains (a, b, c)
383 22 my $span = substr($body, $first, $last - $first + 1);
384 22 ▶ my $masked = $span =~ /\)=\(/ ? _mask_key_text($span) : "($MASK)";
Branch: 1 branch
BranchTrueFalseTotal
$span =~ /\)=\(/ ? : 18 4 22
385 22 substr($body, $first, $last - $first + 1) = $masked;
386 }
387 else {
388 9 ($body) = _mask_quoted_span($body);
389 }
390  
391 # what is left of a value: a bracket or a mark that no mask above covers
392 31 my $left = $body;
393 31 $left =~ s/\([^=]*?\)=\(\Q$MASK\E\)//g;
394 31 $left =~ s/\(\Q$MASK\E\)//g;
395 31 ▶ $left =~ s/($QUOTE_CHARS)$PAD\Q$MASK\E$PAD$QUOTE_CHARS//g unless $bracketed;
Branch: 1 branch
BranchTrueFalseTotal
unless $bracketed 9 22 31
396 31 ▶ $body = $MASK if $left =~ /[()]/ || (!$bracketed && $left =~ $QUOTE_CHARS);
Branch: 1 branch
BranchTrueFalseTotal
if $left =~ /[()]/ or not $bracketed and $left =~ /$QUOTE_CHARS/ 2 29 31
Condition100%6/6
$left =~ /[()]/ or not $bracketed and $left =~ /$QUOTE_CHARS/
l!l&&r!l&&!r
1129
not $bracketed and $left =~ /$QUOTE_CHARS/
!ll&&!rl&&r
2271
MC/DC100%3/3
$left =~ /[()]/ or not $bracketed and $left =~ /$QUOTE_CHARS/
$left =~ /[()]/ not $bracketed $left =~ /$QUOTE_CHARS/
Truth table100%4/4
A or not(B) and C
A= $left =~ /[()]/B= not $bracketedC= $left =~ /$QUOTE_CHARS/
ABCresult
1XX1
0111
00X0
0100
397  
398 31 return $label . $body . $trail;
399 }
400  
401  
402 204 ▶sub mask_error_details {
Subroutine mask_error_details: called CC=3 SCAR=11.0
Pod documented
403 204 my ($message, $state) = @_;
404 204 ▶ return $message unless defined $message && length $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and length $message 2 202 204
Condition100%3/3
defined $message and length $message
!ll&&!rl&&r
11202
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
405 202 ▶ return _as_characters($message, sub { _mask_error_details($_[0], $state) });
Subroutine __ANON__: called CC=1 SCAR=0.0
406 }
407  
408 202 ▶sub _mask_error_details {
Subroutine _mask_error_details: called CC=13 SCAR=25.6
409 202 my ($message, $state) = @_;
410  
411 202 my $tail = '';
412 202 ▶ ($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 200 202
413  
414 # With a SQLSTATE of class 22 or 23 the labels of any language start a field
415 # A message with the severity label in English (the server reports in English,
416 # or has no catalog for its language) is read by the English rules alone: a
417 # sentence they do not know is no sentence of another language.
418 202 ▶ my $gated = _state_class($state) =~ /\A2[23]\z/
Condition100%3/3
_state_class($state) =~ /\A2[23]\z/ && !($message =~ /\A(?:[^\n]*?\bfailed: )?(?:ERROR|FATAL|PANIC|WARNING|NOTICE|INFO|LOG): /)
!ll&&!rl&&r
134662
MC/DC100%2/2
_state_class($state) =~ /\A2[23]\z/ && !($message =~ /\A(?:[^\n]*?\bfailed: )?(?:ERROR|FATAL|PANIC|WARNING|NOTICE|INFO|LOG): /)
_state_class($state) =~ /\A2[23]\z/ !($message =~ /\A(?:[^\n]*?\bfailed: )?(?:ERROR|FATAL|PANIC|WARNING|NOTICE|INFO|LOG): /)
Truth table100%3/3
A && not(B)
A= _state_class($state) =~ /\A2[23]\z/B= !($message =~ /\A(?:[^\n]*?\bfailed: )?(?:ERROR|FATAL|PANIC|WARNING|NOTICE|INFO|LOG): /)
ABresult
0X0
100
111
419 && $message !~ /\A(?:[^\n]*?\bfailed: )?(?:ERROR|FATAL|PANIC|WARNING|NOTICE|INFO|LOG): /;
420 202 ▶ $state = undef unless $gated;
Branch: 1 branch
BranchTrueFalseTotal
unless $gated 140 62 202
421 202 ▶ my $labels = $gated ? $FIELD_ANY : $FIELD_START;
Branch: 1 branch
BranchTrueFalseTotal
$gated ? : 62 140 202
422 202 my @fields = split /(?=$labels)/, $message;
423 202 my $n = 0;
424 202 for my $field (@fields) {
425 384 my $first = !$n++;
426 384 ▶ if ($field =~ /\ADETAIL: /) { $field = mask_detail_values($field, $state) }
Branch: 5 branches
BranchTrueFalseTotal
if ($field =~ /\ADETAIL: /) { } 91 293 384
elsif ($field =~ /\ACONTEXT: /) { } 69 224 293
elsif ($field =~ /$FIELD_START/) { } 8 216 224
elsif ($first) { } 202 14 216
elsif ($field =~ /\A(?:\p{Lu}+ \d+ ?: |STATEMENT:|LOCATION:)/) { } 4 10 14
427 69 elsif ($field =~ /\ACONTEXT: /) { $field = _mask_context($field, $state) }
428 elsif ($field =~ $FIELD_START) { }
429 202 elsif ($first) { $field = _mask_primary($field, $state) }
430 elsif ($field =~ /\A(?:\p{Lu}+ \d+ ?: |STATEMENT:|LOCATION:)/) { }
431 10 else { $field = _mask_foreign_field($field, $state) }
432 384 ▶ $field = _mask_portal_parameters($field)
Branch: 1 branch
BranchTrueFalseTotal
if $gated and not $field =~ /\A(?:\p{Lu}+ \d+ ?: |STATEMENT:|LOCATION:|QUERY:)/ 100 284 384
Condition100%3/3
$gated and not $field =~ /\A(?:\p{Lu}+ \d+ ?: |STATEMENT:|LOCATION:|QUERY:)/
!ll&&!rl&&r
2804100
MC/DC100%2/2
$gated and not $field =~ /\A(?:\p{Lu}+ \d+ ?: |STATEMENT:|LOCATION:|QUERY:)/
$gated not $field =~ /\A(?:\p{Lu}+ \d+ ?: |STATEMENT:|LOCATION:|QUERY:)/
Truth table100%3/3
A and not(B)
A= $gatedB= not $field =~ /\A(?:\p{Lu}+ \d+ ?: |STATEMENT:|LOCATION:|QUERY:)/
ABresult
0X0
100
111
433 if $gated && $field !~ /\A(?:\p{Lu}+ \d+ ?: |STATEMENT:|LOCATION:|QUERY:)/;
434 }
435 202 return join('', @fields) . $tail;
436 }
437  
438 # A field with a label this module does not know, in a text whose SQLSTATE says
439 # it may hold a value: a translated context is read as one, anything else as a
440 # detail.
441 10 ▶sub _mask_foreign_field {
Subroutine _mask_foreign_field: called CC=5 SCAR=16.1
442 10 my ($field, $state) = @_;
443 10 ▶ my $body = $field =~ /\A\p{Lu}+ ?: (.*)\z/s ? $1 : $field;
Branch: 1 branch
BranchTrueFalseTotal
$field =~ /\A\p{Lu}+ ?: (.*)\z/s ? : 10 0 10
444 10 ▶ return $body !~ /\)=\(/
Branch: 1 branch
BranchTrueFalseTotal
!($body =~ /\)=\(/) && ($body =~ /\$\d+ = '/ || $body =~ /^[^:\n]*\d[^:\n]*: /m) ? : 6 4 10
Condition100%6/6
not $body =~ /\)=\(/ and $body =~ /\$\d+ = '/ || $body =~ /^[^:\n]*\d[^:\n]*: /m
!ll&&!rl&&r
226
$body =~ /\$\d+ = '/ or $body =~ /^[^:\n]*\d[^:\n]*: /m
l!l&&r!l&&!r
242
MC/DC100%3/3
not $body =~ /\)=\(/ and $body =~ /\$\d+ = '/ || $body =~ /^[^:\n]*\d[^:\n]*: /m
not $body =~ /\)=\(/ $body =~ /\$\d+ = '/ $body =~ /^[^:\n]*\d[^:\n]*: /m
Truth table100%4/4
not(A) and B or C
A= not $body =~ /\)=\(/B= $body =~ /\$\d+ = '/C= $body =~ /^[^:\n]*\d[^:\n]*: /m
ABCresult
0XX0
1000
11X1
1011
445 && ($body =~ /\$\d+ = '/ || $body =~ /^[^:\n]*\d[^:\n]*: /m)
446 ? _mask_context($field, $state)
447 : mask_detail_values($field, $state);
448 }
449  
450  
451 9 ▶sub error_sqlstate {
Subroutine error_sqlstate: called CC=10 SCAR=23.1
Pod documented
452 9 my ($h) = @_;
453 9 ▶ return undef unless Scalar::Util::blessed($h);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($h) 2 7 9
454  
455 7 ▶ my $dbh = eval { ($h->{Type} // '') eq 'st' ? $h->{Database} : $h };
Branch: 1 branch
BranchTrueFalseTotal
($h->{'Type'} // '') eq 'st' ? : 1 6 7
Condition100%2/2
$h->{"Type"} // ""
l!l
16
MC/DC100%1/1
$h->{"Type"} // ""
$h->{"Type"}
Truth table100%2/2
A
A= $h->{"Type"}
Aresult
11
00
456 7 ▶ return undef unless Scalar::Util::blessed($dbh);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($dbh) 0 7 7
457  
458 7 for my $read (
459 7 ▶ sub { $dbh->state },
Subroutine __ANON__: called CC=1 SCAR=0.0
460 5 ▶ sub { $dbh->pg_error_field('sqlstate') },
Subroutine __ANON__: called CC=1 SCAR=0.0
461 ) {
462 12 my $state = eval { $read->() };
463 12 ▶ return $state if defined $state && !ref $state && length $state && $state ne '00000';
Branch: 1 branch
BranchTrueFalseTotal
if defined $state and not ref $state and length $state and $state ne "00000" 3 9 12
Condition88%8/9
defined $state and not ref $state and length $state and $state ne "00000"
!ll&&!rl&&r
813
defined $state and not ref $state and length $state
!ll&&!rl&&r
624
defined $state and not ref $state
!ll&&!rl&&r
606
MC/DC75%3/4
defined $state and not ref $state and length $state and $state ne "00000"
defined $state not ref $state length $state $state ne "00000"
Truth table80%4/5
A and not(B) and C and D
A= defined $stateB= not ref $stateC= length $stateD= $state ne "00000"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
464 }
465 4 return undef;
466 }
467  
468  
469 217 my %ROLE_KEYWORD = map { $_ => 1 } qw(
470 public current_user current_role session_user
471 );
472  
473 28 ▶sub quote_role {
Subroutine quote_role: called CC=4 SCAR=13.9
Pod documented
474 28 my ($role) = @_;
475 return $role
476 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
477 15 return quote_ident($role);
478 }
479  
480  
481 148 ▶sub deferrable_clause {
Subroutine deferrable_clause: called CC=5 SCAR=16.3
Pod documented
482 148 my ($deferrable, $initially_deferred) = @_;
483 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
484 if $initially_deferred && !$deferrable;
485 148 ▶ return '' unless $deferrable;
Branch: 1 branch
BranchTrueFalseTotal
unless $deferrable 119 29 148
486 29 ▶ return $initially_deferred ? ' DEFERRABLE INITIALLY DEFERRED' : ' DEFERRABLE INITIALLY IMMEDIATE';
Branch: 1 branch
BranchTrueFalseTotal
$initially_deferred ? : 10 19 29
487 }
488  
489  
490 # Every type that takes a type modifier, under every name it can be declared
491 # by. The built-in ones are the ten of pg_catalog.pg_type with a typmodin --
492 # bpchar, varchar, bit, varbit, numeric, time, timetz, timestamp, timestamptz,
493 # interval -- each under its catalog name, the name format_type() gives it and
494 # the further spellings the grammar has for it. Of the interval forms with
495 # fields, the ones that end in the seconds take the precision. vector, halfvec
496 # and sparsevec are pgvector's.
497 #
498 # Read from a PostgreSQL 18.6 server. t/98-ddl-type-modifier-live.t reads
499 # pg_type from the server it runs against, fails for every built-in type with
500 # a typmodin that is missing here, and has the server take every name below
501 # with a modifier.
502 217 my %TAKES_MODIFIER = map { $_ => 1 } (
503 'character', 'char', 'bpchar', 'nchar', 'national character', 'national char',
504 'character varying', 'char varying', 'varchar', 'nchar varying',
505 'national character varying', 'national char varying',
506 'bit', 'bit varying', 'varbit',
507 'numeric', 'decimal', 'dec',
508 'time', 'time without time zone', 'timetz', 'time with time zone',
509 'timestamp', 'timestamp without time zone', 'timestamptz', 'timestamp with time zone',
510 'interval', 'interval second', 'interval day to second',
511 'interval hour to second', 'interval minute to second',
512 'vector', 'halfvec', 'sparsevec',
513 );
514  
515  
516 700 ▶sub type_takes_modifier {
Subroutine type_takes_modifier: called CC=4 SCAR=13.9
Pod documented
517 700 my ($type) = @_;
518 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
519 699 (my $name = lc $type) =~ s/\s+/ /g;
520 699 $name =~ s/\A | \z//g;
521 699 ▶ return $TAKES_MODIFIER{$name} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$TAKES_MODIFIER{$name} ? : 154 545 699
522 }
523  
524  
525 1423 ▶sub type_with_modifier {
Subroutine type_with_modifier: called CC=10 SCAR=23.0
Pod documented
526 1423 my ($type, $size, $context) = @_;
527 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
528 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
529 if ref $size && ref $size ne 'ARRAY';
530  
531 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
532 1417 ▶ return $type unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 1261 156 1417
533  
534 156 my $text = join ',', @parts;
535 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
536 unless $text =~ /\A\d+(?:,\d+)?\z/;
537  
538 146 my $modifier = "($text)";
539 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
540 if $type =~ /\A(\s*\w+)(\s+with(?:out)?\s+time\s+zone\s*)\z/i;
541 124 return $type . $modifier;
542 }
543  
544  
545 18 ▶sub quote_channel {
Subroutine quote_channel: called CC=6 SCAR=17.9
Pod documented
546 18 my ($channel) = @_;
547 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
548 unless defined $channel && !ref $channel && length $channel;
549 15 ▶ croak 'a LISTEN channel name cannot contain a NUL byte'
Branch: 1 branch
BranchTrueFalseTotal
if index($channel, "\0") != -1 1 14 15
550 if index($channel, "\0") >= 0;
551  
552 14 my $bytes = $channel;
553 14 utf8::encode($bytes);
554 14 ▶ croak sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if length $bytes > 63 3 11 14
555 "LISTEN channel name '%s' is %d bytes long; PostgreSQL channel names are "
556 . 'identifiers of at most 63 bytes (NAMEDATALEN - 1) -- LISTEN would '
557 . 'truncate it and pg_notify refuses it',
558 $channel, length $bytes,
559 ) if length $bytes > 63;
560  
561 11 (my $quoted = $channel) =~ s/"/""/g;
562 11 return qq{"$quoted"};
563 }
564  
565  
566 # What number_placeholders() takes from PostgreSQL's scanner:
567 # * a stretch of SQL with no '?' in it and nothing left open: characters that
568 # start none of the constructs number_placeholders() looks at, closed '...'
569 # and "..." runs (a doubled quote reads as two runs back to back, which is
570 # the same text), and words. A word -- identifier or keyword -- is taken as
571 # a whole, so a '$' inside it opens no dollar quote; only the word E before
572 # a quote is left standing, for the escape string it opens;
573 # * the delimiter of a dollar quote;
574 # * the whitespace behind a closing quote that continues the literal: a
575 # newline in it, '--' comments allowed.
576 # Each is anchored at the scan position itself and used as a whole pattern:
577 # that way perl takes the compiled pattern as it is on every match.
578 217 my $PG_WORD = qr/[A-Za-z_\x80-\x{10FFFF}][A-Za-z_0-9\$\x80-\x{10FFFF}]*/;
579 217 my $PG_SQL_STRETCH = qr/\G(?:[^A-Za-z_\x80-\x{10FFFF}'"\$?\/-]+|(?![eE]')$PG_WORD|'[^']*+'|"[^"]*+")++/;
580 217 my $PG_DOLLAR_DELIMITER = qr/\G(\$(?:[A-Za-z_\x80-\x{10FFFF}][A-Za-z_0-9\x80-\x{10FFFF}]*)?\$)/;
581 217 my $PG_STRING_CONTINUES = qr/\G(?:[ \t\f\x0B]|--[^\n\r]*)*[\n\r](?:[ \t\n\r\f\x0B]+|--[^\n\r]*[\n\r])*'/;
582  
583  
584 144 ▶sub number_placeholders {
Subroutine number_placeholders: called CC=23 SCAR=31.4
Pod documented
585 144 my ($sql) = @_;
586 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
587 unless defined $sql && !ref $sql;
588  
589 138 my $len = length $sql;
590 138 my $out = '';
591 138 my $copied = 0; # $sql up to this offset is in $out
592 138 my $n = 0;
593 138 my $dollar = 0; # a $N written in the statement (outside text)
594  
595 138 pos($sql) = 0;
596 138 ▶ while (pos($sql) < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $sql < $len) 604 138 742
597 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
598 # nothing to number in it
599 }
600 elsif ($sql =~ /\G\?/gc) {
601 189 my $at = pos($sql) - 1;
602 # '\?' is a literal question mark: the backslash goes, no placeholder
603 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
604 31 $out .= substr($sql, $copied, $at - 1 - $copied) . '?';
605 31 $copied = $at + 1;
606 31 next;
607 }
608 158 $out .= substr($sql, $copied, $at - $copied) . '$' . ++$n;
609 158 $copied = $at + 1;
610 }
611 elsif ($sql =~ /\G\$[0-9]+/gc) {
612 20 $dollar++;
613 }
614 elsif ($sql =~ /\G[eE]'/gc) {
615 # an escape string, and what continues it on the following lines
616 13 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ /$PG_STRING_CONTINUES/cg and do { do { $sql =~ /\G(?:[^\\']+|\\.|'')*+'/cgs or pos $sql = $len } } 2 13 15
617 15 ▶ $sql =~ /\G(?:[^\\']+|\\.|'')*+'/gcs or pos($sql) = $len;
Branch: 1 branch
BranchTrueFalseTotal
$sql =~ /\G(?:[^\\']+|\\.|'')*+'/cgs or pos $sql = $len 13 2 15
618 } while ($sql =~ /$PG_STRING_CONTINUES/gc);
619 }
620 elsif ($sql =~ /\G--[^\n\r]*/gc) {
621 # a comment to the end of the line
622 }
623 elsif ($sql =~ m{\G/\*}gc) {
624 13 my $depth = 1;
625 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
626 25 ▶ $depth += $1 eq '/*' ? 1 : -1;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '/*' ? : 7 18 25
627 }
628 13 ▶ pos($sql) = $len if $depth;
Branch: 1 branch
BranchTrueFalseTotal
if $depth 2 11 13
629 }
630 elsif ($sql =~ /$PG_DOLLAR_DELIMITER/gc) {
631 15 my $end = index($sql, $1, pos($sql));
632 15 ▶ pos($sql) = $end < 0 ? $len : $end + length $1;
Branch: 1 branch
BranchTrueFalseTotal
$end < 0 ? : 1 14 15
633 }
634 elsif ($sql =~ /\G['"]/gc) {
635 2 pos($sql) = $len; # a quote the stretch did not close: never closed
636 }
637 else {
638 8 pos($sql) = pos($sql) + 1; # a '-', '/' or '$' that opens nothing
639 }
640 }
641  
642 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
643 . 'write one style, and \\? for a literal question mark'
644 if $dollar && $n;
645  
646 130 return $out . substr($sql, $copied);
647 }
648  
649  
650  
651 15 ▶sub quote_dsn_value {
Subroutine quote_dsn_value: called CC=6 SCAR=20.9
Pod documented
652 15 my ($value) = @_;
653 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
654 unless defined $value && !ref $value;
655 14 ▶ croak 'a connection parameter value cannot contain a NUL byte'
Branch: 1 branch
BranchTrueFalseTotal
if index($value, "\0") != -1 0 14 14
656 if index($value, "\0") >= 0;
657  
658 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
659  
660 0 (my $quoted = $value) =~ s/(['\\=])/\\$1/g;
661 0 return qq{'$quoted'};
662 }
663  
664  
665 96 ▶sub dsn_names_target {
Subroutine dsn_names_target: called CC=10 SCAR=23.1
Pod documented
666 96 my ($dsn) = @_;
667 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
668 unless defined $dsn && !ref $dsn;
669  
670 96 $dsn =~ s/\Adbi:\w*(?:\([^)]*\))?://i;
671  
672 # libpq takes a URI for its whole connection string; what it names is its
673 # own business, and this does not judge it.
674 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
675  
676 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
677 80 my ($key, $value) = (lc $1, $2);
678 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
679 80 ▶ next unless $TARGET_KEY{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $TARGET_KEY{$key} 28 52 80
680 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
681 }
682  
683 56 return 0;
684 }
685  
686  
687  
688 48 ▶sub connection_search_path {
Subroutine connection_search_path: called CC=8 SCAR=20.8
Pod documented
689 48 my ($namespace, $extensions, $extension_schemas, $activation, $control) = @_;
690 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
691 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
692  
693 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
694 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
695 48 ▶ @schemas = ('public') unless @schemas;
Branch: 1 branch
BranchTrueFalseTotal
unless @schemas 14 34 48
696  
697 48 my %seen;
698 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
699 }
700  
701  
702 14 ▶sub declared_search_path {
Subroutine declared_search_path: called CC=5 SCAR=16.6
Pod documented
703 14 my ($declared, $extensions, $extension_schemas, $activation) = @_;
704 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
705  
706 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
707  
708 14 my %seen;
709 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
710 }
711  
712  
713 32 ▶sub quote_search_path_entry {
Subroutine quote_search_path_entry: called CC=10 SCAR=26.2
Pod documented
714 32 my ($entry) = @_;
715 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
716 'pg_search_path entry %s is not a name. Entries are schema names; write each as a plain string',
717 defined $entry && !ref $entry ? "'$entry'" : (defined $entry ? 'a reference' : 'undef'))
718 unless defined $entry && !ref $entry && length $entry && index($entry, "\0") < 0;
719 31 ▶ return '"$user"' if $entry eq '$user';
Branch: 1 branch
BranchTrueFalseTotal
if $entry eq "\$user" 2 29 31
720 29 ▶ croak sprintf(
Branch: 1 branch
BranchTrueFalseTotal
if $entry =~ /\A"/ 1 28 29
721 'pg_search_path entry %s is already quoted; write the name without quotes (%s)',
722 $entry, substr($entry, 1, -1))
723 if $entry =~ /\A"/;
724 28 return quote_ident($entry);
725 }
726  
727  
728 217 1;
729  
730 __END__
« blib/lib/DBIO/PostgreSQL/Diff/Sequence.pm ↑ Summary blib/lib/DBIO/PostgreSQL/Introspect/Parse.pm »