| 1 | package DBIx::Class::Storage::Statistics; | ||||||||||||||||||||||||||||
| 2 | 33 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 33 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 33 | ▶ | use base qw/DBIx::Class/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 33 | ▶ | use IO::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 33 | ▶ | use namespace::clean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 33 | __PACKAGE__->mk_group_accessors(simple => qw/callback _debugfh silence/); | |||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||
| 20 | statistics on its actions. Using this class alone merely prints the SQL | ||||||||||||||||||||||||||||
| 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 { | ||||||||||||||||||||||||||||
| 37 | 40 | ▶ | my $self = {}; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.6
Pod
documented
| |||||||||||||||||||||||||||||
| 38 | 40 | ▶ | bless $self, (ref($_[0]) || $_[0]); | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $_[0] || $_[0]
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0]
$_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 40 | return $self; | |||||||||||||||||||||||||||
| 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 | ||||||||||||||||||||||||||||
| 48 | L<DBIC_TRACE> environment variable. | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | As getter it will lazily open a filehandle for you if one is not already set. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | =cut | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | sub debugfh { | ||||||||||||||||||||||||||||
| 55 | 22 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
debugfh: called CC=8 SCAR=21.1
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 22 | ▶ | if (@_) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 58 | 17 | $self->_debugfh($_[0]); | |||||||||||||||||||||||||||
| 59 | } elsif (!defined($self->_debugfh())) { | ||||||||||||||||||||||||||||
| 60 | 3 | my $fh; | |||||||||||||||||||||||||||
| 61 | my $debug_env = $ENV{DBIX_CLASS_STORAGE_DBI_DEBUG} | ||||||||||||||||||||||||||||
| 62 | 3 | ▶ | || $ENV{DBIC_TRACE}; | ||||||||||||||||||||||||||
|
Condition66%2/3
$ENV{'DBIX_CLASS_STORAGE_DBI_DEBUG'} || $ENV{'DBIC_TRACE'}
MC/DC50%1/2
$ENV{'DBIX_CLASS_STORAGE_DBI_DEBUG'} || $ENV{'DBIC_TRACE'}
$ENV{'DBIX_CLASS_STORAGE_DBI_DEBUG'}
$ENV{'DBIC_TRACE'}
Truth table66%2/3
A || B
A= $ENV{'DBIX_CLASS_STORAGE_DBI_DEBUG'}B= $ENV{'DBIC_TRACE'}
| |||||||||||||||||||||||||||||
| 63 | 3 | ▶ | if (defined($debug_env) && ($debug_env =~ /=(.+)$/)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $debug_env and $debug_env =~ /=(.+)$/
MC/DC50%1/2
defined $debug_env and $debug_env =~ /=(.+)$/
defined $debug_env
$debug_env =~ /=(.+)$/
Truth table66%2/3
A and B
A= defined $debug_envB= $debug_env =~ /=(.+)$/
| |||||||||||||||||||||||||||||
| 64 | 2 | ▶ | $fh = IO::File->new($1, 'a') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | or die("Cannot open trace file $1"); | ||||||||||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||||||||||
| 67 | 1 | ▶ | $fh = IO::File->new('>&STDERR') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | or die('Duplication of STDERR for debug output failed (perhaps your STDERR is closed?)'); | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | $fh->autoflush(); | |||||||||||||||||||||||||||
| 72 | 2 | $self->_debugfh($fh); | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 21 | $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 | 4 | ▶ | my ($self, $msg) = @_; | ||||||||||||||||||||||||||
|
Subroutine
print: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 4 | ▶ | return if $self->silence; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 4 | $self->debugfh->print($msg); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | =head2 silence | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | Turn off all output if set to true. | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head2 txn_begin | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | Called when a transaction begins. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||
| 101 | sub txn_begin { | ||||||||||||||||||||||||||||
| 102 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
txn_begin: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 2 | ▶ | return if $self->callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | $self->print("BEGIN WORK\n"); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head2 txn_rollback | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | Called when a transaction is rolled back. | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||
| 114 | sub txn_rollback { | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
txn_rollback: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 0 | ▶ | return if $self->callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | $self->print("ROLLBACK\n"); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =head2 txn_commit | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | Called when a transaction is committed. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =cut | ||||||||||||||||||||||||||||
| 127 | sub txn_commit { | ||||||||||||||||||||||||||||
| 128 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
txn_commit: called CC=2 SCAR=8.1
Pod
documented
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 2 | ▶ | return if $self->callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | $self->print("COMMIT\n"); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head2 svp_begin | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Called when a savepoint is created. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||
| 140 | sub svp_begin { | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
svp_begin: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | ▶ | return if $self->callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 0 | $self->print("SAVEPOINT $name\n"); | |||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 svp_release | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | Called when a savepoint is released. | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||
| 153 | sub svp_release { | ||||||||||||||||||||||||||||
| 154 | 0 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
svp_release: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | ▶ | return if $self->callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | $self->print("RELEASE SAVEPOINT $name\n"); | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 svp_rollback | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Called when rolling back to a savepoint. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||||||||||
| 166 | sub svp_rollback { | ||||||||||||||||||||||||||||
| 167 | 0 | ▶ | my ($self, $name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
svp_rollback: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 0 | ▶ | return if $self->callback; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | $self->print("ROLLBACK TO SAVEPOINT $name\n"); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 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 { | ||||||||||||||||||||||||||||
| 181 | 88 | ▶ | my ($self, $string, @bind) = @_; | ||||||||||||||||||||||||||
|
Subroutine
query_start: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 88 | my $message = "$string: ".join(', ', @bind)."\n"; | |||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | 88 | ▶ | if(defined($self->callback)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | 84 | $string =~ m/^(\w+)/; | |||||||||||||||||||||||||||
| 187 | 84 | $self->callback->($1, $message); | |||||||||||||||||||||||||||
| 188 | 84 | return; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | 4 | $self->print($message); | |||||||||||||||||||||||||||
| 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 { | ||||||||||||||||||||||||||||
| 200 | 87 | ▶ | my ($self, $string) = @_; | ||||||||||||||||||||||||||
|
Subroutine
query_end: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | 33 | 1; | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | =head1 AUTHOR AND CONTRIBUTORS | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | You may distribute this code under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =cut | ||||||||||||||||||||||||||||