Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions SQL/0000-00-06-BiobankTables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ CREATE TABLE `biobank_specimen_protocol_attribute_rel` (
`SpecimenAttributeID` int(10) unsigned NOT NULL,
`Required` tinyint(1) DEFAULT NULL,
`showInDataTable` tinyint(1) DEFAULT NULL,
`OrderIndex` INT UNSIGNED NOT NULL,
PRIMARY KEY (`SpecimenProtocolID`,`SpecimenAttributeID`),
UNIQUE KEY `UK_SpecimenProtocolId_OrderIndex` (`SpecimenProtocolID`, `OrderIndex`),
KEY `FK_biobank_specimen_protocol_attribute_rel_SpecimenAttributeID` (`SpecimenAttributeID`),
CONSTRAINT `FK_biobank_specimen_protocol_attribute__rel_SpecimenProtocolID` FOREIGN KEY (`SpecimenProtocolID`) REFERENCES `biobank_specimen_protocol` (`SpecimenProtocolID`),
CONSTRAINT `FK_biobank_specimen_protocol_attribute_rel_SpecimenAttributeID` FOREIGN KEY (`SpecimenAttributeID`) REFERENCES `biobank_specimen_attribute` (`SpecimenAttributeID`)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- Step 1: Add the column allowing NULL (temporarily)
-- This allows the operation to succeed on a table that already has existing rows.
ALTER TABLE biobank_specimen_protocol_attribute_rel
ADD COLUMN OrderIndex INT UNSIGNED NULL;

-- Step 2: Populate the existing rows with unique, non-NULL OrderIndex values.
-- This uses a variable-based method to assign a sequential number (0, 1, 2, ...)
-- to each row within the same SpecimenProtocolID group.

SET @r := -1;
SET @g := 0;

-- A temporary table/derived table is used to calculate the unique index for each group
UPDATE biobank_specimen_protocol_attribute_rel AS t1
INNER JOIN (
SELECT
t2.SpecimenProtocolID,
t2.SpecimenAttributeID,
@r := CASE WHEN @g = t2.SpecimenProtocolID THEN @r + 1 ELSE 0 END AS new_OrderIndex,
@g := t2.SpecimenProtocolID AS current_group
FROM
biobank_specimen_protocol_attribute_rel AS t2
ORDER BY
t2.SpecimenProtocolID, t2.SpecimenAttributeID

) AS ranked_data
ON t1.SpecimenProtocolID = ranked_data.SpecimenProtocolID
AND t1.SpecimenAttributeID = ranked_data.SpecimenAttributeID
SET t1.OrderIndex = ranked_data.new_OrderIndex;

-- Step 3: Enforce the constraints (NOT NULL and UNIQUE).
-- Now that every row has a valid, unique value, these operations will succeed.

-- 3a. Add the UNIQUE Constraint
ALTER TABLE biobank_specimen_protocol_attribute_rel
ADD CONSTRAINT UK_SpecimenProtocolId_OrderIndex
UNIQUE (SpecimenProtocolID, OrderIndex);

-- 3b. Change the Column to NOT NULL
ALTER TABLE biobank_specimen_protocol_attribute_rel
MODIFY COLUMN OrderIndex INT UNSIGNED NOT NULL;
39 changes: 26 additions & 13 deletions modules/biobank/php/optionsendpoint.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,25 @@ class OptionsEndpoint extends \NDB_Page implements RequestHandlerInterface
// TODO: This should eventually be replaced by a call directly to a
// Candidate endpoint or Candidate controller that will be able to
// provide Candidate Objects.
$query = "SELECT c.CandID as id,
PSCID as pscid,
Sex as sex,
GROUP_CONCAT(DISTINCT DiagnosisID) as diagnosisIds,
GROUP_CONCAT(DISTINCT s.ID) as sessionIds
FROM candidate c
LEFT JOIN session s USING (CandID)
LEFT JOIN candidate_diagnosis_rel USING (CandID)
WHERE s.CenterID IN ($userCenters)
AND s.ProjectID IN ($userProjects)
GROUP BY
CandID";
$query = "
SELECT
c.CandID as id,
PSCID as pscid,
Sex as sex,
GROUP_CONCAT(DISTINCT DxEvolutionID) as diagnosisIds,
GROUP_CONCAT(DISTINCT s.ID) as sessionIds
FROM
candidate c
LEFT JOIN session s
ON s.CandidateID=c.CandID
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think thats correct.

@jeffersoncasimir correct me if I'm wrong but this should be

Suggested change
ON s.CandidateID=c.CandID
ON s.CandidateID=c.ID

While row 143 stays as is!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, I changed any join to use c.ID instead of c.CandID.

LEFT JOIN candidate_diagnosis_evolution_rel cedr
ON cedr.CandidateID=c.CandID
WHERE
s.CenterID IN ($userCenters)
AND s.ProjectID IN ($userProjects)
GROUP BY
CandID
";
$candidates = $db->pselectWithIndexKey($query, [], 'id');
foreach ($candidates as $id => $candidate) {
$candidates[$id]['diagnosisIds'] = $candidate['diagnosisIds']
Expand All @@ -163,7 +170,13 @@ class OptionsEndpoint extends \NDB_Page implements RequestHandlerInterface
// XXX: This should eventually be replaced by a call directly to a
// Candidate endpoint or Candidate controller that will be able to
// provide Diagnosis Options.
$query = 'SELECT DiagnosisID as id, Name as label FROM diagnosis';
$query = '
SELECT
DxEvolutionID as id,
Name as label
FROM
diagnosis_evolution
';
$diagnoses = $db->pselectWithIndexKey($query, [], 'id');

$sessionQuery = "SELECT
Expand Down
6 changes: 3 additions & 3 deletions modules/biobank/php/specimendao.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class SpecimenDAO extends \LORIS\Data\ProvisionerInstance
as ParentSpecimenIDs,
GROUP_CONCAT(DISTINCT bc2.Barcode)
as ParentSpecimenBarcodes,
s.CandID as CandidateID,
s.CandidateID,
c.PSCID as CandidatePSCID,
bs.SessionID,
s.CenterID as SessionCenterID,
Expand Down Expand Up @@ -154,7 +154,7 @@ class SpecimenDAO extends \LORIS\Data\ProvisionerInstance
LEFT JOIN session s
ON bs.SessionID=s.ID
LEFT JOIN candidate c
ON s.CandID=c.CandID
ON s.CandidateID=c.CandID
LEFT JOIN biobank_specimen_pool_rel bspor
ON bs.SpecimenID=bspor.SpecimenID
LEFT JOIN biobank_specimen_collection bsc
Expand Down Expand Up @@ -561,7 +561,7 @@ class SpecimenDAO extends \LORIS\Data\ProvisionerInstance
SELECT IFNULL(MAX(bs.SampleNumber), 0) AS max_sample_number
FROM biobank_specimen bs
JOIN session s ON bs.SessionID = s.ID
WHERE s.CandID = :candId
WHERE s.CandidateID = :candId
";

$sampleNumber = $this->db->pselectOneInt($query, ['candId' => $candId]);
Expand Down
Loading