blib/lib/DBIx/QuickORM/Txn.pm

filter: Statementstmt 80.4% 33 / 41 Branchbran 56.2% 9 / 16 Conditioncond 29.6% 8 / 27 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 77.7% 7 / 9 Podpod 100% 5 / 5 totaltotal 56.0% 65 / 116 CC 23 SCAR 18.9
CC
23
Cov
60%
commit
9.7
commit
9.7
rollback
7.2
CRAP 6.6
« blib/lib/DBIx/QuickORM/WhereBuilder/Simple.pm ↑ Summary blib/lib/DBIx/QuickORM/DSL.pm »
1 package DBIx::QuickORM::Txn;
2 304 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 304 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 304 our $VERSION = '0.000032';
6  
7 304 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
8 <connection
9 <name
10 <depth
11 <caller
12 <failed
13 304 };
14  
15 =pod
16  
17 =encoding UTF-8
18  
19 =head1 NAME
20  
21 DBIx::QuickORM::Txn - A transaction or savepoint.
22  
23 =head1 DESCRIPTION
24  
25 Returned by C<< $orm->txn >>, and given to the block of
26 C<< $orm->txn(sub { ... }) >>. Finish it with C<commit> or C<rollback>. The
27 outermost transaction is a database transaction. One opened while another is
28 open is a savepoint inside it. See L<DBIx::QuickORM/Transactions> for how
29 transactions affect the rows the ORM holds.
30  
31 The outermost transaction opens at an explicit isolation level, C<'read
32 committed'> unless the ORM or the C<txn> call names another (SQLite and
33 DuckDB have none to set). A savepoint runs at its transaction's level. See
34 L<DBIx::QuickORM::Manual::Transactions/ISOLATION>.
35  
36 Only the innermost open transaction may be committed or rolled back. One is
37 finished once the database has confirmed its commit or rollback.
38  
39 A statement that fails inside a transaction marks the innermost one failed,
40 even when the error is caught. Then every statement on the connection
41 croaks, except the rollback, and C<commit> rolls back instead and croaks.
42 Rolling back a savepoint clears its mark, so a nested transaction around work
43 that may fail is the way to recover and go on. See
44 L<DBIx::QuickORM::Manual::Transactions/"A FAILED STATEMENT">.
45  
46 An unfinished transaction that goes out of scope is rolled back, together
47 with any transaction still open inside it, with a warning naming where it was
48 opened. An async query still pending is cancelled first. If the rollback
49 fails, the warning says so and the transaction is dropped from the stack
50 anyway.
51  
52 Whenever a transaction's outcome is unknown like this, no row cached from inside
53 the root transaction stays canonical. Rows cached from before it that the
54 transaction did not touch do. Refresh a row before writing it again.
55  
56 Row state (C<is_in_db>, C<was_deleted>) then follows the rollback rules, but
57 that is a guess: the database may have committed. Re-read the rows the
58 transaction touched.
59  
60 One still open at program exit is left to the database, which discards it
61 when the connection closes. One inherited by a forked child does nothing
62 there, issues no SQL, and croaks if committed or rolled back.
63  
64 =head1 SYNOPSIS
65  
66 =head2 Block form
67  
68 # Commits when the block returns. An exception rolls back and is rethrown.
69 $orm->txn(sub {
70 my $bob = $orm->insert(users => {name => 'Bob'});
71 $orm->insert(
72 posts => {
73 user_id => $bob->field('id'),
74 title => 'Hello',
75 },
76 );
77 });
78  
79 # Commit or roll back early through the transaction the block is given.
80 $orm->txn(sub {
81 my $txn = shift;
82 $orm->insert(users => {name => 'Temp'});
83 $txn->rollback;
84 });
85  
86 =head2 Object form
87  
88 my $txn = $orm->txn;
89 $orm->insert(users => {name => 'Carol'});
90 $txn->commit;
91  
92 my $undo = $orm->txn;
93 $orm->insert(users => {name => 'Nobody'});
94 $undo->rollback;
95  
96 =head1 ATTRIBUTES
97  
98 =over 4
99  
100 =item $conn = $txn->connection
101  
102 The L<DBIx::QuickORM::Connection> that opened it.
103  
104 =item $name = $txn->name
105  
106 Unique within its Connection, and used as a savepoint's name in SQL.
107  
108 =item $depth = $txn->depth
109  
110 1 for the outermost transaction, 2 for a savepoint inside it, and so on.
111  
112 =item $frame = $txn->caller
113  
114 Where it was opened, as C<caller> returns it: C<[$package, $file, $line]>.
115  
116 =item $error = $txn->failed
117  
118 The error of the statement that marked it failed, or undef.
119  
120 =back
121  
122 =head1 PUBLIC METHODS
123  
124 =over 4
125  
126 =item $txn->commit
127  
128 Commits the transaction, or releases the savepoint. Returns 1.
129  
130 On a transaction marked failed it rolls back instead, then croaks.
131  
132 When the database refuses, the transaction is rolled back and the database's
133 error rethrown. When the rollback fails too, the transaction is dropped from
134 the stack, no row cached from inside the root transaction stays canonical, and
135 the croak says its outcome is unknown.
136  
137 =item $txn->rollback
138  
139 Rolls back the transaction or savepoint. Returns 1.
140  
141 When the database refuses, the transaction stays open and the error is thrown.
142  
143 =back
144  
145 Both croak when the transaction is finished, when one opened inside it is
146 still open, while an async query is pending on the connection, or when called
147 in a process other than the one that opened it.
148  
149 =cut
150  
151 # Each sets the lookup table's caller slot, which records who cleared the row cache of a transaction whose outcome
152 # is unknown (see DBIx::QuickORM::LookupTable, "Who made a hash stop being canon").
153 22 ▶sub commit {
Subroutine commit: called CC=4 SCAR=22.7
Pod documented
154 0 ▶ my $self = shift;
Subroutine commit: not called CC=4 SCAR=22.7
Pod documented
155 0 ▶ my $lookup = $self->{+CONNECTION}->debug && $self->{+CONNECTION}->lookup;
Condition16%1/6
$self->{'connection'}->debug && $self->{'connection'}->lookup
!ll&&!rl&&r
2200
$self->{'connection'}->debug && $self->{'connection'}->lookup
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'connection'}->debug && $self->{'connection'}->lookup
$self->{'connection'}->debug $self->{'connection'}->lookup
MC/DC0%0/2
$self->{'connection'}->debug && $self->{'connection'}->lookup
$self->{'connection'}->debug $self->{'connection'}->lookup
Truth table33%1/3
A && B
A= $self->{'connection'}->debugB= $self->{'connection'}->lookup
ABresult
0X0
100
111
Truth table0%0/3
A && B
A= $self->{'connection'}->debugB= $self->{'connection'}->lookup
ABresult
0X0
100
111
156 0 ▶ local $lookup->{caller} = [(CORE::caller(0))[0 .. 3]] if $lookup && !$lookup->{caller};
Branch: 2 branches
BranchTrueFalseTotal
if $lookup and not $lookup->{"caller"} 0 22 22
if $lookup and not $lookup->{"caller"} 0 0 0
Condition16%1/6
$lookup and not $lookup->{"caller"}
!ll&&!rl&&r
2200
$lookup and not $lookup->{"caller"}
!ll&&!rl&&r
000
MC/DC0%0/2
$lookup and not $lookup->{"caller"}
$lookup not $lookup->{"caller"}
MC/DC0%0/2
$lookup and not $lookup->{"caller"}
$lookup not $lookup->{"caller"}
Truth table33%1/3
A and not(B)
A= $lookupB= not $lookup->{"caller"}
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $lookupB= not $lookup->{"caller"}
ABresult
0X0
100
111
157 0 return $self->{+CONNECTION}->commit_txn($self);
158 }
159  
160 51 ▶sub rollback {
Subroutine rollback: called CC=4 SCAR=19.7
Pod documented
161 0 ▶ my $self = shift;
Subroutine rollback: not called CC=4 SCAR=19.7
Pod documented
162 0 ▶ my $lookup = $self->{+CONNECTION}->debug && $self->{+CONNECTION}->lookup;
Condition33%2/6
$self->{'connection'}->debug && $self->{'connection'}->lookup
!ll&&!rl&&r
5001
$self->{'connection'}->debug && $self->{'connection'}->lookup
!ll&&!rl&&r
000
MC/DC50%1/2
$self->{'connection'}->debug && $self->{'connection'}->lookup
$self->{'connection'}->debug $self->{'connection'}->lookup
MC/DC0%0/2
$self->{'connection'}->debug && $self->{'connection'}->lookup
$self->{'connection'}->debug $self->{'connection'}->lookup
Truth table66%2/3
A && B
A= $self->{'connection'}->debugB= $self->{'connection'}->lookup
ABresult
0X0
100
111
Truth table0%0/3
A && B
A= $self->{'connection'}->debugB= $self->{'connection'}->lookup
ABresult
0X0
100
111
163 0 ▶ local $lookup->{caller} = [(CORE::caller(0))[0 .. 3]] if $lookup && !$lookup->{caller};
Branch: 2 branches
BranchTrueFalseTotal
if $lookup and not $lookup->{"caller"} 1 50 51
if $lookup and not $lookup->{"caller"} 0 0 0
Condition33%2/6
$lookup and not $lookup->{"caller"}
!ll&&!rl&&r
5001
$lookup and not $lookup->{"caller"}
!ll&&!rl&&r
000
MC/DC50%1/2
$lookup and not $lookup->{"caller"}
$lookup not $lookup->{"caller"}
MC/DC0%0/2
$lookup and not $lookup->{"caller"}
$lookup not $lookup->{"caller"}
Truth table66%2/3
A and not(B)
A= $lookupB= not $lookup->{"caller"}
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $lookupB= not $lookup->{"caller"}
ABresult
0X0
100
111
164 0 return $self->{+CONNECTION}->rollback_txn($self);
165 }
166  
167 =pod
168  
169 =over 4
170  
171 =item $where = $txn->opened_at
172  
173 Where it was opened, as C<"FILE line N">.
174  
175 =back
176  
177 =cut
178  
179 141 ▶sub opened_at { "$_[0]->{+CALLER}->[1] line $_[0]->{+CALLER}->[2]" }
Subroutine opened_at: called CC=1 SCAR=0.0
Pod documented
180  
181 147 ▶sub DESTROY {
Subroutine DESTROY: called CC=6 SCAR=18.0
182 147 my $self = shift;
183  
184 # At program exit references to other objects may already be cleared. The database discards the transaction.
185 147 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 1 branch
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 147 147
186  
187 147 ▶ my $conn = $self->{+CONNECTION} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $conn = $self->{"connection"} or return 147 0 147
188 147 ▶ return unless $conn->pid == $$ && $conn->_is_open($self);
Branch: 1 branch
BranchTrueFalseTotal
unless $conn->pid == $$ and $conn->_is_open($self) 142 5 147
Condition66%2/3
$conn->pid == $$ and $conn->_is_open($self)
!ll&&!rl&&r
01425
MC/DC50%1/2
$conn->pid == $$ and $conn->_is_open($self)
$conn->pid == $$ $conn->_is_open($self)
Truth table66%2/3
A and B
A= $conn->pid == $$B= $conn->_is_open($self)
ABresult
0X0
100
111
189  
190 5 my $where = $self->opened_at;
191 5 local $@;
192 5 my $ok = eval { $conn->_unwind($self); 1 };
193 5 ▶ return warn "The transaction opened at $where went out of scope without commit() or rollback(); it was rolled back\n" if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 3 2 5
194  
195 # Nothing can close it once this object is gone, so it leaves the stack either way.
196 2 my $err = $@;
197 2 $conn->_abandon($self);
198 2 warn "The transaction opened at $where went out of scope without commit() or rollback(), and rolling it back failed: $err";
199 }
200  
201 304 1;
202  
203 __END__
« blib/lib/DBIx/QuickORM/WhereBuilder/Simple.pm ↑ Summary blib/lib/DBIx/QuickORM/DSL.pm »