Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | 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-2003, 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/libcompiler.h"
00020 
00021 #include <string>
00022 
00023 #include "pqxx/tablestream"
00024 
00025 /* Methods tested in eg. self-test program test1 are marked with "//[t1]"
00026  */
00027 
00028 namespace pqxx
00029 {
00030 class tablereader;      // See pqxx/tablereader.h
00031 
00033 
00042 class PQXX_LIBEXPORT tablewriter : public tablestream
00043 {
00044 public:
00045   typedef unsigned size_type;
00046 
00047   tablewriter(transaction_base &Trans, 
00048       const PGSTD::string &WName,
00049       const PGSTD::string &Null=PGSTD::string());                       //[t5]
00050   ~tablewriter();                                                       //[t5]
00051 
00052   template<typename IT> void insert(IT Begin, IT End);                  //[t5]
00053   template<typename TUPLE> void insert(const TUPLE &);                  //[t5]
00054   template<typename IT> void push_back(IT Begin, IT End);               //[t10]
00055   template<typename TUPLE> void push_back(const TUPLE &);               //[t10]
00056 
00057   void reserve(size_type) {}                                            //[t9]
00058 
00059   template<typename TUPLE> tablewriter &operator<<(const TUPLE &);      //[t5]
00060 
00062   tablewriter &operator<<(tablereader &);                               //[t6]
00063 
00065 
00067   template<typename IT> PGSTD::string generate(IT Begin, IT End) const; //[t10]
00068   template<typename TUPLE> PGSTD::string generate(const TUPLE &) const; //[t10]
00069 
00071 
00078   virtual void complete();                                              //[t5]
00079 
00080 #ifdef PQXX_DEPRECATED_HEADERS
00081 
00082   template<typename IT> PGSTD::string ezinekoT(IT Begin, IT End) const
00083         { return generate(Begin, End); }
00085   template<typename TUPLE> PGSTD::string ezinekoT(const TUPLE &T) const
00086         { return generate(T); }
00087 #endif
00088 
00089 private:
00090   void WriteRawLine(const PGSTD::string &);
00091   void flush_pending();
00092   void writer_close();
00093   PGSTD::string EscapeAny(const char *) const;
00094   PGSTD::string EscapeAny(const PGSTD::string &) const;
00095   template<typename T> PGSTD::string EscapeAny(const T &) const;
00096 
00097   static PGSTD::string Escape(const PGSTD::string &);
00098 
00099   PGSTD::string m_PendingLine;
00100 };
00101 
00102 } // namespace pqxx
00103 
00104 
00105 
00106 namespace PGSTD
00107 {
00109 
00112 template<> 
00113   class back_insert_iterator<pqxx::tablewriter> :                       //[t9]
00114         public iterator<output_iterator_tag, void,void,void,void>
00115 {
00116 public:
00117   explicit back_insert_iterator(pqxx::tablewriter &W) : m_Writer(W) {}
00118 
00119   template<typename TUPLE> 
00120   back_insert_iterator &operator=(const TUPLE &T)
00121   {
00122     m_Writer.insert(T);
00123     return *this;
00124   }
00125 
00126   back_insert_iterator &operator++() { return *this; }
00127   back_insert_iterator &operator++(int) { return *this; }
00128   back_insert_iterator &operator*() { return *this; }
00129 
00130 private:
00131   pqxx::tablewriter &m_Writer;
00132 };
00133 
00134 } // namespace PGSTD
00135 
00136 
00137 namespace pqxx
00138 {
00139 
00140 inline PGSTD::string tablewriter::EscapeAny(const PGSTD::string &t) const
00141 {
00142   return (t == NullStr()) ? "\\N" : Escape(t);
00143 }
00144 
00145 inline PGSTD::string tablewriter::EscapeAny(const char t[]) const
00146 {
00147   return t ? EscapeAny(PGSTD::string(t)) : "\\N";
00148 }
00149 
00150 template<typename T> inline PGSTD::string
00151 tablewriter::EscapeAny(const T &t) const
00152 {
00153   return EscapeAny(ToString(t));
00154 }
00155 
00156 
00157 template<typename IT> 
00158 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
00159 {
00160   PGSTD::string Line;
00161   for (; Begin != End; ++Begin)
00162   {
00163     Line += EscapeAny(*Begin);
00164     Line += "\t";
00165   }
00166 
00167   // Above algorithm generates one separating tab too many.  Take it back.
00168   if (!Line.empty()) Line.erase(Line.size()-1);
00169 
00170   return Line;
00171 }
00172 
00173 
00174 template<typename TUPLE> 
00175 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
00176 {
00177   return generate(T.begin(), T.end());
00178 }
00179 
00180 
00181 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
00182 {
00183   WriteRawLine(generate(Begin, End));
00184 }
00185 
00186 
00187 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
00188 {
00189   insert(T.begin(), T.end());
00190 }
00191 
00192 template<typename IT> 
00193 inline void tablewriter::push_back(IT Begin, IT End)
00194 {
00195   insert(Begin, End);
00196 }
00197 
00198 template<typename TUPLE> 
00199 inline void tablewriter::push_back(const TUPLE &T)
00200 {
00201   insert(T.begin(), T.end());
00202 }
00203 
00204 template<typename TUPLE> 
00205 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
00206 {
00207   insert(T);
00208   return *this;
00209 }
00210 
00211 }
00212 
00213 

Generated on Thu Dec 25 07:22:37 2003 for libpqxx by doxygen 1.3.4