8 use DBIC::SqlMakerTest;
10 use DBIx::Class::SQLMaker::LimitDialects;
12 my ($ROWS, $OFFSET) = (
13 DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
14 DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
17 my $schema = DBICTest->init_schema();
19 # non-collapsing prefetch (no multi prefetches)
21 my $rs = $schema->resultset("CD")
22 ->search_related('tracks',
23 { position => [1,2] },
24 { prefetch => [qw/disc lyrics/], rows => 3, offset => 8 },
26 is ($rs->all, 2, 'Correct number of objects');
30 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
31 $schema->storage->debug(1);
33 is ($rs->count, 2, 'Correct count via count()');
40 JOIN track tracks ON tracks.cd = me.cdid
41 JOIN cd disc ON disc.cdid = tracks.cd
42 WHERE ( ( position = ? OR position = ? ) )
45 'count softlimit applied',
48 my $crs = $rs->count_rs;
49 is ($crs->next, 2, 'Correct count via count_rs()');
57 JOIN track tracks ON tracks.cd = me.cdid
58 JOIN cd disc ON disc.cdid = tracks.cd
59 WHERE ( ( position = ? OR position = ? ) )
64 [ { sqlt_datatype => 'int', dbic_colname => 'position' }
66 [ { sqlt_datatype => 'int', dbic_colname => 'position' }
71 'count_rs db-side limit applied',
75 # has_many prefetch with limit
77 my $rs = $schema->resultset("Artist")
78 ->search_related('cds',
79 { 'tracks.position' => [1,2] },
80 { prefetch => [qw/tracks artist/], rows => 3, offset => 4 },
82 is ($rs->all, 1, 'Correct number of objects');
85 $schema->storage->debugobj(DBIC::DebugObj->new(\$sql, \@bind));
86 $schema->storage->debug(1);
88 is ($rs->count, 1, 'Correct count via count()');
97 JOIN cd cds ON cds.artist = me.artistid
98 LEFT JOIN track tracks ON tracks.cd = cds.cdid
99 JOIN artist artist ON artist.artistid = cds.artist
100 WHERE tracks.position = ? OR tracks.position = ?
105 'count softlimit applied',
108 my $crs = $rs->count_rs;
109 is ($crs->next, 1, 'Correct count via count_rs()');
117 JOIN cd cds ON cds.artist = me.artistid
118 LEFT JOIN track tracks ON tracks.cd = cds.cdid
119 JOIN artist artist ON artist.artistid = cds.artist
120 WHERE tracks.position = ? OR tracks.position = ?
126 [ { sqlt_datatype => 'int', dbic_colname => 'tracks.position' }
128 [ { sqlt_datatype => 'int', dbic_colname => 'tracks.position' }
133 'count_rs db-side limit applied',
137 # count with a having clause
139 my $rs = $schema->resultset("Artist")->search(
143 group_by => 'me.artistid',
144 '+select' => [ { max => 'cds.year', -as => 'newest_cd_year' } ],
145 '+as' => ['newest_cd_year'],
146 having => { 'newest_cd_year' => '2001' }
150 my $crs = $rs->count_rs;
156 SELECT me.artistid, MAX( cds.year ) AS newest_cd_year,
158 LEFT JOIN cd cds ON cds.artist = me.artistid
160 HAVING newest_cd_year = ?
163 [ [ { dbic_colname => 'newest_cd_year' }
165 'count with having clause keeps sql as alias',
168 is ($crs->next, 2, 'Correct artist count (each with one 2001 cd)');