From: Moritz Onken Date: Tue, 25 Jan 2011 16:08:16 +0000 (+0100) Subject: Test for an insane yet valid rel condition with subquery X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fe4118b138326291850bbe2438f61cd0e296034b;p=dbsrgits%2FDBIx-Class-Historic.git Test for an insane yet valid rel condition with subquery --- diff --git a/t/lib/DBICTest/Schema/CD.pm b/t/lib/DBICTest/Schema/CD.pm index 552f16e..9a147d8 100644 --- a/t/lib/DBICTest/Schema/CD.pm +++ b/t/lib/DBICTest/Schema/CD.pm @@ -93,4 +93,37 @@ __PACKAGE__->belongs_to('genre_inefficient', 'DBICTest::Schema::Genre', }, ); + +# This is insane. Don't ever do anything like that +# This is for testing purposes only! + +# mst: mo: DBIC is an "object relational mapper" +# mst: mo: not an "object relational hider-because-mo-doesn't-understand-databases +# ribasushi: mo: try it with a subselect nevertheless, I'd love to be proven wrong +# ribasushi: mo: does sqlite actually take this? +# ribasushi: an order in a correlated subquery is insane - how long does it take you on real data? + +__PACKAGE__->might_have( + 'last_track', + 'DBICTest::Schema::Track', + sub { + my $args = shift; + return ( + { + "$args->{foreign_alias}.trackid" => { '=' => + $args->{self_resultsource}->schema->resultset('Track')->search( + { 'correlated_tracks.cd' => { -ident => "$args->{self_alias}.cdid" } }, + { + order_by => { -desc => 'position' }, + rows => 1, + alias => 'correlated_tracks', + columns => ['trackid'] + }, + )->as_query + } + } + ); + }, +); + 1; diff --git a/t/relationship/custom.t b/t/relationship/custom.t index 99a0786..c136224 100644 --- a/t/relationship/custom.t +++ b/t/relationship/custom.t @@ -171,6 +171,18 @@ is_deeply ( 'last group-entry via self-join works', ); +is_deeply ( + [map { $_->last_track->id } grep { $_->last_track } $schema->resultset('CD')->search ({}, { order_by => 'cdid', prefetch => 'last_track'})->all], + [ map { $_->trackid } @last_tracks ], + 'last_track via insane subquery condition works', +); + +is_deeply ( + [map { $_->last_track->id } grep { $_->last_track } $schema->resultset('CD')->search ({}, { order_by => 'cdid'})->all], + [ map { $_->trackid } @last_tracks ], + 'last_track via insane subquery condition works, even without prefetch', +); + my $artwork = $schema->resultset('Artwork')->search({},{ order_by => 'cd_id' })->first; my @artists = $artwork->artists->all; is(scalar @artists, 2, 'the two artists are associated');