changeset 215:b40b30075c51

added TransactionType_Implicit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 26 Mar 2021 17:32:29 +0100
parents ab96698c73a3
children fbb52129158a
files Framework/Common/DatabaseManager.cpp Framework/Common/DatabasesEnumerations.h Framework/Common/IDatabase.h Framework/MySQL/MySQLDatabase.cpp Framework/MySQL/MySQLDatabase.h Framework/PostgreSQL/PostgreSQLDatabase.cpp Framework/PostgreSQL/PostgreSQLDatabase.h Framework/SQLite/SQLiteDatabase.cpp Framework/SQLite/SQLiteDatabase.h MySQL/UnitTests/UnitTestsMain.cpp PostgreSQL/UnitTests/PostgreSQLTests.cpp SQLite/UnitTests/UnitTestsMain.cpp
diffstat 12 files changed, 57 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/Common/DatabaseManager.cpp	Thu Mar 25 13:56:26 2021 +0100
+++ b/Framework/Common/DatabaseManager.cpp	Fri Mar 26 17:32:29 2021 +0100
@@ -165,7 +165,7 @@
 
       try
       {
-        transaction_.reset(GetDatabase().CreateTransaction(true));
+        transaction_.reset(GetDatabase().CreateTransaction(TransactionType_Implicit));
       }
       catch (Orthanc::OrthancException& e)
       {
@@ -226,7 +226,7 @@
         throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
       }
 
-      transaction_.reset(GetDatabase().CreateTransaction(false));
+      transaction_.reset(GetDatabase().CreateTransaction(type));
     }
     catch (Orthanc::OrthancException& e)
     {
--- a/Framework/Common/DatabasesEnumerations.h	Thu Mar 25 13:56:26 2021 +0100
+++ b/Framework/Common/DatabasesEnumerations.h	Fri Mar 26 17:32:29 2021 +0100
@@ -42,7 +42,8 @@
 
   enum TransactionType
   {
-    TransactionType_ReadOnly,
-    TransactionType_ReadWrite
+    TransactionType_ReadWrite,
+    TransactionType_ReadOnly,  // Should only arise with Orthanc SDK >= 1.9.2 in the index plugin
+    TransactionType_Implicit   // Should only arise with Orthanc SDK <= 1.9.1
   };
 }
--- a/Framework/Common/IDatabase.h	Thu Mar 25 13:56:26 2021 +0100
+++ b/Framework/Common/IDatabase.h	Fri Mar 26 17:32:29 2021 +0100
@@ -38,6 +38,6 @@
 
     virtual IPrecompiledStatement* Compile(const Query& query) = 0;
 
-    virtual ITransaction* CreateTransaction(bool isImplicit) = 0;
+    virtual ITransaction* CreateTransaction(TransactionType type) = 0;
   };
 }
--- a/Framework/MySQL/MySQLDatabase.cpp	Thu Mar 25 13:56:26 2021 +0100
+++ b/Framework/MySQL/MySQLDatabase.cpp	Fri Mar 26 17:32:29 2021 +0100
@@ -518,9 +518,6 @@
   {
     class MySQLImplicitTransaction : public ImplicitTransaction
     {
-    private:
-      MySQLDatabase&  db_;
-
     protected:
       virtual IResult* ExecuteInternal(IPrecompiledStatement& statement,
                                        const Dictionary& parameters)
@@ -533,30 +530,31 @@
       {
         dynamic_cast<MySQLStatement&>(statement).ExecuteWithoutResult(*this, parameters);
       }
-      
-    public:
-      explicit MySQLImplicitTransaction(MySQLDatabase&  db) :
-        db_(db)
-      {
-      }
     };
   }
   
 
-  ITransaction* MySQLDatabase::CreateTransaction(bool isImplicit)
+  ITransaction* MySQLDatabase::CreateTransaction(TransactionType type)
   {
     if (mysql_ == NULL)
     {
       throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
     }
 
-    if (isImplicit)
+    switch (type)
     {
-      return new MySQLImplicitTransaction(*this);
-    }
-    else
-    {
-      return new MySQLTransaction(*this);
+      case TransactionType_Implicit:
+        return new MySQLImplicitTransaction;
+
+      case TransactionType_ReadOnly:
+        // TODO => READ-ONLY
+        return new MySQLTransaction(*this);
+
+      case TransactionType_ReadWrite:
+        return new MySQLTransaction(*this);
+
+      default:
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
     }
   }
 
--- a/Framework/MySQL/MySQLDatabase.h	Thu Mar 25 13:56:26 2021 +0100
+++ b/Framework/MySQL/MySQLDatabase.h	Fri Mar 26 17:32:29 2021 +0100
@@ -98,7 +98,7 @@
 
     virtual IPrecompiledStatement* Compile(const Query& query) ORTHANC_OVERRIDE;
 
