Mercurial > hg > orthanc-databases
annotate Framework/MySQL/MySQLResult.cpp @ 187:0b78198c28d6
compatibility with mysql client 8.x
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 16 Dec 2020 15:41:06 +0100 |
parents | 063aa53b5917 |
children | 3236894320d6 |
rev | line source |
---|---|
0 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
140
4cd7e45b671e
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
67
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
0 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "MySQLResult.h" | |
23 | |
152 | 24 #include <Logging.h> |
25 #include <OrthancException.h> | |
0 | 26 |
6 | 27 #include <errmsg.h> |
0 | 28 #include <mysqld_error.h> |
29 | |
30 namespace OrthancDatabases | |
31 { | |
32 void MySQLResult::Step() | |
33 { | |
34 int code = mysql_stmt_fetch(statement_.GetObject()); | |
35 | |
36 if (code == 1) | |
37 { | |
38 unsigned int error = mysql_errno(database_.GetObject()); | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
39 |
0 | 40 if (error == 0) |
41 { | |
16
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
42 // This case occurs in requests without a result (e.g. if the |
9e419261f1c9
mysql storage area working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
6
diff
changeset
|
43 // SQL request is not a SELECT) |
0 | 44 done_ = true; |
45 } | |
46 else if (error == CR_SERVER_GONE_ERROR || | |
47 error == CR_SERVER_LOST || | |
48 error == ER_QUERY_INTERRUPTED) | |
49 { | |
50 database_.LogError(); | |
51 throw Orthanc::OrthancException(Orthanc::ErrorCode_DatabaseUnavailable); | |
52 } | |
53 else | |
54 { | |
55 database_.LogError(); | |
56 throw Orthanc::OrthancException(Orthanc::ErrorCode_Database); | |
57 } | |
58 } | |
59 else | |
60 { | |
61 done_ = (code != 0 && | |
62 code != MYSQL_DATA_TRUNCATED); // Occurs if mysql_stmt_fetch_column() must be called | |
63 | |
64 FetchFields(); | |
65 } | |
66 } | |
67 | |
68 | |
69 IValue* MySQLResult::FetchField(size_t index) | |
70 { | |
71 return statement_.FetchResultField(index); | |
72 } | |
73 | |
74 | |
75 MySQLResult::MySQLResult(MySQLDatabase& db, | |
76 MySQLStatement& statement) : | |
77 database_(db), | |
78 statement_(statement) | |
79 { | |
80 // !!! https://dev.mysql.com/doc/refman/5.7/en/mysql-stmt-fetch.html | |
81 // https://gist.github.com/hoterran/6365915 | |
82 // https://github.com/hholzgra/connector-c-examples/blob/master/mysql_stmt_bind_result.c | |
83 | |
84 SetFieldsCount(statement_.GetResultFieldsCount()); | |
85 | |
86 Step(); | |
87 } | |
88 | |
89 | |
90 MySQLResult::~MySQLResult() | |
91 { | |
92 // Reset the statement for further use | |
93 if (mysql_stmt_reset(statement_.GetObject()) != 0) | |
94 { | |
95 LOG(ERROR) << "Cannot reset the statement, expect an error"; | |
96 } | |
97 } | |
98 | |
99 | |
100 void MySQLResult::Next() | |
101 { | |
102 if (IsDone()) | |
103 { | |
104 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
105 } | |
106 else | |
107 { | |
108 Step(); | |
109 } | |
110 } | |
111 } |