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