Commit | Line | Data |
62fa8aec |
1 | package DBIx::Class::Storage::DBI::Role::QueryCounter; |
2 | |
3 | use Moose::Role; |
4 | requires '_query_start'; |
5 | |
6 | =head1 NAME |
7 | |
92bc2a19 |
8 | DBIx::Class::Storage::DBI::Role::QueryCounter - Role to add a query counter |
62fa8aec |
9 | |
10 | =head1 SYNOPSIS |
11 | |
12 | my $query_count = $schema->storage->query_count; |
13 | |
14 | =head1 DESCRIPTION |
15 | |
16 | Each time the schema does a query, increment the counter. |
17 | |
18 | =head1 ATTRIBUTES |
19 | |
20 | This package defines the following attributes. |
21 | |
48580715 |
22 | =head2 _query_count |
62fa8aec |
23 | |
24 | Is the attribute holding the current query count. It defines a public reader |
25 | called 'query_count' which you can use to access the total number of queries |
26 | that DBIC has run since connection. |
27 | |
28 | =cut |
29 | |
30 | has '_query_count' => ( |
31 | reader=>'query_count', |
32 | writer=>'_set_query_count', |
33 | isa=>'Int', |
34 | required=>1, |
35 | default=>0, |
36 | ); |
37 | |
38 | |
39 | =head1 METHODS |
40 | |
41 | This module defines the following methods. |
42 | |
43 | =head2 _query_start |
44 | |
48580715 |
45 | Override on the method so that we count the queries. |
62fa8aec |
46 | |
47 | =cut |
48 | |
49 | around '_query_start' => sub { |
50 | my ($_query_start, $self, @args) = @_; |
51 | $self->_increment_query_count; |
52 | return $self->$_query_start(@args); |
53 | }; |
54 | |
55 | |
56 | =head2 _increment_query_count |
57 | |
58 | Used internally. You won't need this unless you enjoy messing with the query |
59 | count. |
60 | |
61 | =cut |
62 | |
63 | sub _increment_query_count { |
64 | my $self = shift @_; |
65 | my $current = $self->query_count; |
66 | $self->_set_query_count(++$current); |
67 | } |
68 | |
69 | |
70 | =head1 AUTHORS |
71 | |
72 | See L<DBIx::Class> for more information regarding authors. |
73 | |
74 | =head1 LICENSE |
75 | |
76 | You may distribute this code under the same terms as Perl itself. |
77 | |
78 | =cut |
79 | |
80 | |
92bc2a19 |
81 | 1; |