X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FResultSet.pm;h=6c1066c82c1e7cbde14bc9f8f13de05d32f1b1a4;hb=992442b3fc60cb51bdaf45c25f169767c1cbc7eb;hp=6ab3ec08344aef2970cafd8c9a9cae1810a6af0f;hpb=c6a0dde1edec30bc70565689ae3b978db9677f15;p=dbsrgits%2FDBIx-Class.git diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 6ab3ec0..6c1066c 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -86,6 +86,7 @@ sub new { my ($source, $attrs) = @_; #weaken $source; + $attrs = { %{$attrs||{}} }; if ($attrs->{page}) { $attrs->{rows} ||= 10; @@ -343,6 +344,22 @@ sub find { $input_query = {@_}; } + my (%related, $info); + + foreach my $key (keys %$input_query) { + if (ref($input_query->{$key}) + && ($info = $self->result_source->relationship_info($key))) { + my $rel_q = $self->result_source->resolve_condition( + $info->{cond}, delete $input_query->{$key}, $key + ); + die "Can't handle OR join condition in find" if ref($rel_q) eq 'ARRAY'; + @related{keys %$rel_q} = values %$rel_q; + } + } + if (my @keys = keys %related) { + @{$input_query}{@keys} = values %related; + } + my @unique_queries = $self->_unique_queries($input_query, $attrs); # Build the final query: Default to the disjunction of the unique queries, @@ -392,21 +409,23 @@ sub _unique_queries { ? ($attrs->{key}) : $self->result_source->unique_constraint_names; + my $where = $self->_collapse_cond($self->{attrs}{where} || {}); + my $num_where = scalar keys %$where; + my @unique_queries; foreach my $name (@constraint_names) { my @unique_cols = $self->result_source->unique_constraint_columns($name); my $unique_query = $self->_build_unique_query($query, \@unique_cols); + my $num_cols = scalar @unique_cols; my $num_query = scalar keys %$unique_query; - next unless $num_query; - # XXX: Assuming quite a bit about $self->{attrs}{where} - my $num_cols = scalar @unique_cols; - my $num_where = exists $self->{attrs}{where} - ? scalar keys %{ $self->{attrs}{where} } - : 0; - push @unique_queries, $unique_query - if $num_query + $num_where == $num_cols; + my $total = $num_query + $num_where; + if ($num_query && ($num_query == $num_cols || $total == $num_cols)) { + # The query is either unique on its own or is unique in combination with + # the existing where clause + push @unique_queries, $unique_query; + } } return @unique_queries; @@ -520,7 +539,7 @@ sub single { $attrs->{where}, $attrs ); - return (@data ? $self->_construct_object(@data) : ()); + return (@data ? ($self->_construct_object(@data))[0] : ()); } # _is_unique_query @@ -705,22 +724,29 @@ sub next { $self->{all_cache_position} = 1; return ($self->all)[0]; } + if ($self->{stashed_objects}) { + my $obj = shift(@{$self->{stashed_objects}}); + delete $self->{stashed_objects} unless @{$self->{stashed_objects}}; + return $obj; + } my @row = ( exists $self->{stashed_row} ? @{delete $self->{stashed_row}} : $self->cursor->next ); return unless (@row); - return $self->_construct_object(@row); + my ($row, @more) = $self->_construct_object(@row); + $self->{stashed_objects} = \@more if @more; + return $row; } sub _construct_object { my ($self, @row) = @_; my $info = $self->_collapse_result($self->{_attrs}{as}, \@row); - my $new = $self->result_class->inflate_result($self->result_source, @$info); - $new = $self->{_attrs}{record_filter}->($new) + my @new = $self->result_class->inflate_result($self->result_source, @$info); + @new = $self->{_attrs}{record_filter}->(@new) if exists $self->{_attrs}{record_filter}; - return $new; + return @new; } sub _collapse_result { @@ -1234,10 +1260,10 @@ sub new_result { my %new = ( %{ $self->_remove_alias($values, $alias) }, %{ $self->_remove_alias($collapsed_cond, $alias) }, + -result_source => $self->result_source, ); my $obj = $self->result_class->new(\%new); - $obj->result_source($self->result_source) if $obj->can('result_source'); return $obj; } @@ -1778,7 +1804,9 @@ Shortcut to include additional columns in the returned results - for example }); would return all CDs and include a 'name' column to the information -passed to object inflation +passed to object inflation. Note that the 'artist' is the name of the +column (or relationship) accessor, and 'name' is the name of the column +accessor in the related table. =head2 select @@ -1829,8 +1857,14 @@ Indicates additional column names for those added via L<+select>. =back -Indicates column names for object inflation. This is used in conjunction with -C contains one or more function or stored +Indicates column names for object inflation. That is, c< as > +indicates the name that the column can be accessed as via the +C method (or via the object accessor, B). It has nothing to do with the SQL code C< SELECT foo AS bar +>. + +The C< as > attribute is used in conjunction with C contains one or more function or stored procedure names: $rs = $schema->resultset('Employee')->search(undef, {