(travis) Bump tested perls a bit
[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);
9c1700e3 18use namespace::clean;
3e110410 19
4c248161 20=head1 NAME
21
22DBIx::Class::Storage::Statistics - SQL Statistics
23
24=head1 SYNOPSIS
25
26=head1 DESCRIPTION
27
28This class is called by DBIx::Class::Storage::DBI as a means of collecting
faaba25f 29statistics on its actions. Using this class alone merely prints the SQL
4c248161 30executed, the fact that it completes and begin/end notification for
31transactions.
32
33To really use this class you should subclass it and create your own method
34for collecting the statistics as discussed in L<DBIx::Class::Manual::Cookbook>.
35
36=head1 METHODS
37
4c248161 38=head2 new
39
40Returns a new L<DBIx::Class::Storage::Statistics> object.
41
4c248161 42=head2 debugfh
43
44Sets or retrieves the filehandle used for trace/debug output. This should
4d93345c 45be an L<IO::Handle> compatible object (only the
8494142c 46L<< print|IO::Handle/METHODS >> method is used). By
4d93345c 47default it is initially set to STDERR - although see discussion of the
48L<DBIC_TRACE|DBIx::Class::Storage/DBIC_TRACE> environment variable.
4c248161 49
8494142c 50Invoked as a getter it will lazily open a filehandle and set it to
51L<< autoflush|perlvar/HANDLE->autoflush( EXPR ) >> (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
8494142c 88 $fh->autoflush(1);
89
68b8ba54 90 $fh;
c6fa3170 91}
92
68b8ba54 93has [qw(_defaulted_to_stderr silence callback)] => (
94 is => 'rw',
95);
96
c6fa3170 97=head2 print
98
99Prints the specified string to our debugging filehandle. Provided to save our
100methods the worry of how to display the message.
101
102=cut
103sub print {
104 my ($self, $msg) = @_;
105
106 return if $self->silence;
107
9d522a4e 108 my $fh = $self->debugfh;
109
110 # not using 'no warnings' here because all of this can change at runtime
111 local $SIG{__WARN__} = sigwarn_silencer(qr/^Wide character in print/)
112 if $self->_defaulted_to_stderr;
113
8494142c 114 $fh->print($msg);
70f39278 115}
116
dcdf7b2c 117=head2 silence
118
119Turn off all output if set to true.
120
4c248161 121=head2 txn_begin
122
123Called when a transaction begins.
124
125=cut
126sub txn_begin {
04cf5bbf 127 my $self = shift;
d2075431 128
b94139c0 129 return if $self->callback;
130
70f39278 131 $self->print("BEGIN WORK\n");
4c248161 132}
133
134=head2 txn_rollback
135
136Called when a transaction is rolled back.
137
138=cut
139sub txn_rollback {
04cf5bbf 140 my $self = shift;
d2075431 141
b94139c0 142 return if $self->callback;
143
70f39278 144 $self->print("ROLLBACK\n");
4c248161 145}
146
147=head2 txn_commit
148
149Called when a transaction is committed.
150
151=cut
152sub txn_commit {
04cf5bbf 153 my $self = shift;
d2075431 154
b94139c0 155 return if $self->callback;
156
70f39278 157 $self->print("COMMIT\n");
4c248161 158}
159
adb3554a 160=head2 svp_begin
161
162Called when a savepoint is created.
163
164=cut
165sub svp_begin {
166 my ($self, $name) = @_;
167
b94139c0 168 return if $self->callback;
169
adb3554a 170 $self->print("SAVEPOINT $name\n");
171}
172
173=head2 svp_release
174
175Called when a savepoint is released.
176
177=cut
8432aeca 178sub svp_release {
adb3554a 179 my ($self, $name) = @_;
180
b94139c0 181 return if $self->callback;
182
183 $self->print("RELEASE SAVEPOINT $name\n");
adb3554a 184}
185
186=head2 svp_rollback
187
188Called when rolling back to a savepoint.
189
190=cut
191sub svp_rollback {
192 my ($self, $name) = @_;
193
b94139c0 194 return if $self->callback;
195
196 $self->print("ROLLBACK TO SAVEPOINT $name\n");
adb3554a 197}
198
4c248161 199=head2 query_start
200
201Called before a query is executed. The first argument is the SQL string being
202executed and subsequent arguments are the parameters used for the query.
203
204=cut
205sub query_start {
04cf5bbf 206 my ($self, $string, @bind) = @_;
68fcff2f 207
04cf5bbf 208 my $message = "$string: ".join(', ', @bind)."\n";
4c248161 209
04cf5bbf 210 if(defined($self->callback)) {
211 $string =~ m/^(\w+)/;
1b7fb46e 212 $self->callback->($1, $message);
04cf5bbf 213 return;
214 }
4c248161 215
70f39278 216 $self->print($message);
4c248161 217}
218
219=head2 query_end
220
221Called when a query finishes executing. Has the same arguments as query_start.
222
223=cut
224sub query_end {
04cf5bbf 225 my ($self, $string) = @_;
4c248161 226}
227
2281;
229
0c11ad0e 230=head1 AUTHOR AND CONTRIBUTORS
4c248161 231
0c11ad0e 232See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
4c248161 233
234=head1 LICENSE
235
0c11ad0e 236You may distribute this code under the same terms as Perl itself.
4c248161 237
238=cut