From dc3e2b1d751cd04e07d77028b37d2c1954dc6804 Mon Sep 17 00:00:00 2001
From: Catherine Pitt <cen1001@cam.ac.uk>
Date: Wed, 2 Aug 2023 09:07:08 +0100
Subject: [PATCH] Remove obsolete method for handling many-many rows

We used to handle representing many-many relationships in a view by
returning multiple rows with the same value in the 'id' column, each row
having column named mm_(something) containing one of the foreign key
values at the other end of the many-many relationship. The code would
look for columns named 'mm_' and group their values for rows with the
same 'id'. That scaled very poorly for views with multiple many-many
relationships and we long ago moved to a model where the view returns
one row for each id and a column named mm_(something) containing an
array of foreign for each many-many relationship. Because we to support
both for a time the code would check for 'collatable columns' by doing a
regexp match on the view definition and for any it found, checked if
they were an array or not.

Unfortunately the regexp match sometimes returns the wrong thing, eg
when the view definition selects underlying columns named mm_(something)
and renames them, which we sometimes want to do. This change removes the
old method of handling many-many relationships, thus removing the need
for a regexp matching process to detect such columns. This should be
fine for Chemistry's database but I don't know about Maths'.
---
 classes/dbView.php | 64 ++++------------------------------------------
 1 file changed, 5 insertions(+), 59 deletions(-)

diff --git a/classes/dbView.php b/classes/dbView.php
index dc1917a..5f5f8fb 100644
--- a/classes/dbView.php
+++ b/classes/dbView.php
@@ -769,77 +769,23 @@ $this->navbarBookmark().
   }
 
  /**
-  * @brief Collates some table rows
-  * @todo With the new MM routines, this should no longer be required
-  * @todo Check and kill
-  * @date 2011-07-11
-  * @version 11
+  * This used to collate table rows when we represented many-many relations
+  * by returning multiple rows with the same 'id' column value. We now provide
+  * many-many values as array columns, so the collation functionality isn't
+  * here any more, but the code still requires this function's rearrangement
+  * of the data in $result
   */
   protected function collateRows($result) {
 
     $data = array();
-    $columns = $this->getCollatableColumns($this->view);
     while ($row = pg_fetch_assoc($result)) {
       foreach (array_keys($row) as $col) {
-        if (in_array($col, $columns)) {
-          if (!(array_key_exists($col,$data[$row['id']]))
-           || is_null($data[$row['id']][$col]) ) {
-            $data[$row['id']][$col] = '';
-          }
-          # RFL added is_array test here
-          if (! is_null($data[$row['id']][$col]) && 
-             (! is_array($data[$row['id']][$col]) ||
-             !in_array($row[$col], $data[$row['id']][$col]))) {
-            $data[$row['id']][$col] = [];
-            $data[$row['id']][$col][] = $row[$col];
-          }
-        } else {
           $data[$row['id']][$col] = $row[$col];
-        }
       }
     }
-/*
-        foreach (array_keys($mm_ids) as $col) {
-          $this->data[$col] = implode(',', $mm_ids[$col]);
-        }
-*/
     return $data;
   }
 
- /**
-  * @brief Returns a list of columns which can be collated.
-  * Needs to die -and will do with the new MM code.
-  * @todo: Is this called? Needs to die
-  * @param[in] view Name of view we're considering
-  * @date 2011-07-11
-  * @version 11
-  */
-  protected function getCollatableColumns($view) {
-    $columns = array();
-    $sql = "SELECT definition FROM ".$this->hwschema."._view_data WHERE view = '$view'";
-    $dbResult = pg_query($this->dbh, $sql);
-    if (!$dbResult) {$this->sqlError('examining definition of view',$sql);}
-    if ($def = pg_fetch_result($dbResult, 'definition')) {
-      if ($def = preg_replace('/^SELECT\s+(.*)\s+FROM.*$/', '$1', $def)) {
-        $columns = array_values(preg_grep('/mm_/', explode(',', $def)));
-        $columns = preg_replace('/.*AS\s+(\w+)/', '$1', $columns);
-        $columns = preg_replace('/.*\.(\w+)/', '$1', $columns);
-      }
-    }
-    # Check to see whether any of these are flagged as arrays
-    # in which case we shouldn't collate them.
-    # This code will eventually bog off
-    $newcols=array();
-    foreach ($columns as $col) {
-     if ((isset($this->colAttrs[$col]['array'])) && 
-        ($this->colAttrs[$col]['array'])) {
-     } else {
-      array_push($newcols,$col);
-     }
-    }
-    return $newcols;
-  }
-
   /**
    * @brief Produces the HTML form fields required for this search 
    * @return HTML representation of the search variables
-- 
GitLab