libpqxx  7.6.1
stream_from.hxx
1 /* Definition of the pqxx::stream_from class.
2  *
3  * pqxx::stream_from enables optimized batch reads from a database table.
4  *
5  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/stream_from instead.
6  *
7  * Copyright (c) 2000-2022, Jeroen T. Vermeulen.
8  *
9  * See COPYING for copyright license. If you did not receive a file called
10  * COPYING with this source code, please notify the distributor of this
11  * mistake, or contact the author.
12  */
13 #ifndef PQXX_H_STREAM_FROM
14 #define PQXX_H_STREAM_FROM
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
18 
19 #include <cassert>
20 #include <functional>
21 #include <variant>
22 
23 #include "pqxx/connection.hxx"
24 #include "pqxx/except.hxx"
25 #include "pqxx/internal/concat.hxx"
26 #include "pqxx/internal/encoding_group.hxx"
27 #include "pqxx/internal/stream_iterator.hxx"
28 #include "pqxx/separated_list.hxx"
29 #include "pqxx/transaction_focus.hxx"
30 
31 
32 namespace pqxx
33 {
34 class transaction_base;
35 
36 
38 
42 
45 
46 
48 
72 class PQXX_LIBEXPORT stream_from : transaction_focus
73 {
74 public:
75  using raw_line =
76  std::pair<std::unique_ptr<char, std::function<void(char *)>>, std::size_t>;
77 
79 
88  static stream_from query(transaction_base &tx, std::string_view q)
89  {
90 #include "pqxx/internal/ignore-deprecated-pre.hxx"
91  return stream_from{tx, from_query, q};
92 #include "pqxx/internal/ignore-deprecated-post.hxx"
93  }
94 
105 
107 
123  static stream_from raw_table(
124  transaction_base &tx, std::string_view path,
125  std::string_view columns = ""sv);
126 
128 
130  static stream_from table(
131  transaction_base &tx, table_path path,
132  std::initializer_list<std::string_view> columns = {});
134 
136 
138  [[deprecated("Use query() factory instead.")]] stream_from(
139  transaction_base &, from_query_t, std::string_view query);
140 
142 
144  [[deprecated("Use table() or raw_table() factory instead.")]] stream_from(
145  transaction_base &, from_table_t, std::string_view table);
146 
148 
150  template<typename Iter>
151  [[deprecated("Use table() or raw_table() factory instead.")]] stream_from(
152  transaction_base &, from_table_t, std::string_view table,
153  Iter columns_begin, Iter columns_end);
154 
156 
158  template<typename Columns>
159  [[deprecated("Use table() or raw_table() factory instead.")]] stream_from(
160  transaction_base &tx, from_table_t, std::string_view table,
161  Columns const &columns);
162 
163 #include "pqxx/internal/ignore-deprecated-pre.hxx"
165  [[deprecated("Use the from_table_t overload instead.")]] stream_from(
166  transaction_base &tx, std::string_view table) :
167  stream_from{tx, from_table, table}
168  {}
169 #include "pqxx/internal/ignore-deprecated-post.hxx"
170 
172  template<typename Columns>
173  [[deprecated("Use the from_table_t overload instead.")]] stream_from(
174  transaction_base &tx, std::string_view table, Columns const &columns) :
175  stream_from{tx, from_table, table, columns}
176  {}
177 
179  template<typename Iter>
180  [[deprecated("Use the from_table_t overload instead.")]] stream_from(
181  transaction_base &, std::string_view table, Iter columns_begin,
182  Iter columns_end);
183 
184  ~stream_from() noexcept;
185 
187  [[nodiscard]] operator bool() const noexcept { return not m_finished; }
189  [[nodiscard]] bool operator!() const noexcept { return m_finished; }
190 
192 
198  void complete();
199 
201 
208  template<typename Tuple> stream_from &operator>>(Tuple &);
209 
211  template<typename... Vs>
212  stream_from &operator>>(std::variant<Vs...> &) = delete;
213 
215 
219  template<typename... TYPE> [[nodiscard]] auto iter()
220  {
221  return pqxx::internal::stream_input_iteration<TYPE...>{*this};
222  }
223 
225 
241  std::vector<zview> const *read_row();
242 
244 
245  raw_line get_raw_line();
246 
247 private:
248  // TODO: Clean up this signature once we cull the deprecated constructors.
250  stream_from(
251  transaction_base &tx, std::string_view table, std::string_view columns,
252  from_table_t);
253 
254  // TODO: Clean up this signature once we cull the deprecated constructors.
256  stream_from(
257  transaction_base &, std::string_view unquoted_table,
258  std::string_view columns, from_table_t, int);
259 
260  template<typename Tuple, std::size_t... indexes>
261  void extract_fields(Tuple &t, std::index_sequence<indexes...>) const
262  {
263  (extract_value<Tuple, indexes>(t), ...);
264  }
265 
266  pqxx::internal::glyph_scanner_func *m_glyph_scanner;
267 
269  std::string m_row;
270 
272  std::vector<zview> m_fields;
273 
274  bool m_finished = false;
275 
276  void close();
277 
278  template<typename Tuple, std::size_t index>
279  void extract_value(Tuple &) const;
280 
282  void parse_line();
283 };
284 
285 
286 template<typename Columns>
288  transaction_base &tx, from_table_t, std::string_view table_name,
289  Columns const &columns) :
290  stream_from{
291  tx, from_table, table_name, std::begin(columns), std::end(columns)}
292 {}
293 
294 
295 template<typename Iter>
297  transaction_base &tx, from_table_t, std::string_view table,
298  Iter columns_begin, Iter columns_end) :
299  stream_from{
300  tx, table, separated_list(",", columns_begin, columns_end),
301  from_table, 1}
302 {}
303 
304 
305 template<typename Tuple> inline stream_from &stream_from::operator>>(Tuple &t)
306 {
307  if (m_finished)
308  return *this;
309  constexpr auto tup_size{std::tuple_size_v<Tuple>};
310  m_fields.reserve(tup_size);
311  parse_line();
312  if (m_finished)
313  return *this;
314 
315  if (std::size(m_fields) != tup_size)
316  throw usage_error{internal::concat(
317  "Tried to extract ", tup_size, " field(s) from a stream of ",
318  std::size(m_fields), ".")};
319 
320  extract_fields(t, std::make_index_sequence<tup_size>{});
321  return *this;
322 }
323 
324 
325 template<typename Tuple, std::size_t index>
326 inline void stream_from::extract_value(Tuple &t) const
327 {
328  using field_type = strip_t<decltype(std::get<index>(t))>;
329  using nullity = nullness<field_type>;
330  assert(index < std::size(m_fields));
331  if constexpr (nullity::always_null)
332  {
333  if (std::data(m_fields[index]) != nullptr)
334  throw conversion_error{"Streaming non-null value into null field."};
335  }
336  else if (std::data(m_fields[index]) == nullptr)
337  {
338  if constexpr (nullity::has_null)
339  std::get<index>(t) = nullity::null();
340  else
341  PQXX_UNLIKELY
342  internal::throw_null_conversion(type_name<field_type>);
343  }
344  else
345  {
346  // Don't ever try to convert a non-null value to nullptr_t!
347  std::get<index>(t) = from_string<field_type>(m_fields[index]);
348  }
349 }
350 } // namespace pqxx
351 
352 #include "pqxx/internal/compiler-internal-post.hxx"
353 #endif
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::remove_cv_t< std::remove_reference_t< TYPE > > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: types.hxx:87
constexpr from_query_t from_query
Pass this to a stream_from constructor to stream query results.
Definition: stream_from.hxx:44
constexpr from_table_t from_table
Pass this to a stream_from constructor to stream table contents.
Definition: stream_from.hxx:34
std::initializer_list< std::string_view > table_path
Representation of a PostgreSQL table path.
Definition: connection.hxx:119
void throw_null_conversion(std::string const &type)
Definition: strconv.cxx:243
Error in usage of libpqxx library, similar to std::logic_error.
Definition: except.hxx:165
Value conversion failed, e.g. when converting "Hello" to int.
Definition: except.hxx:179
Traits describing a type's "null value," if any.
Definition: strconv.hxx:91
Stream data from the database.
Definition: stream_from.hxx:73
static stream_from query(transaction_base &tx, std::string_view q)
Factory: Execute query, and stream the results.
Definition: stream_from.hxx:88
std::pair< std::unique_ptr< char, std::function< void(char *)> >, std::size_t > raw_line
Definition: stream_from.hxx:76
stream_from(transaction_base &, from_query_t, std::string_view query)
Execute query, and stream over the results.
Definition: stream_from.cxx:36
stream_from & operator>>(Tuple &)
Read one row into a tuple.
Definition: stream_from.hxx:305
stream_from(transaction_base &tx, std::string_view table, Columns const &columns)
Definition: stream_from.hxx:173
stream_from(transaction_base &, std::string_view table, Iter columns_begin, Iter columns_end)
stream_from & operator>>(std::variant< Vs... > &)=delete
Doing this with a std::variant is going to be horrifically borked.
bool operator!() const noexcept
Has this stream produced all the data it is going to produce?
Definition: stream_from.hxx:189
stream_from(transaction_base &tx, std::string_view table)
Definition: stream_from.hxx:165
auto iter()
Iterate over this stream. Supports range-based "for" loops.
Definition: stream_from.hxx:219
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:76
Base class for things that monopolise a transaction's attention.
Definition: transaction_focus.hxx:28
Marker for stream_from constructors: "stream from table.".
Definition: types.hxx:64
Marker for stream_from constructors: "stream from query.".
Definition: types.hxx:70