blib/lib/EZDBI.pm

filter: Statementstmt 16.8% 29 / 172 Branchbran 1.0% 1 / 100 Conditioncond 7.1% 2 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 33.3% 6 / 18 Podpod 100% 8 / 8 totaltotal 13.4% 46 / 343 CC 65 SCAR 46.9
CC
65
Cov
11%
_substitute
306.0
Connect
156.0
_parseIni
90.0
CRAP 108.9
↑ Summary
1 package EZDBI;
2 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use vars qw'$E @EXPORT @EXPORT_OK $VERSION $MAX_STH $BINDABLE $DEBUG';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 require 5;
7  
8 3 my $DBH;
9 3 *E = \$DBI::errstr;
10 3 my $sth_cache; # string to statement handle cache
11 my $sth_cacheA; # oldest first (LRU) handle order
12 3 $VERSION = 0.16;
13  
14 # Note that this package does NOT inherit from Exporter
15 3 @EXPORT = qw(Connect Delete Disconnect Insert Select Sql Update Use);
16 3 ▶sub import {
Subroutine import: called CC=5 SCAR=16.7
17 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 my ($package, %parms) = @_;
19 3 my $caller = caller;
20  
21 #Load constants for bindable values
22 DBI->export_to_level(1, undef, ':sql_types')
23 3 ▶ if $BINDABLE = $parms{sql_types} || $parms{':sql_types'};
Branch: 1 branch
BranchTrueFalseTotal
if $EZDBI::BINDABLE = $parms{"sql_types"} || $parms{":sql_types"} 0 3 3
Condition33%1/3
$parms{'sql_types'} || $parms{':sql_types'}
l!l&&r!l&&!r
003
MC/DC0%0/2
$parms{'sql_types'} || $parms{':sql_types'}
$parms{'sql_types'} $parms{':sql_types'}
Truth table33%1/3
A || B
A= $parms{'sql_types'}B= $parms{':sql_types'}
ABresult
1X1
011
000
24  
25 #This is per database handle
26 3 ▶ $MAX_STH = $parms{maxQuery} || 10;
Condition50%1/2
$parms{'maxQuery'} || 10
l!l
03
MC/DC0%0/1
$parms{'maxQuery'} || 10
$parms{'maxQuery'}
Truth table50%1/2
A
A= $parms{'maxQuery'}
Aresult
11
00
27  
28 3 for my $func (@EXPORT) {
29 24 *{"$caller\::$func"} = \&$func;
30 }
31 }
32  
33 0 ▶sub Connect {
Subroutine Connect: not called CC=12 SCAR=50.5
Pod documented
34 0 my ($type, @args) = @_;
35 0 ▶ unless( $type ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 0 0 0
36 0 ▶ defined($DBH) ? return $DBH : croak "Not connected to a database";
Branch: 1 branch
BranchTrueFalseTotal
defined $DBH ? : 0 0 0
37 }
38  
39 0 ▶ if( ref($type) eq 'HASH' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $type eq "HASH") 0 0 0
40 my $cfg = _parseIni(-file=>
41 $type->{ini}||
42 $ENV{'DBIX_CONN'}||
43 $ENV{HOME}.'/.appconfig-dbi',
44 0 ▶ -label=>$type->{label});
Condition0%0/5
$type->{'ini'} || $ENV{'DBIX_CONN'} || $ENV{'HOME'} . '/.appconfig-dbi'
l!l
00
$type->{"ini"} or $ENV{"DBIX_CONN"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$type->{'ini'} || $ENV{'DBIX_CONN'} || $ENV{'HOME'} . '/.appconfig-dbi'
$type->{"ini"} $ENV{"DBIX_CONN"}
Truth table0%0/3
A or B
A= $type->{"ini"}B= $ENV{"DBIX_CONN"}
ABresult
1X1
011
000
45 @args = (
46 $cfg->{user},
47 $cfg->{pass},
48 0 $type->{attr} ? {%{$cfg->{attr}}, %{$type->{attr}}} : $cfg->{attr}
49 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
$type->{'attr'} ? : 0 0 0
50 0 $cfg->{dsn} =~ s/^dbi://i;
51 0 ▶ if( $cfg->{dsn} =~ /\?$/ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($cfg->{"dsn"} =~ /\?$/) 0 0 0
52 croak("Section '$type->{label}' requires a database name") unless
53 0 ▶ exists($type->{database});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $type->{"database"} 0 0 0
54 0 $cfg->{dsn} =~ s/\?$/$type->{database}/;
55 }
56 0 $type = $cfg->{dsn};
57 }
58 0 ▶ if ($type =~ /^Pg:(.*)/ && $1 !~ /dbname=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ /^Pg:(.*)/ and not $1 =~ /dbname=/) 0 0 0
Condition0%0/3
$type =~ /^Pg:(.*)/ and not $1 =~ /dbname=/
!ll&&!rl&&r
000
MC/DC0%0/2
$type =~ /^Pg:(.*)/ and not $1 =~ /dbname=/
$type =~ /^Pg:(.*)/ not $1 =~ /dbname=/
Truth table0%0/3
A and not(B)
A= $type =~ /^Pg:(.*)/B= not $1 =~ /dbname=/
ABresult
0X0
100
111
59 0 $type = "Pg:dbname=$1";
60 }
61 0 ▶ unless ($DBH = DBI->connect("DBI:$type", @args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($DBH = "DBI"->connect("DBI:$type", @args)) 0 0 0
62 0 croak "Couldn't connect to database: $E";
63 }
64 0 $sth_cacheA->{$DBH} = [];
65 0 return $DBH;
66 }
67  
68 0 ▶sub Delete {
Subroutine Delete: not called CC=2 SCAR=17.9
Pod documented
69 0 my ($str, @args) = @_;
70 0 my $sth = _substitute('Delete', $str, \@args);
71 0 my $rc;
72 0 ▶ unless ($rc = $sth->execute(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc = $sth->execute(@args)) 0 0 0
73 0 croak "Delete failed: $E";
74 }
75 0 $sth->finish();
76 0 $rc;
77 }
78  
79 0 ▶sub Disconnect {
Subroutine Disconnect: not called CC=4 SCAR=30.0
Pod documented
80 0 ▶ defined($DBH) || croak "Not connected to a database";
Branch: 1 branch
BranchTrueFalseTotal
defined $DBH or croak("Not connected to a database") 0 0 0
81 0 ▶ my $dbh = $_[0] || $DBH;
Condition0%0/3
$_[0] || $DBH
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0] || $DBH
$_[0] $DBH
Truth table0%0/3
A || B
A= $_[0]B= $DBH
ABresult
1X1
011
000
82 0 delete($_->{$dbh}) for ($sth_cache, $sth_cacheA);
83 0 $DBH->disconnect();
84 0 undef($_[0]);
85 0 undef($DBH);
86 }
87  
88 0 ▶sub Insert {
Subroutine Insert: not called CC=4 SCAR=30.0
Pod documented
89 0 my ($str, @args) = @_;
90  
91 0 ▶ if( ref($args[0]) eq 'HASH' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "HASH") 0 0 0
92 0 my %hash = %{shift @args};
93 0 my @cols = sort keys %hash;
94 0 ▶ $str .= sprintf('(%s) Values(??L) %s',
Branch: 1 branch
BranchTrueFalseTotal
defined $args[1] ? : 0 0 0
95 join(', ', @cols), defined($args[1]) ? $args[1] : '');
96 0 @args = @hash{@cols};
97 }
98  
99 0 my $sth = _substitute('Insert', $str, \@args);
100 0 my $rc;
101 0 ▶ unless ($rc = $sth->execute(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc = $sth->execute(@args)) 0 0 0
102 0 croak "Insert failed: $E";
103 }
104 0 $sth->finish();
105 0 $rc;
106 }
107  
108 # select '* from TABLE WHERE...'
109 # Single column: returns list of scalar in list context
110 # Multi column: returns list of arrayrefs in list context
111 # returns closure/object in scalar context
112 # closure/object returns indvidual records as arrayref or hashref
113 0 ▶sub Select {
Subroutine Select: not called CC=7 SCAR=40.3
Pod documented
114 0 my ($str, @args) = @_;
115 0 my ($columns) = ($str =~ /^\s*(.*\S+)\s+from\s+/i);
116  
117 0 ▶ croak "Select in void context" unless defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 0 0 0
118  
119 0 my $sth = _substitute('Select', $str, \@args);
120 0 ▶ unless ($sth->execute(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->execute(@args)) 0 0 0
121 0 croak "Select failed: $E";
122 }
123  
124 0 my $r;
125 0 ▶ if( wantarray ){
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 0 0
126 0 $r = $sth->fetchall_arrayref;
127 #XXX * on a single column Table? check length of first row?
128 0 ▶ unless( $columns =~ /^\*/ || $columns =~ /,/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($columns =~ /^\*/ or $columns =~ /,/) 0 0 0
Condition0%0/3
$columns =~ /^\*/ or $columns =~ /,/
l!l&&r!l&&!r
000
MC/DC0%0/2
$columns =~ /^\*/ or $columns =~ /,/
$columns =~ /^\*/ $columns =~ /,/
Truth table0%0/3
A or B
A= $columns =~ /^\*/B= $columns =~ /,/
ABresult
1X1
011
000
129 0 $_ = $_->[0] foreach @{$r};
130 }
131 0 $sth->finish();
132 0 return @$r;
133 }
134 0 my $finish;
135 0 ▶ $r = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
136 0 $_ = ref($_[0]);
137 0 ▶ my $res =
Branch: 3 branches
BranchTrueFalseTotal
/HASH/ ? : 0 0 0
/ARRAY/ ? : 0 0 0
/SCALAR/ ? : 0 0 0
138 /HASH/ ? $sth->fetchrow_hashref :
139 /ARRAY/ ? $sth->fetchrow_arrayref :
140 /SCALAR/ ? 0 :
141 croak qq(Select doesn't understand "$_[0]");
142 0 ▶ unless( $res || $finish){
Branch: 1 branch
BranchTrueFalseTotal
unless ($res or $finish) 0 0 0
Condition0%0/3
$res or $finish
l!l&&r!l&&!r
000
MC/DC0%0/2
$res or $finish
$res $finish
Truth table0%0/3
A or B
A= $resB= $finish
ABresult
1X1
011
000
143 0 $sth->finish();
144 0 $finish = 1;
145 0 return 0;
146 }
147 0 };
148 #XXX This object cannot be inherited
149 0 bless $r, 'EZDBI::Select';
150 }
151 0 ▶sub EZDBI::Select::DESTROY{
Subroutine DESTROY: not called CC=1 SCAR=6.9
152 0 $_[0]->(\"_");
153 }
154  
155 # Freeform execution
156 0 ▶sub Sql {
Subroutine Sql: not called CC=3 SCAR=24.8
Pod documented
157 0 ▶ defined($DBH) || croak "Not connected to a database";
Branch: 1 branch
BranchTrueFalseTotal
defined $DBH or croak("Not connected to a database") 0 0 0
158 0 my $caller = caller;
159 0 ▶ unless ($DBH->do(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($DBH->do(@_)) 0 0 0
160 0 croak "Sql failed: $E";
161 }
162 }
163  
164 0 ▶sub Update {
Subroutine Update: not called CC=5 SCAR=34.0
Pod documented
165 0 my ($str, @args) = @_;
166  
167 0 ▶ if( ref($args[0]) eq 'HASH' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args[0] eq "HASH") 0 0 0
168 0 my %hash = %{shift @args};
169 0 my @cols = sort keys %hash;
170 0 ▶ unless($str =~ /\bset\b\s*$/i){
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ /\bset\b\s*$/i) 0 0 0
171 0 $str .= ' Set'
172 }
173 0 $str .= join(',', map{" $_=?"} @cols);
174 0 ▶ $str .= defined($args[0]) ? ' ' . shift @args : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $args[0] ? : 0 0 0
175 0 @args = (@hash{@cols}, @args);
176 }
177  
178 0 my $sth = _substitute('Update', $str, \@args);
179 0 my $rc;
180 0 ▶ unless ($rc = $sth->execute(@args)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rc = $sth->execute(@args)) 0 0 0
181 0 croak "Update failed: $E";
182 }
183 0 $sth->finish();
184 0 $rc;
185 }
186  
187 #Multiple databases, whee!
188 0 ▶sub Use{
Subroutine Use: not called CC=2 SCAR=17.9
Pod documented
189 0 ▶ ref($_[0]) eq 'DBI::db' ? $DBH = $_[0] : croak("Not a DBI handle: $_[0]");
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'DBI::db' ? : 0 0 0
190 }
191  
192 #Private Methods
193 0 ▶sub _parseIni{
Subroutine _parseIni: not called CC=9 SCAR=45.0
194 0 my %parm = @_;
195 0 my $self;
196 0 ▶ open(my $INI, $parm{'-file'}) || croak("$!: $parm{-file}\n");
Branch: 1 branch
BranchTrueFalseTotal
open my $INI, $parm{"-file"} or croak(qq[$!: $parm{"-file"}\n]) 0 0 0
197 0 ▶ while( <$INI> ){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $INI)) 0 0 0
198 0 ▶ next if /^\s*(?:$|[\#\;])/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(?:$|[\#\;])/ 0 0 0
199 0 ▶ if( /^\s*\[$parm{'-label'}\]/ ..
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*\[$parm{"-label"}\]/ .. /^\s*\[(?!$parm{"-label"})/ || eof $INI) 0 0 0
Condition0%0/3
/^\s*\[(?!$parm{'-label'})/ || eof $INI
l!l&&r!l&&!r
000
MC/DC0%0/2
/^\s*\[(?!$parm{'-label'})/ || eof $INI
/^\s*\[(?!$parm{'-label'})/ eof $INI
Truth table0%0/3
A || B
A= /^\s*\[(?!$parm{'-label'})/B= eof $INI
ABresult
1X1
011
000
200 (/^\s*\[(?!$parm{'-label'})/ || eof($INI) ) ){
201 0 /^\s*([^=]+?)\s*=\s*(.*)$/;
202 0 ▶ $self->{$1} = $2 if $1;
Branch: 1 branch
BranchTrueFalseTotal
if $1 0 0 0
203 }
204 }
205 #Handle DBIx::Connect attr construct
206 0 foreach my $key ( grep {/^attr/} keys %{$self} ){
207 0 my $attr = $key;
208 0 $attr =~ s/^attr\s+//i;
209 0 $self->{attr}->{$attr} = delete($self->{$key});
210 }
211  
212 croak("Section [$parm{'-label'}] does not exist in $parm{'-file'}") unless
213 0 ▶ keys %{$self};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$self;} 0 0 0
214 0 return $self;
215 }
216  
217 # given a query string,
218 0 ▶sub _substitute {
Subroutine _substitute: not called CC=17 SCAR=57.2
219 0 ▶ defined($DBH) || croak "Not connected to a database";
Branch: 1 branch
BranchTrueFalseTotal
defined $DBH or croak("Not connected to a database") 0 0 0
220 0 my($function, $str, $args) = @_;
221 0 my $Nargs = scalar @{$args};
222  
223 0 ▶ if( $function eq 'Insert' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($function eq "Insert") 0 0 0
224 0 my $list = join ',' , (('?') x $Nargs);
225 0 ▶ unless( $str =~ s/\?\?L|\(\s*\?\?L\s*\)/($list)/ ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($str =~ s/\?\?L|\(\s*\?\?L\s*\)/($list)/) 0 0 0
226 0 ▶ if( $str =~ /\bvalues\b/i ){
Branch: 2 branches
BranchTrueFalseTotal
if ($str =~ /\bvalues\b/i) { } 0 0 0
elsif ($Nargs) { } 0 0 0
227 0 ▶ $str .= "($list)" unless $str =~ /\)\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\)\s*$/ 0 0 0
228 }
229 elsif( $Nargs ){
230 0 $str .= " values ($list)";
231 }
232 }
233 }
234  
235 0 ▶ carp "$function $str" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $EZDBI::DEBUG 0 0 0
236  
237 0 my $subct = $str =~ tr/?/?/;
238 0 ▶ if( $subct > $Nargs ){
Branch: 2 branches
BranchTrueFalseTotal
if ($subct > $Nargs) { } 0 0 0
elsif ($subct < $Nargs) { } 0 0 0
239 0 croak "Not enough arguments for $function ($subct required)";
240 }
241 elsif( $subct < $Nargs ){
242 0 croak "Too many arguments for $function ($subct required)";
243 }
244  
245 0 my $sth;
246 # was the statement handle cached already?
247 0 ▶ if( $sth = $sth_cache->{$DBH}->{$str} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($sth = $sth_cache->{$DBH}{$str}) { } 0 0 0
248 # remove it from the MRU queue (if it is there) and add it to the end
249 0 ▶ unless( $sth_cacheA->{$DBH}->[-1] eq $str ){
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth_cacheA->{$DBH}[-1] eq $str) 0 0 0
250 0 $sth_cacheA->{$DBH} = [grep($_ ne $str, @{$sth_cacheA->{$DBH}}), $str];
251 }
252 }
253 else{
254 # expire old cache items if cache is full
255 0 ▶ if( scalar @{$sth_cacheA->{$DBH}} >= $MAX_STH -1 ){
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$sth_cacheA->{$DBH};} >= $EZDBI::MAX_STH - 1) 0 0 0
256 0 delete(@{$sth_cache->{$DBH}}{splice(@{$sth_cacheA->{$DBH}},0,$MAX_STH/3)});
257 }
258  
259 # prepare new handle
260 0 $sth = $DBH->prepare("$function $str");
261 0 ▶ croak "Couldn't prepare query for '$function $str': $E; aborting" unless $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 0 0
262 #BINDING
263 0 ▶ foreach(my $i=0; $BINDABLE && $i<$Nargs; $i++){
Branch: 1 branch
BranchTrueFalseTotal
if ($EZDBI::BINDABLE and $i < $Nargs) 0 0 0
Condition0%0/3
$EZDBI::BINDABLE and $i < $Nargs
!ll&&!rl&&r
000
MC/DC0%0/2
$EZDBI::BINDABLE and $i < $Nargs
$EZDBI::BINDABLE $i < $Nargs
Truth table0%0/3
A and B
A= $EZDBI::BINDABLEB= $i < $Nargs
ABresult
0X0
100
111
264 0 ▶ if( ref($args->[$i]) eq 'ARRAY' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->[$i] eq "ARRAY") 0 0 0
265 0 $sth->bind_param($i+1, undef, $args->[$i]->[1]);
266 0 $args->[$i] = $args->[$i]->[0];
267 }
268 }
269  
270 # install new handle in cache
271 0 $sth_cache->{$DBH}->{$str} = $sth;
272 0 push(@{$sth_cacheA->{$DBH}}, $str);
273 }
274 0 return $sth;
275 }
276  
277 3 1;
278 __END__
↑ Summary