DBIx/Class/Storage/TxnScopeGuard.pm

filter: Statementstmt 98.1% 54 / 55 Branchbran 79.1% 19 / 24 Conditioncond 60.0% 9 / 15 MC/DCmcdc 25.0% 2 / 8 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 84.6% 99 / 117 CC 18 SCAR 19.8
CC
18
Cov
87%
DESTROY
14.1
new
4.0
__ANON__
2.1
CRAP 7.2
« DBIx/Class/CDBICompat/AttributeAPI.pm ↑ Summary DBIx/Class/CDBICompat/GetSet.pm »
1 package DBIx::Class::Storage::TxnScopeGuard;
2  
3 251 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 251 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 251 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 251 ▶use Scalar::Util qw/weaken blessed refaddr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 251 ▶use DBIx::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 251 ▶use DBIx::Class::_Util 'is_exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 251 ▶use DBIx::Class::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 251 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub new {
13 8593 ▶ my ($class, $storage) = @_;
Subroutine new: called CC=4 SCAR=14.0
Pod documented
14  
15 8593 my $guard = {
16 inactivated => 0,
17 storage => $storage,
18 };
19  
20 # we are starting with an already set $@ - in order for things to work we need to
21 # be able to recognize it upon destruction - store its weakref
22 # recording it before doing the txn_begin stuff
23 #
24 # FIXME FRAGILE - any eval that fails but *does not* rethrow between here
25 # and the unwind will trample over $@ and invalidate the entire mechanism
26 # There got to be a saner way of doing this...
27 8593 ▶ if (is_exception $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (&is_exception($@)) 595 7998 8593
28 weaken(
29 595 ▶ $guard->{existing_exception_ref} = (ref($@) eq '') ? \$@ : $@
Branch: 1 branch
BranchTrueFalseTotal
ref $@ eq '' ? : 2 593 595
30 );
31 }
32  
33 8593 $storage->txn_begin;
34  
35 8593 weaken( $guard->{dbh} = $storage->_dbh );
36  
37 8593 ▶ bless $guard, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
085930
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
38  
39 8593 $guard;
40 }
41  
42 sub commit {
43 8462 ▶ my $self = shift;
Subroutine commit: called CC=2 SCAR=7.0
Pod documented
44  
45 $self->{storage}->throw_exception("Refusing to execute multiple commits on scope guard $self")
46 8462 ▶ if $self->{inactivated};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inactivated"} 0 8462 8462
47  
48 8462 $self->{storage}->txn_commit;
49 8462 $self->{inactivated} = 1;
50 }
51  
52 sub DESTROY {
53 8593 ▶ my $self = shift;
Subroutine DESTROY: called CC=13 SCAR=26.5
54  
55 8593 ▶ return if $self->{inactivated};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"inactivated"} 8462 131 8593
56  
57 # if our dbh is not ours anymore, the $dbh weakref will go undef
58 131 $self->{storage}->_verify_pid unless DBIx::Class::_ENV_::BROKEN_FORK;
59 131 ▶ return unless $self->{dbh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"dbh"} 10 121 131
60  
61 my $exception = $@ if (
62 is_exception $@
63 and
64 (
65 ! defined $self->{existing_exception_ref}
66 or
67 refaddr( ref($@) eq '' ? \$@ : $@ ) != refaddr($self->{existing_exception_ref})
68 121 ▶ )
Branch: 2 branches
BranchTrueFalseTotal
if &is_exception($@) and !defined($self->{"existing_exception_ref"}) || builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"}) 118 3 121
ref $@ eq '' ? : 0 0 0
Condition50%3/6
&is_exception($@) and !defined($self->{"existing_exception_ref"}) || builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
!ll&&!rl&&r
31180
not defined $self->{"existing_exception_ref"} or builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
l!l&&r!l&&!r
11800
MC/DC0%0/3
&is_exception($@) and !defined($self->{"existing_exception_ref"}) || builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
&is_exception($@) not defined $self->{"existing_exception_ref"} builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
Truth table25%1/4
A and not(B) or C
A= &is_exception($@)B= not defined $self->{"existing_exception_ref"}C= builtin::refaddr(ref $@ eq "" ? \$@ : $@) != builtin::refaddr($self->{"existing_exception_ref"})
ABCresult
0XX0
1000
11X1
1011
69 );
70  
71 {
72 121 local $@;
73  
74 121 ▶ carp 'A DBIx::Class::Storage::TxnScopeGuard went out of scope without explicit commit or error. Rolling back.'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exception 3 118 121
75 unless defined $exception;
76  
77 121 my $rollback_exception;
78 # do minimal connectivity check due to weird shit like
79 # https://rt.cpan.org/Public/Bug/Display.html?id=62370
80 121 ▶ try { $self->{storage}->_seems_connected && $self->{storage}->txn_rollback }
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$self->{"storage"}->_seems_connected and $self->{"storage"}->txn_rollback 121 0 121
81 121 ▶ catch { $rollback_exception = shift };
Subroutine __ANON__: called CC=1 SCAR=0.0
82  
83 121 ▶ if ( $rollback_exception and (
Branch: 1 branch
BranchTrueFalseTotal
if ($rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION")) 5 116 121
Condition83%5/6
$rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION")
!ll&&!rl&&r
11515
defined builtin::blessed($rollback_exception) && $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
!ll&&!rl&&r
501
MC/DC66%2/3
$rollback_exception and not defined builtin::blessed($rollback_exception) && $rollback_exception->isa("DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION")
$rollback_exception defined builtin::blessed($rollback_exception) $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
Truth table75%3/4
A and not(B && C)
A= $rollback_exceptionB= defined builtin::blessed($rollback_exception)C= $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
ABCresult
0XX0
1110
10X1
1101
84 ! defined blessed $rollback_exception
85 or
86 ! $rollback_exception->isa('DBIx::Class::Storage::NESTED_ROLLBACK_EXCEPTION')
87 ) ) {
88 # append our text - THIS IS A TEMPORARY FIXUP!
89 # a real stackable exception object is in the works
90 5 ▶ if (ref $exception eq 'DBIx::Class::Exception') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $exception eq 'DBIx::Class::Exception') { } 0 5 5
elsif ($exception) { } 3 2 5
91 0 $exception->{msg} = "Transaction aborted: $exception->{msg} "
92 ."Rollback failed: ${rollback_exception}";
93 }
94 elsif ($exception) {
95 3 $exception = "Transaction aborted: ${exception} "
96 ."Rollback failed: ${rollback_exception}";
97 }
98 else {
99 2 carp (join ' ',
100 "********************* ROLLBACK FAILED!!! ********************",
101 "\nA rollback operation failed after the guard went out of scope.",
102 'This is potentially a disastrous situation, check your data for',
103 "consistency: $rollback_exception"
104 );
105 }
106 }
107 }
108  
109 121 $@ = $exception;
110 }
111  
112 251 1;
113  
114 __END__
« DBIx/Class/CDBICompat/AttributeAPI.pm ↑ Summary DBIx/Class/CDBICompat/GetSet.pm »