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();
15 my $sdebug = $schema->storage->debug;
17 my $cd_rs = $schema->resultset('CD')->search (
18 { 'tracks.cd' => { '!=', undef } },
19 { prefetch => 'tracks' },
22 # Database sanity check
23 is($cd_rs->count, 5, 'CDs with tracks count');
25 is ($_->tracks->count, 3, '3 tracks for CD' . $_->id );
28 # Test a belongs_to prefetch of a has_many
30 my $track_rs = $schema->resultset ('Track')->search (
31 { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } },
35 { count => 'me.trackid' },
41 group_by => [qw/me.cd/],
46 # this used to fuck up ->all, do not remove!
47 ok ($track_rs->first, 'There is stuff in the rs');
49 is($track_rs->count, 5, 'Prefetched count with groupby');
50 is($track_rs->all, 5, 'Prefetched objects with groupby');
54 $schema->storage->debugcb ( sub { $query_cnt++ } );
55 $schema->storage->debug (1);
57 while (my $collapsed_track = $track_rs->next) {
58 my $cdid = $collapsed_track->get_column('cd');
59 is($collapsed_track->get_column('track_count'), 3, "Correct count of tracks for CD $cdid" );
60 ok($collapsed_track->cd->title, "Prefetched title for CD $cdid" );
63 is ($query_cnt, 1, 'Single query on prefetched titles');
64 $schema->storage->debugcb (undef);
65 $schema->storage->debug ($sdebug);
68 # Test sql by hand, as the sqlite db will simply paper over
69 # improper group/select combinations
72 $track_rs->count_rs->as_query,
78 JOIN cd cd ON cd.cdid = me.cd
79 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
84 [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' }
85 => $_ ] } ($cd_rs->get_column ('cdid')->all) ],
86 'count() query generated expected SQL',
92 SELECT me.cd, me.track_count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
94 SELECT me.cd, COUNT (me.trackid) AS track_count
96 JOIN cd cd ON cd.cdid = me.cd
97 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
100 JOIN cd cd ON cd.cdid = me.cd
101 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
103 [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' }
104 => $_ ] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ],
105 'next() query generated expected SQL',
109 # add an extra track to one of the cds, and then make sure we can get it on top
110 # (check if limit works)
111 my $top_cd = $cd_rs->slice (1,1)->next;
112 $top_cd->create_related ('tracks', {
113 title => 'over the top',
116 my $top_cd_collapsed_track = $track_rs->search ({}, {
119 { -desc => 'track_count' },
123 is ($top_cd_collapsed_track->count, 2);
127 $top_cd_collapsed_track->first->cd->title,
128 'Correct collapsed track with prefetched CD returned on top'
132 # test a has_many/might_have prefetch at the same level
133 # Note that one of the CDs now has 4 tracks instead of 3
135 my $most_tracks_rs = $schema->resultset ('CD')->search (
137 'me.cdid' => { '!=' => undef }, # duh - this is just to test WHERE
140 prefetch => [qw/tracks liner_notes/],
141 select => ['me.cdid', { count => 'tracks.trackid' }, { max => 'tracks.trackid', -as => 'maxtr'} ],
142 as => [qw/cdid track_count max_track_id/],
143 group_by => 'me.cdid',
144 order_by => [ { -desc => 'track_count' }, { -asc => 'maxtr' } ],
150 $most_tracks_rs->count_rs->as_query,
156 WHERE ( me.cdid IS NOT NULL )
162 'count() query generated expected SQL',
166 $most_tracks_rs->as_query,
168 SELECT me.cdid, me.track_count, me.maxtr,
169 tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at,
170 liner_notes.liner_id, liner_notes.notes
172 SELECT me.cdid, COUNT( tracks.trackid ) AS track_count, MAX( tracks.trackid ) AS maxtr
174 LEFT JOIN track tracks ON tracks.cd = me.cdid
175 WHERE ( me.cdid IS NOT NULL )
177 ORDER BY track_count DESC, maxtr ASC
180 LEFT JOIN track tracks ON tracks.cd = me.cdid
181 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
182 WHERE ( me.cdid IS NOT NULL )
183 ORDER BY track_count DESC, maxtr ASC
186 'next() query generated expected SQL',
189 is ($most_tracks_rs->count, 2, 'Limit works');
190 my ($top_cd) = $most_tracks_rs->all;
191 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier
194 $schema->storage->debugcb ( sub { $query_cnt++ } );
195 $schema->storage->debug (1);
197 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
198 is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct');
199 is ($top_cd->tracks->all, 4, 'Number of prefetched track objects still correct');
201 $top_cd->liner_notes->notes,
203 'Correct liner pre-fetched with top cd',
206 is ($query_cnt, 0, 'No queries executed during prefetched data access');
207 $schema->storage->debugcb (undef);
208 $schema->storage->debug ($sdebug);
212 # test lifted from soulchild
214 my $most_tracks_rs = $schema->resultset ('CD')->search (
216 'me.cdid' => { '!=' => undef }, # this is just to test WHERE
217 'tracks.trackid' => { '!=' => undef },
221 prefetch => 'liner_notes',
222 select => ['me.cdid', 'liner_notes.notes', { count => 'tracks.trackid', -as => 'tr_count' }, { max => 'tracks.trackid', -as => 'tr_maxid'} ],
223 as => [qw/cdid notes track_count max_track_id/],
224 order_by => [ { -desc => 'tr_count' }, { -asc => 'tr_maxid' } ],
225 group_by => 'me.cdid',
231 $most_tracks_rs->as_query,
232 '(SELECT me.cdid, liner_notes.notes, me.tr_count, me.tr_maxid,
233 liner_notes.liner_id, liner_notes.notes
235 SELECT me.cdid, COUNT(tracks.trackid) AS tr_count, MAX(tracks.trackid) AS tr_maxid
237 LEFT JOIN track tracks
238 ON tracks.cd = me.cdid
239 WHERE me.cdid IS NOT NULL AND tracks.trackid IS NOT NULL
241 ORDER BY tr_count DESC, tr_maxid ASC
244 LEFT JOIN track tracks
245 ON tracks.cd = me.cdid
246 LEFT JOIN liner_notes liner_notes
247 ON liner_notes.liner_id = me.cdid
248 WHERE me.cdid IS NOT NULL AND tracks.trackid IS NOT NULL
249 ORDER BY tr_count DESC, tr_maxid ASC
252 'Oddball mysql-ish group_by usage yields valid SQL',
255 is ($most_tracks_rs->count, 2, 'Limit works');
256 my ($top_cd) = $most_tracks_rs->all;
257 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier
260 $schema->storage->debugcb ( sub { $query_cnt++ } );
261 $schema->storage->debug (1);
263 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
265 $top_cd->liner_notes->notes,
267 'Correct liner pre-fetched with top cd',
270 is ($query_cnt, 0, 'No queries executed during prefetched data access');
271 $schema->storage->debugcb (undef);
272 $schema->storage->debug ($sdebug);
276 # make sure that distinct still works
278 my $rs = $schema->resultset("CD")->search({}, {
287 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
288 tags.tagid, tags.cd, tags.tag
290 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
292 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
294 LEFT JOIN tags tags ON tags.cd = me.cdid
298 'Prefetch + distinct resulted in correct group_by',
301 is ($rs->all, 5, 'Correct number of CD objects');
302 is ($rs->count, 5, 'Correct count of CDs');
305 # RT 47779, test group_by as a scalar ref
307 my $track_rs = $schema->resultset ('Track')->search (
308 { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } },
312 { count => 'me.trackid' },
318 group_by => \'SUBSTR(me.cd, 1, 1)',
324 $track_rs->count_rs->as_query,
328 SELECT SUBSTR(me.cd, 1, 1)
330 JOIN cd cd ON cd.cdid = me.cd
331 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
332 GROUP BY SUBSTR(me.cd, 1, 1)
336 [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' }
337 => $_ ] } ($cd_rs->get_column ('cdid')->all) ],
338 'count() query generated expected SQL',
343 my $cd_rs = $schema->resultset('CD')->search({}, {
345 join => [qw/ tracks /],
346 prefetch => [qw/ artist /],
348 is($cd_rs->count, 5, 'complex prefetch + non-prefetching has_many join count correct');
349 is($cd_rs->all, 5, 'complex prefetch + non-prefetching has_many join number of objects correct');
351 # make sure join tracks was thrown out
355 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
356 artist.artistid, artist.name, artist.rank, artist.charfield
358 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
360 JOIN artist artist ON artist.artistid = me.artist
361 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
363 JOIN artist artist ON artist.artistid = me.artist
370 # try the same as above, but add a condition so the tracks join can not be thrown away
371 my $cd_rs2 = $cd_rs->search ({ 'tracks.title' => { '!=' => 'ugabuganoexist' } });
372 is($cd_rs2->count, 5, 'complex prefetch + non-prefetching restricted has_many join count correct');
373 is($cd_rs2->all, 5, 'complex prefetch + non-prefetching restricted has_many join number of objects correct');
375 # the outer group_by seems like a necessary evil, if someone can figure out how to take it away
376 # without breaking compat - be my guest
380 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
381 artist.artistid, artist.name, artist.rank, artist.charfield
383 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
385 LEFT JOIN track tracks ON tracks.cd = me.cdid
386 JOIN artist artist ON artist.artistid = me.artist
387 WHERE ( tracks.title != ? )
388 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
390 LEFT JOIN track tracks ON tracks.cd = me.cdid
391 JOIN artist artist ON artist.artistid = me.artist
392 WHERE ( tracks.title != ? )
393 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
394 artist.artistid, artist.name, artist.rank, artist.charfield
396 [ map { [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'tracks.title' }
397 => 'ugabuganoexist' ] } (1,2)
402 # make sure distinct applies to the CD part only, not to the prefetched/collapsed order_by part
404 my $rs = $schema->resultset('CD')->search({}, {
405 columns => [qw( cdid title )],
406 '+select' => [{ count => 'tags.tag' }],
407 '+as' => ['test_count'],
408 prefetch => ['tags'],
410 order_by => {'-desc' => 'tags.tag'},
415 is_same_sql_bind($rs->as_query,
417 SELECT me.cdid, me.title, me.test_count,
418 tags.tagid, tags.cd, tags.tag
420 SELECT me.cdid, me.title,
421 COUNT( tags.tag ) AS test_count
425 GROUP BY me.cdid, me.title
426 ORDER BY MAX( tags.tag ) DESC
432 ORDER BY tags.tag DESC
434 [ [$ROWS => 3], [$OFFSET => 1] ],
435 'Expected limited prefetch with distinct SQL',
439 { cdid => 4, test_count => 2, title => "Generic Manufactured Singles", tags => [
440 { cd => 4, tag => "Shiny", tagid => 9 },
441 { cd => 4, tag => "Cheesy", tagid => 6 },
444 cdid => 5, test_count => 2, title => "Come Be Depressed With Us", tags => [
445 { cd => 5, tag => "Cheesy", tagid => 7 },
446 { cd => 5, tag => "Blue", tagid => 4 },
449 cdid => 1, test_count => 1, title => "Spoonful of bees", tags => [
450 { cd => 1, tag => "Blue", tagid => 1 },
457 'HRI dump of limited prefetch with distinct as expected'
460 # pre-multiplied main source also should work
461 $rs = $schema->resultset('CD')->search_related('artist')->search_related('cds', {}, {
462 columns => [qw( cdid title )],
463 '+select' => [{ count => 'tags.tag' }],
464 '+as' => ['test_count'],
465 prefetch => ['tags'],
467 order_by => {'-desc' => 'tags.tag'},
472 is_same_sql_bind($rs->as_query,
474 SELECT cds.cdid, cds.title, cds.test_count,
475 tags.tagid, tags.cd, tags.tag
478 ON artist.artistid = me.artist
480 SELECT cds.cdid, cds.title,
481 COUNT( tags.tag ) AS test_count,
485 ON artist.artistid = me.artist
487 ON cds.artist = artist.artistid
489 ON tags.cd = cds.cdid
490 GROUP BY cds.cdid, cds.title, cds.artist
491 ORDER BY MAX( tags.tag ) DESC
495 ON cds.artist = artist.artistid
497 ON tags.cd = cds.cdid
498 ORDER BY tags.tag DESC
500 [ [$ROWS => 3], [$OFFSET => 1] ],
501 'Expected limited prefetch with distinct SQL on premultiplied head',
504 # Tag counts are multiplied by the cd->artist->cds multiplication
505 # I would *almost* call this "expected" without wraping an as_subselect_rs
507 local $TODO = 'Not sure if we can stop the count/group of premultiplication abstraction leak';
511 'HRI dump of limited prefetch with distinct as expected on premultiplid head'