1 package DBIx::Class::Storage::Debug::PrettyPrint;
6 use base 'DBIx::Class::Storage::Statistics';
8 use SQL::Abstract::Tree;
10 __PACKAGE__->mk_group_accessors( simple => '_sqlat' );
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' );
14 __PACKAGE__->mk_group_accessors( simple => '_last_sql' );
15 __PACKAGE__->mk_group_accessors( simple => 'squash_repeats' );
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');;
26 my $show_progress = defined $args->{show_progress} ? $args->{show_progress} : 1;
28 my $squash_repeats = $args->{squash_repeats};
29 my $sqlat = SQL::Abstract::Tree->new($args);
30 my $self = $class->next::method(@_);
31 $self->_clear_line_str($clear_line);
32 $self->_executing_str($executing);
33 $self->_show_progress($show_progress);
35 $self->squash_repeats($squash_repeats);
37 $self->_sqlat($sqlat);
46 my $bindargs = shift || [];
49 ($lw, $string, $lr) = $string =~ /^(\s*)(.+?)(\s*)$/s;
51 local $self->_sqlat->{fill_in_placeholders} = 0 if defined $bindargs
52 && defined $bindargs->[0] && $bindargs->[0] eq q('__BULK_INSERT__');
54 my $use_placeholders = !!$self->_sqlat->fill_in_placeholders;
56 # DBIC pre-quotes bindargs
57 $bindargs = [map { s/^'//; s/'$//; $_ } @{$bindargs}] if $use_placeholders;
59 my $sqlat = $self->_sqlat;
61 if ($self->squash_repeats && $self->_last_sql eq $string) {
62 my ( $l, $r ) = @{ $sqlat->placeholder_surround };
63 $formatted = '... : ' . join(', ', map "$l$_$r", @$bindargs) . "\n";
65 $self->_last_sql($string);
66 $formatted = $sqlat->format($string, $bindargs) . "\n";
67 $formatted = "$formatted: " . join ', ', @{$bindargs}
68 unless $use_placeholders;
71 $self->next::method("$lw$formatted$lr", @_);
75 my ($self, $string, @bind) = @_;
77 if(defined $self->callback) {
79 $self->callback->($1, "$string: ".join(', ', @bind)."\n");
85 $self->print("$string\n", \@bind);
87 $self->debugfh->print($self->_executing_str) if $self->_show_progress
91 $_[0]->debugfh->print($_[0]->_clear_line_str) if $_[0]->_show_progress
100 package MyApp::Schema;
102 use parent 'DBIx::Class::Schema';
104 use DBIx::Class::Storage::Debug::PrettyPrint;
106 __PACKAGE__->load_namespaces;
108 my $pp = DBIx::Class::Storage::Debug::PrettyPrint->new({
109 profile => 'console',
115 my $ret = $self->next::method(@_);
117 $self->storage->debugobj($pp);