use strict;
use warnings;
+use Carp qw/croak/;
use overload
'0+' => 'count',
+ 'bool' => sub { 1; },
fallback => 1;
use Data::Page;
use Storable;
$attrs = Storable::dclone($attrs || {}); # { %{ $attrs || {} } };
my %seen;
my $alias = ($attrs->{alias} ||= 'me');
- if (!$attrs->{select}) {
+ if ($attrs->{cols} || !$attrs->{select}) {
+ delete $attrs->{as} if $attrs->{cols};
my @cols = ($attrs->{cols}
? @{delete $attrs->{cols}}
- : $source->result_class->_select_columns);
+ : $source->columns);
$attrs->{select} = [ map { m/\./ ? $_ : "${alias}.$_" } @cols ];
}
$attrs->{as} ||= [ map { m/^$alias\.(.*)$/ ? $1 : $_ } @{$attrs->{select}} ];
sub count {
my $self = shift;
return $self->search(@_)->count if @_ && defined $_[0];
- die "Unable to ->count with a GROUP BY" if defined $self->{attrs}{group_by};
+ croak "Unable to ->count with a GROUP BY" if defined $self->{attrs}{group_by};
unless (defined $self->{count}) {
my $attrs = { %{ $self->{attrs} },
select => { 'count' => '*' },
sub update {
my ($self, $values) = @_;
- die "Values for update must be a hash" unless ref $values eq 'HASH';
+ croak "Values for update must be a hash" unless ref $values eq 'HASH';
return $self->{source}->storage->update(
$self->{source}->from, $values, $self->{cond});
}
sub update_all {
my ($self, $values) = @_;
- die "Values for update must be a hash" unless ref $values eq 'HASH';
+ croak "Values for update must be a hash" unless ref $values eq 'HASH';
foreach my $obj ($self->all) {
$obj->set_columns($values)->update;
}
sub pager {
my ($self) = @_;
my $attrs = $self->{attrs};
- die "Can't create pager for non-paged rs" unless $self->{page};
+ croak "Can't create pager for non-paged rs" unless $self->{page};
$attrs->{rows} ||= 10;
$self->count;
return $self->{pager} ||= Data::Page->new(
return defined($exists) ? $exists : $self->create($hash);
}
-=head2 self
-
- my $rs = $rs->self;
-
-Just returns the resultset. Useful for Template Toolkit.
-
-=cut
-
-sub self { shift; }
-
=head1 ATTRIBUTES
The resultset takes various attributes that modify its behavior.
For a paged resultset, how many rows per page
-=head2 group_by
+=head2 group_by (listref)
-A list of columns to group by (note that 'count' doesn't work on grouped
+A listref of columns to group by (note that 'count' doesn't work on grouped
resultsets)
+ group_by => [qw/ column1 column2 ... /]
+
=head2 distinct
Set to 1 to group by all columns