Commit | Line | Data |
4c248161 |
1 | package DBIx::Class::Storage::Statistics; |
2 | use strict; |
aaba9524 |
3 | use warnings; |
4c248161 |
4 | |
48a76fcf |
5 | use base qw/DBIx::Class/; |
9c1700e3 |
6 | use namespace::clean; |
3e110410 |
7 | |
c6fa3170 |
8 | __PACKAGE__->mk_group_accessors(simple => qw/callback _debugfh silence/); |
4c248161 |
9 | |
10 | =head1 NAME |
11 | |
12 | DBIx::Class::Storage::Statistics - SQL Statistics |
13 | |
14 | =head1 SYNOPSIS |
15 | |
16 | =head1 DESCRIPTION |
17 | |
18 | This class is called by DBIx::Class::Storage::DBI as a means of collecting |
faaba25f |
19 | statistics on its actions. Using this class alone merely prints the SQL |
4c248161 |
20 | executed, the fact that it completes and begin/end notification for |
21 | transactions. |
22 | |
23 | To really use this class you should subclass it and create your own method |
24 | for collecting the statistics as discussed in L<DBIx::Class::Manual::Cookbook>. |
25 | |
26 | =head1 METHODS |
27 | |
28 | =cut |
29 | |
30 | =head2 new |
31 | |
32 | Returns a new L<DBIx::Class::Storage::Statistics> object. |
33 | |
34 | =cut |
35 | sub new { |
04cf5bbf |
36 | my $self = {}; |
37 | bless $self, (ref($_[0]) || $_[0]); |
4c248161 |
38 | |
04cf5bbf |
39 | return $self; |
4c248161 |
40 | } |
41 | |
42 | =head2 debugfh |
43 | |
44 | Sets or retrieves the filehandle used for trace/debug output. This should |
45 | be an IO::Handle compatible object (only the C<print> method is used). Initially |
46 | should be set to STDERR - although see information on the |
6fe735fa |
47 | L<DBIC_TRACE> environment variable. |
4c248161 |
48 | |
c6fa3170 |
49 | As getter it will lazily open a filehandle for you if one is not already set. |
70f39278 |
50 | |
51 | =cut |
70f39278 |
52 | |
c6fa3170 |
53 | sub debugfh { |
54 | my $self = shift; |
9901aad7 |
55 | |
c6fa3170 |
56 | if (@_) { |
57 | $self->_debugfh($_[0]); |
58 | } elsif (!defined($self->_debugfh())) { |
70f39278 |
59 | my $fh; |
60 | my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} |
61 | || $ENV{DBIC_TRACE}; |
62 | if (defined($debug_env) && ($debug_env =~ /=(.+)$/)) { |
238e0711 |
63 | open ($fh, '>>', $1) |
64 | or die("Cannot open trace file $1: $!"); |
70f39278 |
65 | } else { |
238e0711 |
66 | open ($fh, '>&STDERR') |
67 | or die("Duplication of STDERR for debug output failed (perhaps your STDERR is closed?): $!"); |
70f39278 |
68 | } |
69 | |
c6fa3170 |
70 | $self->_debugfh($fh); |
70f39278 |
71 | } |
72 | |
c6fa3170 |
73 | $self->_debugfh; |
74 | } |
75 | |
76 | =head2 print |
77 | |
78 | Prints the specified string to our debugging filehandle. Provided to save our |
79 | methods the worry of how to display the message. |
80 | |
81 | =cut |
82 | sub print { |
83 | my ($self, $msg) = @_; |
84 | |
85 | return if $self->silence; |
86 | |
e9f71ab2 |
87 | $self->debugfh->printflush($msg); |
70f39278 |
88 | } |
89 | |
dcdf7b2c |
90 | =head2 silence |
91 | |
92 | Turn off all output if set to true. |
93 | |
4c248161 |
94 | =head2 txn_begin |
95 | |
96 | Called when a transaction begins. |
97 | |
98 | =cut |
99 | sub txn_begin { |
04cf5bbf |
100 | my $self = shift; |
d2075431 |
101 | |
b94139c0 |
102 | return if $self->callback; |
103 | |
70f39278 |
104 | $self->print("BEGIN WORK\n"); |
4c248161 |
105 | } |
106 | |
107 | =head2 txn_rollback |
108 | |
109 | Called when a transaction is rolled back. |
110 | |
111 | =cut |
112 | sub txn_rollback { |
04cf5bbf |
113 | my $self = shift; |
d2075431 |
114 | |
b94139c0 |
115 | return if $self->callback; |
116 | |
70f39278 |
117 | $self->print("ROLLBACK\n"); |
4c248161 |
118 | } |
119 | |
120 | =head2 txn_commit |
121 | |
122 | Called when a transaction is committed. |
123 | |
124 | =cut |
125 | sub txn_commit { |
04cf5bbf |
126 | my $self = shift; |
d2075431 |
127 | |
b94139c0 |
128 | return if $self->callback; |
129 | |
70f39278 |
130 | $self->print("COMMIT\n"); |
4c248161 |
131 | } |
132 | |
adb3554a |
133 | =head2 svp_begin |
134 | |
135 | Called when a savepoint is created. |
136 | |
137 | =cut |
138 | sub svp_begin { |
139 | my ($self, $name) = @_; |
140 | |
b94139c0 |
141 | return if $self->callback; |
142 | |
adb3554a |
143 | $self->print("SAVEPOINT $name\n"); |
144 | } |
145 | |
146 | =head2 svp_release |
147 | |
148 | Called when a savepoint is released. |
149 | |
150 | =cut |
8432aeca |
151 | sub svp_release { |
adb3554a |
152 | my ($self, $name) = @_; |
153 | |
b94139c0 |
154 | return if $self->callback; |
155 | |
156 | $self->print("RELEASE SAVEPOINT $name\n"); |
adb3554a |
157 | } |
158 | |
159 | =head2 svp_rollback |
160 | |
161 | Called when rolling back to a savepoint. |
162 | |
163 | =cut |
164 | sub svp_rollback { |
165 | my ($self, $name) = @_; |
166 | |
b94139c0 |
167 | return if $self->callback; |
168 | |
169 | $self->print("ROLLBACK TO SAVEPOINT $name\n"); |
adb3554a |
170 | } |
171 | |
4c248161 |
172 | =head2 query_start |
173 | |
174 | Called before a query is executed. The first argument is the SQL string being |
175 | executed and subsequent arguments are the parameters used for the query. |
176 | |
177 | =cut |
178 | sub query_start { |
04cf5bbf |
179 | my ($self, $string, @bind) = @_; |
68fcff2f |
180 | |
04cf5bbf |
181 | my $message = "$string: ".join(', ', @bind)."\n"; |
4c248161 |
182 | |
04cf5bbf |
183 | if(defined($self->callback)) { |
184 | $string =~ m/^(\w+)/; |
1b7fb46e |
185 | $self->callback->($1, $message); |
04cf5bbf |
186 | return; |
187 | } |
4c248161 |
188 | |
70f39278 |
189 | $self->print($message); |
4c248161 |
190 | } |
191 | |
192 | =head2 query_end |
193 | |
194 | Called when a query finishes executing. Has the same arguments as query_start. |
195 | |
196 | =cut |
197 | sub query_end { |
04cf5bbf |
198 | my ($self, $string) = @_; |
4c248161 |
199 | } |
200 | |
201 | 1; |
202 | |
0c11ad0e |
203 | =head1 AUTHOR AND CONTRIBUTORS |
4c248161 |
204 | |
0c11ad0e |
205 | See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class |
4c248161 |
206 | |
207 | =head1 LICENSE |
208 | |
0c11ad0e |
209 | You may distribute this code under the same terms as Perl itself. |
4c248161 |
210 | |
211 | =cut |