tablewriter.hxx
#ifndef PQXX_H_TABLEWRITER
#define PQXX_H_TABLEWRITER
#include <iterator>
#include "pqxx/compiler-public.hxx"
#include "pqxx/compiler-internal-pre.hxx"
#include "pqxx/tablestream.hxx"
namespace pqxx
{
class PQXX_LIBEXPORT tablewriter : public tablestream
{
public:
PQXX_DEPRECATED tablewriter(
transaction_base &,
const std::string &WName,
const std::string &Null=std::string{});
template<typename ITER>
PQXX_DEPRECATED tablewriter(
transaction_base &,
const std::string &WName,
ITER begincolumns,
ITER endcolumns);
template<typename ITER>
PQXX_DEPRECATED tablewriter(
transaction_base &T,
const std::string &WName,
ITER begincolumns,
ITER endcolumns,
const std::string &Null);
~tablewriter() noexcept;
template<typename IT> void insert(IT Begin, IT End);
template<typename TUPLE> void insert(const TUPLE &);
template<typename IT> void push_back(IT Begin, IT End);
template<typename TUPLE> void push_back(const TUPLE &);
template<typename SIZE> void reserve(SIZE) {}
template<typename TUPLE> tablewriter &operator<<(const TUPLE &);
tablewriter &operator<<(tablereader &);
template<typename IT> std::string generate(IT Begin, IT End) const;
template<typename TUPLE> std::string generate(const TUPLE &) const;
virtual void complete() override;
void write_raw_line(const std::string &);
private:
void set_up(
transaction_base &,
const std::string &WName,
const std::string &Columns = std::string{});
PQXX_PRIVATE void writer_close();
};
} // namespace pqxx
namespace std
{
template<>
class back_insert_iterator<pqxx::tablewriter>
{
public:
using iterator_category = output_iterator_tag;
explicit back_insert_iterator(pqxx::tablewriter &W) noexcept :
m_writer{&W} {}
back_insert_iterator &
operator=(const back_insert_iterator &rhs) noexcept
{
m_writer = rhs.m_writer;
return *this;
}
template<typename TUPLE>
back_insert_iterator &operator=(const TUPLE &T)
{
m_writer->insert(T);
return *this;
}
back_insert_iterator &operator++() { return *this; }
back_insert_iterator &operator++(int) { return *this; }
back_insert_iterator &operator*() { return *this; }
private:
pqxx::tablewriter *m_writer;
};
} // namespace std
namespace pqxx
{
template<typename ITER> inline tablewriter::tablewriter(
transaction_base &T,
const std::string &WName,
ITER begincolumns,
ITER endcolumns) :
namedclass{"tablewriter", WName},
tablestream{T, std::string{}}
{
set_up(T, WName, columnlist(begincolumns, endcolumns));
}
template<typename ITER> inline tablewriter::tablewriter(
transaction_base &T,
const std::string &WName,
ITER begincolumns,
ITER endcolumns,
const std::string &Null) :
namedclass{"tablewriter", WName},
tablestream{T, Null}
{
set_up(T, WName, columnlist(begincolumns, endcolumns));
}
namespace internal
{
PQXX_LIBEXPORT std::string escape(
const std::string &s,
const std::string &null);
inline std::string escape_any(
const std::string &s,
const std::string &null)
{ return escape(s, null); }
inline std::string escape_any(
const char s[],
const std::string &null)
{ return s ? escape(std::string{s}, null) : "\\N"; }
template<typename T> inline std::string escape_any(
const T &t,
const std::string &null)
{ return escape(to_string(t), null); }
template<typename IT> class Escaper
{
const std::string &m_null;
public:
explicit Escaper(const std::string &null) : m_null{null} {}
std::string operator()(IT i) const { return escape_any(*i, m_null); }
};
}
template<typename IT>
inline std::string tablewriter::generate(IT Begin, IT End) const
{
return separated_list("\t", Begin, End, internal::Escaper<IT>{NullStr()});
}
template<typename TUPLE>
inline std::string tablewriter::generate(const TUPLE &T) const
{
return generate(std::begin(T), std::end(T));
}
template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
{
write_raw_line(generate(Begin, End));
}
template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
{
insert(std::begin(T), std::end(T));
}
template<typename IT>
inline void tablewriter::push_back(IT Begin, IT End)
{
insert(Begin, End);
}
template<typename TUPLE>
inline void tablewriter::push_back(const TUPLE &T)
{
insert(std::begin(T), std::end(T));
}
template<typename TUPLE>
inline tablewriter &tablewriter::operator<<(const TUPLE &T)
{
insert(T);
return *this;
}
} // namespace pqxx
#include "pqxx/compiler-internal-post.hxx"
#endif
pqxx::internal::Escaper
Definition: tablewriter.hxx:157
pqxx::internal::Escaper::operator()
std::string operator()(IT i) const
Definition: tablewriter.hxx:162
pqxx::tablestream::NullStr
const std::string & NullStr() const
Definition: tablestream.hxx:36
std::back_insert_iterator< pqxx::tablewriter >::operator=
back_insert_iterator & operator=(const back_insert_iterator &rhs) noexcept
Definition: tablewriter.hxx:85
pqxx::operator<<
std::basic_ostream< CHAR > & operator<<(std::basic_ostream< CHAR > &S, const field &F)
Write a result field to any type of stream.
Definition: field.hxx:355
pqxx::tablereader::get_raw_line
bool get_raw_line(std::string &Line)
Definition: tablereader.cxx:55
pqxx::internal::Escaper::Escaper
Escaper(const std::string &null)
Definition: tablewriter.hxx:161
pqxx::internal
Private namespace for libpqxx's internal use; do not access.
Definition: connection_base.hxx:43
pqxx::tablewriter
Definition: tablewriter.hxx:31
pqxx::tablewriter::generate
std::string generate(IT Begin, IT End) const
Definition: tablewriter.hxx:168
pqxx::tablewriter::reserve
void reserve(SIZE)
Definition: tablewriter.hxx:56
pqxx::tablewriter::~tablewriter
~tablewriter() noexcept
Definition: tablewriter.cxx:33
pqxx::tablestream
Base class for obsolete tablereader/tablewriter classes.
Definition: tablestream.hxx:26
std::back_insert_iterator< pqxx::tablewriter >::back_insert_iterator
back_insert_iterator(pqxx::tablewriter &W) noexcept
Definition: tablewriter.hxx:81
pqxx::tablewriter::complete
virtual void complete() override
Definition: tablewriter.cxx:75
std::back_insert_iterator< pqxx::tablewriter >::operator=
back_insert_iterator & operator=(const TUPLE &T)
Definition: tablewriter.hxx:92
pqxx::separated_list
std::string separated_list(const std::string &sep, ITER begin, ITER end, ACCESS access)
Represent sequence of values as a string, joined by a given separator.
Definition: util.hxx:95
std::back_insert_iterator< pqxx::tablewriter >::operator++
back_insert_iterator & operator++(int)
Definition: tablewriter.hxx:99
pqxx::tablewriter::operator<<
tablewriter & operator<<(const TUPLE &)
Definition: tablewriter.hxx:201
pqxx::internal::escape_any
std::string escape_any(const std::string &s, const std::string &null)
Definition: tablewriter.hxx:141
pqxx::internal::number_to_digit
constexpr char number_to_digit(int i) noexcept
Definition: strconv.hxx:324
pqxx::internal::escape
std::string escape(const std::string &s, const std::string &null)
Definition: tablewriter.cxx:131
std::back_insert_iterator< pqxx::tablewriter >::operator*
back_insert_iterator & operator*()
Definition: tablewriter.hxx:100
std::back_insert_iterator< pqxx::tablewriter >::operator++
back_insert_iterator & operator++()
Definition: tablewriter.hxx:98
pqxx::tablewriter::insert
void insert(IT Begin, IT End)
Definition: tablewriter.hxx:178
pqxx::internal::namedclass
Helper base class: object descriptions for error messages and such.
Definition: util.hxx:233
std::back_insert_iterator< pqxx::tablewriter >::iterator_category
output_iterator_tag iterator_category
Definition: tablewriter.hxx:79
pqxx::tablewriter::push_back
void push_back(IT Begin, IT End)
Definition: tablewriter.hxx:189
pqxx::to_string
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:451
pqxx::tablewriter::write_raw_line
void write_raw_line(const std::string &)
Definition: tablewriter.cxx:65
pqxx::tablereader
Definition: tablereader.hxx:29
pqxx::transaction_base
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:136
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
pqxx::tablewriter::tablewriter
tablewriter(transaction_base &, const std::string &WName, const std::string &Null=std::string{})
Definition: tablewriter.cxx:22