blib/lib/DBIx/QuickORM/Txn.pm

filter: Statementstmt 67.5% 27 / 40 Branchbran 37.5% 6 / 16 Conditioncond 33.3% 2 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 87.5% 7 / 8 Podpod 100% 3 / 3 totaltotal 59.7% 46 / 77 CC 11 SCAR 21.5
CC
11
Cov
56%
DESTROY
12.4
DESTROY
12.4
BEGIN
1.0
CRAP 8.6
« blib/lib/DBIx/QuickORM/ORM.pm ↑ Summary blib/lib/DBIx/QuickORM/Handle.pm »
1 package DBIx::QuickORM::Txn;
2 258 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 258 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 258 our $VERSION = '0.000031';
6  
7 258 ▶use Object::HashBase qw{
Subroutine BEGIN: called CC=1 SCAR=0.0
8 <connection
9 <name
10 <depth
11 <caller
12 <failed
13 258 };
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 22 ▶sub commit { $_[0]->{+CONNECTION}->commit_txn($_[0]) }
Subroutine commit: called CC=1 SCAR=0.0
Pod documented
152 48 ▶sub rollback { $_[0]->{+CONNECTION}->rollback_txn($_[0]) }
Subroutine rollback: called CC=1 SCAR=0.0
Pod documented
153  
154 =pod
155  
156 =over 4
157  
158 =item $where = $txn->opened_at
159  
160 Where it was opened, as C<"FILE line N">.
161  
162 =back
163  
164 =cut
165  
166 133 ▶sub opened_at { "$_[0]->{+CALLER}->[1] line $_[0]->{+CALLER}->[2]" }
Subroutine opened_at: called CC=1 SCAR=0.0
Pod documented
167  
168 137 ▶sub DESTROY {
Subroutine DESTROY: called CC=6 SCAR=25.2
169 0 ▶ my $self = shift;
Subroutine DESTROY: not called CC=6 SCAR=25.2
170  
171 # At program exit references to other objects may already be cleared. The database discards the transaction.
172 0 ▶ return if ${^GLOBAL_PHASE} eq 'DESTRUCT';
Branch: 2 branches
BranchTrueFalseTotal
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 137 137
if ${^GLOBAL_PHASE} eq "DESTRUCT" 0 0 0
173  
174 0 ▶ my $conn = $self->{+CONNECTION} or return;
Branch: 2 branches
BranchTrueFalseTotal
my $conn = $self->{"connection"} or return 137 0 137
my $conn = $self->{"connection"} or return 0 0 0
175 0 ▶ return unless $conn->pid == $$ && $conn->_is_open($self);
Branch: 2 branches
BranchTrueFalseTotal
unless $conn->pid == $$ and $conn->_is_open($self) 132 5 137
unless $conn->pid == $$ and $conn->_is_open($self) 0 0 0
Condition33%2/6
$conn->pid == $$ and $conn->_is_open($self)
!ll&&!rl&&r
01325
$conn->pid == $$ and $conn->_is_open($self)
!ll&&!rl&&r
000
MC/DC50%1/2
$conn->pid == $$ and $conn->_is_open($self)
$conn->pid == $$ $conn->_is_open($self)
MC/DC0%0/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
Truth table0%0/3
A and B
A= $conn->pid == $$B= $conn->_is_open($self)
ABresult
0X0
100
111
176  
177 0 my $where = $self->opened_at;
178 0 local $@;
179 0 my $ok = eval { $conn->_unwind($self); 1 };
180 0 ▶ return warn "The transaction opened at $where went out of scope without commit() or rollback(); it was rolled back\n" if $ok;
Branch: 2 branches
BranchTrueFalseTotal
if $ok 3 2 5
if $ok 0 0 0
181  
182 # Nothing can close it once this object is gone, so it leaves the stack either way.
183 0 my $err = $@;
184 0 $conn->_abandon($self);
185 0 warn "The transaction opened at $where went out of scope without commit() or rollback(), and rolling it back failed: $err";
186 }
187  
188 258 1;
189  
190 __END__
« blib/lib/DBIx/QuickORM/ORM.pm ↑ Summary blib/lib/DBIx/QuickORM/Handle.pm »