1 package DBIx::Class::Storage::Statistics;
5 # DO NOT edit away without talking to riba first, he will just put it back
6 # BEGIN pre-Moo2 import block
9 my $initial_fatal_bits = (${^WARNING_BITS}||'') & $warnings::DeadBits{all};
10 local $ENV{PERL_STRICTURES_EXTRA} = 0;
11 require Moo; Moo->import;
12 require Sub::Quote; Sub::Quote->import('quote_sub');
13 ${^WARNING_BITS} &= ( $initial_fatal_bits | ~ $warnings::DeadBits{all} );
15 # END pre-Moo2 import block
17 extends 'DBIx::Class';
18 use DBIx::Class::_Util 'sigwarn_silencer';
23 DBIx::Class::Storage::Statistics - SQL Statistics
29 This class is called by DBIx::Class::Storage::DBI as a means of collecting
30 statistics on its actions. Using this class alone merely prints the SQL
31 executed, the fact that it completes and begin/end notification for
34 To really use this class you should subclass it and create your own method
35 for collecting the statistics as discussed in L<DBIx::Class::Manual::Cookbook>.
41 Returns a new L<DBIx::Class::Storage::Statistics> object.
45 Sets or retrieves the filehandle used for trace/debug output. This should
46 be an IO::Handle compatible object (only the C<print> method is used). Initially
47 should be set to STDERR - although see information on the
48 L<DBIC_TRACE> environment variable.
50 As getter it will lazily open a filehandle for you if one is not already set.
54 # FIXME - there ought to be a way to fold this into _debugfh itself
55 # having the undef re-trigger the builder (or better yet a default
56 # which can be folded in as a qsub)
60 return $self->_debugfh(@_) if @_;
61 $self->_debugfh || $self->_build_debugfh;
67 trigger => quote_sub( '$_[0]->_defaulted_to_stderr(undef)' ),
68 builder => '_build_debugfh',
74 my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} || $ENV{DBIC_TRACE};
76 if (defined($debug_env) and ($debug_env =~ /=(.+)$/)) {
78 or die("Cannot open trace file $1: $!\n");
81 open ($fh, '>&STDERR')
82 or die("Duplication of STDERR for debug output failed (perhaps your STDERR is closed?): $!\n");
83 $_[0]->_defaulted_to_stderr(1);
89 has [qw(_defaulted_to_stderr silence callback)] => (
95 Prints the specified string to our debugging filehandle. Provided to save our
96 methods the worry of how to display the message.
100 my ($self, $msg) = @_;
102 return if $self->silence;
104 my $fh = $self->debugfh;
106 # not using 'no warnings' here because all of this can change at runtime
107 local $SIG{__WARN__} = sigwarn_silencer(qr/^Wide character in print/)
108 if $self->_defaulted_to_stderr;
110 $fh->printflush($msg);
115 Turn off all output if set to true.
119 Called when a transaction begins.
125 return if $self->callback;
127 $self->print("BEGIN WORK\n");
132 Called when a transaction is rolled back.
138 return if $self->callback;
140 $self->print("ROLLBACK\n");
145 Called when a transaction is committed.
151 return if $self->callback;
153 $self->print("COMMIT\n");
158 Called when a savepoint is created.
162 my ($self, $name) = @_;
164 return if $self->callback;
166 $self->print("SAVEPOINT $name\n");
171 Called when a savepoint is released.
175 my ($self, $name) = @_;
177 return if $self->callback;
179 $self->print("RELEASE SAVEPOINT $name\n");
184 Called when rolling back to a savepoint.
188 my ($self, $name) = @_;
190 return if $self->callback;
192 $self->print("ROLLBACK TO SAVEPOINT $name\n");
197 Called before a query is executed. The first argument is the SQL string being
198 executed and subsequent arguments are the parameters used for the query.
202 my ($self, $string, @bind) = @_;
204 my $message = "$string: ".join(', ', @bind)."\n";
206 if(defined($self->callback)) {
207 $string =~ m/^(\w+)/;
208 $self->callback->($1, $message);
212 $self->print($message);
217 Called when a query finishes executing. Has the same arguments as query_start.
221 my ($self, $string) = @_;
226 =head1 AUTHOR AND CONTRIBUTORS
228 See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
232 You may distribute this code under the same terms as Perl itself.