Mercurial > hg > orthanc-databases
comparison Framework/PostgreSQL/PostgreSQLResult.cpp @ 246:483af3f35a4b
turning ResultFileValue into a base class, and implement its primitives for PostgreSQL
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 14 Apr 2021 08:42:31 +0200 |
parents | 9d00e5e073e8 |
children | 7a4f9bcb0bc2 |
comparison
equal
deleted
inserted
replaced
245:9d00e5e073e8 | 246:483af3f35a4b |
---|---|
168 | 168 |
169 return std::string(PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column)); | 169 return std::string(PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column)); |
170 } | 170 } |
171 | 171 |
172 | 172 |
173 void PostgreSQLResult::GetLargeObject(std::string& result, | 173 std::string PostgreSQLResult::GetLargeObjectOid(unsigned int column) const |
174 unsigned int column) const | |
175 { | 174 { |
176 CheckColumn(column, OIDOID); | 175 CheckColumn(column, OIDOID); |
177 | 176 |
178 /** | 177 /** |
179 * In PostgreSQL, the type "Oid" corresponds to "unsigned int", cf. | 178 * In PostgreSQL, the type "Oid" corresponds to "unsigned int", cf. |
183 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == sizeof(oid)); | 182 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == sizeof(oid)); |
184 | 183 |
185 oid = *(const Oid*) PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | 184 oid = *(const Oid*) PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); |
186 oid = ntohl(oid); | 185 oid = ntohl(oid); |
187 | 186 |
188 PostgreSQLLargeObject::Read(result, database_, boost::lexical_cast<std::string>(oid)); | 187 return boost::lexical_cast<std::string>(oid); |
189 } | 188 } |
190 | 189 |
191 | 190 |
192 void PostgreSQLResult::GetLargeObject(void*& result, | 191 void PostgreSQLResult::GetLargeObjectContent(std::string& content, |
193 size_t& size, | 192 unsigned int column) const |
194 unsigned int column) const | 193 { |
195 { | 194 PostgreSQLLargeObject::Read(content, database_, GetLargeObjectOid(column)); |
196 CheckColumn(column, OIDOID); | 195 } |
197 | 196 |
198 Oid oid; | 197 |
199 assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == sizeof(oid)); | 198 class PostgreSQLResult::LargeObjectResult : public ResultFileValue |
200 | 199 { |
201 oid = *(const Oid*) PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column); | 200 private: |
202 oid = ntohl(oid); | 201 PostgreSQLDatabase& database_; |
203 | 202 std::string oid_; |
204 PostgreSQLLargeObject::Read(result, size, database_, boost::lexical_cast<std::string>(oid)); | 203 |
205 } | 204 public: |
205 LargeObjectResult(PostgreSQLDatabase& database, | |
206 const std::string& oid) : | |
207 database_(database), | |
208 oid_(oid) | |
209 { | |
210 } | |
211 | |
212 virtual void ReadWhole(std::string& target) const ORTHANC_OVERRIDE | |
213 { | |
214 PostgreSQLLargeObject::Read(target, database_, oid_); | |
215 | |
216 } | |
217 | |
218 virtual void ReadRange(std::string& target, | |
219 uint64_t start, | |
220 size_t length) const ORTHANC_OVERRIDE | |
221 { | |
222 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
223 } | |
224 }; | |
206 | 225 |
207 | 226 |
208 IValue* PostgreSQLResult::GetValue(unsigned int column) const | 227 IValue* PostgreSQLResult::GetValue(unsigned int column) const |
209 { | 228 { |
210 if (IsNull(column)) | 229 if (IsNull(column)) |
232 | 251 |
233 case BYTEAOID: | 252 case BYTEAOID: |
234 return new BinaryStringValue(GetString(column)); | 253 return new BinaryStringValue(GetString(column)); |
235 | 254 |
236 case OIDOID: | 255 case OIDOID: |
237 { | 256 return new LargeObjectResult(database_, GetLargeObjectOid(column)); |
238 std::unique_ptr<ResultFileValue> value(new ResultFileValue); | |
239 GetLargeObject(value->GetContent(), column); | |
240 return value.release(); | |
241 } | |
242 | 257 |
243 default: | 258 default: |
244 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | 259 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); |
245 } | 260 } |
246 } | 261 } |