From 57379f2ea8b87f473823014e7557c09a88bf022d Mon Sep 17 00:00:00 2001 From: Dave MacFarlane Date: Mon, 13 Nov 2023 10:38:10 -0500 Subject: [PATCH] [instruments] Bulk load SessionID and VisitLabel (#8855) When calling bulkLoadInstanceData, currently only instrument data is bulk loaded from the database. However, calling getSessionID or getVisitLabel is almost always required in order to use the data. This bulk loads the session id and visit label from the same query when calling bulkLoadInstanceData. --- php/libraries/NDB_BVL_Instrument.class.inc | 61 +++++++++++++++------- 1 file changed, 43 insertions(+), 18 deletions(-) diff --git a/php/libraries/NDB_BVL_Instrument.class.inc b/php/libraries/NDB_BVL_Instrument.class.inc index 409a7a40c89..a963eaabec0 100644 --- a/php/libraries/NDB_BVL_Instrument.class.inc +++ b/php/libraries/NDB_BVL_Instrument.class.inc @@ -1409,6 +1409,7 @@ abstract class NDB_BVL_Instrument extends NDB_Page return $allValues[$fieldName]; } + private ?SessionID $sessionID; /** * Gets the sessionID for the timepoint to which this instrument pertains * @@ -1416,22 +1417,23 @@ abstract class NDB_BVL_Instrument extends NDB_Page */ function getSessionID(): ?SessionID { - - $query = "SELECT SessionID FROM flag WHERE CommentID = :CID"; - $result = \NDB_Factory::singleton()->database()->pselectOne( - $query, - [ - 'CID' => $this->getCommentID(), - ] - ); - - if (empty($result)) { - return null; + if (empty($this->sessionID)) { + $query = "SELECT SessionID FROM flag WHERE CommentID = :CID"; + $result = \NDB_Factory::singleton()->database()->pselectOne( + $query, + [ + 'CID' => $this->getCommentID(), + ] + ); + if (empty($result)) { + return null; + } + $this->sessionID = new SessionID($result); } - - return (new SessionID($result)); + return $this->sessionID; } + private ?string $visitLabel = null; /** * Gets the VisitLabel for the timepoint for this instrument. * @@ -1439,7 +1441,12 @@ abstract class NDB_BVL_Instrument extends NDB_Page */ function getVisitLabel(): string { - return \TimePoint::singleton($this->getSessionID())->getVisitLabel(); + if ($this->visitLabel === null) { + $this->visitLabel = \TimePoint::singleton( + $this->getSessionID() + )->getVisitLabel(); + } + return $this->visitLabel; } /** @@ -2211,7 +2218,13 @@ abstract class NDB_BVL_Instrument extends NDB_Page if ($this->jsonData) { $jsondata = $db->pselect( - "SELECT CommentID, Data FROM flag WHERE CommentID IN (" + "SELECT SessionID, + CommentID, + session.Visit_Label as VisitLabel, + Data + FROM flag + JOIN session ON (session.ID=flag.SessionID) + WHERE CommentID IN (" . join(',', $prepBindings) . ')', $prepValues, ); @@ -2220,8 +2233,10 @@ abstract class NDB_BVL_Instrument extends NDB_Page $newinst = clone $this; $newinst->commentID = $row['CommentID']; + $newinst->visitLabel = $row['VisitLabel']; + $newinst->sessionID = new SessionID($row['SessionID']); $newinst->instanceData = json_decode( - $row['Data'], + $row['Data'] ?? '{}', true, ) ?? $this->defaultInstanceData(); @@ -2231,7 +2246,13 @@ abstract class NDB_BVL_Instrument extends NDB_Page ); } else { $defaults = $db->pselect( - "SELECT * FROM $this->table WHERE CommentID IN (" + "SELECT flag.CommentID as CommentID, + session.Visit_Label as VisitLabel, + session.ID as SessionID, t.* + FROM $this->table t + JOIN flag ON (t.CommentID=flag.CommentID) + JOIN session ON (flag.SessionID=session.ID) + WHERE t.CommentID IN (" . join(',', $prepBindings) . ')', $prepValues, ); @@ -2239,7 +2260,11 @@ abstract class NDB_BVL_Instrument extends NDB_Page function ($row) { $newinst = clone $this; - $newinst->commentID = $row['CommentID']; + $newinst->commentID = $row['CommentID']; + $newinst->visitLabel = $row['VisitLabel']; + $newinst->sessionID = new SessionID($row['SessionID']); + unset($row['CommentID'], $row['VisitLabel'], $row['SessionID']); + $newinst->instanceData = $row; return $newinst;