| 1 | package DBIO::MySQL::Util; | ||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Shared MySQL/MariaDB-specific helper functions for DBIO | ||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||
| 4 | 50 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 5 | 50 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||
| 7 | 50 | our $VERSION = '0.900004'; | |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||
| 9 | 50 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | 50 | our @EXPORT_OK = qw(mask_error_details); | |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | # What stands in for a value in an error text. | ||||||||||||||||||||||||||||||||||||||||||||
| 14 | 50 | 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 | 50 | 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 | 3064 | ▶ | sub _mask_quoted { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mask_quoted: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 3064 | my ($text, $head, $tail) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||
| 30 | 3064 | ▶ | $text =~ s/($head)'.*'(?=$tail)/$1'$MASK'/s | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 31 | or $text =~ s/($head)'.*\z/$1'$MASK'/s; | ||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | 3064 | 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 | 383 | ▶ | sub _mask_sentences { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mask_sentences: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 41 | 383 | my ($text) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||
| 43 | # Duplicate entry 'x' for key 'tab.PRIMARY' -- a composite key is one text | ||||||||||||||||||||||||||||||||||||||||||||
| 44 | 383 | $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 | 383 | $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 | 383 | $text = _mask_quoted($text, qr/\bTruncated incorrect [A-Za-z0-9_ ]+? value: /, qr/[^']*\z/); | |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | # Invalid utf8mb4 character string: '\xFF' | ||||||||||||||||||||||||||||||||||||||||||||
| 55 | 383 | $text = _mask_quoted($text, qr/\bInvalid \w+ character string: /, qr/[^']*\z/); | |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | # Cannot convert string '\xFF' from binary to utf8mb4 | ||||||||||||||||||||||||||||||||||||||||||||
| 58 | 383 | $text = _mask_quoted($text, qr/\bCannot convert string /, qr/ from \w+ to \w+/); | |||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | # Cannot convert 'utf8mb4' character 0xC3A9 to 'ascii' | ||||||||||||||||||||||||||||||||||||||||||||
| 61 | 383 | $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 | 383 | $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 | 383 | $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 | 383 | $text = _mask_quoted($text, qr/\b[Tt]oo[- ]big \w+(?: \d+)? specified for /, qr/\. Maximum is/); | |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 383 | 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 | 81 | ▶ | sub _mask_excerpt { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mask_excerpt: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 81 | my ($excerpt) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 81 | $excerpt =~ s{ | |||||||||||||||||||||||||||||||||||||||||||
| 86 | ( `(?:[^`]|``)*(?:`|\z) ) | ||||||||||||||||||||||||||||||||||||||||||||
| 87 | | ( '(?:[^'\\]|\\.?|'')*(?:'|\z) | "(?:[^"\\]|\\.?|"")*(?:"|\z) ) | ||||||||||||||||||||||||||||||||||||||||||||
| 88 | | (?<![\w.\$]) ( [-+]? (?: 0[xX][0-9A-Fa-f]+ | \d+(?:\.\d*)?(?:[eE][-+]?\d+)? | \.\d+ ) ) (?!\w) | ||||||||||||||||||||||||||||||||||||||||||||
| 89 | 65 | ▶ | }{ defined $1 ? $1 : $MASK }gsxe; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 81 | return $excerpt; | |||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 383 | ▶ | sub _mask_syntax_error { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mask_syntax_error: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 95 | 383 | my ($text) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 383 | ▶ | $text =~ s/(\bfor the right syntax to use near ')(.*)(' at line \d+.*)\z/$1 . _mask_excerpt($2) . $3/se | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 98 | or $text =~ s/(\bfor the right syntax to use near ').*\z/$1$MASK'/s; | ||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||
| 100 | 383 | 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 | 118 | ▶ | sub _bound_forms { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_bound_forms: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 108 | 118 | my ($bound) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | 118 | my %form = ($bound => 1); | |||||||||||||||||||||||||||||||||||||||||||
| 111 | 118 | my %escape = ("\\" => '\\\\', "'" => "\\'", '"' => '\\"', "\n" => '\\n', | |||||||||||||||||||||||||||||||||||||||||||
| 112 | "\r" => '\\r', "\0" => '\\0', "\x1a" => '\\Z'); | ||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||
| 114 | 118 | (my $escaped = $bound) =~ s/([\\'"\n\r\0\x1a])/$escape{$1}/g; | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 118 | $form{$escaped} = 1; | |||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||||
| 117 | 118 | (my $doubled = $bound) =~ s/(['"])/$1$1/g; | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 118 | $form{$doubled} = 1; | |||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||
| 120 | 118 | for my $text (keys %form) { | |||||||||||||||||||||||||||||||||||||||||||
| 121 | 125 | my $bytes = $text; | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 125 | utf8::encode($bytes); | |||||||||||||||||||||||||||||||||||||||||||
| 123 | 125 | $form{$bytes} = 1; | |||||||||||||||||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||
| 126 | 118 | return keys %form; | |||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||||
| 130 | 385 | ▶ | sub mask_error_details { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mask_error_details: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 131 | 385 | my ($message, $binds, $statement) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 385 | ▶ | return $message unless defined $message && length $message; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $message and length $message
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
| |||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||
| 134 | 383 | $message = _mask_syntax_error($message); | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 383 | $message = _mask_sentences($message); | |||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||
| 137 | 44 | my @bound = sort { length $b <=> length $a } | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 118 | map { _bound_forms($_) } | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 383 | ▶ | grep { defined && !ref && length >= $MIN_BOUND_LENGTH } @{ $binds // [] }; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%8/8
defined $_ && !ref($_) && length $_ >= $MIN_BOUND_LENGTH
defined $_ and not ref $_
$binds // []
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
Truth table100%2/2
A
A= $binds
| |||||||||||||||||||||||||||||||||||||||||||||
| 140 | 383 | my %seen; | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 383 | for my $bound (grep { !$seen{$_}++ } @bound) { | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 119 | $message =~ s/\Q$bound\E/$MASK/g; | |||||||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||
| 145 | 383 | return $message; | |||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||
| 149 | 50 | 1; | |||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||
| 151 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||