blib/lib/DBIx/Transaction/db.pm

filter: Statementstmt 98.5% 137 / 139 Branchbran 87.5% 35 / 40 Conditioncond 85.7% 18 / 21 MC/DCmcdc 63.6% 7 / 11 Subroutinesub 100% 22 / 22 Podpod 86.6% 13 / 15 totaltotal 93.5% 232 / 248 CC 30 SCAR 14.3
CC
30
Cov
95%
transaction
10.0
commit
6.1
_when
3.0
CRAP 4.2
↑ Summary blib/lib/DBIx/Transaction/st.pm »
1 #!perl;
2  
3 package DBIx::Transaction::db;
4  
5 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use base q(DBI::db);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings (FATAL => 'all');
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Carp qw(confess croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 return 1;
12  
13 4 ▶sub connected {
Subroutine connected: called CC=3 SCAR=11.0
Pod undocumented
14 4 my ( $self, $dsn, $user, $credential, $attrs ) = @_;
15  
16 4 ▶ if ( $self->{AutoCommit} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'AutoCommit'}) { } 2 2 4
17 2 $self->{private_DBIx_Transaction_AutoCommit} = 1;
18 }
19 else {
20 2 $self->{private_DBIx_Transaction_AutoCommit} = 0;
21 }
22  
23 4 $self->{private_DBIx_Transaction_Level} = 0;
24 4 $self->{private_DBIx_Transaction_Error} = 0;
25  
26 4 ▶ my $method = $attrs->{dbi_connect_method} || $DBI::connect_via;
Condition66%2/3
$attrs->{'dbi_connect_method'} || $DBI::connect_via
l!l&&r!l&&!r
220
MC/DC0%0/2
$attrs->{'dbi_connect_method'} || $DBI::connect_via
$attrs->{'dbi_connect_method'} $DBI::connect_via
Truth table66%2/3
A || B
A= $attrs->{'dbi_connect_method'}B= $DBI::connect_via
ABresult
1X1
011
000
27  
28 4 $self->transaction_trace($method);
29  
30 4 return $self;
31 }
32  
33 139 ▶sub transaction_trace {
Subroutine transaction_trace: called CC=1 SCAR=0.0
Pod documented
34 139 my($self, $method) = @_;
35 139 my @vals = map { "$_=$self->{$_}" } map { "private_DBIx_Transaction_$_" }
36 qw(AutoCommit Level Error);
37  
38 139 $self->trace_msg("DBIx::Transaction: $method: " . join(" ", @vals), 3);
39 }
40  
41 114 ▶sub transaction_level {
Subroutine transaction_level: called CC=1 SCAR=0.0
Pod documented
42 114 my $self = shift;
43 114 return $self->{private_DBIx_Transaction_Level};
44 }
45  
46 82 ▶sub inc_transaction_level {
Subroutine inc_transaction_level: called CC=1 SCAR=0.0
Pod documented
47 82 my $self = shift;
48 82 $self->{private_DBIx_Transaction_Level}++;
49 82 return $self->{private_DBIx_Transaction_Level};
50 }
51  
52 86 ▶sub dec_transaction_level {
Subroutine dec_transaction_level: called CC=2 SCAR=6.9
Pod documented
53 86 my $self = shift;
54 confess "Asked to decrement transaction level below zero!"
55 86 ▶ unless($self->{private_DBIx_Transaction_Level});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"private_DBIx_Transaction_Level"} 4 82 86
56 82 $self->{private_DBIx_Transaction_Level}--;
57 82 return $self->{private_DBIx_Transaction_Level};
58 }
59  
60 58 ▶sub clear_transaction_error {
Subroutine clear_transaction_error: called CC=1 SCAR=0.0
Pod documented
61 58 my $self = shift;
62 58 $self->{private_DBIx_Transaction_Error} = 0;
63 58 $self->{private_DBIx_Transaction_Error_Caller} = undef;
64 58 return;
65 }
66  
67 16 ▶sub inc_transaction_error {
Subroutine inc_transaction_error: called CC=3 SCAR=11.0
Pod documented
68 16 my($self, @caller) = @_;
69 16 $self->{private_DBIx_Transaction_Error}++;
70 16 ▶ if(@caller) {
Branch: 1 branch
BranchTrueFalseTotal
if (@caller) 16 0 16
71 16 ▶ $self->{private_DBIx_Transaction_Error_Caller} ||= [];
Condition100%2/2
$self->{"private_DBIx_Transaction_Error_Caller"} ||= []
l!l
412
MC/DC100%1/1
$self->{"private_DBIx_Transaction_Error_Caller"} ||= []
$self->{"private_DBIx_Transaction_Error_Caller"}
Truth table100%2/2
A
A= $self->{"private_DBIx_Transaction_Error_Caller"}
Aresult
11
00
72 16 push(@{$self->{private_DBIx_Transaction_Error_Caller}}, \@caller);
73 }
74 16 return;
75 }
76  
77 80 ▶sub transaction_error {
Subroutine transaction_error: called CC=1 SCAR=0.0
Pod documented
78 80 my $self = shift;
79 80 return $self->{private_DBIx_Transaction_Error};
80 }
81  
82 4 ▶sub transaction_error_callers {
Subroutine transaction_error_callers: called CC=2 SCAR=7.4
Pod undocumented
83 4 my $self = shift;
84 4 ▶ if($self->{private_DBIx_Transaction_Error_Caller}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'private_DBIx_Transaction_Error_Caller'}) { } 4 0 4
85 4 return @{$self->{private_DBIx_Transaction_Error_Caller}};
86 } else {
87 0 return;
88 }
89 }
90  
91 58 ▶sub close_transaction {
Subroutine close_transaction: called CC=1 SCAR=0.0
Pod documented
92 58 my $self = shift;
93 58 my $method = shift;
94 58 my $code = DBI::db->can($method);
95  
96 58 $self->{private_DBIx_Transaction_Level} = 0;
97 58 $self->clear_transaction_error;
98 58 $self->transaction_trace($method);
99 58 my $rv = $code->($self, @_);
100 58 return $rv;
101 }
102  
103 82 ▶sub begin_work {
Subroutine begin_work: called CC=3 SCAR=11.0
Pod documented
104 82 my $self = shift;
105 82 ▶ if(!$self->transaction_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->transaction_level) { } 58 24 82
106 58 $self->inc_transaction_level;
107 58 ▶ if($self->{private_DBIx_Transaction_AutoCommit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'private_DBIx_Transaction_AutoCommit'}) { } 29 29 58
108 29 $self->transaction_trace('begin_work');
109 29 return DBI::db::begin_work($self, @_);
110 } else {
111 29 return 1;
112 }
113 } else {
114 24 $self->inc_transaction_level;
115 24 $self->transaction_trace('fake_begin_work');
116 24 return 1;
117 }
118 }
119  
120 58 ▶sub commit {
Subroutine commit: called CC=6 SCAR=18.1
Pod documented
121 58 my $self = shift;
122 58 ▶ if(my $error = $self->transaction_error) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $error = $self->transaction_error) 4 54 58
123 4 my $err = "commit() called after a transaction error or rollback!";
124 4 ▶ if(my @callers = $self->transaction_error_callers) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@callers) = $self->transaction_error_callers) 4 0 4
125 4 foreach my $i (@callers) {
126 4 $err .= "\nError or rollback at: $i->[1] line $i->[2]";
127 4 ▶ if($i->[3]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i->[3]) 0 4 4
128 0 $err .= " (Error String: $i->[3])";
129 }
130 }
131 }
132  
133 4 $self->set_err(1, $err);
134 2 return;
135 }
136  
137 54 ▶ if(my $l = $self->dec_transaction_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = $self->dec_transaction_level) 20 30 50
138 20 $self->transaction_trace('fake_commit');
139 20 return 1;
140 }
141 30 return $self->close_transaction('commit', @_);
142 }
143  
144 32 ▶sub rollback {
Subroutine rollback: called CC=2 SCAR=6.9
Pod documented
145 32 my $self = shift;
146 32 ▶ if(my $l = $self->dec_transaction_level) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $l = $self->dec_transaction_level) 4 28 32
147 4 $self->transaction_trace('fake_rollback');
148 4 $self->inc_transaction_error(caller);
149 4 return 1;
150 }
151 28 return $self->close_transaction('rollback', @_);
152 }
153  
154 66 ▶sub do {
Subroutine do: called CC=3 SCAR=11.0
Pod documented
155 66 my $self = shift;
156 66 my $rv = eval { DBI::db::do($self, @_); };
157 66 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 64 66
158 2 $self->inc_transaction_error(caller, $self->errstr);
159 2 croak $@;
160 }
161 64 ▶ if(!$rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 2 62 64
162 2 $self->inc_transaction_error(caller, $self->errstr);
163 }
164 64 return $rv;
165 }
166  
167 50 ▶sub _when {
Subroutine _when: called CC=3 SCAR=11.0
168 50 my($dbh, $return_value, $return_exception, $tries) = @_;
169 50 ▶ my $rv = !!($tries && ($return_exception || !$return_value));
Condition83%5/6
$tries && ($return_exception || !$return_value)
!ll&&!rl&&r
36212
$return_exception or not $return_value
l!l&&r!l&&!r
0122
MC/DC66%2/3
$tries && ($return_exception || !$return_value)
$tries $return_exception not $return_value
Truth table75%3/4
A && B or not(C)
A= $triesB= $return_exceptionC= not $return_value
ABCresult
0XX0
1000
11X1
1011
170 50 return $rv;
171 }
172  
173 40 ▶sub transaction {
Subroutine transaction: called CC=10 SCAR=23.0
Pod documented
174 40 my($self, $run, $tries, $when) = @_;
175 40 my($rv, $re);
176  
177 40 my $tried = 0;
178 40 ▶ $tries ||= 1;
Condition100%2/2
$tries ||= 1
l!l
634
MC/DC100%1/1
$tries ||= 1
$tries
Truth table100%2/2
A
A= $tries
Aresult
11
00
179  
180 40 ▶ if(($tries != 1 || $when) && $self->transaction_level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tries != 1 || $when and $self->transaction_level) 2 38 40
Condition83%5/6
$tries != 1 || $when and $self->transaction_level
!ll&&!rl&&r
3442
$tries != 1 or $when
l!l&&r!l&&!r
6034
MC/DC66%2/3
$tries != 1 || $when and $self->transaction_level
$tries != 1 $when $self->transaction_level
Truth table60%3/5
A or B and C
A= $tries != 1B= $whenC= $self->transaction_level
ABCresult
00X0
1X00
0100
1X11
0111
181 2 croak "Transaction retry flow may only be set on the outermost transaction";
182 }
183  
184  
185 38 ▶ $when ||= \&_when;
Condition100%2/2
$when ||= \&DBIx::Transaction::db::_when
l!l
434
MC/DC100%1/1
$when ||= \&DBIx::Transaction::db::_when
$when
Truth table100%2/2
A
A= $when
Aresult
11
00
186  
187  
188 38 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
&$when($self, $rv, $re, $tries) and do { do { $self->set_err(0, "Retrying transaction ($tries tries left)") if $tried; eval { do { $rv = $self->_transaction($run) } }; $re = $@; --$tries unless $tries <= 0; ++$tried } } 12 38 50
189 50 ▶ $self->set_err(0, "Retrying transaction ($tries tries left)")
Branch: 1 branch
BranchTrueFalseTotal
if $tried 12 38 50
190 if $tried;
191  
192 50 eval { $rv = $self->_transaction($run) };
193 50 $re = $@;
194 50 ▶ $tries-- unless $tries <= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $tries <= 0 50 0 50
195 50 $tried++;
196 } while($when->($self, $rv, $re, $tries));
197  
198 38 ▶ if($re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($re) { } 4 34 38
199 4 die $re;
200 } else {
201 34 return $rv;
202 }
203 }
204  
205 50 ▶sub _transaction {
Subroutine _transaction: called CC=3 SCAR=11.0
206 50 my($self, $run) = @_;
207 50 my $rv;
208  
209 50 $self->begin_work;
210  
211 50 eval { $rv = $run->() };
212  
213 50 ▶ if(my $re = $@) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $re = $@) { } 4 46 50
elsif (not $rv) { } 16 30 46
214 4 $self->rollback;
215 4 croak $re;
216 } elsif(!$rv) {
217 16 my $err = $self->err;
218 16 my $errstr = $self->errstr;
219 16 my $state = $self->state;
220 16 $self->rollback;
221 16 $self->set_err($err, $errstr, $state);
222 } else {
223 30 $self->commit;
224 }
225  
226 46 return $rv;
227 }
228  
229 =pod
230  
231 =head1 NAME
232  
233 DBIx::Transaction::db - Database handle that is aware of nested transactions
234  
235 =head1 SYNOPSIS
236  
237 See L<DBIx::Transaction>
238  
239 =head1 DESCRIPTION
240  
241 When you connect to a database using DBIx::Transaction, your database handle
242 will be a DBIx::Transaction::db object. These objects keep track of your
243 transaction state, allowing for transactions to occur within transactions,
244 and only sending "C<commit>" or "C<rollback>" instructions to the underlying
245 database when the outermost transaction has completed. See L<DBIx::Transaction>
246 for a more complete explanation.
247  
248 =head1 METHODS
249  
250 =head2 Overridden Methods
251  
252 The following methods are overridden by DBIx::Transaction::db:
253  
254 =over
255  
256 =item begin_work
257  
258 Start a transaction.
259  
260 If there are no transactions currently running, C<begin_work> will check
261 if C<AutoCommit> is enabled. If it is enabled, a C<begin_work> instruction
262 is sent to the underlying database layer. If C<AutoCommit> is disabled, we
263 assume that the database has already started a transaction for us, and do
264 nothing. This means that B<you must always use begin_work to start a
265 transaction>, even if C<AutoCommit> is enabled!
266  
267 If there is a transaction started, C<begin_work> simply records that a nested
268 transaction has started.
269  
270 C<begin_work> returns the result of the database's C<begin_work> call if it
271 makes one; otherwise it always returns true.
272  
273 =item rollback
274  
275 Abort a transaction.
276  
277 If there are no sub-transactions currently running, C<rollback> will issue the
278 C<rollback> call to the underlying database layer.
279  
280 If there are sub-transactions currently running, C<rollback> notes that the
281 nested transaction has been aborted.
282  
283 If there is no transaction running at all, C<rollback> will raise a fatal
284 error.
285  
286 =item commit
287  
288 If there are sub-transactions currently running, C<commit> records that this
289 transaction has completed successfully and does nothing to the underlying
290 database layer.
291  
292 If there are no sub-transactions currently running, C<commit> checks if
293 there have been any transaction errors. If there has been a transaction
294 error, C<commit> raises an exception. Otherwise, a C<commit> call is
295 issued to the underlying database layer.
296  
297 If there is no transaction running at all, C<commit> will raise a fatal
298 error. This error will contain a full stack trace, and should also contain
299 the file names and line numbers where any rollbacks or query failures
300 happened.
301  
302 =item do
303  
304 Calls L<do()|DBI/do> on your underlying database handle. If an error
305 occurs, this is recorded and you will not be able to issue a C<commit>
306 for the current transaction.
307  
308 =back
309  
310 =head2 Extra Methods
311  
312 The following method is provided for convienence in setting up database
313 transactions:
314  
315 =over
316  
317 =item transaction($coderef[, $tries[, $when]])
318  
319 Execute the code contained inside C<$coderef> within a transaction.
320 C<$coderef> is expected to return a scalar value.
321 If C<$coderef> returns true, the transaction is committed. If
322 C<$coderef> returns false or raises a fatal error, the transaction
323 is rolled back. The return value is the same as what is returned by
324 C<$coderef>.
325  
326 This method is supplied to make it easier to create nested transactions
327 out of many small transactions. Example:
328  
329 sub get_max_id {
330 my $dbh = shift;
331 # this will roll back if it can't get MAX(num)
332 $dbh->transaction(sub {
333 if(my($id) = $dbh->selectrow_array("SELECT MAX(num) FROM foo")) {
334 return $id;
335 } else {
336 return;
337 }
338 });
339 }
340
341 sub do_something {
342 my($dbh, $num) = @_;
343 $dbh->transaction(sub {
344 $dbh->do("UPDATE foo SET bar = bar + 1 WHERE num = $num");
345 });
346 }
347
348 sub do_many_things {
349 my $dbh = shift;
350 # if any of these sub-transactions roll back, the whole thing will roll
351 # back. Try repeating the transaction up to 5 times.
352 $dbh->transaction(sub {
353 if(
354 do_something($dbh, 1) &&
355 do_something($dbh, 2) &&
356 (my $id = get_max_id($dbh))
357 ) {
358 return do_something($dbh, $id);
359 } else {
360 return;
361 }
362 }, 5);
363 }
364  
365 =over
366  
367 =item Re-trying transactions
368  
369 If C<$tries> is specified, the transaction will be tried up to
370 C<$tries> times before giving up. (Default: 1) Specify a negative
371 value to re-try forever.
372  
373 B<Note:> only the outermost transaction may attempt retries. This
374 is because if there is one failure within a transaction, the entire
375 transaction fails -- so any retries in nested transactions would have
376 to fail, by virtue of the previous attempt failing. If you try to set
377 up retries from inside a nested transaction, this will die with the
378 error "Transaction retry flow may only be set on the outermost transaction".
379  
380 C<$when> is an optional code reference that can be used to decide
381 if a transaction should be retried or not. It will be passed the
382 following arguments:
383  
384 =over
385  
386 =item The database handle (C<$dbh>)
387  
388 =item The return value of the transaction
389  
390 =item The exception raised by the transaction, if any (C<$@>)
391  
392 =item How many tries are left
393  
394 =back
395  
396 If the code reference returns true, the transaction will be run again.
397 If it returns false, the C<$dbh->transaction()> will finish, either
398 returning a value, or raising an exception if one was caused by
399 the last execution of C<$coderef>.
400  
401 The default handler for C<$when> is simply:
402  
403 sub {
404 my($dbh, $return_value, $return_exception, $tries) = @_;
405 return $tries && ($return_exception || !$return_value);
406 }
407  
408 =back
409  
410 =back
411  
412 =head2 Other Methods
413  
414 The following methods are used by the overridden methods. In most cases
415 you won't have to use them yourself.
416  
417 =over
418  
419 =item transaction_level
420  
421 Returns an integer value representing how deeply nested our transactions
422 currently are. eg; if we are in a top-level transaction, this returns "1";
423 if we are 4 transactions deep, this returns "4", if we are not in a transaction
424 at all, this returns "0". In some extreme cases this may be used to bail out
425 of a nested transaction safely, as in:
426  
427 $dbh->rollback while $dbh->transaction_level;
428  
429 But I would suggest that you structure your code so that each transaction
430 and sub-transaction bails out safely instead, as that's a lot easier to
431 trace and debug with confidence.
432  
433 =item transaction_error
434  
435 Returns a true value if a sub-transaction has rolled back, false otherwise.
436 Again, you could use this to back out of a transaction safely, but I'd suggest
437 just writing your code to handle this case on each transaction level instead.
438  
439 =item transaction_trace
440  
441 For debugging; If DBI's trace level is 3 or over, emit the current values
442 of all of the internal variables DBIx::Transaction uses to track it's
443 transaction states.
444  
445 =item inc_transaction_level
446  
447 Indicate that we have started a sub transaction by increasing
448 C<transaction_level> by one. This is used by the C<begin_work> override
449 and should not be called directly.
450  
451 =item dec_transaction_level
452  
453 Indicate that we have finished a sub transaction by decrementing
454 C<transaction_level> by one. If this results in a negative number
455 (meaning more transactions have been commited/rolled back than started),
456 C<dec_transaction_level> throws a fatal error. This is used by the
457 C<commit> and C<rollback> methods and should not be called directly.
458  
459 =item inc_transaction_error
460  
461 Indicate that a sub-transaction has failed and that the entire
462 transaction should not be allowed to be committed. This is done
463 automatically whenever a sub-transaction issues a C<rollback>.
464 Optional parameters are the package, filename, and line where
465 the transaction error occured. If provided, they will be used in
466 error messages relating to the rollback.
467  
468 =item clear_transaction_error
469  
470 Clear the transaction error flag. This flag is set whenever a
471 sub-transaction issues a C<rollback>, and cleared whenever the
472 outermost transaction issues a C<rollback>.
473  
474 =item close_transaction($method)
475  
476 Close the outermost transaction by calling C<$method>
477 ("C<commit>" or "C<rollback>") on the underlying database layer and
478 resetting the DBIx::Transaction state. This method is used by the
479 C<commit> and C<rollback> methods and you shouldn't need to use it yourself,
480 unless you wanted to forcibly bail out of an entire transaction without
481 calling C<rollback> repeatedly, but as stated above, that's a bad idea,
482 isn't it?
483  
484 =back
485  
486 =head1 SEE ALSO
487  
488 L<DBI>, L<DBIx::Transaction>
489  
490 =head1 AUTHOR
491  
492 Tyler "Crackerjack" MacDonald <japh@crackerjack.net>
493  
494 =head1 LICENSE
495  
496 Copyright 2005 Tyler MacDonald
497 This is free software; you may redistribute it under the same terms as perl itself.
498  
499 =cut
↑ Summary blib/lib/DBIx/Transaction/st.pm »