blib/lib/DBIO/MySQL/Util.pm

filter: Statementstmt 100% 119 / 119 Branchbran 94.0% 47 / 50 Conditioncond 84.3% 27 / 32 MC/DCmcdc 70.5% 12 / 17 Subroutinesub 100% 18 / 18 Podpod 100% 1 / 1 totaltotal 94.5% 224 / 237 CC 39 SCAR 17.8
CC
39
Cov
96%
_mask_ident_last
12.3
_mask_by_code
8.0
mask_error_details
7.0
CRAP 6.0
« blib/lib/DBIO/MySQL/Diff/ForeignKey.pm ↑ Summary blib/lib/DBIO/MySQL/IndexType.pm »
1 package DBIO::MySQL::Util;
2 # ABSTRACT: Shared MySQL/MariaDB-specific helper functions for DBIO
3  
4 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 51 our $VERSION = '0.900007';
8  
9 51 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 51 our @EXPORT_OK = qw(mask_error_details);
12  
13 # What stands in for a value in an error text.
14 51 my $MASK = '***';
15  
16 # The shortest bound value that is looked for verbatim in an error text. A
17 # shorter one is a letter or a number, which is part of any text; the engine's
18 # sentences that quote a value are known below and catch those.
19 51 my $MIN_BOUND_LENGTH = 4;
20  
21  
22 # The text of the sentences that quote a value in one pair of quotes and name
23 # what follows the value after it. The value is whatever lies between the first
24 # quote behind $head and the LAST $tail in the text, so a value that holds quotes
25 # or the words of the sentence is masked whole. A text that has the head but not
26 # the tail loses all that follows the head: more is masked, never less.
27 4112 ▶sub _mask_quoted {
Subroutine _mask_quoted: called CC=2 SCAR=6.9
28 4112 my ($text, $head, $tail) = @_;
29  
30 4112 ▶ $text =~ s/($head)'.*'(?=$tail)/$1'$MASK'/s
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/($head)'.*'(?=$tail)/$1'${MASK}'/s or $text =~ s/($head)'.*\z/$1'${MASK}'/s 138 3974 4112
31 or $text =~ s/($head)'.*\z/$1'$MASK'/s;
32  
33 4112 return $text;
34 }
35  
36 # The sentences of the engine that hold a value, one rule each. A rule keeps
37 # the words around the value -- the type, the column, the key -- and replaces
38 # the value, so a rule never leaves a part of it. Every rule gives the same text
39 # again when it is applied to its own result.
40 514 ▶sub _mask_sentences {
Subroutine _mask_sentences: called CC=1 SCAR=0.0
41 514 my ($text) = @_;
42  
43 # Duplicate entry 'x' for key 'tab.PRIMARY' -- a composite key is one text
44 514 $text = _mask_quoted($text, qr/\bDuplicate entry /, qr/ for key /);
45  
46 # Incorrect integer value: 'x' for column 'i' at row 1 (also ... for function f)
47 514 $text = _mask_quoted(
48 $text, qr/\bIncorrect [A-Za-z0-9_ ]+? value: /, qr/ for (?:column|function) /,
49 );
50  
51 # Truncated incorrect DOUBLE value: 'x'
52 514 $text = _mask_quoted($text, qr/\bTruncated incorrect [A-Za-z0-9_ ]+? value: /, qr/[^']*\z/);
53  
54 # Invalid utf8mb4 character string: '\xFF'
55 514 $text = _mask_quoted($text, qr/\bInvalid \w+ character string: /, qr/[^']*\z/);
56  
57 # Cannot convert string '\xFF' from binary to utf8mb4
58 514 $text = _mask_quoted($text, qr/\bCannot convert string /, qr/ from \w+ to \w+/);
59  
60 # Cannot convert 'utf8mb4' character 0xC3A9 to 'ascii'
61 514 $text =~ s/(\bCannot convert '[^'\n]*' character )0[xX][0-9A-Fa-f]+/$1$MASK/g;
62  
63 # Unknown or incorrect time zone: 'x' / Unknown character set: 'x' / Unknown collation: 'x'
64 514 $text = _mask_quoted($text, qr/\bUnknown (?:or incorrect time zone|character set|collation): /, qr/[^']*\z/);
65  
66 # Variable 'sql_mode' can't be set to the value of 'x'
67 514 $text = _mask_quoted($text, qr/\bVariable '[^'\n]*' can't be set to the value of /, qr/[^']*\z/);
68  
69 # Too big precision specified for 'x'. Maximum is 65 (MySQL: Too-big precision 70 specified for ''x''. ...)
70 514 $text = _mask_quoted($text, qr/\b[Tt]oo[- ]big \w+(?: \d+)? specified for /, qr/\. Maximum is/);
71  
72 514 return $text;
73 }
74  
75 # The excerpt the server gives of a refused statement: "... for the right syntax
76 # to use near '<excerpt>' at line N". Both DBDs write the bound values into the
77 # statement text before it is sent, so the excerpt holds them. What can be a
78 # value in it goes: every quoted literal (the statement's own with the bound
79 # ones; a literal the server cut at the edge of the excerpt, with no closing
80 # quote, to the end), and every bare number, which is what a bind of a numeric
81 # type is. A quoted name and the words of the statement stay.
82 128 ▶sub _mask_excerpt {
Subroutine _mask_excerpt: called CC=2 SCAR=6.9
83 128 my ($excerpt) = @_;
84  
85 128 $excerpt =~ s{
86 ( `(?:[^`]|``)*(?:`|\z) )
87 | ( '(?:[^'\\]|\\.?|'')*(?:'|\z) | "(?:[^"\\]|\\.?|"")*(?:"|\z) )
88 | (?<![\w.\$]) ( [-+]? (?: 0[xX][0-9A-Fa-f]+ | \d+(?:\.\d*)?(?:[eE][-+]?\d+)? | \.\d+ ) ) (?!\w)
89 107 ▶ }{ defined $1 ? $1 : $MASK }gsxe;
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 21 86 107
90  
91 128 return $excerpt;
92 }
93  
94 514 ▶sub _mask_syntax_error {
Subroutine _mask_syntax_error: called CC=2 SCAR=6.9
95 514 my ($text) = @_;
96  
97 514 ▶ $text =~ s/(\bfor the right syntax to use near ')(.*)(' at line \d+.*)\z/$1 . _mask_excerpt($2) . $3/se
Branch: 1 branch
BranchTrueFalseTotal
$text =~ s/(\bfor the right syntax to use near ')(.*)(' at line \d+.*)\z/$1 . _mask_excerpt($2) . $3;/es or $text =~ s/(\bfor the right syntax to use near ').*\z/$1${MASK}'/s 93 421 514
98 or $text =~ s/(\bfor the right syntax to use near ').*\z/$1$MASK'/s;
99  
100 514 return $text;
101 }
102  
103 # The forms a bound value has in a text: as it is, as DBD::mysql and
104 # DBD::MariaDB write it into a literal (a backslash before a quote, a
105 # backslash and a letter for the control characters), with a doubled quote, and
106 # as the UTF-8 bytes of the characters.
107 161 ▶sub _bound_forms {
Subroutine _bound_forms: called CC=2 SCAR=6.9
108 161 my ($bound) = @_;
109  
110 161 my %form = ($bound => 1);
111 161 my %escape = ("\\" => '\\\\', "'" => "\\'", '"' => '\\"', "\n" => '\\n',
112 "\r" => '\\r', "\0" => '\\0', "\x1a" => '\\Z');
113  
114 161 (my $escaped = $bound) =~ s/([\\'"\n\r\0\x1a])/$escape{$1}/g;
115 161 $form{$escaped} = 1;
116  
117 161 (my $doubled = $bound) =~ s/(['"])/$1$1/g;
118 161 $form{$doubled} = 1;
119  
120 161 for my $text (keys %form) {
121 168 my $bytes = $text;
122 168 utf8::encode($bytes);
123 168 $form{$bytes} = 1;
124 }
125  
126 161 return keys %form;
127 }
128  
129 # --- Masking by the error code of the server --------------------------------
130 #
131 # The server writes an error in the language of the session (lc_messages), and
132 # only the texts in English are known above. What does not change with the
133 # language is the code of the error and the quote the server puts around a
134 # value: a single quote. A code that quotes a value has the structure of its text
135 # described here, and the value is cut out by that structure, not by the words
136 # around it.
137 #
138 # ident the value, then the name of a key, a column or a function that is
139 # quoted too: Duplicate entry 'v' for key 'k'
140 # last the value is the last quoted thing, words may follow it
141 # var a quoted name, then the value: Variable 'n' ... 'v'
142 # mixed either of the two, told by the number of quotes (error 1292)
143 # excerpt a part of the statement, which holds the values: near '...', or the
144 # expression of "... value is out of range in '(a - 5)'"
145 51 my %CODE_KIND = (
146 1062 => 'ident', # ER_DUP_ENTRY
147 1366 => 'ident', # ER_TRUNCATED_WRONG_VALUE_FOR_FIELD
148 1411 => 'ident', # ER_WRONG_VALUE_FOR_TYPE
149 1300 => 'last', # ER_INVALID_CHARACTER_STRING
150 3854 => 'last', # ER_CANNOT_CONVERT_STRING
151 1298 => 'last', # ER_UNKNOWN_TIME_ZONE
152 1115 => 'last', # ER_UNKNOWN_CHARACTER_SET
153 1273 => 'last', # ER_UNKNOWN_COLLATION
154 1525 => 'last', # ER_WRONG_VALUE
155 1231 => 'var', # ER_WRONG_VALUE_FOR_VAR
156 1292 => 'mixed', # ER_TRUNCATED_WRONG_VALUE
157 1064 => 'excerpt', # ER_PARSE_ERROR
158 1690 => 'excerpt', # ER_DATA_OUT_OF_RANGE: "BIGINT value is out of range in '(`a` - 5)'"
159 );
160  
161 # What takes the place of a text that holds a value and cannot be masked for
162 # sure.
163 19 ▶sub _withheld_text {
Subroutine _withheld_text: called CC=1 SCAR=0.0
164 19 my ($code) = @_;
165 19 return "Server error $code: the text quotes a value and is withheld";
166 }
167  
168 # A quote that opens: not the one inside a word (the elision of "d'entier").
169 51 my $OPENING_QUOTE = qr/(?<![\w\x80-\xff])'/;
170  
171 # The span from the first opening quote to the last quote of the text, as the
172 # positions of the two quotes; nothing when there is no pair.
173 135 ▶sub _quoted_span {
Subroutine _quoted_span: called CC=3 SCAR=11.0
174 135 my ($text) = @_;
175  
176 135 ▶ $text =~ /$OPENING_QUOTE/g or return;
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /$OPENING_QUOTE/g or return 126 9 135
177 126 my $start = $-[0];
178 126 my $end = rindex($text, "'");
179  
180 126 ▶ return $end > $start ? ($start, $end) : ();
Branch: 1 branch
BranchTrueFalseTotal
$end > $start ? : 124 2 126
181 }
182  
183 # The span replaced; a text without one is withheld (undef).
184 87 ▶sub _mask_span {
Subroutine _mask_span: called CC=2 SCAR=7.0
185 87 my ($text, $start, $end) = @_;
186 87 ▶ return undef unless defined $start;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $start 0 87 87
187  
188 87 substr($text, $start, $end - $start + 1) = "'$MASK'";
189 87 return $text;
190 }
191  
192 27 ▶sub _mask_last {
Subroutine _mask_last: called CC=1 SCAR=0.0
193 27 my ($text) = @_;
194 27 return _mask_span($text, _quoted_span($text));
195 }
196  
197 # The value, then a name that is quoted again. The name is the token that ends
198 # last, of two forms: a chain of quoted identifiers (`db`.`t`.`c`) or a quote
199 # around one word. The value ends at the last quote before it. A lone quote in
200 # front of a chain (MariaDB in German: "... Feld '`db`.`t`.`c` in Zeile 1") belongs to
201 # neither. Without a name of its own the whole span is the value: a function is
202 # not quoted (... for function str_to_date).
203 69 ▶sub _mask_ident_last {
Subroutine _mask_ident_last: called CC=12 SCAR=25.1
204 69 my ($text) = @_;
205  
206 69 ▶ my ($start, $end) = _quoted_span($text) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = _quoted_span($text) or return undef 62 7 69
207  
208 62 my ($token_start, $token_end);
209  
210 62 ▶ if ($text =~ /\A.*(?<![\w\x80-\xff])('[^\s'`]+')(?![\w\x80-\xff])/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\A.*(?<![\w\x80-\xff])('[^\s'`]+')(?![\w\x80-\xff])/s) 60 2 62
211 60 ($token_start, $token_end) = ($-[1], $+[1]);
212 }
213  
214 62 my $last_tick = rindex($text, '`');
215 62 ▶ if ($last_tick >= 0
Branch: 1 branch
BranchTrueFalseTotal
if ($last_tick >= 0 and substr($text, 0, ($last_tick + 1)) =~ /(`[^`]+`(?:\.`[^`]+`)*)\z/ and !defined($token_end) || $last_tick + 1 > $token_end) 12 50 62
Condition66%6/9
$last_tick >= 0 and substr($text, 0, ($last_tick + 1)) =~ /(`[^`]+`(?:\.`[^`]+`)*)\z/ and !defined($token_end) || $last_tick + 1 > $token_end
!ll&&!rl&&r
50012
$last_tick >= 0 and substr($text, 0, ($last_tick + 1)) =~ /(`[^`]+`(?:\.`[^`]+`)*)\z/
!ll&&!rl&&r
50012
not defined $token_end or $last_tick + 1 > $token_end
l!l&&r!l&&!r
2100
MC/DC25%1/4
$last_tick >= 0 and substr($text, 0, ($last_tick + 1)) =~ /(`[^`]+`(?:\.`[^`]+`)*)\z/ and !defined($token_end) || $last_tick + 1 > $token_end
$last_tick >= 0 substr($text, 0, ($last_tick + 1)) =~ /(`[^`]+`(?:\.`[^`]+`)*)\z/ not defined $token_end $last_tick + 1 > $token_end
Truth table60%3/5
A and B and not(C) or D
A= $last_tick >= 0B= substr($text, 0, ($last_tick + 1)) =~ /(`[^`]+`(?:\.`[^`]+`)*)\z/C= not defined $token_endD= $last_tick + 1 > $token_end
ABCDresult
0XXX0
10XX0
11000
111X1
11011
216 and substr($text, 0, $last_tick + 1) =~ /(`[^`]+`(?:\.`[^`]+`)*)\z/
217 and (!defined $token_end or $last_tick + 1 > $token_end)) {
218 12 ($token_start, $token_end) = ($-[1], $last_tick + 1);
219 12 ▶ $token_start-- if $token_start > 0 and substr($text, $token_start - 1, 1) eq "'";
Branch: 1 branch
BranchTrueFalseTotal
if $token_start > 0 and substr($text, $token_start - 1, 1) eq "'" 9 3 12
Condition66%2/3
$token_start > 0 and substr($text, $token_start - 1, 1) eq "'"
!ll&&!rl&&r
039
MC/DC50%1/2
$token_start > 0 and substr($text, $token_start - 1, 1) eq "'"
$token_start > 0 substr($text, $token_start - 1, 1) eq "'"
Truth table66%2/3
A and B
A= $token_start > 0B= substr($text, $token_start - 1, 1) eq "'"
ABresult
0X0
100
111
220 }
221  
222 62 ▶ return _mask_span($text, $start, $end)
Branch: 1 branch
BranchTrueFalseTotal
if not defined $token_start or $token_start <= $start 3 59 62
Condition66%2/3
not defined $token_start or $token_start <= $start
l!l&&r!l&&!r
0359
MC/DC50%1/2
not defined $token_start or $token_start <= $start
not defined $token_start $token_start <= $start
Truth table66%2/3
not(A) or B
A= not defined $token_startB= $token_start <= $start
ABresult
1X1
011
000
223 if !defined $token_start or $token_start <= $start;
224  
225 59 my $value_end = rindex(substr($text, 0, $token_start), "'");
226 59 ▶ return undef unless $value_end > $start;
Branch: 1 branch
BranchTrueFalseTotal
unless $value_end > $start 2 57 59
227  
228 57 return _mask_span($text, $start, $value_end);
229 }
230  
231 # error 1231: the first pair of quotes is the name of the variable
232 9 ▶sub _mask_variable {
Subroutine _mask_variable: called CC=3 SCAR=11.0
233 9 my ($text) = @_;
234  
235 9 ▶ $text =~ /\A([^']*'[^'\n]*')(.*)\z/s or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\A([^']*'[^'\n]*')(.*)\z/s or return undef 6 3 9
236 6 my ($name, $rest) = ($1, $2);
237  
238 6 my $masked = _mask_last($rest);
239 6 ▶ return defined $masked ? $name . $masked : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $masked ? : 6 0 6
240 }
241  
242 # error 1292 has two sentences that differ in what follows the value:
243 # "Truncated incorrect DOUBLE value: 'x'" ends with it, "Incorrect date value:
244 # 'x' for column 'd' at row 1" names a column. A text that ends with a quote and
245 # has four or more of them is not told apart: all of it is the value.
246 18 ▶sub _mask_mixed {
Subroutine _mask_mixed: called CC=5 SCAR=16.1
247 18 my ($text) = @_;
248  
249 18 my $quotes = $text =~ tr/'//;
250 18 ▶ return undef if $quotes < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $quotes < 2 3 15 18
251 15 ▶ return _mask_last($text) if $quotes == 2 or ($quotes >= 4 and $text =~ /'\s*\z/);
Branch: 1 branch
BranchTrueFalseTotal
if $quotes == 2 or $quotes >= 4 and $text =~ /'\s*\z/ 6 9 15
Condition100%6/6
$quotes == 2 or $quotes >= 4 and $text =~ /'\s*\z/
l!l&&r!l&&!r
429
$quotes >= 4 and $text =~ /'\s*\z/
!ll&&!rl&&r
362
MC/DC100%3/3
$quotes == 2 or $quotes >= 4 and $text =~ /'\s*\z/
$quotes == 2 $quotes >= 4 $text =~ /'\s*\z/
Truth table100%4/4
A or B and C
A= $quotes == 2B= $quotes >= 4C= $text =~ /'\s*\z/
ABCresult
1XX1
0111
00X0
0100
252 9 return _mask_ident_last($text);
253 }
254  
255 39 ▶sub _mask_excerpt_span {
Subroutine _mask_excerpt_span: called CC=2 SCAR=6.9
256 39 my ($text) = @_;
257  
258 39 ▶ my ($start, $end) = _quoted_span($text) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = _quoted_span($text) or return undef 35 4 39
259 35 substr($text, $start + 1, $end - $start - 1) = _mask_excerpt(substr($text, $start + 1, $end - $start - 1));
260  
261 35 return $text;
262 }
263  
264 51 my %MASK_BY_KIND = (
265 ident => \&_mask_ident_last,
266 last => \&_mask_last,
267 var => \&_mask_variable,
268 mixed => \&_mask_mixed,
269 excerpt => \&_mask_excerpt_span,
270 );
271  
272 # The text masked by the structure of its error code, whatever the language of
273 # the server. What DBI adds around the text of the server stays out of the way:
274 # its statement with the values (ParamValues) goes, the DBD prefix, the location
275 # in Perl and the statement of the storage (placeholder form, number of binds)
276 # stay. A text that holds a value and does not have the structure of its code
277 # loses everything but the code.
278 514 ▶sub _mask_by_code {
Subroutine _mask_by_code: called CC=8 SCAR=20.8
279 514 my ($text, $code) = @_;
280  
281 514 ▶ my $kind = defined $code ? $CODE_KIND{$code} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $code ? : 206 308 514
282 514 ▶ return $text unless $kind;
Branch: 1 branch
BranchTrueFalseTotal
unless $kind 373 141 514
283  
284 141 my $statement = '';
285 141 ▶ if ($text =~ s/(\s*\[for Statement ".*)\z//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/(\s*\[for Statement ".*)\z//s) 5 136 141
286 5 my $cut = $1;
287 5 ▶ $statement = $cut if $cut =~ /" with \d+ bind values?\](?:\s+at \S+ line \d+\.?\s*)?\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $cut =~ /" with \d+ bind values?\](?:\s+at \S+ line \d+\.?\s*)?\z/ 3 2 5
288 }
289  
290 141 my ($prefix, $location) = ('', '');
291 141 ▶ $prefix = $1 if $text =~ s/\A((?:DBI Exception: )?(?:DBD::\w+::\w+ \w+ failed: )?)//;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ s/\A((?:DBI Exception: )?(?:DBD::\w+::\w+ \w+ failed: )?)// 141 0 141
292 141 ▶ $location = $1 if $text =~ s/(\s+at \S+ line \d+\.?\s*)\z//;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ s/(\s+at \S+ line \d+\.?\s*)\z// 3 138 141
293  
294 141 my $masked = $MASK_BY_KIND{$kind}->($text);
295 141 ▶ $masked = _withheld_text($code) unless defined $masked;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $masked 19 122 141
296  
297 141 return $prefix . $masked . $location . $statement;
298 }
299  
300  
301 516 ▶sub mask_error_details {
Subroutine mask_error_details: called CC=7 SCAR=19.5
Pod documented
302 516 my ($message, $binds, $statement, $code) = @_;
303 516 ▶ return $message unless defined $message && length $message;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $message and length $message 2 514 516
Condition100%3/3
defined $message and length $message
!ll&&!rl&&r
11514
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
304  
305 514 $message = _mask_by_code($message, $code);
306 514 $message = _mask_syntax_error($message);
307 514 $message = _mask_sentences($message);
308  
309 44 my @bound = sort { length $b <=> length $a }
310 161 map { _bound_forms($_) }
311 514 ▶ grep { defined && !ref && length >= $MIN_BOUND_LENGTH } @{ $binds // [] };
Condition100%8/8
defined $_ && !ref($_) && length $_ >= $MIN_BOUND_LENGTH
!ll&&!rl&&r
4135161
defined $_ and not ref $_
!ll&&!rl&&r
22296
$binds // []
l!l
220294
MC/DC100%3/3
defined $_ && !ref($_) && length $_ >= $MIN_BOUND_LENGTH
defined $_ not ref $_ length $_ >= $MIN_BOUND_LENGTH
MC/DC100%1/1
$binds // []
$binds
Truth table100%4/4
A and not(B) && C
A= defined $_B= not ref $_C= length $_ >= $MIN_BOUND_LENGTH
ABCresult
0XX0
10X0
1100
1111
Truth table100%2/2
A
A= $binds
Aresult
11
00
312 514 my %seen;
313 514 for my $bound (grep { !$seen{$_}++ } @bound) {
314 162 $message =~ s/\Q$bound\E/$MASK/g;
315 }
316  
317 514 return $message;
318 }
319  
320  
321 51 1;
322  
323 __END__
« blib/lib/DBIO/MySQL/Diff/ForeignKey.pm ↑ Summary blib/lib/DBIO/MySQL/IndexType.pm »