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 #ifdef PQXX_DEPRECATED_HEADERS
00097   template<typename IT> PGSTD::string ezinekoT(IT Begin, IT End) const
00098         PQXX_DEPRECATED { return generate(Begin, End); }
00100   template<typename TUPLE> PGSTD::string ezinekoT(const TUPLE &T) const
00101         PQXX_DEPRECATED { return generate(T); }
00102 #endif
00103 
00104 private:
00105   void setup(transaction_base &,
00106       const PGSTD::string &WName,
00107       const PGSTD::string &Columns = PGSTD::string());
00108 
00109   void WriteRawLine(const PGSTD::string &);
00110   void PQXX_PRIVATE writer_close();
00111 };
00112 
00113 } // namespace pqxx
00114 
00115 
00116 
00117 namespace PGSTD
00118 {
00120 
00123 template<>
00124   class back_insert_iterator<pqxx::tablewriter> :                       //[t9]
00125         public iterator<output_iterator_tag, void,void,void,void>
00126 {
00127 public:
00128   explicit back_insert_iterator(pqxx::tablewriter &W) throw () :        //[t83]
00129     m_Writer(&W) {}
00130 
00131   back_insert_iterator &
00132     operator=(const back_insert_iterator &rhs) throw ()                 //[t83]
00133   {
00134     m_Writer = rhs.m_Writer;
00135     return *this;
00136   }
00137 
00138   template<typename TUPLE>
00139   back_insert_iterator &operator=(const TUPLE &T)                       //[t83]
00140   {
00141     m_Writer->insert(T);
00142     return *this;
00143   }
00144 
00145   back_insert_iterator &operator++() { return *this; }                  //[t83]
00146   back_insert_iterator &operator++(int) { return *this; }               //[t83]
00147   back_insert_iterator &operator*() { return *this; }                   //[t83]
00148 
00149 private:
00150   pqxx::tablewriter *m_Writer;
00151 };
00152 
00153 } // namespace PGSTD
00154 
00155 
00156 namespace pqxx
00157 {
00158 
00159 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
00160     const PGSTD::string &WName,
00161     ITER begincolumns,
00162     ITER endcolumns) :
00163   namedclass("tablewriter", WName),
00164   tablestream(T, PGSTD::string())
00165 {
00166   setup(T, WName, columnlist(begincolumns, endcolumns));
00167 }
00168 
00169 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
00170     const PGSTD::string &WName,
00171     ITER begincolumns,
00172     ITER endcolumns,
00173     const PGSTD::string &Null) :
00174   namedclass("tablewriter", WName),
00175   tablestream(T, Null)
00176 {
00177   setup(T, WName, columnlist(begincolumns, endcolumns));
00178 }
00179 
00180 
00181 namespace internal
00182 {
00183 PGSTD::string PQXX_LIBEXPORT Escape(const PGSTD::string &s,
00184     const PGSTD::string &null);
00185 
00186 template<typename STR> inline PGSTD::string EscapeAny(const PGSTD::string &s,
00187     const PGSTD::string &null) { return Escape(s,null); }
00188 template<typename STR> inline PGSTD::string EscapeAny(const char s[],
00189     const PGSTD::string &null) {return s ? Escape(PGSTD::string(s),null):"\\N";}
00190 template<typename T> inline PGSTD::string EscapeAny(const T &t,
00191     const PGSTD::string &null) { return Escape(to_string(t), null); }
00192 
00193 template<typename IT> class Escaper
00194 {
00195   const PGSTD::string m_null;
00196 public:
00197   explicit Escaper(const PGSTD::string &null) : m_null(null) {}
00198   PGSTD::string operator()(IT i) const { return EscapeAny(*i, m_null); }
00199 };
00200 
00201 }
00202 
00203 template<typename IT>
00204 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
00205 {
00206   return separated_list("\t", Begin, End, internal::Escaper<IT>(NullStr()));
00207 }
00208 
00209 
00210 template<typename TUPLE>
00211 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
00212 {
00213   return generate(T.begin(), T.end());
00214 }
00215 
00216 
00217 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
00218 {
00219   WriteRawLine(generate(Begin, End));
00220 }
00221 
00222 
00223 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
00224 {
00225   insert(T.begin(), T.end());
00226 }
00227 
00228 template<typename IT>
00229 inline void tablewriter::push_back(IT Begin, IT End)
00230 {
00231   insert(Begin, End);
00232 }
00233 
00234 template<typename TUPLE>
00235 inline void tablewriter::push_back(const TUPLE &T)
00236 {
00237   insert(T.begin(), T.end());
00238 }
00239 
00240 template<typename TUPLE>
00241 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
00242 {
00243   insert(T);
00244   return *this;
00245 }
00246 
00247 } // namespace pqxx
00248 
00249 
00250 #include "pqxx/compiler-internal-post.hxx"

Generated on Wed Sep 6 16:54:12 2006 for libpqxx by  doxygen 1.4.7