Mercurial > hg > orthanc-databases
annotate Framework/MySQL/MySQLTransaction.cpp @ 189:b968e7bfa7f9
fix build against mariadb client
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 16 Dec 2020 15:49:32 +0100 |
parents | 275e14f57f1e |
children | 3236894320d6 |
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 | |
140
4cd7e45b671e
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
5 * Copyright (C) 2017-2020 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 | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
26 #include <Compatibility.h> // For std::unique_ptr<> |
152 | 27 #include <Logging.h> |
28 #include <OrthancException.h> | |
0 | 29 |
30 #include <memory> | |
31 | |
32 namespace OrthancDatabases | |
33 { | |
34 MySQLTransaction::MySQLTransaction(MySQLDatabase& db) : | |
35 db_(db), | |
36 readOnly_(true), | |
37 active_(false) | |
38 { | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
39 db_.Execute("START TRANSACTION", false); |
0 | 40 active_ = true; |
41 } | |
42 | |
43 | |
44 MySQLTransaction::~MySQLTransaction() | |
45 { | |
46 if (active_) | |
47 { | |
48 LOG(WARNING) << "An active MySQL transaction was dismissed"; | |
49 | |
50 try | |
51 { | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
52 db_.Execute("ROLLBACK", false); |
0 | 53 } |
54 catch (Orthanc::OrthancException&) | |
55 { | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
56 // Ignore possible exceptions due to connection loss |
0 | 57 } |
58 } | |
59 } | |
60 | |
61 | |
62 void MySQLTransaction::Rollback() | |
63 { | |
64 if (active_) | |
65 { | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
66 db_.Execute("ROLLBACK", false); |
0 | 67 active_ = false; |
68 readOnly_ = true; | |
69 } | |
70 else | |
71 { | |
72 LOG(ERROR) << "MySQL: This transaction is already finished"; | |
73 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
74 } | |
75 } | |
76 | |
77 | |
78 void MySQLTransaction::Commit() | |
79 { | |
80 if (active_) | |
81 { | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
82 db_.Execute("COMMIT", false); |
0 | 83 active_ = false; |
84 readOnly_ = true; | |
85 } | |
86 else | |
87 { | |
88 LOG(ERROR) << "MySQL: This transaction is already finished"; | |
89 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
90 } | |
91 } | |
92 | |
93 | |
94 IResult* MySQLTransaction::Execute(IPrecompiledStatement& statement, | |
95 const Dictionary& parameters) | |
96 { | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
97 std::unique_ptr<IResult> result(dynamic_cast<MySQLStatement&>(statement).Execute(*this, parameters)); |
0 | 98 |
99 if (!statement.IsReadOnly()) | |
100 { | |
101 readOnly_ = false; | |
102 } | |
103 | |
104 return result.release(); | |
105 } | |
106 | |
107 | |
108 void MySQLTransaction::ExecuteWithoutResult(IPrecompiledStatement& statement, | |
109 const Dictionary& parameters) | |
110 { | |
111 dynamic_cast<MySQLStatement&>(statement).ExecuteWithoutResult(*this, parameters); | |
112 | |
113 if (!statement.IsReadOnly()) | |
114 { | |
115 readOnly_ = false; | |
116 } | |
117 } | |
118 } |