-    virtual ITransaction* CreateTransaction(bool isImplicit) ORTHANC_OVERRIDE;
+    virtual ITransaction* CreateTransaction(TransactionType type) ORTHANC_OVERRIDE;
 
     static void GlobalFinalization();
 
--- a/Framework/PostgreSQL/PostgreSQLDatabase.cpp	Thu Mar 25 13:56:26 2021 +0100
+++ b/Framework/PostgreSQL/PostgreSQLDatabase.cpp	Fri Mar 26 17:32:29 2021 +0100
@@ -232,9 +232,6 @@
   {
     class PostgreSQLImplicitTransaction : public ImplicitTransaction
     {
-    private:
-      PostgreSQLDatabase&  db_;
-
     protected:
       virtual IResult* ExecuteInternal(IPrecompiledStatement& statement,
                                        const Dictionary& parameters)
@@ -247,25 +244,26 @@
       {
         dynamic_cast<PostgreSQLStatement&>(statement).ExecuteWithoutResult(*this, parameters);
       }
-      
-    public:
-      explicit PostgreSQLImplicitTransaction(PostgreSQLDatabase&  db) :
-        db_(db)
-      {
-      }
     };
   }
   
   
-  ITransaction* PostgreSQLDatabase::CreateTransaction(bool isImplicit)
+  ITransaction* PostgreSQLDatabase::CreateTransaction(TransactionType type)
   {
-    if (isImplicit)
+    switch (type)
     {
-      return new PostgreSQLImplicitTransaction(*this);
-    }
-    else
-    {
-      return new PostgreSQLTransaction(*this);
+      case TransactionType_Implicit:
+        return new PostgreSQLImplicitTransaction;
+
+      case TransactionType_ReadOnly:
+        // TODO => READ-ONLY
+        return new PostgreSQLTransaction(*this);
+
+      case TransactionType_ReadWrite:
+        return new PostgreSQLTransaction(*this);
+
+      default:
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
     }
   }
 
--- a/Framework/PostgreSQL/PostgreSQLDatabase.h	Thu Mar 25 13:56:26 2021 +0100
+++ b/Framework/PostgreSQL/PostgreSQLDatabase.h	Fri Mar 26 17:32:29 2021 +0100
@@ -75,7 +75,7 @@
 
     virtual IPrecompiledStatement* Compile(const Query& query) ORTHANC_OVERRIDE;
 
-    virtual ITransaction* CreateTransaction(bool isImplicit) ORTHANC_OVERRIDE;
+    virtual ITransaction* CreateTransaction(TransactionType type) ORTHANC_OVERRIDE;
 
     class TransientAdvisoryLock
     {
--- a/Framework/SQLite/SQLiteDatabase.cpp	Thu Mar 25 13:56:26 2021 +0100
+++ b/Framework/SQLite/SQLiteDatabase.cpp	Fri Mar 26 17:32:29 2021 +0100
@@ -48,9 +48,6 @@
   {
     class SQLiteImplicitTransaction : public ImplicitTransaction
     {
-    private:
-      SQLiteDatabase&  db_;
-
     protected:
       virtual IResult* ExecuteInternal(IPrecompiledStatement& statement,
                                        const Dictionary& parameters) ORTHANC_OVERRIDE
@@ -63,24 +60,22 @@
       {
         dynamic_cast<SQLiteStatement&>(statement).ExecuteWithoutResult(*this, parameters);
       }
-      
-    public:
-      explicit SQLiteImplicitTransaction(SQLiteDatabase&  db) :
-        db_(db)
-      {
-      }
     };
   }
   
-  ITransaction* SQLiteDatabase::CreateTransaction(bool isImplicit)
+  ITransaction* SQLiteDatabase::CreateTransaction(TransactionType type)
   {
-    if (isImplicit)
+    switch (type)
     {
-      return new SQLiteImplicitTransaction(*this);
-    }
-    else
-    {
-      return new SQLiteTransaction(*this);
+      case TransactionType_Implicit:
+        return new SQLiteImplicitTransaction;
+
+      case TransactionType_ReadOnly:
+      case TransactionType_ReadWrite:
+        return new SQLiteTransaction(*this);
+
+      default:
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
     }
   }
 }
--- a/Framework/SQLite/SQLiteDatabase.h	Thu Mar 25 13:56:26 2021 +0100
+++ b/Framework/SQLite/SQLiteDatabase.h	Fri Mar 26 17:32:29 2021 +0100
@@ -71,6 +71,6 @@
 
     virtual IPrecompiledStatement* Compile(const Query& query) ORTHANC_OVERRIDE;
 
