8 use DBIC::SqlMakerTest;
11 my $schema = DBICTest->init_schema();
13 # non-collapsing prefetch (no multi prefetches)
15 my $rs = $schema->resultset("CD")
16 ->search_related('tracks',
17 { position => [1,2] },
18 { prefetch => [qw/disc lyrics/], rows => 3, offset => 8 },
20 is ($rs->all, 2, 'Correct number of objects');
24 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
25 $schema->storage->debug(1);
27 is ($rs->count, 2, 'Correct count via count()');
34 JOIN track tracks ON tracks.cd = me.cdid
35 JOIN cd disc ON disc.cdid = tracks.cd
36 WHERE ( ( position = ? OR position = ? ) )
39 'count softlimit applied',
42 my $crs = $rs->count_rs;
43 is ($crs->next, 2, 'Correct count via count_rs()');
51 JOIN track tracks ON tracks.cd = me.cdid
52 JOIN cd disc ON disc.cdid = tracks.cd
53 WHERE ( ( position = ? OR position = ? ) )
57 [ [ position => 1 ], [ position => 2 ] ],
58 'count_rs db-side limit applied',
62 # has_many prefetch with limit
64 my $rs = $schema->resultset("Artist")
65 ->search_related('cds',
66 { 'tracks.position' => [1,2] },
67 { prefetch => [qw/tracks artist/], rows => 3, offset => 4 },
69 is ($rs->all, 1, 'Correct number of objects');
72 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
73 $schema->storage->debug(1);
75 is ($rs->count, 1, 'Correct count via count()');
84 JOIN cd cds ON cds.artist = me.artistid
85 LEFT JOIN track tracks ON tracks.cd = cds.cdid
86 JOIN artist artist ON artist.artistid = cds.artist
87 WHERE tracks.position = ? OR tracks.position = ?
92 'count softlimit applied',
95 my $crs = $rs->count_rs;
96 is ($crs->next, 1, 'Correct count via count_rs()');
104 JOIN cd cds ON cds.artist = me.artistid
105 LEFT JOIN track tracks ON tracks.cd = cds.cdid
106 JOIN artist artist ON artist.artistid = cds.artist
107 WHERE tracks.position = ? OR tracks.position = ?
112 [ [ 'tracks.position' => 1 ], [ 'tracks.position' => 2 ] ],
113 'count_rs db-side limit applied',
117 # count with a having clause
119 my $rs = $schema->resultset("Artist")->search(
123 group_by => 'me.artistid',
124 '+select' => [ { max => 'cds.year', -as => 'newest_cd_year' } ],
125 '+as' => ['newest_cd_year'],
126 having => { 'newest_cd_year' => '2001' }
130 my $crs = $rs->count_rs;
136 SELECT me.artistid, MAX( cds.year ) AS newest_cd_year
138 LEFT JOIN cd cds ON cds.artist = me.artistid
140 HAVING newest_cd_year = ?
143 [ [ 'newest_cd_year' => '2001' ],],
144 'count with having clause keeps sql as alias',
147 is ($crs->next, 2, 'Correct artist count (each with one 2001 cd)');