diff Framework/Common/DatabaseManager.cpp @ 255:d663d9e44f8d

reintroduction of IDatabaseFactory into DatabaseManager
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 14 Apr 2021 17:57:08 +0200
parents 8a4ce70f456a
children 29d2b76516f6
line wrap: on
line diff
--- a/Framework/Common/DatabaseManager.cpp	Wed Apr 14 15:33:57 2021 +0200
+++ b/Framework/Common/DatabaseManager.cpp	Wed Apr 14 17:57:08 2021 +0200
@@ -159,30 +159,54 @@
   }
 
     
-  DatabaseManager::DatabaseManager(IDatabase* database) :
-    database_(database)
+  DatabaseManager::DatabaseManager(IDatabaseFactory* factory) :
+    factory_(factory),
+    dialect_(Dialect_Unknown)
   {
-    if (database == NULL)
+    if (factory == NULL)
     {
       throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
     }
-
-    dialect_ = database->GetDialect();
   }
 
   
   IDatabase& DatabaseManager::GetDatabase()
   {
+    assert(factory_.get() != NULL);
+    
     if (database_.get() == NULL)
     {
-      throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable);
+      database_.reset(factory_->Open());
+
+      if (database_.get() == NULL)
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+      }
+
+      dialect_ = database_->GetDialect();
+      if (dialect_ == Dialect_Unknown)
+      {
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
+      }
+    }
+
+    return *database_;
+  }
+
+
+  Dialect DatabaseManager::GetDialect() const
+  {
+    if (database_.get() == NULL)
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
     }
     else
     {
-      return *database_;
+      assert(dialect_ != Dialect_Unknown);
+      return dialect_;
     }
   }
-
+  
 
   void DatabaseManager::StartTransaction(TransactionType type)
   {