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