00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef PQXX_H_TABLEWRITER
00020 #define PQXX_H_TABLEWRITER
00021
00022 #include "pqxx/compiler-public.hxx"
00023 #include "pqxx/compiler-internal-pre.hxx"
00024
00025 #include "pqxx/tablestream"
00026
00027
00028
00029
00030 namespace pqxx
00031 {
00032 class tablereader;
00033
00035
00045 class PQXX_LIBEXPORT tablewriter : public tablestream
00046 {
00047 public:
00048 typedef unsigned size_type;
00049
00050 tablewriter(transaction_base &,
00051 const PGSTD::string &WName,
00052 const PGSTD::string &Null=PGSTD::string());
00053
00055
00061 template<typename ITER> tablewriter(transaction_base &,
00062 const PGSTD::string &WName,
00063 ITER begincolumns,
00064 ITER endcolumns);
00065
00067
00079 template<typename ITER> tablewriter(transaction_base &T,
00080 const PGSTD::string &WName,
00081 ITER begincolumns,
00082 ITER endcolumns,
00083 const PGSTD::string &Null);
00084
00085 ~tablewriter() throw ();
00086
00087 template<typename IT> void insert(IT Begin, IT End);
00088 template<typename TUPLE> void insert(const TUPLE &);
00089 template<typename IT> void push_back(IT Begin, IT End);
00090 template<typename TUPLE> void push_back(const TUPLE &);
00091
00092 void reserve(size_type) {}
00093
00094 template<typename TUPLE> tablewriter &operator<<(const TUPLE &);
00095
00097 tablewriter &operator<<(tablereader &);
00098
00100
00102 template<typename IT> PGSTD::string generate(IT Begin, IT End) const;
00103 template<typename TUPLE> PGSTD::string generate(const TUPLE &) const;
00104
00106
00113 virtual void complete();
00114
00116 void write_raw_line(const PGSTD::string &);
00117
00118 private:
00119 void setup(transaction_base &,
00120 const PGSTD::string &WName,
00121 const PGSTD::string &Columns = PGSTD::string());
00122
00123 void PQXX_PRIVATE writer_close();
00124 };
00125
00126 }
00127
00128
00129
00130 namespace PGSTD
00131 {
00133
00136 template<>
00137 class back_insert_iterator<pqxx::tablewriter> :
00138 public iterator<output_iterator_tag, void,void,void,void>
00139 {
00140 public:
00141 explicit back_insert_iterator(pqxx::tablewriter &W) throw () :
00142 m_Writer(&W) {}
00143
00144 back_insert_iterator &
00145 operator=(const back_insert_iterator &rhs) throw ()
00146 {
00147 m_Writer = rhs.m_Writer;
00148 return *this;
00149 }
00150
00151 template<typename TUPLE>
00152 back_insert_iterator &operator=(const TUPLE &T)
00153 {
00154 m_Writer->insert(T);
00155 return *this;
00156 }
00157
00158 back_insert_iterator &operator++() { return *this; }
00159 back_insert_iterator &operator++(int) { return *this; }
00160 back_insert_iterator &operator*() { return *this; }
00161
00162 private:
00163 pqxx::tablewriter *m_Writer;
00164 };
00165
00166 }
00167
00168
00169 namespace pqxx
00170 {
00171
00172 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
00173 const PGSTD::string &WName,
00174 ITER begincolumns,
00175 ITER endcolumns) :
00176 namedclass("tablewriter", WName),
00177 tablestream(T, PGSTD::string())
00178 {
00179 setup(T, WName, columnlist(begincolumns, endcolumns));
00180 }
00181
00182 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
00183 const PGSTD::string &WName,
00184 ITER begincolumns,
00185 ITER endcolumns,
00186 const PGSTD::string &Null) :
00187 namedclass("tablewriter", WName),
00188 tablestream(T, Null)
00189 {
00190 setup(T, WName, columnlist(begincolumns, endcolumns));
00191 }
00192
00193
00194 namespace internal
00195 {
00196 PGSTD::string PQXX_LIBEXPORT Escape(const PGSTD::string &s,
00197 const PGSTD::string &null);
00198
00199 template<typename STR> inline PGSTD::string EscapeAny(const PGSTD::string &s,
00200 const PGSTD::string &null) { return Escape(s,null); }
00201 template<typename STR> inline PGSTD::string EscapeAny(const char s[],
00202 const PGSTD::string &null) {return s ? Escape(PGSTD::string(s),null):"\\N";}
00203 template<typename T> inline PGSTD::string EscapeAny(const T &t,
00204 const PGSTD::string &null) { return Escape(to_string(t), null); }
00205
00206 template<typename IT> class Escaper
00207 {
00208 const PGSTD::string &m_null;
00209 public:
00210 explicit Escaper(const PGSTD::string &null) : m_null(null) {}
00211 PGSTD::string operator()(IT i) const { return EscapeAny(*i, m_null); }
00212 };
00213
00214 }
00215
00216 template<typename IT>
00217 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
00218 {
00219 return separated_list("\t", Begin, End, internal::Escaper<IT>(NullStr()));
00220 }
00221
00222
00223 template<typename TUPLE>
00224 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
00225 {
00226 return generate(T.begin(), T.end());
00227 }
00228
00229
00230 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
00231 {
00232 write_raw_line(generate(Begin, End));
00233 }
00234
00235
00236 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
00237 {
00238 insert(T.begin(), T.end());
00239 }
00240
00241 template<typename IT>
00242 inline void tablewriter::push_back(IT Begin, IT End)
00243 {
00244 insert(Begin, End);
00245 }
00246
00247 template<typename TUPLE>
00248 inline void tablewriter::push_back(const TUPLE &T)
00249 {
00250 insert(T.begin(), T.end());
00251 }
00252
00253 template<typename TUPLE>
00254 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
00255 {
00256 insert(T);
00257 return *this;
00258 }
00259
00260 }
00261
00262
00263 #include "pqxx/compiler-internal-post.hxx"
00264
00265 #endif
00266