Commit | Line | Data |
0d5df7d6 |
1 | package DBIx::Class::Storage::Debug::PrettyPrint; |
1dc93d17 |
2 | |
84c65032 |
3 | use strict; |
4 | use warnings; |
5 | |
1dc93d17 |
6 | use base 'DBIx::Class::Storage::Statistics'; |
7 | |
8 | use SQL::Abstract::Tree; |
9 | |
10 | __PACKAGE__->mk_group_accessors( simple => '_sqlat' ); |
11 | |
12 | sub 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 |
23 | sub print { |
1dc93d17 |
24 | my $self = shift; |
25 | my $string = shift; |
25d4d820 |
26 | my $bindargs = shift || []; |
84c65032 |
27 | |
28 | my $use_placeholders = !!$self->_sqlat->fill_in_placeholders; |
29 | |
30 | # DBIC pre-quotes bindargs |
31 | $bindargs = [map { s/^'//; s/'$//; } @{$bindargs}] if $use_placeholders; |
32 | |
33 | my $formatted = $self->_sqlat->format($string, $bindargs) . "\n"; |
1dc93d17 |
34 | |
84c65032 |
35 | $formatted = "$formatted: " . join ', ', @{$bindargs} |
36 | unless $use_placeholders; |
1dc93d17 |
37 | |
38 | $self->next::method($formatted, @_); |
39 | } |
40 | |
84c65032 |
41 | sub query_start { |
42 | my ($self, $string, @bind) = @_; |
43 | |
44 | if(defined $self->callback) { |
45 | $string =~ m/^(\w+)/; |
46 | $self->callback->($1, "$string: ".join(', ', @bind)."\n"); |
47 | return; |
48 | } |
49 | |
50 | $self->print($string, \@bind); |
51 | } |
52 | |
1dc93d17 |
53 | 1; |
6b1bf9f8 |
54 | |
55 | =pod |
56 | |
57 | =head1 SYNOPSIS |
58 | |
59 | package MyApp::Schema; |
60 | |
61 | use parent 'DBIx::Class::Schema'; |
62 | |
0d5df7d6 |
63 | use DBIx::Class::Storage::Debug::PrettyPrint; |
6b1bf9f8 |
64 | |
65 | __PACKAGE__->load_namespaces; |
66 | |
0d5df7d6 |
67 | my $pp = DBIx::Class::Storage::Debug::PrettyPrint->new({ |
68 | profile => 'console', |
69 | }); |
6b1bf9f8 |
70 | |
71 | sub connection { |
0d5df7d6 |
72 | my $self = shift; |
6b1bf9f8 |
73 | |
0d5df7d6 |
74 | my $ret = $self->next::method(@_); |
6b1bf9f8 |
75 | |
0d5df7d6 |
76 | $self->storage->debugobj($pp); |
6b1bf9f8 |
77 | |
0d5df7d6 |
78 | $ret |
6b1bf9f8 |
79 | } |
80 | |