Mercurial > hg > orthanc-databases
annotate Framework/PostgreSQL/PostgreSQLResult.cpp @ 385:346fe629d638 db-protobuf
clarifying types of since/limit
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 03 Apr 2023 11:19:19 +0200 |
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:
248
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
16aac0287485
copyright upgraded to 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
248
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 | |
107
5765cc5fd268
reverted fix for OS X that breaks other targets
Sebastien Jodogne <s.jodogne@orthanc-labs.com>
parents:
105
diff
changeset
|
23 #include "PostgreSQLIncludes.h" // Must be the first |
105 | 24 #include "PostgreSQLResult.h" |
0 | 25 |
26 #include "../Common/BinaryStringValue.h" | |
245
9d00e5e073e8
rename FileValue as ResultFileValue
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
242
diff
changeset
|
27 #include "../Common/ResultFileValue.h" |
0 | 28 #include "../Common/Integer64Value.h" |
29 #include "../Common/NullValue.h" | |
30 #include "../Common/Utf8StringValue.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 <OrthancException.h> |
34 #include <Logging.h> | |
35 #include <Endianness.h> | |
0 | 36 |
37 #include <cassert> | |
38 #include <boost/lexical_cast.hpp> | |
39 | |
40 | |
41 namespace OrthancDatabases | |
42 { | |
43 void PostgreSQLResult::Clear() | |
44 { | |
45 if (result_ != NULL) | |
46 { | |
47 PQclear(reinterpret_cast<PGresult*>(result_)); | |
48 result_ = NULL; | |
49 } | |
50 } | |
51 | |
52 | |
53 void PostgreSQLResult::CheckDone() | |
54 { | |
55 if (position_ >= PQntuples(reinterpret_cast<PGresult*>(result_))) | |
56 { | |
57 // We are at the end of the result set | |
58 Clear(); | |
59 } | |
60 } | |
61 | |
62 | |
63 void PostgreSQLResult::CheckColumn(unsigned int column, unsigned int /*Oid*/ expectedType) const | |
64 { | |
65 if (IsDone()) | |
66 { | |
67 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
68 } | |
69 | |
70 if (column >= static_cast<unsigned int>(PQnfields(reinterpret_cast<PGresult*>(result_)))) | |
71 { | |
72 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
73 } | |
74 | |
75 if (expectedType != 0 && | |
76 expectedType != PQftype(reinterpret_cast<PGresult*>(result_), column)) | |
77 { | |
78 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType); | |
79 } | |
80 } | |
81 | |
82 | |
83 PostgreSQLResult::PostgreSQLResult(PostgreSQLStatement& statement) : | |
84 position_(0), | |
85 database_(statement.GetDatabase()) | |
86 { | |
87 result_ = statement.Execute(); | |
88 assert(result_ != NULL); // An exception would have been thrown otherwise | |
89 | |
90 // This is the first call to "Next()" | |
91 if (PQresultStatus(reinterpret_cast<PGresult*>(result_)) == PGRES_TUPLES_OK) | |
92 { | |
93 CheckDone(); | |
94 columnsCount_ = static_cast<unsigned int>(PQnfields(reinterpret_cast<PGresult*>(result_))); | |
95 } | |
96 else | |
97 { | |
98 // This is not a SELECT request, we're done | |
99 Clear(); | |
100 columnsCount_ = 0; | |
101 } | |
102 } | |
103 | |
104 | |
46
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
105 PostgreSQLResult::~PostgreSQLResult() |
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 try |
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 Clear(); |
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 catch (Orthanc::OrthancException&) |
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 // Ignore possible exceptions due to connection loss |
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 } |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
116 |
6a574d810b98
Compatibility with MySQL 8.0
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
14
diff
changeset
|
117 |
0 | 118 void PostgreSQLResult::Next() |
119 { | |
120 position_++; | |
121 CheckDone(); | |
122 } | |
123 | |
124 | |
125 bool PostgreSQLResult::IsNull(unsigned int column) const | |
126 { | |
127 CheckColumn(column, 0); | |
128 return PQgetisnull(reinterpret_cast<PGresult*>(result_), position_, column) != 0; | |
129 } | |
130 | |
131 | |
132 bool PostgreSQLResult::GetBoolean(unsigned int column) const | |
133 { | |
134 CheckColumn(column, BOOLOID); | |
135 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == 1); | |
136 const uint8_t *v = reinterpret_cast<const uint8_t*> | |
137 (PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column)); | |
138 return (v[0] != 0); | |
139 } | |
140 | |
141 | |
142 int PostgreSQLResult::GetInteger(unsigned int column) const | |
143 { | |
144 CheckColumn(column, INT4OID); | |
145 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == 4); | |
146 char *v = PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | |
147 return htobe32(*reinterpret_cast<int32_t*>(v)); | |
148 } | |
149 | |
150 | |
151 int64_t PostgreSQLResult::GetInteger64(unsigned int column) const | |
152 { | |
153 CheckColumn(column, INT8OID); | |
154 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == 8); | |
155 char *v = PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | |
156 return htobe64(*reinterpret_cast<int64_t*>(v)); | |
157 } | |
158 | |
159 | |
160 std::string PostgreSQLResult::GetString(unsigned int column) const | |
161 { | |
162 CheckColumn(column, 0); | |
163 | |
164 Oid oid = PQftype(reinterpret_cast<PGresult*>(result_), column); | |
165 if (oid != TEXTOID && oid != VARCHAROID && oid != BYTEAOID) | |
166 { | |
167 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadParameterType); | |
168 } | |
169 | |
170 return std::string(PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column)); | |
171 } | |
172 | |
173 | |
246
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
174 std::string PostgreSQLResult::GetLargeObjectOid(unsigned int column) const |
0 | 175 { |
176 CheckColumn(column, OIDOID); | |
177 | |
242
b97a537f4613
MySQL: Support of range reads for the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
178 /** |
b97a537f4613
MySQL: Support of range reads for the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
179 * In PostgreSQL, the type "Oid" corresponds to "unsigned int", cf. |
b97a537f4613
MySQL: Support of range reads for the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
180 * /usr/include/postgresql/10/server/postgres_ext.h |
b97a537f4613
MySQL: Support of range reads for the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
193
diff
changeset
|
181 **/ |
0 | 182 Oid oid; |
183 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == sizeof(oid)); | |
184 | |
185 oid = *(const Oid*) PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | |
186 oid = ntohl(oid); | |
187 | |
246
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
188 return boost::lexical_cast<std::string>(oid); |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
189 } |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
190 |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
191 |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
192 void PostgreSQLResult::GetLargeObjectContent(std::string& content, |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
193 unsigned int column) const |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
194 { |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
246
diff
changeset
|
195 PostgreSQLLargeObject::ReadWhole(content, database_, GetLargeObjectOid(column)); |
0 | 196 } |
197 | |
198 | |
246
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
199 class PostgreSQLResult::LargeObjectResult : public ResultFileValue |
0 | 200 { |
246
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
201 private: |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
202 PostgreSQLDatabase& database_; |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
203 std::string oid_; |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
204 |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
205 public: |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
206 LargeObjectResult(PostgreSQLDatabase& database, |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
207 const std::string& oid) : |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
208 database_(database), |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
209 oid_(oid) |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
210 { |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
211 } |
0 | 212 |
246
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
213 virtual void ReadWhole(std::string& target) const ORTHANC_OVERRIDE |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
214 { |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
246
diff
changeset
|
215 PostgreSQLLargeObject::ReadWhole(target, database_, oid_); |
246
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
216 } |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
217 |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
218 virtual void ReadRange(std::string& target, |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
219 uint64_t start, |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
220 size_t length) const ORTHANC_OVERRIDE |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
221 { |
248
7a4f9bcb0bc2
PostgreSQL: Support of range reads from the storage area
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
246
diff
changeset
|
222 PostgreSQLLargeObject::ReadRange(target, database_, oid_, start, length); |
246
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
223 } |
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
224 }; |
0 | 225 |
226 | |
227 IValue* PostgreSQLResult::GetValue(unsigned int column) const | |
228 { | |
229 if (IsNull(column)) | |
230 { | |
231 return new NullValue; | |
232 } | |
233 | |
234 Oid type = PQftype(reinterpret_cast<PGresult*>(result_), column); | |
235 | |
236 switch (type) | |
237 { | |
238 case BOOLOID: | |
239 // Convert Boolean values as integers | |
240 return new Integer64Value(GetBoolean(column) ? 1 : 0); | |
241 | |
242 case INT4OID: | |
243 return new Integer64Value(GetInteger(column)); | |
244 | |
245 case INT8OID: | |
246 return new Integer64Value(GetInteger64(column)); | |
247 | |
248 case TEXTOID: | |
249 case VARCHAROID: | |
250 return new Utf8StringValue(GetString(column)); | |
251 | |
252 case BYTEAOID: | |
253 return new BinaryStringValue(GetString(column)); | |
254 | |
255 case OIDOID: | |
246
483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
245
diff
changeset
|
256 return new LargeObjectResult(database_, GetLargeObjectOid(column)); |
0 | 257 |
258 default: | |
259 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
260 } | |
261 } | |
262 } |