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 "pqxx/tablestream"
00022
00023
00024
00025
00026 namespace pqxx
00027 {
00028 class tablereader;
00029
00031
00041 class PQXX_LIBEXPORT tablewriter : public tablestream
00042 {
00043 public:
00044 typedef unsigned size_type;
00045
00046 tablewriter(transaction_base &,
00047 const PGSTD::string &WName,
00048 const PGSTD::string &Null=PGSTD::string());
00049
00051
00053 template<typename ITER>
00054 tablewriter(transaction_base &,
00055 const PGSTD::string &WName,
00056 ITER begincolumns,
00057 ITER endcolumns,
00058 const PGSTD::string &Null=PGSTD::string());
00059
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 writer_close();
00105 PGSTD::string EscapeAny(const char t[]) const;
00106 PGSTD::string EscapeAny(const PGSTD::string &) const;
00107 template<typename T> PGSTD::string EscapeAny(const T &) const;
00108
00109 static PGSTD::string Escape(const PGSTD::string &);
00110 };
00111
00112 }
00113
00114
00115
00116 namespace PGSTD
00117 {
00119
00122 template<>
00123 class back_insert_iterator<pqxx::tablewriter> :
00124 public iterator<output_iterator_tag, void,void,void,void>
00125 {
00126 public:
00127 explicit back_insert_iterator(pqxx::tablewriter &W) throw () :
00128 m_Writer(&W) {}
00129
00130 back_insert_iterator &
00131 operator=(const back_insert_iterator &rhs) throw ()
00132 {
00133 m_Writer = rhs.m_Writer;
00134 return *this;
00135 }
00136
00137 template<typename TUPLE>
00138 back_insert_iterator &operator=(const TUPLE &T)
00139 {
00140 m_Writer->insert(T);
00141 return *this;
00142 }
00143
00144 back_insert_iterator &operator++() { return *this; }
00145 back_insert_iterator &operator++(int) { return *this; }
00146 back_insert_iterator &operator*() { return *this; }
00147
00148 private:
00149 pqxx::tablewriter *m_Writer;
00150 };
00151
00152 }
00153
00154
00155 namespace pqxx
00156 {
00157
00158 template<typename ITER> inline
00159 tablewriter::tablewriter(transaction_base &T,
00160 const PGSTD::string &WName,
00161 ITER begincolumns,
00162 ITER endcolumns,
00163 const PGSTD::string &Null) :
00164 tablestream(T, WName, Null, "tablewriter")
00165 {
00166 setup(T, WName, columnlist(begincolumns, endcolumns));
00167 }
00168
00169
00170 inline PGSTD::string tablewriter::EscapeAny(const PGSTD::string &t) const
00171 {
00172 return (t == NullStr()) ? "\\N" : Escape(t);
00173 }
00174
00175 inline PGSTD::string tablewriter::EscapeAny(const char t[]) const
00176 {
00177 return t ? EscapeAny(PGSTD::string(t)) : "\\N";
00178 }
00179
00180 template<typename T> inline PGSTD::string
00181 tablewriter::EscapeAny(const T &t) const
00182 {
00183 return EscapeAny(to_string(t));
00184 }
00185
00186
00187 template<typename IT>
00188 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
00189 {
00190 PGSTD::string Line;
00191 for (; Begin != End; ++Begin)
00192 {
00193 Line += EscapeAny(*Begin);
00194 Line += "\t";
00195 }
00196
00197
00198 if (!Line.empty()) Line.erase(Line.size()-1);
00199
00200 return Line;
00201 }
00202
00203
00204 template<typename TUPLE>
00205 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
00206 {
00207 return generate(T.begin(), T.end());
00208 }
00209
00210
00211 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
00212 {
00213 WriteRawLine(generate(Begin, End));
00214 }
00215
00216
00217 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
00218 {
00219 insert(T.begin(), T.end());
00220 }
00221
00222 template<typename IT>
00223 inline void tablewriter::push_back(IT Begin, IT End)
00224 {
00225 insert(Begin, End);
00226 }
00227
00228 template<typename TUPLE>
00229 inline void tablewriter::push_back(const TUPLE &T)
00230 {
00231 insert(T.begin(), T.end());
00232 }
00233
00234 template<typename TUPLE>
00235 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
00236 {
00237 insert(T);
00238 return *this;
00239 }
00240
00241 }
00242
00243