userver: uPg: Mapping a C++ type to PostgreSQL user type
Loading...
Searching...
No Matches
uPg: Mapping a C++ type to PostgreSQL user type

In PosgtgreSQL the following kinds of user types are available:

Domains are essentially some data types with database constraints applied to them, they map to their base data types' C++ counterparts.

Other user types can be mapped to C++ types, more information on defining a mapped C++ type can be found on respective pages. After a C++ type is defined, it must be mapped to it's PostgreSQL counterpart by specialising CppToUserPg template for the type. C++ types are mapped to PostgreSQL types by their names, so the specialization for CppToUserPg template must have a static constexpr member of type DBTypeName named postgres_name.

C++ type
namespace pgtest {
struct FooBar {
pg::Integer i;
std::string s;
double d;
};
} // namespace pgtest
Declaring C++ type to PostgreSQL type mapping
Warning
The type mapping specialization must be accessible at the points where parsing/formatting of the C++ type is instantiated. The header where the C++ type is declared is an appropriate place to do it.
// This specialization MUST go to the header together with the mapped type
template <>
struct CppToUserPg<pgtest::FooBar> {
static constexpr DBTypeName postgres_name = "__pgtest.foobar";
};
} // namespace storages::postgres::io

A connection gets the data types' definitions after connect and uses the definitions to map C++ types to PostgreSQL type oids.