8 use DBIC::SqlMakerTest;
9 use DBIx::Class::SQLMaker::LimitDialects;
11 my ($ROWS, $OFFSET) = (
12 DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype,
13 DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype,
16 my $schema = DBICTest->init_schema();
18 # non-collapsing prefetch (no multi prefetches)
20 my $rs = $schema->resultset("CD")
21 ->search_related('tracks',
22 { position => [1,2] },
23 { prefetch => [qw/disc lyrics/], rows => 3, offset => 8 },
26 [ { sqlt_datatype => 'int', dbic_colname => 'position' }
28 [ { sqlt_datatype => 'int', dbic_colname => 'position' }
32 is ($rs->all, 2, 'Correct number of objects');
34 $schema->is_executed_sql_bind( sub {
35 is ($rs->count, 2, 'Correct count via count()');
39 JOIN track tracks ON tracks.cd = me.cdid
40 JOIN cd disc ON disc.cdid = tracks.cd
41 WHERE ( ( position = ? OR position = ? ) )
43 ]], 'count softlimit applied');
45 my $crs = $rs->count_rs;
46 is ($crs->next, 2, 'Correct count via count_rs()');
54 JOIN track tracks ON tracks.cd = me.cdid
55 JOIN cd disc ON disc.cdid = tracks.cd
56 WHERE ( ( position = ? OR position = ? ) )
60 [ @wherebind, [$ROWS => 3], [$OFFSET => 8] ],
61 'count_rs db-side limit applied',
65 # has_many prefetch with limit
67 my $rs = $schema->resultset("Artist")
68 ->search_related('cds',
69 { 'tracks.position' => [1,2] },
70 { prefetch => [qw/tracks artist/], rows => 3, offset => 4 },
73 [ { sqlt_datatype => 'int', dbic_colname => 'tracks.position' }
75 [ { sqlt_datatype => 'int', dbic_colname => 'tracks.position' }
79 is ($rs->all, 1, 'Correct number of objects');
81 $schema->is_executed_sql_bind( sub {
82 is ($rs->count, 1, 'Correct count via count()');
88 JOIN cd cds ON cds.artist = me.artistid
89 LEFT JOIN track tracks ON tracks.cd = cds.cdid
90 JOIN artist artist ON artist.artistid = cds.artist
91 WHERE tracks.position = ? OR tracks.position = ?
95 ]], 'count softlimit applied' );
97 my $crs = $rs->count_rs;
98 is ($crs->next, 1, 'Correct count via count_rs()');
106 JOIN cd cds ON cds.artist = me.artistid
107 LEFT JOIN track tracks ON tracks.cd = cds.cdid
108 JOIN artist artist ON artist.artistid = cds.artist
109 WHERE tracks.position = ? OR tracks.position = ?
114 [ @wherebind, [$ROWS => 3], [$OFFSET => 4], ],
115 'count_rs db-side limit applied',
119 # count with a having clause
121 my $rs = $schema->resultset("Artist")->search(
125 group_by => 'me.artistid',
126 '+select' => [ { max => 'cds.year', -as => 'newest_cd_year' } ],
127 '+as' => ['newest_cd_year'],
128 having => { 'newest_cd_year' => '2001' }
132 my $crs = $rs->count_rs;
138 SELECT me.artistid, MAX( cds.year ) AS newest_cd_year
140 LEFT JOIN cd cds ON cds.artist = me.artistid
142 HAVING newest_cd_year = ?
145 [ [ { dbic_colname => 'newest_cd_year' }
147 'count with having clause keeps sql as alias',
150 is ($crs->next, 2, 'Correct artist count (each with one 2001 cd)');
153 # count with two having clauses
155 my $rs = $schema->resultset("Artist")->search(
159 group_by => 'me.artistid',
160 '+select' => [ { max => 'cds.year', -as => 'newest_cd_year' } ],
161 '+as' => ['newest_cd_year'],
162 having => { 'newest_cd_year' => [ '1998', '2001' ] }
166 my $crs = $rs->count_rs;
172 SELECT me.artistid, MAX( cds.year ) AS newest_cd_year
174 LEFT JOIN cd cds ON cds.artist = me.artistid
176 HAVING newest_cd_year = ? OR newest_cd_year = ?
180 [ { dbic_colname => 'newest_cd_year' }
182 [ { dbic_colname => 'newest_cd_year' }
185 'count with having clause keeps sql as alias',
188 is ($crs->next, 3, 'Correct artist count (each with one 1998 or 2001 cd)');