KiokuDB/Backend/Role/TXN.pm

filter: Statementstmt 94.2% 33 / 35 Branchbran 50.0% 7 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 84.7% 50 / 59 CC 8 SCAR 11.6
CC
8
Cov
82%
__ANON__
6.3
txn_do
3.2
__ANON__
2.1
CRAP 3.2
« KiokuDB/Meta/Instance.pm ↑ Summary KiokuDB/GC/Naive/Mark.pm »
1 #!/usr/bin/perl
2  
3 package KiokuDB::Backend::Role::TXN;
4 21 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 21 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 21 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 21 requires qw(txn_begin txn_commit txn_rollback);
12  
13 sub txn_do {
14 2090 ▶ my ( $self, $coderef, %args ) = @_;
Subroutine txn_do: called CC=3 SCAR=11.5
Pod documented
15  
16 2090 ▶ my @args = @{ $args{args} || [] };
Branch: 1 branch
BranchTrueFalseTotal
$args{"args"} or [] 2090 0 2090
17  
18 2090 my ( $commit, $rollback ) = @args{qw(commit rollback)};
19  
20 2090 ▶ ref $coderef eq 'CODE' or croak '$coderef must be a CODE reference';
Branch: 1 branch
BranchTrueFalseTotal
ref $coderef eq "CODE" or croak("\$coderef must be a CODE reference") 0 2090 2090
21  
22 2090 my @txn_args = $self->txn_begin;
23  
24 try {
25 2090 ▶ my @ret;
Subroutine __ANON__: called CC=5 SCAR=18.4
26  
27 2090 ▶ if ( wantarray ) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 2090 2090
elsif (defined wantarray) { } 0 2090 2090
28 0 @ret = $coderef->(@args);
29 } elsif ( defined wantarray ) {
30 0 $ret[0] = $coderef->(@args);
31 } else {
32 2090 $coderef->(@args);
33 }
34  
35 1646 ▶ $commit->() if $commit;
Branch: 1 branch
BranchTrueFalseTotal
if $commit 0 1646 1646
36 1646 $self->txn_commit(@txn_args);
37  
38 1621 ▶ return wantarray ? @ret : $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1621 1621
39 } catch {
40 469 ▶ my $err = $_;
Subroutine __ANON__: called CC=1 SCAR=0.0
41  
42 try {
43 469 ▶ $self->txn_rollback(@txn_args);
Subroutine __ANON__: called CC=2 SCAR=7.2
44 444 ▶ $rollback->() if $rollback;
Branch: 1 branch
BranchTrueFalseTotal
if $rollback 444 0 444
45 } catch {
46 25 ▶ croak "Transaction aborted: $err, rollback failed: $_";
Subroutine __ANON__: called CC=1 SCAR=0.0
47 469 };
48  
49 444 die $err;
50 }
51 2090 }
52  
53 21 __PACKAGE__
54  
55 __END__
56  
57 =pod
58  
59 =head1 NAME
60  
61 KiokuDB::Backend::Role::TXN - Backend level transaction support.
62  
63 =head1 SYNOPSIS
64  
65 package MyBackend;
66 use Moose;
67  
68 with qw(
69 KiokuDB::Backend
70 KiokuDB::Backend::Role::TXN
71 );
72  
73 sub txn_begin { ... }
74 sub txn_commit { ... }
75 sub txn_rollback { ... }
76  
77 =head1 DESCRIPTION
78  
79 This API is inspired by standard database transactions much like you get with
80 L<DBI>.
81  
82 This is the low level interface required by L<KiokuDB/txn_do>.
83  
84  
85 =head1 OPTIONAL METHODS
86  
87 =over 4
88  
89 =item txn_do $code, %callbacks
90  
91 This method should evaluate the code reference in the context of a transaction,
92 inside an C<eval>. If any errors are caught the transaction should be aborted,
93 otherwise it should be committed. This is much like
94 L<DBIx::Class::Schema/txn_do>.
95  
96 The C<rollback> callback should be fired when the transaction will be aborted.
97  
98 =back
99  
100 =head1 REQUIRED METHODS
101  
102 =over 4
103  
104 =item txn_begin [ $parent_txn ]
105  
106 Begin a new transaction.
107  
108 This method can return a transaction handle that will later be passed to
109 C<txn_commit> or C<txn_rollback> as necessary.
110  
111 The current handle will be passed to nested calls to C<txn_begin>.
112  
113  
114 =item txn_commit $txn
115  
116 Commit the transaction.
117  
118 =item txn_rollback $txn
119  
120 Rollback the transaction.
121  
122 =back
123  
124 =cut
« KiokuDB/Meta/Instance.pm ↑ Summary KiokuDB/GC/Naive/Mark.pm »