X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fprefetch%2Fgrouped.t;h=b7ec8eb471a53294ba318118db0703a10334eb5c;hb=1e4f9fb3b8bd1f54518bc2942554099356fa6524;hp=edb69b6790ec9c4966aaadf02d3c6ace5ff1fe36;hpb=0491b597f9bf7f3731f5a2799041f2596b0f46be;p=dbsrgits%2FDBIx-Class-Historic.git diff --git a/t/prefetch/grouped.t b/t/prefetch/grouped.t index edb69b6..b7ec8eb 100644 --- a/t/prefetch/grouped.t +++ b/t/prefetch/grouped.t @@ -2,11 +2,13 @@ use strict; use warnings; use Test::More; -use Test::Exception; use lib qw(t/lib); use DBICTest; use DBIC::SqlMakerTest; +use DBIx::Class::SQLMaker::LimitDialects; + +my $ROWS = DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype; my $schema = DBICTest->init_schema(); my $sdebug = $schema->storage->debug; @@ -76,9 +78,10 @@ for ($cd_rs->all) { WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) ) GROUP BY me.cd ) - count_subq + me )', - [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ], + [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' } + => $_ ] } ($cd_rs->get_column ('cdid')->all) ], 'count() query generated expected SQL', ); @@ -96,7 +99,8 @@ for ($cd_rs->all) { JOIN cd cd ON cd.cdid = me.cd WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) ) )', - [ map { [ 'me.cd' => $_] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ], + [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' } + => $_ ] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ], 'next() query generated expected SQL', ); @@ -150,10 +154,10 @@ for ($cd_rs->all) { FROM cd me WHERE ( me.cdid IS NOT NULL ) GROUP BY me.cdid - LIMIT 2 - ) count_subq + LIMIT ? + ) me )', - [], + [[$ROWS => 2]], 'count() query generated expected SQL', ); @@ -161,7 +165,7 @@ for ($cd_rs->all) { $most_tracks_rs->as_query, '( SELECT me.cdid, me.track_count, me.maxtr, - tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, tracks.small_dt, + tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, liner_notes.liner_id, liner_notes.notes FROM ( SELECT me.cdid, COUNT( tracks.trackid ) AS track_count, MAX( tracks.trackid ) AS maxtr @@ -170,19 +174,19 @@ for ($cd_rs->all) { WHERE ( me.cdid IS NOT NULL ) GROUP BY me.cdid ORDER BY track_count DESC, maxtr ASC - LIMIT 2 + LIMIT ? ) me LEFT JOIN track tracks ON tracks.cd = me.cdid LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid WHERE ( me.cdid IS NOT NULL ) - ORDER BY track_count DESC, maxtr ASC, tracks.cd + ORDER BY track_count DESC, maxtr ASC )', - [], + [[$ROWS => 2]], 'next() query generated expected SQL', ); is ($most_tracks_rs->count, 2, 'Limit works'); - my $top_cd = $most_tracks_rs->first; + my ($top_cd) = $most_tracks_rs->all; is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier my $query_cnt = 0; @@ -203,6 +207,71 @@ for ($cd_rs->all) { $schema->storage->debug ($sdebug); } +{ + # test lifted from soulchild + + my $most_tracks_rs = $schema->resultset ('CD')->search ( + { + 'me.cdid' => { '!=' => undef }, # this is just to test WHERE + 'tracks.trackid' => { '!=' => undef }, + }, + { + join => 'tracks', + prefetch => 'liner_notes', + select => ['me.cdid', 'liner_notes.notes', { count => 'tracks.trackid', -as => 'tr_count' }, { max => 'tracks.trackid', -as => 'tr_maxid'} ], + as => [qw/cdid notes track_count max_track_id/], + order_by => [ { -desc => 'tr_count' }, { -asc => 'tr_maxid' } ], + group_by => 'me.cdid', + rows => 2, + } + ); + + is_same_sql_bind( + $most_tracks_rs->as_query, + '(SELECT me.cdid, liner_notes.notes, me.tr_count, me.tr_maxid, + liner_notes.liner_id, liner_notes.notes + FROM ( + SELECT me.cdid, COUNT(tracks.trackid) AS tr_count, MAX(tracks.trackid) AS tr_maxid + FROM cd me + LEFT JOIN track tracks + ON tracks.cd = me.cdid + WHERE me.cdid IS NOT NULL AND tracks.trackid IS NOT NULL + GROUP BY me.cdid + ORDER BY tr_count DESC, tr_maxid ASC + LIMIT ? + ) me + LEFT JOIN track tracks + ON tracks.cd = me.cdid + LEFT JOIN liner_notes liner_notes + ON liner_notes.liner_id = me.cdid + WHERE me.cdid IS NOT NULL AND tracks.trackid IS NOT NULL + ORDER BY tr_count DESC, tr_maxid ASC + )', + [[$ROWS => 2]], + 'Oddball mysql-ish group_by usage yields valid SQL', + ); + + is ($most_tracks_rs->count, 2, 'Limit works'); + my ($top_cd) = $most_tracks_rs->all; + is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier + + my $query_cnt = 0; + $schema->storage->debugcb ( sub { $query_cnt++ } ); + $schema->storage->debug (1); + + is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly'); + is ( + $top_cd->liner_notes->notes, + 'Buy Whiskey!', + 'Correct liner pre-fetched with top cd', + ); + + is ($query_cnt, 0, 'No queries executed during prefetched data access'); + $schema->storage->debugcb (undef); + $schema->storage->debug ($sdebug); +} + + # make sure that distinct still works { my $rs = $schema->resultset("CD")->search({}, { @@ -219,11 +288,10 @@ for ($cd_rs->all) { FROM ( SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track FROM cd me - GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, cdid - ORDER BY cdid + GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track ) me LEFT JOIN tags tags ON tags.cd = me.cdid - ORDER BY cdid, tags.cd, tags.tag + ORDER BY cdid )', [], 'Prefetch + distinct resulted in correct group_by', @@ -262,9 +330,10 @@ for ($cd_rs->all) { WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) ) GROUP BY SUBSTR(me.cd, 1, 1) ) - count_subq + me )', - [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ], + [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' } + => $_ ] } ($cd_rs->get_column ('cdid')->all) ], 'count() query generated expected SQL', ); } @@ -323,7 +392,9 @@ for ($cd_rs->all) { GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, artist.artistid, artist.name, artist.rank, artist.charfield )', - [ map { [ 'tracks.title' => 'ugabuganoexist' ] } (1 .. 2) ], + [ map { [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'tracks.title' } + => 'ugabuganoexist' ] } (1,2) + ], ); } @@ -343,12 +414,12 @@ for ($cd_rs->all) { FROM (SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, COUNT( tags.tag ) AS test_count FROM cd me LEFT JOIN tags tags ON tags.cd = me.cdid GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, tags.tag - ORDER BY tags.tag ASC LIMIT 1) + ORDER BY tags.tag ASC LIMIT ?) me LEFT JOIN tags tags ON tags.cd = me.cdid - ORDER BY tags.tag ASC, tags.cd, tags.tag + ORDER BY tags.tag ASC ) - }, []); + }, [[$ROWS => 1]]); } done_testing;