11#ifndef PQXX_H_LARGEOBJECT
12#define PQXX_H_LARGEOBJECT
16#include "pqxx/dbtransaction.hxx"
36 [[deprecated(
"Use blob instead.")]]
largeobject() noexcept = default;
57 [[deprecated(
"Use blob instead.")]] largeobject(
58 dbtransaction &t, std::string_view file);
65 [[deprecated(
"Use blob instead.")]] largeobject(
66 largeobjectaccess
const &o)
noexcept;
73 [[nodiscard]] oid
id() const noexcept {
return m_id; }
87 return m_id == other.m_id;
93 return m_id != other.m_id;
99 return m_id <= other.m_id;
105 return m_id >= other.m_id;
111 return m_id < other.m_id;
117 return m_id > other.m_id;
136 PQXX_PURE
static internal::pq::PGconn *
139 PQXX_PRIVATE std::string reason(
connection const &,
int err)
const;
167 static constexpr auto default_mode{
168 std::ios::in | std::ios::out | std::ios::binary};
218 using largeobject::id;
226 largeobject::to_file(m_trans, file);
229 using largeobject::to_file;
241 void write(
char const buf[], std::size_t len);
247 void write(std::string_view buf) {
write(std::data(buf), std::size(buf)); }
256 size_type read(
char buf[], std::size_t len);
262 size_type seek(size_type dest, seekdir dir);
268 [[nodiscard]] size_type tell()
const;
292 pos_type cseek(off_type dest, seekdir dir)
noexcept;
301 off_type cwrite(
char const buf[], std::size_t len)
noexcept;
310 off_type cread(
char buf[], std::size_t len)
noexcept;
317 [[nodiscard]] pos_type ctell() const noexcept;
325 void process_notice(
zview) noexcept;
342 PQXX_PRIVATE std::
string reason(
int err) const;
343 internal::pq::PGconn *raw_connection()
const
345 return largeobject::raw_connection(m_trans);
348 PQXX_PRIVATE
void open(openmode mode);
349 void close() noexcept;
368template<typename CHAR =
char, typename TRAITS = std::char_traits<CHAR>>
383 static constexpr auto default_mode{
384 std::ios::in | std::ios::out | std::ios::binary};
388 size_type buf_size = 512) :
389 m_bufsize{buf_size}, m_obj{t, o, mode}, m_g{nullptr}, m_p{nullptr}
396 size_type buf_size = 512) :
397 m_bufsize{buf_size}, m_obj{t, o, mode}, m_g{nullptr}, m_p{nullptr}
415 this->setg(this->eback(), this->eback(), this->egptr());
416 return overflow(eof());
428 return adjust_eof(newpos);
433 auto *
const pp{this->pptr()};
436 auto *
const pb{this->pbase()};
441 auto const write_sz{pp - pb};
442 auto const written_sz{
443 m_obj.cwrite(pb,
static_cast<std::size_t
>(pp - pb))};
444 if (internal::cmp_less_equal(written_sz, 0))
446 "pqxx::largeobject: write failed "
447 "(is transaction still valid on write or flush?), "
448 "libpq reports error"};
449 else if (write_sz != written_sz)
451 "pqxx::largeobject: write failed "
452 "(is transaction still valid on write or flush?), " +
453 std::to_string(written_sz) +
"/" + std::to_string(write_sz) +
455 auto const out{adjust_eof(written_sz)};
457 if constexpr (std::is_arithmetic_v<
decltype(out)>)
458 res = check_cast<int_type>(out,
"largeobject position"sv);
462 this->setp(m_p, m_p + m_bufsize);
467 *this->pptr() =
static_cast<char_type>(ch);
477 if (this->gptr() ==
nullptr)
479 auto *
const eb{this->eback()};
480 auto const res{adjust_eof(
481 m_obj.cread(this->eback(),
static_cast<std::size_t
>(m_bufsize)))};
483 eb, eb, eb + (res == eof() ? 0 :
static_cast<std::size_t
>(res)));
484 return (res == eof() or res == 0) ? eof() : traits_type::to_int_type(*eb);
489 static int_type eof() {
return traits_type::eof(); }
492 template<
typename INTYPE>
static std::streampos adjust_eof(INTYPE pos)
494 bool const at_eof{pos == -1};
495 if constexpr (std::is_arithmetic_v<std::streampos>)
497 return check_cast<std::streampos>(
498 (at_eof ? eof() : pos),
"large object seek"sv);
502 return std::streampos(at_eof ? eof() : pos);
506 void initialize(openmode mode)
508 if ((mode & std::ios::in) != 0)
510 m_g =
new char_type[unsigned(m_bufsize)];
511 this->setg(m_g, m_g, m_g);
513 if ((mode & std::ios::out) != 0)
515 m_p =
new char_type[unsigned(m_bufsize)];
516 this->setp(m_p, m_p + m_bufsize);
520 size_type
const m_bufsize;
521 largeobjectaccess m_obj;
524 char_type *m_g, *m_p;
538template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
541 using super = std::basic_istream<CHAR, TRAITS>;
559 m_buf{t, o, std::ios::in | std::ios::
binary, buf_size}
573 m_buf{t, o, std::ios::in | std::ios::
binary, buf_size}
594template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
597 using super = std::basic_ostream<CHAR, TRAITS>;
615 m_buf{t, o, std::ios::out | std::ios::
binary, buf_size}
629 m_buf{t, o, std::ios::out | std::ios::
binary, buf_size}
641 catch (std::exception
const &e)
643 m_buf.process_notice(e.what());
664template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
667 using super = std::basic_iostream<CHAR, TRAITS>;
686 t, o, std::ios::in | std::ios::out | std::ios::
binary, buf_size}
701 t, o, std::ios::in | std::ios::out | std::ios::
binary, buf_size}
713 catch (std::exception
const &e)
715 m_buf.process_notice(e.what());
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:23
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:328
int64_t large_object_size_type
Number of bytes in a large object.
Definition: types.hxx:39
Connection to a database.
Definition: connection.hxx:181
Abstract transaction base class: bracket transactions on the database.
Definition: dbtransaction.hxx:50
Internal error in libpqxx library.
Definition: except.hxx:155
Identity of a large object.
Definition: largeobject.hxx:30
bool operator==(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:85
bool operator>=(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:103
bool operator<=(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:97
large_object_size_type size_type
Definition: largeobject.hxx:32
bool operator<(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:109
largeobject() noexcept=default
bool operator!=(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:91
bool operator>(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:115
oid id() const noexcept
Object identifier.
Definition: largeobject.hxx:73
Accessor for large object's contents.
Definition: largeobject.hxx:150
size_type pos_type
Definition: largeobject.hxx:154
std::ios::openmode openmode
Open mode: in, out (can be combined using "bitwise or").
Definition: largeobject.hxx:164
std::ios::seekdir seekdir
Seek direction: beg, cur, end.
Definition: largeobject.hxx:171
void to_file(std::string_view file) const
Export large object's contents to a local file.
Definition: largeobject.hxx:224
~largeobjectaccess() noexcept
Definition: largeobject.hxx:212
void write(std::string_view buf)
Write string to large object.
Definition: largeobject.hxx:247
size_type off_type
Definition: largeobject.hxx:153
Streambuf to use large objects in standard I/O streams.
Definition: largeobject.hxx:370
virtual pos_type seekoff(off_type offset, seekdir dir, openmode) override
Definition: largeobject.hxx:419
virtual int_type overflow(int_type ch) override
Definition: largeobject.hxx:431
TRAITS traits_type
Definition: largeobject.hxx:375
typename traits_type::int_type int_type
Definition: largeobject.hxx:376
virtual pos_type seekpos(pos_type pos, openmode) override
Definition: largeobject.hxx:424
largeobject_streambuf(dbtransaction &t, oid o, openmode mode=default_mode, size_type buf_size=512)
Definition: largeobject.hxx:394
CHAR char_type
Definition: largeobject.hxx:374
typename traits_type::off_type off_type
Definition: largeobject.hxx:378
virtual int_type overflow()
Definition: largeobject.hxx:473
virtual ~largeobject_streambuf() noexcept
Definition: largeobject.hxx:402
void process_notice(zview const &s)
For use by large object stream classes.
Definition: largeobject.hxx:409
virtual int sync() override
Definition: largeobject.hxx:412
largeobjectaccess::seekdir seekdir
Definition: largeobject.hxx:380
virtual int_type underflow() override
Definition: largeobject.hxx:475
largeobjectaccess::openmode openmode
Definition: largeobject.hxx:379
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:377
largeobject_streambuf(dbtransaction &t, largeobject o, openmode mode=default_mode, size_type buf_size=512)
Definition: largeobject.hxx:386
Input stream that gets its data from a large object.
Definition: largeobject.hxx:540
basic_ilostream(dbtransaction &t, oid o, largeobject::size_type buf_size=512)
Create a basic_ilostream.
Definition: largeobject.hxx:570
CHAR char_type
Definition: largeobject.hxx:544
basic_ilostream(dbtransaction &t, largeobject o, largeobject::size_type buf_size=512)
Create a basic_ilostream.
Definition: largeobject.hxx:556
typename traits_type::int_type int_type
Definition: largeobject.hxx:546
TRAITS traits_type
Definition: largeobject.hxx:545
typename traits_type::off_type off_type
Definition: largeobject.hxx:548
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:547
Output stream that writes data back to a large object.
Definition: largeobject.hxx:596
typename traits_type::off_type off_type
Definition: largeobject.hxx:604
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:603
basic_olostream(dbtransaction &t, largeobject o, largeobject::size_type buf_size=512)
Create a basic_olostream.
Definition: largeobject.hxx:612
typename traits_type::int_type int_type
Definition: largeobject.hxx:602
~basic_olostream()
Definition: largeobject.hxx:634
CHAR char_type
Definition: largeobject.hxx:600
basic_olostream(dbtransaction &t, oid o, largeobject::size_type buf_size=512)
Create a basic_olostream.
Definition: largeobject.hxx:626
TRAITS traits_type
Definition: largeobject.hxx:601
Stream that reads and writes a large object.
Definition: largeobject.hxx:666
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:673
~basic_lostream()
Definition: largeobject.hxx:706
typename traits_type::int_type int_type
Definition: largeobject.hxx:672
typename traits_type::off_type off_type
Definition: largeobject.hxx:674
basic_lostream(dbtransaction &t, largeobject o, largeobject::size_type buf_size=512)
Create a basic_lostream.
Definition: largeobject.hxx:682
basic_lostream(dbtransaction &t, oid o, largeobject::size_type buf_size=512)
Create a basic_lostream.
Definition: largeobject.hxx:697
TRAITS traits_type
Definition: largeobject.hxx:671
CHAR char_type
Definition: largeobject.hxx:670
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:38