sub _count { # Separated out so pager can get the full count
my $self = shift;
- my $select = { count => '*' };
-
my $attrs = { %{$self->_resolved_attrs} };
- if (my $group_by = delete $attrs->{group_by}) {
+
+ if (my $group_by = $attrs->{group_by}) {
delete $attrs->{having};
+ delete $attrs->{order_by};
my @distinct = (ref $group_by ? @$group_by : ($group_by));
# todo: try CONCAT for multi-column pk
my @pk = $self->result_source->primary_columns;
}
}
- $select = { count => { distinct => \@distinct } };
+ $attrs->{select} = $group_by;
+ $attrs->{from} = (ref $self)->new($self->result_source, $attrs)->cursor->as_query;
}
- $attrs->{select} = $select;
+ $attrs->{select} = { count => '*' };
$attrs->{as} = [qw/count/];
- # offset, order by and page are not needed to count. record_filter is cdbi
- delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/;
+ # offset, order by, group by, where and page are not needed to count. record_filter is cdbi
+ delete $attrs->{$_} for qw/rows offset order_by group_by where page pager record_filter/;
+ $self->result_source->resultset;
my $tmp_rs = (ref $self)->new($self->result_source, $attrs);
my ($count) = $tmp_rs->cursor->next;
return $count;
sub select {
my ($self, $table, $fields, $where, $order, @rest) = @_;
+ local $self->{having_bind} = [];
if (ref $table eq 'SCALAR') {
$table = $$table;
}
+ elsif (ref $table eq 'HASH') {
+ ## what if they want to alias a sub query?
+ }
+ elsif (ref $table eq 'REF') {
+ #my ($sql, @bind) = @{${$t}}; push(@{$self->{having_bind}}, @bind;);
+ my $t = $table;
+ $table = shift @$$t;
+ while (my $b = shift @$$t) { push @{$self->{having_bind}}, $b; }
+ }
elsif (not ref $table) {
$table = $self->_quote($table);
}
@rest = (-1) unless defined $rest[0];
die "LIMIT 0 Does Not Compute" if $rest[0] == 0;
# and anyway, SQL::Abstract::Limit will cause a barf if we don't first
- local $self->{having_bind} = [];
my ($sql, @ret) = $self->SUPER::select(
$table, $self->_recurse_fields($fields), $where, $order, @rest
);