libpqxx  7.3.1
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-2021, 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 #include "pqxx/compiler-public.hxx"
17 #include "pqxx/internal/compiler-internal-pre.hxx"
18 
19 #include <ios>
20 #include <memory>
21 #include <stdexcept>
22 
23 #include "pqxx/except.hxx"
24 #include "pqxx/types.hxx"
25 #include "pqxx/util.hxx"
26 #include "pqxx/zview.hxx"
27 
28 #include "pqxx/internal/encodings.hxx"
29 
30 
31 namespace pqxx::internal
32 {
33 PQXX_LIBEXPORT void clear_result(pq::PGresult const *);
34 }
35 
36 
37 namespace pqxx::internal::gate
38 {
39 class result_connection;
40 class result_creation;
41 class result_pipeline;
42 class result_row;
43 class result_sql_cursor;
44 } // namespace pqxx::internal::gate
45 
46 
47 namespace pqxx
48 {
50 
70 class PQXX_LIBEXPORT result
71 {
72 public:
75  using reference = row;
81 
82  // TODO: Hide constructors from the public.
84  result() noexcept :
85  m_data(make_data_pointer()),
86  m_query(),
87  m_encoding(internal::encoding_group::MONOBYTE)
88  {}
89  // TODO: Hide constructors from the public.
91  result(result const &rhs) noexcept = default;
92 
94 
97  result &operator=(result const &rhs) noexcept = default;
98 
107  [[nodiscard]] bool operator==(result const &) const noexcept;
110  [[nodiscard]] bool operator!=(result const &rhs) const noexcept
111  {
112  return not operator==(rhs);
113  }
115 
117 
123  template<typename... TYPE> auto iter() const;
124 
125  [[nodiscard]] const_reverse_iterator rbegin() const;
126  [[nodiscard]] const_reverse_iterator crbegin() const;
127  [[nodiscard]] const_reverse_iterator rend() const;
128  [[nodiscard]] const_reverse_iterator crend() const;
129 
130  [[nodiscard]] const_iterator begin() const noexcept;
131  [[nodiscard]] const_iterator cbegin() const noexcept;
132  [[nodiscard]] inline const_iterator end() const noexcept;
133  [[nodiscard]] inline const_iterator cend() const noexcept;
134 
135  [[nodiscard]] reference front() const noexcept;
136  [[nodiscard]] reference back() const noexcept;
137 
138  [[nodiscard]] PQXX_PURE size_type size() const noexcept;
139  [[nodiscard]] PQXX_PURE bool empty() const noexcept;
140  [[nodiscard]] size_type capacity() const noexcept { return size(); }
141 
143 
147  void swap(result &) noexcept;
148 
150  [[nodiscard]] row operator[](size_type i) const noexcept;
152  row at(size_type) const;
153 
155 
162  void clear() noexcept
163  {
164  m_data.reset();
165  m_query = nullptr;
166  }
167 
172  [[nodiscard]] PQXX_PURE row_size_type columns() const noexcept;
174 
176  [[nodiscard]] row_size_type column_number(zview name) const;
177 
179  [[nodiscard]] char const *column_name(row_size_type number) const;
180 
182  [[nodiscard]] oid column_type(row_size_type col_num) const;
183 
185  [[nodiscard]] oid column_type(zview col_name) const
186  {
187  return column_type(column_number(col_name));
188  }
189 
191  [[nodiscard]] oid column_table(row_size_type col_num) const;
192 
194  [[nodiscard]] oid column_table(zview col_name) const
195  {
196  return column_table(column_number(col_name));
197  }
198 
200  [[nodiscard]] row_size_type table_column(row_size_type col_num) const;
201 
203  [[nodiscard]] row_size_type table_column(zview col_name) const
204  {
205  return table_column(column_number(col_name));
206  }
208 
210  [[nodiscard]] PQXX_PURE std::string const &query() const noexcept;
211 
213 
216  [[nodiscard]] PQXX_PURE oid inserted_oid() const;
217 
220 
223  [[nodiscard]] PQXX_PURE size_type affected_rows() const;
224 
225 
226 private:
227  using data_pointer = std::shared_ptr<internal::pq::PGresult const>;
228 
230  data_pointer m_data;
231 
233  static data_pointer
234  make_data_pointer(internal::pq::PGresult const *res = nullptr)
235  {
236  return data_pointer{res, internal::clear_result};
237  }
238 
239  friend class pqxx::internal::gate::result_pipeline;
240  PQXX_PURE std::shared_ptr<std::string> query_ptr() const noexcept
241  {
242  return m_query;
243  }
244 
246  std::shared_ptr<std::string> m_query;
247 
248  internal::encoding_group m_encoding;
249 
250  static std::string const s_empty_string;
251 
252  friend class pqxx::field;
253  PQXX_PURE char const *get_value(size_type row, row_size_type col) const;
254  PQXX_PURE bool get_is_null(size_type row, row_size_type col) const;
255  PQXX_PURE
256  field_size_type get_length(size_type, row_size_type) const noexcept;
257 
258  friend class pqxx::internal::gate::result_creation;
259  result(
260  internal::pq::PGresult *rhs, std::shared_ptr<std::string> query,
261  internal::encoding_group enc);
262 
263  PQXX_PRIVATE void check_status(std::string_view desc = ""sv) const;
264 
265  friend class pqxx::internal::gate::result_connection;
266  friend class pqxx::internal::gate::result_row;
267  bool operator!() const noexcept { return m_data.get() == nullptr; }
268  operator bool() const noexcept { return m_data.get() != nullptr; }
269 
270  [[noreturn]] PQXX_PRIVATE void
271  throw_sql_error(std::string const &Err, std::string const &Query) const;
272  PQXX_PRIVATE PQXX_PURE int errorposition() const;
273  PQXX_PRIVATE std::string status_error() const;
274 
275  friend class pqxx::internal::gate::result_sql_cursor;
276  PQXX_PURE char const *cmd_status() const noexcept;
277 };
278 } // namespace pqxx
279 
280 #include "pqxx/internal/compiler-internal-post.hxx"
281 #endif
int result_difference_type
Difference between result sizes.
Definition: types.hxx:21
row_size_type table_column(zview col_name) const
What column in its table did this column come from?
Definition: result.hxx:203
oid column_type(zview col_name) const
Return column&#39;s type, as an OID from the system catalogue.
Definition: result.hxx:185
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
size_type capacity() const noexcept
Definition: result.hxx:140
result_size_type size_type
Definition: result.hxx:73
result() noexcept
Constructor. Do not call this; libpqxx will call it for you.
Definition: result.hxx:84
void clear_result(pq::PGresult const *)
C++ wrapper for libpq&#39;s PQclear.
Definition: result.cxx:37
Reference to a field in a result set.
Definition: field.hxx:33
Iterator for rows in a result. Use as result::const_iterator.
Definition: result_iterator.hxx:35
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
Reference to one row in a result.
Definition: row.hxx:45
Result set containing data returned by a query or command.
Definition: result.hxx:70
bool operator!=(result const &rhs) const noexcept
Compare two results for inequality.
Definition: result.hxx:110
Definition: connection.hxx:94
void clear() noexcept
Let go of the result&#39;s data.
Definition: result.hxx:162
result_difference_type difference_type
Definition: result.hxx:74
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:180
row_size_type col() const noexcept
Definition: field.hxx:261
Internal items for libpqxx&#39; own use. Do not use these yourself.
Definition: composite.hxx:73
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:30
oid column_table(zview col_name) const
What table did this column come from?
Definition: result.hxx:194
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24