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