1 BEGIN { do "./t/lib/ANFANG.pm" or die ( $@ || $! ) }
8 use DBICTest ':DiffSQL';
9 use DBIx::Class::SQLMaker::LimitDialects;
11 my $ROWS = DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype;
12 my $OFFSET = DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype;
14 my $schema = DBICTest->init_schema();
16 my $cd_rs = $schema->resultset('CD')->search (
17 { 'tracks.cd' => { '!=', undef } },
18 { prefetch => 'tracks' },
21 # Database sanity check
22 is($cd_rs->count, 5, 'CDs with tracks count');
24 is ($_->tracks->count, 3, '3 tracks for CD' . $_->id );
27 my @cdids = sort $cd_rs->get_column ('cdid')->all;
29 # Test a belongs_to prefetch of a has_many
31 my $track_rs = $schema->resultset ('Track')->search (
32 { 'me.cd' => { -in => \@cdids } },
36 { count => 'me.trackid' },
42 group_by => [qw/me.cd/],
47 # this used to fuck up ->all, do not remove!
48 ok ($track_rs->first, 'There is stuff in the rs');
50 is($track_rs->count, 5, 'Prefetched count with groupby');
51 is($track_rs->all, 5, 'Prefetched objects with groupby');
53 $schema->is_executed_querycount( sub {
54 while (my $collapsed_track = $track_rs->next) {
55 my $cdid = $collapsed_track->get_column('cd');
56 is($collapsed_track->get_column('track_count'), 3, "Correct count of tracks for CD $cdid" );
57 ok($collapsed_track->cd->title, "Prefetched title for CD $cdid" );
59 }, 1, 'Single query on prefetched titles');
61 # Test sql by hand, as the sqlite db will simply paper over
62 # improper group/select combinations
65 $track_rs->count_rs->as_query,
71 JOIN cd cd ON cd.cdid = me.cd
72 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
77 [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' }
79 'count() query generated expected SQL',
85 SELECT me.cd, me.track_count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
87 SELECT me.cd, COUNT (me.trackid) AS track_count
89 JOIN cd cd ON cd.cdid = me.cd
90 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
93 JOIN cd cd ON cd.cdid = me.cd
94 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
96 [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' }
97 => $_ ] } (@cdids) x 2 ],
98 'next() query generated expected SQL',
102 # add an extra track to one of the cds, and then make sure we can get it on top
103 # (check if limit works)
104 my $top_cd = $cd_rs->search({}, { order_by => 'cdid' })->slice (1,1)->next;
105 $top_cd->create_related ('tracks', {
106 title => 'over the top',
109 my $top_cd_collapsed_track = $track_rs->search ({}, {
112 { -desc => 'track_count' },
116 is ($top_cd_collapsed_track->count, 2);
120 $top_cd_collapsed_track->first->cd->title,
121 'Correct collapsed track with prefetched CD returned on top'
125 # test a has_many/might_have prefetch at the same level
126 # Note that one of the CDs now has 4 tracks instead of 3
128 my $most_tracks_rs = $schema->resultset ('CD')->search (
130 'me.cdid' => { '!=' => undef }, # duh - this is just to test WHERE
133 prefetch => [qw/tracks liner_notes/],
134 select => ['me.cdid', { count => 'tracks.trackid' }, { max => 'tracks.trackid', -as => 'maxtr'} ],
135 as => [qw/cdid track_count max_track_id/],
136 group_by => 'me.cdid',
137 order_by => [ { -desc => 'track_count' }, { -asc => 'maxtr' } ],
143 $most_tracks_rs->count_rs->as_query,
149 WHERE ( me.cdid IS NOT NULL )
155 'count() query generated expected SQL',
159 $most_tracks_rs->as_query,
161 SELECT me.cdid, me.track_count, me.maxtr,
162 tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at,
163 liner_notes.liner_id, liner_notes.notes
165 SELECT me.cdid, COUNT( tracks.trackid ) AS track_count, MAX( tracks.trackid ) AS maxtr
167 LEFT JOIN track tracks ON tracks.cd = me.cdid
168 WHERE ( me.cdid IS NOT NULL )
170 ORDER BY track_count DESC, maxtr ASC
173 LEFT JOIN track tracks ON tracks.cd = me.cdid
174 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
175 WHERE ( me.cdid IS NOT NULL )
176 ORDER BY track_count DESC, maxtr ASC
179 'next() query generated expected SQL',
182 is ($most_tracks_rs->count, 2, 'Limit works');
183 my ($top_cd) = $most_tracks_rs->all;
184 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier
186 $schema->is_executed_querycount( sub {
187 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
188 is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct');
189 is ($top_cd->tracks->all, 4, 'Number of prefetched track objects still correct');
191 $top_cd->liner_notes->notes,
193 'Correct liner pre-fetched with top cd',
195 }, 0, 'No queries executed during prefetched data access');
199 # test lifted from soulchild
201 my $most_tracks_rs = $schema->resultset ('CD')->search (
203 'me.cdid' => { '!=' => undef }, # this is just to test WHERE
204 'tracks.trackid' => { '!=' => undef },
208 prefetch => 'liner_notes',
209 select => ['me.cdid', 'liner_notes.notes', { count => 'tracks.trackid', -as => 'tr_count' }, { max => 'tracks.trackid', -as => 'tr_maxid'} ],
210 as => [qw/cdid notes track_count max_track_id/],
211 order_by => [ { -desc => 'tr_count' }, { -asc => 'tr_maxid' } ],
212 group_by => 'me.cdid',
218 $most_tracks_rs->as_query,
219 '(SELECT me.cdid, liner_notes.notes, me.tr_count, me.tr_maxid,
220 liner_notes.liner_id, liner_notes.notes
222 SELECT me.cdid, COUNT(tracks.trackid) AS tr_count, MAX(tracks.trackid) AS tr_maxid
224 LEFT JOIN track tracks
225 ON tracks.cd = me.cdid
226 WHERE me.cdid IS NOT NULL AND tracks.trackid IS NOT NULL
228 ORDER BY tr_count DESC, tr_maxid ASC
231 LEFT JOIN track tracks
232 ON tracks.cd = me.cdid
233 LEFT JOIN liner_notes liner_notes
234 ON liner_notes.liner_id = me.cdid
235 WHERE me.cdid IS NOT NULL AND tracks.trackid IS NOT NULL
236 ORDER BY tr_count DESC, tr_maxid ASC
239 'Oddball mysql-ish group_by usage yields valid SQL',
242 is ($most_tracks_rs->count, 2, 'Limit works');
243 my ($top_cd) = $most_tracks_rs->all;
244 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier
246 $schema->is_executed_querycount( sub {
247 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
249 $top_cd->liner_notes->notes,
251 'Correct liner pre-fetched with top cd',
253 }, 0, 'No queries executed during prefetched data access');
257 # make sure that distinct still works
259 my $rs = $schema->resultset("CD")->search({}, {
268 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
269 tags.tagid, tags.cd, tags.tag
271 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
273 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
275 LEFT JOIN tags tags ON tags.cd = me.cdid
279 'Prefetch + distinct resulted in correct group_by',
282 is ($rs->all, 5, 'Correct number of CD objects');
283 is ($rs->count, 5, 'Correct count of CDs');
286 # RT 47779, test group_by as a scalar ref
288 my $track_rs = $schema->resultset ('Track')->search (
289 { 'me.cd' => { -in => \@cdids } },
293 { count => 'me.trackid' },
299 group_by => \'SUBSTR(me.cd, 1, 1)',
305 $track_rs->count_rs->as_query,
309 SELECT SUBSTR(me.cd, 1, 1)
311 JOIN cd cd ON cd.cdid = me.cd
312 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
313 GROUP BY SUBSTR(me.cd, 1, 1)
317 [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' }
318 => $_ ] } (@cdids) ],
319 'count() query generated expected SQL',
324 my $cd_rs = $schema->resultset('CD')->search({}, {
326 join => [qw/ tracks /],
327 prefetch => [qw/ artist /],
329 is($cd_rs->count, 5, 'complex prefetch + non-prefetching has_many join count correct');
330 is($cd_rs->all, 5, 'complex prefetch + non-prefetching has_many join number of objects correct');
332 # make sure join tracks was thrown out
336 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
337 artist.artistid, artist.name, artist.rank, artist.charfield
339 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
341 JOIN artist artist ON artist.artistid = me.artist
342 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
344 JOIN artist artist ON artist.artistid = me.artist
351 # try the same as above, but add a condition so the tracks join can not be thrown away
352 my $cd_rs2 = $cd_rs->search ({ 'tracks.title' => { '!=' => 'ugabuganoexist' } });
353 is($cd_rs2->count, 5, 'complex prefetch + non-prefetching restricted has_many join count correct');
354 is($cd_rs2->all, 5, 'complex prefetch + non-prefetching restricted has_many join number of objects correct');
356 # the outer group_by seems like a necessary evil, if someone can figure out how to take it away
357 # without breaking compat - be my guest
361 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
362 artist.artistid, artist.name, artist.rank, artist.charfield
364 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
366 LEFT JOIN track tracks ON tracks.cd = me.cdid
367 JOIN artist artist ON artist.artistid = me.artist
368 WHERE ( tracks.title != ? )
369 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
371 LEFT JOIN track tracks ON tracks.cd = me.cdid
372 JOIN artist artist ON artist.artistid = me.artist
373 WHERE ( tracks.title != ? )
374 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
375 artist.artistid, artist.name, artist.rank, artist.charfield
377 [ map { [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'tracks.title' }
378 => 'ugabuganoexist' ] } (1,2)
383 # make sure distinct applies to the CD part only, not to the prefetched/collapsed order_by part
385 my $rs = $schema->resultset('CD')->search({}, {
386 columns => [qw( cdid title )],
387 '+select' => [{ count => 'tags.tag' }],
388 '+as' => ['test_count'],
389 prefetch => ['tags'],
391 order_by => {'-desc' => 'tags.tag'},
396 is_same_sql_bind($rs->as_query,
398 SELECT me.cdid, me.title, me.test_count,
399 tags.tagid, tags.cd, tags.tag
401 SELECT me.cdid, me.title,
402 COUNT( tags.tag ) AS test_count
406 GROUP BY me.cdid, me.title
407 ORDER BY MAX( tags.tag ) DESC
413 ORDER BY tags.tag DESC
415 [ [$ROWS => 3], [$OFFSET => 1] ],
416 'Expected limited prefetch with distinct SQL',
420 { cdid => 4, test_count => 2, title => "Generic Manufactured Singles", tags => [
421 { cd => 4, tag => "Shiny", tagid => 9 },
422 { cd => 4, tag => "Cheesy", tagid => 6 },
425 cdid => 5, test_count => 2, title => "Come Be Depressed With Us", tags => [
426 { cd => 5, tag => "Cheesy", tagid => 7 },
427 { cd => 5, tag => "Blue", tagid => 4 },
430 cdid => 1, test_count => 1, title => "Spoonful of bees", tags => [
431 { cd => 1, tag => "Blue", tagid => 1 },
438 'HRI dump of limited prefetch with distinct as expected'
441 # pre-multiplied main source also should work
442 $rs = $schema->resultset('CD')->search_related('artist')->search_related('cds', {}, {
443 columns => [qw( cdid title )],
444 '+select' => [{ count => 'tags.tag' }],
445 '+as' => ['test_count'],
446 prefetch => ['tags'],
448 order_by => {'-desc' => 'tags.tag'},
453 is_same_sql_bind($rs->as_query,
455 SELECT cds.cdid, cds.title, cds.test_count,
456 tags.tagid, tags.cd, tags.tag
459 ON artist.artistid = me.artist
461 SELECT cds.cdid, cds.title,
462 COUNT( tags.tag ) AS test_count,
466 ON artist.artistid = me.artist
468 ON cds.artist = artist.artistid
470 ON tags.cd = cds.cdid
471 GROUP BY cds.cdid, cds.title, cds.artist
472 ORDER BY MAX( tags.tag ) DESC
476 ON cds.artist = artist.artistid
478 ON tags.cd = cds.cdid
479 ORDER BY tags.tag DESC
481 [ [$ROWS => 3], [$OFFSET => 1] ],
482 'Expected limited prefetch with distinct SQL on premultiplied head',
485 # Tag counts are multiplied by the cd->artist->cds multiplication
486 # I would *almost* call this "expected" without wraping an as_subselect_rs
488 local $TODO = 'Not sure if we can stop the count/group of premultiplication abstraction leak';
492 'HRI dump of limited prefetch with distinct as expected on premultiplid head'