Transaction.pm untested

filter: Statementstmt 0.0% 0 / 65 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 117 CC 10 SCAR 23.1
CC
10
Cov
0%
DESTROY
20.0
start
12.0
commit
12.0
CRAP 10.1
1 package DBI::Transaction;
2  
3 use strict;
4 use vars qw($VERSION);
5  
6 $VERSION = 0.101;
7  
8 use DBI();
9  
10 sub start {
11 my $class = shift;
12 my $dbh = shift;
13  
14 my $self = {
15 DBH => $dbh,
16 # 0 - finished, 1 - not finished, no need for commit, 2 - needs commit:
17 _STATE => $dbh->{AutoCommit} ? 2 : 1
18 };
19 bless $self, $class;
20 if($dbh->{AutoCommit}) {
21 local $dbh->{HandleSetErr} = _handle_set_err("new");
22 $dbh->begin_work or return undef;
23 }
24 return $self;
25 }
26  
27 sub DESTROY {
28 my $self = shift;
29  
30 return unless $self->{_STATE};
31 my $dbh = $self->{DBH};
32  
33 # Crude hack against segfaults in
34 # Perl 5.8.4 for i386-linux-thread-multi + DBI 1.41 + DBD::mysql 2.9003
35 # Scripts are like:
36 # use DBI; use DBI::Transaction;
37 # $dbh=DBI->connect("DBI:mysql:database=XXX","XXX","XXX",{AutoCommit=>0});
38 # $t=DBI::Transaction->start($dbh);
39 return unless $dbh && ref($dbh) && tied($dbh) && $dbh->{Active};
40  
41 local $dbh->{HandleSetErr} = _handle_set_err("DESTROY");
42 $dbh->set_err(0, "Unfinished transaction - may cause subtle bugs!");
43 if($self->{_STATE} == 2) {
44 #local $dbh->{RaiseError}; # TODO: What it should be?
45 $self->{DBH}->rollback;
46 } else {
47 local $dbh->{PrintError} = 0;
48 $dbh->set_err(1, DBI::Transaction::CannotRollback->new);
49 }
50 }
51  
52 sub _handle_set_err {
53 my $method = shift;
54  
55 my $id = __PACKAGE__ . '::' . $method;
56 return sub { $_[4] = $id; 0 };
57 }
58  
59 sub commit {
60 my $self = shift;
61  
62 my $dbh = $self->{DBH};
63 local $dbh->{HandleSetErr} = _handle_set_err("commit");
64 my $st = $self->{_STATE};
65 if(!$st) {
66 $dbh->set_err(1, "Attempt to commit a finished transaction.");
67 return undef;
68 }
69 $self->{_STATE} = 0; # Want to prevent DESTROY warning.
70 my $ret = $st == 2 ? $dbh->commit : 1;
71 delete $self->{DBH}; # See DESTROY about the bug
72 return $ret;
73 }
74  
75 sub rollback {
76 my $self = shift;
77  
78 my $dbh = $self->{DBH};
79 local $dbh->{HandleSetErr} = _handle_set_err("commit");
80 my $st = $self->{_STATE};
81 if(!$st) {
82 $dbh->set_err(1, "Attempt to roll back a finished transaction.");
83 return undef;
84 }
85 $self->{_STATE} = 0; # Want to prevent DESTROY warning.
86 return $dbh->rollback if $st == 2;
87 local $dbh->{PrintError} = 0;
88 $dbh->set_err(1, DBI::Transaction::CannotRollback->new);
89 delete $self->{DBH}; # See DESTROY about the bug
90 return undef;
91 }
92  
93 sub is_finished {
94 my $self = shift;
95  
96 return !$self->{_STATE};
97 }
98  
99 1; # DBI::Transaction
100  
101 # This exception is ONLY for the case when a subtransaction cannot
102 # be rolled back becuase it is a subtransaction, not for e.g.
103 # broken DB connection during rollback.
104 package DBI::Transaction::CannotRollback;
105 use overload '""' => sub { "(Sub)transaction cannot be rolled back." };
106 sub new { return bless {}, shift; }
107 1; # DBI::Transaction::CannotRollback
108  
109 __END__