comparison Framework/MySQL/MySQLTransaction.cpp @ 0:7cea966b6829

initial commit
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 04 Jul 2018 08:16:29 +0200
parents
children 9e419261f1c9
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 "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 {
38 db_.Execute("START TRANSACTION");
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 {
51 db_.Execute("ROLLBACK");
52 }
53 catch (Orthanc::OrthancException&)
54 {
55 }
56 }
57 }
58
59
60 void MySQLTransaction::Rollback()
61 {
62 if (active_)
63 {
64 db_.Execute("ROLLBACK");
65 active_ = false;
66 readOnly_ = true;
67 }
68 else
69 {
70 LOG(ERROR) << "MySQL: This transaction is already finished";
71 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
72 }
73 }
74
75
76 void MySQLTransaction::Commit()
77 {
78 if (active_)
79 {
80 db_.Execute("COMMIT");
81 active_ = false;
82 readOnly_ = true;
83 }
84 else
85 {
86 LOG(ERROR) << "MySQL: This transaction is already finished";
87 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
88 }
89 }
90
91
92 IResult* MySQLTransaction::Execute(IPrecompiledStatement& statement,
93 const Dictionary& parameters)
94 {
95 std::auto_ptr<IResult> result(dynamic_cast<MySQLStatement&>(statement).Execute(*this, parameters));
96
97 if (!statement.IsReadOnly())
98 {
99 readOnly_ = false;
100 }
101
102 return result.release();
103 }
104
105
106 void MySQLTransaction::ExecuteWithoutResult(IPrecompiledStatement& statement,
107 const Dictionary& parameters)
108 {
109 dynamic_cast<MySQLStatement&>(statement).ExecuteWithoutResult(*this, parameters);
110
111 if (!statement.IsReadOnly())
112 {
113 readOnly_ = false;
114 }
115 }
116 }