X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fcount%2Fcount_rs.t;h=83b625755de37f30f10b24b6a856ec8e86aee471;hb=f073420b2c87814662fc34fd544e29bf0883f26c;hp=acf696cbcd8114fcfadff5febdeb15e980fbea90;hpb=c98169a74e44ef761b38d738d1d16f8a693d0a46;p=dbsrgits%2FDBIx-Class.git diff --git a/t/count/count_rs.t b/t/count/count_rs.t index acf696c..83b6257 100644 --- a/t/count/count_rs.t +++ b/t/count/count_rs.t @@ -7,8 +7,12 @@ use Test::More; use DBICTest; use DBIC::SqlMakerTest; use DBIC::DebugObj; +use DBIx::Class::SQLMaker::LimitDialects; -plan tests => 10; +my ($ROWS, $OFFSET) = ( + DBIx::Class::SQLMaker::LimitDialects->__rows_bindtype, + DBIx::Class::SQLMaker::LimitDialects->__offset_bindtype, +); my $schema = DBICTest->init_schema(); @@ -35,7 +39,6 @@ my $schema = DBICTest->init_schema(); FROM cd me JOIN track tracks ON tracks.cd = me.cdid JOIN cd disc ON disc.cdid = tracks.cd - LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid WHERE ( ( position = ? OR position = ? ) ) ', [ qw/'1' '2'/ ], @@ -53,12 +56,18 @@ my $schema = DBICTest->init_schema(); FROM cd me JOIN track tracks ON tracks.cd = me.cdid JOIN cd disc ON disc.cdid = tracks.cd - LEFT JOIN lyrics lyrics ON lyrics.track_id = tracks.trackid WHERE ( ( position = ? OR position = ? ) ) - LIMIT 3 OFFSET 8 - ) count_subq + LIMIT ? OFFSET ? + ) tracks )', - [ [ position => 1 ], [ position => 2 ] ], + [ + [ { sqlt_datatype => 'int', dbic_colname => 'position' } + => 1 ], + [ { sqlt_datatype => 'int', dbic_colname => 'position' } + => 2 ], + [$ROWS => 3], + [$OFFSET => 8], + ], 'count_rs db-side limit applied', ); } @@ -90,7 +99,7 @@ my $schema = DBICTest->init_schema(); JOIN artist artist ON artist.artistid = cds.artist WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid - ) count_subq + ) cds ', [ qw/'1' '2'/ ], 'count softlimit applied', @@ -110,10 +119,91 @@ my $schema = DBICTest->init_schema(); JOIN artist artist ON artist.artistid = cds.artist WHERE tracks.position = ? OR tracks.position = ? GROUP BY cds.cdid - LIMIT 3 OFFSET 4 - ) count_subq + LIMIT ? OFFSET ? + ) cds )', - [ [ 'tracks.position' => 1 ], [ 'tracks.position' => 2 ] ], + [ + [ { sqlt_datatype => 'int', dbic_colname => 'tracks.position' } + => 1 ], + [ { sqlt_datatype => 'int', dbic_colname => 'tracks.position' } + => 2 ], + [ $ROWS => 3], + [$OFFSET => 4], + ], 'count_rs db-side limit applied', ); } + +# count with a having clause +{ + my $rs = $schema->resultset("Artist")->search( + {}, + { + join => 'cds', + group_by => 'me.artistid', + '+select' => [ { max => 'cds.year', -as => 'newest_cd_year' } ], + '+as' => ['newest_cd_year'], + having => { 'newest_cd_year' => '2001' } + } + ); + + my $crs = $rs->count_rs; + + is_same_sql_bind ( + $crs->as_query, + '(SELECT COUNT( * ) + FROM ( + SELECT me.artistid, MAX( cds.year ) AS newest_cd_year + FROM artist me + LEFT JOIN cd cds ON cds.artist = me.artistid + GROUP BY me.artistid + HAVING newest_cd_year = ? + ) me + )', + [ [ { dbic_colname => 'newest_cd_year' } + => '2001' ] ], + 'count with having clause keeps sql as alias', + ); + + is ($crs->next, 2, 'Correct artist count (each with one 2001 cd)'); +} + +# count with two having clauses +{ + my $rs = $schema->resultset("Artist")->search( + {}, + { + join => 'cds', + group_by => 'me.artistid', + '+select' => [ { max => 'cds.year', -as => 'newest_cd_year' } ], + '+as' => ['newest_cd_year'], + having => { 'newest_cd_year' => [ '1998', '2001' ] } + } + ); + + my $crs = $rs->count_rs; + + is_same_sql_bind ( + $crs->as_query, + '(SELECT COUNT( * ) + FROM ( + SELECT me.artistid, MAX( cds.year ) AS newest_cd_year + FROM artist me + LEFT JOIN cd cds ON cds.artist = me.artistid + GROUP BY me.artistid + HAVING newest_cd_year = ? OR newest_cd_year = ? + ) me + )', + [ + [ { dbic_colname => 'newest_cd_year' } + => '1998' ], + [ { dbic_colname => 'newest_cd_year' } + => '2001' ], + ], + 'count with having clause keeps sql as alias', + ); + + is ($crs->next, 3, 'Correct artist count (each with one 1998 or 2001 cd)'); +} + +done_testing;