| 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
| |||||||||||||||||||||||||||||
| 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]
MC/DC0%0/1
$args{'caller'} || [caller 0]
$args{'caller'}
Truth table0%0/2
A
A= $args{'caller'}
| |||||||||||||||||||||||||||||
| 29 | 0 | my $txns = $self->{active_transactions}; | |||||||||||||||||||||||||||
| 30 | 0 | my $rc = 1; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | if (@$txns == 0 && $self->{dbh}->FETCH('AutoCommit')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@$txns == 0 and $self->{"dbh"}->FETCH("AutoCommit")
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")
| |||||||||||||||||||||||||||||
| 32 | 0 | $rc = $self->{dbh}->begin_work; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 0 | ▶ | if ($rc) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | my $current = pop @$txns; | |||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ( @$txns > 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | if ( $self->{rollbacked_in_nested_transaction} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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]
MC/DC0%0/1
$args{'caller'} || [caller 1]
$args{'caller'}
Truth table0%0/2
A
A= $args{'caller'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if ( $$ != $pid ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||