1 package DBIx::Class::Storage::Statistics;
6 use DBIx::Class::_Util qw(sigwarn_silencer qsub);
14 DBIx::Class::Storage::Statistics - SQL Statistics
20 This class is called by DBIx::Class::Storage::DBI as a means of collecting
21 statistics on its actions. Using this class alone merely prints the SQL
22 executed, the fact that it completes and begin/end notification for
25 To really use this class you should subclass it and create your own method
26 for collecting the statistics as discussed in L<DBIx::Class::Manual::Cookbook>.
32 Returns a new L<DBIx::Class::Storage::Statistics> object.
36 Sets or retrieves the filehandle used for trace/debug output. This should
37 be an L<IO::Handle> compatible object (only the
38 L<< print|IO::Handle/METHODS >> method is used). By
39 default it is initially set to STDERR - although see discussion of the
40 L<DBIC_TRACE|DBIx::Class::Storage/DBIC_TRACE> environment variable.
42 Invoked as a getter it will lazily open a filehandle and set it to
43 L<< autoflush|perlvar/HANDLE->autoflush( EXPR ) >> (if one is not
51 trigger => qsub '$_[0]->_defaulted_to_stderr(undef); $_[0]->_clear_debugfh unless $_[1];',
52 clearer => '_clear_debugfh',
53 builder => '_build_debugfh',
59 my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} || $ENV{DBIC_TRACE};
61 if (defined($debug_env) and ($debug_env =~ /=(.+)$/)) {
63 or die("Cannot open trace file $1: $!\n");
66 open ($fh, '>&STDERR')
67 or die("Duplication of STDERR for debug output failed (perhaps your STDERR is closed?): $!\n");
68 $_[0]->_defaulted_to_stderr(1);
76 has [qw(_defaulted_to_stderr silence callback)] => (
82 Prints the specified string to our debugging filehandle. Provided to save our
83 methods the worry of how to display the message.
87 my ($self, $msg) = @_;
89 return if $self->silence;
91 my $fh = $self->debugfh;
93 # not using 'no warnings' here because all of this can change at runtime
94 local $SIG{__WARN__} = sigwarn_silencer(qr/^Wide character in print/)
95 if $self->_defaulted_to_stderr;
102 Turn off all output if set to true.
106 Called when a transaction begins.
112 return if $self->callback;
114 $self->print("BEGIN WORK\n");
119 Called when a transaction is rolled back.
125 return if $self->callback;
127 $self->print("ROLLBACK\n");
132 Called when a transaction is committed.
138 return if $self->callback;
140 $self->print("COMMIT\n");
145 Called when a savepoint is created.
149 my ($self, $name) = @_;
151 return if $self->callback;
153 $self->print("SAVEPOINT $name\n");
158 Called when a savepoint is released.
162 my ($self, $name) = @_;
164 return if $self->callback;
166 $self->print("RELEASE SAVEPOINT $name\n");
171 Called when rolling back to a savepoint.
175 my ($self, $name) = @_;
177 return if $self->callback;
179 $self->print("ROLLBACK TO SAVEPOINT $name\n");
184 Called before a query is executed. The first argument is the SQL string being
185 executed and subsequent arguments are the parameters used for the query.
189 my ($self, $string, @bind) = @_;
191 my $message = "$string: ".join(', ', @bind)."\n";
193 if(defined($self->callback)) {
194 $string =~ m/^(\w+)/;
195 $self->callback->($1, $message);
199 $self->print($message);
204 Called when a query finishes executing. Has the same arguments as query_start.
209 my ($self, $string) = @_;
212 =head1 FURTHER QUESTIONS?
214 Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>.
216 =head1 COPYRIGHT AND LICENSE
218 This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE>
219 by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can
220 redistribute it and/or modify it under the same terms as the
221 L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>.