13 #ifndef PQXX_H_STREAM_TO 14 #define PQXX_H_STREAM_TO 16 #include "pqxx/compiler-public.hxx" 17 #include "pqxx/internal/compiler-internal-pre.hxx" 19 #include "pqxx/transaction_base.hxx" 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));
138 PQXX_DEPRECATED(
"Use table() or raw_table() factory.")
140 stream_to{tx, table_name,
""sv}
146 template<
typename Columns>
147 PQXX_DEPRECATED(
"Use table() or raw_table() factory.")
154 template<
typename Iter>
155 PQXX_DEPRECATED(
"Use table() or raw_table() factory.")
163 [[nodiscard]] operator
bool() const noexcept {
return not m_finished; }
165 [[nodiscard]]
bool operator!() const noexcept {
return m_finished; }
218 fill_buffer(fields...);
227 bool m_finished =
false;
230 std::string m_buffer;
233 std::string m_field_buf;
236 void write_raw_line(std::string_view);
244 static constexpr std::string_view null_field{
"\\N\t"};
248 static std::enable_if_t<nullness<T>::always_null, std::size_t>
249 estimate_buffer(T
const &)
251 return std::size(null_field);
259 static std::enable_if_t<not nullness<T>::always_null, std::size_t>
260 estimate_buffer(T
const &
field)
266 void escape_field_to_buffer(std::string_view);
275 template<
typename Field>
276 std::enable_if_t<not nullness<Field>::always_null>
277 append_to_buffer(Field
const &f)
285 m_buffer.append(null_field);
292 auto const budget{estimate_buffer(f)};
293 auto const offset{std::size(m_buffer)};
295 if constexpr (std::is_arithmetic_v<Field>)
303 auto const total{offset + budget};
304 m_buffer.resize(total);
305 char *
const end{traits::into_buf(
306 m_buffer.data() + offset, m_buffer.data() + total, f)};
309 m_buffer.resize(static_cast<std::size_t>(end - m_buffer.data()));
316 m_field_buf.resize(budget);
318 m_field_buf.data(), m_field_buf.data() + std::size(m_field_buf), f));
330 template<
typename Field>
331 std::enable_if_t<nullness<Field>::always_null>
332 append_to_buffer(Field
const &)
334 m_buffer.append(null_field);
338 template<
typename Container>
339 std::enable_if_t<not std::is_same_v<typename Container::value_type, char>>
340 fill_buffer(Container
const &c)
345 std::size_t budget{0};
346 for (
auto const &f : c) budget += estimate_buffer(f);
347 m_buffer.reserve(budget);
348 for (
auto const &f : c) append_to_buffer(f);
352 template<
typename Tuple, std::size_t... indexes>
354 budget_tuple(Tuple
const &t, std::index_sequence<indexes...>)
356 return (estimate_buffer(std::get<indexes>(t)) + ...);
360 template<
typename Tuple, std::size_t... indexes>
361 void append_tuple(Tuple
const &t, std::index_sequence<indexes...>)
363 (append_to_buffer(std::get<indexes>(t)), ...);
367 template<
typename... Elts>
void fill_buffer(std::tuple<Elts...>
const &t)
369 using indexes = std::make_index_sequence<
sizeof...(Elts)>;
371 m_buffer.reserve(budget_tuple(t, indexes{}));
372 append_tuple(t, indexes{});
376 template<
typename... Ts>
void fill_buffer(
const Ts &...fields)
378 (..., append_to_buffer(fields));
381 constexpr
static std::string_view s_classname{
"stream_to"};
385 template<
typename Columns>
386 inline stream_to::stream_to(
387 transaction_base &tx, std::string_view table_name, Columns
const &columns) :
388 stream_to{tx, table_name, std::begin(columns), std::end(columns)}
392 template<
typename Iter>
393 inline stream_to::stream_to(
406 #include "pqxx/internal/compiler-internal-post.hxx" bool operator!() const noexcept
Has this stream been through its concluding complete()?
Definition: stream_to.hxx:165
Reference to one row in a result.
Definition: row.hxx:45
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
Traits class for use in string conversions.
Definition: strconv.hxx:148
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:353
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::vector< std::string_view > to_buf(char *here, char const *end, TYPE... value)
Convert multiple values to strings inside a single buffer.
Definition: strconv.hxx:332
void write_values(Ts const &...fields)
Insert values as a row.
Definition: stream_to.hxx:216
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:481
stream_to & operator<<(Row const &row)
Insert a row of data.
Definition: stream_to.hxx:186
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:206
Efficiently write data directly to a database table.
Definition: stream_to.hxx:78
Reference to a field in a result set.
Definition: field.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:42
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
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:188
Base class for things that monopolise a transaction's attention.
Definition: transaction_focus.hxx:27
Stream data from the database.
Definition: stream_from.hxx:69
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
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:74
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition: connection.hxx:120