File Coverage

blib/lib/DBR/Misc/Connection.pm
Criterion Covered Total %
statement 50 61 81.9
branch 12 24 50.0
condition n/a
subroutine 17 22 77.2
pod 0 19 0.0
total 79 126 62.7


line stmt bran cond sub pod time code
1             # the contents of this file are Copyright (c) 2009 Daniel Norman
2             # This program is free software; you can redistribute it and/or
3             # modify it under the terms of the GNU General Public License as
4             # published by the Free Software Foundation.
5              
6             package DBR::Misc::Connection;
7              
8 17     17   142 use strict;
  17         35  
  17         1458  
9 17     17   98 use base 'DBR::Common';
  17         42  
  17         24320  
10              
11 0     0 0 0 sub required_config_fields { [qw(database hostname user password)] };
12              
13             sub new {
14 51     51 0 134 my( $package ) = shift;
15              
16 51         230 my %params = @_;
17 51         210 my $self = {
18             session => $params{session},
19             dbh => $params{dbh},
20             };
21              
22 51         194 bless( $self, $package );
23              
24 51 50       335 return $self->_error('session is required') unless $self->{session};
25 51 50       176 return $self->_error('dbh is required') unless $self->{dbh};
26 51         154 $self->{lastping} = time; # assume the setup of the connection as being a good ping
27              
28 51         267 return $self;
29             }
30              
31 75     75 0 513 sub dbh { $_[0]->{dbh} }
32 556     556 0 1258 sub do { my $self = shift; return $self->_wrap($self->{dbh}->do(@_)) }
  556         9844  
33 563     563 0 1683 sub prepare { my $self = shift; return $self->_wrap($self->{dbh}->prepare(@_)) }
  563         7563  
34 0     0 0 0 sub execute { my $self = shift; return $self->_wrap($self->{dbh}->execute(@_)) }
  0         0  
35 13     13 0 33 sub selectrow_array { my $self = shift; return $self->_wrap($self->{dbh}->selectrow_array(@_)) }
  13         332  
36 51     51 0 101 sub disconnect { my $self = shift; return $self->_wrap($self->{dbh}->disconnect(@_)) }
  51         12976  
37 785     785 0 8805 sub quote { shift->{dbh}->quote(@_) }
38              
39             sub ping {
40 2964     2964 0 15633 my $self = shift;
41              
42             #$self->_logDebug3('PING'); # Logging is inefficient
43 2964 100       50951 return 1 if $self->{lastping} + 2 > time; # only ping every 5 seconds
44              
45             #$self->_logDebug3('REAL PING'); # Logging is inefficient
46 7 50       120 $self->{dbh}->ping or return undef;
47 7         603 $self->{lastping} = time;
48 7         64 return 1;
49             }
50              
51             sub begin {
52 17     17 0 43 my $self = shift;
53 17 50       88 return $self->_error('Transaction is already open - cannot begin') if $self->{'_intran'};
54              
55 17         198 $self->_logDebug('BEGIN');
56 17 50       215 $self->{dbh}->do('BEGIN') or return $self->_error('Failed to begin transaction');
57 17         3253 $self->{_intran} = 1;
58              
59 17         94 return 1;
60             }
61              
62             sub commit{
63 17     17 0 44 my $self = shift;
64 17 50       82 return $self->_error('Transaction is not open - cannot commit') if !$self->{'_intran'};
65              
66 17         101 $self->_logDebug('COMMIT');
67 17 50       271 $self->{dbh}->do('COMMIT') or return $self->_error('Failed to commit transaction');
68              
69 17         2560908 $self->{_intran} = 0;
70              
71 17         468 return 1;
72             }
73              
74             sub rollback{
75 0     0 0 0 my $self = shift;
76 0 0       0 return $self->_error('Transaction is not open - cannot rollback') if !$self->{'_intran'};
77              
78 0         0 $self->_logDebug('ROLLBACK');
79 0 0       0 $self->{dbh}->do('ROLLBACK') or return $self->_error('Failed to rollback transaction');
80              
81 0         0 $self->{_intran} = 0;
82              
83 0         0 return 1;
84             }
85              
86             ######### ability check stubs #########
87              
88 24     24 0 122 sub can_trust_execute_rowcount{ 0 }
89              
90             ############ sequence stubs ###########
91             sub prepSequence{
92 496     496 0 1810 return 1;
93             }
94             sub getSequenceValue{
95 0     0 0 0 return -1;
96             }
97             #######################################
98              
99 17 50   17 0 561 sub b_intrans{ $_[0]->{_intran} ? 1:0 }
100 0     0 0 0 sub b_nestedTrans{ 0 }
101              
102             sub quiet_next_error{
103 42     42 0 90 my $self = shift;
104              
105 42         1243 $self->{dbh}->{PrintError} = 0;
106              
107 42         490 return 1;
108             }
109              
110             sub _wrap{
111 1183     1183   4279576 my $self = shift;
112              
113             #reset any variables now
114 1183         16033 $self->{dbh}->{PrintError} = 1;
115              
116 1183 100       20984 return wantarray?@_:$_[0];
117             }
118             1;