From: Robert Sedlacek Date: Wed, 26 Aug 2015 16:59:49 +0000 (+0000) Subject: override storage to access sth details X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f6e5b2758375964fd8eba24b30de87e91c6782bf;p=dbsrgits%2FDBIx-Class-RowCountStatistics.git override storage to access sth details --- diff --git a/lib/CtrlO/DBIC/Cursor/RowCountStatistics.pm b/lib/CtrlO/DBIC/Cursor/RowCountStatistics.pm index ba0a828..882cc54 100644 --- a/lib/CtrlO/DBIC/Cursor/RowCountStatistics.pm +++ b/lib/CtrlO/DBIC/Cursor/RowCountStatistics.pm @@ -4,26 +4,51 @@ use warnings; package CtrlO::DBIC::Cursor::RowCountStatistics; use Class::Method::Modifiers; +use CtrlO::DBIC::Cursor::RowCountStatistics::Storage; + use parent 'DBIx::Class::Storage::DBI::Cursor'; our $VERSION = '0.000001'; # 0.0.1 $VERSION = eval $VERSION; +around new => sub { + my $orig = shift; + my ($class, $storage, @rest) = @_; + return $orig->( + $class, + CtrlO::DBIC::Cursor::RowCountStatistics::Storage->new($storage), + @rest, + ); +}; + after next => sub { my ($self) = @_; $self->{_ctrlo_rcs_count}++ unless $self->{_done}; }; +around all => sub { + my $orig = shift; + my ($self) = @_; + my @rows = $orig->(@_); + $self->_emit_query_complete(scalar(@rows)); + return @rows; +}; + before __finish_sth => sub { my ($self) = @_; my $sql = $self->sth->{Statement}; + $self->_emit_query_complete($self->{_ctrlo_rcs_count} || 0); +}; + +sub _emit_query_complete { + my ($self, $count) = @_; $self->storage->debugobj->query_complete( - $self->{_ctrlo_rcs_count} || 0, - $sql, + $count, + $self->storage->_cached_sql, # TODO pass bind params ) if $self->storage->debug; -}; +} 1; diff --git a/lib/CtrlO/DBIC/Cursor/RowCountStatistics/Storage.pm b/lib/CtrlO/DBIC/Cursor/RowCountStatistics/Storage.pm new file mode 100644 index 0000000..005a92b --- /dev/null +++ b/lib/CtrlO/DBIC/Cursor/RowCountStatistics/Storage.pm @@ -0,0 +1,41 @@ +use strict; +use warnings; + +package CtrlO::DBIC::Cursor::RowCountStatistics::Storage; +use Class::Method::Modifiers; + +sub new { + my ($class, $original) = @_; + return bless { + original => $original, + }, $class; +} + +sub _cached_sql { + my ($self) = @_; + return $self->{cached_sql}; +} + +sub throw_exception { + my $self = shift; + return $self->{original}->throw_exception(@_); +} + +sub _select { + my $self = shift; + my @res = $self->{original}->_select(@_); + $self->{cached_sql} = $res[1]{Statement}; + return @res; +} + +sub debug { + my $self = shift; + return $self->{original}->debug(@_); +} + +sub debugobj { + my $self = shift; + return $self->{original}->debugobj(@_); +} + +1; diff --git a/t/basic.t b/t/basic.t index db72f38..c48bb6c 100644 --- a/t/basic.t +++ b/t/basic.t @@ -46,7 +46,7 @@ is $schema->storage->cursor_class, my $rs = $schema->resultset('Test'); -subtest 'simple' => sub { +subtest 'count with ->next' => sub { local @_COMPLETE; $rs->create({ id => $_ }) for 1..10; do { @@ -62,6 +62,20 @@ subtest 'simple' => sub { $rs->delete; }; +subtest 'count with ->all' => 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'; + my @all = $rows->all; + }; + is scalar(@_COMPLETE), 1, 'single complete call'; + is $_COMPLETE[0][0], 10, 'full count'; + $rs->delete; +}; + subtest 'empty' => sub { local @_COMPLETE; do {