| 1 | package DBD::Log; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # hartog/20041208 - 0.10 - created | ||||||||||||||||||||||||||||
| 4 | # hartog/20050114 - 0.20 - made ready for release | ||||||||||||||||||||||||||||
| 5 | # hartog/20050504 - 0.21 - tests added, packaged. | ||||||||||||||||||||||||||||
| 6 | # hartog/20050524 - 0.22 - warnings prevented, loglines altered. | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 3 | $DBD::Log::VERSION = "0.22"; | |||||||||||||||||||||||||||
| 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 Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use DBD::Log::Sth; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 3 | my %sthCache = (); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use Class::AccessorMaker { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 3 | logThis => [], | |||||||||||||||||||||||||||
| 22 | logFH => "", | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | dbiLogging => 0, | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | dbi => "", | ||||||||||||||||||||||||||||
| 27 | 3 | }, "new_init"; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 3 | ▶ | sub init { | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=3 SCAR=12.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | $self->logThis([ qw(insert update delete select create drop) ]) | ||||||||||||||||||||||||||||
| 33 | 3 | ▶ | if !@{$self->logThis}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 3 | ▶ | if ( !$self->logFH ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 0 | croak("DBD::Log: Need an IO::File object to log into"); | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | sub logStatement { | ||||||||||||||||||||||||||
|
Subroutine
logStatement: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 0 | my ( $self, $statement, @rest ) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # all references are not to be logged. | ||||||||||||||||||||||||||||
| 44 | 0 | @rest = grep { !ref($_) } @rest; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # should we even log this? | ||||||||||||||||||||||||||||
| 47 | 0 | my ($command) = lc($statement) =~ /^(\w+)/; | |||||||||||||||||||||||||||
| 48 | 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;})
| |||||||||||||||||||||||||||||
| 49 | 0 | && !grep { $_ eq $command } @{$self->logThis} | |||||||||||||||||||||||||||
| 50 | ) { | ||||||||||||||||||||||||||||
| 51 | 0 | return undef; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | if ( my ( $fullSQL, @sqlRest ) = $self->composeStatement($statement, @rest) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | 0 | $self->printLog($fullSQL, @sqlRest); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | } else { | ||||||||||||||||||||||||||||
| 58 | # we couldn't compile the statement. | ||||||||||||||||||||||||||||
| 59 | 0 | $self->printLog('s', $statement, @rest); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 4 | ▶ | sub logAction { | ||||||||||||||||||||||||||
|
Subroutine
logAction: called CC=4 SCAR=15.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 64 | 4 | my ( $function, $self, $statement, @rest ) = @_; | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # do we log DBI actions? | ||||||||||||||||||||||||||||
| 67 | 4 | ▶ | return undef if !$self->dbiLogging; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | # do we log this statement? | ||||||||||||||||||||||||||||
| 70 | 4 | my ($command) = lc($statement) =~ /^(\w+)/; | |||||||||||||||||||||||||||
| 71 | 4 | ▶ | 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;})
| |||||||||||||||||||||||||||||
| 72 | 24 | && !grep { $_ eq $command } @{$self->logThis} | |||||||||||||||||||||||||||
| 73 | ) { | ||||||||||||||||||||||||||||
| 74 | 0 | return undef; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 4 | $self->printLog("[$function]", $statement, @rest); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub composeStatement { | ||||||||||||||||||||||||||
|
Subroutine
composeStatement: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 81 | 0 | my ( $self, $statement, @rest ) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # can we complete the statement with the values? | ||||||||||||||||||||||||||||
| 84 | 0 | ▶ | if ( my @parts = split(/\?/, $statement) ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 85 | # ? replacement. | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | for ( 0..$#parts ) { | |||||||||||||||||||||||||||
| 88 | # add quotes if not fully numeric. | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | $rest[$_] = "'$rest[$_]'" if $rest[$_] =~ /\D+/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # insert the value into the statement. | ||||||||||||||||||||||||||||
| 92 | 0 | $parts[$_] .= $rest[$_]; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | # make completed SQL | ||||||||||||||||||||||||||||
| 96 | 0 | $statement = join("", @parts); | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # if there is more to @rest then to @parts make sure to print it. | ||||||||||||||||||||||||||||
| 99 | 0 | @rest = splice(@rest, $#parts+1, $#rest); | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | return ( $statement, @rest ); | |||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | } elsif ( $statement =~ /\:\w+/ ) { | ||||||||||||||||||||||||||||
| 104 | # oracle style replacement | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | return undef; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 6 | ▶ | sub printLog { | ||||||||||||||||||||||||||
|
Subroutine
printLog: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 112 | 6 | my ( $self, @components ) = @_; | |||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | # print fast and add newlines. | ||||||||||||||||||||||||||||
| 115 | 6 | local $\ = "\n"; | |||||||||||||||||||||||||||
| 116 | 6 | local $| = 1; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 6 | my $fh; | |||||||||||||||||||||||||||
| 119 | 6 | ▶ | unless ( $fh = $self->logFH ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | warn "No FH to log to! Using STDERR"; | |||||||||||||||||||||||||||
| 121 | 0 | open($fh, ">&STDERR") | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | print $fh join("\t", time, map { | ||||||||||||||||||||||||||||
| 125 | # replace new-lines | ||||||||||||||||||||||||||||
| 126 | 6 | s/[\r\n]+//g; | |||||||||||||||||||||||||||
| 127 | # replace tabs. | ||||||||||||||||||||||||||||
| 128 | 10 | s/\t/ /g; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 10 | $_ | |||||||||||||||||||||||||||
| 131 | } @components); | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 4 | ▶ | sub prepare { | ||||||||||||||||||||||||||
|
Subroutine
prepare: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 135 | 4 | my ( $self, $statement, @rest ) = @_; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # prepare is somewhat special - we want to setup a fake $sth. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 4 | ▶ | my $action = | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
[caller 1]->[3] and [caller 1]->[3] =~ /prepare_cached/
MC/DC0%0/2
[caller 1]->[3] and [caller 1]->[3] =~ /prepare_cached/
[caller 1]->[3]
[caller 1]->[3] =~ /prepare_cached/
Truth table33%1/3
A and B
A= [caller 1]->[3]B= [caller 1]->[3] =~ /prepare_cached/
| |||||||||||||||||||||||||||||
| 140 | [caller(1)]->[3] && [caller(1)]->[3] =~ /prepare_cached/ ? "prepare_cached" : "prepare"; | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 4 | logAction($action, @_); | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 4 | my $sth = DBD::Log::Sth->new( dbi => $self->dbi, | |||||||||||||||||||||||||||
| 145 | logFH => $self->logFH, | ||||||||||||||||||||||||||||
| 146 | logThis => $self->logThis, | ||||||||||||||||||||||||||||
| 147 | dbiLogging => $self->dbiLogging, | ||||||||||||||||||||||||||||
| 148 | statement => $statement, | ||||||||||||||||||||||||||||
| 149 | rest => [ @rest ], | ||||||||||||||||||||||||||||
| 150 | ); | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 4 | return $sth; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub prepare_cached { | ||||||||||||||||||||||||||
|
Subroutine
prepare_cached: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 157 | 0 | my ( $self, $statement, @rest ) = @_; | |||||||||||||||||||||||||||
| 158 | 0 | my $KEY = $statement . $rest[0]; | |||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | # let's try to do this caching stuff our selves. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | # prevent warnings. | ||||||||||||||||||||||||||||
| 163 | 0 | ▶ | exists $sthCache{$self} || ( $sthCache{$self} = {} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # return cached STH | ||||||||||||||||||||||||||||
| 166 | 0 | ▶ | exists $sthCache{$self}->{$KEY} && return $sthCache{$self}->{$KEY}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 0 | my $sth = $self->prepare($statement, @rest); | |||||||||||||||||||||||||||
| 169 | 0 | $sthCache{$self}->{$KEY} = $sth; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | return $sth; | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | # define the actions that need to be logged. | ||||||||||||||||||||||||||||
| 175 | 3 | foreach my $sub ( qw( do selectall_arrayref selectcol_arrayref | |||||||||||||||||||||||||||
| 176 | selectrow_array selectrow_arrayref | ||||||||||||||||||||||||||||
| 177 | selectrow_hashref ) | ||||||||||||||||||||||||||||
| 178 | ) { | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 18 | ▶ | *{"DBD::Log::$sub"} = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=4.1
| |||||||||||||||||||||||||||||
| 181 | 0 | my ( $self, $statement, @rest ) = @_; | |||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | logAction($sub, @_); | |||||||||||||||||||||||||||
| 184 | 0 | $self->logStatement($statement, @rest); | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | return $self->dbi->$sub($statement, @rest); | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 18 | } | |||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 3 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=7.1
| |||||||||||||||||||||||||||||
| 192 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | # make all cached sth's done. | ||||||||||||||||||||||||||||
| 195 | 3 | foreach ( keys %{$sthCache{$self}} ) { | |||||||||||||||||||||||||||
| 196 | 0 | $sthCache{$self}->{$_}->destroy; | |||||||||||||||||||||||||||
| 197 | 0 | $sthCache{$self}->{$_}->DESTROY; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | # clear the cache. | ||||||||||||||||||||||||||||
| 201 | 3 | %sthCache = (); | |||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 3 | $self->dbi->disconnect; | |||||||||||||||||||||||||||
| 204 | 3 | $self->dbi({}); | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | 3 | $self = undef; | |||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | sub AUTOLOAD { | ||||||||||||||||||||||||||
|
Subroutine
AUTOLOAD: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # any of the DBI routines we missed, or want not logged, are | ||||||||||||||||||||||||||||
| 212 | # autoloaded. | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 3 | ▶ | no strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | my ($routine) = $AUTOLOAD =~ /\:\:(\w+)$/; | |||||||||||||||||||||||||||
| 217 | 0 | my ( $self, @rest ) = @_; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 0 | return $self->dbi->$routine(@rest); | |||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 3 | 1; | |||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | __END__ | ||||||||||||||||||||||||||||