Skip to content

Commit

Permalink
[instruments] Bulk load SessionID and VisitLabel (#8855)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
driusan authored Nov 13, 2023
1 parent 64ffdf4 commit 57379f2
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions php/libraries/NDB_BVL_Instrument.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -1409,37 +1409,44 @@ 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
*
* @return ?SessionID The sessionID
*/
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.
*
* @return string The visit label
*/
function getVisitLabel(): string
{
return \TimePoint::singleton($this->getSessionID())->getVisitLabel();
if ($this->visitLabel === null) {
$this->visitLabel = \TimePoint::singleton(
$this->getSessionID()
)->getVisitLabel();
}
return $this->visitLabel;
}

/**
Expand Down Expand Up @@ -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,
);
Expand All @@ -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();

Expand All @@ -2231,15 +2246,25 @@ 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,
);
return array_map(
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;
Expand Down

0 comments on commit 57379f2

Please sign in to comment.