Revision history for DBIx::Class
+ * Fixes
+ - Fix duplicated selected columns when calling 'count' when a same
+ aggregate function is used more than once in a 'having' clause
+ (RT#83305)
+
0.08206 2013-02-08
* Fixes
- Fix dbh_do() failing to properly reconnect (regression in 0.08205)
amoore: Andrew Moore <amoore@cpan.org>
+andrewalker: Andre Walker <andre@andrewalker.net>
+
andyg: Andy Grundman <andy@hybridized.org>
ank: Andres Kievsky
my ($lquote, $rquote, $sep) = map { quotemeta $_ } ($sql_maker->_quote_chars, $sql_maker->name_sep);
- my $sql = $sql_maker->_parse_rs_attrs ({ having => $attrs->{having} });
+ my $having_sql = $sql_maker->_parse_rs_attrs ({ having => $attrs->{having} });
+ my %seen_having;
# search for both a proper quoted qualified string, for a naive unquoted scalarref
# and if all fails for an utterly naive quoted scalar-with-function
- while ($sql =~ /
+ while ($having_sql =~ /
$rquote $sep $lquote (.+?) $rquote
|
[\s,] \w+ \. (\w+) [\s,]
|
[\s,] $lquote (.+?) $rquote [\s,]
/gx) {
- push @parts, ($1 || $2 || $3); # one of them matched if we got here
+ my $part = $1 || $2 || $3; # one of them matched if we got here
+ unless ($seen_having{$part}++) {
+ push @parts, $part;
+ }
}
}
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;