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);
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 L<IO::Handle> compatible object (only the
47 L<< printflush|IO::Handle/$io->printflush_(_ARGS_) >> method is used). By
48 default it is initially set to STDERR - although see discussion of the
49 L<DBIC_TRACE|DBIx::Class::Storage/DBIC_TRACE> environment variable.
51 Invoked as a getter it will lazily open a filehandle for you 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);
91 has [qw(_defaulted_to_stderr silence callback)] => (
97 Prints the specified string to our debugging filehandle. Provided to save our
98 methods the worry of how to display the message.
102 my ($self, $msg) = @_;
104 return if $self->silence;
106 my $fh = $self->debugfh;
108 # not using 'no warnings' here because all of this can change at runtime
109 local $SIG{__WARN__} = sigwarn_silencer(qr/^Wide character in print/)
110 if $self->_defaulted_to_stderr;
112 $fh->printflush($msg);
117 Turn off all output if set to true.
121 Called when a transaction begins.
127 return if $self->callback;
129 $self->print("BEGIN WORK\n");
134 Called when a transaction is rolled back.
140 return if $self->callback;
142 $self->print("ROLLBACK\n");
147 Called when a transaction is committed.
153 return if $self->callback;
155 $self->print("COMMIT\n");
160 Called when a savepoint is created.
164 my ($self, $name) = @_;
166 return if $self->callback;
168 $self->print("SAVEPOINT $name\n");
173 Called when a savepoint is released.
177 my ($self, $name) = @_;
179 return if $self->callback;
181 $self->print("RELEASE SAVEPOINT $name\n");
186 Called when rolling back to a savepoint.
190 my ($self, $name) = @_;
192 return if $self->callback;
194 $self->print("ROLLBACK TO SAVEPOINT $name\n");
199 Called before a query is executed. The first argument is the SQL string being
200 executed and subsequent arguments are the parameters used for the query.
204 my ($self, $string, @bind) = @_;
206 my $message = "$string: ".join(', ', @bind)."\n";
208 if(defined($self->callback)) {
209 $string =~ m/^(\w+)/;
210 $self->callback->($1, $message);
214 $self->print($message);
219 Called when a query finishes executing. Has the same arguments as query_start.
223 my ($self, $string) = @_;
228 =head1 AUTHOR AND CONTRIBUTORS
230 See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
234 You may distribute this code under the same terms as Perl itself.