Do not show_progress by default
[dbsrgits/SQL-Abstract.git] / lib / DBIx / Class / Storage / Debug / PrettyPrint.pm
CommitLineData
0d5df7d6 1package DBIx::Class::Storage::Debug::PrettyPrint;
1dc93d17 2
84c65032 3use strict;
4use warnings;
5
1dc93d17 6use base 'DBIx::Class::Storage::Statistics';
7
8use SQL::Abstract::Tree;
9
10__PACKAGE__->mk_group_accessors( simple => '_sqlat' );
a2b743ce 11__PACKAGE__->mk_group_accessors( simple => '_clear_line_str' );
12__PACKAGE__->mk_group_accessors( simple => '_executing_str' );
13__PACKAGE__->mk_group_accessors( simple => '_show_progress' );
b25246f0 14__PACKAGE__->mk_group_accessors( simple => '_last_sql' );
66c2fcc3 15__PACKAGE__->mk_group_accessors( simple => 'squash_repeats' );
1dc93d17 16
17sub new {
0d5df7d6 18 my $class = shift;
416cdb2e 19 my $args = shift;
1dc93d17 20
416cdb2e 21 my $clear_line = $args->{clear_line} || "\r\e[J";
22 my $executing = $args->{executing} || eval { require Term::ANSIColor } ? do {
23 my $c = \&Term::ANSIColor::color;
24 $c->('blink white on_black') . 'EXECUTING...' . $c->('reset');;
25 } : 'EXECUTING...';
17c25d88 26 my $show_progress = $args->{show_progress};
416cdb2e 27
66c2fcc3 28 my $squash_repeats = $args->{squash_repeats};
416cdb2e 29 my $sqlat = SQL::Abstract::Tree->new($args);
0d5df7d6 30 my $self = $class->next::method(@_);
a2b743ce 31 $self->_clear_line_str($clear_line);
32 $self->_executing_str($executing);
33 $self->_show_progress($show_progress);
1dc93d17 34
66c2fcc3 35 $self->squash_repeats($squash_repeats);
b25246f0 36
0d5df7d6 37 $self->_sqlat($sqlat);
b25246f0 38 $self->_last_sql('');
1dc93d17 39
0d5df7d6 40 return $self
1dc93d17 41}
42
31756ef2 43sub print {
1dc93d17 44 my $self = shift;
45 my $string = shift;
25d4d820 46 my $bindargs = shift || [];
84c65032 47
416cdb2e 48 my ($lw, $lr);
49 ($lw, $string, $lr) = $string =~ /^(\s*)(.+?)(\s*)$/s;
50
f038abc7 51 local $self->_sqlat->{fill_in_placeholders} = 0 if defined $bindargs
52 && defined $bindargs->[0] && $bindargs->[0] eq q('__BULK_INSERT__');
fab0bed9 53
84c65032 54 my $use_placeholders = !!$self->_sqlat->fill_in_placeholders;
55
56 # DBIC pre-quotes bindargs
ec02b310 57 $bindargs = [map { s/^'//; s/'$//; $_ } @{$bindargs}] if $use_placeholders;
84c65032 58
b25246f0 59 my $sqlat = $self->_sqlat;
60 my $formatted;
66c2fcc3 61 if ($self->squash_repeats && $self->_last_sql eq $string) {
b25246f0 62 my ( $l, $r ) = @{ $sqlat->placeholder_surround };
63 $formatted = '... : ' . join(', ', map "$l$_$r", @$bindargs) . "\n";
64 } else {
65 $self->_last_sql($string);
66 $formatted = $sqlat->format($string, $bindargs) . "\n";
67 $formatted = "$formatted: " . join ', ', @{$bindargs}
68 unless $use_placeholders;
69 }
1dc93d17 70
416cdb2e 71 $self->next::method("$lw$formatted$lr", @_);
1dc93d17 72}
73
84c65032 74sub query_start {
75 my ($self, $string, @bind) = @_;
76
77 if(defined $self->callback) {
78 $string =~ m/^(\w+)/;
79 $self->callback->($1, "$string: ".join(', ', @bind)."\n");
80 return;
81 }
82
416cdb2e 83 $string =~ s/\s+$//;
84
85 $self->print("$string\n", \@bind);
86
a2b743ce 87 $self->debugfh->print($self->_executing_str) if $self->_show_progress
416cdb2e 88}
89
90sub query_end {
a2b743ce 91 $_[0]->debugfh->print($_[0]->_clear_line_str) if $_[0]->_show_progress
84c65032 92}
93
1dc93d17 941;
6b1bf9f8 95
96=pod
97
98=head1 SYNOPSIS
99
100 package MyApp::Schema;
101
102 use parent 'DBIx::Class::Schema';
103
0d5df7d6 104 use DBIx::Class::Storage::Debug::PrettyPrint;
6b1bf9f8 105
106 __PACKAGE__->load_namespaces;
107
0d5df7d6 108 my $pp = DBIx::Class::Storage::Debug::PrettyPrint->new({
109 profile => 'console',
110 });
6b1bf9f8 111
112 sub connection {
0d5df7d6 113 my $self = shift;
6b1bf9f8 114
0d5df7d6 115 my $ret = $self->next::method(@_);
6b1bf9f8 116
0d5df7d6 117 $self->storage->debugobj($pp);
6b1bf9f8 118
0d5df7d6 119 $ret
6b1bf9f8 120 }
121