-    virtual ITransaction* CreateTransaction(bool isImplicit) ORTHANC_OVERRIDE;
+    virtual ITransaction* CreateTransaction(TransactionType type) ORTHANC_OVERRIDE;
   };
 }
--- a/MySQL/UnitTests/UnitTestsMain.cpp	Thu Mar 25 13:56:26 2021 +0100
+++ b/MySQL/UnitTests/UnitTestsMain.cpp	Fri Mar 26 17:32:29 2021 +0100
@@ -219,7 +219,7 @@
   }
 
   {
-    std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(false));
+    std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(OrthancDatabases::TransactionType_ReadWrite));
     ASSERT_FALSE(t->IsImplicit());
   }
 
@@ -227,7 +227,7 @@
     OrthancDatabases::Query query("CREATE TABLE test(id INT)", false);
     std::unique_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query));
     
-    std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true));
+    std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(OrthancDatabases::TransactionType_Implicit));
     ASSERT_TRUE(t->IsImplicit());
     ASSERT_THROW(t->Commit(), Orthanc::OrthancException);
     ASSERT_THROW(t->Rollback(), Orthanc::OrthancException);
@@ -245,7 +245,7 @@
     OrthancDatabases::Query query("CREATE TABLE test2(id INT)", false);
     std::unique_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query));
     
-    std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true));
+    std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(OrthancDatabases::TransactionType_Implicit));
 
     OrthancDatabases::Dictionary args;
     t->ExecuteWithoutResult(*s, args);
--- a/PostgreSQL/UnitTests/PostgreSQLTests.cpp	Thu Mar 25 13:56:26 2021 +0100
+++ b/PostgreSQL/UnitTests/PostgreSQLTests.cpp	Fri Mar 26 17:32:29 2021 +0100
@@ -406,7 +406,7 @@
   ASSERT_FALSE(db->DoesTableExist("test2"));
 
   {
-    std::unique_ptr<OrthancDatabases::ITransaction> t(db->CreateTransaction(false));
+    std::unique_ptr<OrthancDatabases::ITransaction> t(db->CreateTransaction(TransactionType_ReadWrite));
     ASSERT_FALSE(t->IsImplicit());
   }
 
@@ -414,7 +414,7 @@
     Query query("CREATE TABLE test(id INT)", false);
     std::unique_ptr<IPrecompiledStatement> s(db->Compile(query));
     
-    std::unique_ptr<ITransaction> t(db->CreateTransaction(true));
+    std::unique_ptr<ITransaction> t(db->CreateTransaction(TransactionType_Implicit));
     ASSERT_TRUE(t->IsImplicit());
     ASSERT_THROW(t->Commit(), Orthanc::OrthancException);
     ASSERT_THROW(t->Rollback(), Orthanc::OrthancException);
@@ -432,7 +432,7 @@
     Query query("CREATE TABLE test2(id INT)", false);
     std::unique_ptr<IPrecompiledStatement> s(db->Compile(query));
     
-    std::unique_ptr<ITransaction> t(db->CreateTransaction(true));
+    std::unique_ptr<ITransaction> t(db->CreateTransaction(TransactionType_Implicit));
 
     Dictionary args;
     t->ExecuteWithoutResult(*s, args);
--- a/SQLite/UnitTests/UnitTestsMain.cpp	Thu Mar 25 13:56:26 2021 +0100
+++ b/SQLite/UnitTests/UnitTestsMain.cpp	Fri Mar 26 17:32:29 2021 +0100
@@ -69,7 +69,7 @@
   ASSERT_FALSE(db.DoesTableExist("test2"));
 
   {
-    std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(false));
+    std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(OrthancDatabases::TransactionType_ReadWrite));
     ASSERT_FALSE(t->IsImplicit());
   }
 
@@ -77,7 +77,7 @@
     OrthancDatabases::Query query("CREATE TABLE test(id INT)", false);
     std::unique_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query));
     
-    std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true));
+    std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(OrthancDatabases::TransactionType_Implicit));
     ASSERT_TRUE(t->IsImplicit());
     ASSERT_THROW(t->Commit(), Orthanc::OrthancException);
     ASSERT_THROW(t->Rollback(), Orthanc::OrthancException);
@@ -95,7 +95,7 @@
     OrthancDatabases::Query query("CREATE TABLE test2(id INT)", false);
     std::unique_ptr<OrthancDatabases::IPrecompiledStatement> s(db.Compile(query));
     
-    std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(true));
+    std::unique_ptr<OrthancDatabases::ITransaction> t(db.CreateTransaction(OrthancDatabases::TransactionType_Implicit));
 
     OrthancDatabases::Dictionary args;
     t->ExecuteWithoutResult(*s, args);