SQLite/Transaction.pm

filter: Statementstmt 100% 24 / 24 Branchbran 75.0% 6 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 100% 5 / 5 Podpod 100% 2 / 2 totaltotal 90.9% 40 / 44 CC 6 SCAR 8.8
CC
6
Cov
91%
DESTROY
3.0
new
3.0
commit
2.1
CRAP 2.4
« SQLite/Results.pm ↑ Summary SQLite/PubSub.pm »
1 package Mojo::SQLite::Transaction;
2 6 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 6 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 our $VERSION = 'v4.0.0';
7  
8 6 has db => undef, weak => 1;
9  
10 6 my %behaviors = map { ($_ => 1) } qw(deferred immediate exclusive);
11  
12 27 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
13 27 my $self = shift->SUPER::new(@_);
14 27 my $dbh = $self->{dbh} = $self->db->dbh;
15 27 ▶ if (my $behavior = $self->{behavior}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $behavior = $self->{'behavior'}) { } 24 3 27
16 24 ▶ croak qq{Invalid transaction behavior $behavior} unless exists $behaviors{lc $behavior};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $behaviors{lc $behavior} 0 24 24
17 24 $dbh->do("begin $behavior transaction");
18 } else {
19 3 $dbh->begin_work;
20 }
21 26 $self->{rollback} = 1;
22 26 return $self;
23 }
24  
25 27 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
26 27 my $self = shift;
27 27 ▶ if ($self->{rollback} && (my $dbh = $self->{dbh})) { $dbh->rollback }
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"rollback"} and my $dbh = $self->{"dbh"}) 6 21 27
Condition66%2/3
$self->{"rollback"} and my $dbh = $self->{"dbh"}
!ll&&!rl&&r
2106
MC/DC50%1/2
$self->{"rollback"} and my $dbh = $self->{"dbh"}
$self->{"rollback"} my $dbh = $self->{"dbh"}
Truth table66%2/3
A and B
A= $self->{"rollback"}B= my $dbh = $self->{"dbh"}
ABresult
0X0
100
111
28 }
29  
30 20 ▶sub commit {
Subroutine commit: called CC=2 SCAR=7.2
Pod documented
31 20 my $self = shift;
32 20 ▶ $self->{dbh}->commit if delete $self->{rollback};
Branch: 1 branch
BranchTrueFalseTotal
if delete $self->{"rollback"} 20 0 20
33 }
34  
35 6 1;
36  
37 =head1 NAME
38  
39 Mojo::SQLite::Transaction - Transaction
40  
41 =head1 SYNOPSIS
42  
43 use Mojo::SQLite::Transaction;
44  
45 my $tx = Mojo::SQLite::Transaction->new(db => $db);
46 $tx->commit;
47  
48 =head1 DESCRIPTION
49  
50 L<Mojo::SQLite::Transaction> is a scope guard for L<DBD::SQLite> transactions
51 used by L<Mojo::SQLite::Database>.
52  
53 =head1 ATTRIBUTES
54  
55 L<Mojo::SQLite::Transaction> implements the following attributes.
56  
57 =head2 db
58  
59 my $db = $tx->db;
60 $tx = $tx->db(Mojo::SQLite::Database->new);
61  
62 L<Mojo::SQLite::Database> object this transaction belongs to. Note that this
63 attribute is weakened.
64  
65 =head1 METHODS
66  
67 L<Mojo::SQLite::Transaction> inherits all methods from L<Mojo::Base> and
68 implements the following new ones.
69  
70 =head2 new
71  
72 my $tx = Mojo::SQLite::Transaction->new;
73 my $tx = Mojo::SQLite::Transaction->new(db => Mojo::SQLite::Database->new);
74 my $tx = Mojo::SQLite::Transaction->new({db => Mojo::SQLite::Database->new});
75  
76 Construct a new L<Mojo::SQLite::Transaction> object.
77  
78 =head2 commit
79  
80 $tx->commit;
81  
82 Commit transaction.
83  
84 =head1 BUGS
85  
86 Report any issues on the public bugtracker.
87  
88 =head1 AUTHOR
89  
90 Dan Book, C<dbook@cpan.org>
91  
92 =head1 COPYRIGHT AND LICENSE
93  
94 Copyright 2015, Dan Book.
95  
96 This library is free software; you may redistribute it and/or modify it under
97 the terms of the Artistic License version 2.0.
98  
99 =head1 SEE ALSO
100  
101 L<Mojo::SQLite>
« SQLite/Results.pm ↑ Summary SQLite/PubSub.pm »