| 1 | package DBIO::MSSQL::Util; | ||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Shared MSSQL-specific helper functions for DBIO | ||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||
| 4 | 25 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 5 | 25 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||||
| 7 | 25 | our $VERSION = '0.900004'; | |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||
| 9 | 25 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | 25 | our @EXPORT_OK = qw(mask_error_details); | |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||||
| 13 | # What stands in for a value in an error text. | ||||||||||||||||||||||||||||||||||||||||||||
| 14 | 25 | 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 server's | ||||||||||||||||||||||||||||||||||||||||||||
| 18 | # sentences that quote a value are known below and catch those. | ||||||||||||||||||||||||||||||||||||||||||||
| 19 | 25 | my $MIN_BOUND_LENGTH = 4; | |||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | # A value in single quotes, an apostrophe inside written twice. Possessive, so | ||||||||||||||||||||||||||||||||||||||||||||
| 22 | # a text without the closing quote costs one pass and no backtracking. | ||||||||||||||||||||||||||||||||||||||||||||
| 23 | 25 | my $QUOTED = qr/'(?:[^']++|'')*+'/; | |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||
| 26 | # The sentences of the server that hold a value, one rule each. A rule keeps | ||||||||||||||||||||||||||||||||||||||||||||
| 27 | # the words around the value -- the type, the table, the column, the | ||||||||||||||||||||||||||||||||||||||||||||
| 28 | # constraint -- and replaces the value. The rules work on the text of a | ||||||||||||||||||||||||||||||||||||||||||||
| 29 | # server message, so the frame the DBD puts around it (DBD::Sybase: "Server | ||||||||||||||||||||||||||||||||||||||||||||
| 30 | # message number=245 ... text=", DBD::ODBC: "[FreeTDS][SQL Server]" before and | ||||||||||||||||||||||||||||||||||||||||||||
| 31 | # "(SQL-22018)" after) and further messages after it do not matter. Every rule | ||||||||||||||||||||||||||||||||||||||||||||
| 32 | # gives the same text again when it is applied to its own result. | ||||||||||||||||||||||||||||||||||||||||||||
| 33 | 443 | ▶ | sub _mask_sentences { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mask_sentences: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 34 | 443 | my ($text) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||
| 36 | # Where a message ends: the end of the text, the "(SQL-nnnnn)" of DBD::ODBC, | ||||||||||||||||||||||||||||||||||||||||||||
| 37 | # the next message. | ||||||||||||||||||||||||||||||||||||||||||||
| 38 | 443 | my $END = qr/(?=\s*(?:\(SQL-\d+\)|Server message number=|\[FreeTDS\]|\z))/; | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | # The server writes most values as they are, an apostrophe in them too | ||||||||||||||||||||||||||||||||||||||||||||
| 41 | # (value 'a'b' to data type int; near 'it's'); only "Truncated value" doubles | ||||||||||||||||||||||||||||||||||||||||||||
| 42 | # it. So the value is what stands between the opening quote and the words | ||||||||||||||||||||||||||||||||||||||||||||
| 43 | # that close the sentence at the end of the message. A message is cut at about | ||||||||||||||||||||||||||||||||||||||||||||
| 44 | # 2000 characters, so the value is looked for within that. The failing | ||||||||||||||||||||||||||||||||||||||||||||
| 45 | # conversion of the TEXTSIZE guard of the Sybase class (its text names a | ||||||||||||||||||||||||||||||||||||||||||||
| 46 | # column, no value) is left to that class. | ||||||||||||||||||||||||||||||||||||||||||||
| 47 | 443 | $text =~ s/(\bConversion failed when converting the \w+ value )'(?!DBIO::MSSQL TEXTSIZE guard: column ).{0,2100}?'( to data type [\w ]+(?:\(\d+(?:\s*,\s*\d+)?\))?\.)$END/$1'$MASK'$2/gs; | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 443 | $text =~ s/(\bThe conversion of the \w+ value )'.{0,2100}?'( overflowed an \w+ column\.(?: Use a larger integer column\.)?)$END/$1'$MASK'$2/gs; | |||||||||||||||||||||||||||||||||||||||||||
| 49 | 443 | $text =~ s/(\bIncorrect syntax near )'.{0,2100}?'(\.)$END/$1'$MASK'$2/gs; | |||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||
| 51 | # Conversion failed when converting the varchar value 'x' to data type int. | ||||||||||||||||||||||||||||||||||||||||||||
| 52 | 443 | $text =~ s/(\bConversion failed when converting the \w+ value )$QUOTED( to data type )/$1'$MASK'$2/g; | |||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | # The conversion of the varchar value '3000000000' overflowed an INT2 column. | ||||||||||||||||||||||||||||||||||||||||||||
| 55 | 443 | $text =~ s/(\bThe conversion of the \w+ value )$QUOTED( overflowed an )/$1'$MASK'$2/g; | |||||||||||||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||||||||||||||
| 57 | # ... The duplicate key value is (11, 22). The text has no column names and | ||||||||||||||||||||||||||||||||||||||||||||
| 58 | # a value may hold ", " and ")", so all up to the last ")." is the value. | ||||||||||||||||||||||||||||||||||||||||||||
| 59 | 443 | $text =~ s/(\bThe duplicate key value is \().*(\)\.)/$1$MASK$2/s; | |||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | # String or binary data would be truncated in table 't', column 's'. Truncated value: 'x'. | ||||||||||||||||||||||||||||||||||||||||||||
| 62 | 443 | $text =~ s/(\bTruncated value: )$QUOTED/$1'$MASK'/g; | |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | # Incorrect syntax near 'GARBAGE'. (the form with a keyword has no quotes | ||||||||||||||||||||||||||||||||||||||||||||
| 65 | # right after "near" and is not touched) | ||||||||||||||||||||||||||||||||||||||||||||
| 66 | 443 | $text =~ s/(\bIncorrect syntax near )$QUOTED/$1'$MASK'/g; | |||||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | # JSON text is not properly formatted. Unexpected character 'g' is found at position 1. | ||||||||||||||||||||||||||||||||||||||||||||
| 69 | # The character may be a quote itself, so it is not read as a quoted value. | ||||||||||||||||||||||||||||||||||||||||||||
| 70 | 443 | $text =~ s/(\bUnexpected character )'.{1,8}?'( is found at position )/$1'$MASK'$2/gs; | |||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | # The server cuts a message at about 2000 characters. A long value then runs | ||||||||||||||||||||||||||||||||||||||||||||
| 73 | # to the end of the message, without the closing quote and without the words | ||||||||||||||||||||||||||||||||||||||||||||
| 74 | # behind it, and the rules above do not match. The value is what is left of | ||||||||||||||||||||||||||||||||||||||||||||
| 75 | # the message up to its end. | ||||||||||||||||||||||||||||||||||||||||||||
| 76 | 443 | $text =~ s/(\bConversion failed when converting the \w+ value )'(?:[^']|'')*?$END/$1'$MASK'/g; | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 443 | $text =~ s/(\bThe conversion of the \w+ value )'(?:[^']|'')*?$END/$1'$MASK'/g; | |||||||||||||||||||||||||||||||||||||||||||
| 78 | 443 | $text =~ s/(\bTruncated value: )'(?:[^']|'')*?$END/$1'$MASK'/g; | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 443 | $text =~ s/(\bThe duplicate key value is \()(?!\Q$MASK\E\)\.)(?:(?!\)\.).)*?$END/$1$MASK/gs; | |||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | 443 | return $text; | |||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||
| 85 | 445 | ▶ | sub mask_error_details { | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mask_error_details: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 445 | my ($message, $binds, $statement) = @_; | |||||||||||||||||||||||||||||||||||||||||||
| 87 | 445 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||
| 89 | 443 | $message = _mask_sentences($message); | |||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | 443 | my %bound; | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 443 | ▶ | for my $value (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
| |||||||||||||||||||||||||||||||||||||||||||||
| 93 | 28 | $bound{$value} = 1; | |||||||||||||||||||||||||||||||||||||||||||
| 94 | 28 | (my $literal = $value) =~ s/'/''/g; | |||||||||||||||||||||||||||||||||||||||||||
| 95 | 28 | $bound{$literal} = 1; | |||||||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 97 | 443 | ▶ | for my $value (sort { length $b <=> length $a || $a cmp $b } keys %bound) { | ||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
length $b <=> length $a || $a cmp $b
MC/DC0%0/2
length $b <=> length $a || $a cmp $b
length $b <=> length $a
$a cmp $b
Truth table33%1/3
A || B
A= length $b <=> length $aB= $a cmp $b
| |||||||||||||||||||||||||||||||||||||||||||||
| 98 | 29 | $message =~ s/\Q$value\E/$MASK/g; | |||||||||||||||||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | 443 | return $message; | |||||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||
| 105 | 25 | 1; | |||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||