fallback => 1;
sub new {
- my ($it_class, $db_class, $sth, $args, $cols) = @_;
+ my ($it_class, $db_class, $sth, $args, $cols, $attrs) = @_;
$sth->execute(@{$args || []}) unless $sth->{Active};
my $new = {
class => $db_class,
sth => $sth,
cols => $cols,
- args => $args };
+ args => $args,
+ attrs => $attrs };
return bless ($new, $it_class);
}
}
sub count {
- return scalar $_[0]->all; # So inefficient
+ my ($self) = @_;
+ if (my $cond = $self->{attrs}->{where}) {
+ my $class = $self->{class};
+ my $sth = $class->_get_sth( 'select', [ 'COUNT(*)' ],
+ $class->_table_name, $cond);
+ $sth->execute(@{$self->{args} || []});
+ my ($count) = $sth->fetchrow_array;
+ $sth->finish;
+ return $count;
+ } else {
+ return scalar $_[0]->all; # So inefficient
+ }
}
sub all {
my @cols = $class->_select_columns($attrs);
my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond);
#warn "$cond @vals";
- return $class->sth_to_objects($sth, \@vals, \@cols);
+ return $class->sth_to_objects($sth, \@vals, \@cols, { where => $cond });
}
sub sth_to_objects {
- my ($class, $sth, $args, $cols) = @_;
+ my ($class, $sth, $args, $cols, $attrs) = @_;
my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} );
my $cursor_class = $class->_cursor_class;
eval "use $cursor_class;";
- my $cursor = $cursor_class->new($class, $sth, $args, \@cols);
+ my $cursor = $cursor_class->new($class, $sth, $args, \@cols, $attrs);
return (wantarray ? $cursor->all : $cursor);
}