libpqxx  7.5.2
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-2021, 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 
79 class PQXX_LIBEXPORT stream_to : transaction_focus
80 {
81 public:
82  // TODO: Support WHERE clause?
83 
85 
106  transaction_base &tx, std::string_view path, std::string_view columns = "")
107  {
108  return stream_to{tx, path, columns};
109  }
110 
112 
121  static stream_to table(
122  transaction_base &tx, table_path path,
123  std::initializer_list<std::string_view> columns = {})
124  {
125  auto const &conn{tx.conn()};
126  return raw_table(tx, conn.quote_table(path), conn.quote_columns(columns));
127  }
128 
130 
139  PQXX_DEPRECATED("Use table() or raw_table() factory.")
140  stream_to(transaction_base &tx, std::string_view table_name) :
141  stream_to{tx, table_name, ""sv}
142  {}
143 
145 
147  template<typename Columns>
148  PQXX_DEPRECATED("Use table() or raw_table() factory.")
149  stream_to(
150  transaction_base &, std::string_view table_name, Columns const &columns);
151 
153 
155  template<typename Iter>
156  PQXX_DEPRECATED("Use table() or raw_table() factory.")
157  stream_to(
158  transaction_base &, std::string_view table_name, Iter columns_begin,
159  Iter columns_end);
160 
161  ~stream_to() noexcept;
162 
164  [[nodiscard]] operator bool() const noexcept { return not m_finished; }
166  [[nodiscard]] bool operator!() const noexcept { return m_finished; }
167 
169 
175  void complete();
176 
178 
187  template<typename Row> stream_to &operator<<(Row const &row)
188  {
189  write_row(row);
190  return *this;
191  }
192 
194 
199 
201 
207  template<typename Row> void write_row(Row const &row)
208  {
209  fill_buffer(row);
210  write_buffer();
211  }
212 
214 
217  template<typename... Ts> void write_values(Ts const &...fields)
218  {
219  fill_buffer(fields...);
220  write_buffer();
221  }
222 
223 private:
225  stream_to(
226  transaction_base &tx, std::string_view path, std::string_view columns);
227 
228  bool m_finished = false;
229 
231  std::string m_buffer;
232 
234  std::string m_field_buf;
235 
237  void write_raw_line(std::string_view);
238 
240 
242  void write_buffer();
243 
245  static constexpr std::string_view null_field{"\\N\t"};
246 
248  template<typename T>
249  static std::enable_if_t<nullness<T>::always_null, std::size_t>
250  estimate_buffer(T const &)
251  {
252  return std::size(null_field);
253  }
254 
256 
259  template<typename T>
260  static std::enable_if_t<not nullness<T>::always_null, std::size_t>
261  estimate_buffer(T const &field)
262  {
263  return is_null(field) ? std::size(null_field) : size_buffer(field);
264  }
265 
267  void escape_field_to_buffer(std::string_view);
268 
270 
276  template<typename Field>
277  std::enable_if_t<not nullness<Field>::always_null>
278  append_to_buffer(Field const &f)
279  {
280  // We append each field, terminated by a tab. That will leave us with
281  // one tab too many, assuming we write any fields at all; we remove that
282  // at the end.
283  if (is_null(f))
284  {
285  // Easy. Append null and tab in one go.
286  m_buffer.append(null_field);
287  }
288  else
289  {
290  // Convert f into m_buffer.
291 
292  using traits = string_traits<Field>;
293  auto const budget{estimate_buffer(f)};
294  auto const offset{std::size(m_buffer)};
295 
296  if constexpr (std::is_arithmetic_v<Field>)
297  {
298  // Specially optimised for "safe" types, which never need any
299  // escaping. Convert straight into m_buffer.
300 
301  // The budget we get from size_buffer() includes room for the trailing
302  // zero, which we must remove. But we're also inserting tabs between
303  // fields, so we re-purpose the extra byte for that.
304  auto const total{offset + budget};
305  m_buffer.resize(total);
306  char *const end{traits::into_buf(
307  m_buffer.data() + offset, m_buffer.data() + total, f)};
308  *(end - 1) = '\t';
309  // Shrink to fit. Keep the tab though.
310  m_buffer.resize(static_cast<std::size_t>(end - m_buffer.data()));
311  }
312  else
313  {
314  // TODO: Specialise string/string_view/zview to skip to_buf()!
315  // This field may need escaping. First convert the value into
316  // m_field_buffer, then escape into its final place.
317  m_field_buf.resize(budget);
318  escape_field_to_buffer(traits::to_buf(
319  m_field_buf.data(), m_field_buf.data() + std::size(m_field_buf), f));
320  }
321  }
322  }
323 
325 
331  template<typename Field>
332  std::enable_if_t<nullness<Field>::always_null>
333  append_to_buffer(Field const &)
334  {
335  m_buffer.append(null_field);
336  }
337 
339  template<typename Container>
340  std::enable_if_t<not std::is_same_v<typename Container::value_type, char>>
341  fill_buffer(Container const &c)
342  {
343  // To avoid unnecessary allocations and deallocations, we run through c
344  // twice: once to determine how much buffer space we may need, and once to
345  // actually write it into the buffer.
346  std::size_t budget{0};
347  for (auto const &f : c) budget += estimate_buffer(f);
348  m_buffer.reserve(budget);
349  for (auto const &f : c) append_to_buffer(f);
350  }
351 
353  template<typename Tuple, std::size_t... indexes>
354  static std::size_t
355  budget_tuple(Tuple const &t, std::index_sequence<indexes...>)
356  {
357  return (estimate_buffer(std::get<indexes>(t)) + ...);
358  }
359 
361  template<typename Tuple, std::size_t... indexes>
362  void append_tuple(Tuple const &t, std::index_sequence<indexes...>)
363  {
364  (append_to_buffer(std::get<indexes>(t)), ...);
365  }
366 
368  template<typename... Elts> void fill_buffer(std::tuple<Elts...> const &t)
369  {
370  using indexes = std::make_index_sequence<sizeof...(Elts)>;
371 
372  m_buffer.reserve(budget_tuple(t, indexes{}));
373  append_tuple(t, indexes{});
374  }
375 
377  template<typename... Ts> void fill_buffer(const Ts &...fields)
378  {
379  (..., append_to_buffer(fields));
380  }
381 
382  constexpr static std::string_view s_classname{"stream_to"};
383 };
384 
385 
386 template<typename Columns>
387 inline stream_to::stream_to(
388  transaction_base &tx, std::string_view table_name, Columns const &columns) :
389  stream_to{tx, table_name, std::begin(columns), std::end(columns)}
390 {}
391 
392 
393 template<typename Iter>
394 inline stream_to::stream_to(
395  transaction_base &tx, std::string_view table_name, Iter columns_begin,
396  Iter columns_end) :
397  stream_to{
398  tx,
399  tx.quote_name(
400  table_name,
401  separated_list(",", columns_begin, columns_end, [&tx](auto col) {
402  return tx.quote_name(*col);
403  }))}
404 {}
405 } // namespace pqxx
406 
407 #include "pqxx/internal/compiler-internal-post.hxx"
408 #endif
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:26
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:42
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:364
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition: connection.hxx:122
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:480
bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:353
Reference to one row in a result.
Definition: row.hxx:46
Stream data from the database.
Definition: stream_from.hxx:73
Efficiently write data directly to a database table.
Definition: stream_to.hxx:80
static stream_to raw_table(transaction_base &tx, std::string_view path, std::string_view columns="")
Stream data to a pre-quoted table and columns.
Definition: stream_to.hxx:105
stream_to & operator<<(Row const &row)
Insert a row of data.
Definition: stream_to.hxx:187
static stream_to table(transaction_base &tx, table_path path, std::initializer_list< std::string_view > columns={})
Create a stream_to writing to a named table and columns.
Definition: stream_to.hxx:121
void write_values(Ts const &...fields)
Insert values as a row.
Definition: stream_to.hxx:217
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:207
bool operator!() const noexcept
Has this stream been through its concluding complete()?
Definition: stream_to.hxx:166
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:75
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:495
Base class for things that monopolise a transaction's attention.
Definition: transaction_focus.hxx:28