From: Daniel Ruoso Date: Wed, 24 Nov 2010 17:35:02 +0000 (-0300) Subject: When getting a related_resultset in a row with custom rels without the extended defin... X-Git-Tag: v0.08190~1^2~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7689b9e5b93232a57cbc49de64667317804cd3f6;p=dbsrgits%2FDBIx-Class.git When getting a related_resultset in a row with custom rels without the extended definition, we step back, get a rs equivalent to the current row and do a search_related there. --- diff --git a/lib/DBIx/Class/Relationship/Base.pm b/lib/DBIx/Class/Relationship/Base.pm index 1a46d4e..3d0b44c 100644 --- a/lib/DBIx/Class/Relationship/Base.pm +++ b/lib/DBIx/Class/Relationship/Base.pm @@ -447,57 +447,48 @@ sub related_resultset { } } - # this is where we're going to check if we have an extended - # rel. In that case, we need to: 1) If there's a second - # condition, we use that instead. 2) If there is only one - # condition, we need to join the current resultsource and have - # additional conditions. - if (ref $rel_info->{cond} eq 'CODE') { - # this is an extended relationship. - if ($extended_cond) { + if (ref $rel_info->{cond} eq 'CODE' && !$extended_cond) { + # since we don't have the extended condition, we need to step + # back, get a resultset for the current row and do a + # search_related there. + my $row_srcname = $source->source_name; + my %identity = map { ( $_ => $self->get_column($_) ) } $source->primary_columns; + my $row_rs = $source->schema->resultset($row_srcname)->search(\%identity); + + $row_rs->search_related($rel, $query, $attrs); + + } else { + # when we have the extended condition or we have a simple + # relationship declaration, it can optimize the JOIN away by + # simply adding the identity in WHERE. + + if (ref $rel_info->{cond} eq 'CODE' && $extended_cond) { $cond = $extended_cond; - - } else { - - # it's a bit hard to find out what to do with other joins - $self->throw_exception('Extended relationship '.$rel.' with additional join requires optimized declaration') - if exists $attrs->{join} && $attrs->{join}; - - # aliases get a bit more complicated, so we won't accept additional queries - $self->throw_exception('Extended relationship '.$rel.' with additional query requires optimized declaration') - if $query; - - $attrs->{from} = - [ { $rel => $self->result_source->from }, - [ { 'me' => $self->result_source->related_source($rel)->from }, { 1 => 1 } ] ]; - - $cond->{"${rel}.${_}"} = $self->get_column($_) for $self->result_source->primary_columns; } - } - if (ref $cond eq 'ARRAY') { - $cond = [ map { - if (ref $_ eq 'HASH') { - my $hash; - foreach my $key (keys %$_) { - my $newkey = $key !~ /\./ ? "me.$key" : $key; - $hash->{$newkey} = $_->{$key}; + if (ref $cond eq 'ARRAY') { + $cond = [ map { + if (ref $_ eq 'HASH') { + my $hash; + foreach my $key (keys %$_) { + my $newkey = $key !~ /\./ ? "me.$key" : $key; + $hash->{$newkey} = $_->{$key}; + } + $hash; + } else { + $_; } - $hash; - } else { - $_; + } @$cond ]; + } elsif (ref $cond eq 'HASH') { + foreach my $key (grep { ! /\./ } keys %$cond) { + $cond->{"me.$key"} = delete $cond->{$key}; } - } @$cond ]; - } elsif (ref $cond eq 'HASH') { - foreach my $key (grep { ! /\./ } keys %$cond) { - $cond->{"me.$key"} = delete $cond->{$key}; } - } - $query = ($query ? { '-and' => [ $cond, $query ] } : $cond); - $self->result_source->related_source($rel)->resultset->search( - $query, $attrs - ); + $query = ($query ? { '-and' => [ $cond, $query ] } : $cond); + $self->result_source->related_source($rel)->resultset->search( + $query, $attrs); + } }; } diff --git a/t/relationship/custom.t b/t/relationship/custom.t index 24556f9..a7bb130 100644 --- a/t/relationship/custom.t +++ b/t/relationship/custom.t @@ -5,6 +5,7 @@ use Test::More; use Test::Exception; use lib qw(t/lib); use DBICTest; +use DBIC::SqlMakerTest; my $schema = DBICTest->init_schema(); @@ -29,14 +30,38 @@ foreach my $artwork (@artworks) { $artwork->create_related('artwork_to_artist', { artist => $_ }) for ($artist3, $artist4); } -my @cds_80s = $artist->cds_80s; +my $cds_80s_rs = $artist->cds_80s; +is_same_sql_bind($cds_80s_rs->as_query, + '(SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me'. + ' WHERE ( ( me.artist = ? AND ( me.year < ? AND me.year > ? ) ) ))', + [ + [ 'me.artist' => 4 ], + [ 'me.year' => 1990 ], + [ 'me.year' => 1979 ], + ]); +my @cds_80s = $cds_80s_rs->all; is(@cds_80s, 6, '6 80s cds found (1980 - 1985)'); map { ok($_->year < 1990 && $_->year > 1979) } @cds_80s; -my @cds_90s = $artist2->cds_90s; +my $cds_90s_rs = $artist2->cds_90s; +is_same_sql_bind($cds_90s_rs->as_query, + '(SELECT cds_90s.cdid, cds_90s.artist, cds_90s.title, cds_90s.year, cds_90s.genreid,'. + 'cds_90s.single_track FROM artist me JOIN cd cds_90s ON ( cds_90s.artist = me.artistid'. + ' AND ( cds_90s.year < ? AND cds_90s.year > ? ) ) WHERE ( artistid = ? ))', + [ + [ 'cds_90s.year' => 2000 ], + [ 'cds_90s.year' => 1989 ], + [ 'artistid' => 5 ], + ]); + +my @cds_90s = $cds_90s_rs->all; is(@cds_90s, 6, '6 90s cds found (1990 - 1995) even with non-optimized search'); map { ok($_->year < 2000 && $_->year > 1989) } @cds_90s; +my @cds_90s_95 = $artist2->cds_90s->search({ 'year' => 1995 }); +is(@cds_90s_95, 1, '1 90s (95) cds found even with non-optimized search'); +map { ok($_->year == 1995) } @cds_90s_95; + # search for all artists prefetching published cds in the 80s... ##### # the join must be a prefetch, but it can't work until the collapse rewrite is finished