| 1 | package DBIx::Fast::X; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 38 | ▶ | use v5.38; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 38 | ▶ | use Object::Pad 0.807; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||
| 6 | 38 | our $VERSION = '0.22'; | |||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||
| 8 | # Typed exceptions. Every object stringifies to exactly the text the plain | ||||||||||||||||||||||||||||||||||||
| 9 | # string exception used to carry, so `$@ =~ /.../`, logs and "$@" keep | ||||||||||||||||||||||||||||||||||||
| 10 | # working; callers that want structure test the class: | ||||||||||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||||||||||
| 12 | # if ($@ isa DBIx::Fast::X::Duplicate) { ... $@->constraint ... } | ||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | class DBIx::Fast::X { | ||||||||||||||||||||||||||||||||||||
| 15 | use overload | ||||||||||||||||||||||||||||||||||||
| 16 | 267 | ▶ | '""' => method (@) { $self->message }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 17 | 2 | ▶ | 'bool' => sub { 1 }, | ||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 18 | 38 | fallback => 1; | |||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||
| 20 | 267 | ▶ | field $message :param :reader; # full text, as a string die had | ||||||||||||||||||||||||||||||||||
|
Subroutine
message: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 21 | field $error :param :reader = undef; # the bare driver/validation text | ||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||
| 23 | # JSON encoders (Mojo::JSON, Cpanel::JSON::XS convert_blessed, Minion's job | ||||||||||||||||||||||||||||||||||||
| 24 | # results) serialize it as the plain message. | ||||||||||||||||||||||||||||||||||||
| 25 | 0 | ▶ | method TO_JSON () { return $message } | ||||||||||||||||||||||||||||||||||
|
Subroutine
TO_JSON: not called CC=1 SCAR=3.5
Subroutine
error: not called CC=1 SCAR=3.5
Pod
documented
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||
| 27 | 0 | ▶ | method throw :common (%args) { die $class->new(%args) } | ||||||||||||||||||||||||||||||||||
|
Subroutine
throw: not called CC=1 SCAR=4.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||
| 30 | # Validation / usage errors raised by DBIx::Fast itself (Exception()). | ||||||||||||||||||||||||||||||||||||
| 31 | 38 | class DBIx::Fast::X::Usage :isa(DBIx::Fast::X) { } | |||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||
| 33 | # Errors reported by the database driver. | ||||||||||||||||||||||||||||||||||||
| 34 | 38 | class DBIx::Fast::X::DB :isa(DBIx::Fast::X) { | |||||||||||||||||||||||||||||||||||
| 35 | field $code :param :reader = undef; # $DBI::err (driver error code) | ||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | field $state :param :reader = undef; # SQLSTATE | ||||||||||||||||||||||||||||||||||
|
Subroutine
code: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||||||
| 37 | 0 | ▶ | field $driver :param :reader = undef; # SQLite / MariaDB / mysql / Pg | ||||||||||||||||||||||||||||||||||
|
Subroutine
state: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | field $sql :param :reader = undef; # the statement in flight | ||||||||||||||||||||||||||||||||||
|
Subroutine
driver: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||||||
| 39 | 0 | ▶ | field $binds :param :reader = undef; # its bind values (arrayref) | ||||||||||||||||||||||||||||||||||
|
Subroutine
sql: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||||||
| 40 | 0 | ▶ | field $constraint :param :reader = undef; # violated key/constraint name | ||||||||||||||||||||||||||||||||||
|
Subroutine
binds: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||
| 42 | # Map a driver error to the most specific subclass. Codes and SQLSTATEs | ||||||||||||||||||||||||||||||||||||
| 43 | # first; SQLite (one generic code for all constraints) by its fixed English | ||||||||||||||||||||||||||||||||||||
| 44 | # message prefix. | ||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | sub class_for ($, $driver, $code, $state, $text) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
class_for: called CC=31 SCAR=50.2
Subroutine
constraint: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||||||
| 46 | 16 | ▶ | $code //= ''; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$code //= ""
MC/DC0%0/1
$code //= ""
$code
Truth table50%1/2
A
A= $code
| |||||||||||||||||||||||||||||||||||||
| 47 | 16 | ▶ | $state //= ''; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$state //= ""
MC/DC0%0/1
$state //= ""
$state
Truth table50%1/2
A
A= $state
| |||||||||||||||||||||||||||||||||||||
| 48 | 16 | ▶ | $text //= ''; | ||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$text //= ""
MC/DC0%0/1
$text //= ""
$text
Truth table50%1/2
A
A= $text
| |||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | 16 | ▶ | if ($driver eq 'MariaDB' || $driver eq 'mysql') { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
$driver eq "MariaDB" or $driver eq "mysql"
MC/DC50%1/2
$driver eq "MariaDB" or $driver eq "mysql"
$driver eq "MariaDB"
$driver eq "mysql"
Truth table66%2/3
A or B
A= $driver eq "MariaDB"B= $driver eq "mysql"
| |||||||||||||||||||||||||||||||||||||
| 51 | 1 | ▶ | return 'DBIx::Fast::X::Duplicate' if $code == 1062 || $code == 1586; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$code == 1062 or $code == 1586
MC/DC0%0/2
$code == 1062 or $code == 1586
$code == 1062
$code == 1586
Truth table33%1/3
A or B
A= $code == 1062B= $code == 1586
| |||||||||||||||||||||||||||||||||||||
| 52 | 0 | ▶ | return 'DBIx::Fast::X::ForeignKey' if $code =~ /\A(?:1216|1217|1451|1452)\z/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 53 | 0 | ▶ | return 'DBIx::Fast::X::NotNull' if $code == 1048 || $code == 1364; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$code == 1048 or $code == 1364
MC/DC0%0/2
$code == 1048 or $code == 1364
$code == 1048
$code == 1364
Truth table0%0/3
A or B
A= $code == 1048B= $code == 1364
| |||||||||||||||||||||||||||||||||||||
| 54 | 0 | ▶ | return 'DBIx::Fast::X::Deadlock' if $code == 1213; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | return 'DBIx::Fast::X::LockTimeout' if $code == 1205; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 56 | 0 | ▶ | return 'DBIx::Fast::X::Syntax' if $code == 1064; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return 'DBIx::Fast::X::Connection' if $code =~ /\A(?:2002|2003|2006|2013|4031)\z/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||||
| 59 | elsif ($driver eq 'Pg') { | ||||||||||||||||||||||||||||||||||||
| 60 | 1 | ▶ | return 'DBIx::Fast::X::Duplicate' if $state eq '23505'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 61 | 1 | ▶ | return 'DBIx::Fast::X::ForeignKey' if $state eq '23503'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 62 | 1 | ▶ | return 'DBIx::Fast::X::NotNull' if $state eq '23502'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | return 'DBIx::Fast::X::Deadlock' if $state eq '40P01' || $state eq '40001'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$state eq "40P01" or $state eq 40001
MC/DC0%0/2
$state eq "40P01" or $state eq 40001
$state eq "40P01"
$state eq 40001
Truth table33%1/3
A or B
A= $state eq "40P01"B= $state eq 40001
| |||||||||||||||||||||||||||||||||||||
| 64 | 1 | ▶ | return 'DBIx::Fast::X::LockTimeout' if $state eq '55P03'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 65 | 1 | ▶ | return 'DBIx::Fast::X::Syntax' if $state eq '42601'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | return 'DBIx::Fast::X::Connection' if $state =~ /\A08/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||
| 68 | elsif ($driver eq 'SQLite') { | ||||||||||||||||||||||||||||||||||||
| 69 | 14 | ▶ | return 'DBIx::Fast::X::Duplicate' | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 70 | if $text =~ /UNIQUE constraint failed|PRIMARY KEY must be unique/; | ||||||||||||||||||||||||||||||||||||
| 71 | 13 | ▶ | return 'DBIx::Fast::X::ForeignKey' if $text =~ /FOREIGN KEY constraint failed/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 72 | 13 | ▶ | return 'DBIx::Fast::X::NotNull' if $text =~ /NOT NULL constraint failed/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 73 | 13 | ▶ | return 'DBIx::Fast::X::LockTimeout' if $code == 5 || $code == 6; # BUSY/LOCKED | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$code == 5 or $code == 6
MC/DC0%0/2
$code == 5 or $code == 6
$code == 5
$code == 6
Truth table33%1/3
A or B
A= $code == 5B= $code == 6
| |||||||||||||||||||||||||||||||||||||
| 74 | 13 | ▶ | return 'DBIx::Fast::X::Syntax' if $text =~ /syntax error/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||
| 76 | 14 | return 'DBIx::Fast::X::DB'; | |||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||
| 79 | # The violated key / constraint name, when the driver message carries it. | ||||||||||||||||||||||||||||||||||||
| 80 | 16 | ▶ | sub constraint_from ($, $text) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
constraint_from: called CC=6 SCAR=18.8
| |||||||||||||||||||||||||||||||||||||
| 81 | 16 | ▶ | return undef unless defined $text; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 82 | 16 | ▶ | return $1 if $text =~ /for key '([^']+)'/; # MySQL/MariaDB | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 83 | 16 | ▶ | return $1 if $text =~ /(?:unique|foreign key|check) constraint "([^"]+)"/; # Pg | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 84 | 16 | ▶ | return $1 if $text =~ /(?:UNIQUE|NOT NULL) constraint failed: (\S+)/; # SQLite | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 85 | 15 | ▶ | return $1 if $text =~ /FOREIGN KEY \(`?([^`)]+)`?\)/; # MySQL FK column | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 86 | 15 | return undef; | |||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||
| 90 | 38 | class DBIx::Fast::X::Duplicate :isa(DBIx::Fast::X::DB) { } | |||||||||||||||||||||||||||||||||||
| 91 | 38 | class DBIx::Fast::X::ForeignKey :isa(DBIx::Fast::X::DB) { } | |||||||||||||||||||||||||||||||||||
| 92 | 38 | class DBIx::Fast::X::NotNull :isa(DBIx::Fast::X::DB) { } | |||||||||||||||||||||||||||||||||||
| 93 | 38 | class DBIx::Fast::X::Deadlock :isa(DBIx::Fast::X::DB) { } | |||||||||||||||||||||||||||||||||||
| 94 | 38 | class DBIx::Fast::X::LockTimeout :isa(DBIx::Fast::X::DB) { } | |||||||||||||||||||||||||||||||||||
| 95 | 38 | class DBIx::Fast::X::Syntax :isa(DBIx::Fast::X::DB) { } | |||||||||||||||||||||||||||||||||||
| 96 | 38 | class DBIx::Fast::X::Connection :isa(DBIx::Fast::X::DB) { } | |||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||
| 98 | 38 | 1; | |||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||
| 100 | __END__ | ||||||||||||||||||||||||||||||||||||