Mercurial > hg > orthanc-databases
annotate Framework/MySQL/MySQLTransaction.cpp @ 96:a060f97a1f72 db-changes
cppcheck
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 21 Jan 2019 14:37:32 +0100 |
parents | 714c5d2bee76 |
children | 4cd7e45b671e |
rev | line source |
---|---|
0 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
67 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
0 | 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 "MySQLTransaction.h" | |
23 | |
24 #include "MySQLStatement.h" | |
25 | |
26 #include <Core/Logging.h> | |
27 #include <Core/OrthancException.h> | |
28 | |
29 #include <memory> | |
30 | |
31 namespace OrthancDatabases | |
32 { | |
33 MySQLTransaction::MySQLTransaction(MySQLDatabase& db) : | |
34 db_(db), | |
35 readOnly_(true), | |
36 active_(false) | |
37 { | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
38 db_.Execute("START TRANSACTION", false); |
0 | 39 active_ = true; |
40 } | |
41 | |
42 | |
43 MySQLTransaction::~MySQLTransaction() | |
44 { | |
45 if (active_) | |
46 { | |
47 LOG(WARNING) << "An active MySQL transaction was dismissed"; | |
48 | |
49 try | |
50 { | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
51 db_.Execute("ROLLBACK", false); |
0 | 52 } |
53 catch (Orthanc::OrthancException&) | |
54 { | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
55 // Ignore possible exceptions due to connection loss |
0 | 56 } |
57 } | |
58 } | |
59 | |
60 | |
61 void MySQLTransaction::Rollback() | |
62 { | |
63 if (active_) | |
64 { | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
65 db_.Execute("ROLLBACK", false); |
0 | 66 active_ = false; |
67 readOnly_ = true; | |
68 } | |
69 else | |
70 { | |
71 LOG(ERROR) << "MySQL: This transaction is already finished"; | |
72 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
73 } | |
74 } | |
75 | |
76 | |
77 void MySQLTransaction::Commit() | |
78 { | |
79 if (active_) | |
80 { | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
81 db_.Execute("COMMIT", false); |
0 | 82 active_ = false; |
83 readOnly_ = true; | |
84 } | |
85 else | |
86 { | |
87 LOG(ERROR) << "MySQL: This transaction is already finished"; | |
88 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
89 } | |
90 } | |
91 | |
92 | |
93 IResult* MySQLTransaction::Execute(IPrecompiledStatement& statement, | |
94 const Dictionary& parameters) | |
95 { | |
96 std::auto_ptr<IResult> result(dynamic_cast<MySQLStatement&>(statement).Execute(*this, parameters)); | |
97 | |
98 if (!statement.IsReadOnly()) | |
99 { | |
100 readOnly_ = false; | |
101 } | |
102 | |
103 return result.release(); | |
104 } | |
105 | |
106 | |
107 void MySQLTransaction::ExecuteWithoutResult(IPrecompiledStatement& statement, | |
108 const Dictionary& parameters) | |
109 { | |
110 dynamic_cast<MySQLStatement&>(statement).ExecuteWithoutResult(*this, parameters); | |
111 | |
112 if (!statement.IsReadOnly()) | |
113 { | |
114 readOnly_ = false; | |
115 } | |
116 } | |
117 } |