contributor update
[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 WARNING
77
78 This module overrides internal L<DBIx::Class> functionality, and might
79 break with every update.
80
81 =head1 DESCRIPTION
82
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
86 B<Warning>: This module currently lies on L<DBIx::Class> internals and
87 might break with an update.
88
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 {
97         my ($self, $row_count, $sql) = @_;
98         ...
99     }
100
101 =head1 SPONSORS
102
103 Development of this module was sponsored by
104
105 =over
106
107 =item * Ctrl O L<http://ctrlo.com>
108
109 =back
110
111 =head1 AUTHOR
112
113  Robert Sedlacek <r.sedlacek@shadowcat.co.uk>
114
115 =head1 CONTRIBUTORS
116
117  Matt S. Trout <mst@shadowcat.co.uk>
118
119 =head1 COPYRIGHT
120
121 Copyright (c) 2015 the DBIx::Class::RowCountStatistics L</AUTHOR> and L</CONTRIBUTORS>
122 as listed above.
123
124 =cut