libpqxx
stream_to.hxx
1 
13 #ifndef PQXX_H_STREAM_TO
14 #define PQXX_H_STREAM_TO
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/compiler-internal-pre.hxx"
18 #include "pqxx/transaction_base.hxx"
19 #include "pqxx/stream_base.hxx"
20 #include "pqxx/stream_from.hxx"
21 #include "pqxx/internal/type_utils.hxx"
22 
23 #include <string>
24 
25 
26 namespace pqxx
27 {
28 
30 
59 class PQXX_LIBEXPORT stream_to : public stream_base
60 {
61 public:
63 
70  stream_to(transaction_base &, const std::string &table_name);
71 
73  template<typename Columns> stream_to(
75  const std::string &table_name,
76  const Columns& columns
77  );
78 
80  template<typename Iter> stream_to(
82  const std::string &table_name,
83  Iter columns_begin,
84  Iter columns_end
85  );
86 
87  ~stream_to() noexcept;
88 
90 
96  void complete() override;
97 
99 
106  template<typename Tuple> stream_to & operator<<(const Tuple &);
107 
109 
114 
115 private:
117  void write_raw_line(const std::string &);
118 
119  void set_up(transaction_base &, const std::string &table_name);
120  void set_up(
122  const std::string &table_name,
123  const std::string &columns
124  );
125 
126  void close() override;
127 };
128 
129 
130 template<typename Columns> inline stream_to::stream_to(
131  transaction_base &tb,
132  const std::string &table_name,
133  const Columns& columns
134 ) : stream_to{
135  tb,
136  table_name,
137  std::begin(columns),
138  std::end(columns)
139 }
140 {}
141 
142 
143 template<typename Iter> inline stream_to::stream_to(
144  transaction_base &tb,
145  const std::string &table_name,
146  Iter columns_begin,
147  Iter columns_end
148 ) :
149  namedclass{"stream_from", table_name},
150  stream_base{tb}
151 {
152  set_up(
153  tb,
154  table_name,
155  columnlist(columns_begin, columns_end)
156  );
157 }
158 
159 
160 namespace internal
161 {
162 
163 class PQXX_LIBEXPORT TypedCopyEscaper
164 {
165  static std::string escape(const std::string &);
166 public:
167  template<typename T> std::string operator()(const T* t) const
168  {
169  return string_traits<T>::is_null(*t) ? "\\N" : escape(to_string(*t));
170  }
171 };
172 
173 // Explicit specialization so we don't need a string_traits<> for nullptr_t
174 template<> inline std::string TypedCopyEscaper::operator()<std::nullptr_t>(
175  const std::nullptr_t*
176 ) const
177 { return "\\N"; }
178 
179 } // namespace pqxx::internal
180 
181 
182 template<typename Tuple> stream_to & stream_to::operator<<(const Tuple &t)
183 {
184  write_raw_line(separated_list("\t", t, internal::TypedCopyEscaper()));
185  return *this;
186 }
187 
188 } // namespace pqxx
189 
190 
191 #include "pqxx/compiler-internal-post.hxx"
192 #endif
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:136
stream_to & operator<<(const Tuple &)
Insert a row of data.
Definition: stream_to.hxx:182
std::basic_ostream< CHAR > & operator<<(std::basic_ostream< CHAR > &S, const field &F)
Write a result field to any type of stream.
Definition: field.hxx:355
namedclass(const std::string &Classname)
Definition: util.hxx:248
Definition: stream_to.hxx:163
std::string operator()(const T *t) const
Definition: stream_to.hxx:167
Efficiently write data directly to a database table.
Definition: stream_to.hxx:59
std::string to_string(const field &Obj)
Convert a field to a string.
Definition: result.cxx:448
stream_to(transaction_base &, const std::string &table_name)
Create a stream, without specifying columns.
Definition: stream_to.cxx:18
Efficiently pull data directly out of a table.
Definition: stream_from.hxx:29
Traits class for use in string conversions.
Definition: strconv.hxx:51
Definition: stream_base.hxx:27
static std::string columnlist(const C &)
Definition: stream_base.hxx:47
std::string escape(const std::string &s, const std::string &null)
Definition: tablewriter.cxx:131
std::string separated_list(const std::string &sep, ITER begin, ITER end, ACCESS access)
Represent sequence of values as a string, joined by a given separator.
Definition: util.hxx:95
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25