X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fcount%2Fprefetch.t;h=f3818c14ee7876d5f8a77d87c851ca683ca0a8fb;hb=336feb8ef85d16d22ae0b131b0c7a85abc9e9435;hp=bd0426ba6a38f89b3e254987f9b532e09d679900;hpb=639cf8f9d212c3e950ae5bae680133762c1df0aa;p=dbsrgits%2FDBIx-Class.git diff --git a/t/count/prefetch.t b/t/count/prefetch.t index bd0426b..f3818c1 100644 --- a/t/count/prefetch.t +++ b/t/count/prefetch.t @@ -6,9 +6,6 @@ use lib qw(t/lib); use Test::More; use DBICTest; use DBIC::SqlMakerTest; -use DBIC::DebugObj; - -plan tests => 6; my $schema = DBICTest->init_schema(); @@ -20,20 +17,55 @@ my $schema = DBICTest->init_schema(); { prefetch => [qw/tracks artist/] }, ); is ($rs->all, 5, 'Correct number of objects'); + is ($rs->count, 5, 'Correct count'); + is_same_sql_bind ( + $rs->count_rs->as_query, + '( + SELECT COUNT( * ) + FROM ( + SELECT cds.cdid + FROM artist me + JOIN cd cds ON cds.artist = me.artistid + LEFT JOIN track tracks ON tracks.cd = cds.cdid + JOIN artist artist ON artist.artistid = cds.artist + WHERE tracks.position = ? OR tracks.position = ? + GROUP BY cds.cdid + ) cds + )', + [ map { [ 'tracks.position' => $_ ] } (1, 2) ], + ); +} - my ($sql, @bind); - $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)); - $schema->storage->debug(1); - - - is ($rs->count, 5, 'Correct count'); +# collapsing prefetch with distinct +{ + my $rs = $schema->resultset("Artist")->search(undef, {distinct => 1}) + ->search_related('cds')->search_related('genre', + { 'genre.name' => 'emo' }, + { prefetch => q(cds) }, + ); + is ($rs->all, 1, 'Correct number of objects'); + is ($rs->count, 1, 'Correct count'); is_same_sql_bind ( - $sql, - \@bind, - 'SELECT COUNT( * ) FROM (SELECT cds.cdid FROM artist me LEFT JOIN cd cds ON cds.artist = me.artistid LEFT JOIN track tracks ON tracks.cd = cds.cdid JOIN artist artist ON artist.artistid = cds.artist WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid ORDER BY tracks.cd) count_subq', - [ qw/'1' '2'/ ], # wtf? we quote bind vals? + $rs->count_rs->as_query, + '( + SELECT COUNT( * ) + FROM ( + SELECT genre.genreid + FROM ( + SELECT me.artistid, me.name, me.rank, me.charfield + FROM artist me + GROUP BY me.artistid, me.name, me.rank, me.charfield + ) me + JOIN cd cds ON cds.artist = me.artistid + JOIN genre genre ON genre.genreid = cds.genreid + WHERE ( genre.name = ? ) + GROUP BY genre.genreid + ) + genre + )', + [ [ 'genre.name' => 'emo' ] ], ); } @@ -41,23 +73,26 @@ my $schema = DBICTest->init_schema(); { my $rs = $schema->resultset("CD") ->search_related('tracks', - { position => [1,2] }, + { position => [1,2], 'lyrics.lyric_id' => undef }, { prefetch => [qw/disc lyrics/] }, ); is ($rs->all, 10, 'Correct number of objects'); - my ($sql, @bind); - $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind)); - $schema->storage->debug(1); - - is ($rs->count, 10, 'Correct count'); is_same_sql_bind ( - $sql, - \@bind, - 'SELECT COUNT( * ) FROM cd me LEFT JOIN track tracks ON tracks.cd = me.cdid JOIN cd disc ON disc.cdid = tracks.cd LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid WHERE ( ( position = ? OR position = ? ) )', - [ qw/'1' '2'/ ], # wtf? we quote bind vals? + $rs->count_rs->as_query, + '( + SELECT COUNT( * ) + FROM cd me + JOIN track tracks ON tracks.cd = me.cdid + JOIN cd disc ON disc.cdid = tracks.cd + LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid + WHERE lyrics.lyric_id IS NULL AND (position = ? OR position = ?) + )', + [ map { [ position => $_ ] } (1, 2) ], ); } + +done_testing;