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 <string>
20 #include <string_view>
21 #include <type_traits>
22 #include <typeinfo>
23 #include <utility>
24 
25 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
26 # include <ranges>
27 #endif
28 
29 
30 namespace pqxx::internal
31 {
33 PQXX_LIBEXPORT std::string demangle_type_name(char const[]);
34 } // namespace pqxx::internal
35 
36 
37 namespace pqxx
38 {
40 using result_size_type = int;
41 
44 
46 using row_size_type = int;
47 
49 using row_difference_type = int;
50 
52 using field_size_type = std::size_t;
53 
55 using large_object_size_type = int64_t;
56 
57 
58 // Forward declarations, to help break compilation dependencies.
59 // These won't necessarily include all classes in libpqxx.
60 class binarystring;
61 class connection;
65 class const_row_iterator;
66 class dbtransaction;
67 // 9.0: Remove this.
68 class errorhandler;
69 class field;
70 class largeobjectaccess;
72 struct range_error;
73 class result;
74 class row;
75 class stream_from;
76 class transaction_base;
77 
79 
81 enum class format : int
82 {
83  text = 0,
84  binary = 1,
85 };
86 
87 
89 
91 template<typename TYPE>
92 using strip_t = std::remove_cv_t<std::remove_reference_t<TYPE>>;
93 
94 
95 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
96 
100 template<std::ranges::range CONTAINER>
102 #else // PQXX_HAVE_CONCEPTS
103 
107 template<typename CONTAINER>
109 #endif // PQXX_HAVE_CONCEPTS
110 
111 
112 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
113 template<typename STRING>
115 concept char_string = std::ranges::contiguous_range<STRING> and
116  std::same_as<strip_t<value_type<STRING>>, char>;
117 
119 template<typename RANGE>
120 concept char_strings =
121  std::ranges::range<RANGE> and char_string<strip_t<value_type<RANGE>>>;
122 
124 template<typename DATA>
125 concept potential_binary =
126  std::ranges::contiguous_range<DATA> and (sizeof(value_type<DATA>) == 1);
127 #endif // PQXX_HAVE_CONCEPTS
128 
129 
130 // C++20: Retire these compatibility definitions.
131 #if defined(PQXX_HAVE_CONCEPTS) && defined(PQXX_HAVE_RANGES)
132 
134 
137 # define PQXX_RANGE_ARG std::ranges::range
138 
140 
143 # define PQXX_CHAR_STRING_ARG pqxx::char_string
144 
146 
149 # define PQXX_CHAR_STRINGS_ARG pqxx::char_strings
150 
151 #else // PQXX_HAVE_CONCEPTS
152 
154 
157 # define PQXX_RANGE_ARG typename
158 
160 
163 # define PQXX_CHAR_STRING_ARG typename
164 
166 
169 # define PQXX_CHAR_STRINGS_ARG typename
170 
171 #endif // PQXX_HAVE_CONCEPTS
172 
174 
177 {};
178 
180 
183 {};
184 
185 
187 
195 template<typename TYPE>
196 std::string const type_name{internal::demangle_type_name(typeid(TYPE).name())};
197 
198 
200 template<typename TYPE> inline constexpr std::string_view name_type() noexcept
201 {
202  return type_name<TYPE>;
203 }
204 
205 
206 // Specialisations of name_type<>() follow. This may avoid problems with gcc
207 // 15, where definitions of `type_name<...>` (the templated global variable)
208 // in the library and in the headers are not considered identical, leading to
209 // a double free.
210 //
211 // We work around this by specialising the function for the built-in types, so
212 // that we may never need type_name.
213 
214 template<>
215 PQXX_PURE constexpr inline std::string_view name_type<std::string>() noexcept
216 {
217  return "std::string";
218 }
219 template<>
220 PQXX_PURE constexpr inline std::string_view
221 name_type<std::string_view>() noexcept
222 {
223  return "std::string_view";
224 }
225 template<>
226 PQXX_PURE constexpr inline std::string_view name_type<char const *>() noexcept
227 {
228  return "char const *";
229 }
230 template<>
231 PQXX_PURE constexpr inline std::string_view name_type<bool>() noexcept
232 {
233  return "bool";
234 }
235 template<>
236 PQXX_PURE constexpr inline std::string_view name_type<short>() noexcept
237 {
238  return "short";
239 }
240 template<>
241 PQXX_PURE constexpr inline std::string_view name_type<int>() noexcept
242 {
243  return "int";
244 }
245 template<>
246 PQXX_PURE constexpr inline std::string_view name_type<long>() noexcept
247 {
248  return "long";
249 }
250 template<>
251 PQXX_PURE constexpr inline std::string_view name_type<long long>() noexcept
252 {
253  return "long long";
254 }
255 template<>
256 PQXX_PURE constexpr inline std::string_view
258 {
259  return "unsigned short";
260 }
261 template<>
262 PQXX_PURE constexpr inline std::string_view name_type<unsigned>() noexcept
263 {
264  return "unsigned";
265 }
266 template<>
267 PQXX_PURE constexpr inline std::string_view name_type<unsigned long>() noexcept
268 {
269  return "unsigned long";
270 }
271 template<>
272 PQXX_PURE constexpr inline std::string_view
274 {
275  return "unsigned long long";
276 }
277 template<>
278 PQXX_PURE constexpr inline std::string_view name_type<float>() noexcept
279 {
280  return "float";
281 }
282 template<>
283 PQXX_PURE constexpr inline std::string_view name_type<double>() noexcept
284 {
285  return "double";
286 }
287 template<>
288 PQXX_PURE constexpr inline std::string_view name_type<long double>() noexcept
289 {
290  return "long double";
291 }
292 template<>
293 PQXX_PURE constexpr inline std::string_view
294 name_type<std::nullptr_t>() noexcept
295 {
296  return "std::nullptr_t";
297 }
298 
299 } // namespace pqxx
300 #endif
PQXX_PURE constexpr std::string_view name_type< char const * >() noexcept
Definition: types.hxx:226
Accessor for large object's contents.
Definition: largeobject.hxx:153
PQXX_PURE constexpr std::string_view name_type< long long >() noexcept
Definition: types.hxx:251
Marker for stream_from constructors: "stream from query.".
Definition: types.hxx:182
#define PQXX_PURE
Declare function "pure": no side effects, only reads globals and its args.
Definition: header-pre.hxx:77
PQXX_PURE constexpr std::string_view name_type< short >() noexcept
Definition: types.hxx:236
Internal items for libpqxx' own use. Do not use these yourself.
Definition: encodings.cxx:32
PQXX_PURE constexpr std::string_view name_type< unsigned short >() noexcept
Definition: types.hxx:257
Reference to one row in a result.
Definition: row.hxx:46
PQXX_PURE constexpr std::string_view name_type< bool >() noexcept
Definition: types.hxx:231
PQXX_PURE constexpr std::string_view name_type< float >() noexcept
Definition: types.hxx:278
Something is out of range, similar to std::out_of_range.
Definition: except.hxx:325
constexpr std::string_view name_type() noexcept
Return human-readable name for TYPE.
Definition: types.hxx:200
int row_size_type
Number of fields in a row of database data.
Definition: types.hxx:46
Result set containing data returned by a query or command.
Definition: result.hxx:91
format
Format code: is data text or binary?
Definition: types.hxx:81
int result_size_type
Number of rows in a result set.
Definition: types.hxx:40
PQXX_PURE constexpr std::string_view name_type< unsigned long long >() noexcept
Definition: types.hxx:273
PQXX_PURE constexpr std::string_view name_type< double >() noexcept
Definition: types.hxx:283
PQXX_PURE constexpr std::string_view name_type< unsigned long >() noexcept
Definition: types.hxx:267
Abstract transaction base class: bracket transactions on the database.
Definition: dbtransaction.hxx:53
Definition: notification.hxx:56
int result_difference_type
Difference between result sizes.
Definition: types.hxx:43
Stream data from the database.
Definition: stream_from.hxx:78
Marker for stream_from constructors: "stream from table.".
Definition: types.hxx:176
Binary data corresponding to PostgreSQL's "BYTEA" binary-string type.
Definition: binarystring.hxx:57
#define PQXX_LIBEXPORT
Definition: header-pre.hxx:157
PQXX_PURE constexpr std::string_view name_type< long >() noexcept
Definition: types.hxx:246
Definition: errorhandler.hxx:45
int64_t large_object_size_type
Number of bytes in a large object.
Definition: types.hxx:55
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:196
Reference to a field in a result set.
Definition: field.hxx:34
int row_difference_type
Difference between row sizes.
Definition: types.hxx:49
Reverse iterator for a row. Use as row::const_reverse_iterator.
Definition: row.hxx:414
Iterator for rows in a result. Use as result::const_iterator.
Definition: result_iterator.hxx:32
PQXX_PURE constexpr std::string_view name_type< int >() noexcept
Definition: types.hxx:241
Iterator for fields in a row. Use as row::const_iterator.
Definition: row.hxx:294
strip_t< decltype(*std::begin(std::declval< CONTAINER >()))> value_type
The type of a container's elements.
Definition: types.hxx:108
std::size_t field_size_type
Number of bytes in a field of database data.
Definition: types.hxx:52
PQXX_PURE constexpr std::string_view name_type< long double >() noexcept
Definition: types.hxx:288
PQXX_PURE constexpr std::string_view name_type< unsigned >() noexcept
Definition: types.hxx:262
std::string demangle_type_name(char const raw[])
Attempt to demangle std::type_info::name() to something human-readable.
Definition: strconv.cxx:228
Connection to a database.
Definition: connection.hxx:278
Reverse iterator for result. Use as result::const_reverse_iterator.
Definition: result_iterator.hxx:194
std::remove_cv_t< std::remove_reference_t< TYPE >> strip_t
Remove any constness, volatile, and reference-ness from a type.
Definition: types.hxx:92
Interface definition (and common code) for "transaction" classes.
Definition: transaction_base.hxx:150