add dbic internals warning and todo note about bind values
[dbsrgits/DBIx-Class-RowCountStatistics.git] / lib / CtrlO / DBIC / Cursor / RowCountStatistics.pm
1 use strict;
2 use warnings;
3
4 package CtrlO::DBIC::Cursor::RowCountStatistics;
5 use Class::Method::Modifiers;
6
7 use CtrlO::DBIC::Cursor::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 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
24 after next => sub {
25     my ($self) = @_;
26     $self->{_ctrlo_rcs_count}++
27         unless $self->{_done};
28 };
29
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
38 before __finish_sth => sub {
39     my ($self) = @_;
40     my $sql = $self->sth->{Statement};
41     $self->_emit_query_complete($self->{_ctrlo_rcs_count} || 0);
42 };
43
44 sub _emit_query_complete {
45     my ($self, $count) = @_;
46     $self->storage->debugobj->query_complete(
47         $count,
48         $self->storage->_cached_sql,
49         # TODO pass bind params
50     ) if $self->storage->debug
51         and $self->storage->debugobj->can('query_complete');
52 }
53
54 1;
55
56 =head1 NAME
57
58 CtrlO::DBIC::Cursor::RowCountStatistics - Provide row-count statistics
59
60 =head1 SYNOPSIS
61
62     my $schema = Schema->connect(
63         $dsn, $username, $password,
64         {},
65         { cursor_class => 'CtrlO::DBIC::Cursor::RowCountStatistics' },
66     );
67
68 =head1 DESCRIPTION
69
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
73 B<Warning>: This module currently lies on L<DBIx::Class> internals and
74 might break with an update.
75
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
88 B<Note>: The passing of the C<@bind_values> is currently not yet
89 implemented, and no values will be passed yet.
90
91 =head1 AUTHOR
92
93  r.sedlacek@shadowcat.co.uk
94
95 =head1 CONTRIBUTORS
96
97 None yet.
98
99 =head1 COPYRIGHT
100
101 Copyright (c) 2015 the CtrlO::DBIC::Cursor::RowCountStatistics L</AUTHOR> and L</CONTRIBUTORS>
102 as listed above.
103
104 =cut