--- /dev/null
+use strict;
+use warnings;
+
+package CtrlO::DBIC::Cursor::RowCountStatistics;
+use Class::Method::Modifiers;
+
+use parent 'DBIx::Class::Storage::DBI::Cursor';
+
+our $VERSION = '0.000001'; # 0.0.1
+$VERSION = eval $VERSION;
+
+after next => sub {
+ my ($self) = @_;
+ $self->{_ctrlo_rcs_count}++
+ unless $self->{_done};
+};
+
+before __finish_sth => sub {
+ my ($self) = @_;
+ my $sql = $self->sth->{Statement};
+ $self->storage->debugobj->query_complete(
+ $self->{_ctrlo_rcs_count} || 0,
+ $sql,
+ # TODO pass bind params
+ ) if $self->storage->debug;
+};
+
+1;
+
+=head1 NAME
+
+CtrlO::DBIC::Cursor::RowCountStatistics - Description goes here
+
+=head1 SYNOPSIS
+
+=head1 DESCRIPTION
+
+=head1 AUTHOR
+
+ r.sedlacek@shadowcat.co.uk
+
+=head1 CONTRIBUTORS
+
+None yet - maybe this software is perfect! (ahahahahahahahahaha)
+
+=head1 COPYRIGHT
+
+Copyright (c) 2015 the CtrlO::DBIC::Cursor::RowCountStatistics L</AUTHOR> and L</CONTRIBUTORS>
+as listed above.
+
+=head1 LICENSE
+
+This library is free software and may be distributed under the same terms
+as perl itself.
--- /dev/null
+use strict;
+use warnings;
+
+use Test::More;
+use FindBin;
+
+BEGIN {
+ push @INC, "$FindBin::Bin/lib";
+}
+
+use TestSchema;
+use CtrlO::DBIC::Cursor::RowCountStatistics;
+
+our @_COMPLETE;
+
+do {
+ package TestStats;
+ use parent 'DBIx::Class::Storage::Statistics';
+ sub new {
+ bless {}, shift;
+ }
+ sub query_start { }
+ sub query_end { }
+ sub query_complete {
+ my ($self, @args) = @_;
+ push @_COMPLETE, \@args;
+ }
+};
+
+my $schema = TestSchema->connect(
+ 'dbi:SQLite:dbname=:memory:',
+ undef, undef,
+ {},
+ {
+ cursor_class => 'CtrlO::DBIC::Cursor::RowCountStatistics',
+ }
+);
+$schema->deploy();
+$schema->storage->debugobj(TestStats->new);
+$schema->storage->debug(1);
+
+ok $schema, 'schema created';
+is $schema->storage->cursor_class,
+ 'CtrlO::DBIC::Cursor::RowCountStatistics',
+ 'cursor_class set';
+
+my $rs = $schema->resultset('Test');
+
+subtest 'simple' => sub {
+ local @_COMPLETE;
+ $rs->create({ id => $_ }) for 1..10;
+ do {
+ my $rows = $rs->search_rs;
+ is ref($rows->cursor), 'CtrlO::DBIC::Cursor::RowCountStatistics',
+ 'resultset cursor';
+ 1 while $rows->next;
+ };
+ is scalar(@_COMPLETE), 1, 'single complete call';
+ is $_COMPLETE[0][0], 10, 'full count';
+ $rs->delete;
+};
+
+subtest 'empty' => sub {
+ local @_COMPLETE;
+ do {
+ my $rows = $rs->search_rs;
+ 1 while $rows->next;
+ };
+ is scalar(@_COMPLETE), 1, 'single complete call';
+ is $_COMPLETE[0][0], 0, 'full count';
+};
+
+subtest 'no fetch' => sub {
+ local @_COMPLETE;
+ $rs->create({ id => $_ }) for 1..10;
+ do {
+ my $rows = $rs->search_rs;
+ };
+ is scalar(@_COMPLETE), 0, 'no complete calls';
+ $rs->delete;
+};
+
+done_testing;
--- /dev/null
+use strict;
+use warnings;
+
+package TestSchema::Result::Test;
+use parent 'DBIx::Class::Core';
+
+__PACKAGE__->table('test');
+__PACKAGE__->add_columns(
+ id => { data_type => 'integer', },
+);
+__PACKAGE__->set_primary_key('id');
+
+1;