libpqxx  7.3.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-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_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  result() noexcept :
83  m_data(make_data_pointer()),
84  m_query(),
85  m_encoding(internal::encoding_group::MONOBYTE)
86  {}
87  result(result const &rhs) noexcept = default;
88 
89  result &operator=(result const &rhs) noexcept = default;
90 
95  [[nodiscard]] bool operator==(result const &) const noexcept;
96  [[nodiscard]] bool operator!=(result const &rhs) const noexcept
97  {
98  return not operator==(rhs);
99  }
101 
103 
109  template<typename... TYPE> auto iter() const;
110 
111  [[nodiscard]] const_reverse_iterator rbegin() const;
112  [[nodiscard]] const_reverse_iterator crbegin() const;
113  [[nodiscard]] const_reverse_iterator rend() const;
114  [[nodiscard]] const_reverse_iterator crend() const;
115 
116  [[nodiscard]] const_iterator begin() const noexcept;
117  [[nodiscard]] const_iterator cbegin() const noexcept;
118  [[nodiscard]] inline const_iterator end() const noexcept;
119  [[nodiscard]] inline const_iterator cend() const noexcept;
120 
121  [[nodiscard]] reference front() const noexcept;
122  [[nodiscard]] reference back() const noexcept;
123 
124  [[nodiscard]] PQXX_PURE size_type size() const noexcept;
125  [[nodiscard]] PQXX_PURE bool empty() const noexcept;
126  [[nodiscard]] size_type capacity() const noexcept { return size(); }
127 
128  void swap(result &) noexcept;
129 
130  [[nodiscard]] row operator[](size_type i) const noexcept;
131  row at(size_type) const;
132 
133  void clear() noexcept
134  {
135  m_data.reset();
136  m_query = nullptr;
137  }
138 
143  [[nodiscard]] PQXX_PURE row_size_type columns() const noexcept;
145 
147  [[nodiscard]] row_size_type column_number(zview name) const;
148 
150  [[nodiscard]] char const *column_name(row_size_type number) const;
151 
153  [[nodiscard]] oid column_type(row_size_type col_num) const;
154 
156  [[nodiscard]] oid column_type(zview col_name) const
157  {
158  return column_type(column_number(col_name));
159  }
160 
162  [[nodiscard]] oid column_table(row_size_type col_num) const;
163 
165  [[nodiscard]] oid column_table(zview col_name) const
166  {
167  return column_table(column_number(col_name));
168  }
169 
171  [[nodiscard]] row_size_type table_column(row_size_type col_num) const;
172 
174  [[nodiscard]] row_size_type table_column(zview col_name) const
175  {
176  return table_column(column_number(col_name));
177  }
179 
181  [[nodiscard]] PQXX_PURE std::string const &query() const noexcept;
182 
184 
187  [[nodiscard]] PQXX_PURE oid inserted_oid() const;
188 
191 
194  [[nodiscard]] PQXX_PURE size_type affected_rows() const;
195 
196 
197 private:
198  using data_pointer = std::shared_ptr<internal::pq::PGresult const>;
199 
201  data_pointer m_data;
202 
204  static data_pointer
205  make_data_pointer(internal::pq::PGresult const *res = nullptr)
206  {
207  return data_pointer{res, internal::clear_result};
208  }
209 
210  friend class pqxx::internal::gate::result_pipeline;
211  PQXX_PURE std::shared_ptr<std::string> query_ptr() const noexcept
212  {
213  return m_query;
214  }
215 
217  std::shared_ptr<std::string> m_query;
218 
219  internal::encoding_group m_encoding;
220 
221  static std::string const s_empty_string;
222 
223  friend class pqxx::field;
224  PQXX_PURE char const *get_value(size_type row, row_size_type col) const;
225  PQXX_PURE bool get_is_null(size_type row, row_size_type col) const;
226  PQXX_PURE
227  field_size_type get_length(size_type, row_size_type) const noexcept;
228 
229  friend class pqxx::internal::gate::result_creation;
230  result(
231  internal::pq::PGresult *rhs, std::shared_ptr<std::string> query,
232  internal::encoding_group enc);
233 
234  PQXX_PRIVATE void check_status() const;
235 
236  friend class pqxx::internal::gate::result_connection;
237  friend class pqxx::internal::gate::result_row;
238  bool operator!() const noexcept { return m_data.get() == nullptr; }
239  operator bool() const noexcept { return m_data.get() != nullptr; }
240 
241  [[noreturn]] PQXX_PRIVATE void
242  throw_sql_error(std::string const &Err, std::string const &Query) const;
243  PQXX_PRIVATE PQXX_PURE int errorposition() const;
244  PQXX_PRIVATE std::string status_error() const;
245 
246  friend class pqxx::internal::gate::result_sql_cursor;
247  PQXX_PURE char const *cmd_status() const noexcept;
248 };
249 } // namespace pqxx
250 
251 #include "pqxx/internal/compiler-internal-post.hxx"
252 #endif
oid column_type(zview col_name) const
Return column&#39;s type, as an OID from the system catalogue.
Definition: result.hxx:156
Internal items for libpqxx&#39; own use. Do not use these yourself.
Definition: composite.hxx:73
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:174
int result_difference_type
Difference between result sizes.
Definition: types.hxx:21
void clear_result(pq::PGresult const *)
C++ wrapper for libpq&#39;s PQclear.
Definition: result.cxx:37
result() noexcept
Definition: result.hxx:82
result_difference_type difference_type
Definition: result.hxx:74
Iterator for rows in a result. Use as result::const_iterator.
Definition: result_iterator.hxx:35
row_size_type col() const noexcept
Definition: field.hxx:257
Reference to one row in a result.
Definition: row.hxx:45
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
size_type capacity() const noexcept
Definition: result.hxx:126
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:30
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
bool operator!=(result const &rhs) const noexcept
Definition: result.hxx:96
result_size_type size_type
Definition: result.hxx:73
oid column_table(zview col_name) const
What table did this column come from?
Definition: result.hxx:165
Definition: connection.hxx:94
row_size_type table_column(zview col_name) const
What column in its table did this column come from?
Definition: result.hxx:174
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:37
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
Reference to a field in a result set.
Definition: field.hxx:33
Result set containing data returned by a query or command.
Definition: result.hxx:70
void clear() noexcept
Definition: result.hxx:133