1 package DBIx::Class::Storage::Statistics;
6 # DO NOT edit away without talking to riba first, he will just put it back
7 # 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 ${^WARNING_BITS} &= ( $initial_fatal_bits | ~ $warnings::DeadBits{all} );
14 # END pre-Moo2 import block
16 extends 'DBIx::Class';
17 use DBIx::Class::_Util qw(sigwarn_silencer qsub);
22 DBIx::Class::Storage::Statistics - SQL Statistics
28 This class is called by DBIx::Class::Storage::DBI as a means of collecting
29 statistics on its actions. Using this class alone merely prints the SQL
30 executed, the fact that it completes and begin/end notification for
33 To really use this class you should subclass it and create your own method
34 for collecting the statistics as discussed in L<DBIx::Class::Manual::Cookbook>.
40 Returns a new L<DBIx::Class::Storage::Statistics> object.
44 Sets or retrieves the filehandle used for trace/debug output. This should
45 be an L<IO::Handle> compatible object (only the
46 L<< print|IO::Handle/METHODS >> method is used). By
47 default it is initially set to STDERR - although see discussion of the
48 L<DBIC_TRACE|DBIx::Class::Storage/DBIC_TRACE> environment variable.
50 Invoked as a getter it will lazily open a filehandle and set it to
51 L<< autoflush|perlvar/HANDLE->autoflush( EXPR ) >> (if one is not
56 # FIXME - there ought to be a way to fold this into _debugfh itself
57 # having the undef re-trigger the builder (or better yet a default
58 # which can be folded in as a qsub)
62 return $self->_debugfh(@_) if @_;
63 $self->_debugfh || $self->_build_debugfh;
69 trigger => qsub '$_[0]->_defaulted_to_stderr(undef)',
70 builder => '_build_debugfh',
76 my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} || $ENV{DBIC_TRACE};
78 if (defined($debug_env) and ($debug_env =~ /=(.+)$/)) {
80 or die("Cannot open trace file $1: $!\n");
83 open ($fh, '>&STDERR')
84 or die("Duplication of STDERR for debug output failed (perhaps your STDERR is closed?): $!\n");
85 $_[0]->_defaulted_to_stderr(1);
93 has [qw(_defaulted_to_stderr silence callback)] => (
99 Prints the specified string to our debugging filehandle. Provided to save our
100 methods the worry of how to display the message.
104 my ($self, $msg) = @_;
106 return if $self->silence;
108 my $fh = $self->debugfh;
110 # not using 'no warnings' here because all of this can change at runtime
111 local $SIG{__WARN__} = sigwarn_silencer(qr/^Wide character in print/)
112 if $self->_defaulted_to_stderr;
119 Turn off all output if set to true.
123 Called when a transaction begins.
129 return if $self->callback;
131 $self->print("BEGIN WORK\n");
136 Called when a transaction is rolled back.
142 return if $self->callback;
144 $self->print("ROLLBACK\n");
149 Called when a transaction is committed.
155 return if $self->callback;
157 $self->print("COMMIT\n");
162 Called when a savepoint is created.
166 my ($self, $name) = @_;
168 return if $self->callback;
170 $self->print("SAVEPOINT $name\n");
175 Called when a savepoint is released.
179 my ($self, $name) = @_;
181 return if $self->callback;
183 $self->print("RELEASE SAVEPOINT $name\n");
188 Called when rolling back to a savepoint.
192 my ($self, $name) = @_;
194 return if $self->callback;
196 $self->print("ROLLBACK TO SAVEPOINT $name\n");
201 Called before a query is executed. The first argument is the SQL string being
202 executed and subsequent arguments are the parameters used for the query.
206 my ($self, $string, @bind) = @_;
208 my $message = "$string: ".join(', ', @bind)."\n";
210 if(defined($self->callback)) {
211 $string =~ m/^(\w+)/;
212 $self->callback->($1, $message);
216 $self->print($message);
221 Called when a query finishes executing. Has the same arguments as query_start.
225 my ($self, $string) = @_;
230 =head1 AUTHOR AND CONTRIBUTORS
232 See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
236 You may distribute this code under the same terms as Perl itself.