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<count> with C<group_by>, L<DBIx::Class> emulates C<GROUP BY>
-using C<COUNT( DISTINCT( columns ) )>. Some databases (notably SQLite) do
-not support C<DISTINCT> with multiple columns. If you are using such a
-database, you should only use columns from the main table in your C<group_by>
-clause.
-
=cut
sub 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 } ];
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");