libpqxx
The C++ client library for PostgreSQL
types.hxx
Go to the documentation of this file.
1 /* Basic type aliases and forward declarations.
2  *
3  * Copyright (c) 2000-2026, Jeroen T. Vermeulen
4  *
5  * See COPYING for copyright license. If you did not receive a file called
6  * COPYING with this source code, please notify the distributor of this
7  * mistake, or contact the author.
8  */
9 #ifndef PQXX_TYPES_HXX
10 #define PQXX_TYPES_HXX
11 
12 #if !defined(PQXX_HEADER_PRE)
13 # error "Include libpqxx headers as <pqxx/header>, not <pqxx/header.hxx>."
14 #endif
15 
16 #include <cstddef>
17 #include <cstdint>
18 #include <iterator>
19 #include <ranges>
20 #include <source_location>
21 #include <string>
22 #include <string_view>
23 #include <type_traits>
24 #include <typeinfo>
25 
26 #if defined(PQXX_HAVE_STACKTRACE)
27 # include <stacktrace>
28 #endif
29 
30 #if defined(PQXX_HAVE_TYPE_DISPLAY)
31 # include <meta>
32 #endif
33 
34 
35 namespace pqxx
36 {
38 using sl = std::source_location;
39 
40 #if defined(PQXX_HAVE_STACKTRACE)
42 using st = std::stacktrace;
43 #else
46 {
47  constexpr stacktrace_placeholder() noexcept = default;
48  constexpr stacktrace_placeholder(stacktrace_placeholder const &) noexcept =
49  default;
50 
53  ~stacktrace_placeholder() noexcept = default;
54 
56  operator=(stacktrace_placeholder const &) noexcept = default;
58  operator=(stacktrace_placeholder &&) noexcept = default;
59 
61  [[nodiscard]] PQXX_PURE static constexpr stacktrace_placeholder
62  current() noexcept
63  {
64  return {};
65  }
66 };
69 #endif
70 
71 
73 using oid = unsigned int;
74 
75 
77 using result_size_type = int;
78 
81 
83 using row_size_type = int;
84 
86 using row_difference_type = int;
87 
89 using field_size_type = std::size_t;
90 
92 using large_object_size_type = int64_t;
93 
94 
95 // Forward declarations, to help break compilation dependencies.
96 // These won't necessarily include all classes in libpqxx.
97 class connection;
101 class const_row_iterator;
102 class dbtransaction;
103 // 9.0: Remove this.
104 class errorhandler;
105 class field;
106 class largeobjectaccess;
108 struct range_error;
109 class result;
110 class row;
111 class stream_from;
112 class transaction_base;
113 
115 
120 enum class format : int
121 {
122  text = 0,
123  binary = 1,
124 };
125 
126 
128 
130 template<typename TYPE> using strip_t = std::remove_cvref_t<TYPE>;
131 
132 
134 
137 template<std::ranges::range CONTAINER>
138 using value_type = std::remove_cvref_t<std::ranges::range_value_t<CONTAINER>>;
139 
140 
142 template<typename CHAR>
143 concept char_sized = (sizeof(CHAR) == 1);
144 
145 
147 template<typename STRING>
148 concept char_string = std::ranges::contiguous_range<STRING> and
149  std::same_as<std::remove_cv_t<value_type<STRING>>, char>;
150 
151 
153 template<typename RANGE>
154 concept char_strings = std::ranges::range<RANGE> and
155  char_string<std::remove_cv_t<value_type<RANGE>>>;
156 
157 
159 template<typename DATA>
161  std::ranges::contiguous_range<DATA> and char_sized<value_type<DATA>> and
162  not std::is_reference_v<value_type<DATA>>;
163 
164 
166 
172 template<typename T>
173 concept binary = std::ranges::contiguous_range<T> and
174  std::same_as<std::remove_cv_t<value_type<T>>, std::byte>;
175 
176 
178 template<typename T>
180  std::ranges::range<T> and
181  not std::same_as<
182  std::remove_cvref_t<std::ranges::range_reference_t<T>>, std::byte> and
183  not std::same_as<
184  std::remove_cvref_t<std::ranges::range_reference_t<T>>, char>;
185 
186 
188 using bytes_view = std::span<std::byte const>;
189 
190 
192 using writable_bytes_view = std::span<std::byte>;
193 
194 
196 
205 template<typename T>
206 concept not_borrowed =
207  not std::is_reference_v<T> and not std::is_pointer_v<T> and
208  not std::ranges::borrowed_range<T>;
209 
210 
212 template<typename E>
213 concept enum_type = std::is_enum_v<E>;
214 
215 
217 
219 struct from_table_t final
220 {};
221 
223 
225 struct from_query_t final
226 {};
227 } // namespace pqxx
228 
229 
230 namespace pqxx::internal
231 {
232 #if !defined(PQXX_HAVE_TYPE_DISPLAY)
234 
242 PQXX_LIBEXPORT PQXX_ZARGS std::string
243 demangle_type_name(char const[]) noexcept;
244 #endif
245 } // namespace pqxx::internal
246 
247 
248 namespace pqxx
249 {
251 #if defined(PQXX_HAVE_TYPE_DISPLAY)
253 template<typename TYPE>
254 [[deprecated("Use name_type() instead.")]]
255 std::string const type_name{display_string_of(^^TYPE)};
256 
257 #else
258 
260 
268 template<typename TYPE>
269 [[deprecated("Use name_type() instead.")]]
270 std::string const type_name{
271  pqxx::internal::demangle_type_name(typeid(TYPE).name())};
272 
273 #endif
275 
277 template<typename TYPE> inline constexpr std::string_view name_type() noexcept
278 {
279 #if defined(PQXX_HAVE_TYPE_DISPLAY)
280 
281  return display_string_of(^^TYPE);
282 
283 #else
284 
286  return type_name<TYPE>;
288 
289 #endif
290 }
291 
292 
293 // Specialisations of name_type<>() follow. Some are tested, but because they
294 // are so trivial, the coverage tools never notice that they get tested.
295 // There's just no point running coverage for these.
296 // LCOV_EXCL_START
297 
299 template<>
300 PQXX_PURE constexpr inline std::string_view name_type<std::string>() noexcept
301 {
302  return "std::string";
303 }
305 template<>
306 PQXX_PURE constexpr inline std::string_view
307 name_type<std::string_view>() noexcept
308 {
309  return "std::string_view";
310 }
311 template<>
312 PQXX_PURE constexpr inline std::string_view name_type<char const *>() noexcept
313 {
314  return "char const *";
315 }
317 template<>
318 PQXX_PURE constexpr inline std::string_view name_type<bool>() noexcept
319 {
320  return "bool";
321 }
323 template<>
324 PQXX_PURE constexpr inline std::string_view name_type<short>() noexcept
325 {
326  return "short";
327 }
329 template<>
330 PQXX_PURE constexpr inline std::string_view name_type<int>() noexcept
331 {
332  return "int";
333 }
335 template<>
336 PQXX_PURE constexpr inline std::string_view name_type<long>() noexcept
337 {
338  return "long";
339 }
341 template<>
342 PQXX_PURE constexpr inline std::string_view name_type<long long>() noexcept
343 {
344  return "long long";
345 }
347 template<>
348 PQXX_PURE constexpr inline std::string_view
349 name_type<unsigned short>() noexcept
350 {
351  return "unsigned short";
352 }
354 template<>
355 PQXX_PURE constexpr inline std::string_view name_type<unsigned>() noexcept
356 {
357  return "unsigned";
358 }
360 template<>
361 PQXX_PURE constexpr inline std::string_view name_type<unsigned long>() noexcept
362 {
363  return "unsigned long";
364 }
366 template<>
367 PQXX_PURE constexpr inline std::string_view
369 {
370  return "unsigned long long";
371 }
373 template<>
374 PQXX_PURE constexpr inline std::string_view name_type<float>() noexcept
375 {
376  return "float";
377 }
379 template<>
380 PQXX_PURE constexpr inline std::string_view name_type<double>() noexcept
381 {
382  return "double";
383 }
385 template<>
386 PQXX_PURE constexpr inline std::string_view name_type<long double>() noexcept
387 {
388  return "long double";
389 }
391 template<>
392 PQXX_PURE constexpr inline std::string_view
393 name_type<std::nullptr_t>() noexcept
394 {
395  return "std::nullptr_t";
396 }
397 
398 // LCOV_EXCL_STOP
399 } // namespace pqxx
400 
401 
402 namespace pqxx::internal
403 {
405 template<typename T>
406 concept char_type = std::same_as<std::remove_cv_t<T>, char> or
407  std::same_as<std::remove_cv_t<T>, signed char> or
408  std::same_as<std::remove_cv_t<T>, unsigned char>;
409 
410 
412 
414 template<typename T>
415 concept integer = std::integral<T> and not char_type<T>;
416 } // namespace pqxx::internal
417 
418 
420 
426 namespace pqxx::internal::pq
427 {
429 using PGconn = void;
431 using PGresult = void;
433 using PGnotify = void;
435 using PQnoticeProcessor = void (*)(void *, char const *);
437 using PQconninfoOption = void;
438 } // namespace pqxx::internal::pq
439 #endif
Connection to a database.
Definition: connection.hxx:273
Iterator for rows in a result. Use as result::const_iterator.
Definition: result_iterator.hxx:35
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:173
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition: row.hxx:879
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:748
Definition: errorhandler.hxx:46
Reference to a field in a result set.
Definition: field.hxx:309
Accessor for large object's contents.
Definition: largeobject.hxx:167
Definition: notification.hxx:60
Result set containing data returned by a query or command.
Definition: result.hxx:101
Reference to one row in a result.
Definition: row.hxx:415
Stream data from the database.
Definition: stream_from.hxx:79
Something is out of range, similar to std::out_of_range.
Definition: except.hxx:651
Abstract transaction base class: bracket transactions on the database.
Definition: dbtransaction.hxx:54
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:151
#define PQXX_ZARGS
Definition: header-pre.hxx:136
#define PQXX_LIBEXPORT
Definition: header-pre.hxx:206
#define PQXX_PURE
Definition: header-pre.hxx:64
Placeholders for libpq declarations.
Definition: util.cxx:298
void PQconninfoOption
Placeholder for libpq's PQconninfoOption type.
Definition: types.hxx:437
void PGconn
Placeholder for libpq's connection type.
Definition: types.hxx:429
void PGresult
Placeholder for libpq's result type.
Definition: types.hxx:431
void(*)(void *, char const *) PQnoticeProcessor
Placeholder for libpq's notice processor type.
Definition: types.hxx:435
void PGnotify
Placeholder for libpq's notification type.
Definition: types.hxx:433
Private namespace for libpqxx's internal use; do not access.
Definition: connection.cxx:333
PQXX_LIBEXPORT PQXX_ZARGS std::string demangle_type_name(char const[]) noexcept
Attempt to demangle std::type_info::name() to something human-readable.
Definition: types.cxx:28
concept integer
Concept: an integral number type.
Definition: types.hxx:415
concept char_type
Concept: one of the "char" types.
Definition: types.hxx:406
The home of all libpqxx classes, functions, templates, etc.
Definition: array.cxx:26
std::string const type_name
A human-readable name for a type, used in error messages and such.
Definition: types.hxx:270
std::span< std::byte const > bytes_view
Type alias for a view of bytes.
Definition: types.hxx:188
concept not_borrowed
Concept: A value that's not just a reference to values elsewhere.
Definition: types.hxx:206
constexpr PQXX_PURE std::string_view name_type< long long >() noexcept
Specialisation to save on startup work.
Definition: types.hxx:342
concept enum_type
Concept: A C++ enum type.
Definition: types.hxx:213
constexpr PQXX_PURE std::string_view name_type< double >() noexcept
Specialisation to save on startup work.
Definition: types.hxx:380
constexpr PQXX_PURE std::string_view name_type< float >() noexcept
Specialisation to save on startup work.
Definition: types.hxx:374
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:83
constexpr PQXX_PURE std::string_view name_type< short >() noexcept
Specialisation to save on startup work.
Definition: types.hxx:324
int row_difference_type
Difference between row sizes.
Definition: types.hxx:86
constexpr PQXX_PURE std::string_view name_type< int >() noexcept
Specialisation to save on startup work.
Definition: types.hxx:330
std::remove_cvref_t< TYPE > strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: types.hxx:130
std::source_location sl
Convenience alias for std::source_location. It's just too long.
Definition: types.hxx:38
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:89
constexpr std::string_view name_type() noexcept
Return human-readable name for TYPE.
Definition: types.hxx:277
concept nonbinary_range
A series of something that's not bytes.
Definition: types.hxx:179
int result_difference_type
Difference between result sizes.
Definition: types.hxx:80
constexpr PQXX_PURE std::string_view name_type< unsigned >() noexcept
Specialisation to save on startup work.
Definition: types.hxx:355
constexpr PQXX_PURE std::string_view name_type< bool >() noexcept
Specialisation to save on startup work.
Definition: types.hxx:318
std::span< std::byte > writable_bytes_view
Type alias for a view of writable bytes.
Definition: types.hxx:192
concept potential_binary
Concept: Anything we might want to treat as binary data.
Definition: types.hxx:160
int result_size_type
Number of rows in a result set.
Definition: types.hxx:77
concept binary
Concept: Binary string, akin to std::string for binary data.
Definition: types.hxx:173
std::remove_cvref_t< std::ranges::range_value_t< CONTAINER > > value_type
The type of a container's elements.
Definition: types.hxx:138
constexpr PQXX_PURE std::string_view name_type< unsigned short >() noexcept
Specialisation to save on startup work.
Definition: types.hxx:349
constexpr PQXX_PURE std::string_view name_type< long >() noexcept
Specialisation to save on startup work.
Definition: types.hxx:336
concept char_sized
A type one byte in size.
Definition: types.hxx:143
constexpr PQXX_PURE std::string_view name_type< unsigned long long >() noexcept
Specialisation to save on startup work.
Definition: types.hxx:368
unsigned int oid
PostgreSQL database row identifier.
Definition: types.hxx:73
constexpr PQXX_PURE std::string_view name_type< unsigned long >() noexcept
Specialisation to save on startup work.
Definition: types.hxx:361
constexpr PQXX_PURE std::string_view name_type< long double >() noexcept
Specialisation to save on startup work.
Definition: types.hxx:386
concept char_strings
Concept: Anything we can iterate to get things we can read as strings.
Definition: types.hxx:154
concept char_string
Concept: Any type that we can read as a string of char.
Definition: types.hxx:148
constexpr PQXX_PURE std::string_view name_type< char const * >() noexcept
Definition: types.hxx:312
int64_t large_object_size_type
Number of bytes in a large object.
Definition: types.hxx:92
format
Format code: is data text or binary?
Definition: types.hxx:121
Marker for stream_from constructors: "stream from query.".
Definition: types.hxx:226
Marker for stream_from constructors: "stream from table.".
Definition: types.hxx:220
There is no std::stacktrace on this system. Use a placeholder.
Definition: types.hxx:46
static constexpr PQXX_PURE stacktrace_placeholder current() noexcept
Placeholder for std::stacktrace::current().
Definition: types.hxx:62
~stacktrace_placeholder() noexcept=default
constexpr stacktrace_placeholder() noexcept=default