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"
123 std::initializer_list<std::string_view> columns = {})
125 auto const &conn{tx.
conn()};
126 return raw_table(tx, conn.quote_table(path), conn.quote_columns(columns));
139 PQXX_DEPRECATED(
"Use table() or raw_table() factory.")
147 template<
typename Columns>
148 PQXX_DEPRECATED(
"Use table() or raw_table() factory.")
150 transaction_base &, std::string_view table_name, Columns const &columns);
155 template<typename Iter>
156 PQXX_DEPRECATED("Use table() or raw_table() factory.")
164 [[nodiscard]] operator
bool() const noexcept {
return not m_finished; }
166 [[nodiscard]]
bool operator!() const noexcept {
return m_finished; }
219 fill_buffer(fields...);
228 bool m_finished =
false;
231 std::string m_buffer;
234 std::string m_field_buf;
237 void write_raw_line(std::string_view);
245 static constexpr std::string_view null_field{
"\\N\t"};
249 static std::enable_if_t<nullness<T>::always_null, std::size_t>
250 estimate_buffer(T
const &)
252 return std::size(null_field);
260 static std::enable_if_t<not nullness<T>::always_null, std::size_t>
261 estimate_buffer(T
const &field)
267 void escape_field_to_buffer(std::string_view);
276 template<
typename Field>
277 std::enable_if_t<not nullness<Field>::always_null>
278 append_to_buffer(Field
const &f)
286 m_buffer.append(null_field);
292 using traits = string_traits<Field>;
293 auto const budget{estimate_buffer(f)};
294 auto const offset{std::size(m_buffer)};
296 if constexpr (std::is_arithmetic_v<Field>)
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)};
310 m_buffer.resize(
static_cast<std::size_t
>(end - m_buffer.data()));
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));
331 template<
typename Field>
332 std::enable_if_t<nullness<Field>::always_null>
333 append_to_buffer(Field
const &)
335 m_buffer.append(null_field);
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)
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);
353 template<
typename Tuple, std::size_t... indexes>
355 budget_tuple(Tuple
const &t, std::index_sequence<indexes...>)
357 return (estimate_buffer(std::get<indexes>(t)) + ...);
361 template<
typename Tuple, std::size_t... indexes>
362 void append_tuple(Tuple
const &t, std::index_sequence<indexes...>)
364 (append_to_buffer(std::get<indexes>(t)), ...);
368 template<
typename... Elts>
void fill_buffer(std::tuple<Elts...>
const &t)
370 using indexes = std::make_index_sequence<
sizeof...(Elts)>;
372 m_buffer.reserve(budget_tuple(t, indexes{}));
373 append_tuple(t, indexes{});
377 template<
typename... Ts>
void fill_buffer(
const Ts &...fields)
379 (..., append_to_buffer(fields));
382 constexpr
static std::string_view s_classname{
"stream_to"};
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)}
393 template<
typename Iter>
394 inline stream_to::stream_to(
395 transaction_base &tx, std::string_view table_name, Iter columns_begin,
402 return tx.quote_name(*col);
407 #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: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