libpqxx
The C++ client library for PostgreSQL
blob.hxx
Go to the documentation of this file.
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-2026, 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_BLOB_HXX
14 #define PQXX_BLOB_HXX
15 
16 #if !defined(PQXX_HEADER_PRE)
17 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
18 #endif
19 
20 #include <cstdint>
21 
22 #include <ranges>
23 #include <span>
24 
25 #include "pqxx/dbtransaction.hxx"
26 
27 
28 namespace pqxx
29 {
43 class PQXX_LIBEXPORT blob final
44 {
45 public:
47 
51  [[nodiscard]] static oid
52  create(dbtransaction &, oid = 0, sl = sl::current());
53 
55  static void remove(dbtransaction &, oid, sl = sl::current());
56 
58  [[nodiscard]] static blob open_r(dbtransaction &, oid, sl = sl::current());
59  // Open blob for writing. Any attempt to read from it will fail.
60  [[nodiscard]] static blob open_w(dbtransaction &, oid, sl = sl::current());
61  // Open blob for reading and/or writing.
62  [[nodiscard]] static blob open_rw(dbtransaction &, oid, sl = sl::current());
63 
65 
68  blob() = default;
69 
71  blob(blob &&);
73  blob &operator=(blob &&);
74 
75  blob(blob const &) = delete;
76  blob &operator=(blob const &) = delete;
77  ~blob();
78 
80 
86  static constexpr std::size_t chunk_limit = 0x7fffffff;
87 
89 
97  std::size_t read(bytes &buf, std::size_t size, sl = sl::current());
98 
100 
105  template<std::size_t extent = std::dynamic_extent>
107  read(std::span<std::byte, extent> buf, sl loc = sl::current())
108  {
109  return buf.first(raw_read(std::data(buf), std::size(buf), loc));
110  }
111 
113 
118  template<binary DATA>
119  writable_bytes_view read(DATA &buf, sl loc = sl::current())
120  {
121  return {std::data(buf), raw_read(std::data(buf), std::size(buf), loc)};
122  }
123 
125 
143  template<binary DATA> void write(DATA const &data, sl loc = sl::current())
144  {
145  return raw_write(binary_cast(data), loc);
146  }
147 
149 
155  void resize(std::int64_t size, sl = sl::current());
156 
158  [[nodiscard]] std::int64_t tell(sl = sl::current()) const;
159 
161 
162  std::int64_t seek_abs(std::int64_t offset = 0, sl = sl::current());
163 
165 
169  std::int64_t seek_rel(std::int64_t offset = 0, sl = sl::current());
170 
172 
176  std::int64_t seek_end(std::int64_t offset = 0, sl = sl::current());
177 
179 
182  static oid
183  from_buf(dbtransaction &tx, bytes_view data, oid id = 0, sl = sl::current());
184 
186 
189  template<binary DATA>
190  static oid from_buf(
191  dbtransaction &tx, DATA const &data, oid id = 0, sl loc = sl::current())
192  {
193  return from_buf(tx, binary_cast(data), id, loc);
194  }
195 
197 
199  static void append_from_buf(
200  dbtransaction &tx, bytes_view data, oid id, sl = sl::current());
201 
203 
205  template<binary DATA>
206  static void append_from_buf(
207  dbtransaction &tx, DATA const &data, oid id, sl loc = sl::current())
208  {
209  append_from_buf(tx, binary_cast(data), id, loc);
210  }
211 
213  [[nodiscard]] static oid
214  from_file(dbtransaction &, zview path, sl = sl::current());
215 
217 
220  static oid from_file(dbtransaction &, zview path, oid, sl = sl::current());
221 
223 
226  static void to_buf(
227  dbtransaction &, oid, bytes &, std::size_t max_size, sl = sl::current());
228 
230 
236  static std::size_t append_to_buf(
237  dbtransaction &tx, oid id, std::int64_t offset, bytes &buf,
238  std::size_t append_max, sl = sl::current());
239 
241  static void to_file(dbtransaction &, oid, zview path, sl = sl::current());
242 
244 
255  void close();
256 
257 private:
258  PQXX_PRIVATE blob(connection &cx, int fd) noexcept : m_conn{&cx}, m_fd{fd} {}
259  static blob open_internal(dbtransaction &, oid, int, sl);
261  raw_conn(pqxx::connection *) noexcept;
263  raw_conn(pqxx::dbtransaction const &) noexcept;
264  PQXX_PRIVATE static std::string errmsg(connection const *);
265  PQXX_PRIVATE static std::string errmsg(dbtransaction const &tx)
266  {
267  return errmsg(&tx.conn());
268  }
269  PQXX_PRIVATE [[nodiscard]] std::string errmsg() const
270  {
271  return errmsg(m_conn);
272  }
273  PQXX_PRIVATE std::int64_t seek(std::int64_t offset, int whence, sl);
274  std::size_t raw_read(std::byte buf[], std::size_t size, sl);
275  void raw_write(bytes_view, sl);
276 
277  connection *m_conn = nullptr;
278  int m_fd = -1;
279 };
280 } // namespace pqxx
281 #endif
Definition: blob.hxx:44
static oid from_buf(dbtransaction &tx, DATA const &data, oid id=0, sl loc=sl::current())
Create a binary large object containing given data.
Definition: blob.hxx:190
writable_bytes_view read(std::span< std::byte, extent > buf, sl loc=sl::current())
Read up to std::size(buf) bytes from the object.
Definition: blob.hxx:107
blob(blob const &)=delete
blob()=default
You can default-construct a blob, but it won't do anything useful.
blob & operator=(blob const &)=delete
static void append_from_buf(dbtransaction &tx, DATA const &data, oid id, sl loc=sl::current())
Append data to binary large object.
Definition: blob.hxx:206
writable_bytes_view read(DATA &buf, sl loc=sl::current())
Read up to std::size(buf) bytes from the object.
Definition: blob.hxx:119
void write(DATA const &data, sl loc=sl::current())
Write data to large object, at the current position.
Definition: blob.hxx:143
Connection to a database.
Definition: connection.hxx:273
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:55
Abstract transaction base class: bracket transactions on the database.
Definition: dbtransaction.hxx:54
#define PQXX_LIBEXPORT
Definition: header-pre.hxx:206
#define PQXX_PURE
Definition: header-pre.hxx:64
#define PQXX_PRIVATE
Definition: header-pre.hxx:207
void PGconn
Placeholder for libpq's connection type.
Definition: types.hxx:429
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
std::string_view to_buf(std::span< char > buf, TYPE const &value, ctx c={})
Represent value as SQL text, optionally using buf as storage.
Definition: strconv.hxx:430
std::span< std::byte const > bytes_view
Type alias for a view of bytes.
Definition: types.hxx:188
std::source_location sl
Convenience alias for std::source_location. It's just too long.
Definition: types.hxx:38
bytes_view binary_cast(TYPE const &data)
Cast binary data to a type that libpqxx will recognise as binary.
Definition: util.hxx:260
std::span< std::byte > writable_bytes_view
Type alias for a view of writable bytes.
Definition: types.hxx:192
unsigned int oid
PostgreSQL database row identifier.
Definition: types.hxx:73
std::vector< std::byte > bytes
Type alias for a container containing bytes.
Definition: util.hxx:240