X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FResultSet.pm;h=e52e17922b675e5a64996bbaab6e6558119976ee;hb=54540863adce71e931685a37d33e37650e5feb5e;hp=8b8da5fbfea7857596b92eec9b21f72ce1e1c65d;hpb=bfab575afa37545ee175b824cea554c9c37ab6f5;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 8b8da5f..e52e179 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -24,22 +24,29 @@ or a C relationship. =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})) { @@ -49,25 +56,27 @@ sub new { $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; } @@ -136,7 +145,7 @@ sub cursor { $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); } @@ -166,7 +175,7 @@ Returns a subset of elements from the resultset. 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); @@ -188,22 +197,22 @@ sub next { 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) { @@ -216,7 +225,7 @@ sub _construct_object { } 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"); } } } @@ -236,19 +245,22 @@ on the resultset and counts the results of that. 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}; } @@ -351,9 +363,19 @@ Which column(s) to order the results by. This is currently passed through directly to SQL, so you can give e.g. C 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 @@ -390,6 +412,15 @@ for an unpaged resultset. 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;