From: Dagfinn Ilmari Mannsåker Date: Fri, 26 Feb 2016 11:00:39 +0000 (+0000) Subject: WIP: Make ->count call ->count_rs->first unless software_limit=1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fwip%2Fcount-no-soft-limit;p=dbsrgits%2FDBIx-Class.git WIP: Make ->count call ->count_rs->first unless software_limit=1 This removes the (at least sometimes) bogus "optimisation" of not passing the rows/offset to the DB when counting, unless explicitly asked for. It may have to be reinstated on a per-DB (or per limit dialect) basis. Also removes bogus ->count test in fetch_first, since a) software_limit completely removes the paging logic from ->count. b) SQLite doesn't support FETCH FIRST x ROWS ONLY TODO: Figure out if ->count can be further simplified. --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 0a6c002..2531df2 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1594,6 +1594,8 @@ sub count { my $attrs = { %{ $self->_resolved_attrs } }; + return $self->count_rs->first unless $attrs->{software_limit}; + # this is a little optimization - it is faster to do the limit # adjustments in software, instead of a subquery my ($rows, $offset) = delete @{$attrs}{qw/rows offset/}; diff --git a/t/count/count_rs.t b/t/count/count_rs.t index 174f630..af2eeb3 100644 --- a/t/count/count_rs.t +++ b/t/count/count_rs.t @@ -31,7 +31,7 @@ my $schema = DBICTest->init_schema(); is ($rs->all, 2, 'Correct number of objects'); $schema->is_executed_sql_bind( sub { - is ($rs->count, 2, 'Correct count via count()'); + is ($rs->count({}, { software_limit => 1 }), 2, 'Correct count via count()'); }, [[ 'SELECT COUNT( * ) FROM cd me @@ -78,7 +78,7 @@ my $schema = DBICTest->init_schema(); is ($rs->all, 1, 'Correct number of objects'); $schema->is_executed_sql_bind( sub { - is ($rs->count, 1, 'Correct count via count()'); + is ($rs->count({}, { software_limit => 1 }), 1, 'Correct count via count()'); }, [ [ 'SELECT COUNT( * ) FROM ( diff --git a/t/sqlmaker/limit_dialects/fetch_first.t b/t/sqlmaker/limit_dialects/fetch_first.t index ab3e170..338401b 100644 --- a/t/sqlmaker/limit_dialects/fetch_first.t +++ b/t/sqlmaker/limit_dialects/fetch_first.t @@ -134,9 +134,6 @@ for my $ord_set ( my $rs = $books_45_and_owners->search ({}, {order_by => $ord_set->{order_by}}); - # query actually works - ok( defined $rs->count, 'Query actually works' ); - is_same_sql_bind( $rs->as_query, "(SELECT me.id, me.source, me.owner, me.price, owner__id, owner__name