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 $first_cd = $schema->resultset('Artist')->first->cds->first;
44 genreid => $first_cd->create_related (
45 genre => ({ name => 'vague genre' })
49 my $rs = $schema->resultset("Artist")->search(undef, {distinct => 1})
50 ->search_related('cds')->search_related('genre',
51 { 'genre.name' => { '!=', 'foo' } },
52 { prefetch => q(cds) },
54 is ($rs->all, 1, 'Correct number of objects');
55 is ($rs->count, 1, 'Correct count');
58 $rs->count_rs->as_query,
64 JOIN cd cds ON cds.artist = me.artistid
65 JOIN genre genre ON genre.genreid = cds.genreid
66 LEFT JOIN cd cds_2 ON cds_2.genreid = genre.genreid
67 WHERE ( genre.name != ? )
68 GROUP BY genre.genreid
71 [ [ 'genre.name' => 'foo' ] ],
75 # non-collapsing prefetch (no multi prefetches)
77 my $rs = $schema->resultset("CD")
78 ->search_related('tracks',
79 { position => [1,2] },
80 { prefetch => [qw/disc lyrics/] },
82 is ($rs->all, 10, 'Correct number of objects');
85 is ($rs->count, 10, 'Correct count');
88 $rs->count_rs->as_query,
92 JOIN track tracks ON tracks.cd = me.cdid
93 JOIN cd disc ON disc.cdid = tracks.cd
94 LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid
95 WHERE position = ? OR position = ?
97 [ map { [ position => $_ ] } (1, 2) ],