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-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/libcompiler.h" 00020 00021 #include <string> 00022 00023 #include "pqxx/tablestream" 00024 00025 /* Methods tested in eg. self-test program test001 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 &, 00048 const PGSTD::string &WName, 00049 const PGSTD::string &Null=PGSTD::string()); //[t5] 00050 00052 00054 template<typename ITER> 00055 tablewriter(transaction_base &, 00056 const PGSTD::string &WName, 00057 ITER begincolumns, 00058 ITER endcolumns, 00059 const PGSTD::string &Null=PGSTD::string()); //[t9] 00060 00061 ~tablewriter() throw (); //[t5] 00062 00063 template<typename IT> void insert(IT Begin, IT End); //[t5] 00064 template<typename TUPLE> void insert(const TUPLE &); //[t5] 00065 template<typename IT> void push_back(IT Begin, IT End); //[t10] 00066 template<typename TUPLE> void push_back(const TUPLE &); //[t10] 00067 00068 void reserve(size_type) {} //[t9] 00069 00070 template<typename TUPLE> tablewriter &operator<<(const TUPLE &); //[t5] 00071 00073 tablewriter &operator<<(tablereader &); //[t6] 00074 00076 00078 template<typename IT> PGSTD::string generate(IT Begin, IT End) const; //[t10] 00079 template<typename TUPLE> PGSTD::string generate(const TUPLE &) const; //[t10] 00080 00082 00089 virtual void complete(); //[t5] 00090 00091 #ifdef PQXX_DEPRECATED_HEADERS 00093 template<typename IT> PGSTD::string ezinekoT(IT Begin, IT End) const 00094 { return generate(Begin, End); } 00096 template<typename TUPLE> PGSTD::string ezinekoT(const TUPLE &T) const 00097 { return generate(T); } 00098 #endif 00099 00100 private: 00101 void setup(transaction_base &, 00102 const PGSTD::string &WName, 00103 const PGSTD::string &Columns = PGSTD::string()); 00104 void WriteRawLine(const PGSTD::string &); 00105 void flush_pending(); 00106 void writer_close(); 00107 PGSTD::string EscapeAny(const char *) const; 00108 PGSTD::string EscapeAny(const PGSTD::string &) const; 00109 template<typename T> PGSTD::string EscapeAny(const T &) const; 00110 00111 static PGSTD::string Escape(const PGSTD::string &); 00112 00113 PGSTD::string m_PendingLine; 00114 }; 00115 00116 } // namespace pqxx 00117 00118 00119 00120 namespace PGSTD 00121 { 00123 00126 template<> 00127 class back_insert_iterator<pqxx::tablewriter> : //[t9] 00128 public iterator<output_iterator_tag, void,void,void,void> 00129 { 00130 public: 00131 explicit back_insert_iterator(pqxx::tablewriter &W) : m_Writer(W) {} 00132 00133 template<typename TUPLE> 00134 back_insert_iterator &operator=(const TUPLE &T) 00135 { 00136 m_Writer.insert(T); 00137 return *this; 00138 } 00139 00140 back_insert_iterator &operator++() { return *this; } 00141 back_insert_iterator &operator++(int) { return *this; } 00142 back_insert_iterator &operator*() { return *this; } 00143 00144 private: 00145 pqxx::tablewriter &m_Writer; 00146 }; 00147 00148 } // namespace PGSTD 00149 00150 00151 namespace pqxx 00152 { 00153 00154 template<typename ITER> inline 00155 tablewriter::tablewriter(transaction_base &T, 00156 const PGSTD::string &WName, 00157 ITER begincolumns, 00158 ITER endcolumns, 00159 const PGSTD::string &Null) : 00160 tablestream(T, WName, Null, "tablewriter"), 00161 m_PendingLine() 00162 { 00163 setup(T, WName, columnlist(begincolumns, endcolumns)); 00164 } 00165 00166 00167 inline PGSTD::string tablewriter::EscapeAny(const PGSTD::string &t) const 00168 { 00169 return (t == NullStr()) ? "\\N" : Escape(t); 00170 } 00171 00172 inline PGSTD::string tablewriter::EscapeAny(const char t[]) const 00173 { 00174 return t ? EscapeAny(PGSTD::string(t)) : "\\N"; 00175 } 00176 00177 template<typename T> inline PGSTD::string 00178 tablewriter::EscapeAny(const T &t) const 00179 { 00180 return EscapeAny(to_string(t)); 00181 } 00182 00183 00184 template<typename IT> 00185 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const 00186 { 00187 PGSTD::string Line; 00188 for (; Begin != End; ++Begin) 00189 { 00190 Line += EscapeAny(*Begin); 00191 Line += "\t"; 00192 } 00193 00194 // Above algorithm generates one separating tab too many. Take it back. 00195 if (!Line.empty()) Line.erase(Line.size()-1); 00196 00197 return Line; 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 } 00239 00240

Generated on Sun Jun 6 20:55:10 2004 for libpqxx by doxygen 1.3.7