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