1 package DBIx::Class::Storage::Statistics;
5 use base qw/DBIx::Class/;
9 __PACKAGE__->mk_group_accessors(simple => qw/callback _debugfh silence/);
13 DBIx::Class::Storage::Statistics - SQL Statistics
19 This class is called by DBIx::Class::Storage::DBI as a means of collecting
20 statistics on its actions. Using this class alone merely prints the SQL
21 executed, the fact that it completes and begin/end notification for
24 To really use this class you should subclass it and create your own method
25 for collecting the statistics as discussed in L<DBIx::Class::Manual::Cookbook>.
33 Returns a new L<DBIx::Class::Storage::Statistics> object.
38 bless $self, (ref($_[0]) || $_[0]);
45 Sets or retrieves the filehandle used for trace/debug output. This should
46 be an IO::Handle compatible object (only the C<print> method is used). Initially
47 should be set to STDERR - although see information on the
48 L<DBIC_TRACE> environment variable.
50 As getter it will lazily open a filehandle for you if one is not already set.
58 $self->_debugfh($_[0]);
59 } elsif (!defined($self->_debugfh())) {
61 my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}
63 if (defined($debug_env) && ($debug_env =~ /=(.+)$/)) {
64 $fh = IO::File->new($1, 'a')
65 or die("Cannot open trace file $1");
67 $fh = IO::File->new('>&STDERR')
68 or die('Duplication of STDERR for debug output failed (perhaps your STDERR is closed?)');
80 Prints the specified string to our debugging filehandle. Provided to save our
81 methods the worry of how to display the message.
85 my ($self, $msg) = @_;
87 return if $self->silence;
89 $self->debugfh->print($msg);
94 Turn off all output if set to true.
98 Called when a transaction begins.
104 return if $self->callback;
106 $self->print("BEGIN WORK\n");
111 Called when a transaction is rolled back.
117 return if $self->callback;
119 $self->print("ROLLBACK\n");
124 Called when a transaction is committed.
130 return if $self->callback;
132 $self->print("COMMIT\n");
137 Called when a savepoint is created.
141 my ($self, $name) = @_;
143 return if $self->callback;
145 $self->print("SAVEPOINT $name\n");
150 Called when a savepoint is released.
154 my ($self, $name) = @_;
156 return if $self->callback;
158 $self->print("RELEASE SAVEPOINT $name\n");
163 Called when rolling back to a savepoint.
167 my ($self, $name) = @_;
169 return if $self->callback;
171 $self->print("ROLLBACK TO SAVEPOINT $name\n");
176 Called before a query is executed. The first argument is the SQL string being
177 executed and subsequent arguments are the parameters used for the query.
181 my ($self, $string, @bind) = @_;
183 my $message = "$string: ".join(', ', @bind)."\n";
185 if(defined($self->callback)) {
186 $string =~ m/^(\w+)/;
187 $self->callback->($1, $message);
191 $self->print($message);
196 Called when a query finishes executing. Has the same arguments as query_start.
200 my ($self, $string) = @_;
207 Cory G. Watson <gphat@cpan.org>
211 You may distribute this code under the same license as Perl itself.