Mercurial > hg > orthanc-databases
annotate Framework/MySQL/MySQLStatement.cpp @ 370:d2b5d9c92214 pg-transactions
PG: test feature: configurable transaction isolation level
author | Alain Mazy <am@osimis.io> |
---|---|
date | Wed, 22 Feb 2023 16:52:04 +0100 |
parents | 16aac0287485 |
children | 3d6886f3e5b3 |
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 | |
359
16aac0287485
copyright upgraded to 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
244
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
16aac0287485
copyright upgraded to 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
244
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
0 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU Affero General Public License | |
10 * as published by the Free Software Foundation, either version 3 of | |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * Affero General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU Affero General Public License | |
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 **/ | |
21 | |
22 | |
23 #include "MySQLStatement.h" | |
24 | |
25 #include "../Common/BinaryStringValue.h" | |
244
02cd7254c949
separating class InputFileValue from FileValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
26 #include "../Common/InputFileValue.h" |
0 | 27 #include "../Common/Integer64Value.h" |
28 #include "../Common/NullValue.h" | |
29 #include "../Common/Utf8StringValue.h" | |
30 #include "MySQLResult.h" | |
31 | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
32 #include <Compatibility.h> // For std::unique_ptr<> |
152 | 33 #include <Logging.h> |
34 #include <OrthancException.h> | |
0 | 35 |
36 #include <list> | |
37 #include <memory> | |
38 | |
39 namespace OrthancDatabases | |
40 { | |
41 class MySQLStatement::ResultField : public boost::noncopyable | |
42 { | |
43 private: | |
186 | 44 IValue* CreateIntegerValue(const MYSQL_BIND& bind) const |
0 | 45 { |
46 if (length_ != buffer_.size()) | |
47 { | |
48 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
49 } | |
50 | |
51 switch (mysqlType_) | |
52 { | |
53 case MYSQL_TYPE_TINY: | |
54 if (bind.is_unsigned) | |
55 { | |
56 return new Integer64Value(*reinterpret_cast<const uint8_t*>(&buffer_[0])); | |
57 } | |
58 else | |
59 { | |
60 return new Integer64Value(*reinterpret_cast<const int8_t*>(&buffer_[0])); | |
61 } | |
62 | |
63 case MYSQL_TYPE_SHORT: | |
64 if (bind.is_unsigned) | |
65 { | |
66 return new Integer64Value(*reinterpret_cast<const uint16_t*>(&buffer_[0])); | |
67 } | |
68 else | |
69 { | |
70 return new Integer64Value(*reinterpret_cast<const int16_t*>(&buffer_[0])); | |
71 } | |
72 | |
73 break; | |
74 | |
75 case MYSQL_TYPE_LONG: | |
76 if (bind.is_unsigned) | |
77 { | |
78 return new Integer64Value(*reinterpret_cast<const uint32_t*>(&buffer_[0])); | |
79 } | |
80 else | |
81 { | |
82 return new Integer64Value(*reinterpret_cast<const int32_t*>(&buffer_[0])); | |
83 } | |
84 | |
85 break; | |
86 | |
87 case MYSQL_TYPE_LONGLONG: | |
88 if (bind.is_unsigned) | |
89 { | |
90 uint64_t value = *reinterpret_cast<const uint64_t*>(&buffer_[0]); | |
91 if (static_cast<uint64_t>(static_cast<int64_t>(value)) != value) | |
92 { | |
93 LOG(WARNING) << "Overflow in a 64 bit integer"; | |
94 } | |
95 | |
96 return new Integer64Value(static_cast<int64_t>(value)); | |
97 } | |
98 else | |
99 { | |
100 return new Integer64Value(*reinterpret_cast<const int64_t*>(&buffer_[0])); | |
101 } | |
102 | |
103 break; | |
104 | |
105 default: | |
106 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
107 } | |
108 } | |
109 | |
110 | |
111 enum enum_field_types mysqlType_; | |
112 ValueType orthancType_; | |
113 std::string buffer_; | |
188 | 114 |
189
b968e7bfa7f9
fix build against mariadb client
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
188
diff
changeset
|
115 #if (MYSQL_VERSION_ID < 80000) || defined(MARIADB_VERSION_ID) |
0 | 116 my_bool isNull_; |
117 my_bool isError_; | |
188 | 118 #else |
119 bool isNull_; | |
120 bool isError_; | |
121 #endif | |
122 | |
0 | 123 unsigned long length_; |
124 | |
125 public: | |
186 | 126 explicit ResultField(const MYSQL_FIELD& field) : |
127 mysqlType_(field.type), | |
128 length_(0) | |
0 | 129 { |
130 // https://dev.mysql.com/doc/refman/8.0/en/c-api-data-structures.html | |
131 // https://dev.mysql.com/doc/refman/8.0/en/mysql-stmt-fetch.html => size of "buffer_" | |
132 switch (field.type) | |
133 { | |
134 case MYSQL_TYPE_TINY: | |
135 orthancType_ = ValueType_Integer64; | |
136 buffer_.resize(1); | |
137 break; | |
138 | |
139 case MYSQL_TYPE_SHORT: | |
140 orthancType_ = ValueType_Integer64; | |
141 buffer_.resize(2); | |
142 break; | |
143 | |
144 case MYSQL_TYPE_LONG: | |
145 orthancType_ = ValueType_Integer64; | |
146 buffer_.resize(4); | |
147 break; | |
148 | |
149 case MYSQL_TYPE_LONGLONG: | |
150 orthancType_ = ValueType_Integer64; | |
151 buffer_.resize(8); | |
152 break; | |
153 | |
154 case MYSQL_TYPE_STRING: | |
155 case MYSQL_TYPE_VAR_STRING: | |
156 case MYSQL_TYPE_BLOB: | |
242
b97a537f4613
MySQL: Support of range reads for the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
214
diff
changeset
|
157 case MYSQL_TYPE_LONG_BLOB: |
0 | 158 // https://medium.com/@adamhooper/in-mysql-never-use-utf8-use-utf8mb4-11761243e434 |
159 switch (field.charsetnr) | |
160 { | |
161 case 45: // utf8mb4_general_ci | |
162 case 46: // utf8mb4_bin | |
163 case 224: // utf8mb4_unicode_ci => RECOMMENDED collation | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
164 case 255: // utf8mb4_0900_ai_ci => necessary for MySQL 8.0 |
0 | 165 // https://stackoverflow.com/questions/766809/whats-the-difference-between-utf8-general-ci-and-utf8-unicode-ci |
166 orthancType_ = ValueType_Utf8String; | |
167 break; | |
168 | |
169 case 63: | |
170 orthancType_ = ValueType_BinaryString; | |
171 break; | |
172 | |
173 default: | |
174 LOG(ERROR) << "Unsupported MySQL charset: " << field.charsetnr; | |
175 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
176 } | |
177 | |
178 if (field.max_length > 0) | |
179 { | |
180 buffer_.resize(field.max_length); | |
181 } | |
182 | |
183 break; | |
184 | |
185 default: | |
186 LOG(ERROR) << "MYSQL_TYPE not implemented: " << field.type; | |
187 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
188 } | |
189 } | |
190 | |
191 enum enum_field_types GetMysqlType() const | |
192 { | |
193 return mysqlType_; | |
194 } | |
195 | |
196 ValueType GetOrthancType() const | |
197 { | |
198 return orthancType_; | |
199 } | |
200 | |
201 void PrepareBind(MYSQL_BIND& bind) | |
202 { | |
203 memset(&bind, 0, sizeof(bind)); | |
204 | |
9
eff482803d30
fix uninitialized values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
205 isNull_ = false; |
eff482803d30
fix uninitialized values
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
206 isError_ = false; |
0 | 207 length_ = 0; |
208 | |
209 bind.buffer_length = buffer_.size(); | |
210 bind.buffer_type = mysqlType_; | |
211 bind.is_null = &isNull_; | |
212 bind.length = &length_; | |
213 | |
214 if (buffer_.empty()) | |
215 { | |
216 // Only fetches the actual size of the field (*): | |
217 // mysql_stmt_fetch_column() must be invoked afterward | |
218 bind.buffer = 0; | |
219 } | |
220 else | |
221 { | |
222 bind.buffer = &buffer_[0]; | |
223 bind.error = &isError_; | |
224 } | |
225 } | |
226 | |
227 | |
228 IValue* FetchValue(MySQLDatabase& database, | |
229 MYSQL_STMT& statement, | |
230 MYSQL_BIND& bind, | |
231 unsigned int column) const | |
232 { | |
233 if (isError_) | |
234 { | |
235 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
236 } | |
237 else if (isNull_) | |
238 { | |
239 return new NullValue; | |
240 } | |
241 else if (orthancType_ == ValueType_Integer64) | |
242 { | |
243 return CreateIntegerValue(bind); | |
244 } | |
245 else if (orthancType_ == ValueType_Utf8String || | |
246 orthancType_ == ValueType_BinaryString) | |
247 { | |
248 std::string tmp; | |
249 tmp.resize(length_); | |
250 | |
251 if (!tmp.empty()) | |
252 { | |
253 if (buffer_.empty()) | |
254 { | |
255 bind.buffer = &tmp[0]; | |
256 bind.buffer_length = tmp.size(); | |
257 | |
258 database.CheckErrorCode(mysql_stmt_fetch_column(&statement, &bind, column, 0)); | |
259 } | |
260 else if (tmp.size() <= buffer_.size()) | |
261 { | |
262 memcpy(&tmp[0], &buffer_[0], length_); | |
263 } | |
264 else | |
265 { | |
266 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
267 } | |
268 } | |
269 | |
270 if (orthancType_ == ValueType_Utf8String) | |
271 { | |
272 return new Utf8StringValue(tmp); | |
273 } | |
274 else | |
275 { | |
276 return new BinaryStringValue(tmp); | |
277 } | |
278 } | |
279 else | |
280 { | |
281 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
282 } | |
283 } | |
284 }; | |
285 | |
286 | |
287 class MySQLStatement::ResultMetadata : public boost::noncopyable | |
288 { | |
289 private: | |
290 MYSQL_RES* metadata_; | |
291 | |
292 public: | |
293 ResultMetadata(MySQLDatabase& db, | |
294 MySQLStatement& statement) : | |
295 metadata_(NULL) | |
296 { | |
297 metadata_ = mysql_stmt_result_metadata(statement.GetObject()); | |
298 } | |
299 | |
300 ~ResultMetadata() | |
301 { | |
302 if (metadata_ != NULL) | |
303 { | |
304 mysql_free_result(metadata_); | |
305 } | |
306 } | |
307 | |
308 bool HasFields() const | |
309 { | |
310 return metadata_ != NULL; | |
311 } | |
312 | |
313 size_t GetFieldsCount() | |
314 { | |
315 if (HasFields()) | |
316 { | |
317 return mysql_num_fields(metadata_); | |
318 } | |
319 else | |
320 { | |
321 return 0; | |
322 } | |
323 } | |
324 | |
325 MYSQL_RES* GetObject() | |
326 { | |
327 return metadata_; | |
328 } | |
329 }; | |
330 | |
331 | |
332 void MySQLStatement::Close() | |
333 { | |
334 for (size_t i = 0; i < result_.size(); i++) | |
335 { | |
336 if (result_[i] != NULL) | |
337 { | |
338 delete result_[i]; | |
339 } | |
340 } | |
341 | |
342 if (statement_ != NULL) | |
343 { | |
344 mysql_stmt_close(statement_); | |
345 statement_ = NULL; | |
346 } | |
347 } | |
348 | |
349 | |
350 MySQLStatement::MySQLStatement(MySQLDatabase& db, | |
351 const Query& query) : | |
352 db_(db), | |
353 statement_(NULL), | |
354 formatter_(Dialect_MySQL) | |
355 { | |
356 std::string sql; | |
357 query.Format(sql, formatter_); | |
358 | |
359 statement_ = mysql_stmt_init(db.GetObject()); | |
360 if (statement_ == NULL) | |
361 { | |
362 db.LogError(); | |
363 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
364 } | |
365 | |
169
c17f219cec42
replacing VLOG(1) by LOG(TRACE)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
162
diff
changeset
|
366 LOG(TRACE) << "Preparing MySQL statement: " << sql; |
0 | 367 |
368 db_.CheckErrorCode(mysql_stmt_prepare(statement_, sql.c_str(), sql.size())); | |
369 | |
370 if (mysql_stmt_param_count(statement_) != formatter_.GetParametersCount()) | |
371 { | |
372 Close(); | |
373 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
374 } | |
375 | |
376 try | |
377 { | |
378 ResultMetadata result(db, *this); | |
379 | |
380 if (result.HasFields()) | |
381 { | |
382 MYSQL_FIELD *field; | |
383 while ((field = mysql_fetch_field(result.GetObject()))) | |
384 { | |
385 result_.push_back(new ResultField(*field)); | |
386 } | |
387 } | |
388 | |
389 if (result_.size() != result.GetFieldsCount()) | |
390 { | |
391 throw Orthanc::OrthancException(Orthanc::ErrorCode_InternalError); | |
392 } | |
393 } | |
394 catch (Orthanc::OrthancException&) | |
395 { | |
396 Close(); | |
397 throw; | |
398 } | |
399 | |
400 if (query.IsReadOnly()) | |
401 { | |
402 unsigned long type = (unsigned long) CURSOR_TYPE_READ_ONLY; | |
403 mysql_stmt_attr_set(statement_, STMT_ATTR_CURSOR_TYPE, (void*) &type); | |
404 } | |
405 } | |
406 | |
407 | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
408 MySQLStatement::~MySQLStatement() |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
409 { |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
410 try |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
411 { |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
412 Close(); |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
413 } |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
414 catch (Orthanc::OrthancException&) |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
415 { |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
416 // Ignore possible exceptions due to connection loss |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
417 } |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
418 } |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
419 |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
23
diff
changeset
|
420 |
0 | 421 MYSQL_STMT* MySQLStatement::GetObject() |
422 { | |
423 if (statement_ == NULL) | |
424 { | |
425 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
426 } | |
427 else | |
428 { | |
429 return statement_; | |
430 } | |
431 } | |
432 | |
433 | |
434 IValue* MySQLStatement::FetchResultField(size_t i) | |
435 { | |
436 if (i >= result_.size()) | |
437 { | |
438 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
439 } | |
440 else | |
441 { | |
442 assert(result_[i] != NULL); | |
443 return result_[i]->FetchValue(db_, *statement_, outputs_[i], i); | |
444 } | |
445 } | |
446 | |
447 | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
9
diff
changeset
|
448 IResult* MySQLStatement::Execute(ITransaction& transaction, |
0 | 449 const Dictionary& parameters) |
450 { | |
451 std::list<long long int> int64Parameters; | |
452 | |
453 std::vector<MYSQL_BIND> inputs(formatter_.GetParametersCount()); | |
454 | |
455 for (size_t i = 0; i < inputs.size(); i++) | |
456 { | |
457 memset(&inputs[i], 0, sizeof(MYSQL_BIND)); | |
458 | |
459 const std::string& name = formatter_.GetParameterName(i); | |
460 if (!parameters.HasKey(name)) | |
461 { | |
462 LOG(ERROR) << "Missing required parameter in a SQL query: " << name; | |
463 throw Orthanc::OrthancException(Orthanc::ErrorCode_InexistentItem); | |
464 } | |
465 | |
466 ValueType type = formatter_.GetParameterType(i); | |
467 | |
468 const IValue& value = parameters.GetValue(name); | |
469 if (value.GetType() != type) | |
470 { | |
471 LOG(ERROR) << "Bad type of argument provided to a SQL query: " << name; | |
472 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType); | |
473 } | |
474 | |
475 // https://dev.mysql.com/doc/refman/8.0/en/c-api-prepared-statement-type-codes.html | |
476 switch (type) | |
477 { | |
478 case ValueType_Integer64: | |
479 { | |
480 int64Parameters.push_back(dynamic_cast<const Integer64Value&>(value).GetValue()); | |
481 inputs[i].buffer = &int64Parameters.back(); | |
482 inputs[i].buffer_type = MYSQL_TYPE_LONGLONG; | |
483 break; | |
484 } | |
485 | |
486 case ValueType_Utf8String: | |
487 { | |
488 const std::string& utf8 = dynamic_cast<const Utf8StringValue&>(value).GetContent(); | |
489 inputs[i].buffer = const_cast<char*>(utf8.c_str()); | |
490 inputs[i].buffer_length = utf8.size(); | |
491 inputs[i].buffer_type = MYSQL_TYPE_STRING; | |
492 break; | |
493 } | |
494 | |
495 case ValueType_BinaryString: | |
496 { | |
497 const std::string& content = dynamic_cast<const BinaryStringValue&>(value).GetContent(); | |
498 inputs[i].buffer = const_cast<char*>(content.c_str()); | |
499 inputs[i].buffer_length = content.size(); | |
500 inputs[i].buffer_type = MYSQL_TYPE_BLOB; | |
501 break; | |
502 } | |
503 | |
244
02cd7254c949
separating class InputFileValue from FileValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
504 case ValueType_InputFile: |
0 | 505 { |
244
02cd7254c949
separating class InputFileValue from FileValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
506 const std::string& content = dynamic_cast<const InputFileValue&>(value).GetContent(); |
0 | 507 inputs[i].buffer = const_cast<char*>(content.c_str()); |
508 inputs[i].buffer_length = content.size(); | |
509 inputs[i].buffer_type = MYSQL_TYPE_BLOB; | |
510 break; | |
511 } | |
512 | |
513 case ValueType_Null: | |
514 { | |
515 inputs[i].buffer = NULL; | |
516 inputs[i].buffer_type = MYSQL_TYPE_NULL; | |
517 break; | |
518 } | |
519 | |
520 default: | |
521 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
522 } | |
523 } | |
524 | |
525 if (!inputs.empty()) | |
526 { | |
527 db_.CheckErrorCode(mysql_stmt_bind_param(statement_, &inputs[0])); | |
528 } | |
529 | |
530 db_.CheckErrorCode(mysql_stmt_execute(statement_)); | |
531 | |
532 outputs_.resize(result_.size()); | |
533 | |
534 for (size_t i = 0; i < result_.size(); i++) | |
535 { | |
536 assert(result_[i] != NULL); | |
537 result_[i]->PrepareBind(outputs_[i]); | |
538 } | |
539 | |
540 if (!outputs_.empty()) | |
541 { | |
542 db_.CheckErrorCode(mysql_stmt_bind_result(statement_, &outputs_[0])); | |
543 db_.CheckErrorCode(mysql_stmt_store_result(statement_)); | |
544 } | |
545 | |
546 return new MySQLResult(db_, *this); | |
547 } | |
548 | |
549 | |
23
b2ff1cd2907a
handling of implicit transactions in DatabaseManager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
9
diff
changeset
|
550 void MySQLStatement::ExecuteWithoutResult(ITransaction& transaction, |
0 | 551 const Dictionary& parameters) |
552 { | |
157
275e14f57f1e
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
152
diff
changeset
|
553 std::unique_ptr<IResult> dummy(Execute(transaction, parameters)); |
0 | 554 } |
555 } |