From: Justin Hunter Date: Mon, 30 Mar 2009 19:40:05 +0000 (+0000) Subject: * add more tests X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0027911c408b489f46088be90b6418c463ea112b;p=dbsrgits%2FDBIx-Class-Historic.git * add more tests * remove old cruft * remove old note --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index bde72fe..0c38d55 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1117,12 +1117,6 @@ Performs an SQL C with the same query as the resultset was built with to find the number of elements. If passed arguments, does a search on the resultset and counts the results of that. -Note: When using C with C, L emulates C -using C. Some databases (notably SQLite) do -not support C with multiple columns. If you are using such a -database, you should only use columns from the main table in your C -clause. - =cut sub count { @@ -1146,20 +1140,7 @@ sub _count { # Separated out so pager can get the full count my $attrs = { %{$self->_resolved_attrs} }; if (my $group_by = $attrs->{group_by}) { - delete $attrs->{having}; delete $attrs->{order_by}; - my @distinct = (ref $group_by ? @$group_by : ($group_by)); - # todo: try CONCAT for multi-column pk - my @pk = $self->result_source->primary_columns; - if (@pk == 1) { - my $alias = $attrs->{alias}; - foreach my $column (@distinct) { - if ($column =~ qr/^(?:\Q${alias}.\E)?$pk[0]$/) { - @distinct = ($column); - last; - } - } - } $attrs->{select} = $group_by; $attrs->{from} = [ { 'mesub' => (ref $self)->new($self->result_source, $attrs)->cursor->as_query } ]; diff --git a/t/count_distinct.t b/t/count_distinct.t index b0ebd5f..9e2219f 100644 --- a/t/count_distinct.t +++ b/t/count_distinct.t @@ -12,16 +12,45 @@ my $schema = DBICTest->init_schema(); eval "use DBD::SQLite"; plan skip_all => 'needs DBD::SQLite for testing' if $@; -plan tests => 5; +plan tests => 13; + +my $in_rs = $schema->resultset("Tag")->search({ tag => [ 'Blue', 'Shiny' ] }); cmp_ok($schema->resultset("Tag")->count({ tag => 'Blue' }), - '==', 9, 'Count without DISTINCT ok'); + '==', 4, 'Count without DISTINCT'); cmp_ok($schema->resultset("Tag")->count({ tag => [ 'Blue', 'Shiny' ] }, { group_by => 'tag' }), - '==', 2, 'Count with single column group_by ok'); + '==', 2, 'Count with single column group_by'); cmp_ok($schema->resultset("Tag")->count({ tag => 'Blue' }, { group_by => [ qw/tag cd/ ]}), - '==', 4, 'Count with multiple column group_by ok'); + '==', 4, 'Count with multiple column group_by'); cmp_ok($schema->resultset("Tag")->count({ tag => 'Blue' }, { distinct => 1 }), - '==', 4, "Count with single column distinct ok"); + '==', 4, "Count with single column distinct"); + +cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->get_column('tag')->as_query } }), + '==', 4, "Count with IN subquery"); + +cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { group_by => 'tag' }), + '==', 1, "Count with IN subquery with outside group_by"); + +cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1 }), + '==', 4, "Count with IN subquery with outside distinct"); + +cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->get_column('tag')->as_query } }, { distinct => 1, select => 'tag' }), + '==', 1, "Count with IN subquery with outside distinct on a single column"); + +cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->search({}, { group_by => 'tag' })->get_column('tag')->as_query } }), + '==', 4, "Count with IN subquery with single group_by"); + +cmp_ok($schema->resultset("Tag")->count({ tag => { -in => $in_rs->search({}, { group_by => [ qw/tag cd/ ] })->get_column('tag')->as_query } }), + '==', 4, "Count with IN subquery with multiple group_by"); + +cmp_ok($schema->resultset("Tag")->count({ tag => \"= 'Blue'" }), + '==', 4, "Count without DISTINCT, using literal SQL"); + +cmp_ok($schema->resultset("Tag")->count({ tag => \" IN ('Blue', 'Shiny')" }, { group_by => 'tag' }), + '==', 2, "Count with literal SQL and single group_by"); + +cmp_ok($schema->resultset("Tag")->count({ tag => \" IN ('Blue', 'Shiny')" }, { group_by => [ qw/tag cd/ ] }), + '==', 6, "Count with literal SQL and multiple group_by");