Log4perl/Appender/DBI.pm

filter: Statementstmt 80.1% 105 / 131 Branchbran 43.9% 29 / 66 Conditioncond 46.4% 13 / 28 MC/DCmcdc 11.1% 2 / 18 Subroutinesub 100% 12 / 12 Podpod 16.6% 1 / 6 totaltotal 62.0% 162 / 261 CC 48 SCAR 33.4
CC
48
Cov
65%
query_execute
101.6
calculate_bind_values
18.6
new
9.6
CRAP 28.2
« Log4perl.pm ↑ Summary Log4perl/Appender/Socket.pm »
1 package Log::Log4perl::Appender::DBI;
2  
3 3 our @ISA = qw(Log::Log4perl::Appender);
4  
5 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶sub new {
Subroutine new: called CC=9 SCAR=22.6
Pod documented
11 5 my($proto, %p) = @_;
12 5 ▶ my $class = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
050
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
13  
14 5 my $self = bless {}, $class;
15  
16 5 $self->_init(%p);
17  
18 5 my %defaults = (
19 reconnect_attempts => 1,
20 reconnect_sleep => 0,
21 );
22  
23 5 for (keys %defaults) {
24 10 ▶ if(exists $p{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p{$_}) { } 0 10 10
25 0 $self->{$_} = $p{$_};
26 } else {
27 10 $self->{$_} = $defaults{$_};
28 }
29 }
30  
31 #e.g.
32 #log4j.appender.DBAppndr.params.1 = %p
33 #log4j.appender.DBAppndr.params.2 = %5.5m
34 5 foreach my $pnum (keys %{$p{params}}){
35 $self->{bind_value_layouts}{$pnum} =
36 Log::Log4perl::Layout::PatternLayout->new({
37 13 ConversionPattern => {value => $p{params}->{$pnum}},
38 undef_column_value => undef,
39 });
40 }
41 #'bind_value_layouts' now contains a PatternLayout
42 #for each parameter heading for the Sql engine
43  
44 5 $self->{SQL} = $p{sql}; #save for error msg later on
45  
46 5 $self->{MAX_COL_SIZE} = $p{max_col_size};
47  
48 5 ▶ $self->{BUFFERSIZE} = $p{bufferSize} || 1;
Condition100%2/2
$p{'bufferSize'} || 1
l!l
14
MC/DC100%1/1
$p{'bufferSize'} || 1
$p{'bufferSize'}
Truth table100%2/2
A
A= $p{'bufferSize'}
Aresult
11
00
49  
50 5 ▶ if ($p{usePreparedStmt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p{'usePreparedStmt'}) { } 3 2 5
51 3 $self->{sth} = $self->create_statement($p{sql});
52 3 $self->{usePreparedStmt} = 1;
53 }else{
54 $self->{layout} = Log::Log4perl::Layout::PatternLayout->new({
55 ConversionPattern => {value => $p{sql}},
56 2 undef_column_value => undef,
57 });
58 }
59  
60 5 ▶ if ($self->{usePreparedStmt} && $self->{bufferSize}){
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"usePreparedStmt"} and $self->{"bufferSize"}) 0 5 5
Condition66%2/3
$self->{"usePreparedStmt"} and $self->{"bufferSize"}
!ll&&!rl&&r
230
MC/DC0%0/2
$self->{"usePreparedStmt"} and $self->{"bufferSize"}
$self->{"usePreparedStmt"} $self->{"bufferSize"}
Truth table66%2/3
A and B
A= $self->{"usePreparedStmt"}B= $self->{"bufferSize"}
ABresult
0X0
100
111
61 0 warn "Log4perl: you've defined both usePreparedStmt and bufferSize \n".
62 "in your appender '$p{name}'--\n".
63 "I'm going to ignore bufferSize and just use a prepared stmt\n";
64 }
65  
66 5 return $self;
67 }
68  
69  
70 5 ▶sub _init {
Subroutine _init: called CC=2 SCAR=7.6
71 5 my $self = shift;
72 5 my %params = @_;
73  
74 5 ▶ if ($params{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'dbh'}) { } 0 5 5
75 0 $self->{dbh} = $params{dbh};
76 } else {
77 5 ▶ $self->{connect} = sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
78 DBI->connect(@params{qw(datasource username password)},
79 0 ▶ {PrintError => 0, $params{attrs} ? %{$params{attrs}} : ()})
Branch: 2 branches
BranchTrueFalseTotal
"DBI"->connect(@params{"datasource", "username", "password"}, {"PrintError", 0, $params{"attrs"} ? %{$params{"attrs"};} : ()}) or croak("Log4perl: $DBI::errstr") 5 0 5
$params{'attrs'} ? : 0 5 5
80 or croak "Log4perl: $DBI::errstr";
81 5 };
82 5 $self->{dbh} = $self->{connect}->();
83 5 $self->{_mine} = 1;
84 }
85 }
86  
87 7 ▶sub create_statement {
Subroutine create_statement: called CC=3 SCAR=11.7
Pod undocumented
88 7 my ($self, $stmt) = @_;
89  
90 7 ▶ $stmt || croak "Log4perl: sql not set in Log4perl::Appender::DBI";
Branch: 1 branch
BranchTrueFalseTotal
$stmt or croak("Log4perl: sql not set in Log4perl::Appender::DBI") 7 0 7
91  
92 7 ▶ return $self->{dbh}->prepare($stmt) || croak "Log4perl: DBI->prepare failed $DBI::errstr\n$stmt";
Condition50%1/2
$self->{'dbh'}->prepare($stmt) || croak("Log4perl: DBI->prepare failed $DBI::errstr\n$stmt")
l!l
70
MC/DC0%0/1
$self->{'dbh'}->prepare($stmt) || croak("Log4perl: DBI->prepare failed $DBI::errstr\n$stmt")
$self->{'dbh'}->prepare($stmt)
Truth table50%1/2
A
A= $self->{'dbh'}->prepare($stmt)
Aresult
11
00
93  
94 }
95  
96  
97 10 ▶sub log {
Subroutine log: called CC=3 SCAR=11.0
Pod undocumented
98 10 my $self = shift;
99 10 my %p = @_;
100  
101 #%p is
102 # { name => $appender_name,
103 # level => loglevel
104 # message => $message,
105 # log4p_category => $category,
106 # log4p_level => $level,);
107 # },
108  
109 #getting log4j behavior with no specified ConversionPattern
110 10 ▶ chomp $p{message} unless ref $p{message};
Branch: 1 branch
BranchTrueFalseTotal
unless ref $p{"message"} 0 10 10
111  
112
113 10 my $qmarks = $self->calculate_bind_values(\%p);
114  
115  
116 10 ▶ if ($self->{usePreparedStmt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'usePreparedStmt'}) { } 4 6 10
117  
118 4 $self->query_execute($self->{sth}, @$qmarks);
119  
120 }else{
121  
122 #first expand any %x's in the statement
123 my $stmt = $self->{layout}->render(
124 $p{message},
125 $p{log4p_category},
126 $p{log4p_level},
127 6 5 + $Log::Log4perl::caller_depth,
128 );
129  
130 6 push @{$self->{BUFFER}}, $stmt, $qmarks;
131  
132 6 $self->check_buffer();
133 }
134 }
135  
136 10 ▶sub query_execute {
Subroutine query_execute: called CC=12 SCAR=46.2
Pod undocumented
137 10 my($self, $sth, @qmarks) = @_;
138  
139 10 my $errstr = "[no error]";
140  
141 10 for my $attempt (0..$self->{reconnect_attempts}) {
142 #warn "Exe: @qmarks"; # TODO
143 10 ▶ if(! $sth->execute(@qmarks)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->execute(@qmarks)) 0 10 10
144  
145 # save errstr because ping() would override it [RT 56145]
146 0 $errstr = $self->{dbh}->errstr();
147  
148 # Exe failed -- was it because we lost the DB
149 # connection?
150 0 ▶ if($self->{dbh}->ping()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"dbh"}->ping) 0 0 0
151 # No, the connection is ok, we failed because there's
152 # something wrong with the execute(): Bad SQL or
153 # missing parameters or some such). Abort.
154 0 croak "Log4perl: DBI appender error: '$errstr'";
155 }
156  
157 0 ▶ if($attempt == $self->{reconnect_attempts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt == $self->{"reconnect_attempts"}) 0 0 0
158 croak "Log4perl: DBI appender failed to " .
159 ($self->{reconnect_attempts} == 1 ? "" : "re") .
160 "connect " .
161 "to database after " .
162 "$self->{reconnect_attempts} attempt" .
163 0 ▶ ($self->{reconnect_attempts} == 1 ? "" : "s") .
Branch: 2 branches
BranchTrueFalseTotal
$self->{'reconnect_attempts'} == 1 ? : 0 0 0
$self->{'reconnect_attempts'} == 1 ? : 0 0 0
164 " (last error error was [$errstr]";
165 }
166 0 ▶ if(! $self->{dbh}->ping()) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"dbh"}->ping) 0 0 0
167 # Ping failed, try to reconnect
168 0 ▶ if($attempt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attempt) 0 0 0
169 #warn "Sleeping"; # TODO
170 0 ▶ sleep($self->{reconnect_sleep}) if $self->{reconnect_sleep};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"reconnect_sleep"} 0 0 0
171 }
172  
173 0 eval {
174 #warn "Reconnecting to DB"; # TODO
175 0 $self->{dbh} = $self->{connect}->();
176 };
177 }
178  
179 0 ▶ if ($self->{usePreparedStmt}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'usePreparedStmt'}) { } 0 0 0
180 0 $sth = $self->create_statement($self->{SQL});
181 0 ▶ $self->{sth} = $sth if $self->{sth};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"sth"} 0 0 0
182 } else {
183 #warn "Pending stmt: $self->{pending_stmt}"; #TODO
184 0 $sth = $self->create_statement($self->{pending_stmt});
185 }
186  
187 0 next;
188 }
189 10 return 1;
190 }
191 0 croak "Log4perl: DBI->execute failed $errstr, \n".
192 "on $self->{SQL}\n @qmarks";
193 }
194  
195 10 ▶sub calculate_bind_values {
Subroutine calculate_bind_values: called CC=12 SCAR=29.3
Pod undocumented
196 10 my ($self, $p) = @_;
197  
198 10 my @qmarks;
199 10 my $user_ph_idx = 0;
200  
201 10 my $i=0;
202
203 10 ▶ if ($self->{bind_value_layouts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"bind_value_layouts"}) 10 0 10
204  
205 10 my $prev_pnum = 0;
206 10 my $max_pnum = 0;
207
208 10 my @pnums = sort {$a <=> $b} keys %{$self->{bind_value_layouts}};
209 10 $max_pnum = $pnums[-1];
210
211 #Walk through the integers for each possible bind value.
212 #If it doesn't have a layout assigned from the config file
213 #then shift it off the array from the $log call
214 #This needs to be reworked now that we always get an arrayref? --kg 1/2003
215 10 foreach my $pnum (1..$max_pnum){
216 42 my $msg;
217
218 #we've got a bind_value_layout to fill the spot
219 42 ▶ if ($self->{bind_value_layouts}{$pnum}){
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'bind_value_layouts'}{$pnum}) { } 30 12 42
elsif (ref $p->{'message'} eq 'ARRAY' and @{$p->{'message'};}) { } 12 0 12
elsif (ref $p->{'message'} eq 'ARRAY') { } 0 0 0
elsif (not ref $p->{'message'}) { } 0 0 0
220 $msg = $self->{bind_value_layouts}{$pnum}->render(
221 $p->{message},
222 $p->{log4p_category},
223 $p->{log4p_level},
224 30 5 + $Log::Log4perl::caller_depth,
225 );
226  
227 #we don't have a bind_value_layout, so get
228 #a message bit
229 12 ▶ }elsif (ref $p->{message} eq 'ARRAY' && @{$p->{message}}){
Condition33%1/3
ref $p->{"message"} eq "ARRAY" and @{$p->{"message"};}
!ll&&!rl&&r
0012
MC/DC0%0/2
ref $p->{"message"} eq "ARRAY" and @{$p->{"message"};}
ref $p->{"message"} eq "ARRAY" @{$p->{"message"};}
Truth table33%1/3
A and B
A= ref $p->{"message"} eq "ARRAY"B= @{$p->{"message"};}
ABresult
0X0
100
111
230 #$msg = shift @{$p->{message}};
231 12 $msg = $p->{message}->[$i++];
232  
233 #here handle cases where we ran out of message bits
234 #before we ran out of bind_value_layouts, just keep going
235 }elsif (ref $p->{message} eq 'ARRAY'){
236 0 $msg = undef;
237 0 $p->{message} = undef;
238  
239 #here handle cases where we didn't get an arrayref
240 #log the message in the first placeholder and nothing in the rest
241 }elsif (! ref $p->{message} ){
242 0 $msg = $p->{message};
243 0 $p->{message} = undef;
244  
245 }
246  
247 42 ▶ if ($self->{MAX_COL_SIZE} &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"MAX_COL_SIZE"} and length $msg > $self->{"MAX_COL_SIZE"}) 0 42 42
Condition33%1/3
$self->{"MAX_COL_SIZE"} and length $msg > $self->{"MAX_COL_SIZE"}
!ll&&!rl&&r
4200
MC/DC0%0/2
$self->{"MAX_COL_SIZE"} and length $msg > $self->{"MAX_COL_SIZE"}
$self->{"MAX_COL_SIZE"} length $msg > $self->{"MAX_COL_SIZE"}
Truth table33%1/3
A and B
A= $self->{"MAX_COL_SIZE"}B= length $msg > $self->{"MAX_COL_SIZE"}
ABresult
0X0
100
111
248 length($msg) > $self->{MAX_COL_SIZE}){
249 0 substr($msg, $self->{MAX_COL_SIZE}) = '';
250 }
251 42 push @qmarks, $msg;
252 }
253 }
254  
255 #handle leftovers
256 10 ▶ if (ref $p->{message} eq 'ARRAY' && @{$p->{message}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $p->{"message"} eq "ARRAY" and @{$p->{"message"};}) 10 0 10
Condition33%1/3
ref $p->{"message"} eq "ARRAY" and @{$p->{"message"};}
!ll&&!rl&&r
0010
MC/DC0%0/2
ref $p->{"message"} eq "ARRAY" and @{$p->{"message"};}
ref $p->{"message"} eq "ARRAY" @{$p->{"message"};}
Truth table33%1/3
A and B
A= ref $p->{"message"} eq "ARRAY"B= @{$p->{"message"};}
ABresult
0X0
100
111
257 #push @qmarks, @{$p->{message}};
258 10 push @qmarks, @{$p->{message}}[$i..@{$p->{message}}-1];
259  
260 }
261  
262 10 return \@qmarks;
263 }
264  
265  
266 11 ▶sub check_buffer {
Subroutine check_buffer: called CC=9 SCAR=22.3
Pod undocumented
267 11 my $self = shift;
268  
269 11 ▶ return unless ($self->{BUFFER} && ref $self->{BUFFER} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"BUFFER"} and ref $self->{"BUFFER"} eq "ARRAY" 3 8 11
Condition66%2/3
$self->{"BUFFER"} and ref $self->{"BUFFER"} eq "ARRAY"
!ll&&!rl&&r
308
MC/DC50%1/2
$self->{"BUFFER"} and ref $self->{"BUFFER"} eq "ARRAY"
$self->{"BUFFER"} ref $self->{"BUFFER"} eq "ARRAY"
Truth table66%2/3
A and B
A= $self->{"BUFFER"}B= ref $self->{"BUFFER"} eq "ARRAY"
ABresult
0X0
100
111
270  
271 8 ▶ if (scalar @{$self->{BUFFER}} >= $self->{BUFFERSIZE} * 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"BUFFER"};} >= $self->{"BUFFERSIZE"} * 2) 4 4 8
272  
273 4 my ($sth, $stmt, $prev_stmt);
274  
275 4 $prev_stmt = ""; # Init to avoid warning (ms 5/10/03)
276  
277 4 ▶ while (@{$self->{BUFFER}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"BUFFER"};}) 6 4 10
278 6 my ($stmt, $qmarks) = splice (@{$self->{BUFFER}},0,2);
279  
280 6 $self->{pending_stmt} = $stmt;
281  
282 #reuse the sth if the stmt doesn't change
283 6 ▶ if ($stmt ne $prev_stmt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stmt ne $prev_stmt) 4 2 6
284 4 ▶ $sth->finish if $sth;
Branch: 1 branch
BranchTrueFalseTotal
if $sth 0 4 4
285 4 $sth = $self->create_statement($stmt);
286 }
287  
288 6 $self->query_execute($sth, @$qmarks);
289  
290 6 $prev_stmt = $stmt;
291  
292 }
293  
294 4 $sth->finish;
295  
296 4 my $dbh = $self->{dbh};
297  
298 4 ▶ if ($dbh && ! $dbh->{AutoCommit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbh and not $dbh->{"AutoCommit"}) 0 4 4
Condition33%1/3
$dbh and not $dbh->{"AutoCommit"}
!ll&&!rl&&r
040
MC/DC0%0/2
$dbh and not $dbh->{"AutoCommit"}
$dbh not $dbh->{"AutoCommit"}
Truth table33%1/3
A and not(B)
A= $dbhB= not $dbh->{"AutoCommit"}
ABresult
0X0
100
111
299 0 $dbh->commit;
300 }
301 }
302 }
303  
304 5 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.8
305 5 my $self = shift;
306  
307 5 $self->{BUFFERSIZE} = 1;
308  
309 5 $self->check_buffer();
310  
311 5 ▶ if ($self->{_mine} && $self->{dbh}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_mine"} and $self->{"dbh"}) 5 0 5
Condition33%1/3
$self->{"_mine"} and $self->{"dbh"}
!ll&&!rl&&r
005
MC/DC0%0/2
$self->{"_mine"} and $self->{"dbh"}
$self->{"_mine"} $self->{"dbh"}
Truth table33%1/3
A and B
A= $self->{"_mine"}B= $self->{"dbh"}
ABresult
0X0
100
111
312 5 $self->{dbh}->disconnect;
313 }
314 }
315  
316  
317 3 1;
318  
319 __END__
« Log4perl.pm ↑ Summary Log4perl/Appender/Socket.pm »