Browse Source

If given a new or newly modified file, update the Images database row

Fixes #357 and is verified by ctkDICOMDatabaseTest5.

When the user imports a file that is already in the database, delete
the old row for that image and let it be re-imported so that the
database will always point to the most current location of the file
(or at least the most recently imported).
Steve Pieper 11 years ago
parent
commit
77b27a2e23
1 changed files with 14 additions and 11 deletions
  1. 14 11
      Libs/DICOM/Core/ctkDICOMDatabase.cpp

+ 14 - 11
Libs/DICOM/Core/ctkDICOMDatabase.cpp

@@ -1123,18 +1123,12 @@ void ctkDICOMDatabasePrivate::insert( const ctkDICOMItem& ctkDataset, const QStr
       logger.error("SQLITE ERROR: " + fileExistsQuery.lastError().driverText());
       logger.error("SQLITE ERROR: " + fileExistsQuery.lastError().driverText());
       return;
       return;
     }
     }
+  fileExistsQuery.next();
 
 
   QString databaseFilename(fileExistsQuery.value(1).toString());
   QString databaseFilename(fileExistsQuery.value(1).toString());
   QDateTime fileLastModified(QFileInfo(databaseFilename).lastModified());
   QDateTime fileLastModified(QFileInfo(databaseFilename).lastModified());
   QDateTime databaseInsertTimestamp(QDateTime::fromString(fileExistsQuery.value(0).toString(),Qt::ISODate));
   QDateTime databaseInsertTimestamp(QDateTime::fromString(fileExistsQuery.value(0).toString(),Qt::ISODate));
 
 
-  qDebug() << "Looking for sopInstanceUID: " << sopInstanceUID;
-  qDebug() << " bound value : " << fileExistsQuery.boundValue(0);
-  qDebug() << " Ran Query : " << fileExistsQuery.lastQuery();
-  qDebug() << " Found databaseFilename : " << databaseFilename;
-  qDebug() << "  databaseInsertTimestamp : " << databaseInsertTimestamp;
-  qDebug() << "  fileLastModified : " << fileLastModified;
-
   qDebug() << "inserting filePath: " << filePath;
   qDebug() << "inserting filePath: " << filePath;
   if (databaseFilename == "")
   if (databaseFilename == "")
     {
     {
@@ -1142,14 +1136,23 @@ void ctkDICOMDatabasePrivate::insert( const ctkDICOMItem& ctkDataset, const QStr
     }
     }
   else
   else
     {
     {
-      qDebug() << "database filename for " << sopInstanceUID << " is: " << databaseFilename;
-      qDebug() << "modified date is: " << fileLastModified;
-      qDebug() << "db insert date is: " << databaseInsertTimestamp;
-      if ( fileExistsQuery.next() && fileLastModified < databaseInsertTimestamp )
+      if ( databaseFilename == filePath && fileLastModified < databaseInsertTimestamp )
         {
         {
           logger.debug ( "File " + databaseFilename + " already added" );
           logger.debug ( "File " + databaseFilename + " already added" );
           return;
           return;
         }
         }
+      else
+        {
+        QSqlQuery deleteFile ( Database );
+        deleteFile.prepare("DELETE FROM Images WHERE SOPInstanceUID == :sopInstanceUID");
+        deleteFile.bindValue(":sopInstanceUID",sopInstanceUID);
+        bool success = deleteFile.exec();
+        if (!success)
+          {
+            logger.error("SQLITE ERROR deleting old image row: " + deleteFile.lastError().driverText());
+            return;
+          }
+        }
     }
     }
   }
   }