| 1 | package DBD::Log::Sth; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # hartog/20041208 | ||||||||||||||||||||||||||||
| 4 | # hartog/20050525 - 0.11 - backtracing added | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use base 'DBD::Log'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | $DBD::Log::Sth::VERSION = "0.11"; | |||||||||||||||||||||||||||
| 10 | } | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Class::AccessorMaker { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | dbi => "", | |||||||||||||||||||||||||||
| 17 | sth => "", | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | statement => "", | ||||||||||||||||||||||||||||
| 20 | rest => [], | ||||||||||||||||||||||||||||
| 21 | bound => [], | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | logFH => "", | ||||||||||||||||||||||||||||
| 24 | logThis => [], | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | dbiLogging => 0, | ||||||||||||||||||||||||||||
| 27 | fullLogging => 0, | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | }, "new_init"; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 3 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 4 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 34 | 4 | my ( $self, $command, @rest ) = @_; | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | $self->sth( $self->dbi->prepare( $self->statement, @{$self->rest}) ); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | sub logCall { | ||||||||||||||||||||||||||
|
Subroutine
logCall: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 0 | my ( $function, $self, @rest ) = @_; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | # are we logging this? | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | return undef if !$self->dbiLogging; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 0 | my ($command) = lc($self->statement) =~ /^(\w+)/; | |||||||||||||||||||||||||||
| 46 | 0 | ▶ | if ( $self->logThis->[0] ne "all" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->logThis->[0] ne "all" and not grep({$_ eq $command;} @{$self->logThis;})
MC/DC0%0/2
$self->logThis->[0] ne "all" and not grep({$_ eq $command;} @{$self->logThis;})
$self->logThis->[0] ne "all"
not grep({$_ eq $command;} @{$self->logThis;})
Truth table0%0/3
A and not(B)
A= $self->logThis->[0] ne "all"B= not grep({$_ eq $command;} @{$self->logThis;})
| |||||||||||||||||||||||||||||
| 47 | 0 | && !grep { $_ eq $command } @{$self->logThis} | |||||||||||||||||||||||||||
| 48 | ) { | ||||||||||||||||||||||||||||
| 49 | 0 | return undef; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | $self->printLog("[$function]", $self->statement, @rest); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | sub logAction { | ||||||||||||||||||||||||||
|
Subroutine
logAction: called CC=8 SCAR=29.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 56 | 2 | my ( $function, $self, @rest ) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # define logging | ||||||||||||||||||||||||||||
| 59 | 2 | ▶ | @rest = () if !$self->fullLogging; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 2 | my ($command) = lc($self->statement) =~ /^(\w+)/; | |||||||||||||||||||||||||||
| 62 | 2 | ▶ | if ( $self->logThis->[0] ne "all" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->logThis->[0] ne "all" and not grep({$_ eq $command;} @{$self->logThis;})
MC/DC0%0/2
$self->logThis->[0] ne "all" and not grep({$_ eq $command;} @{$self->logThis;})
$self->logThis->[0] ne "all"
not grep({$_ eq $command;} @{$self->logThis;})
Truth table33%1/3
A and not(B)
A= $self->logThis->[0] ne "all"B= not grep({$_ eq $command;} @{$self->logThis;})
| |||||||||||||||||||||||||||||
| 63 | 12 | && !grep { $_ eq $command } @{$self->logThis} | |||||||||||||||||||||||||||
| 64 | ) { | ||||||||||||||||||||||||||||
| 65 | 0 | return undef; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 2 | ▶ | if ( $function eq "execute" ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 69 | 2 | $self->printLog( $self->composeStatement(@{$self->bound}), @rest ); | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | } elsif ( $function eq "execute_array" ) { | ||||||||||||||||||||||||||||
| 72 | 0 | ▶ | if ( ref($self->bound->[0]) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 0 | foreach my $bound ( @{$self->bound} ) { | |||||||||||||||||||||||||||
| 74 | 0 | my @print = $self->composeStatement(@$bound); | |||||||||||||||||||||||||||
| 75 | 0 | $self->printLog( @print, @rest ); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | } else { | ||||||||||||||||||||||||||||
| 79 | 0 | $self->printLog( $self->composeStatement(@{$self->bound}), @rest ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | ▶ | sub composeStatement { | ||||||||||||||||||||||||||
|
Subroutine
composeStatement: called CC=7 SCAR=22.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 86 | 2 | my ( $self, @bound ) = @_; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 2 | my $statement = $self->statement; | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 2 | ▶ | if ( $statement =~ /\?/ ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 91 | 2 | my @parts = split(/\?/, $statement); | |||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | 2 | for ( 0..$#parts ) { | |||||||||||||||||||||||||||
| 94 | # skip the parts that are not bound. | ||||||||||||||||||||||||||||
| 95 | 2 | ▶ | next if !defined $bound[$_]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # if the bound value is NaN, wrap it in quotes. | ||||||||||||||||||||||||||||
| 98 | 2 | my $val = $bound[$_]; | |||||||||||||||||||||||||||
| 99 | 2 | ▶ | $val =~ /\D+/ && ( $val = "'$val'" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 2 | $parts[$_] .= $val; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | $statement = join("", @parts); | |||||||||||||||||||||||||||
| 105 | 2 | ▶ | if ( ($#parts+1) < $#bound ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | 0 | @bound = splice(@bound, $#parts+1, $#bound); | |||||||||||||||||||||||||||
| 107 | } else { | ||||||||||||||||||||||||||||
| 108 | 2 | @bound = (); | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | } elsif ( $statement =~ /\:\w+/ ) { | ||||||||||||||||||||||||||||
| 112 | # oracle style replacement | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 0 | $statement =~ s/(\:\w+)/&oracleSubstitute($1, \@bound)/eg; | |||||||||||||||||||||||||||
| 115 | 0 | @bound = (); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 2 | return $statement, @bound | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub oracleSubstitute{ | ||||||||||||||||||||||||||
|
Subroutine
oracleSubstitute: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 122 | 0 | my ( $subst, $bound ) = @_; | |||||||||||||||||||||||||||
| 123 | 0 | my $var = ""; | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | my @list = grep { $_->[0] eq $subst } @$bound; | |||||||||||||||||||||||||||
| 126 | 0 | ▶ | @list && ( $var = $list[0]->[1] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | ref($var) =~ /scalar/i && ( $var = $$var ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 129 | 0 | ▶ | $var =~ /\D+/ && ( $var = "'$var'" ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | $var ||= "''"; | ||||||||||||||||||||||||||
|
Condition0%0/2
$var ||= "''"
MC/DC0%0/1
$var ||= "''"
$var
Truth table0%0/2
A
A= $var
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | return $var; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | ## make multiple routines | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # logging actions | ||||||||||||||||||||||||||||
| 138 | 3 | foreach my $sub ( qw( execute bind_param execute_array bind_param_array bind_param_inout ) ) { | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | 15 | ▶ | *{"DBD::Log::Sth::$sub"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=15 SCAR=32.7
| |||||||||||||||||||||||||||||
| 141 | 2 | my ( $self, @rest ) = @_; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 2 | my @bound = @{$self->bound}; | |||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 2 | ▶ | if ( $#rest >= 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 2 | ▶ | if ( $sub eq "execute" ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 148 | # bind litteral | ||||||||||||||||||||||||||||
| 149 | 2 | @bound = @rest; | |||||||||||||||||||||||||||
| 150 | } elsif ( $sub eq "execute_array" ) { | ||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ( $#rest >= 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 152 | # bind the array | ||||||||||||||||||||||||||||
| 153 | 0 | @bound = @rest[1..$#rest]; | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | ▶ | } elsif ( $#rest >= 1 && $rest[0] =~ /\D+/ ) { | |||||||||||||||||||||||||||
|
Condition0%0/3
$#rest >= 1 and $rest[0] =~ /\D+/
MC/DC0%0/2
$#rest >= 1 and $rest[0] =~ /\D+/
$#rest >= 1
$rest[0] =~ /\D+/
Truth table0%0/3
A and B
A= $#rest >= 1B= $rest[0] =~ /\D+/
| |||||||||||||||||||||||||||||
| 157 | # oracle style binding | ||||||||||||||||||||||||||||
| 158 | # rest[0] = :key | ||||||||||||||||||||||||||||
| 159 | # rest[1] = value | ||||||||||||||||||||||||||||
| 160 | 0 | push @bound, [@rest]; | |||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | } else { | ||||||||||||||||||||||||||||
| 163 | # rest[0] = index (start at 1). | ||||||||||||||||||||||||||||
| 164 | # rest[1] = value. | ||||||||||||||||||||||||||||
| 165 | 0 | $bound[$rest[0]-1] = $rest[1]; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 2 | $self->bound( [ @bound ] ); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 2 | ▶ | logAction($sub, $self, @bound) if $sub =~ /execute/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 2 | ▶ | logCall($sub, @_) if $sub !~ /execute/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | 2 | my $res = $self->sth->$sub(@rest); | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 2 | ▶ | if ( my $error = ( $self->dbi->errstr || $self->sth->errstr ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$self->dbi->errstr || $self->sth->errstr
MC/DC50%1/2
$self->dbi->errstr || $self->sth->errstr
$self->dbi->errstr
$self->sth->errstr
Truth table66%2/3
A || B
A= $self->dbi->errstrB= $self->sth->errstr
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 1 | my @backtrace; | |||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # walk through the backtrace trying to find the error. | ||||||||||||||||||||||||||||
| 183 | 1 | for ( 0..5 ) { | |||||||||||||||||||||||||||
| 184 | 2 | my ( $package, $filename, $line, @xtra ) = caller($_); | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 2 | ▶ | last if !caller($_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 1 | ▶ | if ( $package =~ /dbd/i ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 189 | # this is me - ignore. | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | } elsif ( $package =~ /dbi/i ) { | ||||||||||||||||||||||||||||
| 192 | # this is the dbi - ignore | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | } else { | ||||||||||||||||||||||||||||
| 195 | 1 | $self->dbi->{dbd_log_error} = "$error in $filename at line $line\n"; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | unshift @backtrace, ( "$xtra[0](" . | ||||||||||||||||||||||||||||
| 200 | 1 | join(", ", @{$xtra[1]}) . | |||||||||||||||||||||||||||
| 201 | ") at $filename line $line." | ||||||||||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 1 | $self->dbi->{dbd_log_backtrace} = join("\n", @backtrace); | |||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 2 | return $res; | |||||||||||||||||||||||||||
| 209 | 15 | }; | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | # non-logging actions | ||||||||||||||||||||||||||||
| 214 | 3 | foreach my $sub ( qw( bind_col bind_columns fetchrow_array fetchrow_arrayref | |||||||||||||||||||||||||||
| 215 | fetchall_arrayref fetchrow_hashref fetchall_hashref | ||||||||||||||||||||||||||||
| 216 | rows ) | ||||||||||||||||||||||||||||
| 217 | ) { | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 24 | ▶ | *{"DBD::Log::Sth::$sub"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||
| 220 | 0 | my ( $self, @rest ) = @_; | |||||||||||||||||||||||||||
| 221 | 0 | return $self->sth->$sub(@rest); | |||||||||||||||||||||||||||
| 222 | 24 | }; | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 4 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 227 | # kill the object and return the real sth. | ||||||||||||||||||||||||||||
| 228 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 229 | 4 | $self->dbi(""); | |||||||||||||||||||||||||||
| 230 | 4 | $self->sth(""); | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | # any of the DBI routines we missed, or want not logged, are | ||||||||||||||||||||||||||||
| 236 | # autoloaded. | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 3 | ▶ | no strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 0 | my ($routine) = $AUTOLOAD =~ /\:\:(\w+)$/; | |||||||||||||||||||||||||||
| 241 | 0 | my ( $self, @rest ) = @_; | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 0 | return $self->sth->$routine(@rest); | |||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 3 | 1; | |||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | __END__ | ||||||||||||||||||||||||||||