     MODIFIES SQL DATA
     COMMENT 'Inserts into or updates a row in the image quality indicators table'
 BEGIN
-
-  IF p_id IS NOT NULL OR (p_id IS NULL AND p_dataCollectionId IS NOT NULL AND p_imageNumber IS NOT NULL) THEN
-    INSERT INTO ImageQualityIndicators (
-      imageQualityIndicatorsId, dataCollectionId, autoProcProgramId, imageNumber, spotTotal, inResTotal, goodBraggCandidates, iceRings, 
-	  method1Res, method2Res, maxUnitCell, pctSaturationTop50Peaks,
-	  inResolutionOvrlSpots, binPopCutOffMethod2Res, totalIntegratedSignal, dozor_score, driftFactor) 
-      VALUES (
-        p_id, p_dataCollectionId, p_autoProcProgramId, p_imageNumber, p_spotTotal, p_inResTotal, p_goodBraggCandidates, p_iceRings,
-        p_method1Res, p_method2Res, p_maxUnitCell, p_pctSaturationTop50Peaks, 
-        p_inResolutionOvrlSpots, p_binPopCutOffMethod2Res, p_totalIntegratedSignal, p_dozorScore, p_driftFactor
-      )
-      ON DUPLICATE KEY UPDATE
-        dataCollectionId = IFNULL(p_dataCollectionId, dataCollectionId),
-        autoProcProgramId = IFNULL(p_autoProcProgramId, autoProcProgramId),
-        imageNumber = IFNULL(p_imageNumber, imageNumber),
-        spotTotal = IFNULL(p_spotTotal, spotTotal),
-        inResTotal = IFNULL(p_inResTotal, inResTotal),
-        goodBraggCandidates = IFNULL(p_goodBraggCandidates, goodBraggCandidates),
-        iceRings = IFNULL(p_iceRings, iceRings),
-        method1Res = IFNULL(p_method1Res, method1Res),
-        method2Res = IFNULL(p_method2Res, method2Res),
-        maxUnitCell = IFNULL(p_maxUnitCell, maxUnitCell),
-        pctSaturationTop50Peaks = IFNULL(p_pctSaturationTop50Peaks, pctSaturationTop50Peaks),
-        inResolutionOvrlSpots = IFNULL(p_inResolutionOvrlSpots, inResolutionOvrlSpots),
-        binPopCutOffMethod2Res = IFNULL(p_binPopCutOffMethod2Res, binPopCutOffMethod2Res),
-        totalIntegratedSignal = IFNULL(p_totalIntegratedSignal, totalIntegratedSignal),
-        dozor_score = IFNULL(p_dozorScore, dozor_score),
-        driftFactor = IFNULL(p_driftFactor, driftFactor);
-      
-    IF p_id IS NULL THEN 
-      SET p_id = LAST_INSERT_ID();
-    END IF;      
+  DECLARE iqiId int(11) unsigned DEFAULT NULL;
+  IF (p_dataCollectionId IS NOT NULL AND p_imageNumber IS NOT NULL) THEN
+    SELECT MAX(imageQualityIndicatorsId) INTO iqiId FROM ImageQualityIndicators WHERE dataCollectionId = p_dataCollectionId AND imageNumber = p_imageNumber;
+    IF iqiId IS NULL THEN
+        INSERT INTO ImageQualityIndicators (
+          dataCollectionId, imageNumber, spotTotal, goodBraggCandidates,  
+	      method1Res, method2Res, totalIntegratedSignal, dozor_score, driftFactor) 
+        VALUES (
+          p_dataCollectionId, p_imageNumber, p_spotTotal, p_goodBraggCandidates, 
+          p_method1Res, p_method2Res, p_totalIntegratedSignal, p_dozorScore, p_driftFactor
+        );
+        SET p_id = LAST_INSERT_ID();
+    ELSE
+        -- Not setting dataCollectionId and imageNumber as they are sort of the "primary keys" here
+        -- and have already been used for looking up the row:
+        UPDATE ImageQualityIndicators 
+        SET
+          spotTotal = IFNULL(p_spotTotal, spotTotal),
+          goodBraggCandidates = IFNULL(p_goodBraggCandidates, goodBraggCandidates),
+          method1Res = IFNULL(p_method1Res, method1Res),
+          method2Res = IFNULL(p_method2Res, method2Res),
+          totalIntegratedSignal = IFNULL(p_totalIntegratedSignal, totalIntegratedSignal),
+          dozor_score = IFNULL(p_dozorScore, dozor_score),
+          driftFactor = IFNULL(p_driftFactor, driftFactor)
+		WHERE imageQualityIndicatorsId = iqiId;
+        SET p_id = iqiId;
+    END IF;
   ELSE
-        SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO=1644, MESSAGE_TEXT='Mandatory argument(s) p_id or (p_dataCollectionId and p_imageNumber) are NULL';  
+	SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO=1644, MESSAGE_TEXT='Mandatory arguments p_dataCollectionId and/or p_imageNumber are NULL';  
   END IF;
 END ;;
 DELIMITER ;

/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2017-11-08 16:34:12

      LIMIT 1;
    ELSE
        SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO=1644, MESSAGE_TEXT='Mandatory argument p_barcode is NULL';
    END IF;
END ;;
DELIMITER ;
/*!50003 SET sql_mode              = @saved_sql_mode */ ;
/*!50003 SET character_set_client  = @saved_cs_client */ ;
/*!50003 SET character_set_results = @saved_cs_results */ ;
/*!50003 SET collation_connection  = @saved_col_connection */ ;
/*!50003 DROP PROCEDURE IF EXISTS `retrieve_container_info_for_id` */;
/*!50003 SET @saved_cs_client      = @@character_set_client */ ;
/*!50003 SET @saved_cs_results     = @@character_set_results */ ;
/*!50003 SET @saved_col_connection = @@collation_connection */ ;
/*!50003 SET character_set_client  = utf8 */ ;
/*!50003 SET character_set_results = utf8 */ ;
/*!50003 SET collation_connection  = utf8_general_ci */ ;
/*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
/*!50003 SET sql_mode              = '' */ ;
DELIMITER ;;
CREATE PROCEDURE `retrieve_container_info_for_id`(IN p_containerId int unsigned)
    READS SQL DATA
    COMMENT 'Return single-row result set with info about a Container identified by p_containerId'
BEGIN
    IF NOT (p_containerId IS NULL) THEN
	    SELECT c.dewarId "dewarId", c.code "name", c.barcode "barcode", c.containerStatus "status", c.containerType "type", c.capacity "capacity",
	      c.sampleChangerLocation "location", c.beamlineLocation "beamline", c.comments "comments", c.experimentType "experimentType",
          p.proposalCode "proposalCode", p.proposalNumber "proposalNumber", bs.visit_number "sessionNumber",
          i.name "imagerName", i.serial "imagerSerialNumber", i.temperature "storageTemperature"
      FROM Container c
          LEFT OUTER JOIN Imager i on c.imagerId = i.imagerId
          LEFT OUTER JOIN BLSession bs on bs.sessionId = c.sessionId 
          LEFT OUTER JOIN Proposal p on p.proposalId = bs.proposalId
        WHERE c.containerId = p_containerId;
     ELSE
        SIGNAL SQLSTATE '45000' SET MYSQL_ERRNO=1644, MESSAGE_TEXT='Mandatory argument is NULL: p_containerId';
    END IF;
END ;;
DELIMITER ;
