Bump deps to pull in bugfixes
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / Statistics.pm
CommitLineData
4c248161 1package DBIx::Class::Storage::Statistics;
7f9a3f70 2
4c248161 3use strict;
aaba9524 4use warnings;
4c248161 5
cbd7f87a 6use DBIx::Class::_Util qw(sigwarn_silencer qsub);
924a474d 7use IO::Handle ();
cbd7f87a 8
68b8ba54 9# DO NOT edit away without talking to riba first, he will just put it back
10# BEGIN pre-Moo2 import block
11BEGIN {
68b8ba54 12 my $initial_fatal_bits = (${^WARNING_BITS}||'') & $warnings::DeadBits{all};
cbd7f87a 13
68b8ba54 14 local $ENV{PERL_STRICTURES_EXTRA} = 0;
cbd7f87a 15 # load all of these now, so that lazy-loading does not escape
16 # the current PERL_STRICTURES_EXTRA setting
17 require Sub::Quote;
18 require Sub::Defer;
19 require Moo;
20 require Moo::Object;
21 require Method::Generate::Accessor;
22 require Method::Generate::Constructor;
23
24 Moo->import;
68b8ba54 25 ${^WARNING_BITS} &= ( $initial_fatal_bits | ~ $warnings::DeadBits{all} );
26}
27# END pre-Moo2 import block
28
29extends 'DBIx::Class';
9c1700e3 30use namespace::clean;
3e110410 31
4c248161 32=head1 NAME
33
34DBIx::Class::Storage::Statistics - SQL Statistics
35
36=head1 SYNOPSIS
37
38=head1 DESCRIPTION
39
40This class is called by DBIx::Class::Storage::DBI as a means of collecting
faaba25f 41statistics on its actions. Using this class alone merely prints the SQL
4c248161 42executed, the fact that it completes and begin/end notification for
43transactions.
44
45To really use this class you should subclass it and create your own method
46for collecting the statistics as discussed in L<DBIx::Class::Manual::Cookbook>.
47
48=head1 METHODS
49
4c248161 50=head2 new
51
52Returns a new L<DBIx::Class::Storage::Statistics> object.
53
4c248161 54=head2 debugfh
55
56Sets or retrieves the filehandle used for trace/debug output. This should
4d93345c 57be an L<IO::Handle> compatible object (only the
8494142c 58L<< print|IO::Handle/METHODS >> method is used). By
4d93345c 59default it is initially set to STDERR - although see discussion of the
60L<DBIC_TRACE|DBIx::Class::Storage/DBIC_TRACE> environment variable.
4c248161 61
8494142c 62Invoked as a getter it will lazily open a filehandle and set it to
63L<< autoflush|perlvar/HANDLE->autoflush( EXPR ) >> (if one is not
64already set).
70f39278 65
66=cut
70f39278 67
68b8ba54 68# FIXME - there ought to be a way to fold this into _debugfh itself
69# having the undef re-trigger the builder (or better yet a default
70# which can be folded in as a qsub)
c6fa3170 71sub debugfh {
72 my $self = shift;
9901aad7 73
68b8ba54 74 return $self->_debugfh(@_) if @_;
75 $self->_debugfh || $self->_build_debugfh;
76}
77
78has _debugfh => (
79 is => 'rw',
80 lazy => 1,
7f9a3f70 81 trigger => qsub '$_[0]->_defaulted_to_stderr(undef)',
68b8ba54 82 builder => '_build_debugfh',
83);
84
85sub _build_debugfh {
86 my $fh;
87
88 my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} || $ENV{DBIC_TRACE};
89
90 if (defined($debug_env) and ($debug_env =~ /=(.+)$/)) {
91 open ($fh, '>>', $1)
92 or die("Cannot open trace file $1: $!\n");
93 }
94 else {
95 open ($fh, '>&STDERR')
96 or die("Duplication of STDERR for debug output failed (perhaps your STDERR is closed?): $!\n");
97 $_[0]->_defaulted_to_stderr(1);
70f39278 98 }
99
8494142c 100 $fh->autoflush(1);
101
68b8ba54 102 $fh;
c6fa3170 103}
104
68b8ba54 105has [qw(_defaulted_to_stderr silence callback)] => (
106 is => 'rw',
107);
108
c6fa3170 109=head2 print
110
111Prints the specified string to our debugging filehandle. Provided to save our
112methods the worry of how to display the message.
113
114=cut
115sub print {
116 my ($self, $msg) = @_;
117
118 return if $self->silence;
119
9d522a4e 120 my $fh = $self->debugfh;
121
122 # not using 'no warnings' here because all of this can change at runtime
123 local $SIG{__WARN__} = sigwarn_silencer(qr/^Wide character in print/)
124 if $self->_defaulted_to_stderr;
125
8494142c 126 $fh->print($msg);
70f39278 127}
128
dcdf7b2c 129=head2 silence
130
131Turn off all output if set to true.
132
4c248161 133=head2 txn_begin
134
135Called when a transaction begins.
136
137=cut
138sub txn_begin {
04cf5bbf 139 my $self = shift;
d2075431 140
b94139c0 141 return if $self->callback;
142
70f39278 143 $self->print("BEGIN WORK\n");
4c248161 144}
145
146=head2 txn_rollback
147
148Called when a transaction is rolled back.
149
150=cut
151sub txn_rollback {
04cf5bbf 152 my $self = shift;
d2075431 153
b94139c0 154 return if $self->callback;
155
70f39278 156 $self->print("ROLLBACK\n");
4c248161 157}
158
159=head2 txn_commit
160
161Called when a transaction is committed.
162
163=cut
164sub txn_commit {
04cf5bbf 165 my $self = shift;
d2075431 166
b94139c0 167 return if $self->callback;
168
70f39278 169 $self->print("COMMIT\n");
4c248161 170}
171
adb3554a 172=head2 svp_begin
173
174Called when a savepoint is created.
175
176=cut
177sub svp_begin {
178 my ($self, $name) = @_;
179
b94139c0 180 return if $self->callback;
181
adb3554a 182 $self->print("SAVEPOINT $name\n");
183}
184
185=head2 svp_release
186
187Called when a savepoint is released.
188
189=cut
8432aeca 190sub svp_release {
adb3554a 191 my ($self, $name) = @_;
192
b94139c0 193 return if $self->callback;
194
195 $self->print("RELEASE SAVEPOINT $name\n");
adb3554a 196}
197
198=head2 svp_rollback
199
200Called when rolling back to a savepoint.
201
202=cut
203sub svp_rollback {
204 my ($self, $name) = @_;
205
b94139c0 206 return if $self->callback;
207
208 $self->print("ROLLBACK TO SAVEPOINT $name\n");
adb3554a 209}
210
4c248161 211=head2 query_start
212
213Called before a query is executed. The first argument is the SQL string being
214executed and subsequent arguments are the parameters used for the query.
215
216=cut
217sub query_start {
04cf5bbf 218 my ($self, $string, @bind) = @_;
68fcff2f 219
04cf5bbf 220 my $message = "$string: ".join(', ', @bind)."\n";
4c248161 221
04cf5bbf 222 if(defined($self->callback)) {
223 $string =~ m/^(\w+)/;
1b7fb46e 224 $self->callback->($1, $message);
04cf5bbf 225 return;
226 }
4c248161 227
70f39278 228 $self->print($message);
4c248161 229}
230
231=head2 query_end
232
233Called when a query finishes executing. Has the same arguments as query_start.
234
235=cut
a2bd3796 236
4c248161 237sub query_end {
04cf5bbf 238 my ($self, $string) = @_;
4c248161 239}
240
a2bd3796 241=head1 FURTHER QUESTIONS?
4c248161 242
a2bd3796 243Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
4c248161 244
a2bd3796 245=head1 COPYRIGHT AND LICENSE
4c248161 246
a2bd3796 247This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
248by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
249redistribute it and/or modify it under the same terms as the
250L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.
4c248161 251
252=cut
a2bd3796 253
2541;