| 1 | package DBIx::Class::QueryLog; | ||||||||||
| 2 | 6 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | |||||||||||
| 4 | has bucket => ( | ||||||||||
| 5 | is => 'rw', | ||||||||||
| 6 | isa => 'Str', | ||||||||||
| 7 | 4 | ▶ | default => sub { 'default' } | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 6 | ); | |||||||||
| 9 | |||||||||||
| 10 | 6 | has current_query => ( | |||||||||
| 11 | is => 'rw', | ||||||||||
| 12 | isa => 'Maybe[DBIx::Class::QueryLog::Query]' | ||||||||||
| 13 | ); | ||||||||||
| 14 | |||||||||||
| 15 | 6 | has current_transaction => ( | |||||||||
| 16 | is => 'rw', | ||||||||||
| 17 | isa => 'Maybe[DBIx::Class::QueryLog::Transaction]' | ||||||||||
| 18 | ); | ||||||||||
| 19 | |||||||||||
| 20 | has log => ( | ||||||||||
| 21 | traits => [qw(Array)], | ||||||||||
| 22 | is => 'rw', | ||||||||||
| 23 | isa => 'ArrayRef', | ||||||||||
| 24 | 4 | ▶ | default => sub { [] }, | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 25 | 6 | handles => { | |||||||||
| 26 | add_to_log => 'push', | ||||||||||
| 27 | reset => 'clear' | ||||||||||
| 28 | } | ||||||||||
| 29 | ); | ||||||||||
| 30 | |||||||||||
| 31 | 6 | has passthrough => ( | |||||||||
| 32 | is => 'rw', | ||||||||||
| 33 | isa => 'Bool', | ||||||||||
| 34 | default => 0 | ||||||||||
| 35 | ); | ||||||||||
| 36 | |||||||||||
| 37 | before 'add_to_log' => sub { | ||||||||||
| 38 | 11 | ▶ | my ($self, $thing) = @_; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 39 | |||||||||||
| 40 | 11 | $thing->bucket($self->bucket); | |||||||||
| 41 | 6 | }; | |||||||||
| 42 | |||||||||||
| 43 | 6 | ▶ | use base qw(DBIx::Class::Storage::Statistics); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 44 | |||||||||||
| 45 | 6 | ▶ | use Time::HiRes; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 46 | |||||||||||
| 47 | 6 | ▶ | use DBIx::Class::QueryLog::Query; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 48 | 6 | ▶ | use DBIx::Class::QueryLog::Transaction; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 49 | |||||||||||
| 50 | =head1 NAME | ||||||||||
| 51 | |||||||||||
| 52 | DBIx::Class::QueryLog - Log queries for later analysis. | ||||||||||
| 53 | |||||||||||
| 54 | =cut | ||||||||||
| 55 | |||||||||||
| 56 | 6 | our $VERSION = '1.3.3'; | |||||||||
| 57 | |||||||||||
| 58 | =head1 SYNOPSIS | ||||||||||
| 59 | |||||||||||
| 60 | DBIx::Class::QueryLog 'logs' each transaction and query executed so you can | ||||||||||
| 61 | analyze what happened in the 'session'. It must be installed as the debugobj | ||||||||||
| 62 | in DBIx::Class: | ||||||||||
| 63 | |||||||||||
| 64 | use DBIx::Class::QueryLog; | ||||||||||
| 65 | use DBIx::Class::QueryLog::Analyzer; | ||||||||||
| 66 | |||||||||||
| 67 | my $schema = ... # Get your schema! | ||||||||||
| 68 | my $ql = DBIx::Class::QueryLog->new; | ||||||||||
| 69 | $schema->storage->debugobj($ql); | ||||||||||
| 70 | $schema->storage->debug(1); | ||||||||||
| 71 | ... # do some stuff! | ||||||||||
| 72 | my $ana = DBIx::Class::QueryLog::Analyzer->new({ querylog => $ql }); | ||||||||||
| 73 | my @queries = $ana->get_sorted_queries; | ||||||||||
| 74 | |||||||||||
| 75 | Every transaction and query executed will have a corresponding Transaction | ||||||||||
| 76 | and Query object stored in order of execution, like so: | ||||||||||
| 77 | |||||||||||
| 78 | Query | ||||||||||
| 79 | Query | ||||||||||
| 80 | Transaction | ||||||||||
| 81 | Query | ||||||||||
| 82 | |||||||||||
| 83 | This array can be retrieved with the log method. Queries executed inside | ||||||||||
| 84 | a transaction are stored inside their Transaction object, not inside the | ||||||||||
| 85 | QueryLog directly. | ||||||||||
| 86 | |||||||||||
| 87 | See L<DBIx::Class::QueryLog::Analyzer> for options on digesting the results | ||||||||||
| 88 | of a QueryLog session. | ||||||||||
| 89 | |||||||||||
| 90 | If you wish to have the QueryLog collecting results, and the normal trace | ||||||||||
| 91 | output of SQL queries from DBIx::Class, then set C<passthrough> to 1 | ||||||||||
| 92 | |||||||||||
| 93 | $ql->passthrough(1); | ||||||||||
| 94 | |||||||||||
| 95 | =head1 BUCKETS | ||||||||||
| 96 | |||||||||||
| 97 | Sometimes you want to break your analysis down into stages. To segregate the | ||||||||||
| 98 | queries and transactions, simply set the bucket and run some queries: | ||||||||||
| 99 | |||||||||||
| 100 | $ql->bucket('selects'); | ||||||||||
| 101 | $schema->resultset('Foo')->find(..); | ||||||||||
| 102 | # Some queries | ||||||||||
| 103 | $ql->bucket('updates'); | ||||||||||
| 104 | $foo->update({ name => 'Gorch' }); | ||||||||||
| 105 | $ql->bucket('something else); | ||||||||||
| 106 | ... | ||||||||||
| 107 | |||||||||||
| 108 | Any time a query or transaction is completed the QueryLog's current bucket | ||||||||||
| 109 | will be copied into it so that the Analyzer can later use it. See | ||||||||||
| 110 | the get_totaled_queries method and it's optional parameter. | ||||||||||
| 111 | |||||||||||
| 112 | =head1 METHODS | ||||||||||
| 113 | |||||||||||
| 114 | =head2 new | ||||||||||
| 115 | |||||||||||
| 116 | Create a new DBIx::Class::QueryLog. | ||||||||||
| 117 | |||||||||||
| 118 | =head2 bucket | ||||||||||
| 119 | |||||||||||
| 120 | Set the current bucket for this QueryLog. This bucket will be copied to any | ||||||||||
| 121 | transactions or queries that finish. | ||||||||||
| 122 | |||||||||||
| 123 | =head2 time_elapsed | ||||||||||
| 124 | |||||||||||
| 125 | Returns the total time elapsed for ALL transactions and queries in this log. | ||||||||||
| 126 | |||||||||||
| 127 | =cut | ||||||||||
| 128 | sub time_elapsed { | ||||||||||
| 129 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
time_elapsed: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 130 | |||||||||||
| 131 | 0 | my $total = 0; | |||||||||
| 132 | 0 | foreach my $t (@{ $self->log }) { | |||||||||
| 133 | 0 | $total += $t->time_elapsed; | |||||||||
| 134 | } | ||||||||||
| 135 | |||||||||||
| 136 | 0 | return $total; | |||||||||
| 137 | } | ||||||||||
| 138 | |||||||||||
| 139 | =head2 count | ||||||||||
| 140 | |||||||||||
| 141 | Returns the number of queries executed in this QueryLog | ||||||||||
| 142 | |||||||||||
| 143 | =cut | ||||||||||
| 144 | sub count { | ||||||||||
| 145 | 0 | ▶ | my $self = shift; | ||||||||
|
Subroutine
count: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 146 | |||||||||||
| 147 | 0 | my $total = 0; | |||||||||
| 148 | 0 | foreach my $t (@{ $self->log }) { | |||||||||
| 149 | 0 | $total += $t->count; | |||||||||
| 150 | } | ||||||||||
| 151 | |||||||||||
| 152 | 0 | return $total; | |||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | =head2 reset | ||||||||||
| 156 | |||||||||||
| 157 | Reset this QueryLog by removing all transcations and queries. | ||||||||||
| 158 | |||||||||||
| 159 | =head2 add_to_log | ||||||||||
| 160 | |||||||||||
| 161 | Add this provided Transaction or Query to the log. | ||||||||||
| 162 | |||||||||||
| 163 | =head2 txn_begin | ||||||||||
| 164 | |||||||||||
| 165 | Called by DBIx::Class when a transaction is begun. | ||||||||||
| 166 | |||||||||||
| 167 | =cut | ||||||||||
| 168 | |||||||||||
| 169 | sub txn_begin { | ||||||||||
| 170 | 5 | ▶ | my $self = shift; | ||||||||
|
Subroutine
txn_begin: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 171 | |||||||||||
| 172 | 5 | ▶ | $self->next::method(@_) if $self->passthrough; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 173 | 5 | $self->current_transaction( | |||||||||
| 174 | $self->transaction_class()->new({ | ||||||||||
| 175 | start_time => Time::HiRes::time | ||||||||||
| 176 | }) | ||||||||||
| 177 | ); | ||||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | =head2 txn_commit | ||||||||||
| 181 | |||||||||||
| 182 | Called by DBIx::Class when a transaction is committed. | ||||||||||
| 183 | |||||||||||
| 184 | =cut | ||||||||||
| 185 | |||||||||||
| 186 | sub txn_commit { | ||||||||||
| 187 | 4 | ▶ | my $self = shift; | ||||||||
|
Subroutine
txn_commit: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||
| 188 | |||||||||||
| 189 | 4 | ▶ | $self->next::method(@_) if $self->passthrough; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 190 | 4 | ▶ | if(defined($self->current_transaction)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 191 | 4 | my $txn = $self->current_transaction; | |||||||||
| 192 | 4 | $txn->end_time(Time::HiRes::time); | |||||||||
| 193 | 4 | $txn->committed(1); | |||||||||
| 194 | 4 | $txn->rolledback(0); | |||||||||
| 195 | 4 | $self->add_to_log($txn); | |||||||||
| 196 | 4 | $self->current_transaction(undef); | |||||||||
| 197 | } else { | ||||||||||
| 198 | 0 | warn('Unknown transaction committed.') | |||||||||
| 199 | } | ||||||||||
| 200 | } | ||||||||||
| 201 | |||||||||||
| 202 | =head2 txn_rollback | ||||||||||
| 203 | |||||||||||
| 204 | Called by DBIx::Class when a transaction is rolled back. | ||||||||||
| 205 | |||||||||||
| 206 | =cut | ||||||||||
| 207 | |||||||||||
| 208 | sub txn_rollback { | ||||||||||
| 209 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
txn_rollback: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||
| 210 | |||||||||||
| 211 | 1 | ▶ | $self->next::method(@_) if $self->passthrough; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 212 | 1 | ▶ | if(defined($self->current_transaction)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 213 | 1 | my $txn = $self->current_transaction; | |||||||||
| 214 | 1 | $txn->end_time(Time::HiRes::time); | |||||||||
| 215 | 1 | $txn->committed(0); | |||||||||
| 216 | 1 | $txn->rolledback(1); | |||||||||
| 217 | 1 | $self->add_to_log($txn); | |||||||||
| 218 | 1 | $self->current_transaction(undef); | |||||||||
| 219 | } else { | ||||||||||
| 220 | 0 | warn('Unknown transaction committed.') | |||||||||
| 221 | } | ||||||||||
| 222 | } | ||||||||||
| 223 | |||||||||||
| 224 | =head2 query_start | ||||||||||
| 225 | |||||||||||
| 226 | Called by DBIx::Class when a query is begun. | ||||||||||
| 227 | |||||||||||
| 228 | =cut | ||||||||||
| 229 | |||||||||||
| 230 | sub query_start { | ||||||||||
| 231 | 13 | ▶ | my $self = shift; | ||||||||
|
Subroutine
query_start: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 232 | 13 | my $sql = shift; | |||||||||
| 233 | 13 | my @params = @_; | |||||||||
| 234 | |||||||||||
| 235 | 13 | ▶ | $self->next::method($sql, @params) if $self->passthrough; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 236 | 13 | $self->current_query( | |||||||||
| 237 | $self->query_class()->new({ | ||||||||||
| 238 | start_time => Time::HiRes::time, | ||||||||||
| 239 | sql => $sql, | ||||||||||
| 240 | params => \@params, | ||||||||||
| 241 | }) | ||||||||||
| 242 | ); | ||||||||||
| 243 | } | ||||||||||
| 244 | |||||||||||
| 245 | =head2 query_end | ||||||||||
| 246 | |||||||||||
| 247 | Called by DBIx::Class when a query is completed. | ||||||||||
| 248 | |||||||||||
| 249 | =cut | ||||||||||
| 250 | |||||||||||
| 251 | sub query_end { | ||||||||||
| 252 | 13 | ▶ | my $self = shift; | ||||||||
|
Subroutine
query_end: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 253 | |||||||||||
| 254 | 13 | ▶ | $self->next::method(@_) if $self->passthrough; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 255 | 13 | ▶ | if(defined($self->current_query)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 256 | 13 | my $q = $self->current_query; | |||||||||
| 257 | 13 | $q->end_time(Time::HiRes::time); | |||||||||
| 258 | 13 | $q->bucket($self->bucket); | |||||||||
| 259 | 13 | ▶ | if(defined($self->current_transaction)) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 260 | 7 | $self->current_transaction->add_to_queries($q); | |||||||||
| 261 | } else { | ||||||||||
| 262 | 6 | $self->add_to_log($q) | |||||||||
| 263 | } | ||||||||||
| 264 | 13 | $self->current_query(undef); | |||||||||
| 265 | } else { | ||||||||||
| 266 | 0 | warn('Completed unknown query.'); | |||||||||
| 267 | } | ||||||||||
| 268 | } | ||||||||||
| 269 | |||||||||||
| 270 | =head2 query_class | ||||||||||
| 271 | |||||||||||
| 272 | Returns the name of the class to use for storing queries, by default | ||||||||||
| 273 | C<DBIx::Class::QueryLog::Query>. If you subclass C<DBIx::Class::QueryLog>, | ||||||||||
| 274 | you may wish to override this. Whatever you override it to must be a | ||||||||||
| 275 | subclass of DBIx::Class::QueryLog::Query. | ||||||||||
| 276 | |||||||||||
| 277 | =head2 transaction_class | ||||||||||
| 278 | |||||||||||
| 279 | As query_class but for the class for storing transactions. Defaults to | ||||||||||
| 280 | C<DBIx::Class::QueryLog::Transaction>. | ||||||||||
| 281 | |||||||||||
| 282 | =cut | ||||||||||
| 283 | |||||||||||
| 284 | 13 | ▶ | sub query_class { 'DBIx::Class::QueryLog::Query' } | ||||||||
|
Subroutine
query_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 285 | |||||||||||
| 286 | 5 | ▶ | sub transaction_class { 'DBIx::Class::QueryLog::Transaction' } | ||||||||
|
Subroutine
transaction_class: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 287 | |||||||||||
| 288 | =head1 AUTHOR | ||||||||||
| 289 | |||||||||||
| 290 | Cory G Watson, C<< <gphat at cpan.org> >> | ||||||||||
| 291 | |||||||||||
| 292 | with some contributions from David Cantrell | ||||||||||
| 293 | |||||||||||
| 294 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 295 | |||||||||||
| 296 | Copyright 2009 Cory G Watson, all rights reserved. | ||||||||||
| 297 | |||||||||||
| 298 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 299 | under the same terms as Perl itself. | ||||||||||
| 300 | |||||||||||
| 301 | =cut | ||||||||||
| 302 | |||||||||||
| 303 | 6 | 1; | |||||||||