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