File Coverage

blib/lib/DBIx/Class/Storage/Statistics.pm
Criterion Covered Total %
statement 47 57 82.4
branch 15 24 62.5
condition 4 6 66.6
subroutine 13 16 81.2
pod 9 9 100.0
total 88 112 78.5


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