11 #ifndef PQXX_LARGEOBJECT_HXX
12 #define PQXX_LARGEOBJECT_HXX
14 #if !defined(PQXX_HEADER_PRE)
15 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
50 [[deprecated(
"Use blob instead.")]]
largeobject() noexcept = default;
71 [[deprecated(
"Use blob instead.")]] largeobject(
72 dbtransaction &t, std::string_view file);
80 [[deprecated(
"Use blob instead.")]] largeobject(
81 largeobjectaccess
const &o) noexcept;
89 [[nodiscard]]
oid id() const noexcept {
return m_id; }
103 return m_id == other.m_id;
109 return m_id != other.m_id;
115 return m_id <= other.m_id;
121 return m_id >= other.m_id;
127 return m_id < other.m_id;
133 return m_id > other.m_id;
184 static constexpr
auto default_mode{
264 void write(
char const buf[], std::size_t len);
270 void write(std::string_view buf) {
write(std::data(buf), std::size(buf)); }
279 size_type read(
char buf[], std::size_t len);
285 size_type seek(size_type dest, seekdir dir);
291 [[nodiscard]] size_type tell()
const;
315 pos_type cseek(off_type dest, seekdir dir) noexcept;
324 off_type cwrite(
char const buf[], std::size_t len) noexcept;
333 off_type cread(
char buf[], std::size_t len) noexcept;
340 [[nodiscard]] pos_type ctell() const noexcept;
348 void process_notice(
zview) noexcept;
361 PQXX_PRIVATE [[nodiscard]] std::
string reason(
int err) const;
362 [[nodiscard]] internal::pq::
PGconn *raw_connection()
const
368 void close() noexcept;
370 dbtransaction &m_trans;
389 template<typename CHAR =
char, typename TRAITS = std::char_traits<CHAR>>
404 static constexpr
auto default_mode{
410 size_type buf_size = 512) :
411 m_bufsize{buf_size}, m_obj{t, o, mode}, m_g{nullptr}, m_p{nullptr}
419 size_type buf_size = 512) :
420 m_bufsize{buf_size}, m_obj{t, o, mode}, m_g{nullptr}, m_p{nullptr}
444 this->setg(this->eback(), this->eback(), this->egptr());
445 return overflow(eof());
457 return adjust_eof(newpos);
462 auto *
const pp{this->pptr()};
465 auto *
const pb{this->pbase()};
470 auto const write_sz{pp - pb};
471 auto const written_sz{
472 m_obj.cwrite(pb,
static_cast<std::size_t
>(pp - pb))};
473 if (std::cmp_less_equal(written_sz, 0))
475 "pqxx::largeobject: write failed "
476 "(is transaction still valid on write or flush?), "
477 "libpq reports error"};
478 else if (write_sz != written_sz)
480 "pqxx::largeobject: write failed "
481 "(is transaction still valid on write or flush?), {}/{} "
483 written_sz, write_sz)};
484 auto const out{adjust_eof(written_sz)};
486 if constexpr (std::is_arithmetic_v<decltype(out)>)
487 res = check_cast<int_type>(out,
"largeobject position"sv);
491 this->setp(m_p, m_p + m_bufsize);
496 *this->pptr() =
static_cast<char_type>(ch);
504 if (this->gptr() ==
nullptr)
506 auto *
const eb{this->eback()};
507 auto const res{adjust_eof(
508 m_obj.cread(this->eback(),
static_cast<std::size_t
>(m_bufsize)))};
510 eb, eb, eb + (res == eof() ? 0 :
static_cast<std::size_t
>(res)));
511 return (res == eof() or res == 0) ? eof() : traits_type::to_int_type(*eb);
516 static constexpr int_type eof() {
return traits_type::eof(); }
519 template<
typename INTYPE>
static std::streampos adjust_eof(INTYPE pos)
521 bool const at_eof{pos == -1};
522 if constexpr (std::is_arithmetic_v<std::streampos>)
524 return check_cast<std::streampos>(
525 (at_eof ? eof() : pos),
"large object seek"sv);
529 return std::streampos(at_eof ? eof() : pos);
533 void initialize(openmode mode)
535 if ((mode & std::ios::in) != 0)
537 m_g =
new char_type[unsigned(m_bufsize)];
538 this->setg(m_g, m_g, m_g);
540 if ((mode & std::ios::out) != 0)
542 m_p =
new char_type[unsigned(m_bufsize)];
543 this->setp(m_p, m_p + m_bufsize);
547 size_type
const m_bufsize;
548 largeobjectaccess m_obj;
566 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
569 using super = std::basic_istream<CHAR, TRAITS>;
588 m_buf{t, o, std::ios::in | std::ios::
binary, buf_size}
603 m_buf{t, o, std::ios::in | std::ios::
binary, buf_size}
624 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
627 using super = std::basic_ostream<CHAR, TRAITS>;
646 m_buf{t, o, std::ios::out | std::ios::
binary, buf_size}
661 m_buf{t, o, std::ios::out | std::ios::
binary, buf_size}
676 catch (std::exception
const &e)
678 m_buf.process_notice(e.what());
702 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
705 using super = std::basic_iostream<CHAR, TRAITS>;
724 t, o, std::ios::in | std::ios::out | std::ios::
binary, buf_size}
739 t, o, std::ios::in | std::ios::out | std::ios::
binary, buf_size}
755 catch (std::exception
const &e)
757 m_buf.process_notice(e.what());
Input stream that gets its data from a large object.
Definition: largeobject.hxx:568
basic_ilostream(dbtransaction &t, oid o, largeobject::size_type buf_size=512)
Create a basic_ilostream.
Definition: largeobject.hxx:600
CHAR char_type
Definition: largeobject.hxx:572
basic_ilostream(dbtransaction &t, largeobject o, largeobject::size_type buf_size=512)
Create a basic_ilostream.
Definition: largeobject.hxx:585
typename traits_type::int_type int_type
Definition: largeobject.hxx:574
TRAITS traits_type
Definition: largeobject.hxx:573
typename traits_type::off_type off_type
Definition: largeobject.hxx:576
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:575
Stream that reads and writes a large object.
Definition: largeobject.hxx:704
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:711
typename traits_type::int_type int_type
Definition: largeobject.hxx:710
basic_lostream & operator=(basic_lostream &&)=delete
typename traits_type::off_type off_type
Definition: largeobject.hxx:712
basic_lostream(dbtransaction &t, largeobject o, largeobject::size_type buf_size=512)
Create a basic_lostream.
Definition: largeobject.hxx:720
~basic_lostream() override
Definition: largeobject.hxx:748
basic_lostream(dbtransaction &t, oid o, largeobject::size_type buf_size=512)
Create a basic_lostream.
Definition: largeobject.hxx:735
TRAITS traits_type
Definition: largeobject.hxx:709
basic_lostream(basic_lostream const &)=delete
basic_lostream(basic_lostream &&)=delete
CHAR char_type
Definition: largeobject.hxx:708
basic_lostream & operator=(basic_lostream const &)=delete
Output stream that writes data back to a large object.
Definition: largeobject.hxx:626
typename traits_type::off_type off_type
Definition: largeobject.hxx:634
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:633
basic_olostream(basic_olostream &&)=delete
basic_olostream(dbtransaction &t, largeobject o, largeobject::size_type buf_size=512)
Create a basic_olostream.
Definition: largeobject.hxx:643
typename traits_type::int_type int_type
Definition: largeobject.hxx:632
CHAR char_type
Definition: largeobject.hxx:630
basic_olostream & operator=(basic_olostream const &)=delete
~basic_olostream() override
Definition: largeobject.hxx:669
basic_olostream(dbtransaction &t, oid o, largeobject::size_type buf_size=512)
Create a basic_olostream.
Definition: largeobject.hxx:658
TRAITS traits_type
Definition: largeobject.hxx:631
basic_olostream(basic_olostream const &)=delete
basic_olostream & operator=(basic_olostream &&)=delete
Connection to a database.
Definition: connection.hxx:273
Streambuf to use large objects in standard I/O streams.
Definition: largeobject.hxx:391
largeobject_streambuf(largeobject_streambuf const &)=delete
TRAITS traits_type
Definition: largeobject.hxx:396
typename traits_type::int_type int_type
Definition: largeobject.hxx:397
int_type underflow() override
Definition: largeobject.hxx:502
largeobject_streambuf(dbtransaction &t, oid o, openmode mode=default_mode, size_type buf_size=512)
Definition: largeobject.hxx:417
CHAR char_type
Definition: largeobject.hxx:395
typename traits_type::off_type off_type
Definition: largeobject.hxx:399
void process_notice(zview const &s)
For use by large object stream classes.
Definition: largeobject.hxx:438
largeobject_streambuf & operator=(largeobject_streambuf &&)=delete
largeobjectaccess::seekdir seekdir
Definition: largeobject.hxx:401
largeobject_streambuf(largeobject_streambuf &&)=delete
largeobjectaccess::openmode openmode
Definition: largeobject.hxx:400
int_type overflow(int_type ch=eof()) override
Definition: largeobject.hxx:460
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:398
largeobject_streambuf & operator=(largeobject_streambuf const &)=delete
~largeobject_streambuf() noexcept override
Definition: largeobject.hxx:428
pos_type seekpos(pos_type pos, openmode) override
Definition: largeobject.hxx:453
largeobject_streambuf(dbtransaction &t, largeobject o, openmode mode=default_mode, size_type buf_size=512)
Definition: largeobject.hxx:408
pos_type seekoff(off_type offset, seekdir dir, openmode) override
Definition: largeobject.hxx:448
int sync() override
Definition: largeobject.hxx:441
Identity of a large object.
Definition: largeobject.hxx:44
bool operator==(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:101
static PQXX_PURE internal::pq::PGconn * raw_connection(dbtransaction const &T)
Definition: largeobject.cxx:160
bool operator>=(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:119
bool operator<=(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:113
void to_file(dbtransaction &t, std::string_view file) const
Export large object's contents to a local file.
Definition: largeobject.cxx:128
large_object_size_type size_type
Definition: largeobject.hxx:46
bool operator<(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:125
largeobject() noexcept=default
bool operator!=(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:107
bool operator>(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:131
oid id() const noexcept
Object identifier.
Definition: largeobject.hxx:89
Accessor for large object's contents.
Definition: largeobject.hxx:167
size_type pos_type
Definition: largeobject.hxx:171
largeobjectaccess & operator=(largeobjectaccess const &)=delete
std::ios::openmode openmode
Open mode: in, out (can be combined using "bitwise or").
Definition: largeobject.hxx:181
std::ios::seekdir seekdir
Seek direction: beg, cur, end.
Definition: largeobject.hxx:188
largeobjectaccess()=delete
largeobjectaccess(largeobjectaccess &&)=delete
largeobjectaccess(largeobjectaccess const &)=delete
void to_file(std::string_view file) const
Export large object's contents to a local file.
Definition: largeobject.hxx:247
~largeobjectaccess() noexcept
Definition: largeobject.hxx:232
void write(std::string_view buf)
Write string to large object.
Definition: largeobject.hxx:270
size_type off_type
Definition: largeobject.hxx:170
largeobjectaccess & operator=(largeobjectaccess &&)=delete
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:55
Internal error in libpqxx library.
Definition: except.hxx:558
Abstract transaction base class: bracket transactions on the database.
Definition: dbtransaction.hxx:54
void PGconn
Placeholder for libpq's connection type.
Definition: types.hxx:429
concept char_type
Concept: one of the "char" types.
Definition: types.hxx:406
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
concept binary
Concept: Binary string, akin to std::string for binary data.
Definition: types.hxx:173
unsigned int oid
PostgreSQL database row identifier.
Definition: types.hxx:73
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:283
int64_t large_object_size_type
Number of bytes in a large object.
Definition: types.hxx:92
format
Format code: is data text or binary?
Definition: types.hxx:121