userver: storages::postgres::io::Codegen Struct Reference
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
storages::postgres::io::Codegen Struct Reference

Your opinion will help to improve our service

Leave a feedback >

Table of Contents

#include <userver/storages/postgres/io/enum_types.hpp>

Detailed Description

Helper for enumerators that use Parse and ToString functions for conversions, mostly for autogenerated enums from chaotic.

A generated enum:

// Autogenerated enum with formatters and parsers
enum class GeneratedRainbow { kRed, kOrange, kYellow, kGreen, kCyan };
GeneratedRainbow Parse(std::string_view value, formats::parse::To<GeneratedRainbow>);
std::string ToString(GeneratedRainbow value);

can be registered in the following way

template <>
struct storages::postgres::io::CppToUserPg<GeneratedRainbow> : EnumMappingBase<GeneratedRainbow> {
static constexpr DBTypeName postgres_name = "__pgtest.rainbow";
static constexpr auto enumerators = storages::postgres::io::Codegen{};
};

After that the enum is mapped to the PostgreSQL type that can be created in the following way:

CREATE TYPE __pgtest.rainbow AS enum (
'red', 'orange', 'yellow', 'green', 'cyan'
)

Use it as usual:

auto res = GetConn()->Execute("select 'red'::__pgtest.rainbow");
EXPECT_EQ(res[0][0].As<GeneratedRainbow>(), GeneratedRainbow::kRed);
res = GetConn()->Execute("select $1", GeneratedRainbow::kYellow);
EXPECT_EQ(res[0][0].As<GeneratedRainbow>(), GeneratedRainbow::kYellow);

Definition at line 70 of file enum_types.hpp.


The documentation for this struct was generated from the following file: