Mercurial > hg > orthanc-databases
annotate Framework/MySQL/MySQLDatabase.cpp @ 345:76d53dcfb92b OrthancMySQL-3.0
closing OrthancMySQL-3.0
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 12 Aug 2021 16:08:58 +0200 |
parents | 0b78198c28d6 |
children | 3236894320d6 |
rev | line source |
---|---|
0 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
140
4cd7e45b671e
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
137
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "MySQLDatabase.h" | |
23 | |
24 #include "MySQLResult.h" | |
25 #include "MySQLStatement.h" | |
26 #include "MySQLTransaction.h" | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
27 #include "../Common/ImplicitTransaction.h" |
0 | 28 #include "../Common/Integer64Value.h" |
29 | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
30 #include <Compatibility.h> // For std::unique_ptr<> |
152 | 31 #include <Logging.h> |
32 #include <OrthancException.h> | |
33 #include <Toolbox.h> | |
0 | 34 |
6 | 35 #include <errmsg.h> |
0 | 36 #include <mysqld_error.h> |
37 | |
38 #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
|
39 #include <boost/thread.hpp> |
0 | 40 |
41 namespace OrthancDatabases | |
42 { | |
43 void MySQLDatabase::Close() | |
44 { | |
45 if (mysql_ != NULL) | |
46 { | |
47 LOG(INFO) << "Closing connection to MySQL database"; | |
48 mysql_close(mysql_); | |
49 mysql_ = NULL; | |
50 } | |
51 } | |
52 | |
53 | |
54 void MySQLDatabase::CheckErrorCode(int code) | |
55 { | |
56 if (code == 0) | |
57 { | |
58 return; | |
59 } | |
60 else | |
61 { | |
62 LogError(); | |
63 | |
64 unsigned int error = mysql_errno(mysql_); | |
65 if (error == CR_SERVER_GONE_ERROR || | |
66 error == CR_SERVER_LOST || | |
67 error == ER_QUERY_INTERRUPTED) | |
68 { | |
69 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable); | |
70 } | |
71 else | |
72 { | |
73 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
74 } | |
75 } | |
76 } | |
77 | |
78 | |
79 MySQLDatabase::MySQLDatabase(const MySQLParameters& parameters) : | |
80 parameters_(parameters), | |
81 mysql_(NULL) | |
82 { | |
83 } | |
84 | |
85 | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
86 MySQLDatabase::~MySQLDatabase() |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
87 { |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
88 try |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
89 { |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
90 Close(); |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
91 } |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
92 catch (Orthanc::OrthancException&) |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
93 { |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
94 // Ignore possible exceptions due to connection loss |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
95 } |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
96 } |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
97 |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
98 |
0 | 99 void MySQLDatabase::LogError() |
100 { | |
101 if (mysql_ != NULL) | |
102 { | |
103 LOG(ERROR) << "MySQL error (" << mysql_errno(mysql_) | |
104 << "," << mysql_sqlstate(mysql_) | |
105 << "): " << mysql_error(mysql_); | |
106 } | |
107 } | |
108 | |
109 | |
110 MYSQL* MySQLDatabase::GetObject() | |
111 { | |
112 if (mysql_ == NULL) | |
113 { | |
114 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
115 } | |
116 else | |
117 { | |
118 return mysql_; | |
119 } | |
120 } | |
121 | |
122 | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
123 void MySQLDatabase::OpenInternal(const char* db) |
0 | 124 { |
125 if (mysql_ != NULL) | |
126 { | |
127 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
128 } | |
129 | |
130 mysql_ = mysql_init(NULL); | |
131 if (mysql_ == NULL) | |
132 { | |
133 LOG(ERROR) << "Cannot initialize the MySQL connector"; | |
134 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
135 } | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
136 |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
137 if (parameters_.GetUnixSocket().empty()) |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
138 { |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
139 // 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
|
140 unsigned int protocol = MYSQL_PROTOCOL_TCP; |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
24
diff
changeset
|
141 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
|
142 } |
163
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
157
diff
changeset
|
143 |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
157
diff
changeset
|
144 if (parameters_.IsSsl()) |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
157
diff
changeset
|
145 { |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
157
diff
changeset
|
146 if (parameters_.IsVerifyServerCertificates()) |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
157
diff
changeset
|
147 { |
187
0b78198c28d6
compatibility with mysql client 8.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
186
diff
changeset
|
148 #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
|
149 my_bool verifyCert = 1; |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
157
diff
changeset
|
150 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
|
151 #endif |
0b78198c28d6
compatibility with mysql client 8.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
186
diff
changeset
|
152 |
165
0632c2408af3
fix compatiblity with mysql client
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
163
diff
changeset
|
153 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
|
154 } |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
157
diff
changeset
|
155 |
187
0b78198c28d6
compatibility with mysql client 8.x
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
186
diff
changeset
|
156 #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
|
157 my_bool enforceTls = 1; |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
157
diff
changeset
|
158 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
|
159 #endif |
163
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
157
diff
changeset
|
160 } |
4d32c9c8d6c0
Added support for TLS connections
Alain Mazy <alain@mazy.be>
parents:
157
diff
changeset
|
161 |
0 | 162 const char* socket = (parameters_.GetUnixSocket().empty() ? NULL : |
163 parameters_.GetUnixSocket().c_str()); | |
164 | |
165 if (mysql_real_connect(mysql_, | |
166 parameters_.GetHost().c_str(), | |
167 parameters_.GetUsername().c_str(), | |
168 parameters_.GetPassword().c_str(), db, | |
169 parameters_.GetPort(), socket, 0) == 0) | |
170 { | |
171 LogError(); | |
172 Close(); | |
173 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable); | |
174 } | |
175 else | |
176 { | |
177 LOG(INFO) << "Successful connection to MySQL database"; | |
178 } | |
179 | |
180 if (mysql_set_character_set(mysql_, "utf8mb4") != 0) | |
181 { | |
182 LOG(ERROR) << "Cannot set the character set to UTF8"; | |
183 Close(); | |
184 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
185 } | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
186 } |
0 | 187 |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
188 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
189 void MySQLDatabase::Open() |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
190 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
191 if (parameters_.GetDatabase().empty()) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
192 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
193 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
194 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
195 else |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
196 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
197 OpenInternal(parameters_.GetDatabase().c_str()); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
198 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
199 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
200 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
201 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
202 void MySQLDatabase::ClearDatabase(const MySQLParameters& parameters) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
203 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
204 MySQLDatabase db(parameters); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
205 db.OpenRoot(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
206 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
207 const std::string& database = parameters.GetDatabase(); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
208 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
209 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
210 MySQLTransaction t(db); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
211 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
212 if (!db.DoesDatabaseExist(t, database)) |
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 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
|
215 throw Orthanc::OrthancException(Orthanc::ErrorCode_UnknownResource); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
216 } |
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 db.Execute("DROP DATABASE " + database, false); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
219 db.Execute("CREATE DATABASE " + database, false); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
220 t.Commit(); |
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 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
223 |
0 | 224 |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
225 namespace |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
226 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
227 class ResultWrapper : public boost::noncopyable |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
228 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
229 private: |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
230 MYSQL_RES *result_; |
0 | 231 |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
232 public: |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
233 ResultWrapper(MySQLDatabase& mysql, |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
234 const std::string& sql) : |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
235 result_(NULL) |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
236 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
237 if (mysql_real_query(mysql.GetObject(), sql.c_str(), sql.size())) |
0 | 238 { |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
239 mysql.LogError(); |
0 | 240 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
241 } | |
242 | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
243 result_ = mysql_use_result(mysql.GetObject()); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
244 if (result_ == NULL) |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
245 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
246 mysql.LogError(); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
247 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
248 } |
0 | 249 } |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
250 |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
251 ~ResultWrapper() |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
252 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
253 if (result_ != NULL) |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
254 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
255 mysql_free_result(result_); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
256 result_ = NULL; |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
257 } |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
258 } |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
259 |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
260 MYSQL_RES *GetObject() |
0 | 261 { |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
262 return result_; |
0 | 263 } |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
264 }; |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
265 } |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
266 |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
267 |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
268 bool MySQLDatabase::LookupGlobalStringVariable(std::string& value, |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
269 const std::string& variable) |
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 ResultWrapper result(*this, "SELECT @@global." + variable); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
272 |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
273 MYSQL_ROW row = mysql_fetch_row(result.GetObject()); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
274 if (mysql_errno(mysql_) == 0 && |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
275 row && |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
276 row[0]) |
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 value = std::string(row[0]); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
279 return true; |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
280 } |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
281 else |
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 return false; |
0 | 284 } |
285 } | |
286 | |
287 | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
288 bool MySQLDatabase::LookupGlobalIntegerVariable(int64_t& value, |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
289 const std::string& variable) |
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 std::string s; |
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 if (LookupGlobalStringVariable(s, variable)) |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
294 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
295 try |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
296 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
297 value = boost::lexical_cast<int64_t>(s); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
298 return true; |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
299 } |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
300 catch (boost::bad_lexical_cast&) |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
301 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
302 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
303 } |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
304 } |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
305 else |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
306 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
307 return false; |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
308 } |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
309 } |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
310 |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
311 |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
312 bool MySQLDatabase::RunAdvisoryLockStatement(Query& query, |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
313 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
|
314 { |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
315 const std::string& dbName = parameters_.GetDatabase(); |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
316 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
317 // 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
|
318 // 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
|
319 // 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
|
320 std::string prefix; |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
321 prefix.reserve(dbName.size()); |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
322 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
|
323 { |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
324 if (isalnum(dbName[i]) || |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
325 dbName[i] == '$' || |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
326 dbName[i] == '_') |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
327 { |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
328 prefix.push_back(dbName[i]); |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
329 } |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
330 } |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
331 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
332 query.SetType("lock", ValueType_Utf8String); |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
333 |
135
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
334 Dictionary args; |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
335 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
|
336 |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
337 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
|
338 |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
339 { |
186 | 340 MySQLStatement statement(*this, query); |
341 | |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
342 MySQLTransaction t(*this); |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
343 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
|
344 |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
345 success = (!result->IsDone() && |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
346 result->GetField(0).GetType() == ValueType_Integer64 && |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
347 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
|
348 |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
349 t.Commit(); |
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
350 } |
135
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
351 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
352 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
|
353 } |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
354 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
355 |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
356 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
|
357 { |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
358 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
|
359 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
|
360 } |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
361 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
362 |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
363 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
|
364 { |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
365 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
|
366 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
|
367 } |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
368 |
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 void MySQLDatabase::AdvisoryLock(const std::string& lock) |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
371 { |
135
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
372 if (!AcquireAdvisoryLock(lock)) |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
373 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
374 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
|
375 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
376 } |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
377 } |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
378 |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
379 |
0 | 380 bool MySQLDatabase::DoesTableExist(MySQLTransaction& transaction, |
381 const std::string& name) | |
382 { | |
383 if (mysql_ == NULL) | |
384 { | |
385 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
386 } | |
387 | |
60
412e30336847
allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
46
diff
changeset
|
388 if (!IsValidDatabaseIdentifier(name)) |
24
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
389 { |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
390 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
391 } |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
392 |
0 | 393 Query query("SELECT COUNT(*) FROM information_schema.TABLES WHERE " |
394 "(TABLE_SCHEMA = ${database}) AND (TABLE_NAME = ${table})", true); | |
395 query.SetType("database", ValueType_Utf8String); | |
396 query.SetType("table", ValueType_Utf8String); | |
397 | |
398 MySQLStatement statement(*this, query); | |
399 | |
400 Dictionary args; | |
401 args.SetUtf8Value("database", parameters_.GetDatabase()); | |
402 args.SetUtf8Value("table", name); | |
403 | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
404 std::unique_ptr<IResult> result(statement.Execute(transaction, args)); |
0 | 405 return (!result->IsDone() && |
406 result->GetFieldsCount() == 1 && | |
407 result->GetField(0).GetType() == ValueType_Integer64 && | |
408 dynamic_cast<const Integer64Value&>(result->GetField(0)).GetValue() == 1); | |
409 } | |
410 | |
411 | |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
412 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
|
413 const std::string& name) |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
414 { |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
415 if (mysql_ == NULL) |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
416 { |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
417 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
|
418 } |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
419 |
60
412e30336847
allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
46
diff
changeset
|
420 if (!IsValidDatabaseIdentifier(name)) |
24
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
421 { |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
422 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
423 } |
22
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
424 |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
425 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
|
426 "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
|
427 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
|
428 |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
429 MySQLStatement statement(*this, query); |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
430 |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
431 Dictionary args; |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
432 args.SetUtf8Value("database", name); |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
433 |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
434 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
|
435 return (!result->IsDone() && |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
436 result->GetFieldsCount() == 1 && |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
437 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
|
438 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
|
439 } |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
440 |
1e9bad493475
prevent running unit tests on a non-existing db
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
16
diff
changeset
|
441 |
144
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
442 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
|
443 const std::string& name) |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
444 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
445 if (mysql_ == NULL) |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
446 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
447 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
|
448 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
449 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
450 if (!IsValidDatabaseIdentifier(name)) |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
451 { |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
452 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
|
453 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
454 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
455 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
|
456 "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
|
457 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
|
458 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
459 MySQLStatement statement(*this, query); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
460 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
461 Dictionary args; |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
462 args.SetUtf8Value("trigger", name); |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
463 |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
464 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
|
465 return (!result->IsDone() && |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
466 result->GetFieldsCount() == 1 && |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
467 result->GetField(0).GetType() == ValueType_Integer64 && |
186 | 468 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
|
469 } |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
470 |
740d9829f52e
handling of errors if MySQL user cannot CREATE TRIGGER
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
140
diff
changeset
|
471 |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
472 void MySQLDatabase::Execute(const std::string& sql, |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
473 bool arobaseSeparator) |
0 | 474 { |
475 if (mysql_ == NULL) | |
476 { | |
477 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
478 } | |
479 | |
480 // This emulates the behavior of "CLIENT_MULTI_STATEMENTS" in | |
481 // "mysql_real_connect()", avoiding to implement a loop over | |
482 // "mysql_query()" | |
483 std::vector<std::string> commands; | |
484 Orthanc::Toolbox::TokenizeString(commands, sql, ';'); | |
485 | |
486 for (size_t i = 0; i < commands.size(); i++) | |
487 { | |
488 std::string s = Orthanc::Toolbox::StripSpaces(commands[i]); | |
489 | |
490 if (!s.empty()) | |
491 { | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
492 if (arobaseSeparator) |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
493 { |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
494 // Replace the escape character "@" by a semicolon |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
495 std::replace(s.begin(), s.end(), '@', ';'); |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
496 } |
0 | 497 |
498 LOG(TRACE) << "MySQL: " << s; | |
499 CheckErrorCode(mysql_query(mysql_, s.c_str())); | |
500 } | |
501 } | |
502 } | |
503 | |
504 | |
505 IPrecompiledStatement* MySQLDatabase::Compile(const Query& query) | |
506 { | |
507 if (mysql_ == NULL) | |
508 { | |
509 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
510 } | |
511 | |
512 return new MySQLStatement(*this, query); | |
513 } | |
514 | |
515 | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
516 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
517 namespace |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
518 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
519 class MySQLImplicitTransaction : public ImplicitTransaction |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
520 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
521 private: |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
522 MySQLDatabase& db_; |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
523 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
524 protected: |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
525 virtual IResult* ExecuteInternal(IPrecompiledStatement& statement, |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
526 const Dictionary& parameters) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
527 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
528 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
|
529 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
530 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
531 virtual void ExecuteWithoutResultInternal(IPrecompiledStatement& statement, |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
532 const Dictionary& parameters) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
533 { |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
534 dynamic_cast<MySQLStatement&>(statement).ExecuteWithoutResult(*this, parameters); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
535 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
536 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
537 public: |
186 | 538 explicit MySQLImplicitTransaction(MySQLDatabase& db) : |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
539 db_(db) |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
540 { |
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 }; |
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 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
545 |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
546 ITransaction* MySQLDatabase::CreateTransaction(bool isImplicit) |
0 | 547 { |
548 if (mysql_ == NULL) | |
549 { | |
550 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
551 } | |
552 | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
553 if (isImplicit) |
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 return new MySQLImplicitTransaction(*this); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
556 } |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
557 else |
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 return new MySQLTransaction(*this); |
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
22
diff
changeset
|
560 } |
0 | 561 } |
562 | |
563 | |
564 void MySQLDatabase::GlobalFinalization() | |
565 { | |
566 mysql_library_end(); | |
567 } | |
24
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
568 |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
569 |
60
412e30336847
allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
46
diff
changeset
|
570 bool MySQLDatabase::IsValidDatabaseIdentifier(const std::string& s) |
24
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
571 { |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
572 for (size_t i = 0; i < s.length(); i++) |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
573 { |
60
412e30336847
allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
46
diff
changeset
|
574 // 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
|
575 if (!isalnum(s[i]) && |
412e30336847
allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
46
diff
changeset
|
576 s[i] != '$' && |
412e30336847
allowing dollars and underscores in MySQL database identifiers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
46
diff
changeset
|
577 s[i] != '_') |
24
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
578 { |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
579 return false; |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
580 } |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
581 } |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
582 |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
583 return true; |
17f849b2af34
sharing plugin initialization code
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
584 } |
135
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
585 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
586 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
587 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
|
588 TransientAdvisoryLock(MySQLDatabase& database, |
137
52b3859ee0b7
MySQL: acquiring named locks instead of numbers
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
135
diff
changeset
|
589 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
|
590 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
|
591 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
|
592 { |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
593 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
|
594 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
595 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
|
596 { |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
597 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
|
598 { |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
599 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
|
600 break; |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
601 } |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
602 else |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
603 { |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
604 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
|
605 } |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
606 } |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
607 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
608 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
|
609 { |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
610 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
|
611 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
|
612 } |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
613 } |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
614 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
615 |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
616 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
|
617 { |
e26690365c25
MySQL: Added an advisory lock to avoid race conditions during database setup
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
618 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
|
619 } |
0 | 620 } |