mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-01 06:59:42 -06:00
1.4 KiB
1.4 KiB
Enums
Enums can directly be used in sqlgen structs. They are mapped to native enum types in PostgreSQL and MySQL. In sqlite, which does not support native enum types, they are stored as TEXT by default.
Usage
Basic Definition
enum class Color : int { RED = 1, GREEN = 2, BLUE = 3 };
struct Flower {
sqlgen::PrimaryKey<std::string> name;
Color color;
}
const auto red_rose = Flower{
.name = "Rose",
.color = Color::RED,
};
This generates the following SQL schema:
CREATE TYPE IF NOT EXISTS "Color" AS ENUM ('RED', 'GREEN', 'BLUE');
CREATE TABLE IF NOT EXISTS "Flower"(
"name" TEXT NOT NULL,
"color" Color NOT NULL,
PRIMARY_KEY("name")
);
Notes
- Due to naming restrictions in PostgreSQL, the namespace operator
::is replaced with__. Thus, an enum defined asnamespace1::some_struct::MyEnumwill be created in the database asnamespace1__some_struct__MyEnum. This mangled name can at most be 63 characters long. - Enums are specific types in PostgreSQL. They are only created once and shared between tables.
- In MySQL, enums are stored as native types but they are not shared between tables.
- In sqlite, enums are stored as
TEXTby default. If you need to use integers, you can specialize theParser<T>struct as explained here. - You can use
rfl::enum_to_stringandrfl::string_to_enumto convert between enum values and their string representations.