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"
60 largeobject(dbtransaction &t, std::string_view file);
67 largeobject(largeobjectaccess
const &o) noexcept;
74 [[nodiscard]] oid id() const noexcept {
return m_id; }
88 return m_id == other.m_id;
94 return m_id != other.m_id;
100 return m_id <= other.m_id;
106 return m_id >= other.m_id;
112 return m_id < other.m_id;
118 return m_id > other.m_id;
137 PQXX_PURE
static internal::pq::PGconn *
140 PQXX_PRIVATE std::string reason(
connection const &,
int err)
const;
166 static constexpr
auto default_mode{
167 std::ios::in | std::ios::out | std::ios::binary};
238 void write(
char const buf[], std::size_t len);
244 void write(std::string_view buf) {
write(buf.data(), buf.size()); }
253 size_type read(
char buf[], std::size_t len);
259 size_type seek(size_type dest, seekdir dir);
265 [[nodiscard]] size_type tell()
const;
289 pos_type cseek(off_type dest, seekdir dir) noexcept;
298 off_type cwrite(
char const buf[], std::size_t len) noexcept;
307 off_type cread(
char buf[], std::size_t len) noexcept;
314 [[nodiscard]] pos_type ctell() const noexcept;
321 void process_notice(std::
string const &) noexcept;
339 PQXX_PRIVATE std::
string reason(
int err) const;
340 internal::pq::PGconn *raw_connection()
const
345 PQXX_PRIVATE
void open(openmode mode);
346 void close() noexcept;
360 template<typename CHAR =
char, typename TRAITS = std::char_traits<CHAR>>
375 static constexpr
auto default_mode{
376 std::ios::in | std::ios::out | std::ios::binary};
380 size_type buf_size = 512) :
381 m_bufsize{buf_size}, m_obj{t, o, mode}, m_g{
nullptr}, m_p{
nullptr}
388 size_type buf_size = 512) :
389 m_bufsize{buf_size}, m_obj{t, o, mode}, m_g{
nullptr}, m_p{
nullptr}
407 this->setg(this->eback(), this->eback(), this->egptr());
408 return overflow(eof());
420 return adjust_eof(newpos);
425 auto *
const pp{this->pptr()};
428 auto *
const pb{this->pbase()};
434 adjust_eof(m_obj.cwrite(pb,
static_cast<std::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() =
static_cast<char_type>(ch);
455 if (this->gptr() ==
nullptr)
457 auto *
const eb{this->eback()};
458 auto const res{adjust_eof(
459 m_obj.cread(this->eback(),
static_cast<std::size_t
>(m_bufsize)))};
461 eb, eb, eb + (res == eof() ? 0 :
static_cast<std::size_t
>(res)));
462 return (res == eof() or res == 0) ? eof() : traits_type::to_int_type(*eb);
467 static int_type eof() {
return traits_type::eof(); }
470 template<
typename INTYPE>
static std::streampos adjust_eof(INTYPE pos)
472 bool const at_eof{pos == -1};
473 if constexpr (std::is_arithmetic_v<std::streampos>)
475 return check_cast<std::streampos>(
476 (at_eof ? eof() : pos),
"large object seek");
480 return std::streampos(at_eof ? eof() : pos);
484 void initialize(openmode mode)
486 if ((mode & std::ios::in) != 0)
488 m_g =
new char_type[unsigned(m_bufsize)];
489 this->setg(m_g, m_g, m_g);
491 if ((mode & std::ios::out) != 0)
493 m_p =
new char_type[unsigned(m_bufsize)];
494 this->setp(m_p, m_p + m_bufsize);
498 size_type
const m_bufsize;
499 largeobjectaccess m_obj;
502 char_type *m_g, *m_p;
515 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
518 using super = std::basic_istream<CHAR, TRAITS>;
536 m_buf{t, o, std::ios::in | std::ios::binary, buf_size}
550 m_buf{t, o, std::ios::in | std::ios::binary, buf_size}
556 largeobject_streambuf<CHAR, TRAITS> m_buf;
571 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
574 using super = std::basic_ostream<CHAR, TRAITS>;
592 m_buf{t, o, std::ios::out | std::ios::binary, buf_size}
606 m_buf{t, o, std::ios::out | std::ios::binary, buf_size}
618 catch (std::exception
const &e)
620 m_buf.process_notice(e.what());
640 template<
typename CHAR =
char,
typename TRAITS = std::
char_traits<CHAR>>
643 using super = std::basic_iostream<CHAR, TRAITS>;
662 t, o, std::ios::in | std::ios::out | std::ios::binary, buf_size}
677 t, o, std::ios::in | std::ios::out | std::ios::binary, buf_size}
689 catch (std::exception
const &e)
691 m_buf.process_notice(e.what());
702 #include "pqxx/internal/compiler-internal-post.hxx"
largeobject() noexcept=default
basic_ilostream(dbtransaction &t, largeobject o, largeobject::size_type buf_size=512)
Create a basic_ilostream.
Definition: largeobject.hxx:533
size_type off_type
Definition: largeobject.hxx:152
typename traits_type::int_type int_type
Definition: largeobject.hxx:523
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
Accessor for large object's contents.
Definition: largeobject.hxx:149
size_type read(char buf[], std::size_t len)
Read data from large object.
Definition: largeobject.cxx:254
CHAR char_type
Definition: largeobject.hxx:521
~basic_olostream()
Definition: largeobject.hxx:611
virtual int_type underflow() override
Definition: largeobject.hxx:453
TRAITS traits_type
Definition: largeobject.hxx:647
CHAR char_type
Definition: largeobject.hxx:646
std::string to_string(field const &value)
Convert a field to a string.
Definition: result.cxx:498
~basic_lostream()
Definition: largeobject.hxx:682
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:579
Run-time failure encountered by libpqxx, similar to std::runtime_error.
Definition: except.hxx:44
TRAITS traits_type
Definition: largeobject.hxx:578
void remove(dbtransaction &t) const
Delete large object from database.
Definition: largeobject.cxx:116
largeobjectaccess()=delete
std::ios::seekdir seekdir
Seek direction: beg, cur, end.
Definition: largeobject.hxx:170
Input stream that gets its data from a large object.
Definition: largeobject.hxx:517
basic_lostream(dbtransaction &t, oid o, largeobject::size_type buf_size=512)
Create a basic_lostream.
Definition: largeobject.hxx:673
typename traits_type::off_type off_type
Definition: largeobject.hxx:581
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:487
virtual int_type overflow(int_type ch) override
Definition: largeobject.hxx:423
largeobjectaccess::openmode openmode
Definition: largeobject.hxx:371
basic_lostream(dbtransaction &t, largeobject o, largeobject::size_type buf_size=512)
Create a basic_lostream.
Definition: largeobject.hxx:658
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:74
Abstract transaction base class: bracket transactions on the database.
Definition: dbtransaction.hxx:53
Stream that reads and writes a large object.
Definition: largeobject.hxx:642
Streambuf to use large objects in standard I/O streams.
Definition: largeobject.hxx:362
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:198
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:649
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:369
size_type seek(size_type dest, seekdir dir)
Seek in large object's data stream.
Definition: largeobject.cxx:180
off_type cwrite(char const buf[], std::size_t len) noexcept
Write to large object's data stream.
Definition: largeobject.cxx:205
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:524
typename traits_type::off_type off_type
Definition: largeobject.hxx:525
char char_type
Definition: largeobject.hxx:366
void to_file(std::string_view file) const
Export large object's contents to a local file.
Definition: largeobject.hxx:221
largeobject_streambuf(dbtransaction &t, largeobject o, openmode mode=default_mode, size_type buf_size=512)
Definition: largeobject.hxx:378
largeobjectaccess::seekdir seekdir
Definition: largeobject.hxx:372
typename traits_type::off_type off_type
Definition: largeobject.hxx:370
size_type tell() const
Report current position in large object's data stream.
Definition: largeobject.cxx:295
CHAR char_type
Definition: largeobject.hxx:577
typename traits_type::off_type off_type
Definition: largeobject.hxx:650
basic_ilostream(dbtransaction &t, oid o, largeobject::size_type buf_size=512)
Create a basic_ilostream.
Definition: largeobject.hxx:547
typename traits_type::int_type int_type
Definition: largeobject.hxx:648
Connection to a database.
Definition: connection.hxx:137
TRAITS traits_type
Definition: largeobject.hxx:522
~largeobjectaccess() noexcept
Definition: largeobject.hxx:209
Identity of a large object.
Definition: largeobject.hxx:35
void write(std::string_view buf)
Write string to large object.
Definition: largeobject.hxx:244
static PQXX_PURE internal::pq::PGconn * raw_connection(dbtransaction const &T)
Definition: largeobject.cxx:133
off_type cread(char buf[], std::size_t len) noexcept
Read from large object's data stream.
Definition: largeobject.cxx:212
basic_olostream(dbtransaction &t, largeobject o, largeobject::size_type buf_size=512)
Create a basic_olostream.
Definition: largeobject.hxx:589
typename traits_type::int_type int_type
Definition: largeobject.hxx:368
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:573
void process_notice(std::string const &) noexcept
Issue message to transaction's notice processor.
Definition: largeobject.cxx:312
pos_type ctell() const noexcept
Report current position in large object's data stream.
Definition: largeobject.cxx:219
virtual ~largeobject_streambuf() noexcept
Definition: largeobject.hxx:394
virtual int sync() override
Definition: largeobject.hxx:404
std::char_traits< char > traits_type
Definition: largeobject.hxx:367
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:187
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:580
int64_t large_object_size_type
Number of bytes in a large object.
Definition: types.hxx:33
void write(char const buf[], std::size_t len)
Write data to large object.
Definition: largeobject.cxx:225
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:166
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:140
size_type pos_type
Definition: largeobject.hxx:153
largeobject_streambuf(dbtransaction &t, oid o, openmode mode=default_mode, size_type buf_size=512)
Definition: largeobject.hxx:386
basic_olostream(dbtransaction &t, oid o, largeobject::size_type buf_size=512)
Create a basic_olostream.
Definition: largeobject.hxx:603
std::ios::openmode openmode
Open mode: in, out (can be combined using "bitwise or").
Definition: largeobject.hxx:163
large_object_size_type size_type
Definition: largeobject.hxx:37