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-2008, 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 
00022 #include "pqxx/compiler-public.hxx"
00023 #include "pqxx/compiler-internal-pre.hxx"
00024 
00025 #include "pqxx/tablestream"
00026 
00027 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
00028  */
00029 
00030 namespace pqxx
00031 {
00032 class tablereader;      // See pqxx/tablereader.h
00033 
00035 
00045 class PQXX_LIBEXPORT tablewriter : public tablestream
00046 {
00047 public:
00048   typedef unsigned size_type;
00049 
00050   tablewriter(transaction_base &,
00051       const PGSTD::string &WName,
00052       const PGSTD::string &Null=PGSTD::string());                       //[t5]
00053 
00055 
00061   template<typename ITER> tablewriter(transaction_base &,
00062       const PGSTD::string &WName,
00063       ITER begincolumns,
00064       ITER endcolumns);                                                 //[t9]
00065 
00067 
00075   template<typename ITER> tablewriter(transaction_base &,
00076       const PGSTD::string &WName,
00077       ITER begincolumns,
00078       ITER endcolumns,
00079       const PGSTD::string &Null);                                       //[t9]
00080 
00081   ~tablewriter() throw ();                                              //[t5]
00082 
00083   template<typename IT> void insert(IT Begin, IT End);                  //[t5]
00084   template<typename TUPLE> void insert(const TUPLE &);                  //[t5]
00085   template<typename IT> void push_back(IT Begin, IT End);               //[t10]
00086   template<typename TUPLE> void push_back(const TUPLE &);               //[t10]
00087 
00088   void reserve(size_type) {}                                            //[t9]
00089 
00090   template<typename TUPLE> tablewriter &operator<<(const TUPLE &);      //[t5]
00091 
00093   tablewriter &operator<<(tablereader &);                               //[t6]
00094 
00096 
00098   template<typename IT> PGSTD::string generate(IT Begin, IT End) const; //[t10]
00099   template<typename TUPLE> PGSTD::string generate(const TUPLE &) const; //[t10]
00100 
00102 
00109   virtual void complete();                                              //[t5]
00110 
00112   void write_raw_line(const PGSTD::string &);
00113 
00114 private:
00115   void setup(transaction_base &,
00116       const PGSTD::string &WName,
00117       const PGSTD::string &Columns = PGSTD::string());
00118 
00119   void PQXX_PRIVATE writer_close();
00120 };
00121 
00122 } // namespace pqxx
00123 
00124 
00125 
00126 namespace PGSTD
00127 {
00129 
00132 template<>
00133   class back_insert_iterator<pqxx::tablewriter> :                       //[t9]
00134         public iterator<output_iterator_tag, void,void,void,void>
00135 {
00136 public:
00137   explicit back_insert_iterator(pqxx::tablewriter &W) throw () :        //[t83]
00138     m_Writer(&W) {}
00139 
00140   back_insert_iterator &
00141     operator=(const back_insert_iterator &rhs) throw ()                 //[t83]
00142   {
00143     m_Writer = rhs.m_Writer;
00144     return *this;
00145   }
00146 
00147   template<typename TUPLE>
00148   back_insert_iterator &operator=(const TUPLE &T)                       //[t83]
00149   {
00150     m_Writer->insert(T);
00151     return *this;
00152   }
00153 
00154   back_insert_iterator &operator++() { return *this; }                  //[t83]
00155   back_insert_iterator &operator++(int) { return *this; }               //[t83]
00156   back_insert_iterator &operator*() { return *this; }                   //[t83]
00157 
00158 private:
00159   pqxx::tablewriter *m_Writer;
00160 };
00161 
00162 } // namespace PGSTD
00163 
00164 
00165 namespace pqxx
00166 {
00167 
00168 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
00169     const PGSTD::string &WName,
00170     ITER begincolumns,
00171     ITER endcolumns) :
00172   namedclass("tablewriter", WName),
00173   tablestream(T, PGSTD::string())
00174 {
00175   setup(T, WName, columnlist(begincolumns, endcolumns));
00176 }
00177 
00178 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
00179     const PGSTD::string &WName,
00180     ITER begincolumns,
00181     ITER endcolumns,
00182     const PGSTD::string &Null) :
00183   namedclass("tablewriter", WName),
00184   tablestream(T, Null)
00185 {
00186   setup(T, WName, columnlist(begincolumns, endcolumns));
00187 }
00188 
00189 
00190 namespace internal
00191 {
00192 PGSTD::string PQXX_LIBEXPORT Escape(const PGSTD::string &s,
00193     const PGSTD::string &null);
00194 
00195 template<typename STR> inline PGSTD::string EscapeAny(const PGSTD::string &s,
00196     const PGSTD::string &null) { return Escape(s,null); }
00197 template<typename STR> inline PGSTD::string EscapeAny(const char s[],
00198     const PGSTD::string &null) {return s ? Escape(PGSTD::string(s),null):"\\N";}
00199 template<typename T> inline PGSTD::string EscapeAny(const T &t,
00200     const PGSTD::string &null) { return Escape(to_string(t), null); }
00201 
00202 template<typename IT> class Escaper
00203 {
00204   const PGSTD::string &m_null;
00205 public:
00206   explicit Escaper(const PGSTD::string &null) : m_null(null) {}
00207   PGSTD::string operator()(IT i) const { return EscapeAny(*i, m_null); }
00208 };
00209 
00210 }
00211 
00212 template<typename IT>
00213 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
00214 {
00215   return separated_list("\t", Begin, End, internal::Escaper<IT>(NullStr()));
00216 }
00217 
00218 
00219 template<typename TUPLE>
00220 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
00221 {
00222   return generate(T.begin(), T.end());
00223 }
00224 
00225 
00226 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
00227 {
00228   write_raw_line(generate(Begin, End));
00229 }
00230 
00231 
00232 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
00233 {
00234   insert(T.begin(), T.end());
00235 }
00236 
00237 template<typename IT>
00238 inline void tablewriter::push_back(IT Begin, IT End)
00239 {
00240   insert(Begin, End);
00241 }
00242 
00243 template<typename TUPLE>
00244 inline void tablewriter::push_back(const TUPLE &T)
00245 {
00246   insert(T.begin(), T.end());
00247 }
00248 
00249 template<typename TUPLE>
00250 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
00251 {
00252   insert(T);
00253   return *this;
00254 }
00255 
00256 } // namespace pqxx
00257 
00258 
00259 #include "pqxx/compiler-internal-post.hxx"
00260 
00261 #endif
00262 

Generated on Sat Aug 15 00:16:49 2009 for libpqxx by  doxygen 1.5.8