Mercurial > hg > orthanc-databases
changeset 216:fbb52129158a
TransactionType given to PostgreSQLTransaction constructor
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 26 Mar 2021 17:47:56 +0100 |
parents | b40b30075c51 |
children | ee5858d438dc |
files | Framework/PostgreSQL/PostgreSQLDatabase.cpp Framework/PostgreSQL/PostgreSQLTransaction.cpp Framework/PostgreSQL/PostgreSQLTransaction.h PostgreSQL/Plugins/PostgreSQLIndex.cpp PostgreSQL/Plugins/PostgreSQLStorageArea.cpp PostgreSQL/UnitTests/PostgreSQLTests.cpp |
diffstat | 6 files changed, 47 insertions(+), 24 deletions(-) [+] |
line wrap: on
line diff
--- a/Framework/PostgreSQL/PostgreSQLDatabase.cpp Fri Mar 26 17:32:29 2021 +0100 +++ b/Framework/PostgreSQL/PostgreSQLDatabase.cpp Fri Mar 26 17:47:56 2021 +0100 @@ -112,7 +112,7 @@ bool PostgreSQLDatabase::RunAdvisoryLockStatement(const std::string& statement) { - PostgreSQLTransaction transaction(*this); + PostgreSQLTransaction transaction(*this, TransactionType_ReadWrite); Query query(statement, false); PostgreSQLStatement s(*this, query); @@ -206,7 +206,7 @@ void PostgreSQLDatabase::ClearAll() { - PostgreSQLTransaction transaction(*this); + PostgreSQLTransaction transaction(*this, TransactionType_ReadWrite); // Remove all the large objects Execute("SELECT lo_unlink(loid) FROM (SELECT DISTINCT loid FROM pg_catalog.pg_largeobject) as loids;"); @@ -255,12 +255,9 @@ case TransactionType_Implicit: return new PostgreSQLImplicitTransaction; + case TransactionType_ReadWrite: case TransactionType_ReadOnly: - // TODO => READ-ONLY - return new PostgreSQLTransaction(*this); - - case TransactionType_ReadWrite: - return new PostgreSQLTransaction(*this); + return new PostgreSQLTransaction(*this, type); default: throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
--- a/Framework/PostgreSQL/PostgreSQLTransaction.cpp Fri Mar 26 17:32:29 2021 +0100 +++ b/Framework/PostgreSQL/PostgreSQLTransaction.cpp Fri Mar 26 17:47:56 2021 +0100 @@ -29,11 +29,12 @@ namespace OrthancDatabases { - PostgreSQLTransaction::PostgreSQLTransaction(PostgreSQLDatabase& database) : + PostgreSQLTransaction::PostgreSQLTransaction(PostgreSQLDatabase& database, + TransactionType type) : database_(database), isOpen_(false) { - Begin(); + Begin(type); } @@ -55,7 +56,7 @@ } - void PostgreSQLTransaction::Begin() + void PostgreSQLTransaction::Begin(TransactionType type) { if (isOpen_) { @@ -64,7 +65,21 @@ } database_.Execute("BEGIN"); - database_.Execute("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE"); + + switch (type) + { + case TransactionType_ReadWrite: + database_.Execute("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ WRITE"); + break; + + case TransactionType_ReadOnly: + database_.Execute("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY"); + break; + + default: + throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); + } + isOpen_ = true; }
--- a/Framework/PostgreSQL/PostgreSQLTransaction.h Fri Mar 26 17:32:29 2021 +0100 +++ b/Framework/PostgreSQL/PostgreSQLTransaction.h Fri Mar 26 17:47:56 2021 +0100 @@ -37,8 +37,11 @@ PostgreSQLDatabase& database_; bool isOpen_; + void Begin(TransactionType type); + public: - explicit PostgreSQLTransaction(PostgreSQLDatabase& database); + explicit PostgreSQLTransaction(PostgreSQLDatabase& database, + TransactionType type); ~PostgreSQLTransaction(); @@ -47,8 +50,6 @@ return false; } - void Begin(); - virtual void Rollback() ORTHANC_OVERRIDE; virtual void Commit() ORTHANC_OVERRIDE;
--- a/PostgreSQL/Plugins/PostgreSQLIndex.cpp Fri Mar 26 17:32:29 2021 +0100 +++ b/PostgreSQL/Plugins/PostgreSQLIndex.cpp Fri Mar 26 17:47:56 2021 +0100 @@ -81,7 +81,7 @@ } { - PostgreSQLTransaction t(*db); + PostgreSQLTransaction t(*db, TransactionType_ReadWrite); if (!db->DoesTableExist("Resources")) { @@ -127,7 +127,7 @@ } { - PostgreSQLTransaction t(*db); + PostgreSQLTransaction t(*db, TransactionType_ReadWrite); int hasTrigram = 0; if (!LookupGlobalIntegerProperty(hasTrigram, *db, t, @@ -174,7 +174,7 @@ } { - PostgreSQLTransaction t(*db); + PostgreSQLTransaction t(*db, TransactionType_ReadWrite); int property = 0; if (!LookupGlobalIntegerProperty(property, *db, t,
--- a/PostgreSQL/Plugins/PostgreSQLStorageArea.cpp Fri Mar 26 17:32:29 2021 +0100 +++ b/PostgreSQL/Plugins/PostgreSQLStorageArea.cpp Fri Mar 26 17:47:56 2021 +0100 @@ -51,7 +51,7 @@ } { - PostgreSQLTransaction t(*db); + PostgreSQLTransaction t(*db, TransactionType_ReadWrite); if (!db->DoesTableExist("StorageArea")) {
--- a/PostgreSQL/UnitTests/PostgreSQLTests.cpp Fri Mar 26 17:32:29 2021 +0100 +++ b/PostgreSQL/UnitTests/PostgreSQLTests.cpp Fri Mar 26 17:47:56 2021 +0100 @@ -206,7 +206,15 @@ s.Run(); { - PostgreSQLTransaction t(*pg); + PostgreSQLTransaction t(*pg, TransactionType_ReadOnly); + s.BindInteger(0, 0); + s.BindInteger(1, 1); + // Failure, as INSERT in a read-only transaction + ASSERT_THROW(s.Run(), Orthanc::OrthancException); + } + + { + PostgreSQLTransaction t(*pg, TransactionType_ReadWrite); s.BindInteger(0, 43); s.BindInteger(1, 4343); s.Run(); @@ -222,13 +230,14 @@ } { + // Implicit transaction PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test"); PostgreSQLResult r(u); ASSERT_EQ(1, r.GetInteger64(0)); // Just "1" because of implicit rollback } { - PostgreSQLTransaction t(*pg); + PostgreSQLTransaction t(*pg, TransactionType_ReadWrite); s.BindInteger(0, 43); s.BindInteger(1, 4343); s.Run(); @@ -248,6 +257,7 @@ } { + PostgreSQLTransaction t(*pg, TransactionType_ReadOnly); PostgreSQLStatement u(*pg, "SELECT COUNT(*) FROM Test"); PostgreSQLResult r(u); ASSERT_EQ(3, r.GetInteger64(0)); @@ -276,7 +286,7 @@ for (int i = 0; i < 10; i++) { - PostgreSQLTransaction t(*pg); + PostgreSQLTransaction t(*pg, TransactionType_ReadWrite); std::string value = "Value " + boost::lexical_cast<std::string>(i * 2); PostgreSQLLargeObject obj(*pg, value); @@ -297,7 +307,7 @@ ASSERT_EQ(10, CountLargeObjects(*pg)); { - PostgreSQLTransaction t(*pg); + PostgreSQLTransaction t(*pg, TransactionType_ReadOnly); PostgreSQLStatement s(*pg, "SELECT * FROM Test ORDER BY name DESC"); PostgreSQLResult r(s); @@ -318,7 +328,7 @@ { - PostgreSQLTransaction t(*pg); + PostgreSQLTransaction t(*pg, TransactionType_ReadWrite); PostgreSQLStatement s(*pg, "DELETE FROM Test WHERE name='Index 9'"); s.Run(); t.Commit(); @@ -327,7 +337,7 @@ { // Count the number of items in the DB - PostgreSQLTransaction t(*pg); + PostgreSQLTransaction t(*pg, TransactionType_ReadOnly); PostgreSQLStatement s(*pg, "SELECT COUNT(*) FROM Test"); PostgreSQLResult r(s); ASSERT_EQ(9, r.GetInteger64(0));