tablewriter.hxx

Go to the documentation of this file.
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-2006, 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 #include "pqxx/compiler-public.hxx"
00020 #include "pqxx/compiler-internal-pre.hxx"
00021 
00022 #include "pqxx/tablestream"
00023 
00024 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
00025  */
00026 
00027 namespace pqxx
00028 {
00029 class tablereader;      // See pqxx/tablereader.h
00030 
00032 
00042 class PQXX_LIBEXPORT tablewriter : public tablestream
00043 {
00044 public:
00045   typedef unsigned size_type;
00046 
00047   tablewriter(transaction_base &,
00048       const PGSTD::string &WName,
00049       const PGSTD::string &Null=PGSTD::string());                       //[t5]
00050 
00052 
00054   template<typename ITER> tablewriter(transaction_base &,
00055       const PGSTD::string &WName,
00056       ITER begincolumns,
00057       ITER endcolumns);                                                 //[t9]
00058 
00059   template<typename ITER> tablewriter(transaction_base &,
00060       const PGSTD::string &WName,
00061       ITER begincolumns,
00062       ITER endcolumns,
00063       const PGSTD::string &Null);                                       //[t9]
00064 
00065   ~tablewriter() throw ();                                              //[t5]
00066 
00067   template<typename IT> void insert(IT Begin, IT End);                  //[t5]
00068   template<typename TUPLE> void insert(const TUPLE &);                  //[t5]
00069   template<typename IT> void push_back(IT Begin, IT End);               //[t10]
00070   template<typename TUPLE> void push_back(const TUPLE &);               //[t10]
00071 
00072   void reserve(size_type) {}                                            //[t9]
00073 
00074   template<typename TUPLE> tablewriter &operator<<(const TUPLE &);      //[t5]
00075 
00077   tablewriter &operator<<(tablereader &);                               //[t6]
00078 
00080 
00082   template<typename IT> PGSTD::string generate(IT Begin, IT End) const; //[t10]
00083   template<typename TUPLE> PGSTD::string generate(const TUPLE &) const; //[t10]
00084 
00086 
00093   virtual void complete();                                              //[t5]
00094 
00095 private:
00096   void setup(transaction_base &,
00097       const PGSTD::string &WName,
00098       const PGSTD::string &Columns = PGSTD::string());
00099 
00100   void WriteRawLine(const PGSTD::string &);
00101   void PQXX_PRIVATE writer_close();
00102 };
00103 
00104 } // namespace pqxx
00105 
00106 
00107 
00108 namespace PGSTD
00109 {
00111 
00114 template<>
00115   class back_insert_iterator<pqxx::tablewriter> :                       //[t9]
00116         public iterator<output_iterator_tag, void,void,void,void>
00117 {
00118 public:
00119   explicit back_insert_iterator(pqxx::tablewriter &W) throw () :        //[t83]
00120     m_Writer(&W) {}
00121 
00122   back_insert_iterator &
00123     operator=(const back_insert_iterator &rhs) throw ()                 //[t83]
00124   {
00125     m_Writer = rhs.m_Writer;
00126     return *this;
00127   }
00128 
00129   template<typename TUPLE>
00130   back_insert_iterator &operator=(const TUPLE &T)                       //[t83]
00131   {
00132     m_Writer->insert(T);
00133     return *this;
00134   }
00135 
00136   back_insert_iterator &operator++() { return *this; }                  //[t83]
00137   back_insert_iterator &operator++(int) { return *this; }               //[t83]
00138   back_insert_iterator &operator*() { return *this; }                   //[t83]
00139 
00140 private:
00141   pqxx::tablewriter *m_Writer;
00142 };
00143 
00144 } // namespace PGSTD
00145 
00146 
00147 namespace pqxx
00148 {
00149 
00150 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
00151     const PGSTD::string &WName,
00152     ITER begincolumns,
00153     ITER endcolumns) :
00154   namedclass("tablewriter", WName),
00155   tablestream(T, PGSTD::string())
00156 {
00157   setup(T, WName, columnlist(begincolumns, endcolumns));
00158 }
00159 
00160 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
00161     const PGSTD::string &WName,
00162     ITER begincolumns,
00163     ITER endcolumns,
00164     const PGSTD::string &Null) :
00165   namedclass("tablewriter", WName),
00166   tablestream(T, Null)
00167 {
00168   setup(T, WName, columnlist(begincolumns, endcolumns));
00169 }
00170 
00171 
00172 namespace internal
00173 {
00174 PGSTD::string PQXX_LIBEXPORT Escape(const PGSTD::string &s,
00175     const PGSTD::string &null);
00176 
00177 template<typename STR> inline PGSTD::string EscapeAny(const PGSTD::string &s,
00178     const PGSTD::string &null) { return Escape(s,null); }
00179 template<typename STR> inline PGSTD::string EscapeAny(const char s[],
00180     const PGSTD::string &null) {return s ? Escape(PGSTD::string(s),null):"\\N";}
00181 template<typename T> inline PGSTD::string EscapeAny(const T &t,
00182     const PGSTD::string &null) { return Escape(to_string(t), null); }
00183 
00184 template<typename IT> class Escaper
00185 {
00186   const PGSTD::string m_null;
00187 public:
00188   explicit Escaper(const PGSTD::string &null) : m_null(null) {}
00189   PGSTD::string operator()(IT i) const { return EscapeAny(*i, m_null); }
00190 };
00191 
00192 }
00193 
00194 template<typename IT>
00195 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
00196 {
00197   return separated_list("\t", Begin, End, internal::Escaper<IT>(NullStr()));
00198 }
00199 
00200 
00201 template<typename TUPLE>
00202 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
00203 {
00204   return generate(T.begin(), T.end());
00205 }
00206 
00207 
00208 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
00209 {
00210   WriteRawLine(generate(Begin, End));
00211 }
00212 
00213 
00214 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
00215 {
00216   insert(T.begin(), T.end());
00217 }
00218 
00219 template<typename IT>
00220 inline void tablewriter::push_back(IT Begin, IT End)
00221 {
00222   insert(Begin, End);
00223 }
00224 
00225 template<typename TUPLE>
00226 inline void tablewriter::push_back(const TUPLE &T)
00227 {
00228   insert(T.begin(), T.end());
00229 }
00230 
00231 template<typename TUPLE>
00232 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
00233 {
00234   insert(T);
00235   return *this;
00236 }
00237 
00238 } // namespace pqxx
00239 
00240 
00241 #include "pqxx/compiler-internal-post.hxx"

Generated on Thu Feb 1 17:12:08 2007 for libpqxx by  doxygen 1.5.1