libpqxx 7.8.1
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-2023, 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#if !defined(PQXX_HEADER_PRE)
17# error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
18#endif
19
20#include "pqxx/separated_list.hxx"
21#include "pqxx/transaction_base.hxx"
22
23
24namespace pqxx
25{
27
80class PQXX_LIBEXPORT stream_to : transaction_focus
81{
82public:
84
105 transaction_base &tx, std::string_view path, std::string_view columns = "")
106 {
107 return {tx, path, columns};
108 }
109
111
122 std::initializer_list<std::string_view> columns = {})
123 {
124 auto const &conn{tx.conn()};
125 return raw_table(tx, conn.quote_table(path), conn.quote_columns(columns));
126 }
127
128#if defined(PQXX_HAVE_CONCEPTS)
130
137 template<PQXX_CHAR_STRINGS_ARG COLUMNS>
138 static stream_to
139 table(transaction_base &tx, table_path path, COLUMNS const &columns)
140 {
141 auto const &conn{tx.conn()};
143 tx, conn.quote_table(path), tx.conn().quote_columns(columns));
144 }
145
147
154 template<PQXX_CHAR_STRINGS_ARG COLUMNS>
155 static stream_to
156 table(transaction_base &tx, std::string_view path, COLUMNS const &columns)
157 {
158 return stream_to::raw_table(tx, path, tx.conn().quote_columns(columns));
159 }
160#endif // PQXX_HAVE_CONCEPTS
161
163
172 [[deprecated("Use table() or raw_table() factory.")]] stream_to(
173 transaction_base &tx, std::string_view table_name) :
174 stream_to{tx, table_name, ""sv}
175 {}
176
178
180 template<typename Columns>
181 [[deprecated("Use table() or raw_table() factory.")]] stream_to(
182 transaction_base &, std::string_view table_name, Columns const &columns);
183
185
187 template<typename Iter>
188 [[deprecated("Use table() or raw_table() factory.")]] stream_to(
189 transaction_base &, std::string_view table_name, Iter columns_begin,
190 Iter columns_end);
191
192 explicit stream_to(stream_to &&other) :
193 // (This first step only moves the transaction_focus base-class
194 // object.)
195 transaction_focus{std::move(other)},
196 m_finished{other.m_finished},
197 m_buffer{std::move(other.m_buffer)},
198 m_field_buf{std::move(other.m_field_buf)},
199 m_finder{other.m_finder}
200 {
201 other.m_finished = true;
202 }
203 ~stream_to() noexcept;
204
206 [[nodiscard]] constexpr operator bool() const noexcept
207 {
208 return not m_finished;
209 }
211 [[nodiscard]] constexpr bool operator!() const noexcept
212 {
213 return m_finished;
214 }
215
217
223 void complete();
224
226
235 template<typename Row> stream_to &operator<<(Row const &row)
236 {
237 write_row(row);
238 return *this;
239 }
240
242
247
249
255 template<typename Row> void write_row(Row const &row)
256 {
257 fill_buffer(row);
258 write_buffer();
259 }
260
262
265 template<typename... Ts> void write_values(Ts const &...fields)
266 {
267 fill_buffer(fields...);
268 write_buffer();
269 }
270
271private:
273 stream_to(
274 transaction_base &tx, std::string_view path, std::string_view columns);
275
276 bool m_finished = false;
277
279 std::string m_buffer;
280
282 std::string m_field_buf;
283
285 internal::char_finder_func *m_finder;
286
288 void write_raw_line(std::string_view);
289
291
293 void write_buffer();
294
296 static constexpr std::string_view null_field{"\\N\t"};
297
299 template<typename T>
300 static std::enable_if_t<nullness<T>::always_null, std::size_t>
301 estimate_buffer(T const &)
302 {
303 return std::size(null_field);
304 }
305
307
310 template<typename T>
311 static std::enable_if_t<not nullness<T>::always_null, std::size_t>
312 estimate_buffer(T const &field)
313 {
314 return is_null(field) ? std::size(null_field) : size_buffer(field);
315 }
316
318 void escape_field_to_buffer(std::string_view data);
319
321
327 template<typename Field>
328 std::enable_if_t<not nullness<Field>::always_null>
329 append_to_buffer(Field const &f)
330 {
331 // We append each field, terminated by a tab. That will leave us with
332 // one tab too many, assuming we write any fields at all; we remove that
333 // at the end.
334 if (is_null(f))
335 {
336 // Easy. Append null and tab in one go.
337 m_buffer.append(null_field);
338 }
339 else
340 {
341 // Convert f into m_buffer.
342
343 using traits = string_traits<Field>;
344 auto const budget{estimate_buffer(f)};
345 auto const offset{std::size(m_buffer)};
346
347 if constexpr (std::is_arithmetic_v<Field>)
348 {
349 // Specially optimised for "safe" types, which never need any
350 // escaping. Convert straight into m_buffer.
351
352 // The budget we get from size_buffer() includes room for the trailing
353 // zero, which we must remove. But we're also inserting tabs between
354 // fields, so we re-purpose the extra byte for that.
355 auto const total{offset + budget};
356 m_buffer.resize(total);
357 auto const data{m_buffer.data()};
358 char *const end{traits::into_buf(data + offset, data + total, f)};
359 *(end - 1) = '\t';
360 // Shrink to fit. Keep the tab though.
361 m_buffer.resize(static_cast<std::size_t>(end - data));
362 }
363 else if constexpr (
364 std::is_same_v<Field, std::string> or
365 std::is_same_v<Field, std::string_view> or
366 std::is_same_v<Field, zview>)
367 {
368 // This string may need escaping.
369 m_field_buf.resize(budget);
370 escape_field_to_buffer(f);
371 }
372 else if constexpr (
373 std::is_same_v<Field, std::optional<std::string>> or
374 std::is_same_v<Field, std::optional<std::string_view>> or
375 std::is_same_v<Field, std::optional<zview>>)
376 {
377 // Optional string. It's not null (we checked for that above), so...
378 // Treat like a string.
379 m_field_buf.resize(budget);
380 escape_field_to_buffer(f.value());
381 }
382 // TODO: Support deleter template argument on unique_ptr.
383 else if constexpr (
384 std::is_same_v<Field, std::unique_ptr<std::string>> or
385 std::is_same_v<Field, std::unique_ptr<std::string_view>> or
386 std::is_same_v<Field, std::unique_ptr<zview>> or
387 std::is_same_v<Field, std::shared_ptr<std::string>> or
388 std::is_same_v<Field, std::shared_ptr<std::string_view>> or
389 std::is_same_v<Field, std::shared_ptr<zview>>)
390 {
391 // TODO: Can we generalise this elegantly without Concepts?
392 // Effectively also an optional string. It's not null (we checked
393 // for that above).
394 m_field_buf.resize(budget);
395 escape_field_to_buffer(*f);
396 }
397 else
398 {
399 // This field needs to be converted to a string, and after that,
400 // escaped as well.
401 m_field_buf.resize(budget);
402 auto const data{m_field_buf.data()};
403 escape_field_to_buffer(
404 traits::to_buf(data, data + std::size(m_field_buf), f));
405 }
406 }
407 }
408
410
416 template<typename Field>
417 std::enable_if_t<nullness<Field>::always_null>
418 append_to_buffer(Field const &)
419 {
420 m_buffer.append(null_field);
421 }
422
424 template<typename Container>
425 std::enable_if_t<not std::is_same_v<typename Container::value_type, char>>
426 fill_buffer(Container const &c)
427 {
428 // To avoid unnecessary allocations and deallocations, we run through c
429 // twice: once to determine how much buffer space we may need, and once to
430 // actually write it into the buffer.
431 std::size_t budget{0};
432 for (auto const &f : c) budget += estimate_buffer(f);
433 m_buffer.reserve(budget);
434 for (auto const &f : c) append_to_buffer(f);
435 }
436
438 template<typename Tuple, std::size_t... indexes>
439 static std::size_t
440 budget_tuple(Tuple const &t, std::index_sequence<indexes...>)
441 {
442 return (estimate_buffer(std::get<indexes>(t)) + ...);
443 }
444
446 template<typename Tuple, std::size_t... indexes>
447 void append_tuple(Tuple const &t, std::index_sequence<indexes...>)
448 {
449 (append_to_buffer(std::get<indexes>(t)), ...);
450 }
451
453 template<typename... Elts> void fill_buffer(std::tuple<Elts...> const &t)
454 {
455 using indexes = std::make_index_sequence<sizeof...(Elts)>;
456
457 m_buffer.reserve(budget_tuple(t, indexes{}));
458 append_tuple(t, indexes{});
459 }
460
462 template<typename... Ts> void fill_buffer(const Ts &...fields)
463 {
464 (..., append_to_buffer(fields));
465 }
466
467 constexpr static std::string_view s_classname{"stream_to"};
468};
469
470
471template<typename Columns>
473 transaction_base &tx, std::string_view table_name, Columns const &columns) :
474 stream_to{tx, table_name, std::begin(columns), std::end(columns)}
475{}
476
477
478template<typename Iter>
480 transaction_base &tx, std::string_view table_name, Iter columns_begin,
481 Iter columns_end) :
482 stream_to{
483 tx,
484 tx.quote_name(
485 table_name,
486 separated_list(",", columns_begin, columns_end, [&tx](auto col) {
487 return tx.quote_name(*col);
488 }))}
489{}
490} // namespace pqxx
491#endif
The home of all libpqxx classes, functions, templates, etc.
Definition array.hxx:33
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:44
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:509
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition connection.hxx:188
Reference to one row in a result.
Definition row.hxx:47
Stream data from the database.
Definition stream_from.hxx:79
Efficiently write data directly to a database table.
Definition stream_to.hxx:81
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:104
constexpr bool operator!() const noexcept
Has this stream been through its concluding complete()?
Definition stream_to.hxx:211
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:120
void write_values(Ts const &...fields)
Insert values as a row.
Definition stream_to.hxx:265
stream_to(transaction_base &tx, std::string_view table_name)
Create a stream, without specifying columns.
Definition stream_to.hxx:172
stream_to(stream_to &&other)
Definition stream_to.hxx:192
stream_to & operator<<(Row const &row)
Insert a row of data.
Definition stream_to.hxx:235
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:255
Interface definition (and common code) for "transaction" classes.
Definition transaction_base.hxx:88
constexpr connection & conn() const noexcept
The connection in which this transaction lives.
Definition transaction_base.hxx:780
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition transaction_base.hxx:231
Base class for things that monopolise a transaction's attention.
Definition transaction_focus.hxx:29