libpqxx  7.9.0
result.hxx
1 /* Definitions for the pqxx::result class and support classes.
2  *
3  * pqxx::result represents the set of result rows from a database query.
4  *
5  * DO NOT INCLUDE THIS FILE DIRECTLY; include pqxx/result instead.
6  *
7  * Copyright (c) 2000-2024, 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_RESULT
14 #define PQXX_H_RESULT
15 
16 #if !defined(PQXX_HEADER_PRE)
17 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
18 #endif
19 
20 #include <functional>
21 #include <ios>
22 #include <memory>
23 #include <stdexcept>
24 
25 #include "pqxx/except.hxx"
26 #include "pqxx/types.hxx"
27 #include "pqxx/util.hxx"
28 #include "pqxx/zview.hxx"
29 
30 #include "pqxx/internal/encodings.hxx"
31 
32 
33 namespace pqxx::internal
34 {
35 PQXX_LIBEXPORT void clear_result(pq::PGresult const *) noexcept;
36 } // namespace pqxx::internal
37 
38 
39 namespace pqxx::internal::gate
40 {
41 class result_connection;
42 class result_creation;
43 class result_pipeline;
44 class result_row;
45 class result_sql_cursor;
46 } // namespace pqxx::internal::gate
47 
48 
49 namespace pqxx
50 {
52 
72 class PQXX_LIBEXPORT result
73 {
74 public:
77  using reference = row;
78  using const_iterator = const_result_iterator;
81  using const_reverse_iterator = const_reverse_result_iterator;
83 
84  result() noexcept :
85  m_data{}, m_query{}, m_encoding{internal::encoding_group::MONOBYTE}
86  {}
87 
88  result(result const &rhs) noexcept = default;
89  result(result &&rhs) noexcept = default;
90 
92 
95  result &operator=(result const &rhs) noexcept = default;
96 
98  result &operator=(result &&rhs) noexcept = default;
99 
109  [[nodiscard]] bool operator==(result const &) const noexcept;
111  [[nodiscard]] bool operator!=(result const &rhs) const noexcept
112  {
113  return not operator==(rhs);
114  }
116 
118 
124  template<typename... TYPE> auto iter() const;
125 
126  [[nodiscard]] const_reverse_iterator rbegin() const;
127  [[nodiscard]] const_reverse_iterator crbegin() const;
128  [[nodiscard]] const_reverse_iterator rend() const;
129  [[nodiscard]] const_reverse_iterator crend() const;
130 
131  [[nodiscard]] const_iterator begin() const noexcept;
132  [[nodiscard]] const_iterator cbegin() const noexcept;
133  [[nodiscard]] inline const_iterator end() const noexcept;
134  [[nodiscard]] inline const_iterator cend() const noexcept;
135 
136  [[nodiscard]] reference front() const noexcept;
137  [[nodiscard]] reference back() const noexcept;
138 
139  [[nodiscard]] PQXX_PURE size_type size() const noexcept;
140  [[nodiscard]] PQXX_PURE bool empty() const noexcept;
141  [[nodiscard]] size_type capacity() const noexcept { return size(); }
142 
144 
148  void swap(result &) noexcept;
149 
151 
155  [[nodiscard]] row operator[](size_type i) const noexcept;
156 
157 #if defined(PQXX_HAVE_MULTIDIM)
158  [[nodiscard]] field
159  operator[](size_type row_num, row_size_type col_num) const noexcept;
160 #endif // PQXX_HAVE_MULTIDIM
161 
163  row at(size_type) const;
164 
166  field at(size_type, row_size_type) const;
167 
169 
176  void clear() noexcept
177  {
178  m_data.reset();
179  m_query = nullptr;
180  }
181 
187  [[nodiscard]] PQXX_PURE row_size_type columns() const noexcept;
188 
190  [[nodiscard]] row_size_type column_number(zview name) const;
191 
193  [[nodiscard]] char const *column_name(row_size_type number) const &;
194 
196 
199  [[nodiscard]] int column_storage(row_size_type number) const;
200 
202 
212  [[nodiscard]] int column_type_modifier(row_size_type number) const noexcept;
213 
215  [[nodiscard]] oid column_type(row_size_type col_num) const;
216 
218  [[nodiscard]] oid column_type(zview col_name) const
219  {
220  return column_type(column_number(col_name));
221  }
222 
224  [[nodiscard]] oid column_table(row_size_type col_num) const;
225 
227  [[nodiscard]] oid column_table(zview col_name) const
228  {
229  return column_table(column_number(col_name));
230  }
231 
233  [[nodiscard]] row_size_type table_column(row_size_type col_num) const;
234 
236  [[nodiscard]] row_size_type table_column(zview col_name) const
237  {
238  return table_column(column_number(col_name));
239  }
241 
243  [[nodiscard]] PQXX_PURE std::string const &query() const & noexcept;
244 
246 
249  [[nodiscard]] PQXX_PURE oid inserted_oid() const;
250 
252 
255  [[nodiscard]] PQXX_PURE size_type affected_rows() const;
256 
257  // C++20: Concept like std::invocable, but without specifying param types.
259 
296  template<typename CALLABLE> inline void for_each(CALLABLE &&func) const;
297 
298 private:
299  using data_pointer = std::shared_ptr<internal::pq::PGresult const>;
300 
302  data_pointer m_data;
303 
304  friend class pqxx::internal::gate::result_pipeline;
305  PQXX_PURE std::shared_ptr<std::string const> query_ptr() const noexcept
306  {
307  return m_query;
308  }
309 
311  std::shared_ptr<std::string const> m_query;
312 
313  internal::encoding_group m_encoding;
314 
315  static std::string const s_empty_string;
316 
317  friend class pqxx::field;
318  PQXX_PURE char const *
319  get_value(size_type row, row_size_type col) const noexcept;
320  PQXX_PURE bool get_is_null(size_type row, row_size_type col) const noexcept;
321  PQXX_PURE
322  field_size_type get_length(size_type, row_size_type) const noexcept;
323 
324  friend class pqxx::internal::gate::result_creation;
325  result(
326  std::shared_ptr<internal::pq::PGresult> const &rhs,
327  std::shared_ptr<std::string> const &query, internal::encoding_group enc);
328 
329  PQXX_PRIVATE void check_status(std::string_view desc = ""sv) const;
330 
331  friend class pqxx::internal::gate::result_connection;
332  friend class pqxx::internal::gate::result_row;
333  bool operator!() const noexcept { return m_data.get() == nullptr; }
334  operator bool() const noexcept { return m_data.get() != nullptr; }
335 
336  [[noreturn]] PQXX_PRIVATE PQXX_COLD void
337  throw_sql_error(std::string const &Err, std::string const &Query) const;
338  PQXX_PRIVATE PQXX_PURE int errorposition() const;
339  PQXX_PRIVATE std::string status_error() const;
340 
341  friend class pqxx::internal::gate::result_sql_cursor;
342  PQXX_PURE char const *cmd_status() const noexcept;
343 };
344 } // namespace pqxx
345 #endif
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:33
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:34
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:40
int result_difference_type
Difference between result sizes.
Definition: types.hxx:31
int result_size_type
Number of rows in a result set.
Definition: types.hxx:28
Internal items for libpqxx' own use. Do not use these yourself.
Definition: composite.hxx:84
void clear_result(pq::PGresult const *) noexcept
C++ wrapper for libpq's PQclear.
Definition: result.cxx:42
Definition: connection.hxx:109
Reference to a field in a result set.
Definition: field.hxx:35
constexpr row_size_type col() const noexcept
Definition: field.hxx:274
Result set containing data returned by a query or command.
Definition: result.hxx:73
const_reverse_result_iterator const_reverse_iterator
Definition: result.hxx:81
result(result &&rhs) noexcept=default
row_size_type table_column(zview col_name) const
What column in its table did this column come from?
Definition: result.hxx:236
result() noexcept
Definition: result.hxx:84
result_size_type size_type
Definition: result.hxx:75
result & operator=(result &&rhs) noexcept=default
Assign one result to another, invaliding the old one.
bool operator!=(result const &rhs) const noexcept
Compare two results for inequality.
Definition: result.hxx:111
const_iterator pointer
Definition: result.hxx:79
result & operator=(result const &rhs) noexcept=default
Assign one result to another.
void clear() noexcept
Let go of the result's data.
Definition: result.hxx:176
const_iterator iterator
Definition: result.hxx:80
result_difference_type difference_type
Definition: result.hxx:76
const_reverse_iterator reverse_iterator
Definition: result.hxx:82
oid column_table(zview col_name) const
What table did this column come from?
Definition: result.hxx:227
const_result_iterator const_iterator
Definition: result.hxx:78
result(result const &rhs) noexcept=default
auto iter() const
Iterate rows, reading them directly into a tuple of "TYPE...".
Reference to one row in a result.
Definition: row.hxx:47
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:38