I got an issue, when doing count() in both SQLite and Oracle database on
a rather complex query. The part where it fails is when my select looks
something like this:
SELECT COUNT( * ) FROM (
SELECT
me.colA,
MAX(CASE kv.attr_name WHEN 'FOO' THEN upper(kv.attr_value) ELSE
'' END) AS
MAX(CASE kv__attr_name WHEN 'FOO' THEN upper(kv.attr_value)
ELSE '' END)
FROM table me LEFT JOIN kv kv ON kv.foo_id = me.foo_id WHERE ...
)
So the issue is the "string" after "AS". The diff below fixes the problem.
diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm
index 02bceb7..c684a64 100644
--- a/lib/DBIx/Class/ResultSet.pm
+++ b/lib/DBIx/Class/ResultSet.pm
@@ -1525,6 +1525,7 @@ sub _count_subq_rs {
if ($colpiece =~ /\./ && $colpiece !~ /^$attrs->{alias}\./) {
my $as = $colpiece;
$as =~ s/\./__/;
+ $as =~ s/\W/_/g;
$colpiece = \ sprintf ('%s AS %s', map { $sql_maker->_quote
($_) } ($colpiece, $as) );
}
push @{$sub_attrs->{select}}, $colpiece;