=head1 METHODS
-=head2 new($db_class, \%$attrs)
+=head2 new($source, \%$attrs)
-The resultset constructor. Takes a table class and an attribute hash
-(see below for more information on attributes). Does not perform
-any queries -- these are executed as needed by the other methods.
+The resultset constructor. Takes a source object (usually a DBIx::Class::Table)
+and an attribute hash (see below for more information on attributes). Does
+not perform any queries -- these are executed as needed by the other methods.
=cut
sub new {
- my ($class, $db_class, $attrs) = @_;
+ my ($class, $source, $attrs) = @_;
#use Data::Dumper; warn Dumper(@_);
$class = ref $class if ref $class;
$attrs = { %{ $attrs || {} } };
my %seen;
- $attrs->{cols} ||= [ map { "me.$_" } $db_class->_select_columns ];
- $attrs->{from} ||= [ { 'me' => $db_class->_table_name } ];
+ if (!$attrs->{select}) {
+ my @cols = ($attrs->{cols}
+ ? @{delete $attrs->{cols}}
+ : $source->result_class->_select_columns);
+ $attrs->{select} = [ map { m/\./ ? $_ : "me.$_" } @cols ];
+ }
+ $attrs->{as} ||= [ map { m/^me\.(.*)$/ ? $1 : $_ } @{$attrs->{select}} ];
+ #use Data::Dumper; warn Dumper(@{$attrs}{qw/select as/});
+ $attrs->{from} ||= [ { 'me' => $source->name } ];
if ($attrs->{join}) {
foreach my $j (ref $attrs->{join} eq 'ARRAY'
? (@{$attrs->{join}}) : ($attrs->{join})) {
$seen{$j} = 1;
}
}
- push(@{$attrs->{from}}, $db_class->_resolve_join($attrs->{join}, 'me'));
+ push(@{$attrs->{from}}, $source->result_class->_resolve_join($attrs->{join}, 'me'));
}
+ $attrs->{group_by} ||= $attrs->{select} if delete $attrs->{distinct};
foreach my $pre (@{$attrs->{prefetch} || []}) {
- push(@{$attrs->{from}}, $db_class->_resolve_join($pre, 'me'))
+ push(@{$attrs->{from}}, $source->result_class->_resolve_join($pre, 'me'))
unless $seen{$pre};
- push(@{$attrs->{cols}},
+ my @pre =
map { "$pre.$_" }
- $db_class->_relationships->{$pre}->{class}->_select_columns);
+ $source->result_class->_relationships->{$pre}->{class}->columns;
+ push(@{$attrs->{select}}, @pre);
+ push(@{$attrs->{as}}, @pre);
}
my $new = {
- source => $db_class,
- cols => $attrs->{cols},
+ source => $source,
cond => $attrs->{where},
from => $attrs->{from},
count => undef,
pager => undef,
attrs => $attrs };
bless ($new, $class);
- $new->pager if ($attrs->{page});
+ $new->pager if $attrs->{page};
return $new;
}
$attrs->{offset} = $self->pager->skipped;
}
return $self->{cursor}
- ||= $source->storage->select($self->{from}, $self->{cols},
+ ||= $source->storage->select($self->{from}, $attrs->{select},
$attrs->{where},$attrs);
}
sub slice {
my ($self, $min, $max) = @_;
my $attrs = { %{ $self->{attrs} || {} } };
- $self->{source}->throw("Can't slice without where") unless $attrs->{where};
+ $self->{source}->result_class->throw("Can't slice without where") unless $attrs->{where};
$attrs->{offset} = $min;
$attrs->{rows} = ($max ? ($max - $min + 1) : 1);
my $slice = $self->new($self->{source}, $attrs);
sub _construct_object {
my ($self, @row) = @_;
- my @cols = @{ $self->{attrs}{cols} };
- s/^me\.// for @cols;
+ my @cols = @{ $self->{attrs}{as} };
+ #warn "@cols -> @row";
@cols = grep { /\(/ or ! /\./ } @cols;
my $new;
unless ($self->{attrs}{prefetch}) {
- $new = $self->{source}->_row_to_object(\@cols, \@row);
+ $new = $self->{source}->result_class->_row_to_object(\@cols, \@row);
} else {
my @main = splice(@row, 0, scalar @cols);
- $new = $self->{source}->_row_to_object(\@cols, \@main);
+ $new = $self->{source}->result_class->_row_to_object(\@cols, \@main);
PRE: foreach my $pre (@{$self->{attrs}{prefetch}}) {
- my $rel_obj = $self->{source}->_relationships->{$pre};
- my $pre_class = $self->{source}->resolve_class($rel_obj->{class});
+ my $rel_obj = $self->{source}->result_class->_relationships->{$pre};
+ my $pre_class = $self->{source}->result_class->resolve_class($rel_obj->{class});
my @pre_cols = $pre_class->_select_columns;
my @vals = splice(@row, 0, scalar @pre_cols);
my $fetched = $pre_class->_row_to_object(\@pre_cols, \@vals);
- $self->{source}->throw("No accessor for prefetched $pre")
+ $self->{source}->result_class->throw("No accessor for prefetched $pre")
unless defined $rel_obj->{attrs}{accessor};
if ($rel_obj->{attrs}{accessor} eq 'single') {
foreach my $pri ($rel_obj->{class}->primary_columns) {
} elsif ($rel_obj->{attrs}{accessor} eq 'filter') {
$new->{_inflated_column}{$pre} = $fetched;
} else {
- $self->{source}->throw("Don't know how to store prefetched $pre");
+ $self->{source}->result_class->throw("Don't know how to store prefetched $pre");
}
}
}
sub count {
my $self = shift;
return $self->search(@_)->count if @_ && defined $_[0];
- my $attrs = { %{ $self->{attrs} } };
+ die "Unable to ->count with a GROUP BY" if defined $self->{attrs}{group_by};
unless ($self->{count}) {
- # offset and order by are not needed to count
- delete $attrs->{$_} for qw/offset order_by/;
+ my $attrs = { %{ $self->{attrs} },
+ select => { 'count' => '*' },
+ as => [ 'count' ] };
+ # offset and order by are not needed to count, page, join and prefetch
+ # will get in the way (add themselves to from again ...)
+ delete $attrs->{$_} for qw/offset order_by page join prefetch/;
my @cols = 'COUNT(*)';
- $self->{count} = $self->{source}->storage->select_single(
- $self->{from}, \@cols, $self->{cond}, $attrs);
+ ($self->{count}) = $self->search(undef, $attrs)->cursor->next;
}
return 0 unless $self->{count};
return $self->{pager}->entries_on_this_page if ($self->{pager});
- return ( $attrs->{rows} && $attrs->{rows} < $self->{count} )
- ? $attrs->{rows}
+ return ( $self->{attrs}->{rows} && $self->{attrs}->{rows} < $self->{count} )
+ ? $self->{attrs}->{rows}
: $self->{count};
}
through directly to SQL, so you can give e.g. C<foo DESC> for a
descending order.
-=head2 cols
+=head2 cols (arrayref)
+
+Shortcut to request a particular set of columns to be retrieved - adds
+'me.' onto the start of any column without a '.' in it and sets 'select'
+from that, then auto-populates 'as' from 'select' as normal
+
+=head2 select (arrayref)
+
+Indicates which columns should be selected from the storage
+
+=head2 as (arrayref)
-Which columns should be retrieved.
+Indicates column names for object inflation
=head2 join
For a paged resultset, how many rows per page
+=head2 group_by
+
+A list of columns to group by (note that 'count' doesn't work on grouped
+resultsets)
+
+=head2 distinct
+
+Set to 1 to group by all columns
+
=cut
1;