diff 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
line wrap: on
line diff
--- a/Framework/PostgreSQL/PostgreSQLResult.cpp	Tue Apr 13 18:50:44 2021 +0200
+++ b/Framework/PostgreSQL/PostgreSQLResult.cpp	Wed Apr 14 08:42:31 2021 +0200
@@ -170,8 +170,7 @@
   }
 
 
-  void PostgreSQLResult::GetLargeObject(std::string& result,
-                                        unsigned int column) const
+  std::string PostgreSQLResult::GetLargeObjectOid(unsigned int column) const
   {
     CheckColumn(column, OIDOID);
 
@@ -185,24 +184,44 @@
     oid = *(const Oid*) PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column);
     oid = ntohl(oid);
 
-    PostgreSQLLargeObject::Read(result, database_, boost::lexical_cast<std::string>(oid));
+    return boost::lexical_cast<std::string>(oid);
+  }
+
+
+  void PostgreSQLResult::GetLargeObjectContent(std::string& content,
+                                               unsigned int column) const
+  {
+    PostgreSQLLargeObject::Read(content, database_, GetLargeObjectOid(column));
   }
 
 
-  void PostgreSQLResult::GetLargeObject(void*& result,
-                                           size_t& size,
-                                           unsigned int column) const
+  class PostgreSQLResult::LargeObjectResult : public ResultFileValue
   {
-    CheckColumn(column, OIDOID);
+  private:
+    PostgreSQLDatabase&  database_;
+    std::string          oid_;
+
+  public:
+    LargeObjectResult(PostgreSQLDatabase& database,
+                      const std::string& oid) :
+      database_(database),
+      oid_(oid)
+    {
+    }
 
-    Oid oid;
-    assert(PQfsize(reinterpret_cast<PGresult*>(result_), column) == sizeof(oid));
+    virtual void ReadWhole(std::string& target) const ORTHANC_OVERRIDE
+    {
+      PostgreSQLLargeObject::Read(target, database_, oid_);
 
-    oid = *(const Oid*) PQgetvalue(reinterpret_cast<PGresult*>(result_), position_, column);
-    oid = ntohl(oid);
-
-    PostgreSQLLargeObject::Read(result, size, database_, boost::lexical_cast<std::string>(oid));
-  }
+    }
+    
+    virtual void ReadRange(std::string& target,
+                           uint64_t start,
+                           size_t length) const ORTHANC_OVERRIDE
+    {
+      throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);
+    }
+  };
 
 
   IValue* PostgreSQLResult::GetValue(unsigned int column) const
@@ -234,11 +253,7 @@
         return new BinaryStringValue(GetString(column));
 
       case OIDOID:
-      {
-        std::unique_ptr<ResultFileValue> value(new ResultFileValue);
-        GetLargeObject(value->GetContent(), column);
-        return value.release();
-      }
+        return new LargeObjectResult(database_, GetLargeObjectOid(column));
 
       default:
         throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented);