blib/lib/DBIx/TransactionManager.pm

filter: Statementstmt 20.0% 14 / 70 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 22.2% 4 / 18 Podpod 28.5% 2 / 7 totaltotal 15.1% 20 / 132 CC 17 SCAR 24.0
CC
17
Cov
14%
txn_begin
30.0
txn_commit
20.0
txn_rollback
12.0
CRAP 11.1
↑ Summary
1 package DBIx::TransactionManager;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '1.13';
6  
7 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
8 0 my ($class, $dbh) = @_;
9  
10 0 ▶ unless ($dbh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbh) 0 0 0
11 0 Carp::croak('missing mandatory parameter dbh');
12 }
13  
14 bless {
15 0 dbh => $dbh,
16 active_transactions => [],
17 rollbacked_in_nested_transaction => 0,
18 }, $class;
19 }
20  
21 0 ▶sub txn_scope {
Subroutine txn_scope: not called CC=1 SCAR=6.9
Pod documented
22 0 DBIx::TransactionManager::ScopeGuard->new( @_ );
23 }
24  
25 0 ▶sub txn_begin {
Subroutine txn_begin: not called CC=5 SCAR=34.0
Pod undocumented
26 0 my ($self, %args) = @_;
27  
28 0 ▶ my $caller = $args{caller} || [ caller(0) ];
Condition0%0/2
$args{'caller'} || [caller 0]
l!l
00
MC/DC0%0/1
$args{'caller'} || [caller 0]
$args{'caller'}
Truth table0%0/2
A
A= $args{'caller'}
Aresult
11
00
29 0 my $txns = $self->{active_transactions};
30 0 my $rc = 1;
31 0 ▶ if (@$txns == 0 && $self->{dbh}->FETCH('AutoCommit')) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$txns == 0 and $self->{"dbh"}->FETCH("AutoCommit")) 0 0 0
Condition0%0/3
@$txns == 0 and $self->{"dbh"}->FETCH("AutoCommit")
!ll&&!rl&&r
000
MC/DC0%0/2
@$txns == 0 and $self->{"dbh"}->FETCH("AutoCommit")
@$txns == 0 $self->{"dbh"}->FETCH("AutoCommit")
Truth table0%0/3
A and B
A= @$txns == 0B= $self->{"dbh"}->FETCH("AutoCommit")
ABresult
0X0
100
111
32 0 $rc = $self->{dbh}->begin_work;
33 }
34 0 ▶ if ($rc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rc) 0 0 0
35 0 push @$txns, { caller => $caller, pid => $$ };
36 }
37 }
38  
39 0 ▶sub txn_rollback {
Subroutine txn_rollback: not called CC=3 SCAR=24.8
Pod undocumented
40 0 my $self = shift;
41 0 my $txns = $self->{active_transactions};
42 0 ▶ return unless @$txns;
Branch: 1 branch
BranchTrueFalseTotal
unless @$txns 0 0 0
43  
44 0 my $current = pop @$txns;
45 0 ▶ if ( @$txns > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$txns > 0) { } 0 0 0
46 # already popped, so if there's anything, we're in a nested
47 # transaction, mate.
48 0 $self->{rollbacked_in_nested_transaction}++;
49 } else {
50 0 $self->{dbh}->rollback;
51 0 $self->_txn_end;
52 }
53 }
54  
55 0 ▶sub txn_commit {
Subroutine txn_commit: not called CC=4 SCAR=30.0
Pod undocumented
56 0 my $self = shift;
57 0 my $txns = $self->{active_transactions};
58 0 ▶ return unless @$txns;
Branch: 1 branch
BranchTrueFalseTotal
unless @$txns 0 0 0
59  
60 0 ▶ if ( $self->{rollbacked_in_nested_transaction} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rollbacked_in_nested_transaction"}) 0 0 0
61 0 Carp::croak "tried to commit but already rollbacked in nested transaction.";
62 }
63  
64 0 my $current = pop @$txns;
65 0 ▶ if (@$txns == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$txns == 0) 0 0 0
66 0 $self->{dbh}->commit;
67 0 $self->_txn_end;
68 }
69 }
70  
71 0 ▶sub _txn_end {
Subroutine _txn_end: not called CC=1 SCAR=6.9
72 0 @{$_[0]->{active_transactions}} = ();
73 0 $_[0]->{rollbacked_in_nested_transaction} = 0;
74 }
75  
76 0 ▶sub active_transactions { $_[0]->{active_transactions} }
Subroutine active_transactions: not called CC=1 SCAR=6.9
Pod undocumented
77  
78 0 ▶sub in_transaction {
Subroutine in_transaction: not called CC=2 SCAR=17.9
Pod undocumented
79 0 my $self = shift;
80 0 my $txns = $self->{active_transactions};
81 0 ▶ return @$txns ? $txns->[0] : ();
Branch: 1 branch
BranchTrueFalseTotal
@$txns ? : 0 0 0
82 }
83  
84 package DBIx::TransactionManager::ScopeGuard;
85 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
88 0 my($class, $obj, %args) = @_;
89  
90 0 ▶ my $caller = $args{caller} || [ caller(1) ];
Condition0%0/2
$args{'caller'} || [caller 1]
l!l
00
MC/DC0%0/1
$args{'caller'} || [caller 1]
$args{'caller'}
Truth table0%0/2
A
A= $args{'caller'}
Aresult
11
00
91 0 $obj->txn_begin( caller => $caller );
92 0 bless [ 0, $obj, $caller, $$ ], $class;
93 }
94  
95 0 ▶sub rollback {
Subroutine rollback: not called CC=2 SCAR=17.9
96 0 ▶ return if $_[0]->[0]; # do not run twice.
Branch: 1 branch
BranchTrueFalseTotal
if $_[0][0] 0 0 0
97 0 $_[0]->[1]->txn_rollback;
98 0 $_[0]->[0] = 1;
99 }
100  
101 0 ▶sub commit {
Subroutine commit: not called CC=2 SCAR=17.9
102 0 ▶ return if $_[0]->[0]; # do not run twice.
Branch: 1 branch
BranchTrueFalseTotal
if $_[0][0] 0 0 0
103 0 $_[0]->[1]->txn_commit;
104 0 $_[0]->[0] = 1;
105 }
106  
107 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
108 0 my($dismiss, $obj, $caller, $pid) = @{ $_[0] };
109 0 ▶ return if $dismiss;
Branch: 1 branch
BranchTrueFalseTotal
if $dismiss 0 0 0
110  
111 0 ▶ if ( $$ != $pid ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ != $pid) 0 0 0
112 0 return;
113 }
114  
115 0 warn( "Transaction was aborted without calling an explicit commit or rollback. (Guard created at $caller->[1] line $caller->[2])" );
116  
117 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
118 0 $obj->txn_rollback;
119 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 die "Rollback failed: $_";
121 0 };
122 }
123  
124 1 1;
125 __END__
↑ Summary