Commit | Line | Data |
4c248161 |
1 | package DBIx::Class::Storage::Statistics; |
7f9a3f70 |
2 | |
4c248161 |
3 | use strict; |
aaba9524 |
4 | use warnings; |
4c248161 |
5 | |
cbd7f87a |
6 | use DBIx::Class::_Util qw(sigwarn_silencer qsub); |
924a474d |
7 | use IO::Handle (); |
0020e364 |
8 | use Moo; |
68b8ba54 |
9 | extends 'DBIx::Class'; |
9c1700e3 |
10 | use namespace::clean; |
3e110410 |
11 | |
4c248161 |
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 |
faaba25f |
21 | statistics on its actions. Using this class alone merely prints the SQL |
4c248161 |
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<DBIx::Class::Manual::Cookbook>. |
27 | |
28 | =head1 METHODS |
29 | |
4c248161 |
30 | =head2 new |
31 | |
32 | Returns a new L<DBIx::Class::Storage::Statistics> object. |
33 | |
4c248161 |
34 | =head2 debugfh |
35 | |
36 | Sets or retrieves the filehandle used for trace/debug output. This should |
4d93345c |
37 | be an L<IO::Handle> compatible object (only the |
8494142c |
38 | L<< print|IO::Handle/METHODS >> method is used). By |
4d93345c |
39 | default it is initially set to STDERR - although see discussion of the |
40 | L<DBIC_TRACE|DBIx::Class::Storage/DBIC_TRACE> environment variable. |
4c248161 |
41 | |
8494142c |
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). |
70f39278 |
45 | |
46 | =cut |
70f39278 |
47 | |
68b8ba54 |
48 | # FIXME - there ought to be a way to fold this into _debugfh itself |
49 | # having the undef re-trigger the builder (or better yet a default |
50 | # which can be folded in as a qsub) |
c6fa3170 |
51 | sub debugfh { |
52 | my $self = shift; |
9901aad7 |
53 | |
68b8ba54 |
54 | return $self->_debugfh(@_) if @_; |
55 | $self->_debugfh || $self->_build_debugfh; |
56 | } |
57 | |
58 | has _debugfh => ( |
59 | is => 'rw', |
60 | lazy => 1, |
7f9a3f70 |
61 | trigger => qsub '$_[0]->_defaulted_to_stderr(undef)', |
68b8ba54 |
62 | builder => '_build_debugfh', |
63 | ); |
64 | |
65 | sub _build_debugfh { |
66 | my $fh; |
67 | |
68 | my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} || $ENV{DBIC_TRACE}; |
69 | |
70 | if (defined($debug_env) and ($debug_env =~ /=(.+)$/)) { |
71 | open ($fh, '>>', $1) |
72 | or die("Cannot open trace file $1: $!\n"); |
73 | } |
74 | else { |
75 | open ($fh, '>&STDERR') |
76 | or die("Duplication of STDERR for debug output failed (perhaps your STDERR is closed?): $!\n"); |
77 | $_[0]->_defaulted_to_stderr(1); |
70f39278 |
78 | } |
79 | |
8494142c |
80 | $fh->autoflush(1); |
81 | |
68b8ba54 |
82 | $fh; |
c6fa3170 |
83 | } |
84 | |
68b8ba54 |
85 | has [qw(_defaulted_to_stderr silence callback)] => ( |
86 | is => 'rw', |
87 | ); |
88 | |
c6fa3170 |
89 | =head2 print |
90 | |
91 | Prints the specified string to our debugging filehandle. Provided to save our |
92 | methods the worry of how to display the message. |
93 | |
94 | =cut |
95 | sub print { |
96 | my ($self, $msg) = @_; |
97 | |
98 | return if $self->silence; |
99 | |
9d522a4e |
100 | my $fh = $self->debugfh; |
101 | |
102 | # not using 'no warnings' here because all of this can change at runtime |
103 | local $SIG{__WARN__} = sigwarn_silencer(qr/^Wide character in print/) |
104 | if $self->_defaulted_to_stderr; |
105 | |
8494142c |
106 | $fh->print($msg); |
70f39278 |
107 | } |
108 | |
dcdf7b2c |
109 | =head2 silence |
110 | |
111 | Turn off all output if set to true. |
112 | |
4c248161 |
113 | =head2 txn_begin |
114 | |
115 | Called when a transaction begins. |
116 | |
117 | =cut |
118 | sub txn_begin { |
04cf5bbf |
119 | my $self = shift; |
d2075431 |
120 | |
b94139c0 |
121 | return if $self->callback; |
122 | |
70f39278 |
123 | $self->print("BEGIN WORK\n"); |
4c248161 |
124 | } |
125 | |
126 | =head2 txn_rollback |
127 | |
128 | Called when a transaction is rolled back. |
129 | |
130 | =cut |
131 | sub txn_rollback { |
04cf5bbf |
132 | my $self = shift; |
d2075431 |
133 | |
b94139c0 |
134 | return if $self->callback; |
135 | |
70f39278 |
136 | $self->print("ROLLBACK\n"); |
4c248161 |
137 | } |
138 | |
139 | =head2 txn_commit |
140 | |
141 | Called when a transaction is committed. |
142 | |
143 | =cut |
144 | sub txn_commit { |
04cf5bbf |
145 | my $self = shift; |
d2075431 |
146 | |
b94139c0 |
147 | return if $self->callback; |
148 | |
70f39278 |
149 | $self->print("COMMIT\n"); |
4c248161 |
150 | } |
151 | |
adb3554a |
152 | =head2 svp_begin |
153 | |
154 | Called when a savepoint is created. |
155 | |
156 | =cut |
157 | sub svp_begin { |
158 | my ($self, $name) = @_; |
159 | |
b94139c0 |
160 | return if $self->callback; |
161 | |
adb3554a |
162 | $self->print("SAVEPOINT $name\n"); |
163 | } |
164 | |
165 | =head2 svp_release |
166 | |
167 | Called when a savepoint is released. |
168 | |
169 | =cut |
8432aeca |
170 | sub svp_release { |
adb3554a |
171 | my ($self, $name) = @_; |
172 | |
b94139c0 |
173 | return if $self->callback; |
174 | |
175 | $self->print("RELEASE SAVEPOINT $name\n"); |
adb3554a |
176 | } |
177 | |
178 | =head2 svp_rollback |
179 | |
180 | Called when rolling back to a savepoint. |
181 | |
182 | =cut |
183 | sub svp_rollback { |
184 | my ($self, $name) = @_; |
185 | |
b94139c0 |
186 | return if $self->callback; |
187 | |
188 | $self->print("ROLLBACK TO SAVEPOINT $name\n"); |
adb3554a |
189 | } |
190 | |
4c248161 |
191 | =head2 query_start |
192 | |
193 | Called before a query is executed. The first argument is the SQL string being |
194 | executed and subsequent arguments are the parameters used for the query. |
195 | |
196 | =cut |
197 | sub query_start { |
04cf5bbf |
198 | my ($self, $string, @bind) = @_; |
68fcff2f |
199 | |
04cf5bbf |
200 | my $message = "$string: ".join(', ', @bind)."\n"; |
4c248161 |
201 | |
04cf5bbf |
202 | if(defined($self->callback)) { |
203 | $string =~ m/^(\w+)/; |
1b7fb46e |
204 | $self->callback->($1, $message); |
04cf5bbf |
205 | return; |
206 | } |
4c248161 |
207 | |
70f39278 |
208 | $self->print($message); |
4c248161 |
209 | } |
210 | |
211 | =head2 query_end |
212 | |
213 | Called when a query finishes executing. Has the same arguments as query_start. |
214 | |
215 | =cut |
a2bd3796 |
216 | |
4c248161 |
217 | sub query_end { |
04cf5bbf |
218 | my ($self, $string) = @_; |
4c248161 |
219 | } |
220 | |
a2bd3796 |
221 | =head1 FURTHER QUESTIONS? |
4c248161 |
222 | |
a2bd3796 |
223 | Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>. |
4c248161 |
224 | |
a2bd3796 |
225 | =head1 COPYRIGHT AND LICENSE |
4c248161 |
226 | |
a2bd3796 |
227 | This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE> |
228 | by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can |
229 | redistribute it and/or modify it under the same terms as the |
230 | L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>. |
4c248161 |
231 | |
232 | =cut |
a2bd3796 |
233 | |
234 | 1; |