Fast/X.pm

filter: Statementstmt 85.5% 77 / 90 Branchbran 51.8% 28 / 54 Conditioncond 38.1% 8 / 21 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 88.2% 15 / 17 Podpod 100% 4 / 4 totaltotal 66.8% 133 / 199 CC 36 SCAR 42.5
CC
36
Cov
68%
class_for
116.1
constraint_from
6.6
throw
1.6
CRAP 70.3
« Fast/Profile/MariaDB.pm ↑ Summary Fast/Profile/Pg.pm »
1 package DBIx::Fast::X;
2  
3 35 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 35 ▶use Object::Pad 0.807;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 35 our $VERSION = '0.21';
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 158 ▶ '""' => method (@) { $self->message },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
17 1 ▶ 'bool' => sub { 1 },
Subroutine __ANON__: called CC=1 SCAR=0.0
18 35 fallback => 1;
19  
20 158 ▶ 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 1 ▶ method TO_JSON () { return $message }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Subroutine error: called CC=1 SCAR=0.0
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 35 class DBIx::Fast::X::Usage :isa(DBIx::Fast::X) { }
32  
33 # Errors reported by the database driver.
34 35 class DBIx::Fast::X::DB :isa(DBIx::Fast::X) {
35 field $code :param :reader = undef; # $DBI::err (driver error code)
36 1 ▶ field $state :param :reader = undef; # SQLSTATE
Subroutine code: called CC=1 SCAR=0.0
37 0 ▶ field $driver :param :reader = undef; # SQLite / MariaDB / mysql / Pg
Subroutine state: not called CC=1 SCAR=2.6
38 1 ▶ field $sql :param :reader = undef; # the statement in flight
Subroutine driver: called CC=1 SCAR=0.0
39 1 ▶ field $binds :param :reader = undef; # its bind values (arrayref)
Subroutine sql: called CC=1 SCAR=0.0
40 1 ▶ field $constraint :param :reader = undef; # violated key/constraint name
Subroutine binds: called CC=1 SCAR=0.0
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 1 ▶ sub class_for ($, $driver, $code, $state, $text) {
Subroutine class_for: called CC=31 SCAR=47.5
Subroutine constraint: called CC=1 SCAR=0.0
46 13 ▶ $code //= '';
Condition50%1/2
$code //= ""
l!l
130
MC/DC0%0/1
$code //= ""
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
47 13 ▶ $state //= '';
Condition50%1/2
$state //= ""
l!l
130
MC/DC0%0/1
$state //= ""
$state
Truth table50%1/2
A
A= $state
Aresult
11
00
48 13 ▶ $text //= '';
Condition50%1/2
$text //= ""
l!l
130
MC/DC0%0/1
$text //= ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
49  
50 13 ▶ if ($driver eq 'MariaDB' || $driver eq 'mysql') {
Branch: 3 branches
BranchTrueFalseTotal
if ($driver eq 'MariaDB' or $driver eq 'mysql') { } 1 12 13
elsif ($driver eq 'Pg') { } 1 11 12
elsif ($driver eq 'SQLite') { } 11 0 11
Condition66%2/3
$driver eq "MariaDB" or $driver eq "mysql"
l!l&&r!l&&!r
1012
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"
ABresult
1X1
011
000
51 1 ▶ return 'DBIx::Fast::X::Duplicate' if $code == 1062 || $code == 1586;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 1062 or $code == 1586 1 0 1
Condition33%1/3
$code == 1062 or $code == 1586
l!l&&r!l&&!r
100
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
ABresult
1X1
011
000
52 0 ▶ return 'DBIx::Fast::X::ForeignKey' if $code =~ /\A(?:1216|1217|1451|1452)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /\A(?:1216|1217|1451|1452)\z/u 0 0 0
53 0 ▶ return 'DBIx::Fast::X::NotNull' if $code == 1048 || $code == 1364;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 1048 or $code == 1364 0 0 0
Condition0%0/3
$code == 1048 or $code == 1364
l!l&&r!l&&!r
000
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
ABresult
1X1
011
000
54 0 ▶ return 'DBIx::Fast::X::Deadlock' if $code == 1213;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 1213 0 0 0
55 0 ▶ return 'DBIx::Fast::X::LockTimeout' if $code == 1205;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 1205 0 0 0
56 0 ▶ return 'DBIx::Fast::X::Syntax' if $code == 1064;
Branch: 1 branch
BranchTrueFalseTotal
if $code == 1064 0 0 0
57 0 ▶ return 'DBIx::Fast::X::Connection' if $code =~ /\A(?:2002|2003|2006|2013|4031)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $code =~ /\A(?:2002|2003|2006|2013|4031)\z/u 0 0 0
58 }
59 elsif ($driver eq 'Pg') {
60 1 ▶ return 'DBIx::Fast::X::Duplicate' if $state eq '23505';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq 23505 0 1 1
61 1 ▶ return 'DBIx::Fast::X::ForeignKey' if $state eq '23503';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq 23503 0 1 1
62 1 ▶ return 'DBIx::Fast::X::NotNull' if $state eq '23502';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq 23502 0 1 1
63 1 ▶ return 'DBIx::Fast::X::Deadlock' if $state eq '40P01' || $state eq '40001';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "40P01" or $state eq 40001 0 1 1
Condition33%1/3
$state eq "40P01" or $state eq 40001
l!l&&r!l&&!r
001
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
ABresult
1X1
011
000
64 1 ▶ return 'DBIx::Fast::X::LockTimeout' if $state eq '55P03';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq "55P03" 0 1 1
65 1 ▶ return 'DBIx::Fast::X::Syntax' if $state eq '42601';
Branch: 1 branch
BranchTrueFalseTotal
if $state eq 42601 0 1 1
66 1 ▶ return 'DBIx::Fast::X::Connection' if $state =~ /\A08/;
Branch: 1 branch
BranchTrueFalseTotal
if $state =~ /\A08/u 0 1 1
67 }
68 elsif ($driver eq 'SQLite') {
69 11 ▶ return 'DBIx::Fast::X::Duplicate'
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /UNIQUE constraint failed|PRIMARY KEY must be unique/u 4 7 11
70 if $text =~ /UNIQUE constraint failed|PRIMARY KEY must be unique/;
71 7 ▶ return 'DBIx::Fast::X::ForeignKey' if $text =~ /FOREIGN KEY constraint failed/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /FOREIGN KEY constraint failed/u 1 6 7
72 6 ▶ return 'DBIx::Fast::X::NotNull' if $text =~ /NOT NULL constraint failed/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /NOT NULL constraint failed/u 1 5 6
73 5 ▶ return 'DBIx::Fast::X::LockTimeout' if $code == 5 || $code == 6; # BUSY/LOCKED
Branch: 1 branch
BranchTrueFalseTotal
if $code == 5 or $code == 6 0 5 5
Condition33%1/3
$code == 5 or $code == 6
l!l&&r!l&&!r
005
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
ABresult
1X1
011
000
74 5 ▶ return 'DBIx::Fast::X::Syntax' if $text =~ /syntax error/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /syntax error/u 2 3 5
75 }
76 4 return 'DBIx::Fast::X::DB';
77 }
78  
79 # The violated key / constraint name, when the driver message carries it.
80 13 ▶ sub constraint_from ($, $text) {
Subroutine constraint_from: called CC=6 SCAR=18.8
81 13 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 13 13
82 13 ▶ return $1 if $text =~ /for key '([^']+)'/; # MySQL/MariaDB
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /for key '([^']+)'/u 0 13 13
83 13 ▶ return $1 if $text =~ /(?:unique|foreign key|check) constraint "([^"]+)"/; # Pg
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /(?:unique|foreign key|check) constraint "([^"]+)"/u 0 13 13
84 13 ▶ return $1 if $text =~ /(?:UNIQUE|NOT NULL) constraint failed: (\S+)/; # SQLite
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /(?:UNIQUE|NOT NULL) constraint failed: (\S+)/u 5 8 13
85 8 ▶ return $1 if $text =~ /FOREIGN KEY \(`?([^`)]+)`?\)/; # MySQL FK column
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /FOREIGN KEY \(`?([^`)]+)`?\)/u 0 8 8
86 8 return undef;
87 }
88 }
89  
90 35 class DBIx::Fast::X::Duplicate :isa(DBIx::Fast::X::DB) { }
91 35 class DBIx::Fast::X::ForeignKey :isa(DBIx::Fast::X::DB) { }
92 35 class DBIx::Fast::X::NotNull :isa(DBIx::Fast::X::DB) { }
93 35 class DBIx::Fast::X::Deadlock :isa(DBIx::Fast::X::DB) { }
94 35 class DBIx::Fast::X::LockTimeout :isa(DBIx::Fast::X::DB) { }
95 35 class DBIx::Fast::X::Syntax :isa(DBIx::Fast::X::DB) { }
96 35 class DBIx::Fast::X::Connection :isa(DBIx::Fast::X::DB) { }
97  
98 35 1;
99  
100 __END__
« Fast/Profile/MariaDB.pm ↑ Summary Fast/Profile/Pg.pm »