Commit | Line | Data |
231ba12b |
1 | use strict; |
2 | use warnings; |
3 | |
ef422264 |
4 | package DBIx::Class::RowCountStatistics; |
231ba12b |
5 | use Class::Method::Modifiers; |
6 | |
ef422264 |
7 | use DBIx::Class::RowCountStatistics::Storage; |
f6e5b275 |
8 | |
231ba12b |
9 | use parent 'DBIx::Class::Storage::DBI::Cursor'; |
10 | |
11 | our $VERSION = '0.000001'; # 0.0.1 |
12 | $VERSION = eval $VERSION; |
13 | |
7135993c |
14 | # Wrap the original ::Storage::DBI we received so we can extract data |
15 | # from it's usage by the Cursor. |
f6e5b275 |
16 | around new => sub { |
17 | my $orig = shift; |
18 | my ($class, $storage, @rest) = @_; |
7135993c |
19 | |
f6e5b275 |
20 | return $orig->( |
21 | $class, |
ef422264 |
22 | DBIx::Class::RowCountStatistics::Storage->new($storage), |
f6e5b275 |
23 | @rest, |
24 | ); |
25 | }; |
26 | |
231ba12b |
27 | after next => sub { |
28 | my ($self) = @_; |
29 | $self->{_ctrlo_rcs_count}++ |
30 | unless $self->{_done}; |
31 | }; |
32 | |
f6e5b275 |
33 | around all => sub { |
34 | my $orig = shift; |
35 | my ($self) = @_; |
7135993c |
36 | |
f6e5b275 |
37 | my @rows = $orig->(@_); |
7135993c |
38 | |
39 | # ->all does its own finalization, so we fire the complete event |
40 | # right here. |
f6e5b275 |
41 | $self->_emit_query_complete(scalar(@rows)); |
42 | return @rows; |
43 | }; |
44 | |
231ba12b |
45 | before __finish_sth => sub { |
46 | my ($self) = @_; |
ff601bd2 |
47 | |
f6e5b275 |
48 | $self->_emit_query_complete($self->{_ctrlo_rcs_count} || 0); |
49 | }; |
50 | |
51 | sub _emit_query_complete { |
52 | my ($self, $count) = @_; |
7135993c |
53 | |
231ba12b |
54 | $self->storage->debugobj->query_complete( |
f6e5b275 |
55 | $count, |
56 | $self->storage->_cached_sql, |
231ba12b |
57 | # TODO pass bind params |
dcf77f95 |
58 | ) if $self->storage->debug |
59 | and $self->storage->debugobj->can('query_complete'); |
f6e5b275 |
60 | } |
231ba12b |
61 | |
62 | 1; |
63 | |
64 | =head1 NAME |
65 | |
ef422264 |
66 | DBIx::Class::RowCountStatistics - Provide row-count statistics |
231ba12b |
67 | |
68 | =head1 SYNOPSIS |
69 | |
af26217a |
70 | my $schema = Schema->connect( |
71 | $dsn, $username, $password, |
72 | {}, |
ef422264 |
73 | { cursor_class => 'DBIx::Class::RowCountStatistics' }, |
af26217a |
74 | ); |
75 | |
03f37b20 |
76 | =head1 WARNING |
77 | |
78 | This module overrides internal L<DBIx::Class> functionality, and might |
79 | break with every update. |
80 | |
231ba12b |
81 | =head1 DESCRIPTION |
82 | |
af26217a |
83 | This L<DBIx::Class::Storage::DBI::Cursor> subclass allows you to log the |
84 | number of rows that were fetched through the cursor. |
85 | |
cf23f5e2 |
86 | B<Warning>: This module currently lies on L<DBIx::Class> internals and |
87 | might break with an update. |
88 | |
af26217a |
89 | =head1 DEBUG OBJECT METHODS |
90 | |
91 | =head2 query_complete (optional) |
92 | |
93 | If available on the L<DBIx::Class::Storage/debugobj>, this method will be |
94 | called with the following signature: |
95 | |
96 | sub query_complete { |
03f37b20 |
97 | my ($self, $row_count, $sql) = @_; |
af26217a |
98 | ... |
99 | } |
100 | |
231ba12b |
101 | =head1 AUTHOR |
102 | |
103 | r.sedlacek@shadowcat.co.uk |
104 | |
105 | =head1 CONTRIBUTORS |
106 | |
af26217a |
107 | None yet. |
231ba12b |
108 | |
109 | =head1 COPYRIGHT |
110 | |
ef422264 |
111 | Copyright (c) 2015 the DBIx::Class::RowCountStatistics L</AUTHOR> and L</CONTRIBUTORS> |
231ba12b |
112 | as listed above. |
113 | |
af26217a |
114 | =cut |