changeset 3116:0fa7181ac4e5 db-changes

conrt
author Sebastien Jodogne <s.jodogne@gmail.com>
date Sat, 12 Jan 2019 11:08:53 +0100
parents 2c108461d409
children a323b75e5b08
files OrthancServer/Database/IDatabaseWrapper.h OrthancServer/Database/SQLiteDatabaseWrapper.cpp OrthancServer/Database/SQLiteDatabaseWrapper.h UnitTestsSources/ServerIndexTests.cpp
diffstat 4 files changed, 84 insertions(+), 41 deletions(-) [+]
line wrap: on
line diff
--- a/OrthancServer/Database/IDatabaseWrapper.h	Sat Jan 12 09:15:32 2019 +0100
+++ b/OrthancServer/Database/IDatabaseWrapper.h	Sat Jan 12 11:08:53 2019 +0100
@@ -221,9 +221,12 @@
                                       ResourceType queryLevel,
                                       size_t limit) = 0;
 
-    // Returns "true" iff. the instance already exists. If "false" is
-    // returned, the content of "result" is undefined, but
-    // "instanceId" must be properly set.
+    // Returns "true" iff. the instance is new and has been inserted
+    // into the database. If "false" is returned, the content of
+    // "result" is undefined, but "instanceId" must be properly
+    // set. This method must also tag the parent patient as the most
+    // recent in the patient recycling order if it is not protected
+    // (so as to fix issue #58).
     virtual bool CreateInstance(CreateInstanceResult& result, /* out */
                                 int64_t& instanceId,          /* out */
                                 const std::string& patient,
--- a/OrthancServer/Database/SQLiteDatabaseWrapper.cpp	Sat Jan 12 09:15:32 2019 +0100
+++ b/OrthancServer/Database/SQLiteDatabaseWrapper.cpp	Sat Jan 12 11:08:53 2019 +0100
@@ -329,6 +329,31 @@
   }
 
 
+  int SQLiteDatabaseWrapper::GetGlobalIntegerProperty(GlobalProperty property,
+                                                      unsigned int defaultValue)
+  {
+    std::string tmp;
+
+    if (!LookupGlobalProperty(tmp, GlobalProperty_DatabasePatchLevel))
+    {
+      return defaultValue;
+    }
+    else
+    {
+      try
+      {
+        return boost::lexical_cast<int>(tmp);
+      }
+      catch (boost::bad_lexical_cast&)
+      {
+        throw OrthancException(ErrorCode_ParameterOutOfRange,
+                               "Global property " + boost::lexical_cast<std::string>(property) +
+                               " should be an integer, but found: " + tmp);
+      }
+    }
+  }
+
+
   void SQLiteDatabaseWrapper::Open()
   {
     db_.Execute("PRAGMA ENCODING=\"UTF-8\";");
@@ -344,49 +369,61 @@
     // Make "LIKE" case-sensitive in SQLite 
     db_.Execute("PRAGMA case_sensitive_like = true;");
     
-    if (!db_.DoesTableExist("GlobalProperties"))
     {
-      LOG(INFO) << "Creating the database";
-      std::string query;
-      EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE);
-      db_.Execute(query);
-    }
-
-    // Check the version of the database
-    std::string tmp;
-    if (!LookupGlobalProperty(tmp, GlobalProperty_DatabaseSchemaVersion))
-    {
-      tmp = "Unknown";
-    }
+      SQLite::Transaction t(db_);
+      t.Begin();
 
-    bool ok = false;
-    try
-    {
-      LOG(INFO) << "Version of the Orthanc database: " << tmp;
-      version_ = boost::lexical_cast<unsigned int>(tmp);
-      ok = true;
-    }
-    catch (boost::bad_lexical_cast&)
-    {
-    }
-
-    if (!ok)
-    {
-      throw OrthancException(ErrorCode_IncompatibleDatabaseVersion,
-                             "Incompatible version of the Orthanc database: " + tmp);
-    }
-
-    // New in Orthanc 1.5.1
-    if (version_ == 6)
-    {
-      if (!LookupGlobalProperty(tmp, GlobalProperty_GetTotalSizeIsFast) ||
-          tmp != "1")
+      if (!db_.DoesTableExist("GlobalProperties"))
       {
-        LOG(INFO) << "Installing the SQLite triggers to track the size of the attachments";
+        LOG(INFO) << "Creating the database";
         std::string query;
-        EmbeddedResources::GetFileResource(query, EmbeddedResources::INSTALL_TRACK_ATTACHMENTS_SIZE);
+        EmbeddedResources::GetFileResource(query, EmbeddedResources::PREPARE_DATABASE);
         db_.Execute(query);
       }
+
+      // Check the version of the database
+      std::string tmp;
+      if (!LookupGlobalProperty(tmp, GlobalProperty_DatabaseSchemaVersion))
+      {
+        tmp = "Unknown";
+      }
+
+      bool ok = false;
+      try
+      {
+        LOG(INFO) << "Version of the Orthanc database: " << tmp;
+        version_ = boost::lexical_cast<unsigned int>(tmp);
+        ok = true;
+      }
+      catch (boost::bad_lexical_cast&)
+      {
+      }
+
+      if (!ok)
+      {
+        throw OrthancException(ErrorCode_IncompatibleDatabaseVersion,
+                               "Incompatible version of the Orthanc database: " + tmp);
+      }
+
+      // New in Orthanc 1.5.1
+      if (version_ == 6)
+      {
+        if (!LookupGlobalProperty(tmp, GlobalProperty_GetTotalSizeIsFast) ||
+            tmp != "1")
+        {
+          LOG(INFO) << "Installing the SQLite triggers to track the size of the attachments";
+          std::string query;
+          EmbeddedResources::GetFileResource(query, EmbeddedResources::INSTALL_TRACK_ATTACHMENTS_SIZE);
+          db_.Execute(query);
+        }
+      }
+
+      /*if (GetGlobalIntegerProperty(GlobalProperty_DatabasePatchLevel, 0) <= 0)
+      {
+        SetGlobalProperty(GlobalProperty_DatabasePatchLevel, "1");
+        }*/
+
+      t.Commit();
     }
 
     signalRemainingAncestor_ = new Internals::SignalRemainingAncestor;
--- a/OrthancServer/Database/SQLiteDatabaseWrapper.h	Sat Jan 12 09:15:32 2019 +0100
+++ b/OrthancServer/Database/SQLiteDatabaseWrapper.h	Sat Jan 12 11:08:53 2019 +0100
@@ -79,6 +79,9 @@
 
     void ClearTable(const std::string& tableName);
 
+    int GetGlobalIntegerProperty(GlobalProperty property,
+                                 unsigned int defaultValue);
+
   public:
     SQLiteDatabaseWrapper(const std::string& path);
 
--- a/UnitTestsSources/ServerIndexTests.cpp	Sat Jan 12 09:15:32 2019 +0100
+++ b/UnitTestsSources/ServerIndexTests.cpp	Sat Jan 12 11:08:53 2019 +0100
@@ -395,7 +395,7 @@
 
   CheckTableRecordCount(0, "Resources");
   CheckTableRecordCount(0, "AttachedFiles");
-  CheckTableRecordCount(3, "GlobalProperties");
+  CheckTableRecordCount(4, "GlobalProperties");  // 4 since 1.5.2 because of GlobalProperty_DatabasePatchLevel
 
   std::string tmp;
   ASSERT_TRUE(index_->LookupGlobalProperty(tmp, GlobalProperty_DatabaseSchemaVersion));