Commit 665c59e3 authored by Simon Green's avatar Simon Green Committed by Frédéric Buclin

Bug 1138417: sql_group_concat() generates bad SQL code with PostgreSQL 8.x

r=LpSolit a=glob
parent fe7e0d01
......@@ -85,6 +85,15 @@ sub sql_group_concat {
$sort = 1 if !defined $sort;
$separator = $self->quote(', ') if !defined $separator;
# PostgreSQL 8.x doesn't support STRING_AGG
if (vers_cmp($self->bz_server_version, 9) < 0) {
my $sql = "ARRAY_ACCUM($text)";
if ($sort) {
$sql = "ARRAY_SORT($sql)";
}
return "ARRAY_TO_STRING($sql, $separator)";
}
if ($order_by && $text =~ /^DISTINCT\s*(.+)$/i) {
# Since Postgres (quite rightly) doesn't support "SELECT DISTINCT x
# ORDER BY y", we need to sort the list, and then get the unique
......@@ -102,11 +111,6 @@ sub sql_group_concat {
$order_by = " ORDER BY $1";
}
if (vers_cmp($self->bz_server_version, 9) < 0) {
# PostgreSQL 8.x doesn't support STRING_AGG
return "ARRAY_TO_STRING(ARRAY_AGG($text$order_by), $separator)";
}
return "STRING_AGG(${text}::text, $separator${order_by}::text)"
}
......@@ -234,7 +238,37 @@ sub bz_setup_database {
my ($has_plpgsql) = $self->selectrow_array("SELECT COUNT(*) FROM pg_language WHERE lanname = 'plpgsql'");
$self->do('CREATE LANGUAGE plpgsql') unless $has_plpgsql;
# Custom Functions
if (vers_cmp($self->bz_server_version, 9) < 0) {
# Custom Functions for Postgres 8
my $function = 'array_accum';
my $array_accum = $self->selectrow_array(
'SELECT 1 FROM pg_proc WHERE proname = ?', undef, $function);
if (!$array_accum) {
print "Creating function $function...\n";
$self->do("CREATE AGGREGATE array_accum (
SFUNC = array_append,
BASETYPE = anyelement,
STYPE = anyarray,
INITCOND = '{}'
)");
}
$self->do(<<'END');
CREATE OR REPLACE FUNCTION array_sort(ANYARRAY)
RETURNS ANYARRAY LANGUAGE SQL
IMMUTABLE STRICT
AS $$
SELECT ARRAY(
SELECT $1[s.i] AS each_item
FROM
generate_series(array_lower($1,1), array_upper($1,1)) AS s(i)
ORDER BY each_item
);
$$;
END
}
else {
# Custom functions for Postgres 9.0+
# -Copyright © 2013 Joshua D. Burns (JDBurnZ) and Message In Action LLC
# JDBurnZ: https://github.com/JDBurnZ
......@@ -282,6 +316,7 @@ sub bz_setup_database {
END;
$BODY$ LANGUAGE plpgsql;
|);
}
# PostgreSQL doesn't like having *any* index on the thetext
# field, because it can't have index data longer than 2770
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment