13 #ifndef PQXX_H_STREAM_TO 14 #define PQXX_H_STREAM_TO 16 #include "pqxx/separated_list.hxx" 17 #include "pqxx/transaction_base.hxx" 103 return {tx, path, columns};
118 std::initializer_list<std::string_view> columns = {})
120 auto const &conn{tx.
conn()};
121 return raw_table(tx, conn.quote_table(path), conn.quote_columns(columns));
124 #if defined(PQXX_HAVE_CONCEPTS) 133 template<PQXX_CHAR_STRINGS_ARG COLUMNS>
135 table(transaction_base &tx,
table_path path, COLUMNS
const &columns)
137 auto const &conn{tx.conn()};
139 tx, conn.quote_table(path), tx.conn().quote_columns(columns));
150 template<PQXX_CHAR_STRINGS_ARG COLUMNS>
152 table(transaction_base &tx, std::string_view path, COLUMNS
const &columns)
156 #endif // PQXX_HAVE_CONCEPTS 168 [[deprecated(
"Use table() or raw_table() factory.")]]
stream_to(
176 template<
typename Columns>
177 [[deprecated(
"Use table() or raw_table() factory.")]] stream_to(
178 transaction_base &, std::string_view table_name, Columns
const &columns);
183 template<
typename Iter>
184 [[deprecated(
"Use table() or raw_table() factory.")]] stream_to(
185 transaction_base &, std::string_view table_name, Iter columns_begin,
188 ~stream_to() noexcept;
191 [[nodiscard]] operator
bool() const noexcept {
return not m_finished; }
193 [[nodiscard]]
bool operator!() const noexcept {
return m_finished; }
246 fill_buffer(fields...);
255 bool m_finished =
false;
258 std::string m_buffer;
261 std::string m_field_buf;
264 internal::glyph_scanner_func *m_scanner;
267 void write_raw_line(std::string_view);
276 static constexpr std::string_view null_field{
"\\N\t"};
280 static std::enable_if_t<nullness<T>::always_null, std::size_t>
281 estimate_buffer(T
const &)
283 return std::size(null_field);
291 static std::enable_if_t<not nullness<T>::always_null, std::size_t>
292 estimate_buffer(T
const &field)
298 void escape_field_to_buffer(std::string_view data);
307 template<
typename Field>
308 std::enable_if_t<not nullness<Field>::always_null>
309 append_to_buffer(Field
const &f)
317 m_buffer.append(null_field);
323 using traits = string_traits<Field>;
324 auto const budget{estimate_buffer(f)};
325 auto const offset{std::size(m_buffer)};
327 if constexpr (std::is_arithmetic_v<Field>)
335 auto const total{offset + budget};
336 m_buffer.resize(total);
337 auto const data{m_buffer.data()};
338 char *
const end{traits::into_buf(data + offset, data + total, f)};
341 m_buffer.resize(static_cast<std::size_t>(end - data));
344 std::is_same_v<Field, std::string> or
345 std::is_same_v<Field, std::string_view> or
346 std::is_same_v<Field, zview>)
349 m_field_buf.resize(budget);
350 escape_field_to_buffer(f);
356 m_field_buf.resize(budget);
357 auto const data{m_field_buf.data()};
358 escape_field_to_buffer(
371 template<
typename Field>
372 std::enable_if_t<nullness<Field>::always_null>
373 append_to_buffer(Field
const &)
375 m_buffer.append(null_field);
379 template<
typename Container>
380 std::enable_if_t<not std::is_same_v<typename Container::value_type, char>>
381 fill_buffer(Container
const &c)
386 std::size_t budget{0};
387 for (
auto const &f : c) budget += estimate_buffer(f);
388 m_buffer.reserve(budget);
389 for (
auto const &f : c) append_to_buffer(f);
393 template<
typename Tuple, std::size_t... indexes>
395 budget_tuple(Tuple
const &t, std::index_sequence<indexes...>)
397 return (estimate_buffer(std::get<indexes>(t)) + ...);
401 template<
typename Tuple, std::size_t... indexes>
402 void append_tuple(Tuple
const &t, std::index_sequence<indexes...>)
404 (append_to_buffer(std::get<indexes>(t)), ...);
408 template<
typename... Elts>
void fill_buffer(std::tuple<Elts...>
const &t)
410 using indexes = std::make_index_sequence<
sizeof...(Elts)>;
412 m_buffer.reserve(budget_tuple(t, indexes{}));
413 append_tuple(t, indexes{});
417 template<
typename... Ts>
void fill_buffer(
const Ts &...fields)
419 (..., append_to_buffer(fields));
423 constexpr
static std::string_view s_classname{
"stream_to"};
427 template<
typename Columns>
429 transaction_base &tx, std::string_view table_name, Columns
const &columns) :
430 stream_to{tx, table_name, std::begin(columns), std::end(columns)}
434 template<
typename Iter>
void write_values(Ts const &...fields)
Insert values as a row.
Definition: stream_to.hxx:244
Stream data from the database.
Definition: stream_from.hxx:71
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:375
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:343
Reference to one row in a result.
Definition: row.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:100
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:72
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:483
bool operator!() const noexcept
Has this stream been through its concluding complete()?
Definition: stream_to.hxx:193
bool is_null(TYPE const &value) noexcept
Is value null?
Definition: strconv.hxx:364
stream_to(transaction_base &tx, std::string_view table_name)
Create a stream, without specifying columns.
Definition: stream_to.hxx:168
connection & conn() const
The connection in which this transaction lives.
Definition: transaction_base.hxx:523
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition: connection.hxx:119
Efficiently write data directly to a database table.
Definition: stream_to.hxx:76
Base class for things that monopolise a transaction's attention.
Definition: transaction_focus.hxx:24
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:116
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:22
std::string quote_name(std::string_view identifier) const
Escape an SQL identifier for use in a query.
Definition: transaction_base.hxx:216
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:39
stream_to & operator<<(Row const &row)
Insert a row of data.
Definition: stream_to.hxx:214
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:234