libpqxx  7.0.5
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 
102  [[nodiscard]] const_reverse_iterator rbegin() const;
103  [[nodiscard]] const_reverse_iterator crbegin() const;
104  [[nodiscard]] const_reverse_iterator rend() const;
105  [[nodiscard]] const_reverse_iterator crend() const;
106 
107  [[nodiscard]] const_iterator begin() const noexcept;
108  [[nodiscard]] const_iterator cbegin() const noexcept;
109  [[nodiscard]] inline const_iterator end() const noexcept;
110  [[nodiscard]] inline const_iterator cend() const noexcept;
111 
112  [[nodiscard]] reference front() const noexcept;
113  [[nodiscard]] reference back() const noexcept;
114 
115  [[nodiscard]] PQXX_PURE size_type size() const noexcept;
116  [[nodiscard]] PQXX_PURE bool empty() const noexcept;
117  [[nodiscard]] size_type capacity() const noexcept { return size(); }
118 
119  void swap(result &) noexcept;
120 
121  [[nodiscard]] row operator[](size_type i) const noexcept;
122  row at(size_type) const;
123 
124  void clear() noexcept
125  {
126  m_data.reset();
127  m_query = nullptr;
128  }
129 
134  [[nodiscard]] PQXX_PURE row_size_type columns() const noexcept;
136 
138  row_size_type column_number(char const col_name[]) const;
139 
141  row_size_type column_number(std::string const &name) const
142  {
143  return column_number(name.c_str());
144  }
145 
148  {
149  return column_number(name.c_str());
150  }
151 
153  [[nodiscard]] char const *column_name(row_size_type number) const;
154 
156  [[nodiscard]] oid column_type(row_size_type col_num) const;
157 
159  template<typename STRING>[[nodiscard]] oid column_type(STRING col_name) const
160  {
161  return column_type(column_number(col_name));
162  }
163 
165  [[nodiscard]] oid column_table(row_size_type col_num) const;
166 
168  template<typename STRING>
169  [[nodiscard]] oid column_table(STRING col_name) const
170  {
171  return column_table(column_number(col_name));
172  }
173 
175  [[nodiscard]] row_size_type table_column(row_size_type col_num) const;
176 
178  template<typename STRING>
179  [[nodiscard]] row_size_type table_column(STRING col_name) const
180  {
181  return table_column(column_number(col_name));
182  }
184 
186  [[nodiscard]] PQXX_PURE std::string const &query() const noexcept;
187 
189 
192  [[nodiscard]] PQXX_PURE oid inserted_oid() const;
193 
196 
199  [[nodiscard]] PQXX_PURE size_type affected_rows() const;
200 
201 
202 private:
203  using data_pointer = std::shared_ptr<internal::pq::PGresult const>;
204 
206  data_pointer m_data;
207 
209  static data_pointer
210  make_data_pointer(internal::pq::PGresult const *res = nullptr)
211  {
212  return data_pointer{res, internal::clear_result};
213  }
214 
215  friend class pqxx::internal::gate::result_pipeline;
216  PQXX_PURE std::shared_ptr<std::string> query_ptr() const noexcept
217  {
218  return m_query;
219  }
220 
222  std::shared_ptr<std::string> m_query;
223 
224  internal::encoding_group m_encoding;
225 
226  static std::string const s_empty_string;
227 
228  friend class pqxx::field;
229  PQXX_PURE char const *get_value(size_type row, row_size_type col) const;
230  PQXX_PURE bool get_is_null(size_type row, row_size_type col) const;
231  PQXX_PURE field_size_type get_length(size_type, row_size_type) const
232  noexcept;
233 
234  friend class pqxx::internal::gate::result_creation;
235  result(
236  internal::pq::PGresult *rhs, std::shared_ptr<std::string> query,
237  internal::encoding_group enc);
238 
239  PQXX_PRIVATE void check_status() const;
240 
241  friend class pqxx::internal::gate::result_connection;
242  friend class pqxx::internal::gate::result_row;
243  bool operator!() const noexcept { return m_data.get() == nullptr; }
244  operator bool() const noexcept { return m_data.get() != nullptr; }
245 
246  [[noreturn]] PQXX_PRIVATE void
247  ThrowSQLError(std::string const &Err, std::string const &Query) const;
248  PQXX_PRIVATE PQXX_PURE int errorposition() const;
249  PQXX_PRIVATE std::string StatusError() const;
250 
251  friend class pqxx::internal::gate::result_sql_cursor;
252  PQXX_PURE char const *cmd_status() const noexcept;
253 };
254 } // namespace pqxx
255 
256 #include "pqxx/internal/compiler-internal-post.hxx"
257 #endif
row_size_type column_number(zview name) const
Number of given column (throws exception if it doesn&#39;t exist).
Definition: result.hxx:147
row_size_type col() const noexcept
Definition: field.hxx:215
Private namespace for libpqxx&#39;s internal use; do not access.
Definition: connection.hxx:59
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:35
row_size_type column_number(std::string const &name) const
Number of given column (throws exception if it doesn&#39;t exist).
Definition: result.hxx:141
void clear() noexcept
Definition: result.hxx:124
result() noexcept
Definition: result.hxx:82
oid column_type(STRING col_name) const
Return column&#39;s type, as an OID from the system catalogue.
Definition: result.hxx:159
result_difference_type difference_type
Definition: result.hxx:74
oid column_table(STRING col_name) const
What table did this column come from?
Definition: result.hxx:169
The home of all libpqxx classes, functions, templates, etc.
Definition: array.hxx:25
bool operator!=(result const &rhs) const noexcept
Definition: result.hxx:96
constexpr char const * c_str() const noexcept
Either a null pointer, or a zero-terminated text buffer.
Definition: zview.hxx:41
size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:30
size_type capacity() const noexcept
Definition: result.hxx:117
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:24
row_size_type table_column(STRING col_name) const
What column in its table did this column come from?
Definition: result.hxx:179
Definition: connection.hxx:65
Result set containing data returned by a query or command.
Definition: result.hxx:70
result_size_type size_type
Definition: result.hxx:73
Reference to one row in a result.
Definition: row.hxx:38
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:174
int result_size_type
Number of rows in a result set.
Definition: types.hxx:18
Marker-type wrapper: zero-terminated std::string_view.
Definition: zview.hxx:32
Reference to a field in a result set.
Definition: field.hxx:32
Iterator for rows in a result. Use as result::const_iterator.
Definition: result_iterator.hxx:35