8 use DBIC::SqlMakerTest;
9 use DBIx::Class::SQLMaker::LimitDialects;
11 my $ROWS = DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype;
13 my $schema = DBICTest->init_schema();
14 my $sdebug = $schema->storage->debug;
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');
53 $schema->storage->debugcb ( sub { $query_cnt++ } );
54 $schema->storage->debug (1);
56 while (my $collapsed_track = $track_rs->next) {
57 my $cdid = $collapsed_track->get_column('cd');
58 is($collapsed_track->get_column('track_count'), 3, "Correct count of tracks for CD $cdid" );
59 ok($collapsed_track->cd->title, "Prefetched title for CD $cdid" );
62 is ($query_cnt, 1, 'Single query on prefetched titles');
63 $schema->storage->debugcb (undef);
64 $schema->storage->debug ($sdebug);
67 # Test sql by hand, as the sqlite db will simply paper over
68 # improper group/select combinations
71 $track_rs->count_rs->as_query,
77 JOIN cd cd ON cd.cdid = me.cd
78 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
83 [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' }
84 => $_ ] } ($cd_rs->get_column ('cdid')->all) ],
85 'count() query generated expected SQL',
91 SELECT me.cd, me.track_count, cd.cdid, cd.artist, cd.title, cd.year, cd.genreid, cd.single_track
93 SELECT me.cd, COUNT (me.trackid) AS track_count
95 JOIN cd cd ON cd.cdid = me.cd
96 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
99 JOIN cd cd ON cd.cdid = me.cd
100 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
102 [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' }
103 => $_ ] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ],
104 'next() query generated expected SQL',
108 # add an extra track to one of the cds, and then make sure we can get it on top
109 # (check if limit works)
110 my $top_cd = $cd_rs->slice (1,1)->next;
111 $top_cd->create_related ('tracks', {
112 title => 'over the top',
115 my $top_cd_collapsed_track = $track_rs->search ({}, {
118 { -desc => 'track_count' },
122 is ($top_cd_collapsed_track->count, 2);
126 $top_cd_collapsed_track->first->cd->title,
127 'Correct collapsed track with prefetched CD returned on top'
131 # test a has_many/might_have prefetch at the same level
132 # Note that one of the CDs now has 4 tracks instead of 3
134 my $most_tracks_rs = $schema->resultset ('CD')->search (
136 'me.cdid' => { '!=' => undef }, # duh - this is just to test WHERE
139 prefetch => [qw/tracks liner_notes/],
140 select => ['me.cdid', { count => 'tracks.trackid' }, { max => 'tracks.trackid', -as => 'maxtr'} ],
141 as => [qw/cdid track_count max_track_id/],
142 group_by => 'me.cdid',
143 order_by => [ { -desc => 'track_count' }, { -asc => 'maxtr' } ],
149 $most_tracks_rs->count_rs->as_query,
155 WHERE ( me.cdid IS NOT NULL )
161 'count() query generated expected SQL',
165 $most_tracks_rs->as_query,
167 SELECT me.cdid, me.track_count, me.maxtr,
168 tracks.trackid, tracks.cd, tracks.position, tracks.title, tracks.last_updated_on, tracks.last_updated_at,
169 liner_notes.liner_id, liner_notes.notes
171 SELECT me.cdid, COUNT( tracks.trackid ) AS track_count, MAX( tracks.trackid ) AS maxtr
173 LEFT JOIN track tracks ON tracks.cd = me.cdid
174 WHERE ( me.cdid IS NOT NULL )
176 ORDER BY track_count DESC, maxtr ASC
179 LEFT JOIN track tracks ON tracks.cd = me.cdid
180 LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
181 WHERE ( me.cdid IS NOT NULL )
182 ORDER BY track_count DESC, maxtr ASC, tracks.cd
185 'next() query generated expected SQL',
188 is ($most_tracks_rs->count, 2, 'Limit works');
189 my $top_cd = $most_tracks_rs->first;
190 is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier
193 $schema->storage->debugcb ( sub { $query_cnt++ } );
194 $schema->storage->debug (1);
196 is ($top_cd->get_column ('track_count'), 4, 'Track count fetched correctly');
197 is ($top_cd->tracks->count, 4, 'Count of prefetched tracks rs still correct');
198 is ($top_cd->tracks->all, 4, 'Number of prefetched track objects still correct');
200 $top_cd->liner_notes->notes,
202 'Correct liner pre-fetched with top cd',
205 is ($query_cnt, 0, 'No queries executed during prefetched data access');
206 $schema->storage->debugcb (undef);
207 $schema->storage->debug ($sdebug);
210 # make sure that distinct still works
212 my $rs = $schema->resultset("CD")->search({}, {
221 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
222 tags.tagid, tags.cd, tags.tag
224 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
226 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
229 LEFT JOIN tags tags ON tags.cd = me.cdid
230 ORDER BY cdid, tags.cd, tags.tag
233 'Prefetch + distinct resulted in correct group_by',
236 is ($rs->all, 5, 'Correct number of CD objects');
237 is ($rs->count, 5, 'Correct count of CDs');
240 # RT 47779, test group_by as a scalar ref
242 my $track_rs = $schema->resultset ('Track')->search (
243 { 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } },
247 { count => 'me.trackid' },
253 group_by => \'SUBSTR(me.cd, 1, 1)',
259 $track_rs->count_rs->as_query,
263 SELECT SUBSTR(me.cd, 1, 1)
265 JOIN cd cd ON cd.cdid = me.cd
266 WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
267 GROUP BY SUBSTR(me.cd, 1, 1)
271 [ map { [ { sqlt_datatype => 'integer', dbic_colname => 'me.cd' }
272 => $_ ] } ($cd_rs->get_column ('cdid')->all) ],
273 'count() query generated expected SQL',
278 my $cd_rs = $schema->resultset('CD')->search({}, {
280 join => [qw/ tracks /],
281 prefetch => [qw/ artist /],
283 is($cd_rs->count, 5, 'complex prefetch + non-prefetching has_many join count correct');
284 is($cd_rs->all, 5, 'complex prefetch + non-prefetching has_many join number of objects correct');
286 # make sure join tracks was thrown out
290 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
291 artist.artistid, artist.name, artist.rank, artist.charfield
293 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
295 JOIN artist artist ON artist.artistid = me.artist
296 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
298 JOIN artist artist ON artist.artistid = me.artist
305 # try the same as above, but add a condition so the tracks join can not be thrown away
306 my $cd_rs2 = $cd_rs->search ({ 'tracks.title' => { '!=' => 'ugabuganoexist' } });
307 is($cd_rs2->count, 5, 'complex prefetch + non-prefetching restricted has_many join count correct');
308 is($cd_rs2->all, 5, 'complex prefetch + non-prefetching restricted has_many join number of objects correct');
310 # the outer group_by seems like a necessary evil, if someone can figure out how to take it away
311 # without breaking compat - be my guest
315 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
316 artist.artistid, artist.name, artist.rank, artist.charfield
318 SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
320 LEFT JOIN track tracks ON tracks.cd = me.cdid
321 JOIN artist artist ON artist.artistid = me.artist
322 WHERE ( tracks.title != ? )
323 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track
325 LEFT JOIN track tracks ON tracks.cd = me.cdid
326 JOIN artist artist ON artist.artistid = me.artist
327 WHERE ( tracks.title != ? )
328 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track,
329 artist.artistid, artist.name, artist.rank, artist.charfield
331 [ map { [ { sqlt_datatype => 'varchar', sqlt_size => 100, dbic_colname => 'tracks.title' }
332 => 'ugabuganoexist' ] } (1,2)
338 my $rs = $schema->resultset('CD')->search({},
340 '+select' => [{ count => 'tags.tag' }],
341 '+as' => ['test_count'],
342 prefetch => ['tags'],
344 order_by => {'-asc' => 'tags.tag'},
348 is_same_sql_bind($rs->as_query, q{
349 (SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, me.test_count, tags.tagid, tags.cd, tags.tag
350 FROM (SELECT me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, COUNT( tags.tag ) AS test_count
351 FROM cd me LEFT JOIN tags tags ON tags.cd = me.cdid
352 GROUP BY me.cdid, me.artist, me.title, me.year, me.genreid, me.single_track, tags.tag
353 ORDER BY tags.tag ASC LIMIT ?)
355 LEFT JOIN tags tags ON tags.cd = me.cdid
356 ORDER BY tags.tag ASC, tags.cd, tags.tag