| 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
Condition33%1/3
$parms{'sql_types'} || $parms{':sql_types'}
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'}
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | #This is per database handle | ||||||||||||||||||||||||||||
| 26 | 3 | ▶ | $MAX_STH = $parms{maxQuery} || 10; | ||||||||||||||||||||||||||
|
Condition50%1/2
$parms{'maxQuery'} || 10
MC/DC0%0/1
$parms{'maxQuery'} || 10
$parms{'maxQuery'}
Truth table50%1/2
A
A= $parms{'maxQuery'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 36 | 0 | ▶ | defined($DBH) ? return $DBH : croak "Not connected to a database"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | if( ref($type) eq 'HASH' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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'
$type->{"ini"} or $ENV{"DBIX_CONN"}
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"}
| |||||||||||||||||||||||||||||
| 45 | @args = ( | ||||||||||||||||||||||||||||
| 46 | $cfg->{user}, | ||||||||||||||||||||||||||||
| 47 | $cfg->{pass}, | ||||||||||||||||||||||||||||
| 48 | 0 | $type->{attr} ? {%{$cfg->{attr}}, %{$type->{attr}}} : $cfg->{attr} | |||||||||||||||||||||||||||
| 49 | 0 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | 0 | $cfg->{dsn} =~ s/^dbi://i; | |||||||||||||||||||||||||||
| 51 | 0 | ▶ | if( $cfg->{dsn} =~ /\?$/ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | croak("Section '$type->{label}' requires a database name") unless | ||||||||||||||||||||||||||||
| 53 | 0 | ▶ | exists($type->{database}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 0 | $cfg->{dsn} =~ s/\?$/$type->{database}/; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 0 | $type = $cfg->{dsn}; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | if ($type =~ /^Pg:(.*)/ && $1 !~ /dbname=/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$type =~ /^Pg:(.*)/ and not $1 =~ /dbname=/
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=/
| |||||||||||||||||||||||||||||
| 59 | 0 | $type = "Pg:dbname=$1"; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | 0 | ▶ | unless ($DBH = DBI->connect("DBI:$type", @args)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $dbh = $_[0] || $DBH; | ||||||||||||||||||||||||||
|
Condition0%0/3
$_[0] || $DBH
MC/DC0%0/2
$_[0] || $DBH
$_[0]
$DBH
Truth table0%0/3
A || B
A= $_[0]B= $DBH
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 92 | 0 | my %hash = %{shift @args}; | |||||||||||||||||||||||||||
| 93 | 0 | my @cols = sort keys %hash; | |||||||||||||||||||||||||||
| 94 | 0 | ▶ | $str .= sprintf('(%s) Values(??L) %s', | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | my $sth = _substitute('Select', $str, \@args); | |||||||||||||||||||||||||||
| 120 | 0 | ▶ | unless ($sth->execute(@args)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 0 | croak "Select failed: $E"; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | my $r; | |||||||||||||||||||||||||||
| 125 | 0 | ▶ | if( wantarray ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$columns =~ /^\*/ or $columns =~ /,/
MC/DC0%0/2
$columns =~ /^\*/ or $columns =~ /,/
$columns =~ /^\*/
$columns =~ /,/
Truth table0%0/3
A or B
A= $columns =~ /^\*/B= $columns =~ /,/
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$res or $finish
MC/DC0%0/2
$res or $finish
$res
$finish
Truth table0%0/3
A or B
A= $resB= $finish
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 158 | 0 | my $caller = caller; | |||||||||||||||||||||||||||
| 159 | 0 | ▶ | unless ($DBH->do(@_)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 168 | 0 | my %hash = %{shift @args}; | |||||||||||||||||||||||||||
| 169 | 0 | my @cols = sort keys %hash; | |||||||||||||||||||||||||||
| 170 | 0 | ▶ | unless($str =~ /\bset\b\s*$/i){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | $str .= ' Set' | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 0 | $str .= join(',', map{" $_=?"} @cols); | |||||||||||||||||||||||||||
| 174 | 0 | ▶ | $str .= defined($args[0]) ? ' ' . shift @args : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 197 | 0 | ▶ | while( <$INI> ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | ▶ | next if /^\s*(?:$|[\#\;])/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 0 | ▶ | if( /^\s*\[$parm{'-label'}\]/ .. | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
/^\s*\[(?!$parm{'-label'})/ || eof $INI
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
| |||||||||||||||||||||||||||||
| 200 | (/^\s*\[(?!$parm{'-label'})/ || eof($INI) ) ){ | ||||||||||||||||||||||||||||
| 201 | 0 | /^\s*([^=]+?)\s*=\s*(.*)$/; | |||||||||||||||||||||||||||
| 202 | 0 | ▶ | $self->{$1} = $2 if $1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 220 | 0 | my($function, $str, $args) = @_; | |||||||||||||||||||||||||||
| 221 | 0 | my $Nargs = scalar @{$args}; | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 0 | ▶ | if( $function eq 'Insert' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | 0 | my $list = join ',' , (('?') x $Nargs); | |||||||||||||||||||||||||||
| 225 | 0 | ▶ | unless( $str =~ s/\?\?L|\(\s*\?\?L\s*\)/($list)/ ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 0 | ▶ | if( $str =~ /\bvalues\b/i ){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 227 | 0 | ▶ | $str .= "($list)" unless $str =~ /\)\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | elsif( $Nargs ){ | ||||||||||||||||||||||||||||
| 230 | 0 | $str .= " values ($list)"; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | 0 | ▶ | carp "$function $str" if $DEBUG; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | 0 | my $subct = $str =~ tr/?/?/; | |||||||||||||||||||||||||||
| 238 | 0 | ▶ | if( $subct > $Nargs ){ | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 262 | #BINDING | ||||||||||||||||||||||||||||
| 263 | 0 | ▶ | foreach(my $i=0; $BINDABLE && $i<$Nargs; $i++){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$EZDBI::BINDABLE and $i < $Nargs
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
| |||||||||||||||||||||||||||||
| 264 | 0 | ▶ | if( ref($args->[$i]) eq 'ARRAY' ){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||