File Coverage

lib/LEOCHARRE/Database.pm
Criterion Covered Total %
statement 76 101 75.2
branch 14 46 30.4
condition 3 8 37.5
subroutine 18 24 75.0
pod 13 13 100.0
total 124 192 64.5


line stmt bran cond sub pod time code
1             package LEOCHARRE::Database;
2 1     1   32615 use DBI;
  1         39444  
  1         74  
3 1     1   11 use strict;
  1         2  
  1         34  
4 1     1   6 use warnings;
  1         7  
  1         34  
5 1     1   5 use Carp;
  1         1  
  1         93  
6 1     1   945 use LEOCHARRE::DEBUG;
  1         1958  
  1         7  
7 1     1   100 use base 'LEOCHARRE::Database::Base';
  1         1  
  1         544  
8 1     1   6 use vars qw($VERSION);
  1         2  
  1         1062  
9             $VERSION = sprintf "%d.%02d", q$Revision: 1.16 $ =~ /(\d+)/g;
10              
11              
12             sub dbh {
13 24     24 1 13885 my $self = shift;
14 24 100       87 unless( $self->{DBH} ){
15            
16 1   50     17 $self->{AUTOCOMMIT} ||=0;
17              
18 1 50       9 if( ! defined $self->{DBABSPATH} ){
19 0         0 debug('will open mysql connection..');
20 0 0       0 my $dbname = $self->{DBNAME} or confess('missing DBNAME argument to constructor');
21 0 0       0 my $user = $self->{DBUSER} or confess('missing DBUSER argument to constructor');
22 0 0       0 my $password = $self->{DBPASSWORD} or confess('missing DBPASSWORD argument to constructor');
23              
24 0 0       0 $self->{DBH} = DBI::connect_mysql($self->{DBNAME}, $self->{DBUSER}, $self->{DBPASSWORD}, $self->{DBHOST})
25             or die('connect_mysql failed');
26 0         0 debug("ok");
27             }
28              
29            
30             else {
31 1 50       8 $self->{DBABSPATH} or confess('DBABSPATH argument to constructor bad');
32 1         20 debug("will open sqlite connection..$$self{DBABSPATH} ");
33            
34 1 50       27 $self->{DBH} = DBI::connect_sqlite($self->{DBABSPATH}) or die('conect_sqlite failed');
35             }
36 1         4 $self->{__DBH_IS_LOCAL_} = 1;
37            
38 1         7 $self->{DBH}->{RaiseError} = 1; # freak out when things go bad
39             #$self->{DBH}->{PrintError} = 1; # print error or not
40            
41 1         5 $self->{DBH}->{AutoCommit} = $self->{AUTOCOMMIT};
42              
43 1 50       5 if ($DEBUG){
44 0         0 $LEOCHARRE::Database::Base::DEBUG=1;
45             }
46            
47            
48             }
49 24         23020 return $self->{DBH};
50             }
51              
52              
53             # did the connection happen here?
54             sub _dbh_is_local {
55 1     1   2 my $self = shift;
56 1   50     4 $self->{__DBH_IS_LOCAL_} ||=0;
57 1         4 return $self->{__DBH_IS_LOCAL_};
58             }
59              
60              
61             sub dbh_selectcol {
62 1     1 1 311 my ($self, $statement) = @_;
63 1         3 return $self->dbh->selectcol($statement);
64             }
65              
66              
67             sub dbh_do {
68 0     0 1 0 my($self, $arg) = @_;
69 0 0       0 defined $arg or confess('missing arg');
70 0 0       0 ref $arg eq 'HASH' or confess('arg must be hashref');
71 0 0       0 defined $arg->{sqlite} or confess('missing sqlite key');
72 0 0       0 defined $arg->{mysql} or confess('missing mysql key');
73              
74 0 0       0 my $q = $self->dbh->is_mysql ? $arg->{mysql} : $arg->{sqlite};
75              
76 0         0 $self->dbh->do($q) ; # TODO or .. ????
77              
78 0         0 return 1;
79             }
80             sub dbh_count {
81 2     2 1 305 my ($self,$statement) = @_;
82 2         7 return $self->dbh->rows_count($statement);
83             }
84              
85             sub dbh_sth {
86 2     2 1 5 my ($self, $statement) = @_;
87 2 50       8 $statement or confess("missing statement argument");
88            
89 2 50       9 unless ($self->{_handles}->{$statement}){
90 2   50     7 $self->{_handles} ||={};
91 2         14 debug("preparing [$statement]..");
92            
93 2         22 local $self->dbh->{RaiseError};
94 2         8 my $sth = $self->dbh->prepare($statement);
95 2 50       142 defined $sth or confess("statment [$statement] failed to prepare, ". $self->dbh->errstr );
96 2         20 $self->{_handles}->{$statement} = $sth;
97             }
98              
99 2         13 return $self->{_handles}->{$statement};
100             }
101              
102             sub dbh_is_sqlite {
103 1     1 1 2 my $self = shift;
104 1         3 return $self->dbh->is_sqlite;
105             }
106              
107             sub dbh_is_mysql {
108 1     1 1 2 my $self = shift;
109 1         3 return $self->dbh->is_mysql;
110             }
111              
112             sub dbh_driver {
113 2     2 1 29 my $self = shift;
114 2         6 return $self->dbh->driver;
115             }
116              
117              
118              
119             sub dbh_droptable {
120 0     0 1 0 my ($self,$tablename) = @_;
121 0         0 return $self->dbh->drop_table($tablename);
122             }
123              
124             sub dbh_table_exists {
125 0     0 1 0 my ($self,$tablename) = @_;
126 0         0 return $self->dbh->table_exists($tablename);
127             }
128              
129             sub dbh_table_dump {
130 0     0 1 0 my ($self,$tablename) = @_;
131 0         0 return $self->dbh->table_dump($tablename);
132             }
133              
134              
135              
136             sub dbh_lid {
137 1     1 1 4 my ($self,$tablename) = @_;
138 1         4 return $self->dbh->lid($tablename);
139             }
140              
141              
142             # for retro
143             sub dbh_close_active_handles {
144 0     0 1 0 my $self = shift;
145 0 0       0 defined $self->dbh or return 1;
146 0         0 return $self->dbh->close_active_handles;
147             }
148              
149              
150              
151             sub DESTROY {
152 1     1   300 my $self = shift;
153              
154 1 50       3 defined $self->dbh or return 1;
155            
156 1 50 0     6 $self->_dbh_is_local or debug("dbh is not local\n") and return 1;
157             # unless( $self->dbh->{AutoCommit} or return 1;
158 1         5 debug("dbh is local..");
159            
160              
161 1         8 $self->dbh->close_active_handles;
162              
163            
164            
165 1 50       4 unless ( $self->dbh->{AutoCommit} ){
166 1         4 debug("committing..");
167 1         7 local $self->dbh->{RaiseError};
168 1 50       5 $self->dbh->commit or confess("cannot commit.. ".$self->dbh->errstr);
169 1         13 debug("done.\n");
170             }
171              
172             {
173             # sqlite reports errors here.. a bug
174             # "closing dbh with active statement handles at.."
175             # this is a brute force approach to get rid of that warning
176 1     1   5 no warnings;
  1         2  
  1         112  
  1         39  
177 1 50   0   8 (local $SIG{'__WARN__'} = sub {} ) if $self->dbh->is_sqlite;
  0         0  
178            
179 1         6 $self->dbh->disconnect;
180 1         5 debug("disconnected.\n");
181             };
182            
183 1         231 return 1;
184             }
185              
186              
187              
188              
189              
190              
191             1;
192              
193             __END__