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
00104 void WriteRawLine(const PGSTD::string &);
00105 void PQXX_PRIVATE writer_close();
00106 };
00107
00108 }
00109
00110
00111
00112 namespace PGSTD
00113 {
00115
00118 template<>
00119 class back_insert_iterator<pqxx::tablewriter> :
00120 public iterator<output_iterator_tag, void,void,void,void>
00121 {
00122 public:
00123 explicit back_insert_iterator(pqxx::tablewriter &W) throw () :
00124 m_Writer(&W) {}
00125
00126 back_insert_iterator &
00127 operator=(const back_insert_iterator &rhs) throw ()
00128 {
00129 m_Writer = rhs.m_Writer;
00130 return *this;
00131 }
00132
00133 template<typename TUPLE>
00134 back_insert_iterator &operator=(const TUPLE &T)
00135 {
00136 m_Writer->insert(T);
00137 return *this;
00138 }
00139
00140 back_insert_iterator &operator++() { return *this; }
00141 back_insert_iterator &operator++(int) { return *this; }
00142 back_insert_iterator &operator*() { return *this; }
00143
00144 private:
00145 pqxx::tablewriter *m_Writer;
00146 };
00147
00148 }
00149
00150
00151 namespace pqxx
00152 {
00153
00154 template<typename ITER> inline
00155 tablewriter::tablewriter(transaction_base &T,
00156 const PGSTD::string &WName,
00157 ITER begincolumns,
00158 ITER endcolumns,
00159 const PGSTD::string &Null) :
00160 tablestream(T, WName, Null, "tablewriter")
00161 {
00162 setup(T, WName, columnlist(begincolumns, endcolumns));
00163 }
00164
00165
00166 namespace internal
00167 {
00168 PGSTD::string PQXX_LIBEXPORT Escape(const PGSTD::string &s,
00169 const PGSTD::string &null);
00170
00171 template<typename STR> inline PGSTD::string EscapeAny(const PGSTD::string &s,
00172 const PGSTD::string &null) { return Escape(s,null); }
00173 template<typename STR> inline PGSTD::string EscapeAny(const char s[],
00174 const PGSTD::string &null) {return s ? Escape(PGSTD::string(s),null):"\\N";}
00175 template<typename T> inline PGSTD::string EscapeAny(const T &t,
00176 const PGSTD::string &null) { return Escape(to_string(t), null); }
00177
00178 template<typename IT> class Escaper
00179 {
00180 const PGSTD::string m_null;
00181 public:
00182 explicit Escaper(const PGSTD::string &null) : m_null(null) {}
00183 PGSTD::string operator()(IT i) const { return EscapeAny(*i, m_null); }
00184 };
00185
00186 }
00187
00188 template<typename IT>
00189 inline PGSTD::string tablewriter::generate(IT Begin, IT End) const
00190 {
00191 return separated_list("\t", Begin, End, internal::Escaper<IT>(NullStr()));
00192 }
00193
00194
00195 template<typename TUPLE>
00196 inline PGSTD::string tablewriter::generate(const TUPLE &T) const
00197 {
00198 return generate(T.begin(), T.end());
00199 }
00200
00201
00202 template<typename IT> inline void tablewriter::insert(IT Begin, IT End)
00203 {
00204 WriteRawLine(generate(Begin, End));
00205 }
00206
00207
00208 template<typename TUPLE> inline void tablewriter::insert(const TUPLE &T)
00209 {
00210 insert(T.begin(), T.end());
00211 }
00212
00213 template<typename IT>
00214 inline void tablewriter::push_back(IT Begin, IT End)
00215 {
00216 insert(Begin, End);
00217 }
00218
00219 template<typename TUPLE>
00220 inline void tablewriter::push_back(const TUPLE &T)
00221 {
00222 insert(T.begin(), T.end());
00223 }
00224
00225 template<typename TUPLE>
00226 inline tablewriter &tablewriter::operator<<(const TUPLE &T)
00227 {
00228 insert(T);
00229 return *this;
00230 }
00231
00232 }
00233
00234