00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "pqxx/compiler-public.hxx"
00020 #include "pqxx/compiler-internal-pre.hxx"
00021
00022 #include "pqxx/tablestream"
00023
00024
00025
00026
00027 namespace pqxx
00028 {
00029 class tablereader;
00030
00032
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> tablewriter(transaction_base &,
00055 const PGSTD::string &WName,
00056 ITER begincolumns,
00057 ITER endcolumns);
00058
00059 template<typename ITER> tablewriter(transaction_base &,
00060 const PGSTD::string &WName,
00061 ITER begincolumns,
00062 ITER endcolumns,
00063 const PGSTD::string &Null);
00064
00065 ~tablewriter() throw ();
00066
00067 template<typename IT> void insert(IT Begin, IT End);
00068 template<typename TUPLE> void insert(const TUPLE &);
00069 template<typename IT> void push_back(IT Begin, IT End);
00070 template<typename TUPLE> void push_back(const TUPLE &);
00071
00072 void reserve(size_type) {}
00073
00074 template<typename TUPLE> tablewriter &operator<<(const TUPLE &);
00075
00077 tablewriter &operator<<(tablereader &);
00078
00080
00082 template<typename IT> PGSTD::string generate(IT Begin, IT End) const;
00083 template<typename TUPLE> PGSTD::string generate(const TUPLE &) const;
00084
00086
00093 virtual void complete();
00094
00095 #ifdef PQXX_DEPRECATED_HEADERS
00097 template<typename IT> PGSTD::string ezinekoT(IT Begin, IT End) const
00098 PQXX_DEPRECATED { return generate(Begin, End); }
00100 template<typename TUPLE> PGSTD::string ezinekoT(const TUPLE &T) const
00101 PQXX_DEPRECATED { return generate(T); }
00102 #endif
00103
00104 private:
00105 void setup(transaction_base &,
00106 const PGSTD::string &WName,
00107 const PGSTD::string &Columns = PGSTD::string());
00108
00109 void WriteRawLine(const PGSTD::string &);
00110 void PQXX_PRIVATE writer_close();
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 tablewriter::tablewriter(transaction_base &T,
00160 const PGSTD::string &WName,
00161 ITER begincolumns,
00162 ITER endcolumns) :
00163 namedclass("tablewriter", WName),
00164 tablestream(T, PGSTD::string())
00165 {
00166 setup(T, WName, columnlist(begincolumns, endcolumns));
00167 }
00168
00169 template<typename ITER> inline tablewriter::tablewriter(transaction_base &T,
00170 const PGSTD::string &WName,
00171 ITER begincolumns,
00172 ITER endcolumns,
00173 const PGSTD::string &Null) :
00174 namedclass("tablewriter", WName),
00175 tablestream(T, Null)
00176 {
00177 setup(T, WName, columnlist(begincolumns, endcolumns));
00178 }
00179
00180
00181 namespace internal
00182 {
00183 PGSTD::string PQXX_LIBEXPORT Escape(const PGSTD::string &s,
00184 const PGSTD::string &null);
00185
00186 template<typename STR> inline PGSTD::string EscapeAny(const PGSTD::string &s,
00187 const PGSTD::string &null) { return Escape(s,null); }
00188 template<typename STR> inline PGSTD::string EscapeAny(const char s[],
00189 const PGSTD::string &null) {return s ? Escape(PGSTD::string(s),null):"\\N";}
00190 template<typename T> inline PGSTD::string EscapeAny(const T &t,
00191 const PGSTD::string &null) { return Escape(to_string(t), null); }
00192
00193 template<typename IT> class Escaper
00194 {
00195 const PGSTD::string m_null;
00196 public:
00197 explicit Escaper(const PGSTD::string &null) : m_null(null) {}
00198 PGSTD::string operator()(IT i) const { return EscapeAny(*i, m_null); }
00199 };
00200
00201 }
00202
00203 template<typename IT>
00204 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
00205 {
00206 return separated_list("\t", Begin, End, internal::Escaper<IT>(NullStr()));
00207 }
00208
00209
00210 template<typename TUPLE>
00211 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
00212 {
00213 return generate(T.begin(), T.end());
00214 }
00215
00216
00217 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
00218 {
00219 WriteRawLine(generate(Begin, End));
00220 }
00221
00222
00223 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
00224 {
00225 insert(T.begin(), T.end());
00226 }
00227
00228 template<typename IT>
00229 inline void tablewriter::push_back(IT Begin, IT End)
00230 {
00231 insert(Begin, End);
00232 }
00233
00234 template<typename TUPLE>
00235 inline void tablewriter::push_back(const TUPLE &T)
00236 {
00237 insert(T.begin(), T.end());
00238 }
00239
00240 template<typename TUPLE>
00241 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
00242 {
00243 insert(T);
00244 return *this;
00245 }
00246
00247 }
00248
00249
00250 #include "pqxx/compiler-internal-post.hxx"