]> git.ipfire.org Git - thirdparty/bugzilla.git/commitdiff
Bug 552919: Sort group_concat results so that they sort correctly for buglists
authorMax Kanat-Alexander <mkanat@bugzilla.org>
Wed, 7 Jul 2010 00:13:46 +0000 (17:13 -0700)
committerMax Kanat-Alexander <mkanat@bugzilla.org>
Wed, 7 Jul 2010 00:13:46 +0000 (17:13 -0700)
r=mkanat, a=mkanat (module owner)

Bugzilla/DB/Mysql.pm
Bugzilla/DB/Oracle.pm
Bugzilla/DB/Pg.pm
Bugzilla/Install/DB.pm
Bugzilla/Search.pm

index 4b90a2a3442119d522a57b04d26a927c2e54a799..7f3eb2ef8eeede28529a17dc8b068df1b7a76901 100644 (file)
@@ -126,12 +126,15 @@ sub bz_last_key {
 }
 
 sub sql_group_concat {
-    my ($self, $column, $separator) = @_;
-    my $sep_sql;
-    if ($separator) {
-        $sep_sql = " SEPARATOR $separator";
+    my ($self, $column, $separator, $sort) = @_;
+    $separator = $self->quote(', ') if !defined $separator;
+    $sort = 1 if !defined $sort;
+    if ($sort) {
+        my $sort_order = $column;
+        $sort_order =~ s/^DISTINCT\s+//i;
+        $column = "$column ORDER BY $sort_order";
     }
-    return "GROUP_CONCAT($column$sep_sql)";
+    return "GROUP_CONCAT($column SEPARATOR $separator)";
 }
 
 sub sql_regexp {
index a671a0e68b5484983fc1eccebc36af193a952db2..0819bd19ada67ae08f09717f2643977ffe1de158 100644 (file)
@@ -119,7 +119,7 @@ sub bz_explain {
 
 sub sql_group_concat {
     my ($self, $text, $separator) = @_;
-    $separator ||= "','";
+    $separator = $self->quote(', ') if !defined $separator;
     return "group_concat(T_CLOB_DELIM($text, $separator))";
 }
 
index c85c1d710beca2e2ba1717e664f489888cec66e4..0189001299a41f921acd4a35bc065b0fb7db517c 100644 (file)
@@ -98,9 +98,14 @@ sub bz_last_key {
 }
 
 sub sql_group_concat {
-    my ($self, $text, $separator) = @_;
-    $separator ||= "','";
-    return "array_to_string(array_accum($text), $separator)";
+    my ($self, $text, $separator, $sort) = @_;
+    $sort = 1 if !defined $sort;
+    $separator = $self->quote(', ') if !defined $separator;
+    my $sql = "array_accum($text)";
+    if ($sort) {
+        $sql = "array_sort($sql)";
+    }
+    return "array_to_string($sql, $separator)";
 }
 
 sub sql_istring {
@@ -224,6 +229,20 @@ sub bz_setup_database {
                    )");
     }
 
+   $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
+
     # PostgreSQL doesn't like having *any* index on the thetext
     # field, because it can't have index data longer than 2770
     # characters on that field.
index ada500c586a50414e453be75824688657a6ab88e..d9a36b75b136e0254cb7b6cb3f00cc6f38abd76c 100644 (file)
@@ -3198,8 +3198,8 @@ sub _populate_bugs_fulltext {
             q{INSERT INTO bugs_fulltext (bug_id, short_desc, comments, 
                                          comments_noprivate)
                    SELECT bugs.bug_id, bugs.short_desc, }
-                 . $dbh->sql_group_concat('longdescs.thetext', $newline)
-          . ', ' . $dbh->sql_group_concat('nopriv.thetext',    $newline) .
+                 . $dbh->sql_group_concat('longdescs.thetext', $newline, 0)
+          . ', ' . $dbh->sql_group_concat('nopriv.thetext',    $newline, 0) .
                  qq{ FROM bugs 
                           LEFT JOIN longdescs
                                  ON bugs.bug_id = longdescs.bug_id
index 61f5e995ed93ba103e42b7ffc78af6a0a91fd9c8..ad8ab0edbdbbd2d1293821abe692e8b0aa2efea4 100644 (file)
@@ -308,10 +308,10 @@ sub COLUMNS {
                    . " * ($actual_time / ($actual_time + bugs.remaining_time))"
               . " END)",
 
-        'flagtypes.name' => $dbh->sql_group_concat('DISTINCT '
-                            . $dbh->sql_string_concat('flagtypes.name', 'flags.status'), "', '"),
+        'flagtypes.name' => $dbh->sql_group_concat('DISTINCT ' 
+            . $dbh->sql_string_concat('flagtypes.name', 'flags.status')),
 
-        'keywords' => $dbh->sql_group_concat('DISTINCT keyworddefs.name', "', '"),
+        'keywords' => $dbh->sql_group_concat('DISTINCT keyworddefs.name'),
     );
 
     # Backward-compatibility for old field names. Goes new_name => old_name.
@@ -354,8 +354,7 @@ sub COLUMNS {
         }
         elsif ($field->type == FIELD_TYPE_MULTI_SELECT) {
             $sql = $dbh->sql_group_concat(
-                'DISTINCT map_bug_' . $field->name . '.value',
-                $dbh->quote(', '));
+                'DISTINCT map_bug_' . $field->name . '.value');
         }
         else {
             $sql = 'bugs.' . $field->name;