8 use DBIC::SqlMakerTest;
10 my $schema = DBICTest->init_schema();
14 my $rs = $schema->resultset("Artist")
15 ->search_related('cds',
16 { 'tracks.position' => [1,2] },
17 { prefetch => [qw/tracks artist/] },
19 is ($rs->all, 5, 'Correct number of objects');
20 is ($rs->count, 5, 'Correct count');
23 $rs->count_rs->as_query,
29 JOIN cd cds ON cds.artist = me.artistid
30 LEFT JOIN track tracks ON tracks.cd = cds.cdid
31 JOIN artist artist ON artist.artistid = cds.artist
32 WHERE tracks.position = ? OR tracks.position = ?
36 [ map { [ 'tracks.position' => $_ ] } (1, 2) ],
40 # collapsing prefetch with distinct
42 my $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
43 ->search_related('cds')->search_related('genre',
44 { 'genre.name' => 'emo' },
45 { prefetch => q(cds) },
47 is ($rs->all, 1, 'Correct number of objects');
48 is ($rs->count, 1, 'Correct count');
51 $rs->count_rs->as_query,
57 SELECT me.artistid, me.name, me.rank, me.charfield
59 GROUP BY me.artistid, me.name, me.rank, me.charfield
61 JOIN cd cds ON cds.artist = me.artistid
62 JOIN genre genre ON genre.genreid = cds.genreid
63 WHERE ( genre.name = ? )
64 GROUP BY genre.genreid
68 [ [ 'genre.name' => 'emo' ] ],
72 # non-collapsing prefetch (no multi prefetches)
74 my $rs = $schema->resultset("CD")
75 ->search_related('tracks',
76 { position => [1,2], 'lyrics.lyric_id' => undef },
77 { prefetch => [qw/disc lyrics/] },
79 is ($rs->all, 10, 'Correct number of objects');
82 is ($rs->count, 10, 'Correct count');
85 $rs->count_rs->as_query,
89 JOIN track tracks ON tracks.cd = me.cdid
90 JOIN cd disc ON disc.cdid = tracks.cd
91 LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid
92 WHERE lyrics.lyric_id IS NULL AND (position = ? OR position = ?)
94 [ map { [ position => $_ ] } (1, 2) ],