refactor DBIx::Class::Storage::Statistics::debugfh() to be lazy
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / Statistics.pm
CommitLineData
4c248161 1package DBIx::Class::Storage::Statistics;
2use strict;
aaba9524 3use warnings;
4c248161 4
48a76fcf 5use base qw/DBIx::Class/;
a0024650 6use IO::File;
3e110410 7
c6fa3170 8__PACKAGE__->mk_group_accessors(simple => qw/callback _debugfh silence/);
4c248161 9
10=head1 NAME
11
12DBIx::Class::Storage::Statistics - SQL Statistics
13
14=head1 SYNOPSIS
15
16=head1 DESCRIPTION
17
18This class is called by DBIx::Class::Storage::DBI as a means of collecting
faaba25f 19statistics on its actions. Using this class alone merely prints the SQL
4c248161 20executed, the fact that it completes and begin/end notification for
21transactions.
22
23To really use this class you should subclass it and create your own method
24for collecting the statistics as discussed in L<DBIx::Class::Manual::Cookbook>.
25
26=head1 METHODS
27
28=cut
29
30=head2 new
31
32Returns a new L<DBIx::Class::Storage::Statistics> object.
33
34=cut
35sub new {
04cf5bbf 36 my $self = {};
37 bless $self, (ref($_[0]) || $_[0]);
4c248161 38
04cf5bbf 39 return $self;
4c248161 40}
41
42=head2 debugfh
43
44Sets or retrieves the filehandle used for trace/debug output. This should
45be an IO::Handle compatible object (only the C<print> method is used). Initially
46should be set to STDERR - although see information on the
6fe735fa 47L<DBIC_TRACE> environment variable.
4c248161 48
c6fa3170 49As getter it will lazily open a filehandle for you if one is not already set.
70f39278 50
51=cut
70f39278 52
c6fa3170 53sub debugfh {
54 my $self = shift;
9901aad7 55
c6fa3170 56 if (@_) {
57 $self->_debugfh($_[0]);
58 } elsif (!defined($self->_debugfh())) {
70f39278 59 my $fh;
60 my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG}
61 || $ENV{DBIC_TRACE};
62 if (defined($debug_env) && ($debug_env =~ /=(.+)$/)) {
63 $fh = IO::File->new($1, 'w')
64 or die("Cannot open trace file $1");
65 } else {
66 $fh = IO::File->new('>&STDERR')
67 or die('Duplication of STDERR for debug output failed (perhaps your STDERR is closed?)');
68 }
69
70 $fh->autoflush();
c6fa3170 71 $self->_debugfh($fh);
70f39278 72 }
73
c6fa3170 74 $self->_debugfh;
75}
76
77=head2 print
78
79Prints the specified string to our debugging filehandle. Provided to save our
80methods the worry of how to display the message.
81
82=cut
83sub print {
84 my ($self, $msg) = @_;
85
86 return if $self->silence;
87
70f39278 88 $self->debugfh->print($msg);
89}
90
dcdf7b2c 91=head2 silence
92
93Turn off all output if set to true.
94
4c248161 95=head2 txn_begin
96
97Called when a transaction begins.
98
99=cut
100sub txn_begin {
04cf5bbf 101 my $self = shift;
d2075431 102
b94139c0 103 return if $self->callback;
104
70f39278 105 $self->print("BEGIN WORK\n");
4c248161 106}
107
108=head2 txn_rollback
109
110Called when a transaction is rolled back.
111
112=cut
113sub txn_rollback {
04cf5bbf 114 my $self = shift;
d2075431 115
b94139c0 116 return if $self->callback;
117
70f39278 118 $self->print("ROLLBACK\n");
4c248161 119}
120
121=head2 txn_commit
122
123Called when a transaction is committed.
124
125=cut
126sub txn_commit {
04cf5bbf 127 my $self = shift;
d2075431 128
b94139c0 129 return if $self->callback;
130
70f39278 131 $self->print("COMMIT\n");
4c248161 132}
133
adb3554a 134=head2 svp_begin
135
136Called when a savepoint is created.
137
138=cut
139sub svp_begin {
140 my ($self, $name) = @_;
141
b94139c0 142 return if $self->callback;
143
adb3554a 144 $self->print("SAVEPOINT $name\n");
145}
146
147=head2 svp_release
148
149Called when a savepoint is released.
150
151=cut
8432aeca 152sub svp_release {
adb3554a 153 my ($self, $name) = @_;
154
b94139c0 155 return if $self->callback;
156
157 $self->print("RELEASE SAVEPOINT $name\n");
adb3554a 158}
159
160=head2 svp_rollback
161
162Called when rolling back to a savepoint.
163
164=cut
165sub svp_rollback {
166 my ($self, $name) = @_;
167
b94139c0 168 return if $self->callback;
169
170 $self->print("ROLLBACK TO SAVEPOINT $name\n");
adb3554a 171}
172
4c248161 173=head2 query_start
174
175Called before a query is executed. The first argument is the SQL string being
176executed and subsequent arguments are the parameters used for the query.
177
178=cut
179sub query_start {
04cf5bbf 180 my ($self, $string, @bind) = @_;
68fcff2f 181
04cf5bbf 182 my $message = "$string: ".join(', ', @bind)."\n";
4c248161 183
04cf5bbf 184 if(defined($self->callback)) {
185 $string =~ m/^(\w+)/;
1b7fb46e 186 $self->callback->($1, $message);
04cf5bbf 187 return;
188 }
4c248161 189
70f39278 190 $self->print($message);
4c248161 191}
192
193=head2 query_end
194
195Called when a query finishes executing. Has the same arguments as query_start.
196
197=cut
198sub query_end {
04cf5bbf 199 my ($self, $string) = @_;
4c248161 200}
201
2021;
203
204=head1 AUTHORS
205
206Cory G. Watson <gphat@cpan.org>
207
208=head1 LICENSE
209
210You may distribute this code under the same license as Perl itself.
211
212=cut