9 use DBIC::SqlMakerTest;
11 my $schema = DBICTest->init_schema();
12 my $sdebug = $schema->storage->debug;
14 my $cd_rs = $schema->resultset('CD')->search (
15 { 'tracks.cd' => { '!=', undef } },
16 { prefetch => 'tracks' },
19 # Database sanity check
20 is($cd_rs->count, 5, 'CDs with tracks count');
22 is ($_->tracks->count, 3, '3 tracks for CD' . $_->id );
25 # Test a belongs_to prefetch of a has_many
27 my $track_rs = $schema->resultset ('Track')->search (
28 { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } },
32 { count => 'me.trackid' },
38 group_by => [qw/me.cd/],
43 # this used to fuck up ->all, do not remove!
44 ok ($track_rs->first, 'There is stuff in the rs');
46 is($track_rs->count, 5, 'Prefetched count with groupby');
47 is($track_rs->all, 5, 'Prefetched objects with groupby');
51 $schema->storage->debugcb ( sub { $query_cnt++ } );
52 $schema->storage->debug (1);
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" );
60 is ($query_cnt, 1, 'Single query on prefetched titles');
61 $schema->storage->debugcb (undef);
62 $schema->storage->debug ($sdebug);
65 # Test sql by hand, as the sqlite db will simply paper over
66 # improper group/select combinations
69 $track_rs->count_rs->as_query,
75 JOIN cd cd ON cd.cdid = me.cd
76 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
81 [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ],
82 'count() query generated expected SQL',
88 SELECT me.cd, me.track_count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
90 SELECT me.cd, COUNT (me.trackid) AS track_count,
92 JOIN cd cd ON cd.cdid = me.cd
93 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
96 JOIN cd cd ON cd.cdid = me.cd
97 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
99 [ map { [ 'me.cd' => $_] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ],
100 'next() query generated expected SQL',
104 # add an extra track to one of the cds, and then make sure we can get it on top
105 # (check if limit works)
106 my $top_cd = $cd_rs->slice (1,1)->next;
107 $top_cd->create_related ('tracks', {
108 title => 'over the top',
111 my $top_cd_collapsed_track = $track_rs->search ({}, {
114 { -desc => 'track_count' },
118 is ($top_cd_collapsed_track->count, 2);
122 $top_cd_collapsed_track->first->cd->title,
123 'Correct collapsed track with prefetched CD returned on top'
127 # test a has_many/might_have prefetch at the same level
128 # Note that one of the CDs now has 4 tracks instead of 3
130 my $most_tracks_rs = $schema->resultset ('CD')->search (
132 'me.cdid' => { '!=' => undef }, # duh - this is just to test WHERE
135 prefetch => [qw/tracks liner_notes/],
136 select => ['me.cdid', { count => 'tracks.trackid' }, { max => 'tracks.trackid', -as => 'maxtr'} ],
137 as => [qw/cdid track_count max_track_id/],
138 group_by => 'me.cdid',
139 order_by => [ { -desc => 'track_count' }, { -asc => 'maxtr' } ],
145 $most_tracks_rs->count_rs->as_query,
151 LEFT JOIN track tracks ON tracks.cd = me.cdid
152 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
153 WHERE ( me.cdid IS NOT NULL )
159 'count() query generated expected SQL',
163 $most_tracks_rs->as_query,
165 SELECT me.cdid, me.track_count, me.maxtr,
166 tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at, tracks.small_dt,
167 liner_notes.liner_id, liner_notes.notes
169 SELECT me.cdid, COUNT( tracks.trackid ) AS track_count, MAX( tracks.trackid ) AS maxtr,
171 LEFT JOIN track tracks ON tracks.cd = me.cdid
172 WHERE ( me.cdid IS NOT NULL )
174 ORDER BY track_count DESC, maxtr ASC
177 LEFT JOIN track tracks ON tracks.cd = me.cdid
178 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
179 WHERE ( me.cdid IS NOT NULL )
180 ORDER BY track_count DESC, maxtr ASC, tracks.cd
183 'next() query generated expected SQL',
186 is ($most_tracks_rs->count, 2, 'Limit works');
187 my $top_cd = $most_tracks_rs->first;
188 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier
191 $schema->storage->debugcb ( sub { $query_cnt++ } );
192 $schema->storage->debug (1);
194 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
195 is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct');
196 is ($top_cd->tracks->all, 4, 'Number of prefetched track objects still correct');
198 $top_cd->liner_notes->notes,
200 'Correct liner pre-fetched with top cd',
203 is ($query_cnt, 0, 'No queries executed during prefetched data access');
204 $schema->storage->debugcb (undef);
205 $schema->storage->debug ($sdebug);
208 # make sure that distinct still works
210 my $rs = $schema->resultset("CD")->search({}, {
219 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
220 tags.tagid, tags.cd, tags.tag
222 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
224 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, cdid
227 LEFT JOIN tags tags ON tags.cd = me.cdid
228 ORDER BY cdid, tags.cd, tags.tag
231 'Prefetch + distinct resulted in correct group_by',
234 is ($rs->all, 5, 'Correct number of CD objects');
235 is ($rs->count, 5, 'Correct count of CDs');
238 # RT 47779, test group_by as a scalar ref
240 my $track_rs = $schema->resultset ('Track')->search (
241 { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } },
245 { count => 'me.trackid' },
251 group_by => \'SUBSTR(me.cd, 1, 1)',
257 $track_rs->count_rs->as_query,
261 SELECT SUBSTR(me.cd, 1, 1)
263 JOIN cd cd ON cd.cdid = me.cd
264 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
265 GROUP BY SUBSTR(me.cd, 1, 1)
269 [ map { [ 'me.cd' => $_] } ($cd_rs->get_column ('cdid')->all) ],
270 'count() query generated expected SQL',
275 my $cd_rs = $schema->resultset('CD')->search({}, {
277 join => [qw/ tracks /],
278 prefetch => [qw/ artist /],
280 is($cd_rs->count, 5, 'complex prefetch + non-prefetching has_many join count correct');
281 is($cd_rs->all, 5, 'complex prefetch + non-prefetching has_many join number of objects correct');
283 # make sure join tracks was thrown out
287 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
288 artist.artistid, artist.name, artist.rank, artist.charfield
290 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
292 JOIN artist artist ON artist.artistid = me.artist
293 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
295 JOIN artist artist ON artist.artistid = me.artist
302 # try the same as above, but add a condition so the tracks join can not be thrown away
303 my $cd_rs2 = $cd_rs->search ({ 'tracks.title' => { '!=' => 'ugabuganoexist' } });
304 is($cd_rs2->count, 5, 'complex prefetch + non-prefetching restricted has_many join count correct');
305 is($cd_rs2->all, 5, 'complex prefetch + non-prefetching restricted has_many join number of objects correct');
307 # the outer group_by seems like a necessary evil, if someone can figure out how to take it away
308 # without breaking compat - be my guest
312 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
313 artist.artistid, artist.name, artist.rank, artist.charfield
315 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
317 LEFT JOIN track tracks ON tracks.cd = me.cdid
318 JOIN artist artist ON artist.artistid = me.artist
319 WHERE ( tracks.title != ? )
320 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
322 LEFT JOIN track tracks ON tracks.cd = me.cdid
323 JOIN artist artist ON artist.artistid = me.artist
324 WHERE ( tracks.title != ? )
325 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
326 artist.artistid, artist.name, artist.rank, artist.charfield
328 [ map { [ 'tracks.title' => 'ugabuganoexist' ] } (1 .. 2) ],
333 my $rs = $schema->resultset('CD')->search({},
335 '+select' => [{ count => 'tags.tag' }],
336 '+as' => ['test_count'],
337 prefetch => ['tags'],
339 order_by => {'-asc' => 'tags.tag'},
343 is_same_sql_bind($rs->as_query, q{
344 (SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, me.test_count, tags.tagid, tags.cd, tags.tag
345 FROM (SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, COUNT( tags.tag ) AS test_count
346 FROM cd me LEFT JOIN tags tags ON tags.cd = me.cdid
347 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, tags.tag
348 ORDER BY tags.tag ASC LIMIT 1)
350 LEFT JOIN tags tags ON tags.cd = me.cdid
351 ORDER BY tags.tag ASC, tags.cd, tags.tag