AUTHORS mass update; mst doesn't have to take credit for -everything- :)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / Statistics.pm
CommitLineData
4c248161 1package DBIx::Class::Storage::Statistics;
2use strict;
aaba9524 3use warnings;
4c248161 4
48a76fcf 5use base qw/DBIx::Class/;
a0024650 6use IO::File;
9c1700e3 7use namespace::clean;
3e110410 8
c6fa3170 9__PACKAGE__->mk_group_accessors(simple => qw/callback _debugfh silence/);
4c248161 10
11=head1 NAME
12
13DBIx::Class::Storage::Statistics - SQL Statistics
14
15=head1 SYNOPSIS
16
17=head1 DESCRIPTION
18
19This class is called by DBIx::Class::Storage::DBI as a means of collecting
faaba25f 20statistics on its actions. Using this class alone merely prints the SQL
4c248161 21executed, the fact that it completes and begin/end notification for
22transactions.
23
24To really use this class you should subclass it and create your own method
25for collecting the statistics as discussed in L<DBIx::Class::Manual::Cookbook>.
26
27=head1 METHODS
28
29=cut
30
31=head2 new
32
33Returns a new L<DBIx::Class::Storage::Statistics> object.
34
35=cut
36sub new {
04cf5bbf 37 my $self = {};
38 bless $self, (ref($_[0]) || $_[0]);
4c248161 39
04cf5bbf 40 return $self;
4c248161 41}
42
43=head2 debugfh
44
45Sets or retrieves the filehandle used for trace/debug output. This should
46be an IO::Handle compatible object (only the C<print> method is used). Initially
47should be set to STDERR - although see information on the
6fe735fa 48L<DBIC_TRACE> environment variable.
4c248161 49
c6fa3170 50As getter it will lazily open a filehandle for you if one is not already set.
70f39278 51
52=cut
70f39278 53
c6fa3170 54sub 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
80Prints the specified string to our debugging filehandle. Provided to save our
81methods the worry of how to display the message.
82
83=cut
84sub 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
94Turn off all output if set to true.
95
4c248161 96=head2 txn_begin
97
98Called when a transaction begins.
99
100=cut
101sub 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
111Called when a transaction is rolled back.
112
113=cut
114sub 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
124Called when a transaction is committed.
125
126=cut
127sub 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
137Called when a savepoint is created.
138
139=cut
140sub 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
150Called when a savepoint is released.
151
152=cut
8432aeca 153sub 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
163Called when rolling back to a savepoint.
164
165=cut
166sub 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
176Called before a query is executed. The first argument is the SQL string being
177executed and subsequent arguments are the parameters used for the query.
178
179=cut
180sub 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
196Called when a query finishes executing. Has the same arguments as query_start.
197
198=cut
199sub query_end {
04cf5bbf 200 my ($self, $string) = @_;
4c248161 201}
202
2031;
204
0c11ad0e 205=head1 AUTHOR AND CONTRIBUTORS
4c248161 206
0c11ad0e 207See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class
4c248161 208
209=head1 LICENSE
210
0c11ad0e 211You may distribute this code under the same terms as Perl itself.
4c248161 212
213=cut