annotate Framework/MySQL/MySQLDatabase.cpp @ 583:ae7375d38607 find-refactoring tip

MySQL: fix ordering
author Alain Mazy <am@orthanc.team>
date Mon, 21 Oct 2024 18:19:51 +0200
parents 54d518dcd74a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Orthanc - A Lightweight, RESTful DICOM Store
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
507
54d518dcd74a updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 459
diff changeset
5 * Copyright (C) 2017-2023 Osimis S.A., Belgium
54d518dcd74a updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 459
diff changeset
6 * Copyright (C) 2024-2024 Orthanc Team SRL, Belgium
459
ecd0b719cff5 update year to 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 389
diff changeset
7 * Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * This program is free software: you can redistribute it and/or
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * modify it under the terms of the GNU Affero General Public License
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 * as published by the Free Software Foundation, either version 3 of
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * the License, or (at your option) any later version.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * This program is distributed in the hope that it will be useful, but
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * Affero General Public License for more details.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 *
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 * You should have received a copy of the GNU Affero General Public License
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21 **/
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
23
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
24 #include "MySQLDatabase.h"
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
25
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
26 #include "../Common/ImplicitTransaction.h"
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
27 #include "../Common/Integer64Value.h"
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
28 #include "../Common/RetryDatabaseFactory.h"
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29 #include "MySQLResult.h"
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 #include "MySQLStatement.h"
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 #include "MySQLTransaction.h"
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32
157
275e14f57f1e replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 152
diff changeset
33 #include <Compatibility.h> // For std::unique_ptr<>
152
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 144
diff changeset
34 #include <Logging.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 144
diff changeset
35 #include <OrthancException.h>
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 144
diff changeset
36 #include <Toolbox.h>
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37
6
5a97c68a7a51 fix mysql msvc
jodogne
parents: 0
diff changeset
38 #include <errmsg.h>
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 #include <mysqld_error.h>
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 #include <memory>
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
42 #include <boost/thread.hpp>
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 namespace OrthancDatabases
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46 void MySQLDatabase::Close()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
47 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 if (mysql_ != NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 LOG(INFO) << "Closing connection to MySQL database";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 mysql_close(mysql_);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 mysql_ = NULL;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
53 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
54 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
55
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56
240
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
57 void MySQLDatabase::ThrowException()
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
58 {
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
59 LogError();
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
60
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
61 unsigned int error = mysql_errno(mysql_);
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
62 if (error == CR_SERVER_GONE_ERROR ||
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
63 error == CR_SERVER_LOST ||
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
64 error == ER_QUERY_INTERRUPTED)
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
65 {
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
66 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable);
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
67 }
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
68 else if (error == CR_COMMANDS_OUT_OF_SYNC)
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
69 {
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
70 #if !defined(MARIADB_VERSION_ID)
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
71 LOG(ERROR) << "TODO - This error seems to be related to the use of libmysqlclient: Try to switch to mariadb-connector";
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
72 #endif
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
73
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
74 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable);
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
75 }
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
76 else if (error == ER_LOCK_DEADLOCK)
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
77 {
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
78 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 9, 2)
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
79 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseCannotSerialize);
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
80 #else
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
81 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database, "Collision between multiple writers");
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
82 #endif
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
83 }
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
84 else
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
85 {
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
86 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
87 }
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
88 }
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
89
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
90
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
91 void MySQLDatabase::CheckErrorCode(int code)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
92 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
93 if (code == 0)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
94 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
95 return;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
96 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
97 else
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
98 {
240
c82c2cf84ae8 added handling of CR_COMMANDS_OUT_OF_SYNC
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 237
diff changeset
99 ThrowException();
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
100 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
104 MySQLDatabase::MySQLDatabase(const MySQLParameters& parameters) :
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
105 parameters_(parameters),
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
106 mysql_(NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
107 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
108 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
109
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
110
46
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
111 MySQLDatabase::~MySQLDatabase()
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
112 {
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
113 try
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
114 {
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
115 Close();
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
116 }
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
117 catch (Orthanc::OrthancException&)
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
118 {
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
119 // Ignore possible exceptions due to connection loss
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
120 }
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
121 }
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
122
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
123
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
124 void MySQLDatabase::LogError()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
125 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
126 if (mysql_ != NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
127 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
128 LOG(ERROR) << "MySQL error (" << mysql_errno(mysql_)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
129 << "," << mysql_sqlstate(mysql_)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
130 << "): " << mysql_error(mysql_);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
131 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
132 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
133
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
134
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
135 MYSQL* MySQLDatabase::GetObject()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
136 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
137 if (mysql_ == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
138 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
139 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
140 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
141 else
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
142 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
143 return mysql_;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
144 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
145 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
146
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
147
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
148 void MySQLDatabase::OpenInternal(const char* db)
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
149 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
150 if (mysql_ != NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
151 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
152 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
153 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
154
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
155 mysql_ = mysql_init(NULL);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
156 if (mysql_ == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
157 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
158 LOG(ERROR) << "Cannot initialize the MySQL connector";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
159 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
160 }
46
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
161
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
162 if (parameters_.GetUnixSocket().empty())
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
163 {
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
164 // Fallback to TCP connection if no UNIX socket is provided
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
165 unsigned int protocol = MYSQL_PROTOCOL_TCP;
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
166 mysql_options(mysql_, MYSQL_OPT_PROTOCOL, (unsigned int *) &protocol);
6a574d810b98 Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 24
diff changeset
167 }
163
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
168
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
169 if (parameters_.IsSsl())
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
170 {
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
171 if (parameters_.IsVerifyServerCertificates())
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
172 {
187
0b78198c28d6 compatibility with mysql client 8.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 186
diff changeset
173 #if (MYSQL_VERSION_ID > 50110 && MYSQL_VERSION_ID < 80000) // Removed in MySQL client 8.0
163
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
174 my_bool verifyCert = 1;
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
175 mysql_options(mysql_, MYSQL_OPT_SSL_VERIFY_SERVER_CERT, (void *) &verifyCert);
187
0b78198c28d6 compatibility with mysql client 8.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 186
diff changeset
176 #endif
0b78198c28d6 compatibility with mysql client 8.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 186
diff changeset
177
165
0632c2408af3 fix compatiblity with mysql client
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 163
diff changeset
178 mysql_options(mysql_, MYSQL_OPT_SSL_CA, (void *)(parameters_.GetSslCaCertificates()));
163
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
179 }
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
180
187
0b78198c28d6 compatibility with mysql client 8.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 186
diff changeset
181 #if (MYSQL_VERSION_ID > 50110 && MYSQL_VERSION_ID < 80000) // Removed in MySQL client 8.0
163
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
182 my_bool enforceTls = 1;
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
183 mysql_options(mysql_, MYSQL_OPT_SSL_ENFORCE, (void *) &enforceTls);
187
0b78198c28d6 compatibility with mysql client 8.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 186
diff changeset
184 #endif
163
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
185 }
4d32c9c8d6c0 Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents: 157
diff changeset
186
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
187 const char* socket = (parameters_.GetUnixSocket().empty() ? NULL :
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
188 parameters_.GetUnixSocket().c_str());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
189
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
190 if (mysql_real_connect(mysql_,
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
191 parameters_.GetHost().c_str(),
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
192 parameters_.GetUsername().c_str(),
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
193 parameters_.GetPassword().c_str(), db,
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
194 parameters_.GetPort(), socket, 0) == 0)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
195 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
196 LogError();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
197 Close();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
198 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
199 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
200 else
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
201 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
202 LOG(INFO) << "Successful connection to MySQL database";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
203 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
204
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
205 if (mysql_set_character_set(mysql_, "utf8mb4") != 0)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
206 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
207 LOG(ERROR) << "Cannot set the character set to UTF8";
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
208 Close();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
209 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
210 }
16
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
211 }
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
212
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
213
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
214 void MySQLDatabase::Open()
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
215 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
216 if (parameters_.GetDatabase().empty())
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
217 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
218 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
219 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
220 else
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
221 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
222 OpenInternal(parameters_.GetDatabase().c_str());
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
223 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
224 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
225
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
226
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
227 void MySQLDatabase::ClearDatabase(const MySQLParameters& parameters)
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
228 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
229 MySQLDatabase db(parameters);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
230 db.OpenRoot();
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
231
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
232 const std::string& database = parameters.GetDatabase();
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
233
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
234 {
217
ee5858d438dc TransactionType given to MySQLTransaction constructor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
235 MySQLTransaction t(db, TransactionType_ReadWrite);
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
236
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
237 if (!db.DoesDatabaseExist(t, database))
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
238 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
239 LOG(ERROR) << "Inexistent database, please create it first: " << database;
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
240 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
241 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
242
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
243 db.ExecuteMultiLines("DROP DATABASE " + database, false);
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
244 db.ExecuteMultiLines("CREATE DATABASE " + database, false);
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
245 t.Commit();
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
246 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
247 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
248
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
249
16
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
250 namespace
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
251 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
252 class ResultWrapper : public boost::noncopyable
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
253 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
254 private:
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
255 MYSQL_RES *result_;
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
256
16
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
257 public:
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
258 ResultWrapper(MySQLDatabase& mysql,
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
259 const std::string& sql) :
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
260 result_(NULL)
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
261 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
262 if (mysql_real_query(mysql.GetObject(), sql.c_str(), sql.size()))
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
263 {
16
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
264 mysql.LogError();
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
265 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
266 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
267
16
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
268 result_ = mysql_use_result(mysql.GetObject());
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
269 if (result_ == NULL)
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
270 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
271 mysql.LogError();
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
272 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
273 }
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
274 }
16
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
275
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
276 ~ResultWrapper()
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
277 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
278 if (result_ != NULL)
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
279 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
280 mysql_free_result(result_);
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
281 result_ = NULL;
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
282 }
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
283 }
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
284
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
285 MYSQL_RES *GetObject()
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
286 {
16
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
287 return result_;
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
288 }
16
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
289 };
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
290 }
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
291
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
292
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
293 bool MySQLDatabase::LookupGlobalStringVariable(std::string& value,
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
294 const std::string& variable)
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
295 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
296 ResultWrapper result(*this, "SELECT @@global." + variable);
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
297
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
298 MYSQL_ROW row = mysql_fetch_row(result.GetObject());
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
299 if (mysql_errno(mysql_) == 0 &&
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
300 row &&
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
301 row[0])
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
302 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
303 value = std::string(row[0]);
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
304 return true;
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
305 }
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
306 else
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
307 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
308 return false;
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
309 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
310 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
311
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
312
16
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
313 bool MySQLDatabase::LookupGlobalIntegerVariable(int64_t& value,
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
314 const std::string& variable)
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
315 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
316 std::string s;
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
317
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
318 if (LookupGlobalStringVariable(s, variable))
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
319 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
320 try
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
321 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
322 value = boost::lexical_cast<int64_t>(s);
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
323 return true;
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
324 }
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
325 catch (boost::bad_lexical_cast&)
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
326 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
327 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
328 }
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
329 }
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
330 else
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
331 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
332 return false;
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
333 }
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
334 }
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
335
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
336
137
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
337 bool MySQLDatabase::RunAdvisoryLockStatement(Query& query,
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
338 const std::string& lock)
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
339 {
137
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
340 const std::string& dbName = parameters_.GetDatabase();
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
341
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
342 // Prepend the name of the lock by the database name. This allows
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
343 // to create a namespace for advisory locks:
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
344 // https://groups.google.com/d/msg/orthanc-users/yV3LSTh_TjI/MQIcvnMlAQAJ
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
345 std::string prefix;
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
346 prefix.reserve(dbName.size());
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
347 for (size_t i = 0; i < dbName.size(); i++)
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
348 {
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
349 if (isalnum(dbName[i]) ||
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
350 dbName[i] == '$' ||
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
351 dbName[i] == '_')
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
352 {
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
353 prefix.push_back(dbName[i]);
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
354 }
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
355 }
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
356
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
357 query.SetType("lock", ValueType_Utf8String);
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
358
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
359 Dictionary args;
137
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
360 args.SetUtf8Value("lock", prefix + "." + lock);
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
361
137
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
362 bool success;
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
363
137
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
364 {
186
6fe74f9a516e cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 165
diff changeset
365 MySQLStatement statement(*this, query);
6fe74f9a516e cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 165
diff changeset
366
217
ee5858d438dc TransactionType given to MySQLTransaction constructor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
367 MySQLTransaction t(*this, TransactionType_ReadWrite);
157
275e14f57f1e replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 152
diff changeset
368 std::unique_ptr<IResult> result(t.Execute(statement, args));
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
369
137
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
370 success = (!result->IsDone() &&
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
371 result->GetField(0).GetType() == ValueType_Integer64 &&
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
372 dynamic_cast<const Integer64Value&>(result->GetField(0)).GetValue() == 1);
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
373
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
374 t.Commit();
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
375 }
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
376
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
377 return success;
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
378 }
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
379
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
380
137
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
381 bool MySQLDatabase::AcquireAdvisoryLock(const std::string& lock)
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
382 {
137
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
383 Query query("SELECT GET_LOCK(${lock}, 0)", false);
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
384 return RunAdvisoryLockStatement(query, lock);
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
385 }
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
386
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
387
137
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
388 bool MySQLDatabase::ReleaseAdvisoryLock(const std::string& lock)
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
389 {
137
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
390 Query query("SELECT RELEASE_LOCK(${lock})", false);
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
391 return RunAdvisoryLockStatement(query, lock);
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
392 }
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
393
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
394
137
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
395 void MySQLDatabase::AdvisoryLock(const std::string& lock)
16
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
396 {
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
397 if (!AcquireAdvisoryLock(lock))
16
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
398 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
399 LOG(ERROR) << "The MySQL database is locked by another instance of Orthanc";
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
400 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database);
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
401 }
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
402 }
22
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
403
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
404
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
405 bool MySQLDatabase::DoesTableExist(MySQLTransaction& transaction,
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
406 const std::string& name)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
407 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
408 if (mysql_ == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
409 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
410 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
411 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
412
60
412e30336847 allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 46
diff changeset
413 if (!IsValidDatabaseIdentifier(name))
24
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
414 {
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
415 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
416 }
22
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
417
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
418 Query query("SELECT COUNT(*) FROM information_schema.TABLES WHERE "
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
419 "(TABLE_SCHEMA = ${database}) AND (TABLE_NAME = ${table})", true);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
420 query.SetType("database", ValueType_Utf8String);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
421 query.SetType("table", ValueType_Utf8String);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
422
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
423 MySQLStatement statement(*this, query);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
424
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
425 Dictionary args;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
426 args.SetUtf8Value("database", parameters_.GetDatabase());
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
427 args.SetUtf8Value("table", name);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
428
157
275e14f57f1e replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 152
diff changeset
429 std::unique_ptr<IResult> result(statement.Execute(transaction, args));
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
430 return (!result->IsDone() &&
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
431 result->GetFieldsCount() == 1 &&
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
432 result->GetField(0).GetType() == ValueType_Integer64 &&
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
433 dynamic_cast<const Integer64Value&>(result->GetField(0)).GetValue() == 1);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
434 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
435
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
436
22
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
437 bool MySQLDatabase::DoesDatabaseExist(MySQLTransaction& transaction,
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
438 const std::string& name)
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
439 {
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
440 if (mysql_ == NULL)
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
441 {
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
442 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
443 }
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
444
60
412e30336847 allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 46
diff changeset
445 if (!IsValidDatabaseIdentifier(name))
24
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
446 {
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
447 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
448 }
22
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
449
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
450 Query query("SELECT COUNT(*) FROM information_schema.SCHEMATA "
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
451 "WHERE SCHEMA_NAME = ${database}", true);
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
452 query.SetType("database", ValueType_Utf8String);
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
453
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
454 MySQLStatement statement(*this, query);
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
455
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
456 Dictionary args;
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
457 args.SetUtf8Value("database", name);
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
458
157
275e14f57f1e replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 152
diff changeset
459 std::unique_ptr<IResult> result(statement.Execute(transaction, args));
22
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
460 return (!result->IsDone() &&
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
461 result->GetFieldsCount() == 1 &&
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
462 result->GetField(0).GetType() == ValueType_Integer64 &&
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
463 dynamic_cast<const Integer64Value&>(result->GetField(0)).GetValue() == 1);
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
464 }
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
465
1e9bad493475 prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 16
diff changeset
466
144
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
467 bool MySQLDatabase::DoesTriggerExist(MySQLTransaction& transaction,
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
468 const std::string& name)
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
469 {
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
470 if (mysql_ == NULL)
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
471 {
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
472 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
473 }
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
474
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
475 if (!IsValidDatabaseIdentifier(name))
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
476 {
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
477 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
478 }
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
479
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
480 Query query("SELECT COUNT(*) FROM information_schema.TRIGGERS "
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
481 "WHERE TRIGGER_NAME = ${trigger}", true);
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
482 query.SetType("trigger", ValueType_Utf8String);
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
483
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
484 MySQLStatement statement(*this, query);
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
485
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
486 Dictionary args;
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
487 args.SetUtf8Value("trigger", name);
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
488
157
275e14f57f1e replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 152
diff changeset
489 std::unique_ptr<IResult> result(statement.Execute(transaction, args));
144
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
490 return (!result->IsDone() &&
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
491 result->GetFieldsCount() == 1 &&
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
492 result->GetField(0).GetType() == ValueType_Integer64 &&
186
6fe74f9a516e cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 165
diff changeset
493 dynamic_cast<const Integer64Value&>(result->GetField(0)).GetValue() != 0);
144
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
494 }
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
495
740d9829f52e handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 140
diff changeset
496
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
497 void MySQLDatabase::ExecuteMultiLines(const std::string& sql,
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
498 bool arobaseSeparator)
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
499 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
500 if (mysql_ == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
501 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
502 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
503 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
504
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
505 // This emulates the behavior of "CLIENT_MULTI_STATEMENTS" in
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
506 // "mysql_real_connect()", avoiding to implement a loop over
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
507 // "mysql_query()"
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
508 std::vector<std::string> commands;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
509 Orthanc::Toolbox::TokenizeString(commands, sql, ';');
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
510
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
511 for (size_t i = 0; i < commands.size(); i++)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
512 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
513 std::string s = Orthanc::Toolbox::StripSpaces(commands[i]);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
514
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
515 if (!s.empty())
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
516 {
16
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
517 if (arobaseSeparator)
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
518 {
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
519 // Replace the escape character "@" by a semicolon
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
520 std::replace(s.begin(), s.end(), '@', ';');
9e419261f1c9 mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 6
diff changeset
521 }
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
522
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
523 LOG(TRACE) << "MySQL: " << s;
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
524 CheckErrorCode(mysql_query(mysql_, s.c_str()));
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
525 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
526 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
527 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
528
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
529
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
530 IPrecompiledStatement* MySQLDatabase::Compile(const Query& query)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
531 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
532 if (mysql_ == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
533 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
534 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
535 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
536
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
537 return new MySQLStatement(*this, query);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
538 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
539
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
540
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
541
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
542 namespace
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
543 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
544 class MySQLImplicitTransaction : public ImplicitTransaction
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
545 {
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
546 private:
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
547 MySQLDatabase& db_;
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
548
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
549 protected:
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
550 virtual IResult* ExecuteInternal(IPrecompiledStatement& statement,
280
b8ba198a8df3 adding missing ORTHANC_OVERRIDE
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
551 const Dictionary& parameters) ORTHANC_OVERRIDE
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
552 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
553 return dynamic_cast<MySQLStatement&>(statement).Execute(*this, parameters);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
554 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
555
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
556 virtual void ExecuteWithoutResultInternal(IPrecompiledStatement& statement,
280
b8ba198a8df3 adding missing ORTHANC_OVERRIDE
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 255
diff changeset
557 const Dictionary& parameters) ORTHANC_OVERRIDE
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
558 {
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
559 dynamic_cast<MySQLStatement&>(statement).ExecuteWithoutResult(*this, parameters);
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
560 }
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
561
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
562 public:
252
33fa478c119a cppcheck
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 240
diff changeset
563 explicit MySQLImplicitTransaction(MySQLDatabase& db) :
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
564 db_(db)
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
565 {
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
566 }
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
567
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
568 virtual bool DoesTableExist(const std::string& name) ORTHANC_OVERRIDE
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
569 {
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
570 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "An explicit transaction is needed");
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
571 }
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
572
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
573 virtual bool DoesTriggerExist(const std::string& name) ORTHANC_OVERRIDE
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
574 {
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
575 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError, "An explicit transaction is needed");
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
576 }
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
577
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
578 virtual void ExecuteMultiLines(const std::string& query) ORTHANC_OVERRIDE
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
579 {
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
580 db_.ExecuteMultiLines(query, false /* don't deal with arobases */);
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
581 }
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
582 };
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
583 }
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
584
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
585
215
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
586 ITransaction* MySQLDatabase::CreateTransaction(TransactionType type)
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
587 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
588 if (mysql_ == NULL)
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
589 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
590 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
591 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
592
215
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
593 switch (type)
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
594 {
215
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
595 case TransactionType_Implicit:
237
35598014f140 refactoring to remove GlobalProperties.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 236
diff changeset
596 return new MySQLImplicitTransaction(*this);
215
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
597
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
598 case TransactionType_ReadOnly:
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
599 case TransactionType_ReadWrite:
217
ee5858d438dc TransactionType given to MySQLTransaction constructor
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 215
diff changeset
600 return new MySQLTransaction(*this, type);
215
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
601
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
602 default:
b40b30075c51 added TransactionType_Implicit
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 193
diff changeset
603 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange);
23
b2ff1cd2907a handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 22
diff changeset
604 }
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
605 }
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
606
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
607
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
608 void MySQLDatabase::GlobalFinalization()
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
609 {
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
610 mysql_library_end();
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
611 }
24
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
612
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
613
60
412e30336847 allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 46
diff changeset
614 bool MySQLDatabase::IsValidDatabaseIdentifier(const std::string& s)
24
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
615 {
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
616 for (size_t i = 0; i < s.length(); i++)
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
617 {
60
412e30336847 allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 46
diff changeset
618 // https://dev.mysql.com/doc/refman/8.0/en/identifiers.html
412e30336847 allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 46
diff changeset
619 if (!isalnum(s[i]) &&
412e30336847 allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 46
diff changeset
620 s[i] != '$' &&
412e30336847 allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 46
diff changeset
621 s[i] != '_')
24
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
622 {
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
623 return false;
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
624 }
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
625 }
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
626
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
627 return true;
17f849b2af34 sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 23
diff changeset
628 }
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
629
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
630
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
631 MySQLDatabase::TransientAdvisoryLock::
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
632 TransientAdvisoryLock(MySQLDatabase& database,
137
52b3859ee0b7 MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 135
diff changeset
633 const std::string& lock) :
135
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
634 database_(database),
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
635 lock_(lock)
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
636 {
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
637 bool locked = true;
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
638
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
639 for (unsigned int i = 0; i < 10; i++)
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
640 {
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
641 if (database_.AcquireAdvisoryLock(lock_))
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
642 {
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
643 locked = false;
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
644 break;
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
645 }
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
646 else
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
647 {
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
648 boost::this_thread::sleep(boost::posix_time::milliseconds(500));
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
649 }
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
650 }
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
651
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
652 if (locked)
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
653 {
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
654 LOG(ERROR) << "Cannot acquire a transient advisory lock";
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
655 throw Orthanc::OrthancException(Orthanc::ErrorCode_Plugin);
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
656 }
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
657 }
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
658
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
659
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
660 MySQLDatabase::TransientAdvisoryLock::~TransientAdvisoryLock()
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
661 {
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
662 database_.ReleaseAdvisoryLock(lock_);
e26690365c25 MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 67
diff changeset
663 }
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
664
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
665
255
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
666 class MySQLDatabase::Factory : public RetryDatabaseFactory
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
667 {
255
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
668 private:
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
669 MySQLParameters parameters_;
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
670
255
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
671 protected:
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
672 virtual IDatabase* TryOpen()
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
673 {
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
674 std::unique_ptr<MySQLDatabase> db(new MySQLDatabase(parameters_));
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
675 db->Open();
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
676 db->ExecuteMultiLines("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE", false);
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
677 return db.release();
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
678 }
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
679
255
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
680 public:
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
681 explicit Factory(const MySQLParameters& parameters) :
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
682 RetryDatabaseFactory(parameters.GetMaxConnectionRetries(),
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
683 parameters.GetConnectionRetryInterval()),
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
684 parameters_(parameters)
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
685 {
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
686 }
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
687 };
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
688
255
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
689
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
690 IDatabaseFactory* MySQLDatabase::CreateDatabaseFactory(const MySQLParameters& parameters)
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
691 {
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
692 return new Factory(parameters);
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
693 }
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
694
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
695
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
696 MySQLDatabase* MySQLDatabase::CreateDatabaseConnection(const MySQLParameters& parameters)
d663d9e44f8d reintroduction of IDatabaseFactory into DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 252
diff changeset
697 {
226
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
698 Factory factory(parameters);
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
699 return dynamic_cast<MySQLDatabase*>(factory.Open());
a4918d57435c DatabaseManager doesn't IDatabaseFactory anymore
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 217
diff changeset
700 }
0
7cea966b6829 initial commit
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
701 }