libpqxx  7.3.0
stream_to.hxx
1 /* Definition of the pqxx::stream_to class.
2  *
3  * pqxx::stream_to enables optimized batch updates to a database table.
4  *
5  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/stream_to.hxx instead.
6  *
7  * Copyright (c) 2000-2020, Jeroen T. Vermeulen.
8  *
9  * See COPYING for copyright license. If you did not receive a file called
10  * COPYING with this source code, please notify the distributor of this
11  * mistake, or contact the author.
12  */
13 #ifndef PQXX_H_STREAM_TO
14 #define PQXX_H_STREAM_TO
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
18 
19 #include "pqxx/separated_list.hxx"
20 #include "pqxx/transaction_base.hxx"
21 
22 
23 namespace pqxx
24 {
26 
69 class PQXX_LIBEXPORT stream_to : internal::transactionfocus
70 {
71 public:
73 
80  stream_to(transaction_base &, std::string_view table_name);
81 
83  template<typename Columns>
84  stream_to(
85  transaction_base &, std::string_view table_name, Columns const &columns);
86 
88  template<typename Iter>
89  stream_to(
90  transaction_base &, std::string_view table_name, Iter columns_begin,
91  Iter columns_end);
92 
93  ~stream_to() noexcept;
94 
95  [[nodiscard]] operator bool() const noexcept { return not m_finished; }
96  [[nodiscard]] bool operator!() const noexcept { return m_finished; }
97 
99 
105  void complete();
106 
108 
117  template<typename Row> stream_to &operator<<(Row const &row)
118  {
119  write_row(row);
120  return *this;
121  }
122 
124 
129 
131 
137  template<typename Row> void write_row(Row const &row)
138  {
139  fill_buffer(row);
140  write_buffer();
141  }
142 
144 
147  template<typename... Ts> void write_values(const Ts &...fields)
148  {
149  fill_buffer(fields...);
150  write_buffer();
151  }
152 
153 private:
154  bool m_finished = false;
155 
157  std::string m_buffer;
158 
160  std::string m_field_buf;
161 
163  void write_raw_line(std::string_view);
164 
166 
168  void write_buffer();
169 
171  static constexpr std::string_view null_field{"\\N\t"};
172 
174  template<typename T>
175  static std::enable_if_t<nullness<T>::always_null, std::size_t>
176  estimate_buffer(T const &)
177  {
178  return std::size(null_field);
179  }
180 
182 
185  template<typename T>
186  static std::enable_if_t<not nullness<T>::always_null, std::size_t>
187  estimate_buffer(T const &field)
188  {
189  return is_null(field) ? std::size(null_field) : size_buffer(field);
190  }
191 
193  void escape_field_to_buffer(std::string_view);
194 
196 
202  template<typename Field>
203  std::enable_if_t<not nullness<Field>::always_null>
204  append_to_buffer(Field const &f)
205  {
206  // We append each field, terminated by a tab. That will leave us with
207  // one tab too many, assuming we write any fields at all; we remove that
208  // at the end.
209  if (is_null(f))
210  {
211  // Easy. Append null and tab in one go.
212  m_buffer.append(null_field);
213  }
214  else
215  {
216  // Convert f into m_buffer.
217 
218  using traits = string_traits<Field>;
219  auto const budget{estimate_buffer(f)};
220  auto const offset{std::size(m_buffer)};
221 
222  if constexpr (std::is_arithmetic_v<Field>)
223  {
224  // Specially optimised for "safe" types, which never need any
225  // escaping. Convert straight into m_buffer.
226 
227  // The budget we get from size_buffer() includes room for the trailing
228  // zero, which we must remove. But we're also inserting tabs between
229  // fields, so we re-purpose the extra byte for that.
230  auto const total{offset + budget};
231  m_buffer.resize(total);
232  char *const end{traits::into_buf(
233  m_buffer.data() + offset, m_buffer.data() + total, f)};
234  *(end - 1) = '\t';
235  // Shrink to fit. Keep the tab though.
236  m_buffer.resize(static_cast<std::size_t>(end - m_buffer.data()));
237  }
238  else
239  {
240  // TODO: Specialise string/string_view/zview to skip to_buf()!
241  // This field may need escaping. First convert the value into
242  // m_field_buffer, then escape into its final place.
243  m_field_buf.resize(budget);
244  escape_field_to_buffer(traits::to_buf(
245  m_field_buf.data(), m_field_buf.data() + std::size(m_field_buf), f));
246  }
247  }
248  }
249 
251 
257  template<typename Field>
258  std::enable_if_t<nullness<Field>::always_null>
259  append_to_buffer(Field const &)
260  {
261  m_buffer.append(null_field);
262  }
263 
265  template<typename Container> void fill_buffer(Container const &c)
266  {
267  // To avoid unnecessary allocations and deallocations, we run through c
268  // twice: once to determine how much buffer space we may need, and once to
269  // actually write it into the buffer.
270  std::size_t budget{0};
271  for (auto const &f : c) budget += estimate_buffer(f);
272  m_buffer.reserve(budget);
273  for (auto const &f : c) append_to_buffer(f);
274  }
275 
277  template<typename Tuple, std::size_t... indexes>
278  static std::size_t
279  budget_tuple(Tuple const &t, std::index_sequence<indexes...>)
280  {
281  return (estimate_buffer(std::get<indexes>(t)) + ...);
282  }
283 
285  template<typename Tuple, std::size_t... indexes>
286  void append_tuple(Tuple const &t, std::index_sequence<indexes...>)
287  {
288  (append_to_buffer(std::get<indexes>(t)), ...);
289  }
290 
292  template<typename... Elts> void fill_buffer(std::tuple<Elts...> const &t)
293  {
294  using indexes = std::make_index_sequence<sizeof...(Elts)>;
295 
296  m_buffer.reserve(budget_tuple(t, indexes{}));
297  append_tuple(t, indexes{});
298  }
299 
300  void set_up(transaction_base &, std::string_view table_name);
301  void set_up(
302  transaction_base &, std::string_view table_name, std::string_view columns);
303 
305  template<typename... Ts> void fill_buffer(const Ts &...fields)
306  {
307  (..., append_to_buffer(fields));
308  }
309 
310  constexpr static std::string_view s_classname{"stream_to"};
311 };
312 
313 
314 template<typename Columns>
316  transaction_base &tb, std::string_view table_name, Columns const &columns) :
317  stream_to{tb, table_name, std::begin(columns), std::end(columns)}
318 {}
319 
320 
321 template<typename Iter>
323  transaction_base &tb, std::string_view table_name, Iter columns_begin,
324  Iter columns_end) :
325  internal::transactionfocus{tb, s_classname, table_name}
326 {
327  set_up(tb, table_name, separated_list(",", columns_begin, columns_end));
328 }
329 } // namespace pqxx
330 
331 #include "pqxx/internal/compiler-internal-post.hxx"
332 #endif
pqxx::stream_to::write_row
void write_row(Row const &row)
Insert a row of data, given in the form of a std::tuple or container.
Definition: stream_to.hxx:137
pqxx::stream_to::write_values
void write_values(const Ts &...fields)
Insert values as a row.
Definition: stream_to.hxx:147
pqxx
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
pqxx::is_null
bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:331
pqxx::stream_from::get_raw_line
raw_line get_raw_line()
Read a raw line of text from the COPY command.
Definition: stream_from.cxx:80
pqxx::stream_to::operator<<
stream_to & operator<<(Row const &row)
Insert a row of data.
Definition: stream_to.hxx:117
pqxx::stream_to
Efficiently write data directly to a database table.
Definition: stream_to.hxx:70
pqxx::row
Reference to one row in a result.
Definition: row.hxx:46
pqxx::stream_to::operator!
bool operator!() const noexcept
Definition: stream_to.hxx:96
pqxx::stream_to::stream_to
stream_to(transaction_base &, std::string_view table_name)
Create a stream, without specifying columns.
Definition: stream_to.cxx:36
pqxx::transaction_base
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:72
pqxx::transaction_base::quote_name
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:178
pqxx::transaction_base::exec0
result exec0(zview query, std::string_view desc=std::string_view{})
Execute query, which should zero rows of data.
Definition: transaction_base.hxx:221
pqxx::size_buffer
std::size_t size_buffer(TYPE const &...value) noexcept
Estimate how much buffer space is needed to represent values as a string.
Definition: strconv.hxx:342
pqxx::separated_list
std::string separated_list(std::string_view sep, ITER begin, ITER end, ACCESS access)
Represent sequence of values as a string, joined by a given separator.
Definition: separated_list.hxx:40
pqxx::operator<<
std::basic_ostream< CHAR > & operator<<(std::basic_ostream< CHAR > &s, field const &value)
Write a result field to any type of stream.
Definition: field.hxx:476
pqxx::stream_to::complete
void complete()
Complete the operation, and check for errors.
Definition: stream_to.cxx:103
pqxx::stream_to::~stream_to
~stream_to() noexcept
Definition: stream_to.cxx:43
pqxx::stream_from
Stream data from the database.
Definition: stream_from.hxx:62