| 1 | package DBIO::Oracle::Util; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Shared Oracle-specific helper functions for DBIO | ||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 23 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 23 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 23 | our $VERSION = '0.900004'; | |||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 23 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 23 | our @EXPORT_OK = qw(mask_error_details); | |||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # What stands in for a value in an error text. | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 23 | 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 | 23 | my $MIN_BOUND_LENGTH = 4; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # What may follow the last value of a message: the stack lines of a PL/SQL | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # call, the Help line of 23ai, the DBD's tag without a statement (also the bare | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # SUCCESS_WITH_INFO of an array execution) and the | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # count of the array execution. A value is masked up to the first place from | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # where the text is nothing but this; a value that holds a line of this kind | ||||||||||||||||||||||||||||||||||||||||||
| 26 | # in its middle is thereby masked as a whole. A text that does not end this way | ||||||||||||||||||||||||||||||||||||||||||
| 27 | # is masked to its end. | ||||||||||||||||||||||||||||||||||||||||||
| 28 | 23 | my $END = qr{ | |||||||||||||||||||||||||||||||||||||||||
| 29 | (?:\nORA-06512:\ at\ [^\n]*)* | ||||||||||||||||||||||||||||||||||||||||||
| 30 | (?:\nHelp:\ https://docs\.oracle\.com/error-help/\S*)? | ||||||||||||||||||||||||||||||||||||||||||
| 31 | (?:[ \t]*\(DBD\ [A-Z_]+(?::\ OCI\w+)?\))? | ||||||||||||||||||||||||||||||||||||||||||
| 32 | (?:\nexecuting\ \d+\ generated\ \d+\ errors)? | ||||||||||||||||||||||||||||||||||||||||||
| 33 | \s*\z | ||||||||||||||||||||||||||||||||||||||||||
| 34 | }x; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # A value that runs up to $END (or, if there is none, to the end of the text). | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 23 | my $VALUE = qr/(?:.*?(?=$END)|.*)/s; | |||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | # The statement the DBD appends to a message ("(DBD ERROR: error possibly near | ||||||||||||||||||||||||||||||||||||||||||
| 41 | # <*> indicator at char 39 in 'SELECT ...')") is the caller's own text, with | ||||||||||||||||||||||||||||||||||||||||||
| 42 | # the placeholders written :p1, :p2 and the marker <*> put in. It is split off | ||||||||||||||||||||||||||||||||||||||||||
| 43 | # only when it is that: the statement inside, without the marker and with the | ||||||||||||||||||||||||||||||||||||||||||
| 44 | # placeholders as ?, is $statement the same way, and the marker stands where | ||||||||||||||||||||||||||||||||||||||||||
| 45 | # the char number says. Returns ($message, $appendix); the appendix is empty | ||||||||||||||||||||||||||||||||||||||||||
| 46 | # unless it is the DBD's own. | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 108 | ▶ | sub _split_appendix { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_split_appendix: called CC=8 SCAR=20.8
| |||||||||||||||||||||||||||||||||||||||||||
| 48 | 108 | my ($message, $statement) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | 108 | ▶ | return ($message, '') unless defined $statement && !ref $statement && length $statement; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined $statement and not ref $statement and length $statement
defined $statement and not ref $statement
MC/DC33%1/3
defined $statement and not ref $statement and length $statement
defined $statement
not ref $statement
length $statement
Truth table50%2/4
A and not(B) and C
A= defined $statementB= not ref $statementC= length $statement
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 7 | ▶ | $message =~ /\A(.*)\s*\(DBD\ [A-Z_]+:\ error\ possibly\ near\ <\*>\ indicator\ at\ char\ (\d+)\ in\ '(.*)'\)(?:\nexecuting\ \d+\ generated\ \d+\ errors)?\z/xs | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | or return ($message, ''); | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 6 | my ($head, $at, $inner) = ($1, $2, $3); | |||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 6 | ▶ | return ($message, '') unless substr($inner, $at, 3) eq '<*>'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 5 | substr($inner, $at, 3, ''); | |||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | 5 | my $given = $statement; | |||||||||||||||||||||||||||||||||||||||||
| 59 | 5 | s/:p\d+/?/g for $inner, $given; | |||||||||||||||||||||||||||||||||||||||||
| 60 | 5 | ▶ | return ($message, '') unless $inner eq $given; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | 4 | return ($head, substr($message, length $head)); | |||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | # "A:1, B:'x'": the columns and the values of a key (the text between the | ||||||||||||||||||||||||||||||||||||||||||
| 66 | # brackets of ORA-03301). Oracle escapes nothing, so a value may hold ", " or | ||||||||||||||||||||||||||||||||||||||||||
| 67 | # ":" or a quote. The columns are known from the line of ORA-00001 before | ||||||||||||||||||||||||||||||||||||||||||
| 68 | # (@names), and the text is read as a pair list only where that is the one | ||||||||||||||||||||||||||||||||||||||||||
| 69 | # reading: it starts with the first column, and every following ", NAME:" is in | ||||||||||||||||||||||||||||||||||||||||||
| 70 | # it exactly once, in order. Anything else is masked whole. | ||||||||||||||||||||||||||||||||||||||||||
| 71 | 32 | ▶ | sub _mask_key_values { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mask_key_values: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||||||||||||||||
| 72 | 32 | my ($names, $content) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | 32 | ▶ | return $MASK unless @$names && !grep { !length } @$names; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@$names and not grep({not length $_;} @$names)
MC/DC0%0/2
@$names and not grep({not length $_;} @$names)
@$names
not grep({not length $_;} @$names)
Truth table33%1/3
A and not(B)
A= @$namesB= not grep({not length $_;} @$names)
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 32 | ▶ | return $MASK unless index($content, "$names->[0]:") == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | 31 | my $pos = length($names->[0]) + 1; | |||||||||||||||||||||||||||||||||||||||||
| 78 | 31 | for my $n (@{$names}[1 .. $#$names]) { | |||||||||||||||||||||||||||||||||||||||||
| 79 | 5 | my $delimiter = ", $n:"; | |||||||||||||||||||||||||||||||||||||||||
| 80 | 5 | my $first = index($content, $delimiter, $pos); | |||||||||||||||||||||||||||||||||||||||||
| 81 | 5 | ▶ | return $MASK if $first < 0 || index($content, $delimiter, $first + 1) >= 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$first < 0 or index($content, $delimiter, $first + 1) != -1
MC/DC50%1/2
$first < 0 or index($content, $delimiter, $first + 1) != -1
$first < 0
index($content, $delimiter, $first + 1) != -1
Truth table66%2/3
A or B
A= $first < 0B= index($content, $delimiter, $first + 1) != -1
| |||||||||||||||||||||||||||||||||||||||||||
| 82 | 4 | $pos = $first + length $delimiter; | |||||||||||||||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 30 | return join ', ', map { "$_:$MASK" } @$names; | |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||
| 88 | # The sentences of the server that hold a value, one rule each. A rule keeps | ||||||||||||||||||||||||||||||||||||||||||
| 89 | # the words around the value -- the type, the column, the constraint -- and | ||||||||||||||||||||||||||||||||||||||||||
| 90 | # replaces the value, wherever the server puts quotes, so a rule never leaves | ||||||||||||||||||||||||||||||||||||||||||
| 91 | # a part of it. Every rule gives the same text again when it is applied to its | ||||||||||||||||||||||||||||||||||||||||||
| 92 | # own result. | ||||||||||||||||||||||||||||||||||||||||||
| 93 | 108 | ▶ | sub _mask_sentences { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_mask_sentences: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 94 | 108 | my ($text) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | # ORA-01722: unable to convert string value containing 'g' to a number: | ||||||||||||||||||||||||||||||||||||||||||
| 97 | # the first character that is not a digit: 'g', ' ', UNISTR('\FFFD') | ||||||||||||||||||||||||||||||||||||||||||
| 98 | 108 | $text =~ s/(unable to convert string value containing ).*?( to a number: )/$1'$MASK'$2/s; | |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | # ORA-03302: (ORA-01722 details) invalid string value: geheim42 | ||||||||||||||||||||||||||||||||||||||||||
| 101 | 108 | $text =~ s/(\(ORA-\d+ details\) invalid string value: )$VALUE/$1$MASK/s; | |||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | # ORA-61800: invalid boolean literal: geheim42 | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 108 | $text =~ s/(\binvalid boolean literal: )$VALUE/$1$MASK/s; | |||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | # ORA-01438: value 99999.9 greater than specified precision (4, 1) for column | ||||||||||||||||||||||||||||||||||||||||||
| 107 | 108 | $text =~ s/(\bvalue )\S+( greater than specified precision )/$1$MASK$2/g; | |||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | # ORA-01428: Argument -4 is out of range. | ||||||||||||||||||||||||||||||||||||||||||
| 110 | 108 | $text =~ s/(\bArgument )\S+( is out of range\.)/$1$MASK$2/g; | |||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | # ORA-00001 with the key of the row, 23ai: | ||||||||||||||||||||||||||||||||||||||||||
| 113 | # violated on table APP.USERS columns (A, B) | ||||||||||||||||||||||||||||||||||||||||||
| 114 | # ORA-03301: (ORA-00001 details) row with column values (A:1, B:'x') already exists | ||||||||||||||||||||||||||||||||||||||||||
| 115 | 108 | $text =~ s{((?:\bcolumns \(([^\n]*?)\)\n)?ORA-03301: \(ORA-\d+ details\) row with column values \()($VALUE)(\) already exists)(?=$END)} | |||||||||||||||||||||||||||||||||||||||||
| 116 | 34 | ▶ | {$1 . (defined $2 ? _mask_key_values([ split /, /, $2, -1 ], $3) : $MASK) . $4}se; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | 108 | return $text; | |||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | 110 | ▶ | sub mask_error_details { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
mask_error_details: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | 110 | my ($message, $binds, $statement) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 124 | 110 | ▶ | 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
| |||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | 108 | my $tail; | |||||||||||||||||||||||||||||||||||||||||
| 127 | 108 | ($message, $tail) = _split_appendix($message, $statement); | |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | 108 | $message = _mask_sentences($message); | |||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 108 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 132 | 1 | my @bound = sort { length $b <=> length $a } | |||||||||||||||||||||||||||||||||||||||||
| 133 | 10 | grep { !$seen{$_}++ } | |||||||||||||||||||||||||||||||||||||||||
| 134 | 9 | ▶ | map { my $v = $_; (index($v, "'") >= 0 ? ($v, $v =~ s/'/''/gr) : ($v)) } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 14 | ▶ | grep { defined && !ref && length >= $MIN_BOUND_LENGTH } | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%6/6
defined $_ && !ref($_) && length $_ >= $MIN_BOUND_LENGTH
defined $_ and not ref $_
MC/DC100%3/3
defined $_ && !ref($_) && length $_ >= $MIN_BOUND_LENGTH
defined $_
not ref $_
length $_ >= $MIN_BOUND_LENGTH
Truth table100%4/4
A and not(B) && C
A= defined $_B= not ref $_C= length $_ >= $MIN_BOUND_LENGTH
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 108 | ▶ | map { ref($_) eq 'ARRAY' && @$_ == 2 ? $_->[1] : $_ } @{ $binds // [] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
ref $_ eq "ARRAY" and @$_ == 2
$binds // []
MC/DC100%2/2
ref $_ eq "ARRAY" and @$_ == 2
ref $_ eq "ARRAY"
@$_ == 2
MC/DC100%1/1
$binds // []
$binds
Truth table100%3/3
A and B
A= ref $_ eq "ARRAY"B= @$_ == 2
Truth table100%2/2
A
A= $binds
| |||||||||||||||||||||||||||||||||||||||||||
| 137 | 108 | for my $bound (@bound) { | |||||||||||||||||||||||||||||||||||||||||
| 138 | 10 | $message =~ s/\Q$bound\E/$MASK/g; | |||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||
| 141 | 108 | return $message . $tail; | |||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||
| 145 | 23 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | __END__ | ||||||||||||||||||||||||||||||||||||||||||