libpqxx
The C++ client library for PostgreSQL
Loading...
Searching...
No Matches
result.hxx
Go to the documentation of this file.
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-2025, 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 <list>
23#include <memory>
24#include <optional>
25#include <stdexcept>
26
27#include "pqxx/except.hxx"
28#include "pqxx/types.hxx"
29#include "pqxx/util.hxx"
30#include "pqxx/zview.hxx"
31
33
34
35namespace pqxx::internal
36{
37PQXX_LIBEXPORT void clear_result(pq::PGresult const *) noexcept;
38} // namespace pqxx::internal
39
40
42{
44class result_creation;
45class result_pipeline;
46class result_row;
48} // namespace pqxx::internal::gate
49
50
51namespace pqxx::internal
52{
53// 9.0: Remove this, just use the notice handler in connection/result.
56{
57 std::function<void(zview)> notice_handler;
58 std::list<errorhandler *> errorhandlers;
59
60 notice_waiters() = default;
61 notice_waiters(notice_waiters const &) = delete;
65};
66} // namespace pqxx::internal
67
68
69namespace pqxx
70{
72
93{
94public:
97 using reference = row;
98 using const_iterator = const_result_iterator;
101 using const_reverse_iterator = const_reverse_result_iterator;
103
104 result() noexcept :
105 m_data{}, m_query{}, m_encoding{internal::encoding_group::MONOBYTE}
106 {}
107
108 result(result const &rhs) noexcept = default;
109 result(result &&rhs) noexcept = default;
110
112
115 result &operator=(result const &rhs) noexcept = default;
116
118 result &operator=(result &&rhs) noexcept = default;
119
128
129 [[nodiscard]] bool operator==(result const &) const noexcept;
131 [[nodiscard]] bool operator!=(result const &rhs) const noexcept
132 {
133 return not operator==(rhs);
134 }
135
136
138
144 template<typename... TYPE> auto iter() const;
145
146 [[nodiscard]] const_reverse_iterator rbegin() const;
147 [[nodiscard]] const_reverse_iterator crbegin() const;
148 [[nodiscard]] const_reverse_iterator rend() const;
149 [[nodiscard]] const_reverse_iterator crend() const;
150
151 [[nodiscard]] const_iterator begin() const noexcept;
152 [[nodiscard]] const_iterator cbegin() const noexcept;
153 [[nodiscard]] inline const_iterator end() const noexcept;
154 [[nodiscard]] inline const_iterator cend() const noexcept;
155
156 [[nodiscard]] reference front() const noexcept;
157 [[nodiscard]] reference back() const noexcept;
158
159 [[nodiscard]] PQXX_PURE size_type size() const noexcept;
160 [[nodiscard]] PQXX_PURE bool empty() const noexcept;
161 [[nodiscard]] size_type capacity() const noexcept { return size(); }
162
164
168 void swap(result &) noexcept;
169
171
175 [[nodiscard]] row operator[](size_type i) const noexcept;
176
177#if defined(PQXX_HAVE_MULTIDIM)
178 [[nodiscard]] field
179 operator[](size_type row_num, row_size_type col_num) const noexcept;
180#endif // PQXX_HAVE_MULTIDIM
181
183 row at(size_type) const;
184
186 field at(size_type, row_size_type) const;
187
189
196 void clear() noexcept
197 {
198 m_data.reset();
199 m_query = nullptr;
200 }
201
206
207 [[nodiscard]] PQXX_PURE row_size_type columns() const noexcept;
208
210 [[nodiscard]] row_size_type column_number(zview name) const;
211
213 [[nodiscard]] char const *column_name(row_size_type number) const &;
214
216
219 [[nodiscard]] int column_storage(row_size_type number) const;
220
222
232 [[nodiscard]] int column_type_modifier(row_size_type number) const noexcept;
233
235 [[nodiscard]] oid column_type(row_size_type col_num) const;
236
238 [[nodiscard]] oid column_type(zview col_name) const
239 {
240 return column_type(column_number(col_name));
241 }
242
244 [[nodiscard]] oid column_table(row_size_type col_num) const;
245
247 [[nodiscard]] oid column_table(zview col_name) const
248 {
249 return column_table(column_number(col_name));
250 }
251
253 [[nodiscard]] row_size_type table_column(row_size_type col_num) const;
254
256 [[nodiscard]] row_size_type table_column(zview col_name) const
257 {
258 return table_column(column_number(col_name));
259 }
260
261
263 [[nodiscard]] PQXX_PURE std::string const &query() const & noexcept;
264
266
269 [[nodiscard]] PQXX_PURE oid inserted_oid() const;
270
272
275 [[nodiscard]] PQXX_PURE size_type affected_rows() const;
276
277 // C++20: Concept like std::invocable, but without specifying param types.
279
316 template<typename CALLABLE> inline void for_each(CALLABLE &&func) const;
317
319
323 {
324 auto const sz{size()};
325 if (sz != n)
326 {
327 // TODO: See whether result contains a generated statement.
328 if (not m_query or m_query->empty())
330 "Expected ", n, " row(s) from query, got ", sz, ".")};
331 else
333 "Expected ", n, " row(s) from query '", *m_query, "', got ", sz,
334 ".")};
335 }
336 return *this;
337 }
338
340
343 row one_row() const;
344
346
351 std::optional<row> opt_row() const;
352
355 {
356 expect_rows(0);
357 return *this;
358 }
359
361
365 {
366 auto const actual{columns()};
367 if (actual != cols)
368 {
369 // TODO: See whether result contains a generated statement.
370 if (not m_query or m_query->empty())
372 "Expected ", cols, " column(s) from query, got ", actual, ".")};
373 else
375 "Expected ", cols, " column(s) from query '", *m_query, "', got ",
376 actual, ".")};
377 }
378 return *this;
379 }
380
382
385 field one_field() const;
386
387private:
388 using data_pointer = std::shared_ptr<internal::pq::PGresult const>;
389
391 data_pointer m_data;
392
394 PQXX_PURE std::shared_ptr<std::string const> query_ptr() const noexcept
395 {
396 return m_query;
397 }
398
400 std::shared_ptr<std::string const> m_query;
401
403
407 std::shared_ptr<pqxx::internal::notice_waiters> m_notice_waiters;
408
409 internal::encoding_group m_encoding;
410
411 static std::string const s_empty_string;
412
413 friend class pqxx::field;
414 PQXX_PURE char const *
415 get_value(size_type row, row_size_type col) const noexcept;
416 PQXX_PURE bool get_is_null(size_type row, row_size_type col) const noexcept;
418 field_size_type get_length(size_type, row_size_type) const noexcept;
419
421 result(
422 std::shared_ptr<internal::pq::PGresult> const &rhs,
423 std::shared_ptr<std::string> const &query,
424 std::shared_ptr<pqxx::internal::notice_waiters> const &waiters,
426
427 PQXX_PRIVATE void check_status(std::string_view desc = ""sv) const;
428
430 friend class pqxx::internal::gate::result_row;
431 bool operator!() const noexcept { return m_data.get() == nullptr; }
432 operator bool() const noexcept { return m_data.get() != nullptr; }
433
434 [[noreturn]] PQXX_PRIVATE PQXX_COLD void
435 throw_sql_error(std::string const &Err, std::string const &Query) const;
436 PQXX_PRIVATE PQXX_PURE int errorposition() const;
437 PQXX_PRIVATE std::string status_error() const;
438
440 PQXX_PURE char const *cmd_status() const noexcept;
441};
442} // namespace pqxx
443#endif
Definition result-connection.hxx:6
Definition result-creation.hxx:6
Definition result-pipeline.hxx:6
Definition result-sql_cursor.hxx:6
Result set containing data returned by a query or command.
Definition result.hxx:93
PQXX_PURE row_size_type columns() const noexcept
Number of columns in result.
Definition result.cxx:514
const_reverse_result_iterator const_reverse_iterator
Definition result.hxx:101
oid column_type(row_size_type col_num) const
Return column's type, as an OID from the system catalogue.
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:256
row reference
Definition result.hxx:97
result() noexcept
Definition result.hxx:104
result & operator=(result &&rhs) noexcept=default
Assign one result to another, invaliding the old one.
result_size_type size_type
Definition result.hxx:95
bool operator==(result const &) const noexcept
Compare two results for equality.
Definition result.cxx:65
oid column_table(row_size_type col_num) const
What table did this column come from?
bool operator!=(result const &rhs) const noexcept
Compare two results for inequality.
Definition result.hxx:131
result expect_rows(size_type n) const
Check that result contains exactly n rows.
Definition result.hxx:322
const_iterator pointer
Definition result.hxx:99
void clear() noexcept
Let go of the result's data.
Definition result.hxx:196
row_size_type table_column(row_size_type col_num) const
What column in its table did this column come from?
const_iterator iterator
Definition result.hxx:100
row_size_type column_number(zview name) const
Number of given column (throws exception if it doesn't exist).
Definition result.cxx:434
result_difference_type difference_type
Definition result.hxx:96
size_type capacity() const noexcept
Definition result.hxx:161
PQXX_PURE std::string const & query() const &noexcept
Query that produced this result, if available (empty string otherwise).
Definition result.cxx:372
PQXX_PURE size_type size() const noexcept
Definition result.cxx:115
result expect_columns(row_size_type cols) const
Expect that result consists of exactly cols columns.
Definition result.hxx:364
const_reverse_iterator reverse_iterator
Definition result.hxx:102
oid column_table(zview col_name) const
What table did this column come from?
Definition result.hxx:247
result & operator=(result const &rhs) noexcept=default
Assign one result to another.
const_result_iterator const_iterator
Definition result.hxx:98
result no_rows() const
Expect that result contains no rows. Return result for convenience.
Definition result.hxx:354
result(result const &rhs) noexcept=default
Marker-type wrapper: zero-terminated std::string_view.
Definition zview.hxx:38
Query returned an unexpected number of rows.
Definition except.hxx:343
Error in usage of libpqxx library, similar to std::logic_error.
Definition except.hxx:249
#define PQXX_COLD
Definition header-pre.hxx:87
#define PQXX_LIBEXPORT
Definition header-pre.hxx:157
#define PQXX_PURE
Declare function "pure": no side effects, only reads globals and its args.
Definition header-pre.hxx:77
#define PQXX_PRIVATE
Definition header-pre.hxx:158
Definition connection.hxx:108
pg_result PGresult
Definition libpq-forward.hxx:25
Internal items for libpqxx' own use. Do not use these yourself.
Definition encodings.cxx:33
std::string concat(TYPE... item)
Efficiently combine a bunch of items into one big string.
Definition concat.hxx:31
encoding_group
Definition encoding_group.hxx:19
PQXX_LIBEXPORT void clear_result(pq::PGresult const *) noexcept
C++ wrapper for libpq's PQclear.
Definition result.cxx:43
The home of all libpqxx classes, functions, templates, etc.
Definition array.cxx:27
unsigned int oid
PostgreSQL database row identifier.
Definition libpq-forward.hxx:33
std::size_t field_size_type
Number of bytes in a field of database data.
Definition types.hxx:40
int result_size_type
Number of rows in a result set.
Definition types.hxx:28
int row_size_type
Number of fields in a row of database data.
Definition types.hxx:34
int result_difference_type
Difference between result sizes.
Definition types.hxx:31
std::function< void(zview)> notice_handler
Definition result.hxx:57
notice_waiters & operator=(notice_waiters &&)=delete
notice_waiters(notice_waiters &&)=delete
notice_waiters & operator=(notice_waiters const &)=delete
notice_waiters(notice_waiters const &)=delete
std::list< errorhandler * > errorhandlers
Definition result.hxx:58