Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

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-2004, 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/tablestream"
00020 
00021 /* Methods tested in eg. self-test program test001 are marked with "//[t1]"
00022  */
00023 
00024 namespace pqxx
00025 {
00026 class tablereader;      // See pqxx/tablereader.h
00027 
00029 
00039 class PQXX_LIBEXPORT tablewriter : public tablestream
00040 {
00041 public:
00042   typedef unsigned size_type;
00043 
00044   tablewriter(transaction_base &, 
00045       const PGSTD::string &WName,
00046       const PGSTD::string &Null=PGSTD::string());                       //[t5]
00047 
00049 
00051   template<typename ITER>
00052   tablewriter(transaction_base &, 
00053       const PGSTD::string &WName,
00054       ITER begincolumns,
00055       ITER endcolumns,
00056       const PGSTD::string &Null=PGSTD::string());                       //[t9]
00057 
00058   ~tablewriter() throw ();                                              //[t5]
00059 
00060   template<typename IT> void insert(IT Begin, IT End);                  //[t5]
00061   template<typename TUPLE> void insert(const TUPLE &);                  //[t5]
00062   template<typename IT> void push_back(IT Begin, IT End);               //[t10]
00063   template<typename TUPLE> void push_back(const TUPLE &);               //[t10]
00064 
00065   void reserve(size_type) {}                                            //[t9]
00066 
00067   template<typename TUPLE> tablewriter &operator<<(const TUPLE &);      //[t5]
00068 
00070   tablewriter &operator<<(tablereader &);                               //[t6]
00071 
00073 
00075   template<typename IT> PGSTD::string generate(IT Begin, IT End) const; //[t10]
00076   template<typename TUPLE> PGSTD::string generate(const TUPLE &) const; //[t10]
00077 
00079 
00086   virtual void complete();                                              //[t5]
00087 
00088 #ifdef PQXX_DEPRECATED_HEADERS
00089 
00090   template<typename IT> PGSTD::string ezinekoT(IT Begin, IT End) const
00091         { return generate(Begin, End); }
00093   template<typename TUPLE> PGSTD::string ezinekoT(const TUPLE &T) const
00094         { return generate(T); }
00095 #endif
00096 
00097 private:
00098   void setup(transaction_base &, 
00099       const PGSTD::string &WName,
00100       const PGSTD::string &Columns = PGSTD::string());
00101   void WriteRawLine(const PGSTD::string &);
00102   void writer_close();
00103   PGSTD::string EscapeAny(const char *) const;
00104   PGSTD::string EscapeAny(const PGSTD::string &) const;
00105   template<typename T> PGSTD::string EscapeAny(const T &) const;
00106 
00107   static PGSTD::string Escape(const PGSTD::string &);
00108 };
00109 
00110 } // namespace pqxx
00111 
00112 
00113 
00114 namespace PGSTD
00115 {
00117 
00120 template<> 
00121   class back_insert_iterator<pqxx::tablewriter> :                       //[t9]
00122         public iterator<output_iterator_tag, void,void,void,void>
00123 {
00124 public:
00125   explicit back_insert_iterator(pqxx::tablewriter &W) throw () :        //[t83]
00126     m_Writer(&W) {}
00127 
00128   back_insert_iterator &
00129     operator=(const back_insert_iterator &rhs) throw ()                 //[t83]
00130   {
00131     m_Writer = rhs.m_Writer;
00132     return *this;
00133   }
00134 
00135   template<typename TUPLE> 
00136   back_insert_iterator &operator=(const TUPLE &T)                       //[t83]
00137   {
00138     m_Writer->insert(T);
00139     return *this;
00140   }
00141 
00142   back_insert_iterator &operator++() { return *this; }                  //[t83]
00143   back_insert_iterator &operator++(int) { return *this; }               //[t83]
00144   back_insert_iterator &operator*() { return *this; }                   //[t83]
00145 
00146 private:
00147   pqxx::tablewriter *m_Writer;
00148 };
00149 
00150 } // namespace PGSTD
00151 
00152 
00153 namespace pqxx
00154 {
00155 
00156 template<typename ITER> inline
00157 tablewriter::tablewriter(transaction_base &T,
00158     const PGSTD::string &WName,
00159     ITER begincolumns,
00160     ITER endcolumns,
00161     const PGSTD::string &Null) :
00162   tablestream(T, WName, Null, "tablewriter")
00163 {
00164   setup(T, WName, columnlist(begincolumns, endcolumns));
00165 }
00166 
00167 
00168 inline PGSTD::string tablewriter::EscapeAny(const PGSTD::string &t) const
00169 {
00170   return (t == NullStr()) ? "\\N" : Escape(t);
00171 }
00172 
00173 inline PGSTD::string tablewriter::EscapeAny(const char t[]) const
00174 {
00175   return t ? EscapeAny(PGSTD::string(t)) : "\\N";
00176 }
00177 
00178 template<typename T> inline PGSTD::string
00179 tablewriter::EscapeAny(const T &t) const
00180 {
00181   return EscapeAny(to_string(t));
00182 }
00183 
00184 
00185 template<typename IT> 
00186 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
00187 {
00188   PGSTD::string Line;
00189   for (; Begin != End; ++Begin)
00190   {
00191     Line += EscapeAny(*Begin);
00192     Line += "\t";
00193   }
00194 
00195   // Above algorithm generates one separating tab too many.  Take it back.
00196   if (!Line.empty()) Line.erase(Line.size()-1);
00197 
00198   return Line;
00199 }
00200 
00201 
00202 template<typename TUPLE> 
00203 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
00204 {
00205   return generate(T.begin(), T.end());
00206 }
00207 
00208 
00209 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
00210 {
00211   WriteRawLine(generate(Begin, End));
00212 }
00213 
00214 
00215 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
00216 {
00217   insert(T.begin(), T.end());
00218 }
00219 
00220 template<typename IT> 
00221 inline void tablewriter::push_back(IT Begin, IT End)
00222 {
00223   insert(Begin, End);
00224 }
00225 
00226 template<typename TUPLE> 
00227 inline void tablewriter::push_back(const TUPLE &T)
00228 {
00229   insert(T.begin(), T.end());
00230 }
00231 
00232 template<typename TUPLE> 
00233 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
00234 {
00235   insert(T);
00236   return *this;
00237 }
00238 
00239 }
00240 
00241 

Generated on Mon Nov 15 11:28:00 2004 for libpqxx by  doxygen 1.3.9.1