1 package DBIx::Class::Storage::Statistics;
6 use DBIx::Class::_Util qw(sigwarn_silencer qsub);
9 # DO NOT edit away without talking to riba first, he will just put it back
10 # BEGIN pre-Moo2 import block
12 my $initial_fatal_bits = (${^WARNING_BITS}||'') & $warnings::DeadBits{all};
14 local $ENV{PERL_STRICTURES_EXTRA} = 0;
15 # load all of these now, so that lazy-loading does not escape
16 # the current PERL_STRICTURES_EXTRA setting
21 require Method::Generate::Accessor;
22 require Method::Generate::Constructor;
25 ${^WARNING_BITS} &= ( $initial_fatal_bits | ~ $warnings::DeadBits{all} );
27 # END pre-Moo2 import block
29 extends 'DBIx::Class';
34 DBIx::Class::Storage::Statistics - SQL Statistics
40 This class is called by DBIx::Class::Storage::DBI as a means of collecting
41 statistics on its actions. Using this class alone merely prints the SQL
42 executed, the fact that it completes and begin/end notification for
45 To really use this class you should subclass it and create your own method
46 for collecting the statistics as discussed in L<DBIx::Class::Manual::Cookbook>.
52 Returns a new L<DBIx::Class::Storage::Statistics> object.
56 Sets or retrieves the filehandle used for trace/debug output. This should
57 be an L<IO::Handle> compatible object (only the
58 L<< print|IO::Handle/METHODS >> method is used). By
59 default it is initially set to STDERR - although see discussion of the
60 L<DBIC_TRACE|DBIx::Class::Storage/DBIC_TRACE> environment variable.
62 Invoked as a getter it will lazily open a filehandle and set it to
63 L<< autoflush|perlvar/HANDLE->autoflush( EXPR ) >> (if one is not
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)
74 return $self->_debugfh(@_) if @_;
75 $self->_debugfh || $self->_build_debugfh;
81 trigger => qsub '$_[0]->_defaulted_to_stderr(undef)',
82 builder => '_build_debugfh',
88 my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} || $ENV{DBIC_TRACE};
90 if (defined($debug_env) and ($debug_env =~ /=(.+)$/)) {
92 or die("Cannot open trace file $1: $!\n");
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);
105 has [qw(_defaulted_to_stderr silence callback)] => (
111 Prints the specified string to our debugging filehandle. Provided to save our
112 methods the worry of how to display the message.
116 my ($self, $msg) = @_;
118 return if $self->silence;
120 my $fh = $self->debugfh;
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;
131 Turn off all output if set to true.
135 Called when a transaction begins.
141 return if $self->callback;
143 $self->print("BEGIN WORK\n");
148 Called when a transaction is rolled back.
154 return if $self->callback;
156 $self->print("ROLLBACK\n");
161 Called when a transaction is committed.
167 return if $self->callback;
169 $self->print("COMMIT\n");
174 Called when a savepoint is created.
178 my ($self, $name) = @_;
180 return if $self->callback;
182 $self->print("SAVEPOINT $name\n");
187 Called when a savepoint is released.
191 my ($self, $name) = @_;
193 return if $self->callback;
195 $self->print("RELEASE SAVEPOINT $name\n");
200 Called when rolling back to a savepoint.
204 my ($self, $name) = @_;
206 return if $self->callback;
208 $self->print("ROLLBACK TO SAVEPOINT $name\n");
213 Called before a query is executed. The first argument is the SQL string being
214 executed and subsequent arguments are the parameters used for the query.
218 my ($self, $string, @bind) = @_;
220 my $message = "$string: ".join(', ', @bind)."\n";
222 if(defined($self->callback)) {
223 $string =~ m/^(\w+)/;
224 $self->callback->($1, $message);
228 $self->print($message);
233 Called when a query finishes executing. Has the same arguments as query_start.
238 my ($self, $string) = @_;
241 =head1 FURTHER QUESTIONS?
243 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
245 =head1 COPYRIGHT AND LICENSE
247 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
248 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
249 redistribute it and/or modify it under the same terms as the
250 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.