libpqxx  7.5.2
blob.hxx
1 /* Binary Large Objects interface.
2  *
3  * Read or write large objects, stored in their own storage on the server.
4  *
5  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/largeobject instead.
6  *
7  * Copyright (c) 2000-2021, Jeroen T. Vermeulen.
8  *
9  * See COPYING for copyright license. If you did not receive a file called
10  * COPYING with this source code, please notify the distributor of this
11  * mistake, or contact the author.
12  */
13 #ifndef PQXX_H_BLOB
14 #define PQXX_H_BLOB
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
18 
19 #include <cstdint>
20 
21 #if defined(PQXX_HAVE_SPAN) && __has_include(<span>)
22 # include <span>
23 #endif
24 
25 #include "pqxx/dbtransaction.hxx"
26 
27 
28 namespace pqxx
29 {
43 class PQXX_LIBEXPORT blob
44 {
45 public:
47 
51  [[nodiscard]] static oid create(dbtransaction &, oid = 0);
52 
54  static void remove(dbtransaction &, oid);
55 
57  [[nodiscard]] static blob open_r(dbtransaction &, oid);
58  // Open blob for writing. Any attempt to read from it will fail.
59  [[nodiscard]] static blob open_w(dbtransaction &, oid);
60  // Open blob for reading and/or writing.
61  [[nodiscard]] static blob open_rw(dbtransaction &, oid);
62 
64 
66  blob() = default;
67 
69  blob(blob &&);
71  blob &operator=(blob &&);
72 
73  blob(blob const &) = delete;
74  blob &operator=(blob const &) = delete;
75  ~blob();
76 
78 
84  static constexpr std::size_t chunk_limit = 0x7fffffff;
85 
87 
95  std::size_t read(std::basic_string<std::byte> &buf, std::size_t size);
96 
97 #if defined(PQXX_HAVE_SPAN)
98 
100 
105  std::span<std::byte> read(std::span<std::byte> buf)
106  {
107  return buf.subspan(0, raw_read(buf.data(), std::size(buf)));
108  }
109 
110 #else
111 
113 
125  template<typename ALLOC>
126  std::basic_string_view<std::byte> read(std::vector<std::byte, ALLOC> &buf)
127  {
128  return std::basic_string_view<std::byte>{
129  buf.data(), raw_read(buf.data(), std::size(buf))};
130  }
131 
132 #endif // PQXX_HAVE_SPAN
133 
134 
136 
154  void write(std::basic_string_view<std::byte> data);
155 
157 
163  void resize(std::int64_t size);
164 
166  [[nodiscard]] std::int64_t tell() const;
167 
169  std::int64_t seek_abs(std::int64_t offset = 0);
171  std::int64_t seek_rel(std::int64_t offset = 0);
173  std::int64_t seek_end(std::int64_t offset = 0);
174 
176 
179  static oid from_buf(
180  dbtransaction &tx, std::basic_string_view<std::byte> data, oid id = 0);
181 
183 
185  static void append_from_buf(
186  dbtransaction &tx, std::basic_string_view<std::byte> data, oid id);
187 
189  static oid from_file(dbtransaction &, char const path[]);
190 
192 
195  static oid from_file(dbtransaction &, char const path[], oid);
196 
198 
201  static void to_buf(
202  dbtransaction &, oid, std::basic_string<std::byte> &,
203  std::size_t max_size);
204 
206 
212  static std::size_t append_to_buf(
213  dbtransaction &tx, oid id, std::int64_t offset,
214  std::basic_string<std::byte> &buf, std::size_t append_max);
215 
217  static void to_file(dbtransaction &, oid, char const path[]);
218 
220 
227  void close();
228 
229 private:
230  PQXX_PRIVATE blob(connection &conn, int fd) noexcept :
231  m_conn{&conn}, m_fd{fd}
232  {}
233  static PQXX_PRIVATE blob open_internal(dbtransaction &, oid, int);
234  static PQXX_PRIVATE pqxx::internal::pq::PGconn *
235  raw_conn(pqxx::connection *) noexcept;
236  static PQXX_PRIVATE pqxx::internal::pq::PGconn *
237  raw_conn(pqxx::dbtransaction const &) noexcept;
238  static PQXX_PRIVATE std::string errmsg(connection const *);
239  static PQXX_PRIVATE std::string errmsg(dbtransaction const &tx)
240  {
241  return errmsg(&tx.conn());
242  }
243  PQXX_PRIVATE std::string errmsg() const { return errmsg(m_conn); }
244  PQXX_PRIVATE std::int64_t seek(std::int64_t offset, int whence);
245  std::size_t raw_read(std::byte buf[], std::size_t size);
246 
247  connection *m_conn = nullptr;
248  int m_fd = -1;
249 };
250 } // namespace pqxx
251 #endif
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
std::vector< std::string_view > to_buf(char *here, char const *end, TYPE... value)
Convert multiple values to strings inside a single buffer.
Definition: strconv.hxx:332
Definition: blob.hxx:44
blob(blob const &)=delete
blob()=default
You can default-construct a blob, but it won't do anything useful.
blob & operator=(blob const &)=delete
std::basic_string_view< std::byte > read(std::vector< std::byte, ALLOC > &buf)
Read up to std::size(buf) bytes from the object.
Definition: blob.hxx:126
Connection to a database.
Definition: connection.hxx:184
Abstract transaction base class: bracket transactions on the database.
Definition: dbtransaction.hxx:53