allow disabling of show_progress; _ some accessors
[scpubgit/Q-Branch.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' );
1dc93d17 14
15sub new {
0d5df7d6 16 my $class = shift;
416cdb2e 17 my $args = shift;
1dc93d17 18
416cdb2e 19 my $clear_line = $args->{clear_line} || "\r\e[J";
20 my $executing = $args->{executing} || eval { require Term::ANSIColor } ? do {
21 my $c = \&Term::ANSIColor::color;
22 $c->('blink white on_black') . 'EXECUTING...' . $c->('reset');;
23 } : 'EXECUTING...';
a2b743ce 24 my $show_progress = defined $args->{show_progress} ? $args->{show_progress} : 1;
416cdb2e 25
26 my $sqlat = SQL::Abstract::Tree->new($args);
0d5df7d6 27 my $self = $class->next::method(@_);
a2b743ce 28 $self->_clear_line_str($clear_line);
29 $self->_executing_str($executing);
30 $self->_show_progress($show_progress);
1dc93d17 31
0d5df7d6 32 $self->_sqlat($sqlat);
1dc93d17 33
0d5df7d6 34 return $self
1dc93d17 35}
36
31756ef2 37sub print {
1dc93d17 38 my $self = shift;
39 my $string = shift;
25d4d820 40 my $bindargs = shift || [];
84c65032 41
416cdb2e 42 my ($lw, $lr);
43 ($lw, $string, $lr) = $string =~ /^(\s*)(.+?)(\s*)$/s;
44
fab0bed9 45 return if defined $bindargs && defined $bindargs->[0] &&
46 $bindargs->[0] eq q('__BULK_INSERT__');
47
84c65032 48 my $use_placeholders = !!$self->_sqlat->fill_in_placeholders;
49
50 # DBIC pre-quotes bindargs
ec02b310 51 $bindargs = [map { s/^'//; s/'$//; $_ } @{$bindargs}] if $use_placeholders;
84c65032 52
416cdb2e 53 my $formatted = $self->_sqlat->format($string, $bindargs);
1dc93d17 54
84c65032 55 $formatted = "$formatted: " . join ', ', @{$bindargs}
56 unless $use_placeholders;
1dc93d17 57
416cdb2e 58 $self->next::method("$lw$formatted$lr", @_);
1dc93d17 59}
60
84c65032 61sub query_start {
62 my ($self, $string, @bind) = @_;
63
64 if(defined $self->callback) {
65 $string =~ m/^(\w+)/;
66 $self->callback->($1, "$string: ".join(', ', @bind)."\n");
67 return;
68 }
69
416cdb2e 70 $string =~ s/\s+$//;
71
72 $self->print("$string\n", \@bind);
73
a2b743ce 74 $self->debugfh->print($self->_executing_str) if $self->_show_progress
416cdb2e 75}
76
77sub query_end {
a2b743ce 78 $_[0]->debugfh->print($_[0]->_clear_line_str) if $_[0]->_show_progress
84c65032 79}
80
1dc93d17 811;
6b1bf9f8 82
83=pod
84
85=head1 SYNOPSIS
86
87 package MyApp::Schema;
88
89 use parent 'DBIx::Class::Schema';
90
0d5df7d6 91 use DBIx::Class::Storage::Debug::PrettyPrint;
6b1bf9f8 92
93 __PACKAGE__->load_namespaces;
94
0d5df7d6 95 my $pp = DBIx::Class::Storage::Debug::PrettyPrint->new({
96 profile => 'console',
97 });
6b1bf9f8 98
99 sub connection {
0d5df7d6 100 my $self = shift;
6b1bf9f8 101
0d5df7d6 102 my $ret = $self->next::method(@_);
6b1bf9f8 103
0d5df7d6 104 $self->storage->debugobj($pp);
6b1bf9f8 105
0d5df7d6 106 $ret
6b1bf9f8 107 }
108