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