Available in versions: Dev (3.21) | Latest (3.20) | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11

This documentation is for the unreleased development version of jOOQ. Click on the above version links to get this documentation for a supported version of jOOQ.

Enum data types

Supported by ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

Enumerated data types are a vendor specific SQL feature by some RDBMS that allow for adding a constraint like restriction to a VARCHAR column. PostgreSQL style enum types can be created using the CREATE TYPE .. AS ENUM statement.

There are mainly two flavours:

// PostgreSQL: Explicit, reusable types
CREATE TYPE day_of_week AS ENUM ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
CREATE TABLE t (day_of_week day_of_week);

// MySQL: Implicit, ad-hoc types
CREATE TABLE t (day_of_week ENUM ('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'));

In both cases, a org.jooq.EnumType class is generated by the code generator, containing type information as well as the enum literals.

public enum DayOfWeek implements EnumType {
    Mon("Mon"),
    Tue("Tue"),
    Wed("Wed"),
    Thu("Thu"),
    Fri("Fri"),
    Sat("Sat"),
    Sun("Sun");
    
    // ...
}

Even without code generation, any enum type that also implements jOOQ's org.jooq.EnumType can be used to construct a org.jooq.DataType:

DataType<DayOfWeek> dayOfWeekType = VARCHAR.asEnumDataType(DayOfWeek.class);

And then, the type can be attached to any expression, e.g. if generated code is not available:

// Plain SQL template based
Field<DayOfWeek> f1 = DSL.field("my_table.day_of_week", emailType);

// Name based
Field<DayOfWeek> f2 = DSL.field(name("my_table", "day_of_week"), emailType);

Feedback

Do you have any feedback about this page? We'd love to hear it!

The jOOQ Logo