| 1 | package DBI::Log; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | no strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | no warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use DBI; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Time::HiRes; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | our $VERSION = "0.11"; | |||||||||||||||||||||||||||
| 10 | 1 | our %opts = ( | |||||||||||||||||||||||||||
| 11 | file => $file, | ||||||||||||||||||||||||||||
| 12 | trace => 0, | ||||||||||||||||||||||||||||
| 13 | timing => 0, | ||||||||||||||||||||||||||||
| 14 | replace_placeholders => 1, | ||||||||||||||||||||||||||||
| 15 | fh => undef, | ||||||||||||||||||||||||||||
| 16 | exclude => undef, | ||||||||||||||||||||||||||||
| 17 | format => "sql", | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 1 | my $orig_execute = \&DBI::st::execute; | |||||||||||||||||||||||||||
| 21 | *DBI::st::execute = sub { | ||||||||||||||||||||||||||||
| 22 | 4 | ▶ | my ($sth, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 4 | my $log = pre_query("execute", $sth->{Database}, $sth, $sth->{Statement}, \@args); | |||||||||||||||||||||||||||
| 24 | 4 | my $retval = $orig_execute->($sth, @args); | |||||||||||||||||||||||||||
| 25 | 4 | post_query($log); | |||||||||||||||||||||||||||
| 26 | 4 | return $retval; | |||||||||||||||||||||||||||
| 27 | 1 | }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 1 | my $orig_selectall_arrayref = \&DBI::db::selectall_arrayref; | |||||||||||||||||||||||||||
| 30 | *DBI::db::selectall_arrayref = sub { | ||||||||||||||||||||||||||||
| 31 | 1 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | 1 | my $log = pre_query("selectall_arrayref", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 33 | 1 | my $retval = $orig_selectall_arrayref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 34 | 1 | post_query($log); | |||||||||||||||||||||||||||
| 35 | 1 | return $retval; | |||||||||||||||||||||||||||
| 36 | 1 | }; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 1 | my $orig_selectcol_arrayref = \&DBI::db::selectcol_arrayref; | |||||||||||||||||||||||||||
| 39 | *DBI::db::selectcol_arrayref = sub { | ||||||||||||||||||||||||||||
| 40 | 1 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 1 | my $log = pre_query("selectcol_arrayref", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 42 | 1 | my $retval = $orig_selectcol_arrayref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 43 | 1 | post_query($log); | |||||||||||||||||||||||||||
| 44 | 1 | return $retval; | |||||||||||||||||||||||||||
| 45 | 1 | }; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 1 | my $orig_selectall_hashref = \&DBI::db::selectall_hashref; | |||||||||||||||||||||||||||
| 48 | *DBI::db::selectall_hashref = sub { | ||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 50 | 0 | my $log = pre_query("selectall_hashref", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 51 | 0 | my $retval = $orig_selectall_hashref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 52 | 0 | post_query($log); | |||||||||||||||||||||||||||
| 53 | 0 | return $retval; | |||||||||||||||||||||||||||
| 54 | 1 | }; | |||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 1 | my $orig_selectrow_arrayref = \&DBI::db::selectrow_arrayref; | |||||||||||||||||||||||||||
| 57 | *DBI::db::selectrow_arrayref = sub { | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | 0 | my $log = pre_query("selectrow_arrayref", $dbh, $sth, $query, \@args); | |||||||||||||||||||||||||||
| 60 | 0 | my $retval = $orig_selectrow_arrayref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 61 | 0 | post_query($log); | |||||||||||||||||||||||||||
| 62 | 0 | return $retval; | |||||||||||||||||||||||||||
| 63 | 1 | }; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | my $orig_selectrow_array = \&DBI::db::selectrow_array; | |||||||||||||||||||||||||||
| 66 | *DBI::db::selectrow_array = sub { | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 68 | 0 | my $log = pre_query("selectrow_array", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 69 | 0 | my $retval = $orig_selectrow_array->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 70 | 0 | post_query($log); | |||||||||||||||||||||||||||
| 71 | 0 | return $retval; | |||||||||||||||||||||||||||
| 72 | 1 | }; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | my $orig_selectrow_hashref = \&DBI::db::selectrow_hashref; | |||||||||||||||||||||||||||
| 75 | *DBI::db::selectrow_hashref = sub { | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 77 | 0 | my $log = pre_query("selectrow_hashref", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 78 | 0 | my $retval = $orig_selectrow_hashref->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 79 | 0 | post_query($log); | |||||||||||||||||||||||||||
| 80 | 0 | return $retval; | |||||||||||||||||||||||||||
| 81 | 1 | }; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 1 | my $orig_do = \&DBI::db::do; | |||||||||||||||||||||||||||
| 84 | *DBI::db::do = sub { | ||||||||||||||||||||||||||||
| 85 | 3 | ▶ | my ($dbh, $query, $yup, @args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 86 | 3 | my $log = pre_query("do", $dbh, undef, $query, \@args); | |||||||||||||||||||||||||||
| 87 | 3 | my $retval = $orig_do->($dbh, $query, $yup, @args); | |||||||||||||||||||||||||||
| 88 | 2 | post_query($log); | |||||||||||||||||||||||||||
| 89 | 2 | return $retval; | |||||||||||||||||||||||||||
| 90 | 1 | }; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub import { | ||||||||||||||||||||||||||||
| 94 | 2 | ▶ | my ($package, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=6 SCAR=22.9
| |||||||||||||||||||||||||||||
| 95 | 2 | for my $key (keys %args) { | |||||||||||||||||||||||||||
| 96 | 3 | $opts{$key} = $args{$key}; | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | 2 | ▶ | if (!$opts{file}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 0 | $opts{fh} = \*STDERR; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | else { | ||||||||||||||||||||||||||||
| 102 | 2 | my $file2 = $opts{file}; | |||||||||||||||||||||||||||
| 103 | 2 | ▶ | if ($file2 =~ m{^~/}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my $home = $ENV{HOME} || (getpwuid($<))[7]; | ||||||||||||||||||||||||||
|
Condition0%0/3
$ENV{'HOME'} || (getpwuid $<)[7]
MC/DC0%0/2
$ENV{'HOME'} || (getpwuid $<)[7]
$ENV{'HOME'}
(getpwuid $<)[7]
Truth table0%0/3
A || B
A= $ENV{'HOME'}B= (getpwuid $<)[7]
| |||||||||||||||||||||||||||||
| 105 | 0 | $file2 =~ s{^~/}{$home/}; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | 2 | ▶ | open $opts{fh}, ">>", $file2 or die "Can't open $opts{file}: $!\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | sub pre_query { | ||||||||||||||||||||||||||||
| 112 | 9 | ▶ | my ($name, $dbh, $sth, $query, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
pre_query: called CC=25 SCAR=34.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 113 | 9 | my $log = {}; | |||||||||||||||||||||||||||
| 114 | 9 | my $mcount = 0; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Some DBI functions are composed of other DBI functions, so make sure we | ||||||||||||||||||||||||||||
| 117 | # are only logging the top level one. For example $dbh->do() will call | ||||||||||||||||||||||||||||
| 118 | # $dbh->execute() internally, so we need to make sure a DBI::Log function | ||||||||||||||||||||||||||||
| 119 | # logs the $dbh->do() and not the internal $dbh->execute(). If multiple | ||||||||||||||||||||||||||||
| 120 | # functions were called, we return and flag this log entry to be skipped in | ||||||||||||||||||||||||||||
| 121 | # the post_query() part. | ||||||||||||||||||||||||||||
| 122 | 9 | ▶ | for (my $i = 0; my @caller = caller($i); $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 123 | 22 | my ($package, $file, $line, $sub) = @caller; | |||||||||||||||||||||||||||
| 124 | 22 | ▶ | if ($package eq "DBI::Log") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 12 | $mcount++; | |||||||||||||||||||||||||||
| 126 | 12 | ▶ | if ($mcount > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 3 | $log->{skip} = 1; | |||||||||||||||||||||||||||
| 128 | 3 | return $log; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 6 | my @callers; | |||||||||||||||||||||||||||
| 133 | 6 | ▶ | for (my $i = 0; my @caller = caller($i); $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 13 | push @callers, \@caller; | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | # Order the call stack based on the highest level calls first, then the | ||||||||||||||||||||||||||||
| 138 | # lower level calls. Once you reach a package that is excluded, do not show | ||||||||||||||||||||||||||||
| 139 | # any more lines in the stack trace. By default, it will exclude anything | ||||||||||||||||||||||||||||
| 140 | # past the DBI::Log package, but if user provides an exclude option, it will | ||||||||||||||||||||||||||||
| 141 | # stop there. | ||||||||||||||||||||||||||||
| 142 | 6 | my @filtered_callers; | |||||||||||||||||||||||||||
| 143 | 6 | CALLER: for my $caller (reverse @callers) { | |||||||||||||||||||||||||||
| 144 | 13 | my ($package, $file, $line, $long_sub) = @$caller; | |||||||||||||||||||||||||||
| 145 | 13 | ▶ | if ($package eq "DBI::Log") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 6 | last CALLER; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 7 | ▶ | if ($opts{exclude}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | for my $item (@{$opts{exclude}}) { | |||||||||||||||||||||||||||
| 150 | 0 | ▶ | if ($package =~ /^$item(::|$)/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 0 | last CALLER; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | 7 | push @filtered_callers, $caller; | |||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | 6 | ▶ | if (!$opts{trace}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | 6 | @filtered_callers = ($filtered_callers[-1]); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 6 | my @stack; | |||||||||||||||||||||||||||
| 163 | 6 | for my $caller (@filtered_callers) { | |||||||||||||||||||||||||||
| 164 | 6 | my ($package, $file, $line, $long_sub) = @$caller; | |||||||||||||||||||||||||||
| 165 | 6 | my $sub = $long_sub; | |||||||||||||||||||||||||||
| 166 | 6 | $sub =~ s/.*:://; | |||||||||||||||||||||||||||
| 167 | 6 | ▶ | $sub = $name if $long_sub =~ /^DBI::Log::__ANON__/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | 6 | push @stack, { | |||||||||||||||||||||||||||
| 169 | sub => $sub, | ||||||||||||||||||||||||||||
| 170 | file => $file, | ||||||||||||||||||||||||||||
| 171 | line => $line, | ||||||||||||||||||||||||||||
| 172 | }; | ||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 6 | ▶ | if (ref($query) && ref($query) eq "DBI::st") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $query and ref $query eq "DBI::st"
MC/DC50%1/2
ref $query and ref $query eq "DBI::st"
ref $query
ref $query eq "DBI::st"
Truth table66%2/3
A and B
A= ref $queryB= ref $query eq "DBI::st"
| |||||||||||||||||||||||||||||
| 176 | 1 | $sth = $query; | |||||||||||||||||||||||||||
| 177 | 1 | $query = $query->{Statement}; | |||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 6 | ▶ | if ($dbh && $opts{replace_placeholders}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$dbh and $opts{"replace_placeholders"}
MC/DC0%0/2
$dbh and $opts{"replace_placeholders"}
$dbh
$opts{"replace_placeholders"}
Truth table33%1/3
A and B
A= $dbhB= $opts{"replace_placeholders"}
| |||||||||||||||||||||||||||||
| 181 | # When you use $sth->bind_param(1, "value") the params can be found in | ||||||||||||||||||||||||||||
| 182 | # $sth->{ParamValues} and they override arguments sent in to | ||||||||||||||||||||||||||||
| 183 | # $sth->execute() | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 6 | my @args_copy = @$args; | |||||||||||||||||||||||||||
| 186 | 6 | my %values; | |||||||||||||||||||||||||||
| 187 | 6 | ▶ | if ($sth && $sth->{ParamValues}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$sth and $sth->{"ParamValues"}
MC/DC50%1/2
$sth and $sth->{"ParamValues"}
$sth
$sth->{"ParamValues"}
Truth table66%2/3
A and B
A= $sthB= $sth->{"ParamValues"}
| |||||||||||||||||||||||||||||
| 188 | 2 | %values = %{$sth->{ParamValues}}; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | 6 | for my $key (keys %values) { | |||||||||||||||||||||||||||
| 191 | 0 | ▶ | if (defined $key && $key =~ /^\d+$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $key and $key =~ /^\d+$/
MC/DC0%0/2
defined $key and $key =~ /^\d+$/
defined $key
$key =~ /^\d+$/
Truth table0%0/3
A and B
A= defined $keyB= $key =~ /^\d+$/
| |||||||||||||||||||||||||||||
| 192 | 0 | $args_copy[$key - 1] = $values{$key}; | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 6 | for my $i (0 .. @args_copy - 1) { | |||||||||||||||||||||||||||
| 197 | 4 | my $value = $args_copy[$i]; | |||||||||||||||||||||||||||
| 198 | 4 | $value = $dbh->quote($value); | |||||||||||||||||||||||||||
| 199 | 4 | $query =~ s{\?}{$value}e; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 6 | $query =~ s/^\s*\n|\s*$//g; | |||||||||||||||||||||||||||
| 204 | 6 | $log->{time_started} = Time::HiRes::time(); | |||||||||||||||||||||||||||
| 205 | 6 | $log->{query} = $query; | |||||||||||||||||||||||||||
| 206 | 6 | $log->{stack} = \@stack; | |||||||||||||||||||||||||||
| 207 | 6 | ▶ | if ($opts{format} eq "json") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 208 | # For JSON output we don't want to output anything yet, so post_query() | ||||||||||||||||||||||||||||
| 209 | # can emit the whole JSON object, just remember them. | ||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||
| 211 | else { | ||||||||||||||||||||||||||||
| 212 | 5 | my $mesg; | |||||||||||||||||||||||||||
| 213 | 5 | $mesg .= "-- " . scalar(localtime()) . "\n"; | |||||||||||||||||||||||||||
| 214 | 5 | for my $caller (@stack) { | |||||||||||||||||||||||||||
| 215 | 5 | $mesg .= "-- $caller->{sub} $caller->{file} $caller->{line}\n"; | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | 5 | $mesg .= "$query\n"; | |||||||||||||||||||||||||||
| 218 | 5 | print {$opts{fh}} $mesg; | |||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | 6 | return $log; | |||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | sub post_query { | ||||||||||||||||||||||||||||
| 225 | 8 | ▶ | my ($log) = @_; | ||||||||||||||||||||||||||
|
Subroutine
post_query: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 226 | 8 | ▶ | return if $log->{skip}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 5 | $log->{time_ended} = Time::HiRes::time(); | |||||||||||||||||||||||||||
| 228 | 5 | $log->{time_taken} = sprintf "%.3f", $log->{time_ended} - $log->{time_started}; | |||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | 5 | ▶ | if ($opts{format} eq "json") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 231 | # print all the info as JSON | ||||||||||||||||||||||||||||
| 232 | 1 | print {$opts{fh}} to_json($log) . "\n"; | |||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||
| 234 | else { | ||||||||||||||||||||||||||||
| 235 | # For SQL output format, pre_query already printed most of the info, we | ||||||||||||||||||||||||||||
| 236 | # just need to add the time taken - and that only if we're doing | ||||||||||||||||||||||||||||
| 237 | # timings... | ||||||||||||||||||||||||||||
| 238 | 4 | ▶ | if ($opts{timing}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 239 | 0 | print {$opts{fh}} "-- $log->{time_taken}s\n"; | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 4 | print {$opts{fh}} "\n"; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | sub to_json { | ||||||||||||||||||||||||||||
| 246 | 10 | ▶ | my ($val, $depth) = @_; | ||||||||||||||||||||||||||
|
Subroutine
to_json: called CC=20 SCAR=31.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 247 | 10 | ▶ | $depth ||= 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$depth ||= 0
MC/DC100%1/1
$depth ||= 0
$depth
Truth table100%2/2
A
A= $depth
| |||||||||||||||||||||||||||||
| 248 | 10 | my $pretty = 0; | |||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 10 | my $out; | |||||||||||||||||||||||||||
| 251 | 10 | ▶ | if (!defined $val) { | ||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||
| 252 | 0 | $out = "null"; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | elsif (ref $val eq "HASH") { | ||||||||||||||||||||||||||||
| 255 | 2 | $out = "{"; | |||||||||||||||||||||||||||
| 256 | 2 | ▶ | $out .= "\n" if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 257 | 2 | my $i = 0; | |||||||||||||||||||||||||||
| 258 | 2 | for my $key (sort keys %$val) { | |||||||||||||||||||||||||||
| 259 | 8 | my $val2 = $val->{$key}; | |||||||||||||||||||||||||||
| 260 | 8 | ▶ | if ($i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 261 | 6 | ▶ | $out .= $pretty ? ",\n" : ", "; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | 8 | ▶ | $out .= " " x ($depth + 1) if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | 8 | $out .= "\"$key\": " . to_json($val2, $depth + 1); | |||||||||||||||||||||||||||
| 265 | 8 | $i++; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | 2 | ▶ | $out .= "\n" if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 268 | 2 | ▶ | $out .= " " x ($depth) if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 2 | $out .= "}"; | |||||||||||||||||||||||||||
| 270 | } | ||||||||||||||||||||||||||||
| 271 | elsif (ref $val eq "ARRAY") { | ||||||||||||||||||||||||||||
| 272 | 1 | $out = "["; | |||||||||||||||||||||||||||
| 273 | 1 | ▶ | $out .= "\n" if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | 1 | for my $i (0 .. @$val - 1) { | |||||||||||||||||||||||||||
| 275 | 1 | my $val2 = $val->[$i]; | |||||||||||||||||||||||||||
| 276 | 1 | ▶ | if ($i) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 0 | ▶ | $out .= $pretty ? ",\n" : ", "; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | 1 | ▶ | $out .= " " x ($depth + 1) if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 280 | 1 | $out .= to_json($val2, $depth + 1); | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | 1 | ▶ | $out .= "\n" if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 1 | ▶ | $out .= " " x ($depth) if $pretty; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 1 | $out .= "]"; | |||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||
| 286 | elsif ($val =~ /^(-?\d+(\.\d*)?(e[+-]?\d+)?)$/i) { | ||||||||||||||||||||||||||||
| 287 | 4 | $out = $val; | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | else { | ||||||||||||||||||||||||||||
| 290 | 3 | $val =~ s/"/\"/g; | |||||||||||||||||||||||||||
| 291 | 3 | $out = "\"$val\""; | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 10 | return $out; | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 1 | 1; | |||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||
| 299 | __END__ | ||||||||||||||||||||||||||||