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 ~tablewriter() throw ();
00061
00062 template<typename IT> void insert(IT Begin, IT End);
00063 template<typename TUPLE> void insert(const TUPLE &);
00064 template<typename IT> void push_back(IT Begin, IT End);
00065 template<typename TUPLE> void push_back(const TUPLE &);
00066
00067 void reserve(size_type) {}
00068
00069 template<typename TUPLE> tablewriter &operator<<(const TUPLE &);
00070
00072 tablewriter &operator<<(tablereader &);
00073
00075
00077 template<typename IT> PGSTD::string generate(IT Begin, IT End) const;
00078 template<typename TUPLE> PGSTD::string generate(const TUPLE &) const;
00079
00081
00088 virtual void complete();
00089
00090 #ifdef PQXX_DEPRECATED_HEADERS
00091
00092 template<typename IT> PGSTD::string ezinekoT(IT Begin, IT End) const
00093 { return generate(Begin, End); }
00095 template<typename TUPLE> PGSTD::string ezinekoT(const TUPLE &T) const
00096 { return generate(T); }
00097 #endif
00098
00099 private:
00100 void setup(transaction_base &,
00101 const PGSTD::string &WName,
00102 const PGSTD::string &Columns = PGSTD::string());
00103 void WriteRawLine(const PGSTD::string &);
00104 void flush_pending();
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 PGSTD::string m_PendingLine;
00113 };
00114
00115 }
00116
00117
00118
00119 namespace PGSTD
00120 {
00122
00125 template<>
00126 class back_insert_iterator<pqxx::tablewriter> :
00127 public iterator<output_iterator_tag, void,void,void,void>
00128 {
00129 public:
00130 explicit back_insert_iterator(pqxx::tablewriter &W) : m_Writer(W) {}
00131
00132 template<typename TUPLE>
00133 back_insert_iterator &operator=(const TUPLE &T)
00134 {
00135 m_Writer.insert(T);
00136 return *this;
00137 }
00138
00139 back_insert_iterator &operator++() { return *this; }
00140 back_insert_iterator &operator++(int) { return *this; }
00141 back_insert_iterator &operator*() { return *this; }
00142
00143 private:
00144 pqxx::tablewriter &m_Writer;
00145 };
00146
00147 }
00148
00149
00150 namespace pqxx
00151 {
00152
00153 template<typename ITER> inline
00154 tablewriter::tablewriter(transaction_base &T,
00155 const PGSTD::string &WName,
00156 ITER begincolumns,
00157 ITER endcolumns,
00158 const PGSTD::string &Null) :
00159 tablestream(T, WName, Null, "tablewriter"),
00160 m_PendingLine()
00161 {
00162 setup(T, WName, columnlist(begincolumns, endcolumns));
00163 }
00164
00165
00166 inline PGSTD::string tablewriter::EscapeAny(const PGSTD::string &t) const
00167 {
00168 return (t == NullStr()) ? "\\N" : Escape(t);
00169 }
00170
00171 inline PGSTD::string tablewriter::EscapeAny(const char t[]) const
00172 {
00173 return t ? EscapeAny(PGSTD::string(t)) : "\\N";
00174 }
00175
00176 template<typename T> inline PGSTD::string
00177 tablewriter::EscapeAny(const T &t) const
00178 {
00179 return EscapeAny(to_string(t));
00180 }
00181
00182
00183 template<typename IT>
00184 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
00185 {
00186 PGSTD::string Line;
00187 for (; Begin != End; ++Begin)
00188 {
00189 Line += EscapeAny(*Begin);
00190 Line += "\t";
00191 }
00192
00193
00194 if (!Line.empty()) Line.erase(Line.size()-1);
00195
00196 return Line;
00197 }
00198
00199
00200 template<typename TUPLE>
00201 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
00202 {
00203 return generate(T.begin(), T.end());
00204 }
00205
00206
00207 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
00208 {
00209 WriteRawLine(generate(Begin, End));
00210 }
00211
00212
00213 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
00214 {
00215 insert(T.begin(), T.end());
00216 }
00217
00218 template<typename IT>
00219 inline void tablewriter::push_back(IT Begin, IT End)
00220 {
00221 insert(Begin, End);
00222 }
00223
00224 template<typename TUPLE>
00225 inline void tablewriter::push_back(const TUPLE &T)
00226 {
00227 insert(T.begin(), T.end());
00228 }
00229
00230 template<typename TUPLE>
00231 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
00232 {
00233 insert(T);
00234 return *this;
00235 }
00236
00237 }
00238
00239