13 #ifndef PQXX_H_LARGEOBJECT
14 #define PQXX_H_LARGEOBJECT
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
21 #include "pqxx/dbtransaction.hxx"
59 largeobject(dbtransaction &t, std::string_view file);
66 largeobject(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;
194 openmode mode = std::ios::in | std::ios::out);
204 openmode mode = std::ios::in | std::ios::out);
235 void write(
char const buf[],
size_t len);
241 void write(std::string_view buf) {
write(buf.data(), buf.size()); }
250 size_type read(
char buf[],
size_t len);
256 size_type seek(size_type dest, seekdir dir);
262 [[nodiscard]] size_type tell()
const;
286 pos_type cseek(off_type dest, seekdir dir) noexcept;
295 off_type cwrite(
char const buf[],
size_t len) noexcept;
304 off_type cread(
char buf[],
size_t len) noexcept;
311 [[nodiscard]] pos_type ctell() const noexcept;
318 void process_notice(std::
string const &) noexcept;
336 PQXX_PRIVATE std::
string reason(
int err) const;
337 internal::pq::PGconn *raw_connection()
const
342 PQXX_PRIVATE
void open(openmode mode);
343 void close() noexcept;
345 dbtransaction &m_trans;
357 template<typename CHAR =
char, typename TRAITS = std::char_traits<CHAR>>
373 openmode mode = std::ios::in | std::ios::out, size_type buf_size = 512) :
384 size_type buf_size = 512) :
407 this->setg(this->eback(), this->eback(), this->egptr());
408 return overflow(eof());
420 return adjust_eof(newpos);
425 char *
const pp{this->pptr()};
428 char *
const pb{this->pbase()};
434 adjust_eof(m_obj.cwrite(pb,
static_cast<size_t>(pp - pb)))};
435 if constexpr (std::is_arithmetic_v<decltype(out)>)
436 res = check_cast<int_type>(out);
440 this->setp(m_p, m_p + m_bufsize);
445 *this->pptr() = char(ch);
455 if (this->gptr() ==
nullptr)
457 char *
const eb{this->eback()};
459 adjust_eof(m_obj.cread(this->eback(),
static_cast<size_t>(m_bufsize))))};
460 this->setg(eb, eb, eb + ((res == eof()) ? 0 : res));
461 return ((res == 0) or (res == eof())) ? eof() : *eb;
466 static int_type eof() {
return traits_type::eof(); }
469 template<
typename INTYPE>
static std::streampos adjust_eof(INTYPE pos)
471 bool const at_eof{pos == -1};
472 if constexpr (std::is_arithmetic_v<std::streampos>)
474 return check_cast<std::streampos>(
475 (at_eof ? eof() : pos),
"large object seek");
479 return std::streampos(at_eof ? eof() : pos);
483 void initialize(openmode mode)
485 if ((mode & std::ios::in) != 0)
487 m_g =
new char_type[unsigned(m_bufsize)];
488 this->setg(m_g, m_g, m_g);
490 if ((mode & std::ios::out) != 0)
492 m_p =
new char_type[unsigned(m_bufsize)];
493 this->setp(m_p, m_p + m_bufsize);
497 size_type
const m_bufsize;
498 largeobjectaccess m_obj;
501 char_type *m_g, *m_p;
514 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
517 using super = std::basic_istream<CHAR, TRAITS>;
535 m_buf{t, o, std::ios::in, buf_size}
549 m_buf{t, o, std::ios::in, buf_size}
555 largeobject_streambuf<CHAR, TRAITS> m_buf;
570 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
573 using super = std::basic_ostream<CHAR, TRAITS>;
591 m_buf{t, o, std::ios::out, buf_size}
605 m_buf{t, o, std::ios::out, buf_size}
617 catch (std::exception
const &e)
619 m_buf.process_notice(e.what());
639 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
642 using super = std::basic_iostream<CHAR, TRAITS>;
660 m_buf{t, o, std::ios::in | std::ios::out, buf_size}
674 m_buf{t, o, std::ios::in | std::ios::out, buf_size}
686 catch (std::exception
const &e)
688 m_buf.process_notice(e.what());
699 #include "pqxx/internal/compiler-internal-post.hxx"
largeobject() noexcept=default
Refer to a nonexistent large object (similar to what a null pointer does)
size_type read(char buf[], size_t len)
Read data from large object.
Definition: largeobject.cxx:247
basic_ilostream(dbtransaction &t, largeobject o, largeobject::size_type buf_size=512)
Create a basic_ilostream.
Definition: largeobject.hxx:532
size_type off_type
Definition: largeobject.hxx:151
typename traits_type::int_type int_type
Definition: largeobject.hxx:522
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
Accessor for large object's contents.
Definition: largeobject.hxx:147
CHAR char_type
Definition: largeobject.hxx:520
~basic_olostream()
Definition: largeobject.hxx:610
virtual int_type underflow() override
Definition: largeobject.hxx:453
bool operator<(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:109
TRAITS traits_type
Definition: largeobject.hxx:646
CHAR char_type
Definition: largeobject.hxx:645
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:478
~basic_lostream()
Definition: largeobject.hxx:679
bool operator>=(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:103
virtual pos_type seekoff(off_type offset, seekdir dir, openmode) override
Definition: largeobject.hxx:411
typename traits_type::int_type int_type
Definition: largeobject.hxx:578
Run-time failure encountered by libpqxx, similar to std::runtime_error.
Definition: except.hxx:42
TRAITS traits_type
Definition: largeobject.hxx:577
void remove(dbtransaction &t) const
Delete large object from database.
Definition: largeobject.cxx:113
largeobjectaccess()=delete
off_type cwrite(char const buf[], size_t len) noexcept
Write to large object's data stream.
Definition: largeobject.cxx:203
largeobject_streambuf(dbtransaction &t, oid o, openmode mode=std::ios::in|std::ios::out, size_type buf_size=512)
Definition: largeobject.hxx:382
std::ios::seekdir seekdir
Seek direction: beg, cur, end.
Definition: largeobject.hxx:166
Input stream that gets its data from a large object.
Definition: largeobject.hxx:515
basic_lostream(dbtransaction &t, oid o, largeobject::size_type buf_size=512)
Create a basic_lostream.
Definition: largeobject.hxx:671
typename traits_type::off_type off_type
Definition: largeobject.hxx:580
virtual int_type overflow(int_type ch) override
Definition: largeobject.hxx:423
largeobjectaccess::openmode openmode
Definition: largeobject.hxx:368
basic_lostream(dbtransaction &t, largeobject o, largeobject::size_type buf_size=512)
Create a basic_lostream.
Definition: largeobject.hxx:657
bool operator<=(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:97
void to_file(dbtransaction &t, std::string_view file) const
Export large object's contents to a local file.
Definition: largeobject.cxx:98
oid id() const noexcept
Object identifier.
Definition: largeobject.hxx:73
Abstract transaction base class: bracket transactions on the database.
Definition: dbtransaction.hxx:52
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:464
Stream that reads and writes a large object.
Definition: largeobject.hxx:640
Streambuf to use large objects in standard I/O streams.
Definition: largeobject.hxx:358
largeobject_streambuf(dbtransaction &t, largeobject o, openmode mode=std::ios::in|std::ios::out, size_type buf_size=512)
Definition: largeobject.hxx:371
virtual int_type overflow()
Definition: largeobject.hxx:451
large_object_size_type size_type
Definition: largeobject.hxx:37
pos_type cseek(off_type dest, seekdir dir) noexcept
Seek in large object's data stream.
Definition: largeobject.cxx:196
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:648
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:366
size_type seek(size_type dest, seekdir dir)
Seek in large object's data stream.
Definition: largeobject.cxx:180
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:523
off_type cread(char buf[], size_t len) noexcept
Read from large object's data stream.
Definition: largeobject.cxx:210
typename traits_type::off_type off_type
Definition: largeobject.hxx:524
char char_type
Definition: largeobject.hxx:363
void to_file(std::string_view file) const
Export large object's contents to a local file.
Definition: largeobject.hxx:218
largeobjectaccess::seekdir seekdir
Definition: largeobject.hxx:369
typename traits_type::off_type off_type
Definition: largeobject.hxx:367
size_type tell() const
Report current position in large object's data stream.
Definition: largeobject.cxx:283
CHAR char_type
Definition: largeobject.hxx:576
typename traits_type::off_type off_type
Definition: largeobject.hxx:649
basic_ilostream(dbtransaction &t, oid o, largeobject::size_type buf_size=512)
Create a basic_ilostream.
Definition: largeobject.hxx:546
typename traits_type::int_type int_type
Definition: largeobject.hxx:647
Connection to a database.
Definition: connection.hxx:135
TRAITS traits_type
Definition: largeobject.hxx:521
~largeobjectaccess() noexcept
Definition: largeobject.hxx:206
Identity of a large object.
Definition: largeobject.hxx:34
void write(std::string_view buf)
Write string to large object.
Definition: largeobject.hxx:241
static PQXX_PURE internal::pq::PGconn * raw_connection(dbtransaction const &T)
Definition: largeobject.cxx:127
bool operator!=(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:91
basic_olostream(dbtransaction &t, largeobject o, largeobject::size_type buf_size=512)
Create a basic_olostream.
Definition: largeobject.hxx:588
typename traits_type::int_type int_type
Definition: largeobject.hxx:365
virtual pos_type seekpos(pos_type pos, openmode) override
Definition: largeobject.hxx:416
Output stream that writes data back to a large object.
Definition: largeobject.hxx:571
void process_notice(std::string const &) noexcept
Issue message to transaction's notice processor.
Definition: largeobject.cxx:300
pos_type ctell() const noexcept
Report current position in large object's data stream.
Definition: largeobject.cxx:216
virtual ~largeobject_streambuf() noexcept
Definition: largeobject.hxx:393
virtual int sync() override
Definition: largeobject.hxx:404
std::char_traits< char > traits_type
Definition: largeobject.hxx:364
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:179
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:579
bool operator>(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:115
int64_t large_object_size_type
Number of bytes in a large object.
Definition: types.hxx:33
bool operator==(largeobject const &other) const
Compare object identities.
Definition: largeobject.hxx:85
void process_notice(std::string const &s)
For use by large object stream classes.
Definition: largeobject.hxx:401
std::string reason(connection const &, int err) const
Definition: largeobject.cxx:134
size_type pos_type
Definition: largeobject.hxx:152
basic_olostream(dbtransaction &t, oid o, largeobject::size_type buf_size=512)
Create a basic_olostream.
Definition: largeobject.hxx:602
void write(char const buf[], size_t len)
Write data to large object.
Definition: largeobject.cxx:223
std::ios::openmode openmode
Open mode: in, out (can be combined with the "or" operator)
Definition: largeobject.hxx:159
large_object_size_type size_type
Definition: largeobject.hxx:37