From: Peter Rabbitson Date: Tue, 26 Mar 2013 07:56:40 +0000 (+0100) Subject: Fix "current rs is always first in from" brainfart introduced in 893403c81 X-Git-Tag: v0.08250~31^2~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=90f10b5a4f971772f25d6304430124abfa4a890c;p=dbsrgits%2FDBIx-Class.git Fix "current rs is always first in from" brainfart introduced in 893403c81 Dear self from 4 years ago - what were you thinking? In any case I can't recognize why this is needed so cleansing it with fire --- diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index d207767..59f4441 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -2296,15 +2296,18 @@ sub _select_args { my ($self, $ident, $select, $where, $attrs) = @_; my $sql_maker = $self->sql_maker; - my ($alias2source, $rs_alias) = $self->_resolve_ident_sources ($ident); + my $alias2source = $self->_resolve_ident_sources ($ident); $attrs = { %$attrs, select => $select, from => $ident, where => $where, - $rs_alias && $alias2source->{$rs_alias} - ? ( _rsroot_rsrc => $alias2source->{$rs_alias} ) + + # limit dialects use this stuff + # yes, some CDBICompat crap does not supply an {alias} >.< + ( $attrs->{alias} and $alias2source->{$attrs->{alias}} ) + ? ( _rsroot_rsrc => $alias2source->{$attrs->{alias}} ) : () , }; @@ -2344,7 +2347,7 @@ sub _select_args { $self->_resolve_aliastypes_from_select_args( $attrs->{from}, undef, undef, { group_by => $attrs->{group_by} } ) } ) { - $complex_prefetch = ! defined first { $_ ne $rs_alias } keys %{ $grp_aliases->{grouping} || {} }; + $complex_prefetch = ! defined first { $_ ne $attrs->{alias} } keys %{ $grp_aliases->{grouping} || {} }; } $complex_prefetch ||= ( $attrs->{rows} && $attrs->{collapse} ); diff --git a/lib/DBIx/Class/Storage/DBIHacks.pm b/lib/DBIx/Class/Storage/DBIHacks.pm index fd290f0..b3b0177 100644 --- a/lib/DBIx/Class/Storage/DBIHacks.pm +++ b/lib/DBIx/Class/Storage/DBIHacks.pm @@ -621,14 +621,12 @@ sub _resolve_ident_sources { my ($self, $ident) = @_; my $alias2source = {}; - my $rs_alias; # the reason this is so contrived is that $ident may be a {from} # structure, specifying multiple tables to join if ( blessed $ident && $ident->isa("DBIx::Class::ResultSource") ) { # this is compat mode for insert/update/delete which do not deal with aliases $alias2source->{me} = $ident; - $rs_alias = 'me'; } elsif (ref $ident eq 'ARRAY') { @@ -636,7 +634,6 @@ sub _resolve_ident_sources { my $tabinfo; if (ref $_ eq 'HASH') { $tabinfo = $_; - $rs_alias = $tabinfo->{-alias}; } if (ref $_ eq 'ARRAY' and ref $_->[0] eq 'HASH') { $tabinfo = $_->[0]; @@ -647,7 +644,7 @@ sub _resolve_ident_sources { } } - return ($alias2source, $rs_alias); + return $alias2source; } # Takes $ident, \@column_names @@ -659,7 +656,7 @@ sub _resolve_ident_sources { # for all sources sub _resolve_column_info { my ($self, $ident, $colnames) = @_; - my ($alias2src, $root_alias) = $self->_resolve_ident_sources($ident); + my $alias2src = $self->_resolve_ident_sources($ident); my (%seen_cols, @auto_colnames); diff --git a/t/prefetch/via_search_related.t b/t/prefetch/via_search_related.t index 79826ba..e95d960 100644 --- a/t/prefetch/via_search_related.t +++ b/t/prefetch/via_search_related.t @@ -145,4 +145,30 @@ lives_ok (sub { $schema->storage->debug ($orig_debug); }, 'distinct generally works with prefetch on deep search_related chains'); +# pathological "user knows what they're doing" case +# lifted from production somewhere +{ + $schema->resultset('CD') + ->search({ cdid => [1,2] }) + ->search_related('tracks', { position => [3,1] }) + ->delete_all; + + my $rs = $schema->resultset('CD')->search_related('tracks', {}, { + group_by => 'me.title', + columns => { title => 'me.title', max_trk => \ 'MAX(tracks.position)' }, + }); + + is_deeply( + $rs->all_hri, + [ + { title => "Caterwaulin' Blues", max_trk => 3 }, + { title => "Come Be Depressed With Us", max_trk => 3 }, + { title => "Forkful of bees", max_trk => 1 }, + { title => "Generic Manufactured Singles", max_trk => 3 }, + { title => "Spoonful of bees", max_trk => 1 }, + ], + 'Expected nonsense', + ); +} + done_testing;