Stop permanently enabling autoflush on the debug filehandle
[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/;
9c1700e3 6use namespace::clean;
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 =~ /=(.+)$/)) {
238e0711 63 open ($fh, '>>', $1)
64 or die("Cannot open trace file $1: $!");
70f39278 65 } else {
238e0711 66 open ($fh, '>&STDERR')
67 or die("Duplication of STDERR for debug output failed (perhaps your STDERR is closed?): $!");
70f39278 68 }
69
c6fa3170 70 $self->_debugfh($fh);
70f39278 71 }
72
c6fa3170 73 $self->_debugfh;
74}
75
76=head2 print
77
78Prints the specified string to our debugging filehandle. Provided to save our
79methods the worry of how to display the message.
80
81=cut
82sub print {
83 my ($self, $msg) = @_;
84
85 return if $self->silence;
86
e9f71ab2 87 $self->debugfh->printflush($msg);
70f39278 88}
89
dcdf7b2c 90=head2 silence
91
92Turn off all output if set to true.
93
4c248161 94=head2 txn_begin
95
96Called when a transaction begins.
97
98=cut
99sub txn_begin {
04cf5bbf 100 my $self = shift;
d2075431 101
b94139c0 102 return if $self->callback;
103
70f39278 104 $self->print("BEGIN WORK\n");
4c248161 105}
106
107=head2 txn_rollback
108
109Called when a transaction is rolled back.
110
111=cut
112sub txn_rollback {
04cf5bbf 113 my $self = shift;
d2075431 114
b94139c0 115 return if $self->callback;
116
70f39278 117 $self->print("ROLLBACK\n");
4c248161 118}
119
120=head2 txn_commit
121
122Called when a transaction is committed.
123
124=cut
125sub txn_commit {
04cf5bbf 126 my $self = shift;
d2075431 127
b94139c0 128 return if $self->callback;
129
70f39278 130 $self->print("COMMIT\n");
4c248161 131}
132
adb3554a 133=head2 svp_begin
134
135Called when a savepoint is created.
136
137=cut
138sub svp_begin {
139 my ($self, $name) = @_;
140
b94139c0 141 return if $self->callback;
142
adb3554a 143 $self->print("SAVEPOINT $name\n");
144}
145
146=head2 svp_release
147
148Called when a savepoint is released.
149
150=cut
8432aeca 151sub svp_release {
adb3554a 152 my ($self, $name) = @_;
153
b94139c0 154 return if $self->callback;
155
156 $self->print("RELEASE SAVEPOINT $name\n");
adb3554a 157}
158
159=head2 svp_rollback
160
161Called when rolling back to a savepoint.
162
163=cut
164sub svp_rollback {
165 my ($self, $name) = @_;
166
b94139c0 167 return if $self->callback;
168
169 $self->print("ROLLBACK TO SAVEPOINT $name\n");
adb3554a 170}
171
4c248161 172=head2 query_start
173
174Called before a query is executed. The first argument is the SQL string being
175executed and subsequent arguments are the parameters used for the query.
176
177=cut
178sub query_start {
04cf5bbf 179 my ($self, $string, @bind) = @_;
68fcff2f 180
04cf5bbf 181 my $message = "$string: ".join(', ', @bind)."\n";
4c248161 182
04cf5bbf 183 if(defined($self->callback)) {
184 $string =~ m/^(\w+)/;
1b7fb46e 185 $self->callback->($1, $message);
04cf5bbf 186 return;
187 }
4c248161 188
70f39278 189 $self->print($message);
4c248161 190}
191
192=head2 query_end
193
194Called when a query finishes executing. Has the same arguments as query_start.
195
196=cut
197sub query_end {
04cf5bbf 198 my ($self, $string) = @_;
4c248161 199}
200
2011;
202
0c11ad0e 203=head1 AUTHOR AND CONTRIBUTORS
4c248161 204
0c11ad0e 205See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
4c248161 206
207=head1 LICENSE
208
0c11ad0e 209You may distribute this code under the same terms as Perl itself.
4c248161 210
211=cut