13 #ifndef PQXX_H_LARGEOBJECT
14 #define PQXX_H_LARGEOBJECT
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/compiler-internal-pre.hxx"
21 #include "pqxx/dbtransaction.hxx"
59 largeobject(dbtransaction &T,
const std::string &File);
66 largeobject(
const largeobjectaccess &O) noexcept;
73 oid
id() const noexcept {
return m_id; }
86 {
return m_id == other.m_id; }
90 {
return m_id != other.m_id; }
94 {
return m_id <= other.m_id; }
98 {
return m_id >= other.m_id; }
102 {
return m_id < other.m_id; }
106 {
return m_id > other.m_id; }
114 void to_file(
dbtransaction &T,
const std::string &File)
const;
124 PQXX_PURE
static internal::pq::PGconn *raw_connection(
127 PQXX_PRIVATE std::string reason(
const connection_base &,
int err)
const;
165 openmode mode=std::ios::in|std::ios::out);
177 openmode mode=std::ios::in|std::ios::out);
188 openmode mode=std::ios::in|std::ios::out);
198 const std::string &File,
199 openmode mode=std::ios::in|std::ios::out);
227 void write(
const char Buf[], size_type Len);
234 { write(Buf.c_str(),
static_cast<size_type>(Buf.size())); }
243 size_type read(
char Buf[], size_type Len);
249 size_type seek(size_type dest, seekdir dir);
255 size_type tell()
const;
276 pos_type cseek(off_type dest, seekdir dir) noexcept;
285 off_type cwrite(
const char Buf[], size_type Len) noexcept;
294 off_type cread(
char Buf[], size_type Len) noexcept;
301 pos_type ctell() const noexcept;
308 void process_notice(const std::
string &) noexcept;
322 PQXX_PRIVATE std::
string reason(
int err) const;
323 internal::pq::PGconn *raw_connection()
const
326 PQXX_PRIVATE
void open(openmode mode);
327 void close() noexcept;
329 dbtransaction &m_trans;
332 largeobjectaccess() =delete;
333 largeobjectaccess(const largeobjectaccess &) =delete;
334 largeobjectaccess operator=(const largeobjectaccess &) =delete;
347 template<typename CHAR=
char, typename TRAITS=std::char_traits<CHAR>>
349 public std::basic_streambuf<CHAR, TRAITS>
351 using size_type = long;
364 openmode mode=std::ios::in|std::ios::out,
365 size_type BufSize=512) :
370 { initialize(mode); }
375 openmode mode=std::ios::in|std::ios::out,
376 size_type BufSize=512) :
381 { initialize(mode); }
384 {
delete [] m_p;
delete [] m_g; }
394 this->setg(this->eback(), this->eback(), this->egptr());
395 return overflow(EoF());
412 return AdjustEOF(newpos);
417 char *
const pp = this->pptr();
418 if (pp ==
nullptr)
return EoF();
419 char *
const pb = this->pbase();
422 if (pp > pb) res =
int_type(AdjustEOF(m_obj.cwrite(pb, pp-pb)));
423 this->setp(m_p, m_p + m_bufsize);
428 *this->pptr() = char(ch);
436 if (this->gptr() ==
nullptr)
return EoF();
437 char *
const eb = this->eback();
439 AdjustEOF(m_obj.cread(this->eback(), m_bufsize))));
440 this->setg(eb, eb, eb + ((res==EoF()) ? 0 : res));
441 return ((res == 0) or (res == EoF())) ? EoF() : *eb;
446 static int_type EoF() {
return traits_type::eof(); }
449 template<
typename INTYPE>
450 static std::streampos AdjustEOF(INTYPE pos)
451 {
return (pos==-1) ? std::streampos(EoF()) : std::streampos(pos); }
453 void initialize(openmode mode)
455 if (mode & std::ios::in)
457 m_g =
new char_type[unsigned(m_bufsize)];
458 this->setg(m_g, m_g, m_g);
460 if (mode & std::ios::out)
462 m_p =
new char_type[unsigned(m_bufsize)];
463 this->setp(m_p, m_p + m_bufsize);
467 const size_type m_bufsize;
468 largeobjectaccess m_obj;
471 char_type *m_g, *m_p;
484 template<
typename CHAR=
char,
typename TRAITS=std::
char_traits<CHAR>>
486 public std::basic_istream<CHAR, TRAITS>
488 using super = std::basic_istream<CHAR, TRAITS>;
508 m_buf{T, O, std::ios::in, BufSize}
509 { super::init(&m_buf); }
522 m_buf{T, O, std::ios::in, BufSize}
523 { super::init(&m_buf); }
526 largeobject_streambuf<CHAR,TRAITS> m_buf;
541 template<
typename CHAR=
char,
typename TRAITS=std::
char_traits<CHAR>>
543 public std::basic_ostream<CHAR, TRAITS>
545 using super = std::basic_ostream<CHAR, TRAITS>;
564 m_buf{T, O, std::ios::out, BufSize}
565 { super::init(&m_buf); }
578 m_buf{T, O, std::ios::out, BufSize}
579 { super::init(&m_buf); }
585 m_buf.pubsync(); m_buf.pubsync();
587 catch (
const std::exception &e)
589 m_buf.process_notice(e.what());
609 template<
typename CHAR=
char,
typename TRAITS=std::
char_traits<CHAR>>
611 public std::basic_iostream<CHAR, TRAITS>
613 using super = std::basic_iostream<CHAR, TRAITS>;
633 m_buf{T, O, std::ios::in | std::ios::out, BufSize}
634 { super::init(&m_buf); }
647 m_buf{T, O, std::ios::in | std::ios::out, BufSize}
648 { super::init(&m_buf); }
654 m_buf.pubsync(); m_buf.pubsync();
656 catch (
const std::exception &e)
658 m_buf.process_notice(e.what());
670 #include "pqxx/compiler-internal-post.hxx"
off_type cwrite(const char Buf[], size_type Len) noexcept
Write to large object's data stream.
Definition: largeobject.cxx:213
~largeobjectaccess() noexcept
Definition: largeobject.hxx:201
typename traits_type::int_type int_type
Definition: largeobject.hxx:549
virtual pos_type seekoff(off_type offset, seekdir dir, openmode) override
Definition: largeobject.hxx:398
void to_file(dbtransaction &T, const std::string &File) const
Export large object's contents to a local file.
Definition: largeobject.cxx:106
std::string reason(const connection_base &, int err) const
Definition: largeobject.cxx:141
TRAITS traits_type
Definition: largeobject.hxx:548
Accessor for large object's contents.
Definition: largeobject.hxx:137
virtual ~largeobject_streambuf() noexcept
Definition: largeobject.hxx:383
std::char_traits< char > traits_type
Definition: largeobject.hxx:354
CHAR char_type
Definition: largeobject.hxx:547
basic_lostream(dbtransaction &T, oid O, largeobject::size_type BufSize=512)
Create a basic_lostream.
Definition: largeobject.hxx:642
Private namespace for libpqxx's internal use; do not access.
Definition: connection_base.hxx:43
long large_object_size_type
Number of bytes in a large object. (Unusual: it's signed.)
Definition: types.hxx:33
void to_file(const std::string &File) const
Export large object's contents to a local file.
Definition: largeobject.hxx:213
large_object_size_type size_type
Definition: largeobject.hxx:37
char char_type
Definition: largeobject.hxx:353
virtual int_type overflow(int_type ch=EoF()) override
Definition: largeobject.hxx:415
largeobject() noexcept=default
Refer to a nonexistent large object (similar to what a null pointer does)
largeobject_streambuf(dbtransaction &T, largeobject O, openmode mode=std::ios::in|std::ios::out, size_type BufSize=512)
Definition: largeobject.hxx:361
oid id() const noexcept
Object identifier.
Definition: largeobject.hxx:73
Run-time failure encountered by libpqxx, similar to std::runtime_error.
Definition: except.hxx:89
typename traits_type::off_type off_type
Definition: largeobject.hxx:495
std::ios::seekdir seekdir
Seek direction: beg, cur, end.
Definition: largeobject.hxx:156
bool operator>=(const largeobject &other) const
Compare object identities.
Definition: largeobject.hxx:97
basic_ilostream(dbtransaction &T, oid O, largeobject::size_type BufSize=512)
Create a basic_ilostream.
Definition: largeobject.hxx:517
off_type cread(char Buf[], size_type Len) noexcept
Read from large object's data stream.
Definition: largeobject.cxx:223
large_object_size_type size_type
Definition: largeobject.hxx:37
virtual int_type underflow() override
Definition: largeobject.hxx:434
TRAITS traits_type
Definition: largeobject.hxx:617
Input stream that gets its data from a large object.
Definition: largeobject.hxx:485
~basic_lostream()
Definition: largeobject.hxx:650
void remove(dbtransaction &T) const
Delete large object from database.
Definition: largeobject.cxx:121
static PQXX_PURE internal::pq::PGconn * raw_connection(const dbtransaction &T)
Definition: largeobject.cxx:134
void process_notice(const std::string &) noexcept
Issue message to transaction's notice processor.
Definition: largeobject.cxx:310
void process_notice(const std::string &s)
For use by large object stream classes.
Definition: largeobject.hxx:388
~basic_olostream()
Definition: largeobject.hxx:581
Stream that reads and writes a large object.
Definition: largeobject.hxx:610
size_type seek(size_type dest, seekdir dir)
Seek in large object's data stream.
Definition: largeobject.cxx:191
virtual pos_type seekpos(pos_type pos, openmode) override
Definition: largeobject.hxx:407
bool operator==(const largeobject &other) const
Compare object identities.
Definition: largeobject.hxx:85
Streambuf to use large objects in standard I/O streams.
Definition: largeobject.hxx:348
typename traits_type::off_type off_type
Definition: largeobject.hxx:620
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:494
basic_lostream(dbtransaction &T, largeobject O, largeobject::size_type BufSize=512)
Create a basic_lostream.
Definition: largeobject.hxx:628
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:550
typename traits_type::int_type int_type
Definition: largeobject.hxx:618
size_type pos_type
Definition: largeobject.hxx:142
bool operator!=(const largeobject &other) const
Compare object identities.
Definition: largeobject.hxx:89
void write(const std::string &Buf)
Write string to large object.
Definition: largeobject.hxx:233
CHAR char_type
Definition: largeobject.hxx:491
basic_ilostream(dbtransaction &T, largeobject O, largeobject::size_type BufSize=512)
Create a basic_ilostream.
Definition: largeobject.hxx:503
typename traits_type::int_type int_type
Definition: largeobject.hxx:355
largeobjectaccess::seekdir seekdir
Definition: largeobject.hxx:359
constexpr oid oid_none
The "null" oid.
Definition: util.hxx:77
size_type read(char Buf[], size_type Len)
Read data from large object.
Definition: largeobject.cxx:260
connection_base & conn() const
Connection this transaction is running in.
Definition: transaction_base.hxx:492
bool operator<=(const largeobject &other) const
Compare object identities.
Definition: largeobject.hxx:93
pos_type ctell() const noexcept
Report current position in large object's data stream.
Definition: largeobject.cxx:230
typename traits_type::int_type int_type
Definition: largeobject.hxx:493
TRAITS traits_type
Definition: largeobject.hxx:492
bool operator>(const largeobject &other) const
Compare object identities.
Definition: largeobject.hxx:105
typename traits_type::off_type off_type
Definition: largeobject.hxx:357
typename traits_type::off_type off_type
Definition: largeobject.hxx:551
pos_type cseek(off_type dest, seekdir dir) noexcept
Seek in large object's data stream.
Definition: largeobject.cxx:206
basic_olostream(dbtransaction &T, largeobject O, largeobject::size_type BufSize=512)
Create a basic_olostream.
Definition: largeobject.hxx:559
largeobjectaccess::openmode openmode
Definition: largeobject.hxx:358
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:619
typename traits_type::pos_type pos_type
Definition: largeobject.hxx:356
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:451
Identity of a large object.
Definition: largeobject.hxx:34
connection_base abstract base class; represents a connection to a database.
Definition: connection_base.hxx:139
Output stream that writes data back to a large object.
Definition: largeobject.hxx:542
Abstract base class responsible for bracketing a backend transaction.
Definition: dbtransaction.hxx:62
void write(const char Buf[], size_type Len)
Write data to large object.
Definition: largeobject.cxx:236
largeobject_streambuf(dbtransaction &T, oid O, openmode mode=std::ios::in|std::ios::out, size_type BufSize=512)
Definition: largeobject.hxx:372
std::ios::openmode openmode
Open mode: in, out (can be combined with the "or" operator)
Definition: largeobject.hxx:149
basic_olostream(dbtransaction &T, oid O, largeobject::size_type BufSize=512)
Create a basic_olostream.
Definition: largeobject.hxx:573
largeobjectaccess(dbtransaction &T, openmode mode=std::ios::in|std::ios::out)
Create new large object and open it.
Definition: largeobject.cxx:149
size_type tell() const
Report current position in large object's data stream.
Definition: largeobject.cxx:295
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
CHAR char_type
Definition: largeobject.hxx:616
virtual int sync() override
Definition: largeobject.hxx:391
long off_type
Definition: largeobject.hxx:141
bool operator<(const largeobject &other) const
Compare object identities.
Definition: largeobject.hxx:101