Mercurial > hg > orthanc-databases
annotate Framework/PostgreSQL/PostgreSQLResult.cpp @ 140:4cd7e45b671e
upgrade to year 2020
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 31 Jan 2020 17:24:29 +0100 |
parents | 5765cc5fd268 |
children | 063aa53b5917 |
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:
107
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 | |
107
5765cc5fd268
reverted fix for OS X that breaks other targets
Sebastien Jodogne <s.jodogne@orthanc-labs.com>
parents:
105
diff
changeset
|
22 #include "PostgreSQLIncludes.h" // Must be the first |
105 | 23 #include "PostgreSQLResult.h" |
0 | 24 |
25 #include "../Common/BinaryStringValue.h" | |
14
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
26 #include "../Common/FileValue.h" |
0 | 27 #include "../Common/Integer64Value.h" |
28 #include "../Common/NullValue.h" | |
29 #include "../Common/Utf8StringValue.h" | |
30 | |
31 #include <Core/OrthancException.h> | |
32 #include <Core/Logging.h> | |
103 | 33 #include <Core/Endianness.h> |
0 | 34 |
35 #include <cassert> | |
36 #include <boost/lexical_cast.hpp> | |
37 | |
38 | |
39 namespace OrthancDatabases | |
40 { | |
41 void PostgreSQLResult::Clear() | |
42 { | |
43 if (result_ != NULL) | |
44 { | |
45 PQclear(reinterpret_cast<PGresult*>(result_)); | |
46 result_ = NULL; | |
47 } | |
48 } | |
49 | |
50 | |
51 void PostgreSQLResult::CheckDone() | |
52 { | |
53 if (position_ >= PQntuples(reinterpret_cast<PGresult*>(result_))) | |
54 { | |
55 // We are at the end of the result set | |
56 Clear(); | |
57 } | |
58 } | |
59 | |
60 | |
61 void PostgreSQLResult::CheckColumn(unsigned int column, unsigned int /*Oid*/ expectedType) const | |
62 { | |
63 if (IsDone()) | |
64 { | |
65 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
66 } | |
67 | |
68 if (column >= static_cast<unsigned int>(PQnfields(reinterpret_cast<PGresult*>(result_)))) | |
69 { | |
70 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
71 } | |
72 | |
73 if (expectedType != 0 && | |
74 expectedType != PQftype(reinterpret_cast<PGresult*>(result_), column)) | |
75 { | |
76 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType); | |
77 } | |
78 } | |
79 | |
80 | |
81 PostgreSQLResult::PostgreSQLResult(PostgreSQLStatement& statement) : | |
82 position_(0), | |
83 database_(statement.GetDatabase()) | |
84 { | |
85 result_ = statement.Execute(); | |
86 assert(result_ != NULL); // An exception would have been thrown otherwise | |
87 | |
88 // This is the first call to "Next()" | |
89 if (PQresultStatus(reinterpret_cast<PGresult*>(result_)) == PGRES_TUPLES_OK) | |
90 { | |
91 CheckDone(); | |
92 columnsCount_ = static_cast<unsigned int>(PQnfields(reinterpret_cast<PGresult*>(result_))); | |
93 } | |
94 else | |
95 { | |
96 // This is not a SELECT request, we're done | |
97 Clear(); | |
98 columnsCount_ = 0; | |
99 } | |
100 } | |
101 | |
102 | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
103 PostgreSQLResult::~PostgreSQLResult() |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
104 { |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
105 try |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
106 { |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
107 Clear(); |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
108 } |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
109 catch (Orthanc::OrthancException&) |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
110 { |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
111 // Ignore possible exceptions due to connection loss |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
112 } |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
113 } |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
114 |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
115 |
0 | 116 void PostgreSQLResult::Next() |
117 { | |
118 position_++; | |
119 CheckDone(); | |
120 } | |
121 | |
122 | |
123 bool PostgreSQLResult::IsNull(unsigned int column) const | |
124 { | |
125 CheckColumn(column, 0); | |
126 return PQgetisnull(reinterpret_cast<PGresult*>(result_), position_, column) != 0; | |
127 } | |
128 | |
129 | |
130 bool PostgreSQLResult::GetBoolean(unsigned int column) const | |
131 { | |
132 CheckColumn(column, BOOLOID); | |
133 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == 1); | |
134 const uint8_t *v = reinterpret_cast<const uint8_t*> | |
135 (PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column)); | |
136 return (v[0] != 0); | |
137 } | |
138 | |
139 | |
140 int PostgreSQLResult::GetInteger(unsigned int column) const | |
141 { | |
142 CheckColumn(column, INT4OID); | |
143 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == 4); | |
144 char *v = PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | |
145 return htobe32(*reinterpret_cast<int32_t*>(v)); | |
146 } | |
147 | |
148 | |
149 int64_t PostgreSQLResult::GetInteger64(unsigned int column) const | |
150 { | |
151 CheckColumn(column, INT8OID); | |
152 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == 8); | |
153 char *v = PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | |
154 return htobe64(*reinterpret_cast<int64_t*>(v)); | |
155 } | |
156 | |
157 | |
158 std::string PostgreSQLResult::GetString(unsigned int column) const | |
159 { | |
160 CheckColumn(column, 0); | |
161 | |
162 Oid oid = PQftype(reinterpret_cast<PGresult*>(result_), column); | |
163 if (oid != TEXTOID && oid != VARCHAROID && oid != BYTEAOID) | |
164 { | |
165 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType); | |
166 } | |
167 | |
168 return std::string(PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column)); | |
169 } | |
170 | |
171 | |
172 void PostgreSQLResult::GetLargeObject(std::string& result, | |
173 unsigned int column) const | |
174 { | |
175 CheckColumn(column, OIDOID); | |
176 | |
177 Oid oid; | |
178 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == sizeof(oid)); | |
179 | |
180 oid = *(const Oid*) PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | |
181 oid = ntohl(oid); | |
182 | |
183 PostgreSQLLargeObject::Read(result, database_, boost::lexical_cast<std::string>(oid)); | |
184 } | |
185 | |
186 | |
187 void PostgreSQLResult::GetLargeObject(void*& result, | |
188 size_t& size, | |
189 unsigned int column) const | |
190 { | |
191 CheckColumn(column, OIDOID); | |
192 | |
193 Oid oid; | |
194 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == sizeof(oid)); | |
195 | |
196 oid = *(const Oid*) PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | |
197 oid = ntohl(oid); | |
198 | |
199 PostgreSQLLargeObject::Read(result, size, database_, boost::lexical_cast<std::string>(oid)); | |
200 } | |
201 | |
202 | |
203 IValue* PostgreSQLResult::GetValue(unsigned int column) const | |
204 { | |
205 if (IsNull(column)) | |
206 { | |
207 return new NullValue; | |
208 } | |
209 | |
210 Oid type = PQftype(reinterpret_cast<PGresult*>(result_), column); | |
211 | |
212 switch (type) | |
213 { | |
214 case BOOLOID: | |
215 // Convert Boolean values as integers | |
216 return new Integer64Value(GetBoolean(column) ? 1 : 0); | |
217 | |
218 case INT4OID: | |
219 return new Integer64Value(GetInteger(column)); | |
220 | |
221 case INT8OID: | |
222 return new Integer64Value(GetInteger64(column)); | |
223 | |
224 case TEXTOID: | |
225 case VARCHAROID: | |
226 return new Utf8StringValue(GetString(column)); | |
227 | |
228 case BYTEAOID: | |
229 return new BinaryStringValue(GetString(column)); | |
230 | |
231 case OIDOID: | |
232 { | |
14
9774802fd05f
PostgreSQLStorageArea working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
0
diff
changeset
|
233 std::auto_ptr<FileValue> value(new FileValue); |
0 | 234 GetLargeObject(value->GetContent(), column); |
235 return value.release(); | |
236 } | |
237 | |
238 default: | |
239 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
240 } | |
241 } | |
242 } |