return @args;
}
-sub count {
+sub _trim_attributes_for_count {
my ($self, $source, $attrs) = @_;
-
- my $tmp_attrs = { %$attrs };
+ my %attrs = %$attrs;
# take off any column specs, any pagers, record_filter is cdbi, and no point of ordering a count
- delete $attrs{$_} for (qw/columns +columns select +select as +as rows offset page pager order_by record_filter/);
- delete $tmp_attrs->{$_} for (qw/columns +columns select +select as +as rows offset page pager order_by record_filter/);
++ delete @attrs{qw/
++ columns +columns select +select as +as rows offset page pager order_by
++ record_filter/)};
+
+ return \%attrs;
+}
+
+sub count {
+ my ($self, $source, $attrs) = @_;
+
+ my $new_attrs = $self->_trim_attributes_for_count($source, $attrs);
- $tmp_attrs->{select} = { count => '*' };
- $tmp_attrs->{as} = [qw/count/];
+ $new_attrs->{select} = { count => '*' };
+ $new_attrs->{as} = [qw/count/];
- my $tmp_rs = $source->resultset_class->new($source, $tmp_attrs);
+ my $tmp_rs = $source->resultset_class->new($source, $new_attrs);
my ($count) = $tmp_rs->cursor->next;
+ # if the offset/rows attributes are still present, we did not use
+ # a subquery, so we need to make the calculations in software
+ $count -= $attrs->{offset} if $attrs->{offset};
+ $count = $attrs->{rows} if $attrs->{rows} and $attrs->{rows} < $count;
+ $count = 0 if ($count < 0);
+
return $count;
}