changeset 217:ee5858d438dc

TransactionType given to MySQLTransaction constructor
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 26 Mar 2021 18:02:34 +0100
parents fbb52129158a
children 90eb271f85b2
files Framework/MySQL/MySQLDatabase.cpp Framework/MySQL/MySQLTransaction.cpp Framework/MySQL/MySQLTransaction.h MySQL/Plugins/MySQLIndex.cpp MySQL/Plugins/MySQLStorageArea.cpp MySQL/UnitTests/UnitTestsMain.cpp
diffstat 6 files changed, 32 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/Framework/MySQL/MySQLDatabase.cpp	Fri Mar 26 17:47:56 2021 +0100
+++ b/Framework/MySQL/MySQLDatabase.cpp	Fri Mar 26 18:02:34 2021 +0100
@@ -207,7 +207,7 @@
     const std::string& database = parameters.GetDatabase();
     
     {
-      MySQLTransaction t(db);
+      MySQLTransaction t(db, TransactionType_ReadWrite);
 
       if (!db.DoesDatabaseExist(t, database))
       {
@@ -339,7 +339,7 @@
     {
       MySQLStatement statement(*this, query);
 
-      MySQLTransaction t(*this);
+      MySQLTransaction t(*this, TransactionType_ReadWrite);
       std::unique_ptr<IResult> result(t.Execute(statement, args));
 
       success = (!result->IsDone() &&
@@ -547,11 +547,8 @@
         return new MySQLImplicitTransaction;
 
       case TransactionType_ReadOnly:
-        // TODO => READ-ONLY
-        return new MySQLTransaction(*this);
-
       case TransactionType_ReadWrite:
-        return new MySQLTransaction(*this);
+        return new MySQLTransaction(*this, type);
 
       default:
         throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
--- a/Framework/MySQL/MySQLTransaction.cpp	Fri Mar 26 17:47:56 2021 +0100
+++ b/Framework/MySQL/MySQLTransaction.cpp	Fri Mar 26 18:02:34 2021 +0100
@@ -31,11 +31,25 @@
 
 namespace OrthancDatabases
 {
-  MySQLTransaction::MySQLTransaction(MySQLDatabase& db) :
+  MySQLTransaction::MySQLTransaction(MySQLDatabase& db,
+                                     TransactionType type) :
     db_(db),
     active_(false)
   {
-    db_.Execute("START TRANSACTION", false);
+    switch (type)
+    {
+      case TransactionType_ReadWrite:
+        db_.Execute("START TRANSACTION READ WRITE", false);
+        break;
+
+      case TransactionType_ReadOnly:
+        db_.Execute("START TRANSACTION READ ONLY", false);
+        break;
+
+      default:
+        throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
+    }
+        
     active_ = true;
   }
 
--- a/Framework/MySQL/MySQLTransaction.h	Fri Mar 26 17:47:56 2021 +0100
+++ b/Framework/MySQL/MySQLTransaction.h	Fri Mar 26 18:02:34 2021 +0100
@@ -37,7 +37,8 @@
     bool            active_;
 
   public:
-    explicit MySQLTransaction(MySQLDatabase& db);
+    explicit MySQLTransaction(MySQLDatabase& db,
+                              TransactionType type);
 
     virtual ~MySQLTransaction();
 
--- a/MySQL/Plugins/MySQLIndex.cpp	Fri Mar 26 17:47:56 2021 +0100
+++ b/MySQL/Plugins/MySQLIndex.cpp	Fri Mar 26 18:02:34 2021 +0100
@@ -97,7 +97,7 @@
        * https://groups.google.com/d/msg/orthanc-users/OCFFkm1qm0k/Mbroy8VWAQAJ
        **/      
       {
-        MySQLTransaction t(*db);
+        MySQLTransaction t(*db, TransactionType_ReadWrite);
         
         db->Execute("ALTER DATABASE " + parameters_.GetDatabase() + 
                     " CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci", false);
@@ -126,7 +126,7 @@
       int version = 0;
 
       {
-        MySQLTransaction t(*db);
+        MySQLTransaction t(*db, TransactionType_ReadWrite);
 
         // This is the last table to be created
         if (!db->DoesTableExist(t, "PatientRecyclingOrder"))
@@ -160,7 +160,7 @@
       int revision = 0;
 
       {
-        MySQLTransaction t(*db);
+        MySQLTransaction t(*db, TransactionType_ReadWrite);
 
         if (!LookupGlobalIntegerProperty(revision, *db, t, Orthanc::GlobalProperty_DatabasePatchLevel))
         {
@@ -173,7 +173,7 @@
 
       if (revision == 1)
       {
-        MySQLTransaction t(*db);
+        MySQLTransaction t(*db, TransactionType_ReadWrite);
         
         // The serialization of jobs as a global property can lead to
         // very long values => switch to the LONGTEXT type that can
@@ -189,7 +189,7 @@
 
       if (revision == 2)
       {        
-        MySQLTransaction t(*db);
+        MySQLTransaction t(*db, TransactionType_ReadWrite);
 
         // Install the "GetLastChangeIndex" extension
         std::string query;
@@ -211,7 +211,7 @@
       
       if (revision == 3)
       {
-        MySQLTransaction t(*db);
+        MySQLTransaction t(*db, TransactionType_ReadWrite);
 
         // Reconfiguration of "Metadata" from TEXT type (up to 64KB)
         // to the LONGTEXT type (up to 4GB). This might be important
@@ -228,7 +228,7 @@
       
       if (revision == 4)
       {
-        MySQLTransaction t(*db);
+        MySQLTransaction t(*db, TransactionType_ReadWrite);
         
         // Install the "CreateInstance" extension
         std::string query;
--- a/MySQL/Plugins/MySQLStorageArea.cpp	Fri Mar 26 17:47:56 2021 +0100
+++ b/MySQL/Plugins/MySQLStorageArea.cpp	Fri Mar 26 18:02:34 2021 +0100
@@ -41,7 +41,7 @@
 
     {
       MySQLDatabase::TransientAdvisoryLock lock(*db, MYSQL_LOCK_DATABASE_SETUP);    
-      MySQLTransaction t(*db);
+      MySQLTransaction t(*db, TransactionType_ReadWrite);
 
       int64_t size;
       if (db->LookupGlobalIntegerVariable(size, "max_allowed_packet"))
--- a/MySQL/UnitTests/UnitTestsMain.cpp	Fri Mar 26 17:47:56 2021 +0100
+++ b/MySQL/UnitTests/UnitTestsMain.cpp	Fri Mar 26 18:02:34 2021 +0100
@@ -137,7 +137,7 @@
 {
   OrthancDatabases::Query query("SELECT COUNT(*) FROM StorageArea", true);
   OrthancDatabases::MySQLStatement s(db, query);
-  OrthancDatabases::MySQLTransaction t(db);
+  OrthancDatabases::MySQLTransaction t(db, OrthancDatabases::TransactionType_ReadOnly);
   OrthancDatabases::Dictionary d;
   std::unique_ptr<OrthancDatabases::IResult> result(s.Execute(t, d));
   return dynamic_cast<const OrthancDatabases::Integer64Value&>(result->GetField(0)).GetValue();
@@ -213,7 +213,7 @@
   db.Open();
 
   {
-    OrthancDatabases::MySQLTransaction t(db);
+    OrthancDatabases::MySQLTransaction t(db, OrthancDatabases::TransactionType_ReadOnly);
     ASSERT_FALSE(db.DoesTableExist(t, "test"));
     ASSERT_FALSE(db.DoesTableExist(t, "test2"));
   }
@@ -252,7 +252,7 @@
   }
 
   {
-    OrthancDatabases::MySQLTransaction t(db);
+    OrthancDatabases::MySQLTransaction t(db, OrthancDatabases::TransactionType_ReadOnly);
     ASSERT_TRUE(db.DoesTableExist(t, "test"));
     ASSERT_TRUE(db.DoesTableExist(t, "test2"));
   }