Database/Base.pm

filter: Statementstmt 83.1% 118 / 142 Branchbran 48.6% 37 / 76 Conditioncond 24.0% 12 / 50 MC/DCmcdc 4.1% 1 / 24 Subroutinesub 87.5% 28 / 32 Podpod 85.7% 12 / 14 totaltotal 61.5% 208 / 338 CC 55 SCAR 23.5
CC
55
Cov
62%
connect_mysql
56.0
table_dump
15.2
drop_table
12.0
CRAP 10.5
« Database.pm ↑ Summary DBI.pm »
1 package LEOCHARRE::Database::Base;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use LEOCHARRE::DEBUG;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 3 ▶use vars qw($VERSION @ISA @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 @ISA = qw/Exporter/;
11  
12 3 @EXPORT_OK = qw(
13 rows_count
14 is_sqlite
15 is_mysql
16 drop_table
17 lid
18 close_active_handles
19 table_exists
20 table_dump
21 driver
22 selectcol
23 connect_sqlite
24 connect_mysql
25 );
26  
27 3 %EXPORT_TAGS = ( all => \@EXPORT_OK );
28  
29 3 $VERSION = sprintf "%d.%02d", q$Revision: 1.7 $ =~ /(\d+)/g;
30  
31  
32  
33 7 ▶sub DBI::db::rows_count { return rows_count(@_); }
Subroutine rows_count: called CC=1 SCAR=0.0
34 3 ▶sub DBI::db::is_sqlite { return is_sqlite(@_); }
Subroutine is_sqlite: called CC=1 SCAR=0.0
35 2 ▶sub DBI::db::is_mysql { return is_mysql(@_); }
Subroutine is_mysql: called CC=1 SCAR=0.0
36 0 ▶sub DBI::db::drop_table { return drop_table(@_); }
Subroutine drop_table: not called CC=1 SCAR=6.9
37 3 ▶sub DBI::db::lid { return lid(@_); }
Subroutine lid: called CC=1 SCAR=0.0
38 1 ▶sub DBI::db::close_active_handles { return close_active_handles(@_); }
Subroutine close_active_handles: called CC=1 SCAR=0.0
39 4 ▶sub DBI::db::table_exists { return table_exists(@_); }
Subroutine table_exists: called CC=1 SCAR=0.0
40 2 ▶sub DBI::db::table_dump { return table_dump(@_); }
Subroutine table_dump: called CC=1 SCAR=0.0
41 3 ▶sub DBI::db::driver { return driver(@_); }
Subroutine driver: called CC=1 SCAR=0.0
42  
43 2 ▶sub DBI::db::selectcol { return selectcol(@_); }
Subroutine selectcol: called CC=1 SCAR=0.0
44  
45 3 ▶sub DBI::connect_sqlite { return connect_sqlite(@_); }
Subroutine connect_sqlite: called CC=1 SCAR=0.0
Pod undocumented
46 0 ▶sub DBI::connect_mysql { return connect_mysql(@_); }
Subroutine connect_mysql: not called CC=1 SCAR=6.9
Pod undocumented
47  
48  
49  
50 # FUNCTIONAL ORIENTED
51  
52 7 ▶sub rows_count {
Subroutine rows_count: called CC=8 SCAR=24.3
Pod documented
53 7 my ($dbh,$statement,$key,$val) = @_;
54 7 ▶ defined $statement or confess('missing statement argument');
Branch: 1 branch
BranchTrueFalseTotal
defined $statement or confess("missing statement argument") 7 0 7
55 7 $statement=~s/^\s+|\s+$//g;
56  
57
58 # is first arg a table name?
59 7 ▶ if ($statement=~/^(\w+)$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($statement =~ /^(\w+)$/) { } 1 6 7
60 1 my $tname = $1;
61 1 debug("arg 1 is table name");
62  
63 1 $statement = "SELECT COUNT(*) FROM $tname";
64  
65 1 ▶ if (defined $key and defined $val){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key and defined $val) 0 1 1
Condition33%1/3
defined $key and defined $val
!ll&&!rl&&r
100
MC/DC0%0/2
defined $key and defined $val
defined $key defined $val
Truth table33%1/3
A and B
A= defined $keyB= defined $val
ABresult
0X0
100
111
66 0 debug("key and val arguments are defined also [$key:$val]");
67  
68 0 $statement.=" WHERE $key='$val'";
69 0 debug("stmnt: [$statement]\n");
70 }
71
72 }
73  
74 # if not, we expect no more arguments
75 else {
76 6 debug("arg 1 is statement");
77 6 ▶ if (defined $key or defined $val){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key or defined $val) 0 6 6
Condition33%1/3
defined $key or defined $val
l!l&&r!l&&!r
006
MC/DC0%0/2
defined $key or defined $val
defined $key defined $val
Truth table33%1/3
A or B
A= defined $keyB= defined $val
ABresult
1X1
011
000
78 0 carp('first argument was a statement, no more arguments should be provided.');
79 }
80
81 6 ▶ $statement=~/count\s*\(/i or confess("statement to dbh_count() must contain COUNT()");
Branch: 1 branch
BranchTrueFalseTotal
$statement =~ /count\s*\(/i or confess("statement to dbh_count() must contain COUNT()") 6 0 6
82  
83 }
84  
85 7 return _rows_count($dbh,$statement);
86 }
87  
88 7 ▶sub _rows_count {
Subroutine _rows_count: called CC=4 SCAR=14.0
89 7 my ($dbh,$statement) = @_;
90 7 ▶ defined $statement or confess('missing statement argument');
Branch: 1 branch
BranchTrueFalseTotal
defined $statement or confess("missing statement argument") 7 0 7
91 7 ▶ my $c = $dbh->prepare($statement) or confess($dbh->errstr);
Branch: 1 branch
BranchTrueFalseTotal
my $c = $dbh->prepare($statement) or confess($dbh->errstr) 7 0 7
92 7 $c->execute;
93 7 my $r = $c->fetchrow_arrayref;
94 7 my $count = $r->[0];
95 7 ▶ $count ||= 0;
Condition100%2/2
$count ||= 0
l!l
61
MC/DC100%1/1
$count ||= 0
$count
Truth table100%2/2
A
A= $count
Aresult
11
00
96 7 return $count;
97 }
98  
99 3 ▶sub is_sqlite {
Subroutine is_sqlite: called CC=3 SCAR=11.4
Pod documented
100 3 my $dbh = shift;
101 3 ▶ my $d = driver($dbh) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $d = driver($dbh) or return 0 3 0 3
102 3 ▶ $d=~/sqlite/i or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$d =~ /sqlite/i or return 0 3 0 3
103 3 return 1;
104 }
105  
106 2 ▶sub is_mysql {
Subroutine is_mysql: called CC=3 SCAR=12.5
Pod documented
107 2 my $dbh = shift;
108 2 ▶ my $d = driver($dbh) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $d = driver($dbh) or return 0 2 0 2
109 2 ▶ $d=~/mysql/i or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$d =~ /mysql/i or return 0 0 2 2
110 0 return 1;
111 }
112  
113 8 ▶sub driver {
Subroutine driver: called CC=5 SCAR=19.6
Pod documented
114 8 my $dbh = shift;
115 8 ▶ defined $dbh or confess('missing dbh object as arg');
Branch: 1 branch
BranchTrueFalseTotal
defined $dbh or confess("missing dbh object as arg") 8 0 8
116 8 ▶ defined $dbh->{Driver} or debug("attribute 'Driver' not present in dbh obj passed") and return;
Branch: 1 branch
BranchTrueFalseTotal
defined $dbh->{"Driver"} or debug("attribute 'Driver' not present in dbh obj passed") and return 8 0 8
Condition20%1/5
defined $dbh->{'Driver'} || debug(q[attribute 'Driver' not present in dbh obj passed]) && (return)
l!l
800
debug("attribute 'Driver' not present in dbh obj passed") and return
!ll
00
MC/DC0%0/2
defined $dbh->{'Driver'} || debug(q[attribute 'Driver' not present in dbh obj passed]) && (return)
defined $dbh->{'Driver'} debug("attribute 'Driver' not present in dbh obj passed")
Truth table33%1/3
A || B
A= defined $dbh->{'Driver'}B= debug("attribute 'Driver' not present in dbh obj passed")
ABresult
1X1
011
000
117 8 ▶ my $n = $dbh->{Driver}->{Name} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $n = $dbh->{"Driver"}{"Name"} or return 8 0 8
118 8 return $n;
119 }
120  
121 0 ▶sub drop_table {
Subroutine drop_table: not called CC=3 SCAR=24.8
Pod documented
122 0 my ($dbh,$tablename)= @_;
123 0 ▶ defined $dbh and defined $tablename or die('missing args');
Branch: 1 branch
BranchTrueFalseTotal
defined $dbh and defined $tablename or die "missing args" 0 0 0
Condition0%0/3
defined $dbh and defined $tablename
!ll&&!rl&&r
000
MC/DC0%0/2
defined $dbh and defined $tablename
defined $dbh defined $tablename
Truth table0%0/3
A and B
A= defined $dbhB= defined $tablename
ABresult
0X0
100
111
124 0 local $dbh->{PrintError};
125 0 local $dbh->{RaiseError};
126 0 $dbh->do("DROP TABLE IF EXISTS $tablename");
127 0 return 1;
128 }
129  
130 3 ▶sub lid {
Subroutine lid: called CC=5 SCAR=20.3
Pod documented
131 3 my($dbh,$tablename) = @_;
132 3 ▶ defined $dbh and defined $tablename or die('missing args');
Branch: 1 branch
BranchTrueFalseTotal
defined $dbh and defined $tablename or die "missing args" 3 0 3
Condition33%1/3
defined $dbh and defined $tablename
!ll&&!rl&&r
003
MC/DC0%0/2
defined $dbh and defined $tablename
defined $dbh defined $tablename
Truth table33%1/3
A and B
A= defined $dbhB= defined $tablename
ABresult
0X0
100
111
133
134 3 my $id = $dbh->last_insert_id(undef,undef,$tablename,undef);
135
136 3 ▶ defined $id
Branch: 1 branch
BranchTrueFalseTotal
defined $id or warn "last insert id on table $tablename returns undef, does table exists?" and return 3 0 3
Condition20%1/5
defined $id || warn("last insert id on table $tablename returns undef, does table exists?") && (return)
l!l
300
warn "last insert id on table $tablename returns undef, does table exists?" and return
!ll
00
MC/DC0%0/2
defined $id || warn("last insert id on table $tablename returns undef, does table exists?") && (return)
defined $id warn "last insert id on table $tablename returns undef, does table exists?"
Truth table33%1/3
A || B
A= defined $idB= warn "last insert id on table $tablename returns undef, does table exists?"
ABresult
1X1
011
000
137 or warn("last insert id on table $tablename returns undef, does table exists?")
138 and return;
139 3 return $id;
140 }
141  
142 1 ▶sub close_active_handles {
Subroutine close_active_handles: called CC=7 SCAR=20.3
Pod documented
143 1 my $dbh = shift;
144 1 ▶ defined $dbh or die('missing arg');
Branch: 1 branch
BranchTrueFalseTotal
defined $dbh or die "missing arg" 1 0 1
145 1 my $x = 1;
146
147 1 debug("closing active handles:");
148  
149 1 ▶ if ( defined $dbh->{ChildHandles} ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dbh->{"ChildHandles"}) 1 0 1
150
151 1 HANDLE: for (@{$dbh->{ChildHandles}}){
152 5 my $handle = $_;
153 5 ▶ defined $handle or next HANDLE;
Branch: 1 branch
BranchTrueFalseTotal
defined $handle or next HANDLE 2 3 5
154 2 ▶ $handle or next HANDLE;
Branch: 1 branch
BranchTrueFalseTotal
$handle or next HANDLE 2 0 2
155
156  
157 2 ▶ if ($handle->{Active}){
Branch: 1 branch
BranchTrueFalseTotal
if ($handle->{"Active"}) 0 2 2
158 0 $handle->finish;
159 0 $x++;
160 }
161
162 2 undef $handle; # was $_
163 }
164
165 }
166 1 debug("$x, done.\n");
167
168 1 return $x;
169 }
170  
171 4 ▶sub table_exists {
Subroutine table_exists: called CC=5 SCAR=16.4
Pod documented
172 4 my($dbh,$tablename) = @_;
173 4 ▶ defined $dbh or confess('missing dbh');
Branch: 1 branch
BranchTrueFalseTotal
defined $dbh or confess("missing dbh") 4 0 4
174 4 ▶ defined $tablename or confess('missing tablename arg');
Branch: 1 branch
BranchTrueFalseTotal
defined $tablename or confess("missing tablename arg") 4 0 4
175  
176 4 local $dbh->{RaiseError};
177 4 local $dbh->{PrintError};
178  
179 4 ▶ my $t = $dbh->prepare("select * from $tablename") or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $t = $dbh->prepare("select * from $tablename") or return 0 3 1 4
180 3 ▶ $t->execute or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$t->execute or return 0 3 0 3
181 3 return 1;
182 }
183  
184 2 ▶sub table_dump {
Subroutine table_dump: called CC=9 SCAR=27.2
Pod documented
185 2 my ($dbh,$tablename) = @_;
186 2 ▶ defined $dbh and defined $tablename or die('missing args');
Branch: 1 branch
BranchTrueFalseTotal
defined $dbh and defined $tablename or die "missing args" 2 0 2
Condition33%1/3
defined $dbh and defined $tablename
!ll&&!rl&&r
002
MC/DC0%0/2
defined $dbh and defined $tablename
defined $dbh defined $tablename
Truth table33%1/3
A and B
A= defined $dbhB= defined $tablename
ABresult
0X0
100
111
187
188 2 ▶ $dbh->table_exists($tablename)
Branch: 1 branch
BranchTrueFalseTotal
$dbh->table_exists($tablename) or warn "table $tablename does not exist" and return 2 0 2
Condition20%1/5
$dbh->table_exists($tablename) || warn("table $tablename does not exist") && (return)
l!l&&r!l&&!r
200
warn "table $tablename does not exist" and return
!ll
00
MC/DC0%0/2
$dbh->table_exists($tablename) || warn("table $tablename does not exist") && (return)
$dbh->table_exists($tablename) warn "table $tablename does not exist"
Truth table33%1/3
A || B
A= $dbh->table_exists($tablename)B= warn "table $tablename does not exist"
ABresult
1X1
011
000
189 or warn("table $tablename does not exist")
190 and return;
191
192 2 my $dump = " # dump table '$tablename':\n";
193
194 2 my $d = $dbh->selectall_arrayref("SELECT * FROM $tablename");
195  
196 2 ▶ defined $d and scalar @$d or carp("table $tablename had no entries") and return '';
Branch: 1 branch
BranchTrueFalseTotal
defined $d and scalar @$d or carp("table $tablename had no entries") and return "" 2 0 2
Condition28%2/7
defined $d && scalar @$d || carp("table $tablename had no entries") && (return '')
l!l&&r!l&&!r
200
defined $d and scalar @$d
!ll
02
carp("table $tablename had no entries") and return ""
!ll
00
MC/DC0%0/2
defined $d && scalar @$d || carp("table $tablename had no entries") && (return '')
defined $d carp("table $tablename had no entries")
Truth table33%1/3
A || B
A= defined $dB= carp("table $tablename had no entries")
ABresult
1X1
011
000
197  
198  
199
200  
201 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
202 2 for (@$d){
203 5 $dump.= ' ['.join(':',@$_)."]\n";
204 }
205
206 #require Data::Dumper;
207 #my $dump = " # dump table '$tablename':\n".Data::Dumper::Dumper($d);
208  
209 2 return $dump;
210
211 }
212  
213  
214  
215  
216  
217  
218 2 ▶sub selectcol {
Subroutine selectcol: called CC=4 SCAR=14.0
Pod documented
219 2 my ($dbh, $statement) = @_;
220 2 ▶ defined $statement or confess('missing statement arg');
Branch: 1 branch
BranchTrueFalseTotal
defined $statement or confess("missing statement arg") 2 0 2
221
222 2 my $return = [];
223
224 2 ▶ my $q = $dbh->prepare($statement) or confess("prepare [$statement] fails.. ".$dbh->errstr);
Branch: 1 branch
BranchTrueFalseTotal
my $q = $dbh->prepare($statement) or confess("prepare [$statement] fails.. " . $dbh->errstr) 2 0 2
225 2 $q->execute;
226  
227 2 ▶ while(my $row = $q->fetchrow_arrayref ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $q->fetchrow_arrayref) 8 2 10
228 8 push @$return, $row->[0];
229 }
230  
231 2 return $return;
232 }
233  
234  
235  
236  
237  
238  
239  
240  
241 0 ▶sub connect_mysql {
Subroutine connect_mysql: not called CC=7 SCAR=40.3
Pod documented
242 0 my($dbname,$user,$pass,$host)= @_;
243 0 ▶ defined $dbname or confess('missing dbname');
Branch: 1 branch
BranchTrueFalseTotal
defined $dbname or confess("missing dbname") 0 0 0
244 0 ▶ defined $user or confess('missing user');
Branch: 1 branch
BranchTrueFalseTotal
defined $user or confess("missing user") 0 0 0
245 0 ▶ defined $pass or confess('missing pass');
Branch: 1 branch
BranchTrueFalseTotal
defined $pass or confess("missing pass") 0 0 0
246 0 ▶ $host||='localhost';
Condition0%0/2
$host ||= "localhost"
l!l
00
MC/DC0%0/1
$host ||= "localhost"
$host
Truth table0%0/2
A
A= $host
Aresult
11
00
247
248 0 debug("[host:$host,dbname:$dbname,user:$user,pass:$pass]\n");
249 0 require DBI;
250 0 ▶ my $dbh = DBI->connect( "DBI:mysql:database=$dbname;host=$host",$user, $pass )
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect("DBI:mysql:database=$dbname;host=$host", $user, $pass) or carp(" -ERROR=[$DBI::errstr]\n -make sure mysqld is running\n -wrong credentials?[$dbname,$user,$host]") and return 0 0 0
Condition0%0/5
(my $dbh = 'DBI'->connect("DBI:mysql:database=$dbname;host=$host", $user, $pass)) || carp(" -ERROR=[$DBI::errstr]\n -make sure mysqld is running\n -wrong credentials?[$dbname,$user,$host]") && (return)
l!l&&r!l&&!r
000
carp(" -ERROR=[$DBI::errstr]\n -make sure mysqld is running\n -wrong credentials?[$dbname,$user,$host]") and return
!ll
00
MC/DC0%0/2
(my $dbh = 'DBI'->connect("DBI:mysql:database=$dbname;host=$host", $user, $pass)) || carp(" -ERROR=[$DBI::errstr]\n -make sure mysqld is running\n -wrong credentials?[$dbname,$user,$host]") && (return)
(my $dbh = 'DBI'->connect("DBI:mysql:database=$dbname;host=$host", $user, $pass)) carp(" -ERROR=[$DBI::errstr]\n -make sure mysqld is running\n -wrong credentials?[$dbname,$user,$host]")
Truth table0%0/3
A || B
A= (my $dbh = 'DBI'->connect("DBI:mysql:database=$dbname;host=$host", $user, $pass))B= carp(" -ERROR=[$DBI::errstr]\n -make sure mysqld is running\n -wrong credentials?[$dbname,$user,$host]")
ABresult
1X1
011
000
251 or carp(" -ERROR=[$DBI::errstr]\n -make sure mysqld is running\n -wrong credentials?[$dbname,$user,$host]")
252 and return;
253 0 return $dbh;
254 }
255  
256 3 ▶sub connect_sqlite {
Subroutine connect_sqlite: called CC=4 SCAR=15.5
Pod documented
257 3 my $abs_db = shift;
258 3 ▶ defined $abs_db or die;
Branch: 1 branch
BranchTrueFalseTotal
defined $abs_db or die 3 0 3
259 3 debug("abs db [$abs_db]");
260  
261 3 require DBI;
262 3 ▶ my $dbh = DBI->connect( "dbi:SQLite:$abs_db", '', '', )
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = "DBI"->connect("dbi:SQLite:$abs_db", "", "") or carp("$DBI::errstr, cant open sqlite connect. []") and return 3 0 3
Condition25%1/4
(my $dbh = 'DBI'->connect("dbi:SQLite:$abs_db", '', '')) || carp("$DBI::errstr, cant open sqlite connect. []") && (return)
l!l
30
carp("$DBI::errstr, cant open sqlite connect. []") and return
!ll
00
MC/DC0%0/2
(my $dbh = 'DBI'->connect("dbi:SQLite:$abs_db", '', '')) || carp("$DBI::errstr, cant open sqlite connect. []") && (return)
(my $dbh = 'DBI'->connect("dbi:SQLite:$abs_db", '', '')) carp("$DBI::errstr, cant open sqlite connect. []")
Truth table33%1/3
A || B
A= (my $dbh = 'DBI'->connect("dbi:SQLite:$abs_db", '', ''))B= carp("$DBI::errstr, cant open sqlite connect. []")
ABresult
1X1
011
000
263 or carp("$DBI::errstr, cant open sqlite connect. []")
264 and return;
265 3 return $dbh;
266 }
267  
268  
269 3 1;
270  
271 __END__
« Database.pm ↑ Summary DBI.pm »