1 package DBIx::Class::Storage::Statistics;
6 use DBIx::Class::_Util qw(sigwarn_silencer qsub);
8 # DO NOT edit away without talking to riba first, he will just put it back
9 # BEGIN pre-Moo2 import block
11 my $initial_fatal_bits = (${^WARNING_BITS}||'') & $warnings::DeadBits{all};
13 local $ENV{PERL_STRICTURES_EXTRA} = 0;
14 # load all of these now, so that lazy-loading does not escape
15 # the current PERL_STRICTURES_EXTRA setting
20 require Method::Generate::Accessor;
21 require Method::Generate::Constructor;
24 ${^WARNING_BITS} &= ( $initial_fatal_bits | ~ $warnings::DeadBits{all} );
26 # END pre-Moo2 import block
28 extends 'DBIx::Class';
33 DBIx::Class::Storage::Statistics - SQL Statistics
39 This class is called by DBIx::Class::Storage::DBI as a means of collecting
40 statistics on its actions. Using this class alone merely prints the SQL
41 executed, the fact that it completes and begin/end notification for
44 To really use this class you should subclass it and create your own method
45 for collecting the statistics as discussed in L<DBIx::Class::Manual::Cookbook>.
51 Returns a new L<DBIx::Class::Storage::Statistics> object.
55 Sets or retrieves the filehandle used for trace/debug output. This should
56 be an L<IO::Handle> compatible object (only the
57 L<< print|IO::Handle/METHODS >> method is used). By
58 default it is initially set to STDERR - although see discussion of the
59 L<DBIC_TRACE|DBIx::Class::Storage/DBIC_TRACE> environment variable.
61 Invoked as a getter it will lazily open a filehandle and set it to
62 L<< autoflush|perlvar/HANDLE->autoflush( EXPR ) >> (if one is not
67 # FIXME - there ought to be a way to fold this into _debugfh itself
68 # having the undef re-trigger the builder (or better yet a default
69 # which can be folded in as a qsub)
73 return $self->_debugfh(@_) if @_;
74 $self->_debugfh || $self->_build_debugfh;
80 trigger => qsub '$_[0]->_defaulted_to_stderr(undef)',
81 builder => '_build_debugfh',
87 my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} || $ENV{DBIC_TRACE};
89 if (defined($debug_env) and ($debug_env =~ /=(.+)$/)) {
91 or die("Cannot open trace file $1: $!\n");
94 open ($fh, '>&STDERR')
95 or die("Duplication of STDERR for debug output failed (perhaps your STDERR is closed?): $!\n");
96 $_[0]->_defaulted_to_stderr(1);
104 has [qw(_defaulted_to_stderr silence callback)] => (
110 Prints the specified string to our debugging filehandle. Provided to save our
111 methods the worry of how to display the message.
115 my ($self, $msg) = @_;
117 return if $self->silence;
119 my $fh = $self->debugfh;
121 # not using 'no warnings' here because all of this can change at runtime
122 local $SIG{__WARN__} = sigwarn_silencer(qr/^Wide character in print/)
123 if $self->_defaulted_to_stderr;
130 Turn off all output if set to true.
134 Called when a transaction begins.
140 return if $self->callback;
142 $self->print("BEGIN WORK\n");
147 Called when a transaction is rolled back.
153 return if $self->callback;
155 $self->print("ROLLBACK\n");
160 Called when a transaction is committed.
166 return if $self->callback;
168 $self->print("COMMIT\n");
173 Called when a savepoint is created.
177 my ($self, $name) = @_;
179 return if $self->callback;
181 $self->print("SAVEPOINT $name\n");
186 Called when a savepoint is released.
190 my ($self, $name) = @_;
192 return if $self->callback;
194 $self->print("RELEASE SAVEPOINT $name\n");
199 Called when rolling back to a savepoint.
203 my ($self, $name) = @_;
205 return if $self->callback;
207 $self->print("ROLLBACK TO SAVEPOINT $name\n");
212 Called before a query is executed. The first argument is the SQL string being
213 executed and subsequent arguments are the parameters used for the query.
217 my ($self, $string, @bind) = @_;
219 my $message = "$string: ".join(', ', @bind)."\n";
221 if(defined($self->callback)) {
222 $string =~ m/^(\w+)/;
223 $self->callback->($1, $message);
227 $self->print($message);
232 Called when a query finishes executing. Has the same arguments as query_start.
236 my ($self, $string) = @_;
241 =head1 AUTHOR AND CONTRIBUTORS
243 See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
247 You may distribute this code under the same terms as Perl itself.