use strict;
use warnings;
+use Class::C3;
use base qw/DBIx::Class::Storage::DBI/;
use Carp::Clan qw/^DBIx::Class/;
my ($self, $source, $attrs) = @_;
if (exists $attrs->{rows}) {
+ my $offset = $attrs->{offset} || 0;
+ my $total = $attrs->{rows} + $offset;
+
my $new_attrs = $self->_trim_attributes_for_count($source, $attrs);
- $new_attrs->{select} = '1';
- $new_attrs->{as} = ['dummy'];
+ my $query = $source->resultset_class->new($source, $new_attrs)->as_query;
-# speed things up at least *a little*
- $new_attrs->{result_class} = 'DBIx::Class::ResultClass::HashRefInflator';
+ my $top_attrs = {};
+ $top_attrs->{from} = [{
+ top_subq => $query
+ }];
+ $top_attrs->{select} = "TOP $total 1";
+ $top_attrs->{as} = ['total'];
- my $offset = $attrs->{offset} || 0;
- my $total = $attrs->{rows} + $offset;
-
- $self->dbh->do("set rowcount $total");
+ my $top_query = $source->resultset_class->new($source, $top_attrs)->as_query;
+
+ my $count_attrs = {};
+ $count_attrs->{from} = [{
+ count_subq => $top_query
+ }];
+ $count_attrs->{select} = { count => '*' };
+ $count_attrs->{as} = ['count'];
- my $tmp_rs = $source->resultset_class->new($source, $new_attrs);
-
- my $count = 0;
- $count++ while $tmp_rs->cursor->next;
+ my $tmp_rs = $source->resultset_class->new($source, $count_attrs);
+ my ($count) = $tmp_rs->cursor->next;
- $self->dbh->do("set rowcount 0");
+ $count -= $offset;
+ $count = $attrs->{rows} if $count > $attrs->{rows};
return $count;
}
plan skip_all => 'Set $ENV{DBICTEST_SYBASE_DSN}, _USER and _PASS to run this test'
unless ($dsn && $user);
-plan tests => 20*2;
+plan tests => (16 + 4*2)*2;
my @storage_types = (
'DBI::Sybase',
is( $it->next->name, "Artist 2", "iterator->next ok" );
is( $it->next, undef, "next past end of resultset ok" );
+# now try with offset
+ $it = $schema->resultset('Artist')->search({}, {
+ rows => 3,
+ offset => 3,
+ order_by => 'artistid',
+ });
+
+ is( $it->count, 3, "LIMIT with offset count ok" );
+
+ is( $it->next->name, "Artist 3", "iterator->next ok" );
+ $it->next;
+ is( $it->next->name, "Artist 5", "iterator->next ok" );
+ is( $it->next, undef, "next past end of resultset ok" );
+
SKIP: {
- skip 'quoting bug with NoBindVars', 8
+ skip 'quoting bug with NoBindVars', 4*2
if $storage_type eq 'DBI::Sybase::NoBindVars';
# Test DateTime inflation with DATETIME