File Coverage

blib/lib/Devel/KYTProf/Profiler/DBI.pm
Criterion Covered Total %
statement 28 28 100.0
branch 3 4 75.0
condition 2 4 50.0
subroutine 10 10 100.0
pod 0 1 0.0
total 43 47 91.4


line stmt bran cond sub pod time code
1             package Devel::KYTProf::Profiler::DBI;
2              
3 6     6   3779 use strict;
  6         14  
  6         192  
4 6     6   32 use warnings;
  6         15  
  6         184  
5 6     6   2753 use DBIx::Tracer;
  6         11347  
  6         2076  
6              
7             sub apply {
8             Devel::KYTProf->add_prof(
9             'DBI',
10             'connect',
11             sub {
12 1     1   4 my ($orig, $class, $dsn, $user, $pass, $attr) = @_;
13             return [
14             '%s %s',
15             ['dbi_connect_method', 'dsn'],
16             {
17 1   50     22 dbi_connect_method => $attr->{dbi_connect_method} || 'connect',
18             dsn => $dsn,
19             },
20             ];
21             }
22 6     6 0 57 );
23              
24 6         19 my $LastSQL;
25             my $LastBinds;
26 6         0 my $IsInProf;
27              
28             our $_TRACER = DBIx::Tracer->new(sub {
29 3     3   728 my %args = @_;
30 3         8 $LastSQL = $args{sql};
31 3   50     24 my $bind_params = $args{bind_params} || [];
32             $LastBinds = scalar(@$bind_params) ?
33 3 50       17 '(bind: '.join(', ', map { defined $_ ? $_ : 'undef' } @$bind_params).')' :
  4 100       23  
34             '';
35 6         33 });
36             Devel::KYTProf->add_prof(
37             'DBI::st',
38             'execute',
39             sub {
40 2     2   6 my (undef, $sth) = @_;
41             return [
42 2         21 '%s %s (%d rows)',
43             ['sql', 'sql_binds', 'rows'],
44             {
45             sql => $LastSQL,
46             sql_binds => $LastBinds,
47             rows => $sth->rows,
48             },
49             ];
50             },
51 2     2   7 sub { !$IsInProf },
52 6         425 );
53              
54             Devel::KYTProf->add_profs(
55             'DBI::db',
56             [qw/do selectall_arrayref selectrow_arrayref selectrow_array/],
57             sub {
58 1     1   3 undef $IsInProf;
59             return [
60 1         7 '%s %s',
61             ['sql', 'sql_binds'],
62             {
63             sql => $LastSQL,
64             sql_binds => $LastBinds,
65             },
66             ];
67             },
68             # Since there is a possibility that these methods call `execute` method
69             # internally (it depends on DBD implementation), we flag here to prevent
70             # duplicate profiling output.
71             # And we drop this flag in the above callback.
72 1     1   3 sub { $IsInProf = 1 },
73 6         51 );
74             }
75              
76             1;