libpqxx  7.3.1
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 #include "pqxx/dbtransaction.hxx"
22 
23 
24 namespace pqxx
25 {
39 class PQXX_LIBEXPORT blob
40 {
41 public:
43 
47  [[nodiscard]] static oid create(dbtransaction &, oid = 0);
48 
50  static void remove(dbtransaction &, oid);
51 
53  [[nodiscard]] static blob open_r(dbtransaction &, oid);
54  // Open blob for writing. Any attempt to read from it will fail.
55  [[nodiscard]] static blob open_w(dbtransaction &, oid);
56  // Open blob for reading and/or writing.
57  [[nodiscard]] static blob open_rw(dbtransaction &, oid);
58 
60 
62  blob() = default;
63 
65  blob(blob &&);
67  blob &operator=(blob &&);
68 
69  blob(blob const &) = delete;
70  blob &operator=(blob const &) = delete;
71  ~blob();
72 
74 
80  static constexpr std::size_t chunk_limit = 0x7fffffff;
81 
83 
90  std::size_t read(std::basic_string<std::byte> &buf, std::size_t size);
91 
93 
111  void write(std::basic_string_view<std::byte> data);
112 
114 
120  void resize(std::int64_t size);
121 
123  [[nodiscard]] std::int64_t tell() const;
124 
126  std::int64_t seek_abs(std::int64_t offset = 0);
128  std::int64_t seek_rel(std::int64_t offset = 0);
130  std::int64_t seek_end(std::int64_t offset = 0);
131 
133 
136  static oid from_buf(
137  dbtransaction &tx, std::basic_string_view<std::byte> data, oid id = 0);
138 
140 
142  static void append_from_buf(
143  dbtransaction &tx, std::basic_string_view<std::byte> data, oid id);
144 
146  static oid from_file(dbtransaction &, char const path[]);
147 
149 
152  static oid from_file(dbtransaction &, char const path[], oid);
153 
155 
158  static void to_buf(
159  dbtransaction &, oid, std::basic_string<std::byte> &,
160  std::size_t max_size);
161 
163 
169  static std::size_t append_to_buf(
170  dbtransaction &tx, oid id, std::int64_t offset,
171  std::basic_string<std::byte> &buf, std::size_t append_max);
172 
174  static void to_file(dbtransaction &, oid, char const path[]);
175 
177 
184  void close();
185 
186 private:
187  PQXX_PRIVATE blob(connection &conn, int fd) noexcept :
188  m_conn{&conn}, m_fd{fd}
189  {}
190  static PQXX_PRIVATE blob open_internal(dbtransaction &, oid, int);
191  static PQXX_PRIVATE pqxx::internal::pq::PGconn *
192  raw_conn(pqxx::connection *) noexcept;
193  static PQXX_PRIVATE pqxx::internal::pq::PGconn *
194  raw_conn(pqxx::dbtransaction const &) noexcept;
195  static PQXX_PRIVATE std::string errmsg(connection const *);
196  static PQXX_PRIVATE std::string errmsg(dbtransaction const &tx)
197  {
198  return errmsg(&tx.conn());
199  }
200  PQXX_PRIVATE std::string errmsg() const { return errmsg(m_conn); }
201  PQXX_PRIVATE std::int64_t seek(std::int64_t offset, int whence);
202 
203  connection *m_conn = nullptr;
204  int m_fd = -1;
205 };
206 } // namespace pqxx
207 #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:40
blob(blob const &)=delete
blob()=default
You can default-construct a blob, but it won't do anything useful.
blob & operator=(blob const &)=delete
Connection to a database.
Definition: connection.hxx:170
Abstract transaction base class: bracket transactions on the database.
Definition: dbtransaction.hxx:53