13#ifndef PQXX_H_STREAM_TO
14#define PQXX_H_STREAM_TO
16#if !defined(PQXX_HEADER_PRE)
17# error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
20#include "pqxx/separated_list.hxx"
21#include "pqxx/transaction_base.hxx"
107 return {tx, path, columns};
122 std::initializer_list<std::string_view> columns = {})
124 auto const &conn{tx.
conn()};
125 return raw_table(tx, conn.quote_table(path), conn.quote_columns(columns));
128#if defined(PQXX_HAVE_CONCEPTS)
137 template<PQXX_CHAR_STRINGS_ARG COLUMNS>
139 table(transaction_base &tx, table_path path, COLUMNS
const &columns)
141 auto const &conn{tx.conn()};
142 return stream_to::raw_table(
143 tx, conn.quote_table(path), tx.conn().quote_columns(columns));
154 template<PQXX_CHAR_STRINGS_ARG COLUMNS>
156 table(transaction_base &tx, std::string_view path, COLUMNS
const &columns)
158 return stream_to::raw_table(tx, path, tx.conn().quote_columns(columns));
172 [[deprecated(
"Use table() or raw_table() factory.")]]
stream_to(
180 template<
typename Columns>
181 [[deprecated(
"Use table() or raw_table() factory.")]]
stream_to(
187 template<
typename Iter>
188 [[deprecated(
"Use table() or raw_table() factory.")]]
stream_to(
195 [[nodiscard]] constexpr operator
bool() const noexcept
197 return not m_finished;
256 fill_buffer(fields...);
265 bool m_finished =
false;
268 std::string m_buffer;
271 std::string m_field_buf;
274 internal::glyph_scanner_func *m_scanner;
277 void write_raw_line(std::string_view);
285 static constexpr std::string_view null_field{
"\\N\t"};
289 static std::enable_if_t<nullness<T>::always_null, std::size_t>
290 estimate_buffer(T
const &)
292 return std::size(null_field);
300 static std::enable_if_t<not nullness<T>::always_null, std::size_t>
301 estimate_buffer(T
const &field)
307 void escape_field_to_buffer(std::string_view data);
316 template<
typename Field>
317 std::enable_if_t<not nullness<Field>::always_null>
318 append_to_buffer(Field
const &f)
326 m_buffer.append(null_field);
332 using traits = string_traits<Field>;
333 auto const budget{estimate_buffer(f)};
334 auto const offset{std::size(m_buffer)};
336 if constexpr (std::is_arithmetic_v<Field>)
344 auto const total{offset + budget};
345 m_buffer.resize(total);
346 auto const data{m_buffer.data()};
347 char *
const end{traits::into_buf(data + offset, data + total, f)};
350 m_buffer.resize(
static_cast<std::size_t
>(end - data));
353 std::is_same_v<Field, std::string> or
354 std::is_same_v<Field, std::string_view> or
355 std::is_same_v<Field, zview>)
358 m_field_buf.resize(budget);
359 escape_field_to_buffer(f);
365 m_field_buf.resize(budget);
366 auto const data{m_field_buf.data()};
367 escape_field_to_buffer(
368 traits::to_buf(data, data + std::size(m_field_buf), f));
380 template<
typename Field>
381 std::enable_if_t<nullness<Field>::always_null>
382 append_to_buffer(Field
const &)
384 m_buffer.append(null_field);
388 template<
typename Container>
389 std::enable_if_t<not std::is_same_v<typename Container::value_type, char>>
390 fill_buffer(Container
const &c)
395 std::size_t budget{0};
396 for (
auto const &f : c) budget += estimate_buffer(f);
397 m_buffer.reserve(budget);
398 for (
auto const &f : c) append_to_buffer(f);
402 template<
typename Tuple, std::size_t... indexes>
404 budget_tuple(Tuple
const &t, std::index_sequence<indexes...>)
406 return (estimate_buffer(std::get<indexes>(t)) + ...);
410 template<
typename Tuple, std::size_t... indexes>
411 void append_tuple(Tuple
const &t, std::index_sequence<indexes...>)
413 (append_to_buffer(std::get<indexes>(t)), ...);
417 template<
typename... Elts>
void fill_buffer(std::tuple<Elts...>
const &t)
419 using indexes = std::make_index_sequence<
sizeof...(Elts)>;
421 m_buffer.reserve(budget_tuple(t, indexes{}));
422 append_tuple(t, indexes{});
426 template<
typename... Ts>
void fill_buffer(
const Ts &...fields)
428 (..., append_to_buffer(fields));
431 constexpr static std::string_view s_classname{
"stream_to"};
435template<
typename Columns>
437 transaction_base &tx, std::string_view table_name, Columns
const &columns) :
438 stream_to{tx, table_name, std::begin(columns), std::end(columns)}
442template<
typename Iter>
The home of all libpqxx classes, functions, templates, etc.
Definition array.hxx:27
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:495
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:43
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:381
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition connection.hxx:123
constexpr bool is_null(TYPE const &value) noexcept
Is value null?
Definition strconv.hxx:370
Reference to one row in a result.
Definition row.hxx:47
Stream data from the database.
Definition stream_from.hxx:78
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:200
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:254
stream_to(transaction_base &tx, std::string_view table_name)
Create a stream, without specifying columns.
Definition stream_to.hxx:172
stream_to & operator<<(Row const &row)
Insert a row of data.
Definition stream_to.hxx:224
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:244
Interface definition (and common code) for "transaction" classes.
Definition transaction_base.hxx:77
constexpr connection & conn() const noexcept
The connection in which this transaction lives.
Definition transaction_base.hxx:769
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition transaction_base.hxx:220
Base class for things that monopolise a transaction's attention.
Definition transaction_focus.hxx:29