renamed
[dbsrgits/DBIx-Class-RowCountStatistics.git] / lib / DBIx / Class / RowCountStatistics.pm
CommitLineData
231ba12b 1use strict;
2use warnings;
3
ef422264 4package DBIx::Class::RowCountStatistics;
231ba12b 5use Class::Method::Modifiers;
6
ef422264 7use DBIx::Class::RowCountStatistics::Storage;
f6e5b275 8
231ba12b 9use parent 'DBIx::Class::Storage::DBI::Cursor';
10
11our $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 16around 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 27after next => sub {
28 my ($self) = @_;
29 $self->{_ctrlo_rcs_count}++
30 unless $self->{_done};
31};
32
f6e5b275 33around 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 45before __finish_sth => sub {
46 my ($self) = @_;
ff601bd2 47
f6e5b275 48 $self->_emit_query_complete($self->{_ctrlo_rcs_count} || 0);
49};
50
51sub _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
621;
63
64=head1 NAME
65
ef422264 66DBIx::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
231ba12b 76=head1 DESCRIPTION
77
af26217a 78This L<DBIx::Class::Storage::DBI::Cursor> subclass allows you to log the
79number of rows that were fetched through the cursor.
80
cf23f5e2 81B<Warning>: This module currently lies on L<DBIx::Class> internals and
82might break with an update.
83
af26217a 84=head1 DEBUG OBJECT METHODS
85
86=head2 query_complete (optional)
87
88If available on the L<DBIx::Class::Storage/debugobj>, this method will be
89called with the following signature:
90
91 sub query_complete {
92 my ($self, $row_count, $sql, @bind_values) = @_;
93 ...
94 }
95
cf23f5e2 96B<Note>: The passing of the C<@bind_values> is currently not yet
97implemented, and no values will be passed yet.
98
231ba12b 99=head1 AUTHOR
100
101 r.sedlacek@shadowcat.co.uk
102
103=head1 CONTRIBUTORS
104
af26217a 105None yet.
231ba12b 106
107=head1 COPYRIGHT
108
ef422264 109Copyright (c) 2015 the DBIx::Class::RowCountStatistics L</AUTHOR> and L</CONTRIBUTORS>
231ba12b 110as listed above.
111
af26217a 112=cut