Mercurial > hg > orthanc-databases
comparison Framework/PostgreSQL/PostgreSQLTransaction.cpp @ 0:7cea966b6829
initial commit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 04 Jul 2018 08:16:29 +0200 |
parents | |
children | 6a574d810b98 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7cea966b6829 |
---|---|
1 /** | |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium | |
6 * | |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "PostgreSQLTransaction.h" | |
23 | |
24 #include "PostgreSQLStatement.h" | |
25 | |
26 #include <Core/Logging.h> | |
27 #include <Core/OrthancException.h> | |
28 | |
29 namespace OrthancDatabases | |
30 { | |
31 PostgreSQLTransaction::PostgreSQLTransaction(PostgreSQLDatabase& database) : | |
32 database_(database), | |
33 isOpen_(false), | |
34 readOnly_(true) | |
35 { | |
36 Begin(); | |
37 } | |
38 | |
39 | |
40 PostgreSQLTransaction::~PostgreSQLTransaction() | |
41 { | |
42 if (isOpen_) | |
43 { | |
44 LOG(WARNING) << "PostgreSQL: An active PostgreSQL transaction was dismissed"; | |
45 | |
46 try | |
47 { | |
48 database_.Execute("ABORT"); | |
49 } | |
50 catch (Orthanc::OrthancException&) | |
51 { | |
52 } | |
53 } | |
54 } | |
55 | |
56 | |
57 void PostgreSQLTransaction::Begin() | |
58 { | |
59 if (isOpen_) | |
60 { | |
61 LOG(ERROR) << "PostgreSQL: Beginning a transaction twice!"; | |
62 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
63 } | |
64 | |
65 database_.Execute("BEGIN"); | |
66 database_.Execute("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE"); | |
67 readOnly_ = true; | |
68 isOpen_ = true; | |
69 } | |
70 | |
71 | |
72 void PostgreSQLTransaction::Rollback() | |
73 { | |
74 if (!isOpen_) | |
75 { | |
76 LOG(ERROR) << "PostgreSQL: Attempting to rollback a nonexistent transaction. " | |
77 << "Did you remember to call Begin()?"; | |
78 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
79 } | |
80 | |
81 database_.Execute("ABORT"); | |
82 isOpen_ = false; | |
83 } | |
84 | |
85 | |
86 void PostgreSQLTransaction::Commit() | |
87 { | |
88 if (!isOpen_) | |
89 { | |
90 LOG(ERROR) << "PostgreSQL: Attempting to roll back a nonexistent transaction. " | |
91 << "Did you remember to call Begin()?"; | |
92 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
93 } | |
94 | |
95 database_.Execute("COMMIT"); | |
96 isOpen_ = false; | |
97 } | |
98 | |
99 | |
100 IResult* PostgreSQLTransaction::Execute(IPrecompiledStatement& statement, | |
101 const Dictionary& parameters) | |
102 { | |
103 std::auto_ptr<IResult> result(dynamic_cast<PostgreSQLStatement&>(statement).Execute(*this, parameters)); | |
104 | |
105 if (!statement.IsReadOnly()) | |
106 { | |
107 readOnly_ = false; | |
108 } | |
109 | |
110 return result.release(); | |
111 } | |
112 | |
113 | |
114 void PostgreSQLTransaction::ExecuteWithoutResult(IPrecompiledStatement& statement, | |
115 const Dictionary& parameters) | |
116 { | |
117 dynamic_cast<PostgreSQLStatement&>(statement).ExecuteWithoutResult(*this, parameters); | |
118 | |
119 if (!statement.IsReadOnly()) | |
120 { | |
121 readOnly_ = false; | |
122 } | |
123 } | |
124 } |