libpqxx 4.0
tablewriter.hxx
00001 /*-------------------------------------------------------------------------
00002  *
00003  *   FILE
00004  *      pqxx/tablewriter.hxx
00005  *
00006  *   DESCRIPTION
00007  *      definition of the pqxx::tablewriter class.
00008  *   pqxx::tablewriter enables optimized batch updates to a database table
00009  *   DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/tablewriter.hxx instead.
00010  *
00011  * Copyright (c) 2001-2011, Jeroen T. Vermeulen <jtv@xs4all.nl>
00012  *
00013  * See COPYING for copyright license.  If you did not receive a file called
00014  * COPYING with this source code, please notify the distributor of this mistake,
00015  * or contact the author.
00016  *
00017  *-------------------------------------------------------------------------
00018  */
00019 #ifndef PQXX_H_TABLEWRITER
00020 #define PQXX_H_TABLEWRITER
00021 #include "pqxx/compiler-public.hxx"
00022 #include "pqxx/compiler-internal-pre.hxx"
00023 #include "pqxx/tablestream"
00024 namespace pqxx
00025 {
00026 class tablereader;
00028 
00031 class PQXX_LIBEXPORT tablewriter : public tablestream
00032 {
00033 public:
00034   typedef unsigned size_type;
00035   tablewriter(transaction_base &,
00036       const PGSTD::string &WName,
00037       const PGSTD::string &Null=PGSTD::string());
00038   template<typename ITER> tablewriter(transaction_base &,
00039       const PGSTD::string &WName,
00040       ITER begincolumns,
00041       ITER endcolumns);
00042   template<typename ITER> tablewriter(transaction_base &T,
00043       const PGSTD::string &WName,
00044       ITER begincolumns,
00045       ITER endcolumns,
00046       const PGSTD::string &Null);
00047   ~tablewriter() throw ();
00048   template<typename IT> void insert(IT Begin, IT End);
00049   template<typename TUPLE> void insert(const TUPLE &);
00050   template<typename IT> void push_back(IT Begin, IT End);
00051   template<typename TUPLE> void push_back(const TUPLE &);
00052   void reserve(size_type) {}
00053   template<typename TUPLE> tablewriter &operator<<(const TUPLE &);
00054   tablewriter &operator<<(tablereader &);
00055   template<typename IT> PGSTD::string generate(IT Begin, IT End) const;
00056   template<typename TUPLE> PGSTD::string generate(const TUPLE &) const;
00057   virtual void complete();
00058   void write_raw_line(const PGSTD::string &);
00059 private:
00060   void setup(transaction_base &,
00061       const PGSTD::string &WName,
00062       const PGSTD::string &Columns = PGSTD::string());
00063   void PQXX_PRIVATE writer_close();
00064 };
00065 } // namespace pqxx
00066 namespace PGSTD
00067 {
00068 template<>
00069   class back_insert_iterator<pqxx::tablewriter> :
00070         public iterator<output_iterator_tag, void,void,void,void>
00071 {
00072 public:
00073   explicit back_insert_iterator(pqxx::tablewriter &W) throw () :
00074     m_Writer(&W) {}
00075   back_insert_iterator &
00076     operator=(const back_insert_iterator &rhs) throw ()
00077   {
00078     m_Writer = rhs.m_Writer;
00079     return *this;
00080   }
00081   template<typename TUPLE>
00082   back_insert_iterator &operator=(const TUPLE &T)
00083   {
00084     m_Writer->insert(T);
00085     return *this;
00086   }
00087   back_insert_iterator &operator++() { return *this; }
00088   back_insert_iterator &operator++(int) { return *this; }
00089   back_insert_iterator &operator*() { return *this; }
00090 private:
00091   pqxx::tablewriter *m_Writer;
00092 };
00093 } // namespace PGSTD
00094 namespace pqxx
00095 {
00096 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
00097     const PGSTD::string &WName,
00098     ITER begincolumns,
00099     ITER endcolumns) :
00100   namedclass("tablewriter", WName),
00101   tablestream(T, PGSTD::string())
00102 {
00103   setup(T, WName, columnlist(begincolumns, endcolumns));
00104 }
00105 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
00106     const PGSTD::string &WName,
00107     ITER begincolumns,
00108     ITER endcolumns,
00109     const PGSTD::string &Null) :
00110   namedclass("tablewriter", WName),
00111   tablestream(T, Null)
00112 {
00113   setup(T, WName, columnlist(begincolumns, endcolumns));
00114 }
00115 namespace internal
00116 {
00117 PGSTD::string PQXX_LIBEXPORT Escape(
00118         const PGSTD::string &s,
00119         const PGSTD::string &null);
00120 inline PGSTD::string EscapeAny(
00121         const PGSTD::string &s,
00122         const PGSTD::string &null)
00123 { return Escape(s, null); }
00124 inline PGSTD::string EscapeAny(
00125         const char s[],
00126         const PGSTD::string &null)
00127 { return s ? Escape(PGSTD::string(s), null) : "\\N"; }
00128 template<typename T> inline PGSTD::string EscapeAny(
00129         const T &t,
00130         const PGSTD::string &null)
00131 { return Escape(to_string(t), null); }
00132 template<typename IT> class Escaper
00133 {
00134   const PGSTD::string &m_null;
00135 public:
00136   explicit Escaper(const PGSTD::string &null) : m_null(null) {}
00137   PGSTD::string operator()(IT i) const { return EscapeAny(*i, m_null); }
00138 };
00139 }
00140 template<typename IT>
00141 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
00142 {
00143   return separated_list("\t", Begin, End, internal::Escaper<IT>(NullStr()));
00144 }
00145 template<typename TUPLE>
00146 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
00147 {
00148   return generate(T.begin(), T.end());
00149 }
00150 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
00151 {
00152   write_raw_line(generate(Begin, End));
00153 }
00154 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
00155 {
00156   insert(T.begin(), T.end());
00157 }
00158 template<typename IT>
00159 inline void tablewriter::push_back(IT Begin, IT End)
00160 {
00161   insert(Begin, End);
00162 }
00163 template<typename TUPLE>
00164 inline void tablewriter::push_back(const TUPLE &T)
00165 {
00166   insert(T.begin(), T.end());
00167 }
00168 template<typename TUPLE>
00169 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
00170 {
00171   insert(T);
00172   return *this;
00173 }
00174 } // namespace pqxx
00175 #include "pqxx/compiler-internal-post.hxx"
00176 #endif