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 = ? ) )
58 [ { sqlt_datatype => 'int', dbic_colname => 'position' }
60 [ { sqlt_datatype => 'int', dbic_colname => 'position' }
63 'count_rs db-side limit applied',
67 # has_many prefetch with limit
69 my $rs = $schema->resultset("Artist")
70 ->search_related('cds',
71 { 'tracks.position' => [1,2] },
72 { prefetch => [qw/tracks artist/], rows => 3, offset => 4 },
74 is ($rs->all, 1, 'Correct number of objects');
77 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
78 $schema->storage->debug(1);
80 is ($rs->count, 1, 'Correct count via count()');
89 JOIN cd cds ON cds.artist = me.artistid
90 LEFT JOIN track tracks ON tracks.cd = cds.cdid
91 JOIN artist artist ON artist.artistid = cds.artist
92 WHERE tracks.position = ? OR tracks.position = ?
97 'count softlimit applied',
100 my $crs = $rs->count_rs;
101 is ($crs->next, 1, 'Correct count via count_rs()');
109 JOIN cd cds ON cds.artist = me.artistid
110 LEFT JOIN track tracks ON tracks.cd = cds.cdid
111 JOIN artist artist ON artist.artistid = cds.artist
112 WHERE tracks.position = ? OR tracks.position = ?
118 [ { sqlt_datatype => 'int', dbic_colname => 'tracks.position' }
120 [ { sqlt_datatype => 'int', dbic_colname => 'tracks.position' }
123 'count_rs db-side limit applied',
127 # count with a having clause
129 my $rs = $schema->resultset("Artist")->search(
133 group_by => 'me.artistid',
134 '+select' => [ { max => 'cds.year', -as => 'newest_cd_year' } ],
135 '+as' => ['newest_cd_year'],
136 having => { 'newest_cd_year' => '2001' }
140 my $crs = $rs->count_rs;
146 SELECT me.artistid, MAX( cds.year ) AS newest_cd_year,
148 LEFT JOIN cd cds ON cds.artist = me.artistid
150 HAVING newest_cd_year = ?
153 [ [ { dbic_colname => 'newest_cd_year' }
155 'count with having clause keeps sql as alias',
158 is ($crs->next, 2, 'Correct artist count (each with one 2001 cd)');