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 IO::Handle compatible object (only the C<print> method is used). Initially
46 should be set to STDERR - although see information on the
47 L<DBIC_TRACE> environment variable.
49 As getter it will lazily open a filehandle for you if one is not already set.
53 # FIXME - there ought to be a way to fold this into _debugfh itself
54 # having the undef re-trigger the builder (or better yet a default
55 # which can be folded in as a qsub)
59 return $self->_debugfh(@_) if @_;
60 $self->_debugfh || $self->_build_debugfh;
66 trigger => qsub '$_[0]->_defaulted_to_stderr(undef)',
67 builder => '_build_debugfh',
73 my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} || $ENV{DBIC_TRACE};
75 if (defined($debug_env) and ($debug_env =~ /=(.+)$/)) {
77 or die("Cannot open trace file $1: $!\n");
80 open ($fh, '>&STDERR')
81 or die("Duplication of STDERR for debug output failed (perhaps your STDERR is closed?): $!\n");
82 $_[0]->_defaulted_to_stderr(1);
88 has [qw(_defaulted_to_stderr silence callback)] => (
94 Prints the specified string to our debugging filehandle. Provided to save our
95 methods the worry of how to display the message.
99 my ($self, $msg) = @_;
101 return if $self->silence;
103 my $fh = $self->debugfh;
105 # not using 'no warnings' here because all of this can change at runtime
106 local $SIG{__WARN__} = sigwarn_silencer(qr/^Wide character in print/)
107 if $self->_defaulted_to_stderr;
109 $fh->printflush($msg);
114 Turn off all output if set to true.
118 Called when a transaction begins.
124 return if $self->callback;
126 $self->print("BEGIN WORK\n");
131 Called when a transaction is rolled back.
137 return if $self->callback;
139 $self->print("ROLLBACK\n");
144 Called when a transaction is committed.
150 return if $self->callback;
152 $self->print("COMMIT\n");
157 Called when a savepoint is created.
161 my ($self, $name) = @_;
163 return if $self->callback;
165 $self->print("SAVEPOINT $name\n");
170 Called when a savepoint is released.
174 my ($self, $name) = @_;
176 return if $self->callback;
178 $self->print("RELEASE SAVEPOINT $name\n");
183 Called when rolling back to a savepoint.
187 my ($self, $name) = @_;
189 return if $self->callback;
191 $self->print("ROLLBACK TO SAVEPOINT $name\n");
196 Called before a query is executed. The first argument is the SQL string being
197 executed and subsequent arguments are the parameters used for the query.
201 my ($self, $string, @bind) = @_;
203 my $message = "$string: ".join(', ', @bind)."\n";
205 if(defined($self->callback)) {
206 $string =~ m/^(\w+)/;
207 $self->callback->($1, $message);
211 $self->print($message);
216 Called when a query finishes executing. Has the same arguments as query_start.
220 my ($self, $string) = @_;
225 =head1 AUTHOR AND CONTRIBUTORS
227 See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
231 You may distribute this code under the same terms as Perl itself.