my $track_rs = $schema->resultset ('Track')->search (
{ 'me.cd' => { -in => [ $cd_rs->get_column ('cdid')->all ] } },
{
- # the select/as is deliberately silly to test both funcs and refs below
select => [
'me.cd',
{ count => 'me.trackid' },
},
);
+ # this used to fuck up ->all, do not remove!
+ ok ($track_rs->first, 'There is stuff in the rs');
+
is($track_rs->count, 5, 'Prefetched count with groupby');
is($track_rs->all, 5, 'Prefetched objects with groupby');
$schema->storage->debugcb ( sub { $query_cnt++ } );
$schema->storage->debug (1);
- $track_rs->reset;
while (my $collapsed_track = $track_rs->next) {
my $cdid = $collapsed_track->get_column('cd');
is($collapsed_track->get_column('track_count'), 3, "Correct count of tracks for CD $cdid" );
ok($collapsed_track->cd->title, "Prefetched title for CD $cdid" );
}
- is ($query_cnt, 0, 'No queries on prefetched titles');
+ is ($query_cnt, 1, 'Single query on prefetched titles');
$schema->storage->debugcb (undef);
$schema->storage->debug ($sdebug);
}
# Test sql by hand, as the sqlite db will simply paper over
# improper group/select combinations
#
- # the exploded IN needs fixing below, coming in another branch
- #
is_same_sql_bind (
$track_rs->count_rs->as_query,
'(
SELECT me.cd
FROM track me
JOIN cd cd ON cd.cdid = me.cd
- WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
+ WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
GROUP BY me.cd
)
count_subq
SELECT me.cd, COUNT (me.trackid) AS track_count,
FROM track me
JOIN cd cd ON cd.cdid = me.cd
- WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
+ WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
GROUP BY me.cd
) as me
JOIN cd cd ON cd.cdid = me.cd
- WHERE ( me.cd IN ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ) )
+ WHERE ( me.cd IN ( ?, ?, ?, ?, ? ) )
)',
[ map { [ 'me.cd' => $_] } ( ($cd_rs->get_column ('cdid')->all) x 2 ) ],
'next() query generated expected SQL',
# test a has_many/might_have prefetch at the same level
# Note that one of the CDs now has 4 tracks instead of 3
{
- my $most_tracks_rs = $cd_rs->search ({}, {
- prefetch => 'liner_notes', # tracks are alredy prefetched
- select => ['me.cdid', { count => 'tracks.trackid' } ],
- as => [qw/cdid track_count/],
- group_by => 'me.cdid',
- order_by => { -desc => 'track_count' },
- rows => 2,
- });
+ my $most_tracks_rs = $schema->resultset ('CD')->search (
+ {
+ 'me.cdid' => { '!=' => undef }, # duh - this is just to test WHERE
+ },
+ {
+ prefetch => [qw/tracks liner_notes/],
+ select => ['me.cdid', { count => 'tracks.trackid' } ],
+ as => [qw/cdid track_count/],
+ group_by => 'me.cdid',
+ order_by => { -desc => 'track_count' },
+ rows => 2,
+ }
+ );
is_same_sql_bind (
$most_tracks_rs->count_rs->as_query,
FROM cd me
LEFT JOIN track tracks ON tracks.cd = me.cdid
LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
- WHERE ( tracks.cd IS NOT NULL )
+ WHERE ( me.cdid IS NOT NULL )
GROUP BY me.cdid
LIMIT 2
) count_subq
SELECT me.cdid, COUNT( tracks.trackid ) AS track_count
FROM cd me
LEFT JOIN track tracks ON tracks.cd = me.cdid
- WHERE ( tracks.cd IS NOT NULL )
+ WHERE ( me.cdid IS NOT NULL )
GROUP BY me.cdid
ORDER BY track_count DESC
LIMIT 2
) me
LEFT JOIN track tracks ON tracks.cd = me.cdid
LEFT JOIN liner_notes liner_notes ON liner_notes.liner_id = me.cdid
- WHERE ( tracks.cd IS NOT NULL )
+ WHERE ( me.cdid IS NOT NULL )
ORDER BY track_count DESC, tracks.cd
)',
[],
is ($most_tracks_rs->count, 2, 'Limit works');
my $top_cd = $most_tracks_rs->first;
- is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) above
+ is ($top_cd->id, 2, 'Correct cd fetched on top'); # 2 because of the slice(1,1) earlier
my $query_cnt = 0;
$schema->storage->debugcb ( sub { $query_cnt++ } );