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