backends/postgresql/blob.cpp
Go to the documentation of this file.
1 //
2 // Copyright (C) 2004-2008 Maciej Sobczak, Stephen Hutton
3 // Distributed under the Boost Software License, Version 1.0.
4 // (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 
8 #define SOCI_POSTGRESQL_SOURCE
9 #include "soci-postgresql.h"
10 #include <libpq/libpq-fs.h> // libpq
11 #include <cctype>
12 #include <cstdio>
13 #include <cstring>
14 #include <ctime>
15 #include <sstream>
16 
17 #ifdef SOCI_POSTGRESQL_NOPARAMS
18 #ifndef SOCI_POSTGRESQL_NOBINDBYNAME
19 #define SOCI_POSTGRESQL_NOBINDBYNAME
20 #endif // SOCI_POSTGRESQL_NOBINDBYNAME
21 #endif // SOCI_POSTGRESQL_NOPARAMS
22 
23 #ifdef _MSC_VER
24 #pragma warning(disable:4355)
25 #endif
26 
27 using namespace soci;
28 using namespace soci::details;
29 
30 
33  : session_(session), fd_(-1)
34 {
35  // nothing to do here, the descriptor is open in the postFetch
36  // method of the Into element
37 }
38 
40 {
41  lo_close(session_.conn_, fd_);
42 }
43 
45 {
46  int const pos = lo_lseek(session_.conn_, fd_, 0, SEEK_END);
47  if (pos == -1)
48  {
49  throw soci_error("Cannot retrieve the size of BLOB.");
50  }
51 
52  return static_cast<std::size_t>(pos);
53 }
54 
56  std::size_t offset, char * buf, std::size_t toRead)
57 {
58  int const pos = lo_lseek(session_.conn_, fd_,
59  static_cast<int>(offset), SEEK_SET);
60  if (pos == -1)
61  {
62  throw soci_error("Cannot seek in BLOB.");
63  }
64 
65  int const readn = lo_read(session_.conn_, fd_, buf, toRead);
66  if (readn < 0)
67  {
68  throw soci_error("Cannot read from BLOB.");
69  }
70 
71  return static_cast<std::size_t>(readn);
72 }
73 
75  std::size_t offset, char const * buf, std::size_t toWrite)
76 {
77  int const pos = lo_lseek(session_.conn_, fd_,
78  static_cast<int>(offset), SEEK_SET);
79  if (pos == -1)
80  {
81  throw soci_error("Cannot seek in BLOB.");
82  }
83 
84  int const writen = lo_write(session_.conn_, fd_,
85  const_cast<char *>(buf), toWrite);
86  if (writen < 0)
87  {
88  throw soci_error("Cannot write to BLOB.");
89  }
90 
91  return static_cast<std::size_t>(writen);
92 }
93 
95  char const * buf, std::size_t toWrite)
96 {
97  int const pos = lo_lseek(session_.conn_, fd_, 0, SEEK_END);
98  if (pos == -1)
99  {
100  throw soci_error("Cannot seek in BLOB.");
101  }
102 
103  int const writen = lo_write(session_.conn_, fd_,
104  const_cast<char *>(buf), toWrite);
105  if (writen < 0)
106  {
107  throw soci_error("Cannot append to BLOB.");
108  }
109 
110  return static_cast<std::size_t>(writen);
111 }
112 
113 void postgresql_blob_backend::trim(std::size_t /* newLen */)
114 {
115  throw soci_error("Trimming BLOBs is not supported.");
116 }
virtual std::size_t append(char const *buf, std::size_t toWrite)
virtual void trim(std::size_t newLen)
postgresql_session_backend & session_
virtual std::size_t read(std::size_t offset, char *buf, std::size_t toRead)
postgresql_blob_backend(postgresql_session_backend &session)
virtual std::size_t write(std::size_t offset, char const *buf, std::size_t toWrite)


asr_lib_ism
Author(s): Hanselmann Fabian, Heller Florian, Heizmann Heinrich, Kübler Marcel, Mehlhaus Jonas, Meißner Pascal, Qattan Mohamad, Reckling Reno, Stroh Daniel
autogenerated on Wed Jan 8 2020 04:02:40