Fast/X.pm

filter: Statementstmt 67.7% 61 / 90 Branchbran 46.3% 25 / 54 Conditioncond 38.1% 8 / 21 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 47.0% 8 / 17 Podpod 100% 4 / 4 totaltotal 53.7% 107 / 199 CC 36 SCAR 45.1
CC
36
Cov
57%
class_for
151.1
constraint_from
6.6
throw
1.6
CRAP 91.2
« Fast/Profile/MariaDB.pm ↑ Summary Fast.pm »
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 //= ""
l!l
160
MC/DC0%0/1
$code //= ""
$code
Truth table50%1/2
A
A= $code
Aresult
11
00
47 16 ▶ $state //= '';
Condition50%1/2
$state //= ""
l!l
160
MC/DC0%0/1
$state //= ""
$state
Truth table50%1/2
A
A= $state
Aresult
11
00
48 16 ▶ $text //= '';
Condition50%1/2
$text //= ""
l!l
160
MC/DC0%0/1
$text //= ""
$text
Truth table50%1/2
A
A= $text
Aresult
11
00
49  
50 16 ▶ if ($driver eq 'MariaDB' || $driver eq 'mysql') {
Branch: 3 branches
BranchTrueFalseTotal
if ($driver eq 'MariaDB' or $driver eq 'mysql') { } 1 15 16
elsif ($driver eq 'Pg') { } 1 14 15
elsif ($driver eq 'SQLite') { } 14 0 14
Condition66%2/3
$driver eq "MariaDB" or $driver eq "mysql"
l!l&&r!l&&!r
1015
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 14 ▶ return 'DBIx::Fast::X::Duplicate'
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /UNIQUE constraint failed|PRIMARY KEY must be unique/u 1 13 14
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
BranchTrueFalseTotal
if $text =~ /FOREIGN KEY constraint failed/u 0 13 13
72 13 ▶ return 'DBIx::Fast::X::NotNull' if $text =~ /NOT NULL constraint failed/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /NOT NULL constraint failed/u 0 13 13
73 13 ▶ return 'DBIx::Fast::X::LockTimeout' if $code == 5 || $code == 6; # BUSY/LOCKED
Branch: 1 branch
BranchTrueFalseTotal
if $code == 5 or $code == 6 0 13 13
Condition33%1/3
$code == 5 or $code == 6
l!l&&r!l&&!r
0013
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 13 ▶ return 'DBIx::Fast::X::Syntax' if $text =~ /syntax error/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /syntax error/u 0 13 13
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
BranchTrueFalseTotal
unless defined $text 0 16 16
82 16 ▶ return $1 if $text =~ /for key '([^']+)'/; # MySQL/MariaDB
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /for key '([^']+)'/u 0 16 16
83 16 ▶ return $1 if $text =~ /(?:unique|foreign key|check) constraint "([^"]+)"/; # Pg
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /(?:unique|foreign key|check) constraint "([^"]+)"/u 0 16 16
84 16 ▶ return $1 if $text =~ /(?:UNIQUE|NOT NULL) constraint failed: (\S+)/; # SQLite
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /(?:UNIQUE|NOT NULL) constraint failed: (\S+)/u 1 15 16
85 15 ▶ return $1 if $text =~ /FOREIGN KEY \(`?([^`)]+)`?\)/; # MySQL FK column
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /FOREIGN KEY \(`?([^`)]+)`?\)/u 0 15 15
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__
« Fast/Profile/MariaDB.pm ↑ Summary Fast.pm »