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