DBIx/Class/Storage/Debug/PrettyPrint.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 121 CC 8 SCAR 22.4
CC
8
Cov
0%
print
20.0
query_start
12.0
new
6.0
CRAP 9.4
1 package DBIx::Class::Storage::Debug::PrettyPrint;
2  
3 our $VERSION = '2.000003';
4 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
5  
6 use strict;
7 use warnings;
8  
9 use base 'DBIx::Class::Storage::Statistics';
10  
11 use SQL::Abstract::Tree;
12  
13 __PACKAGE__->mk_group_accessors( simple => '_sqlat' );
14 __PACKAGE__->mk_group_accessors( simple => '_clear_line_str' );
15 __PACKAGE__->mk_group_accessors( simple => '_executing_str' );
16 __PACKAGE__->mk_group_accessors( simple => '_show_progress' );
17 __PACKAGE__->mk_group_accessors( simple => '_last_sql' );
18 __PACKAGE__->mk_group_accessors( simple => 'squash_repeats' );
19  
20 sub new {
21 my $class = shift;
22 my $args = shift;
23  
24 my $clear_line = $args->{clear_line} || "\r\x1b[J";
25 my $executing = $args->{executing} || (
26 eval { require Term::ANSIColor } ? do {
27 my $c = \&Term::ANSIColor::color;
28 $c->('blink white on_black') . 'EXECUTING...' . $c->('reset');
29 } : 'EXECUTING...'
30 );
31 my $show_progress = $args->{show_progress};
32  
33 my $squash_repeats = $args->{squash_repeats};
34 my $sqlat = SQL::Abstract::Tree->new($args);
35 my $self = $class->next::method(@_);
36 $self->_clear_line_str($clear_line);
37 $self->_executing_str($executing);
38 $self->_show_progress($show_progress);
39  
40 $self->squash_repeats($squash_repeats);
41  
42 $self->_sqlat($sqlat);
43 $self->_last_sql('');
44  
45 return $self
46 }
47  
48 sub print {
49 my $self = shift;
50 my $string = shift;
51 my $bindargs = shift || [];
52  
53 my ($lw, $lr);
54 ($lw, $string, $lr) = $string =~ /^(\s*)(.+?)(\s*)$/s;
55  
56 local $self->_sqlat->{fill_in_placeholders} = 0 if defined $bindargs
57 && defined $bindargs->[0] && $bindargs->[0] eq q('__BULK_INSERT__');
58  
59 my $use_placeholders = !!$self->_sqlat->fill_in_placeholders;
60  
61 my $sqlat = $self->_sqlat;
62 my $formatted;
63 if ($self->squash_repeats && $self->_last_sql eq $string) {
64 my ( $l, $r ) = @{ $sqlat->placeholder_surround };
65 $formatted = '... : ' . join(', ', map "$l$_$r", @$bindargs)
66 } else {
67 $self->_last_sql($string);
68 $formatted = $sqlat->format($string, $bindargs);
69 $formatted = "$formatted : " . join ', ', @{$bindargs}
70 unless $use_placeholders;
71 }
72  
73 $self->next::method("$lw$formatted$lr", @_);
74 }
75  
76 sub query_start {
77 my ($self, $string, @bind) = @_;
78  
79 if (defined $self->callback) {
80 $string =~ m/^(\w+)/;
81 $self->callback->($1, "$string: ".join(', ', @bind)."\n");
82 return;
83 }
84  
85 $string =~ s/\s+$//;
86  
87 $self->print("$string\n", \@bind);
88  
89 $self->debugfh->print($self->_executing_str) if $self->_show_progress
90 }
91  
92 sub query_end {
93 $_[0]->debugfh->print($_[0]->_clear_line_str) if $_[0]->_show_progress
94 }
95  
96 1;
97  
98 =pod
99  
100 =head1 NAME
101  
102 DBIx::Class::Storage::Debug::PrettyPrint - Pretty Printing DebugObj
103  
104 =head1 REQUIRES
105  
106 This module ships inside the SQL-Abstract distribution but requires
107 L<DBIx::Class> at runtime. DBIx::Class is intentionally B<not> a
108 prerequisite of SQL-Abstract - DBIx::Class depends on SQL::Abstract, so
109 declaring the reverse would create a dependency cycle - therefore you must
110 have DBIx::Class installed separately to use this module.
111  
112 =head1 SYNOPSIS
113  
114 DBIC_TRACE_PROFILE=~/dbic.json perl -Ilib ./foo.pl
115  
116 Where dbic.json contains:
117  
118 {
119 "profile":"console",
120 "show_progress":1,
121 "squash_repeats":1
122 }
123  
124 =head1 METHODS
125  
126 =head2 new
127  
128 my $pp = DBIx::Class::Storage::Debug::PrettyPrint->new({
129 show_progress => 1, # tries it's best to make it clear that a SQL
130 # statement is still running
131 executing => '...', # the string that is added to the end of SQL
132 # if show_progress is on. You probably don't
133 # need to set this
134 clear_line => '<CR><ESC>[J', # the string used to erase the string added
135 # to SQL if show_progress is on. Again, the
136 # default is probably good enough.
137  
138 squash_repeats => 1, # set to true to make repeated SQL queries
139 # be ellided and only show the new bind params
140 # any other args are passed through directly to SQL::Abstract::Tree
141 });
142  
143