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