=cut
-my @count_via_subq_attrs = qw/join seen_join prefetch group_by having/;
sub count {
my $self = shift;
return $self->search(@_)->count if @_ and defined $_[0];
return scalar @{ $self->get_cache } if $self->get_cache;
- my @check_attrs = @count_via_subq_attrs;
+ my @subq_attrs = qw/prefetch collapse group_by having/;
# if we are not paged - we are simply asking for a limit
if (not $self->{attrs}{page} and not $self->{attrs}{software_limit}) {
- push @check_attrs, qw/rows offset/;
+ push @subq_attrs, qw/rows offset/;
}
- return $self->_has_attr (@check_attrs)
+ return $self->_has_attr (@subq_attrs)
? $self->_count_subq
: $self->_count_simple
}
}];
# the subquery replaces this
- delete $attrs->{where};
+ delete $attrs->{$_} for qw/where bind prefetch collapse group_by having/;
return $self->__count ($attrs);
}
$attrs ||= { %{$self->{attrs}} };
- # these are the only attributes that actually matter for count
- $attrs = { map { exists $attrs->{$_} ? ( $_ => $attrs->{$_} ) : () } qw/where bind alias from from_bind/ };
+ # take off any column specs, any pagers, record_filter is cdbi, and no point of ordering a count
+ delete $attrs->{$_} for (qw/columns +columns select +select as +as rows offset page pager order_by record_filter/);
$attrs->{select} = { count => '*' };
$attrs->{as} = [qw/count/];
my $join_check_req;
for my $n (@attr_names) {
- return 1 if defined $attrs->{$n};
++$join_check_req if $n =~ /join/;
+
+ my $attr = $attrs->{$n};
+
+ next if not defined $attr;
+
+ if (ref $attr eq 'HASH') {
+ return 1 if keys %$attr;
+ }
+ elsif (ref $attr eq 'ARRAY') {
+ return 1 if @$attr;
+ }
+ else {
+ return 1 if $attr;
+ }
}
# a join can be expressed as a multi-level from
{ join => 'artist' });
eval { $rs->count };
is_same_sql_bind(
- $sql,
- \@bind,
- "SELECT COUNT( * ) FROM (SELECT `me`.`cdid` FROM `cd` `me` JOIN `artist` `artist` ON `artist`.`artistid` = `me`.`artist` WHERE ( ( `artist`.`name` = ? AND `me`.`year` = ? ) ) GROUP BY `me`.`cdid`) `count_subq`",
- ["'Caterwauler McCrae'", "'2001'"],
-
- 'got correct SQL for joined count query with quoting'
+ $sql, \@bind,
+ "SELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
+ 'got correct SQL for count query with quoting'
);
my $order = 'year DESC';
{ join => 'artist' });
eval { $rs->count };
is_same_sql_bind(
- $sql,
- \@bind,
- "SELECT COUNT( * ) FROM (SELECT [me].[cdid] FROM [cd] [me] JOIN [artist] [artist] ON [artist].[artistid] = [me].[artist] WHERE ( ( [artist].[name] = ? AND [me].[year] = ? ) ) GROUP BY [me].[cdid]) [count_subq]",
- ["'Caterwauler McCrae'", "'2001'"],
+ $sql, \@bind,
+ "SELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
'got correct SQL for count query with bracket quoting'
);
{ join => 'artist' });
eval { $rs->count };
is_same_sql_bind(
- $sql,
- \@bind,
- "SELECT COUNT( * ) FROM (SELECT `me`.`cdid` FROM `cd` `me` JOIN `artist` `artist` ON `artist`.`artistid` = `me`.`artist` WHERE ( ( `artist`.`name` = ? AND `me`.`year` = ? ) ) GROUP BY `me`.`cdid`) `count_subq`",
- ["'Caterwauler McCrae'", "'2001'"],
+ $sql, \@bind,
+ "SELECT COUNT( * ) FROM `cd` `me` JOIN `artist` `artist` ON ( `artist`.`artistid` = `me`.`artist` ) WHERE ( `artist`.`name` = ? AND `me`.`year` = ? )", ["'Caterwauler McCrae'", "'2001'"],
'got correct SQL for count query with quoting'
);
{ join => 'artist' });
eval { $rs->count };
is_same_sql_bind(
- $sql,
- \@bind,
- "SELECT COUNT( * ) FROM (SELECT [me].[cdid] FROM [cd] [me] JOIN [artist] [artist] ON [artist].[artistid] = [me].[artist] WHERE ( ( [artist].[name] = ? AND [me].[year] = ? ) ) GROUP BY [me].[cdid]) [count_subq]",
- ["'Caterwauler McCrae'", "'2001'"],
+ $sql, \@bind,
+ "SELECT COUNT( * ) FROM [cd] [me] JOIN [artist] [artist] ON ( [artist].[artistid] = [me].[artist] ) WHERE ( [artist].[name] = ? AND [me].[year] = ? )", ["'Caterwauler McCrae'", "'2001'"],
'got correct SQL for count query with bracket quoting'
);
my $schema = DBICTest->init_schema();
-plan tests => 96;
+plan tests => 98;
eval { require DateTime::Format::MySQL };
my $NO_DTFM = $@ ? 1 : 0;
my( $or_rs ) = $schema->resultset("CD")->search_rs($search, { join => 'tags',
order_by => 'cdid' });
-# At this point in the test there are:
-# 1 artist with the cheesy AND blue tag
-# 1 artist with the cheesy tag
-# 2 artists with the blue tag
-#
-# Formerly this test expected 5 as there was no collapsing of the AND condition
-is($or_rs->count, 4, 'Search with OR ok');
+is($or_rs->all, 5, 'Joined search with OR returned correct number of rows');
+is($or_rs->count, 5, 'Search count with OR ok');
-my $distinct_rs = $schema->resultset("CD")->search($search, { join => 'tags', distinct => 1 });
-is($distinct_rs->all, 4, 'DISTINCT search with OR ok');
+my $collapsed_or_rs = $or_rs->search ({}, { distinct => 1 }); # induce collapse
+is ($collapsed_or_rs->all, 4, 'Collapsed joined search with OR returned correct number of rows');
+is ($collapsed_or_rs->count, 4, 'Collapsed search count with OR ok');
{
my $tcount = $schema->resultset('Track')->search(
my $rel_rs = $tag_rs->search_related('cd');
-# At this point in the test there are:
-# 1 artist with the cheesy AND blue tag
-# 1 artist with the cheesy tag
-# 2 artists with the blue tag
-#
-# Formerly this test expected 5 as there was no collapsing of the AND condition
-is($rel_rs->count, 4, 'Related search ok');
+is($rel_rs->count, 5, 'Related search ok');
is($or_rs->next->cdid, $rel_rs->next->cdid, 'Related object ok');
$or_rs->reset;
my $rs3 = $rs2->search_related('cds');
-# $rs3 selects * from cds_2, with the following join map
-#
-# artist -> cds_2
-# |
-# V
-# cds -> cd_to_producer -> producer
-# |
-# |\--> tags
-# V
-# tracks
-#
-# For some reason it is expected to return an exploded set of rows instead of the
-# logical 3, even for a rowobject retrieval - why?
-#
cmp_ok(scalar($rs3->all), '==', 45, "All cds for artist returned");
-cmp_ok($rs3->count, '==', 3, "All cds for artist returned via count");
+cmp_ok($rs3->count, '==', 45, "All cds for artist returned via count");
my $rs4 = $schema->resultset("CD")->search({ 'artist.artistid' => '1' }, { join => ['tracks', 'artist'], prefetch => 'artist' });
my @rs4_results = $rs4->all;
use DBICTest;
-plan tests => 1;
+plan tests => 3;
my $schema = DBICTest->init_schema();
-{
- my $cds = $schema->resultset("CD")->search({ cdid => 1 }, { join => { cd_to_producer => 'producer' } });
- is($cds->count, 1, "extra joins do not explode single entity count");
-}
+my $cds = $schema->resultset("CD")->search({ cdid => 1 }, { join => { cd_to_producer => 'producer' } });
+cmp_ok($cds->count, '>', 1, "extra joins explode entity count");
+
+is (
+ $cds->search({}, { prefetch => 'cd_to_producer' })->count,
+ 1,
+ "Count correct with extra joins collapsed by prefetch"
+);
+
+is (
+ $cds->search({}, { distinct => 1 })->count,
+ 1,
+ "Count correct with requested distinct collapse of main table"
+);
+
+
+
+