00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "pqxx/libcompiler.h"
00020
00021 #include <string>
00022
00023 #include "pqxx/tablestream"
00024
00025
00026
00027
00028 namespace pqxx
00029 {
00030 class tablereader;
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());
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());
00060
00061 ~tablewriter() throw ();
00062
00063 template<typename IT> void insert(IT Begin, IT End);
00064 template<typename TUPLE> void insert(const TUPLE &);
00065 template<typename IT> void push_back(IT Begin, IT End);
00066 template<typename TUPLE> void push_back(const TUPLE &);
00067
00068 void reserve(size_type) {}
00069
00070 template<typename TUPLE> tablewriter &operator<<(const TUPLE &);
00071
00073 tablewriter &operator<<(tablereader &);
00074
00076
00078 template<typename IT> PGSTD::string generate(IT Begin, IT End) const;
00079 template<typename TUPLE> PGSTD::string generate(const TUPLE &) const;
00080
00082
00089 virtual void complete();
00090
00091 #ifdef PQXX_DEPRECATED_HEADERS
00092
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 writer_close();
00106 PGSTD::string EscapeAny(const char *) const;
00107 PGSTD::string EscapeAny(const PGSTD::string &) const;
00108 template<typename T> PGSTD::string EscapeAny(const T &) const;
00109
00110 static PGSTD::string Escape(const PGSTD::string &);
00111 };
00112
00113 }
00114
00115
00116
00117 namespace PGSTD
00118 {
00120
00123 template<>
00124 class back_insert_iterator<pqxx::tablewriter> :
00125 public iterator<output_iterator_tag, void,void,void,void>
00126 {
00127 public:
00128 explicit back_insert_iterator(pqxx::tablewriter &W) throw () :
00129 m_Writer(&W) {}
00130
00131 back_insert_iterator &
00132 operator=(const back_insert_iterator &rhs) throw ()
00133 {
00134 m_Writer = rhs.m_Writer;
00135 return *this;
00136 }
00137
00138 template<typename TUPLE>
00139 back_insert_iterator &operator=(const TUPLE &T)
00140 {
00141 m_Writer->insert(T);
00142 return *this;
00143 }
00144
00145 back_insert_iterator &operator++() { return *this; }
00146 back_insert_iterator &operator++(int) { return *this; }
00147 back_insert_iterator &operator*() { return *this; }
00148
00149 private:
00150 pqxx::tablewriter *m_Writer;
00151 };
00152
00153 }
00154
00155
00156 namespace pqxx
00157 {
00158
00159 template<typename ITER> inline
00160 tablewriter::tablewriter(transaction_base &T,
00161 const PGSTD::string &WName,
00162 ITER begincolumns,
00163 ITER endcolumns,
00164 const PGSTD::string &Null) :
00165 tablestream(T, WName, Null, "tablewriter")
00166 {
00167 setup(T, WName, columnlist(begincolumns, endcolumns));
00168 }
00169
00170
00171 inline PGSTD::string tablewriter::EscapeAny(const PGSTD::string &t) const
00172 {
00173 return (t == NullStr()) ? "\\N" : Escape(t);
00174 }
00175
00176 inline PGSTD::string tablewriter::EscapeAny(const char t[]) const
00177 {
00178 return t ? EscapeAny(PGSTD::string(t)) : "\\N";
00179 }
00180
00181 template<typename T> inline PGSTD::string
00182 tablewriter::EscapeAny(const T &t) const
00183 {
00184 return EscapeAny(to_string(t));
00185 }
00186
00187
00188 template<typename IT>
00189 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
00190 {
00191 PGSTD::string Line;
00192 for (; Begin != End; ++Begin)
00193 {
00194 Line += EscapeAny(*Begin);
00195 Line += "\t";
00196 }
00197
00198
00199 if (!Line.empty()) Line.erase(Line.size()-1);
00200
00201 return Line;
00202 }
00203
00204
00205 template<typename TUPLE>
00206 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
00207 {
00208 return generate(T.begin(), T.end());
00209 }
00210
00211
00212 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
00213 {
00214 WriteRawLine(generate(Begin, End));
00215 }
00216
00217
00218 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
00219 {
00220 insert(T.begin(), T.end());
00221 }
00222
00223 template<typename IT>
00224 inline void tablewriter::push_back(IT Begin, IT End)
00225 {
00226 insert(Begin, End);
00227 }
00228
00229 template<typename TUPLE>
00230 inline void tablewriter::push_back(const TUPLE &T)
00231 {
00232 insert(T.begin(), T.end());
00233 }
00234
00235 template<typename TUPLE>
00236 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
00237 {
00238 insert(T);
00239 return *this;
00240 }
00241
00242 }
00243
00244