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