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/separated_list.hxx"
20 #include "pqxx/transaction_base.hxx"
121 std::initializer_list<std::string_view> columns = {})
123 auto const &conn{tx.
conn()};
124 return raw_table(tx, conn.quote_table(path), conn.quote_columns(columns));
127 #if defined(PQXX_HAVE_CONCEPTS)
136 template<PQXX_CHAR_STRINGS_ARG COLUMNS>
138 table(transaction_base &tx,
table_path path, COLUMNS
const &columns)
140 auto const &conn{tx.conn()};
142 tx, conn.quote_table(path), tx.conn().quote_columns(columns));
153 template<PQXX_CHAR_STRINGS_ARG COLUMNS>
155 table(transaction_base &tx, std::string_view path, COLUMNS
const &columns)
171 [[deprecated(
"Use table() or raw_table() factory.")]]
stream_to(
179 template<
typename Columns>
180 [[deprecated(
"Use table() or raw_table() factory.")]]
stream_to(
186 template<
typename Iter>
187 [[deprecated(
"Use table() or raw_table() factory.")]]
stream_to(
194 [[nodiscard]] operator
bool() const noexcept {
return not m_finished; }
196 [[nodiscard]]
bool operator!() const noexcept {
return m_finished; }
249 fill_buffer(fields...);
258 bool m_finished =
false;
261 std::string m_buffer;
264 std::string m_field_buf;
267 void write_raw_line(std::string_view);
275 static constexpr std::string_view null_field{
"\\N\t"};
279 static std::enable_if_t<nullness<T>::always_null, std::size_t>
280 estimate_buffer(T
const &)
282 return std::size(null_field);
290 static std::enable_if_t<not nullness<T>::always_null, std::size_t>
291 estimate_buffer(T
const &field)
297 void escape_field_to_buffer(std::string_view data);
306 template<
typename Field>
307 std::enable_if_t<not nullness<Field>::always_null>
308 append_to_buffer(Field
const &f)
316 m_buffer.append(null_field);
322 using traits = string_traits<Field>;
323 auto const budget{estimate_buffer(f)};
324 auto const offset{std::size(m_buffer)};
326 if constexpr (std::is_arithmetic_v<Field>)
334 auto const total{offset + budget};
335 m_buffer.resize(total);
336 auto const data{m_buffer.data()};
337 char *
const end{traits::into_buf(data + offset, data + total, f)};
340 m_buffer.resize(
static_cast<std::size_t
>(end - data));
343 std::is_same_v<Field, std::string> or
344 std::is_same_v<Field, std::string_view> or
345 std::is_same_v<Field, zview>)
348 m_field_buf.resize(budget);
349 escape_field_to_buffer(f);
355 m_field_buf.resize(budget);
356 auto const data{m_field_buf.data()};
357 escape_field_to_buffer(
358 traits::to_buf(data, data + std::size(m_field_buf), f));
370 template<
typename Field>
371 std::enable_if_t<nullness<Field>::always_null>
372 append_to_buffer(Field
const &)
374 m_buffer.append(null_field);
378 template<
typename Container>
379 std::enable_if_t<not std::is_same_v<typename Container::value_type, char>>
380 fill_buffer(Container
const &c)
385 std::size_t budget{0};
386 for (
auto const &f : c) budget += estimate_buffer(f);
387 m_buffer.reserve(budget);
388 for (
auto const &f : c) append_to_buffer(f);
392 template<
typename Tuple, std::size_t... indexes>
394 budget_tuple(Tuple
const &t, std::index_sequence<indexes...>)
396 return (estimate_buffer(std::get<indexes>(t)) + ...);
400 template<
typename Tuple, std::size_t... indexes>
401 void append_tuple(Tuple
const &t, std::index_sequence<indexes...>)
403 (append_to_buffer(std::get<indexes>(t)), ...);
407 template<
typename... Elts>
void fill_buffer(std::tuple<Elts...>
const &t)
409 using indexes = std::make_index_sequence<
sizeof...(Elts)>;
411 m_buffer.reserve(budget_tuple(t, indexes{}));
412 append_tuple(t, indexes{});
416 template<
typename... Ts>
void fill_buffer(
const Ts &...fields)
418 (..., append_to_buffer(fields));
421 constexpr
static std::string_view s_classname{
"stream_to"};
425 template<
typename Columns>
427 transaction_base &tx, std::string_view table_name, Columns
const &columns) :
428 stream_to{tx, table_name, std::begin(columns), std::end(columns)}
432 template<
typename Iter>
446 #include "pqxx/internal/compiler-internal-post.hxx"
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:368
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition: connection.hxx:119
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:479
bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:357
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:103
stream_to & operator<<(Row const &row)
Insert a row of data.
Definition: stream_to.hxx:217
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:119
void write_values(Ts const &...fields)
Insert values as a row.
Definition: stream_to.hxx:247
stream_to(transaction_base &tx, std::string_view table_name)
Create a stream, without specifying columns.
Definition: stream_to.hxx:171
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:237
bool operator!() const noexcept
Has this stream been through its concluding complete()?
Definition: stream_to.hxx:196
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:76
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:524
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:218
Base class for things that monopolise a transaction's attention.
Definition: transaction_focus.hxx:28