Hide bulk inserts from DBIx::Class
[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' );
11
12sub new {
0d5df7d6 13 my $class = shift;
1dc93d17 14
0d5df7d6 15 my $sqlat = SQL::Abstract::Tree->new(shift @_);
16 my $self = $class->next::method(@_);
1dc93d17 17
0d5df7d6 18 $self->_sqlat($sqlat);
1dc93d17 19
0d5df7d6 20 return $self
1dc93d17 21}
22
31756ef2 23sub print {
1dc93d17 24 my $self = shift;
25 my $string = shift;
25d4d820 26 my $bindargs = shift || [];
84c65032 27
fab0bed9 28 return if defined $bindargs && defined $bindargs->[0] &&
29 $bindargs->[0] eq q('__BULK_INSERT__');
30
84c65032 31 my $use_placeholders = !!$self->_sqlat->fill_in_placeholders;
32
33 # DBIC pre-quotes bindargs
ec02b310 34 $bindargs = [map { s/^'//; s/'$//; $_ } @{$bindargs}] if $use_placeholders;
84c65032 35
36 my $formatted = $self->_sqlat->format($string, $bindargs) . "\n";
1dc93d17 37
84c65032 38 $formatted = "$formatted: " . join ', ', @{$bindargs}
39 unless $use_placeholders;
1dc93d17 40
41 $self->next::method($formatted, @_);
42}
43
84c65032 44sub query_start {
45 my ($self, $string, @bind) = @_;
46
47 if(defined $self->callback) {
48 $string =~ m/^(\w+)/;
49 $self->callback->($1, "$string: ".join(', ', @bind)."\n");
50 return;
51 }
52
53 $self->print($string, \@bind);
54}
55
1dc93d17 561;
6b1bf9f8 57
58=pod
59
60=head1 SYNOPSIS
61
62 package MyApp::Schema;
63
64 use parent 'DBIx::Class::Schema';
65
0d5df7d6 66 use DBIx::Class::Storage::Debug::PrettyPrint;
6b1bf9f8 67
68 __PACKAGE__->load_namespaces;
69
0d5df7d6 70 my $pp = DBIx::Class::Storage::Debug::PrettyPrint->new({
71 profile => 'console',
72 });
6b1bf9f8 73
74 sub connection {
0d5df7d6 75 my $self = shift;
6b1bf9f8 76
0d5df7d6 77 my $ret = $self->next::method(@_);
6b1bf9f8 78
0d5df7d6 79 $self->storage->debugobj($pp);
6b1bf9f8 80
0d5df7d6 81 $ret
6b1bf9f8 82 }
83