tablewriter.hxx
/*-------------------------------------------------------------------------
*
* FILE
* pqxx/tablewriter.hxx
*
* DESCRIPTION
* definition of the pqxx::tablewriter class.
* pqxx::tablewriter enables optimized batch updates to a database table
* DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/tablewriter.hxx instead.
*
* Copyright (c) 2001-2011, Jeroen T. Vermeulen <jtv@xs4all.nl>
*
* See COPYING for copyright license. If you did not receive a file called
* COPYING with this source code, please notify the distributor of this mistake,
* or contact the author.
*
*-------------------------------------------------------------------------
*/
#ifndef PQXX_H_TABLEWRITER
#define PQXX_H_TABLEWRITER
#include "pqxx/compiler-public.hxx"
#include "pqxx/compiler-internal-pre.hxx"
#include "pqxx/tablestream"
namespace pqxx
{
class tablereader;
class PQXX_LIBEXPORT tablewriter : public tablestream
{
public:
typedef unsigned size_type;
tablewriter(transaction_base &,
const PGSTD::string &WName,
const PGSTD::string &Null=PGSTD::string());
template<typename ITER> tablewriter(transaction_base &,
const PGSTD::string &WName,
ITER begincolumns,
ITER endcolumns);
template<typename ITER> tablewriter(transaction_base &T,
const PGSTD::string &WName,
ITER begincolumns,
ITER endcolumns,
const PGSTD::string &Null);
~tablewriter() throw ();
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 &);
void reserve(size_type) {}
template<typename TUPLE> tablewriter &operator<<(const TUPLE &);
tablewriter &operator<<(tablereader &);
template<typename IT> PGSTD::string generate(IT Begin, IT End) const;
template<typename TUPLE> PGSTD::string generate(const TUPLE &) const;
virtual void complete();
void write_raw_line(const PGSTD::string &);
private:
void setup(transaction_base &,
const PGSTD::string &WName,
const PGSTD::string &Columns = PGSTD::string());
void PQXX_PRIVATE writer_close();
};
} // namespace pqxx
namespace PGSTD
{
template<>
class back_insert_iterator<pqxx::tablewriter> :
public iterator<output_iterator_tag, void,void,void,void>
{
public:
explicit back_insert_iterator(pqxx::tablewriter &W) throw () :
m_Writer(&W) {}
back_insert_iterator &
operator=(const back_insert_iterator &rhs) throw ()
{
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 PGSTD
namespace pqxx
{
template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
const PGSTD::string &WName,
ITER begincolumns,
ITER endcolumns) :
namedclass("tablewriter", WName),
tablestream(T, PGSTD::string())
{
setup(T, WName, columnlist(begincolumns, endcolumns));
}
template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
const PGSTD::string &WName,
ITER begincolumns,
ITER endcolumns,
const PGSTD::string &Null) :
namedclass("tablewriter", WName),
tablestream(T, Null)
{
setup(T, WName, columnlist(begincolumns, endcolumns));
}
namespace internal
{
PGSTD::string PQXX_LIBEXPORT Escape(
const PGSTD::string &s,
const PGSTD::string &null);
inline PGSTD::string EscapeAny(
const PGSTD::string &s,
const PGSTD::string &null)
{ return Escape(s, null); }
inline PGSTD::string EscapeAny(
const char s[],
const PGSTD::string &null)
{ return s ? Escape(PGSTD::string(s), null) : "\\N"; }
template<typename T> inline PGSTD::string EscapeAny(
const T &t,
const PGSTD::string &null)
{ return Escape(to_string(t), null); }
template<typename IT> class Escaper
{
const PGSTD::string &m_null;
public:
explicit Escaper(const PGSTD::string &null) : m_null(null) {}
PGSTD::string operator()(IT i) const { return EscapeAny(*i, m_null); }
};
}
template<typename IT>
inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
{
return separated_list("\t", Begin, End, internal::Escaper<IT>(NullStr()));
}
template<typename TUPLE>
inline PGSTD::string tablewriter::generate(const TUPLE &T) const
{
return generate(T.begin(), T.end());
}
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(T.begin(), T.end());
}
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(T.begin(), T.end());
}
template<typename TUPLE>
inline tablewriter &tablewriter::operator<<(const TUPLE &T)
{
insert(T);
return *this;
}
} // namespace pqxx
#include "pqxx/compiler-internal-post.hxx"
#endif
pqxx::operator<<
std::basic_ostream< CHAR > & operator<<(std::basic_ostream< CHAR > &S, const pqxx::field &F)
Write a result field to any type of stream.
Definition: result.hxx:452
pqxx::internal::Escaper::operator()
std::string operator()(IT i) const
Definition: tablewriter.hxx:137
pqxx::tablewriter::tablewriter
tablewriter(transaction_base &, const std::string &WName, const std::string &Null=std::string())
Definition: tablewriter.cxx:34
pqxx::tablereader
Definition: tablereader.hxx:31
std::back_insert_iterator< pqxx::tablewriter >::operator=
back_insert_iterator & operator=(const TUPLE &T)
Definition: tablewriter.hxx:82
pqxx::internal::EscapeAny
std::string EscapeAny(const std::string &s, const std::string &null)
Definition: tablewriter.hxx:120
pqxx::tablewriter
Definition: tablewriter.hxx:31
std::back_insert_iterator< pqxx::tablewriter >::back_insert_iterator
back_insert_iterator(pqxx::tablewriter &W)
Definition: tablewriter.hxx:73
pqxx::tablewriter::size_type
unsigned size_type
Definition: tablewriter.hxx:34
pqxx::tablestream::NullStr
const std::string & NullStr() const
Definition: tablestream.hxx:37
pqxx::tablewriter::generate
std::string generate(IT Begin, IT End) const
Definition: tablewriter.hxx:141
std::back_insert_iterator< pqxx::tablewriter >::operator++
back_insert_iterator & operator++(int)
Definition: tablewriter.hxx:88
pqxx::separated_list
std::string separated_list(const std::string &sep, ITER begin, ITER end, ACCESS access)
Access iterators using ACCESS functor, returning separator-separated list.
Definition: util.hxx:417
std::back_insert_iterator< pqxx::tablewriter >::operator++
back_insert_iterator & operator++()
Definition: tablewriter.hxx:87
pqxx::internal::Escaper::Escaper
Escaper(const std::string &null)
Definition: tablewriter.hxx:136
pqxx::tablewriter::insert
void insert(IT Begin, IT End)
Definition: tablewriter.hxx:150
pqxx::to_string
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.hxx:467
pqxx::tablestream::columnlist
static std::string columnlist(ITER colbegin, ITER colend)
Definition: tablestream.hxx:50
pqxx::tablewriter::write_raw_line
void write_raw_line(const std::string &)
Definition: tablewriter.cxx:78
pqxx::tablewriter::operator<<
tablewriter & operator<<(const TUPLE &)
Definition: tablewriter.hxx:169
pqxx::tablewriter::push_back
void push_back(IT Begin, IT End)
Definition: tablewriter.hxx:159
pqxx::internal::Escaper
Definition: tablewriter.hxx:132
pqxx::tablestream
Definition: tablestream.hxx:28
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition: basic_connection.hxx:35
pqxx::transaction_base
Definition: transaction_base.hxx:133
std::back_insert_iterator< pqxx::tablewriter >::operator*
back_insert_iterator & operator*()
Definition: tablewriter.hxx:89
pqxx::internal::namedclass::namedclass
namedclass(const std::string &Classname, const std::string &Name="")
Definition: util.hxx:675
std::back_insert_iterator< pqxx::tablewriter >::operator=
back_insert_iterator & operator=(const back_insert_iterator &rhs)
Definition: tablewriter.hxx:76