libpqxx  7.2.0
cursor.hxx
1 /* Definition of the iterator/container-style cursor classes.
2  *
3  * C++-style wrappers for SQL cursors.
4  *
5  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/cursor instead.
6  *
7  * Copyright (c) 2000-2020, 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_CURSOR
14 #define PQXX_H_CURSOR
15 
16 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
18 
19 #include <limits>
20 #include <stdexcept>
21 
22 #include "pqxx/result.hxx"
23 #include "pqxx/transaction_base.hxx"
24 
25 
26 namespace pqxx
27 {
29 
40 class PQXX_LIBEXPORT cursor_base
41 {
42 public:
45 
47 
51  {
55  random_access
56  };
57 
59 
63  {
67  update
68  };
69 
71 
87  {
91  loose
92  };
93 
94  cursor_base() = delete;
95  cursor_base(cursor_base const &) = delete;
96  cursor_base &operator=(cursor_base const &) = delete;
97 
102 
104 
107  [[nodiscard]] static difference_type all() noexcept;
108 
110 
112  [[nodiscard]] static difference_type next() noexcept { return 1; }
113 
115 
117  [[nodiscard]] static difference_type prior() noexcept { return -1; }
118 
120 
122  [[nodiscard]] static difference_type backward_all() noexcept;
123 
125 
127 
132  [[nodiscard]] std::string const &name() const noexcept { return m_name; }
133 
134 protected:
135  cursor_base(connection &, std::string_view Name, bool embellish_name = true);
136 
137  std::string const m_name;
138 };
139 } // namespace pqxx
140 
141 
142 #include <pqxx/internal/sql_cursor.hxx>
143 
144 
145 namespace pqxx
146 {
148 
154 template<cursor_base::update_policy up, cursor_base::ownership_policy op>
156 {
157 public:
160 
163  transaction_base &trans, std::string_view query, std::string_view cname,
164  bool hold) :
165  m_cur{trans, query, cname, cursor_base::random_access, up, op, hold}
166  {}
167 
169  stateless_cursor(transaction_base &trans, std::string_view adopted_cursor) :
170  m_cur{trans, adopted_cursor, op}
171  {
172  // Put cursor in known position
173  m_cur.move(cursor_base::backward_all());
174  }
175 
176  void close() noexcept { m_cur.close(); }
177 
179 
182  [[nodiscard]] size_type size()
183  {
184  return internal::obtain_stateless_cursor_size(m_cur);
185  }
186 
188 
200  {
201  return internal::stateless_cursor_retrieve(
202  m_cur, result::difference_type(size()), begin_pos, end_pos);
203  }
204 
205  [[nodiscard]] std::string const &name() const noexcept
206  {
207  return m_cur.name();
208  }
209 
210 private:
211  internal::sql_cursor m_cur;
212 };
213 
214 
215 class icursor_iterator;
216 } // namespace pqxx
217 
218 
219 namespace pqxx::internal::gate
220 {
221 class icursor_iterator_icursorstream;
222 class icursorstream_icursor_iterator;
223 } // namespace pqxx::internal::gate
224 
225 
226 namespace pqxx
227 {
229 
244 class PQXX_LIBEXPORT icursorstream
245 {
246 public:
249 
251 
263  transaction_base &context, std::string_view query,
264  std::string_view basename, difference_type sstride = 1);
265 
267 
292  transaction_base &context, field const &cname, difference_type sstride = 1,
294 
295  operator bool() const noexcept { return not m_done; }
296 
298 
305  {
306  res = fetchblock();
307  return *this;
308  }
310 
316  icursorstream &operator>>(result &res) { return get(res); }
317 
319 
323  icursorstream &ignore(std::streamsize n = 1);
324 
326 
329  void set_stride(difference_type stride);
330  [[nodiscard]] difference_type stride() const noexcept { return m_stride; }
331 
332 private:
333  result fetchblock();
334 
335  friend class internal::gate::icursorstream_icursor_iterator;
336  size_type forward(size_type n = 1);
337  void insert_iterator(icursor_iterator *) noexcept;
338  void remove_iterator(icursor_iterator *) const noexcept;
339 
340  void service_iterators(difference_type);
341 
342  internal::sql_cursor m_cur;
343 
344  difference_type m_stride;
345  difference_type m_realpos, m_reqpos;
346 
347  mutable icursor_iterator *m_iterators;
348 
349  bool m_done;
350 };
351 
352 
354 
380 class PQXX_LIBEXPORT icursor_iterator
381 {
382 public:
383  using iterator_category = std::input_iterator_tag;
385  using pointer = result const *;
386  using reference = result const &;
390 
391  icursor_iterator() noexcept;
392  explicit icursor_iterator(istream_type &) noexcept;
393  icursor_iterator(icursor_iterator const &) noexcept;
394  ~icursor_iterator() noexcept;
395 
396  result const &operator*() const
397  {
398  refresh();
399  return m_here;
400  }
401  result const *operator->() const
402  {
403  refresh();
404  return &m_here;
405  }
406  icursor_iterator &operator++();
407  icursor_iterator operator++(int);
408  icursor_iterator &operator+=(difference_type);
409  icursor_iterator &operator=(icursor_iterator const &) noexcept;
410 
411  [[nodiscard]] bool operator==(icursor_iterator const &rhs) const;
412  [[nodiscard]] bool operator!=(icursor_iterator const &rhs) const noexcept
413  {
414  return not operator==(rhs);
415  }
416  [[nodiscard]] bool operator<(icursor_iterator const &rhs) const;
417  [[nodiscard]] bool operator>(icursor_iterator const &rhs) const
418  {
419  return rhs < *this;
420  }
421  [[nodiscard]] bool operator<=(icursor_iterator const &rhs) const
422  {
423  return not(*this > rhs);
424  }
425  [[nodiscard]] bool operator>=(icursor_iterator const &rhs) const
426  {
427  return not(*this < rhs);
428  }
429 
430 private:
431  void refresh() const;
432 
433  friend class internal::gate::icursor_iterator_icursorstream;
434  difference_type pos() const noexcept { return m_pos; }
435  void fill(result const &);
436 
437  icursorstream *m_stream{nullptr};
438  result m_here;
439  difference_type m_pos;
440  icursor_iterator *m_prev{nullptr}, *m_next{nullptr};
441 };
442 } // namespace pqxx
443 
444 #include "pqxx/internal/compiler-internal-post.hxx"
445 #endif
Common definitions for cursor types.
Definition: cursor.hxx:40
cursor_base::size_type size_type
Definition: cursor.hxx:247
static difference_type backward_all() noexcept
Special value: read backwards from current position back to origin.
Definition: cursor.cxx:32
result const & reference
Definition: cursor.hxx:386
size_type size()
Number of rows in cursor&#39;s result set.
Definition: cursor.hxx:182
std::input_iterator_tag iterator_category
Definition: cursor.hxx:383
Simple read-only cursor represented as a stream of results.
Definition: cursor.hxx:244
result_size_type size_type
Definition: cursor.hxx:43
icursorstream & operator>>(result &res)
Read new value into given result object; same as get(result &)
Definition: cursor.hxx:316
int result_difference_type
Difference between result sizes.
Definition: types.hxx:21
bool operator>(icursor_iterator const &rhs) const
Definition: cursor.hxx:417
result const * pointer
Definition: cursor.hxx:385
cursor_base::difference_type difference_type
Definition: cursor.hxx:248
Cursor can move back and forth.
Definition: cursor.hxx:55
Connection to a database.
Definition: connection.hxx:164
difference_type stride() const noexcept
Definition: cursor.hxx:330
bool operator<=(icursor_iterator const &rhs) const
Definition: cursor.hxx:421
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
Destroy SQL cursor when cursor object is closed at end of transaction.
Definition: cursor.hxx:89
"Stateless cursor" class: easy API for retrieving parts of result sets
Definition: cursor.hxx:155
Reference to a field in a result set.
Definition: field.hxx:33
std::string const m_name
Definition: cursor.hxx:137
result const * operator->() const
Definition: cursor.hxx:401
istream_type::size_type size_type
Definition: cursor.hxx:388
Approximate istream_iterator for icursorstream.
Definition: cursor.hxx:380
Cursor can move forward only.
Definition: cursor.hxx:53
std::string const & name() const noexcept
Name of underlying SQL cursor.
Definition: cursor.hxx:132
bool operator!=(icursor_iterator const &rhs) const noexcept
Definition: cursor.hxx:412
bool operator>=(icursor_iterator const &rhs) const
Definition: cursor.hxx:425
Cursor can be used to read data but not to write.
Definition: cursor.hxx:65
Result set containing data returned by a query or command.
Definition: result.hxx:70
istream_type::difference_type difference_type
Definition: cursor.hxx:389
update_policy
Cursor update policy.
Definition: cursor.hxx:62
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:67
Definition: connection.hxx:94
result_difference_type difference_type
Definition: cursor.hxx:44
result_size_type size_type
Definition: cursor.hxx:158
stateless_cursor(transaction_base &trans, std::string_view adopted_cursor)
Adopt existing scrolling SQL cursor.
Definition: cursor.hxx:169
static difference_type prior() noexcept
Special value: read backwards, one row only.
Definition: cursor.hxx:117
access_policy
Cursor access-pattern policy.
Definition: cursor.hxx:50
stateless_cursor(transaction_base &trans, std::string_view query, std::string_view cname, bool hold)
Create cursor.
Definition: cursor.hxx:162
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
std::string const & name() const noexcept
Definition: cursor.hxx:205
result_difference_type difference_type
Definition: cursor.hxx:159
result retrieve(difference_type begin_pos, difference_type end_pos)
Retrieve rows from begin_pos (inclusive) to end_pos (exclusive)
Definition: cursor.hxx:199
result_difference_type difference_type
Definition: result.hxx:74
ownership_policy
Cursor destruction policy.
Definition: cursor.hxx:86
void close() noexcept
Definition: cursor.hxx:176