Database.pm

filter: Statementstmt 76.4% 78 / 102 Branchbran 30.4% 14 / 46 Conditioncond 40.0% 4 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 75.0% 18 / 24 Podpod 100% 13 / 13 totaltotal 63.5% 127 / 200 CC 28 SCAR 25.8
CC
28
Cov
61%
dbh_do
42.0
dbh
27.3
DESTROY
7.8
CRAP 13.1
↑ Summary Database/Base.pm »
1 package LEOCHARRE::Database;
2 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use LEOCHARRE::DEBUG;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use base 'LEOCHARRE::Database::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 $VERSION = sprintf "%d.%02d", q$Revision: 1.16 $ =~ /(\d+)/g;
10  
11  
12 24 ▶sub dbh {
Subroutine dbh: called CC=11 SCAR=33.1
Pod documented
13 24 my $self = shift;
14 24 ▶ unless( $self->{DBH} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"DBH"}) 1 23 24
15
16 1 ▶ $self->{AUTOCOMMIT} ||=0;
Condition50%1/2
$self->{"AUTOCOMMIT"} ||= 0
l!l
01
MC/DC0%0/1
$self->{"AUTOCOMMIT"} ||= 0
$self->{"AUTOCOMMIT"}
Truth table50%1/2
A
A= $self->{"AUTOCOMMIT"}
Aresult
11
00
17  
18 1 ▶ if( ! defined $self->{DBABSPATH} ){
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'DBABSPATH'}) { } 0 1 1
19 0 debug('will open mysql connection..');
20 0 ▶ my $dbname = $self->{DBNAME} or confess('missing DBNAME argument to constructor');
Branch: 1 branch
BranchTrueFalseTotal
my $dbname = $self->{"DBNAME"} or confess("missing DBNAME argument to constructor") 0 0 0
21 0 ▶ my $user = $self->{DBUSER} or confess('missing DBUSER argument to constructor');
Branch: 1 branch
BranchTrueFalseTotal
my $user = $self->{"DBUSER"} or confess("missing DBUSER argument to constructor") 0 0 0
22 0 ▶ my $password = $self->{DBPASSWORD} or confess('missing DBPASSWORD argument to constructor');
Branch: 1 branch
BranchTrueFalseTotal
my $password = $self->{"DBPASSWORD"} or confess("missing DBPASSWORD argument to constructor") 0 0 0
23  
24 $self->{DBH} = DBI::connect_mysql($self->{DBNAME}, $self->{DBUSER}, $self->{DBPASSWORD}, $self->{DBHOST})
25 0 ▶ or die('connect_mysql failed');
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DBH"} = DBI::connect_mysql($self->{"DBNAME"}, $self->{"DBUSER"}, $self->{"DBPASSWORD"}, $self->{"DBHOST"}) or die "connect_mysql failed" 0 0 0
26 0 debug("ok");
27 }
28  
29
30 else {
31 1 ▶ $self->{DBABSPATH} or confess('DBABSPATH argument to constructor bad');
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DBABSPATH"} or confess("DBABSPATH argument to constructor bad") 1 0 1
32 1 debug("will open sqlite connection..$$self{DBABSPATH} ");
33
34 1 ▶ $self->{DBH} = DBI::connect_sqlite($self->{DBABSPATH}) or die('conect_sqlite failed');
Branch: 1 branch
BranchTrueFalseTotal
$self->{"DBH"} = DBI::connect_sqlite($self->{"DBABSPATH"}) or die "conect_sqlite failed" 1 0 1
35 }
36 1 $self->{__DBH_IS_LOCAL_} = 1;
37
38 1 $self->{DBH}->{RaiseError} = 1; # freak out when things go bad
39 #$self->{DBH}->{PrintError} = 1; # print error or not
40
41 1 $self->{DBH}->{AutoCommit} = $self->{AUTOCOMMIT};
42  
43 1 ▶ if ($DEBUG){
Branch: 1 branch
BranchTrueFalseTotal
if ($LEOCHARRE::Database::DEBUG) 0 1 1
44 0 $LEOCHARRE::Database::Base::DEBUG=1;
45 }
46
47
48 }
49 24 return $self->{DBH};
50 }
51  
52  
53 # did the connection happen here?
54 1 ▶sub _dbh_is_local {
Subroutine _dbh_is_local: called CC=2 SCAR=7.1
55 1 my $self = shift;
56 1 ▶ $self->{__DBH_IS_LOCAL_} ||=0;
Condition50%1/2
$self->{"__DBH_IS_LOCAL_"} ||= 0
l!l
10
MC/DC0%0/1
$self->{"__DBH_IS_LOCAL_"} ||= 0
$self->{"__DBH_IS_LOCAL_"}
Truth table50%1/2
A
A= $self->{"__DBH_IS_LOCAL_"}
Aresult
11
00
57 1 return $self->{__DBH_IS_LOCAL_};
58 }
59  
60  
61 1 ▶sub dbh_selectcol {
Subroutine dbh_selectcol: called CC=1 SCAR=0.0
Pod documented
62 1 my ($self, $statement) = @_;
63 1 return $self->dbh->selectcol($statement);
64 }
65  
66  
67 0 ▶sub dbh_do {
Subroutine dbh_do: not called CC=6 SCAR=37.4
Pod documented
68 0 my($self, $arg) = @_;
69 0 ▶ defined $arg or confess('missing arg');
Branch: 1 branch
BranchTrueFalseTotal
defined $arg or confess("missing arg") 0 0 0
70 0 ▶ ref $arg eq 'HASH' or confess('arg must be hashref');
Branch: 1 branch
BranchTrueFalseTotal
ref $arg eq "HASH" or confess("arg must be hashref") 0 0 0
71 0 ▶ defined $arg->{sqlite} or confess('missing sqlite key');
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->{"sqlite"} or confess("missing sqlite key") 0 0 0
72 0 ▶ defined $arg->{mysql} or confess('missing mysql key');
Branch: 1 branch
BranchTrueFalseTotal
defined $arg->{"mysql"} or confess("missing mysql key") 0 0 0
73  
74 0 ▶ my $q = $self->dbh->is_mysql ? $arg->{mysql} : $arg->{sqlite};
Branch: 1 branch
BranchTrueFalseTotal
$self->dbh->is_mysql ? : 0 0 0
75  
76 0 $self->dbh->do($q) ; # TODO or .. ????
77  
78 0 return 1;
79 }
80 2 ▶sub dbh_count {
Subroutine dbh_count: called CC=1 SCAR=0.0
Pod documented
81 2 my ($self,$statement) = @_;
82 2 return $self->dbh->rows_count($statement);
83 }
84  
85 2 ▶sub dbh_sth {
Subroutine dbh_sth: called CC=5 SCAR=16.6
Pod documented
86 2 my ($self, $statement) = @_;
87 2 ▶ $statement or confess("missing statement argument");
Branch: 1 branch
BranchTrueFalseTotal
$statement or confess("missing statement argument") 2 0 2
88
89 2 ▶ unless ($self->{_handles}->{$statement}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_handles"}{$statement}) 2 0 2
90 2 ▶ $self->{_handles} ||={};
Condition50%1/2
$self->{"_handles"} ||= {}
l!l
20
MC/DC0%0/1
$self->{"_handles"} ||= {}
$self->{"_handles"}
Truth table50%1/2
A
A= $self->{"_handles"}
Aresult
11
00
91 2 debug("preparing [$statement]..");
92
93 2 local $self->dbh->{RaiseError};
94 2 my $sth = $self->dbh->prepare($statement);
95 2 ▶ defined $sth or confess("statment [$statement] failed to prepare, ". $self->dbh->errstr );
Branch: 1 branch
BranchTrueFalseTotal
defined $sth or confess("statment [$statement] failed to prepare, " . $self->dbh->errstr) 2 0 2
96 2 $self->{_handles}->{$statement} = $sth;
97 }
98  
99 2 return $self->{_handles}->{$statement};
100 }
101  
102 1 ▶sub dbh_is_sqlite {
Subroutine dbh_is_sqlite: called CC=1 SCAR=0.0
Pod documented
103 1 my $self = shift;
104 1 return $self->dbh->is_sqlite;
105 }
106  
107 1 ▶sub dbh_is_mysql {
Subroutine dbh_is_mysql: called CC=1 SCAR=0.0
Pod documented
108 1 my $self = shift;
109 1 return $self->dbh->is_mysql;
110 }
111  
112 2 ▶sub dbh_driver {
Subroutine dbh_driver: called CC=1 SCAR=0.0
Pod documented
113 2 my $self = shift;
114 2 return $self->dbh->driver;
115 }
116  
117  
118  
119 0 ▶sub dbh_droptable {
Subroutine dbh_droptable: not called CC=1 SCAR=6.9
Pod documented
120 0 my ($self,$tablename) = @_;
121 0 return $self->dbh->drop_table($tablename);
122 }
123  
124 0 ▶sub dbh_table_exists {
Subroutine dbh_table_exists: not called CC=1 SCAR=6.9
Pod documented
125 0 my ($self,$tablename) = @_;
126 0 return $self->dbh->table_exists($tablename);
127 }
128  
129 0 ▶sub dbh_table_dump {
Subroutine dbh_table_dump: not called CC=1 SCAR=6.9
Pod documented
130 0 my ($self,$tablename) = @_;
131 0 return $self->dbh->table_dump($tablename);
132 }
133  
134  
135  
136 1 ▶sub dbh_lid {
Subroutine dbh_lid: called CC=1 SCAR=0.0
Pod documented
137 1 my ($self,$tablename) = @_;
138 1 return $self->dbh->lid($tablename);
139 }
140  
141  
142 # for retro
143 0 ▶sub dbh_close_active_handles {
Subroutine dbh_close_active_handles: not called CC=2 SCAR=17.9
Pod documented
144 0 my $self = shift;
145 0 ▶ defined $self->dbh or return 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->dbh or return 1 0 0 0
146 0 return $self->dbh->close_active_handles;
147 }
148  
149  
150  
151 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=7 SCAR=20.5
152 1 my $self = shift;
153  
154 1 ▶ defined $self->dbh or return 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->dbh or return 1 1 0 1
155
156 1 ▶ $self->_dbh_is_local or debug("dbh is not local\n") and return 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_dbh_is_local or debug("dbh is not local\n") and return 1 1 0 1
Condition25%1/4
$self->_dbh_is_local || debug("dbh is not local\n") && (return 1)
l!l
10
debug("dbh is not local\n") and return 1
!ll
00
MC/DC0%0/2
$self->_dbh_is_local || debug("dbh is not local\n") && (return 1)
$self->_dbh_is_local debug("dbh is not local\n")
Truth table33%1/3
A || B
A= $self->_dbh_is_localB= debug("dbh is not local\n")
ABresult
1X1
011
000
157 # unless( $self->dbh->{AutoCommit} or return 1;
158 1 debug("dbh is local..");
159
160  
161 1 $self->dbh->close_active_handles;
162  
163
164
165 1 ▶ unless ( $self->dbh->{AutoCommit} ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->dbh->{"AutoCommit"}) 1 0 1
166 1 debug("committing..");
167 1 local $self->dbh->{RaiseError};
168 1 ▶ $self->dbh->commit or confess("cannot commit.. ".$self->dbh->errstr);
Branch: 1 branch
BranchTrueFalseTotal
$self->dbh->commit or confess('cannot commit.. ' . $self->dbh->errstr) 1 0 1
169 1 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 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
177 1 ▶ (local $SIG{'__WARN__'} = sub {} ) if $self->dbh->is_sqlite;
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
if $self->dbh->is_sqlite 1 0 1
178
179 1 $self->dbh->disconnect;
180 1 debug("disconnected.\n");
181 };
182
183 1 return 1;
184 }
185  
186  
187  
188  
189  
190  
191 1 1;
192  
193 __END__
↑ Summary Database/Base.pm »