8 use DBIC::SqlMakerTest;
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 # Test a belongs_to prefetch of a has_many
29 my $track_rs = $schema->resultset ('Track')->search (
30 { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } },
34 { count => 'me.trackid' },
40 group_by => [qw/me.cd/],
45 # this used to fuck up ->all, do not remove!
46 ok ($track_rs->first, 'There is stuff in the rs');
48 is($track_rs->count, 5, 'Prefetched count with groupby');
49 is($track_rs->all, 5, 'Prefetched objects with groupby');
51 $schema->is_executed_querycount( sub {
52 while (my $collapsed_track = $track_rs->next) {
53 my $cdid = $collapsed_track->get_column('cd');
54 is($collapsed_track->get_column('track_count'), 3, "Correct count of tracks for CD $cdid" );
55 ok($collapsed_track->cd->title, "Prefetched title for CD $cdid" );
57 }, 1, 'Single query on prefetched titles');
59 # Test sql by hand, as the sqlite db will simply paper over
60 # improper group/select combinations
63 $track_rs->count_rs->as_query,
69 JOIN cd cd ON cd.cdid = me.cd
70 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
75 [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' }
76 => $_ ] } ($cd_rs->get_column ('cdid')->all) ],
77 'count() query generated expected SQL',
83 SELECT me.cd, me.track_count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
85 SELECT me.cd, COUNT (me.trackid) AS track_count
87 JOIN cd cd ON cd.cdid = me.cd
88 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
91 JOIN cd cd ON cd.cdid = me.cd
92 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
94 [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' }
95 => $_ ] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ],
96 'next() query generated expected SQL',
100 # add an extra track to one of the cds, and then make sure we can get it on top
101 # (check if limit works)
102 my $top_cd = $cd_rs->slice (1,1)->next;
103 $top_cd->create_related ('tracks', {
104 title => 'over the top',
107 my $top_cd_collapsed_track = $track_rs->search ({}, {
110 { -desc => 'track_count' },
114 is ($top_cd_collapsed_track->count, 2);
118 $top_cd_collapsed_track->first->cd->title,
119 'Correct collapsed track with prefetched CD returned on top'
123 # test a has_many/might_have prefetch at the same level
124 # Note that one of the CDs now has 4 tracks instead of 3
126 my $most_tracks_rs = $schema->resultset ('CD')->search (
128 'me.cdid' => { '!=' => undef }, # duh - this is just to test WHERE
131 prefetch => [qw/tracks liner_notes/],
132 select => ['me.cdid', { count => 'tracks.trackid' }, { max => 'tracks.trackid', -as => 'maxtr'} ],
133 as => [qw/cdid track_count max_track_id/],
134 group_by => 'me.cdid',
135 order_by => [ { -desc => 'track_count' }, { -asc => 'maxtr' } ],
141 $most_tracks_rs->count_rs->as_query,
147 WHERE ( me.cdid IS NOT NULL )
153 'count() query generated expected SQL',
157 $most_tracks_rs->as_query,
159 SELECT me.cdid, me.track_count, me.maxtr,
160 tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at,
161 liner_notes.liner_id, liner_notes.notes
163 SELECT me.cdid, COUNT( tracks.trackid ) AS track_count, MAX( tracks.trackid ) AS maxtr
165 LEFT JOIN track tracks ON tracks.cd = me.cdid
166 WHERE ( me.cdid IS NOT NULL )
168 ORDER BY track_count DESC, maxtr ASC
171 LEFT JOIN track tracks ON tracks.cd = me.cdid
172 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
173 WHERE ( me.cdid IS NOT NULL )
174 ORDER BY track_count DESC, maxtr ASC
177 'next() query generated expected SQL',
180 is ($most_tracks_rs->count, 2, 'Limit works');
181 my ($top_cd) = $most_tracks_rs->all;
182 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier
184 $schema->is_executed_querycount( sub {
185 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
186 is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct');
187 is ($top_cd->tracks->all, 4, 'Number of prefetched track objects still correct');
189 $top_cd->liner_notes->notes,
191 'Correct liner pre-fetched with top cd',
193 }, 0, 'No queries executed during prefetched data access');
197 # test lifted from soulchild
199 my $most_tracks_rs = $schema->resultset ('CD')->search (
201 'me.cdid' => { '!=' => undef }, # this is just to test WHERE
202 'tracks.trackid' => { '!=' => undef },
206 prefetch => 'liner_notes',
207 select => ['me.cdid', 'liner_notes.notes', { count => 'tracks.trackid', -as => 'tr_count' }, { max => 'tracks.trackid', -as => 'tr_maxid'} ],
208 as => [qw/cdid notes track_count max_track_id/],
209 order_by => [ { -desc => 'tr_count' }, { -asc => 'tr_maxid' } ],
210 group_by => 'me.cdid',
216 $most_tracks_rs->as_query,
217 '(SELECT me.cdid, liner_notes.notes, me.tr_count, me.tr_maxid,
218 liner_notes.liner_id, liner_notes.notes
220 SELECT me.cdid, COUNT(tracks.trackid) AS tr_count, MAX(tracks.trackid) AS tr_maxid
222 LEFT JOIN track tracks
223 ON tracks.cd = me.cdid
224 WHERE me.cdid IS NOT NULL AND tracks.trackid IS NOT NULL
226 ORDER BY tr_count DESC, tr_maxid ASC
229 LEFT JOIN track tracks
230 ON tracks.cd = me.cdid
231 LEFT JOIN liner_notes liner_notes
232 ON liner_notes.liner_id = me.cdid
233 WHERE me.cdid IS NOT NULL AND tracks.trackid IS NOT NULL
234 ORDER BY tr_count DESC, tr_maxid ASC
237 'Oddball mysql-ish group_by usage yields valid SQL',
240 is ($most_tracks_rs->count, 2, 'Limit works');
241 my ($top_cd) = $most_tracks_rs->all;
242 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier
244 $schema->is_executed_querycount( sub {
245 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
247 $top_cd->liner_notes->notes,
249 'Correct liner pre-fetched with top cd',
251 }, 0, 'No queries executed during prefetched data access');
255 # make sure that distinct still works
257 my $rs = $schema->resultset("CD")->search({}, {
266 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
267 tags.tagid, tags.cd, tags.tag
269 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
271 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
273 LEFT JOIN tags tags ON tags.cd = me.cdid
277 'Prefetch + distinct resulted in correct group_by',
280 is ($rs->all, 5, 'Correct number of CD objects');
281 is ($rs->count, 5, 'Correct count of CDs');
284 # RT 47779, test group_by as a scalar ref
286 my $track_rs = $schema->resultset ('Track')->search (
287 { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } },
291 { count => 'me.trackid' },
297 group_by => \'SUBSTR(me.cd, 1, 1)',
303 $track_rs->count_rs->as_query,
307 SELECT SUBSTR(me.cd, 1, 1)
309 JOIN cd cd ON cd.cdid = me.cd
310 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
311 GROUP BY SUBSTR(me.cd, 1, 1)
315 [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' }
316 => $_ ] } ($cd_rs->get_column ('cdid')->all) ],
317 'count() query generated expected SQL',
322 my $cd_rs = $schema->resultset('CD')->search({}, {
324 join => [qw/ tracks /],
325 prefetch => [qw/ artist /],
327 is($cd_rs->count, 5, 'complex prefetch + non-prefetching has_many join count correct');
328 is($cd_rs->all, 5, 'complex prefetch + non-prefetching has_many join number of objects correct');
330 # make sure join tracks was thrown out
334 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
335 artist.artistid, artist.name, artist.rank, artist.charfield
337 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
339 JOIN artist artist ON artist.artistid = me.artist
340 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
342 JOIN artist artist ON artist.artistid = me.artist
349 # try the same as above, but add a condition so the tracks join can not be thrown away
350 my $cd_rs2 = $cd_rs->search ({ 'tracks.title' => { '!=' => 'ugabuganoexist' } });
351 is($cd_rs2->count, 5, 'complex prefetch + non-prefetching restricted has_many join count correct');
352 is($cd_rs2->all, 5, 'complex prefetch + non-prefetching restricted has_many join number of objects correct');
354 # the outer group_by seems like a necessary evil, if someone can figure out how to take it away
355 # without breaking compat - be my guest
359 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
360 artist.artistid, artist.name, artist.rank, artist.charfield
362 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
364 LEFT JOIN track tracks ON tracks.cd = me.cdid
365 JOIN artist artist ON artist.artistid = me.artist
366 WHERE ( tracks.title != ? )
367 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
369 LEFT JOIN track tracks ON tracks.cd = me.cdid
370 JOIN artist artist ON artist.artistid = me.artist
371 WHERE ( tracks.title != ? )
372 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
373 artist.artistid, artist.name, artist.rank, artist.charfield
375 [ map { [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'tracks.title' }
376 => 'ugabuganoexist' ] } (1,2)
381 # make sure distinct applies to the CD part only, not to the prefetched/collapsed order_by part
383 my $rs = $schema->resultset('CD')->search({}, {
384 columns => [qw( cdid title )],
385 '+select' => [{ count => 'tags.tag' }],
386 '+as' => ['test_count'],
387 prefetch => ['tags'],
389 order_by => {'-desc' => 'tags.tag'},
394 is_same_sql_bind($rs->as_query,
396 SELECT me.cdid, me.title, me.test_count,
397 tags.tagid, tags.cd, tags.tag
399 SELECT me.cdid, me.title,
400 COUNT( tags.tag ) AS test_count
404 GROUP BY me.cdid, me.title
405 ORDER BY MAX( tags.tag ) DESC
411 ORDER BY tags.tag DESC
413 [ [$ROWS => 3], [$OFFSET => 1] ],
414 'Expected limited prefetch with distinct SQL',
418 { cdid => 4, test_count => 2, title => "Generic Manufactured Singles", tags => [
419 { cd => 4, tag => "Shiny", tagid => 9 },
420 { cd => 4, tag => "Cheesy", tagid => 6 },
423 cdid => 5, test_count => 2, title => "Come Be Depressed With Us", tags => [
424 { cd => 5, tag => "Cheesy", tagid => 7 },
425 { cd => 5, tag => "Blue", tagid => 4 },
428 cdid => 1, test_count => 1, title => "Spoonful of bees", tags => [
429 { cd => 1, tag => "Blue", tagid => 1 },
436 'HRI dump of limited prefetch with distinct as expected'
439 # pre-multiplied main source also should work
440 $rs = $schema->resultset('CD')->search_related('artist')->search_related('cds', {}, {
441 columns => [qw( cdid title )],
442 '+select' => [{ count => 'tags.tag' }],
443 '+as' => ['test_count'],
444 prefetch => ['tags'],
446 order_by => {'-desc' => 'tags.tag'},
451 is_same_sql_bind($rs->as_query,
453 SELECT cds.cdid, cds.title, cds.test_count,
454 tags.tagid, tags.cd, tags.tag
457 ON artist.artistid = me.artist
459 SELECT cds.cdid, cds.title,
460 COUNT( tags.tag ) AS test_count,
464 ON artist.artistid = me.artist
466 ON cds.artist = artist.artistid
468 ON tags.cd = cds.cdid
469 GROUP BY cds.cdid, cds.title, cds.artist
470 ORDER BY MAX( tags.tag ) DESC
474 ON cds.artist = artist.artistid
476 ON tags.cd = cds.cdid
477 ORDER BY tags.tag DESC
479 [ [$ROWS => 3], [$OFFSET => 1] ],
480 'Expected limited prefetch with distinct SQL on premultiplied head',
483 # Tag counts are multiplied by the cd->artist->cds multiplication
484 # I would *almost* call this "expected" without wraping an as_subselect_rs
486 local $TODO = 'Not sure if we can stop the count/group of premultiplication abstraction leak';
490 'HRI dump of limited prefetch with distinct as expected on premultiplid head'