1 package DBIx::Class::Storage::Statistics;
5 use base qw/Class::Accessor::Grouped/;
7 __PACKAGE__->mk_group_accessors(simple => qw/callback debugfh/);
11 DBIx::Class::Storage::Statistics - SQL Statistics
17 This class is called by DBIx::Class::Storage::DBI as a means of collecting
18 statistics on it's actions. Using this class alone merely prints the SQL
19 executed, the fact that it completes and begin/end notification for
22 To really use this class you should subclass it and create your own method
23 for collecting the statistics as discussed in L<DBIx::Class::Manual::Cookbook>.
31 Returns a new L<DBIx::Class::Storage::Statistics> object.
36 bless $self, (ref($_[0]) || $_[0]);
43 Sets or retrieves the filehandle used for trace/debug output. This should
44 be an IO::Handle compatible object (only the C<print> method is used). Initially
45 should be set to STDERR - although see information on the
46 L<DBIC_TRACE> environment variable.
50 Prints the specified string to our debugging filehandle, which we will attempt
51 to open if we haven't yet. Provided to save our methods the worry of how
52 to display the message.
56 my ($self, $msg) = @_;
58 if(!defined($self->debugfh())) {
60 my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}
62 if (defined($debug_env) && ($debug_env =~ /=(.+)$/)) {
63 $fh = IO::File->new($1, 'w')
64 or die("Cannot open trace file $1");
66 $fh = IO::File->new('>&STDERR')
67 or die('Duplication of STDERR for debug output failed (perhaps your STDERR is closed?)');
74 $self->debugfh->print($msg);
79 Called when a transaction begins.
85 $self->print("BEGIN WORK\n");
90 Called when a transaction is rolled back.
96 $self->print("ROLLBACK\n");
101 Called when a transaction is committed.
107 $self->print("COMMIT\n");
112 Called before a query is executed. The first argument is the SQL string being
113 executed and subsequent arguments are the parameters used for the query.
117 my ($self, $string, @bind) = @_;
119 my $message = "$string: ".join(', ', @bind)."\n";
121 if(defined($self->callback)) {
122 $string =~ m/^(\w+)/;
123 $self->callback->($1, $message);
127 $self->print($message);
132 Called when a query finishes executing. Has the same arguments as query_start.
136 my ($self, $string) = @_;
143 Cory G. Watson <gphat@cpan.org>
147 You may distribute this code under the same license as Perl itself.