From: Peter Rabbitson Date: Tue, 16 Jun 2009 15:22:59 +0000 (+0000) Subject: Fix possible regression with prefetch select resolution X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f1952f5c69e092d9ce416586f29942f8c2f66bce;p=dbsrgits%2FDBIx-Class-Historic.git Fix possible regression with prefetch select resolution --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index fe622b9..ed2d144 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -2599,19 +2599,21 @@ sub _resolved_attrs { my $collapse = $attrs->{collapse} || {}; + if ( my $prefetch = delete $attrs->{prefetch} ) { $prefetch = $self->_merge_attr( {}, $prefetch ); - my @pre_order; - foreach my $p ( ref $prefetch eq 'ARRAY' ? @$prefetch : ($prefetch) ) { - - # bring joins back to level of current class - my $join_map = $self->_joinpath_aliases ($attrs->{from}, $attrs->{seen_join}); - my @prefetch = - $source->_resolve_prefetch( $p, $alias, $join_map, \@pre_order, $collapse ); - push( @{ $attrs->{select} }, map { $_->[0] } @prefetch ); - push( @{ $attrs->{as} }, map { $_->[1] } @prefetch ); - } - push( @{ $attrs->{order_by} }, @pre_order ); + + my $prefetch_ordering = []; + + my $join_map = $self->_joinpath_aliases ($attrs->{from}, $attrs->{seen_join}); + + my @prefetch = + $source->_resolve_prefetch( $prefetch, $alias, $join_map, $prefetch_ordering, $collapse ); + + push( @{ $attrs->{select} }, map { $_->[0] } @prefetch ); + push( @{ $attrs->{as} }, map { $_->[1] } @prefetch ); + + push( @{ $attrs->{order_by} }, @$prefetch_ordering ); } if (delete $attrs->{distinct}) { diff --git a/t/prefetch/attrs_untouched.t b/t/prefetch/attrs_untouched.t index 53894b8..41ad883 100644 --- a/t/prefetch/attrs_untouched.t +++ b/t/prefetch/attrs_untouched.t @@ -8,16 +8,7 @@ use Data::Dumper; my $schema = DBICTest->init_schema(); -my $orig_debug = $schema->storage->debug; - -use IO::File; - -BEGIN { - eval "use DBD::SQLite"; - plan $@ - ? ( skip_all => 'needs DBD::SQLite for testing' ) - : ( tests => 3 ); -} +plan tests => 3; # bug in 0.07000 caused attr (join/prefetch) to be modifed by search # so we check the search & attr arrays are not modified diff --git a/t/prefetch/double_prefetch.t b/t/prefetch/double_prefetch.t new file mode 100644 index 0000000..b666220 --- /dev/null +++ b/t/prefetch/double_prefetch.t @@ -0,0 +1,35 @@ +use warnings; + +use Test::More; +use Test::Exception; +use lib qw(t/lib); +use DBIC::SqlMakerTest; +use DBICTest; + +my $schema = DBICTest->init_schema(); + +plan tests => 1; + +# While this is a rather GIGO case, make sure it behaves as pre-103, +# as it may result in hard-to-track bugs +my $cds = $schema->resultset('Artist') + ->search_related ('cds') + ->search ({}, { + prefetch => [ 'single_track', { single_track => 'cd' } ], + }); + +is_same_sql( + ${$cds->as_query}->[0], + '( + SELECT + cds.cdid, cds.artist, cds.title, cds.year, cds.genreid, cds.single_track, + single_track.trackid, single_track.cd, single_track.position, single_track.title, single_track.last_updated_on, single_track.last_updated_at, + single_track_2.trackid, single_track_2.cd, single_track_2.position, single_track_2.title, single_track_2.last_updated_on, single_track_2.last_updated_at, + cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track + FROM artist me + LEFT JOIN cd cds ON cds.artist = me.artistid + LEFT JOIN track single_track ON single_track.trackid = cds.single_track + LEFT JOIN track single_track_2 ON single_track_2.trackid = cds.single_track + LEFT JOIN cd cd ON cd.cdid = single_track_2.cd + )', +);