jOOQ is an internal DSL and source code generator, modelling the SQL language as a type safe Java API to help you write better SQL
—
Type-safe representations of database schema objects including fields, tables, constraints, and relationships. Provides metadata access and supports both generated and dynamic schema definitions.
Represents column expressions and database fields with type safety and extensive operator support.
public interface Field<T> extends SelectField<T>, GroupField, OrderField<T> {
/**
* Get the field name
* @return Field name as string
*/
String getName();
/**
* Get the field's Java type
* @return Class representing the field type
*/
Class<T> getType();
/**
* Get the field's data type information
* @return DataType with detailed type information
*/
DataType<T> getDataType();
/**
* Get the field comment
* @return Field comment or null
*/
String getComment();
/**
* Create an alias for this field
* @param alias Alias name
* @return Field with alias applied
*/
Field<T> as(String alias);
/**
* Create an alias using a Name object
* @param alias Alias as Name object
* @return Field with alias applied
*/
Field<T> as(Name alias);
/**
* Create an equality condition
* @param value Value to compare with
* @return Condition representing field = value
*/
Condition eq(T value);
/**
* Create a not-equal condition
* @param value Value to compare with
* @return Condition representing field <> value
*/
Condition ne(T value);
/**
* Create an IN condition with multiple values
* @param values Values for IN clause
* @return Condition representing field IN (values)
*/
Condition in(T... values);
/**
* Create a NOT IN condition
* @param values Values for NOT IN clause
* @return Condition representing field NOT IN (values)
*/
Condition notIn(T... values);
/**
* Create an IN condition with subquery
* @param select Subquery providing values
* @return Condition representing field IN (subquery)
*/
Condition in(Select<? extends Record1<T>> select);
/**
* Create an ascending sort field
* @return SortField for ascending order
*/
SortField<T> asc();
/**
* Create a descending sort field
* @return SortField for descending order
*/
SortField<T> desc();
}Additional operations available for numeric fields.
public interface Field<T extends Number> {
/**
* Create a greater-than condition
* @param value Value to compare with
* @return Condition representing field > value
*/
Condition gt(T value);
/**
* Create a greater-than-or-equal condition
* @param value Value to compare with
* @return Condition representing field >= value
*/
Condition ge(T value);
/**
* Create a less-than condition
* @param value Value to compare with
* @return Condition representing field < value
*/
Condition lt(T value);
/**
* Create a less-than-or-equal condition
* @param value Value to compare with
* @return Condition representing field <= value
*/
Condition le(T value);
/**
* Create a BETWEEN condition
* @param minValue Minimum value (inclusive)
* @param maxValue Maximum value (inclusive)
* @return Condition representing field BETWEEN min AND max
*/
Condition between(T minValue, T maxValue);
/**
* Add this field to another
* @param value Value to add
* @return Field representing field + value
*/
Field<T> add(T value);
/**
* Subtract a value from this field
* @param value Value to subtract
* @return Field representing field - value
*/
Field<T> sub(T value);
/**
* Multiply this field by a value
* @param value Value to multiply by
* @return Field representing field * value
*/
Field<T> mul(T value);
/**
* Divide this field by a value
* @param value Value to divide by
* @return Field representing field / value
*/
Field<T> div(T value);
}Additional operations available for string fields.
public interface Field<String> {
/**
* Create a LIKE condition
* @param pattern Pattern for LIKE comparison
* @return Condition representing field LIKE pattern
*/
Condition like(String pattern);
/**
* Create a NOT LIKE condition
* @param pattern Pattern for NOT LIKE comparison
* @return Condition representing field NOT LIKE pattern
*/
Condition notLike(String pattern);
/**
* Create a case-insensitive LIKE condition
* @param pattern Pattern for ILIKE comparison
* @return Condition representing field ILIKE pattern
*/
Condition likeIgnoreCase(String pattern);
/**
* Create a SIMILAR TO condition (regex-like)
* @param pattern Regular expression pattern
* @return Condition representing field SIMILAR TO pattern
*/
Condition similarTo(String pattern);
/**
* Concatenate this field with another value
* @param value Value to concatenate
* @return Field representing field || value
*/
Field<String> concat(String value);
/**
* Create an uppercase version of this field
* @return Field representing UPPER(field)
*/
Field<String> upper();
/**
* Create a lowercase version of this field
* @return Field representing LOWER(field)
*/
Field<String> lower();
/**
* Trim whitespace from this field
* @return Field representing TRIM(field)
*/
Field<String> trim();
}Usage Examples:
// Field comparisons and conditions
Condition authorFilter = AUTHOR.FIRST_NAME.eq("John")
.and(AUTHOR.LAST_NAME.like("D%"))
.and(AUTHOR.BIRTH_YEAR.between(1950, 1990));
// Numeric operations
Field<Integer> adjustedPages = BOOK.PAGES.add(50).mul(2);
Select<Record2<String, Integer>> query = create
.select(BOOK.TITLE, adjustedPages.as("adjusted_pages"))
.from(BOOK);
// String operations
Select<Record1<String>> authors = create
.select(AUTHOR.FIRST_NAME.concat(" ").concat(AUTHOR.LAST_NAME).as("full_name"))
.from(AUTHOR)
.where(AUTHOR.LAST_NAME.upper().like("SMITH%"));
// Field aliases and sorting
Result<Record> result = create
.select(
BOOK.TITLE.as("book_title"),
BOOK.PAGES.as("page_count")
)
.from(BOOK)
.orderBy(BOOK.PAGES.desc(), BOOK.TITLE.asc())
.fetch();Represents database tables with metadata and relationship information.
public interface Table<R extends Record> extends TableLike<R> {
/**
* Get the table's record type information
* @return RecordType describing the table's structure
*/
RecordType<R> recordType();
/**
* Get the table's schema
* @return Schema containing this table
*/
Schema getSchema();
/**
* Get the table name
* @return Table name as string
*/
String getName();
/**
* Get the table comment
* @return Table comment or null
*/
String getComment();
/**
* Get the primary key constraint
* @return UniqueKey representing the primary key
*/
UniqueKey<R> getPrimaryKey();
/**
* Get all unique key constraints
* @return List of unique keys including primary key
*/
List<UniqueKey<R>> getUniqueKeys();
/**
* Get all foreign key references from this table
* @return List of foreign keys referencing other tables
*/
List<ForeignKey<R, ?>> getReferences();
/**
* Get all foreign keys referencing this table
* @return List of foreign keys from other tables
*/
List<ForeignKey<?, R>> getReferencedBy();
/**
* Get the identity column (auto-increment)
* @return Identity representing the auto-increment column
*/
Identity<R, ?> getIdentity();
/**
* Get all indexes on this table
* @return List of indexes
*/
List<Index> getIndexes();
/**
* Create an alias for this table
* @param alias Alias name
* @return Table with alias applied
*/
Table<R> as(String alias);
/**
* Create an alias with field aliases
* @param alias Table alias name
* @param fieldAliases Aliases for the table's fields
* @return Table with table and field aliases applied
*/
Table<R> as(String alias, String... fieldAliases);
/**
* Get a field from this table by name
* @param name Field name
* @return Field from this table
*/
Field<?> field(String name);
/**
* Get a field from this table by index
* @param index Field index (0-based)
* @return Field at the specified index
*/
Field<?> field(int index);
}Usage Examples:
// Table metadata access
Table<AuthorRecord> authors = AUTHOR;
Schema schema = authors.getSchema();
String tableName = authors.getName();
UniqueKey<AuthorRecord> pk = authors.getPrimaryKey();
List<ForeignKey<AuthorRecord, ?>> fks = authors.getReferences();
// Table aliases
Table<AuthorRecord> a = AUTHOR.as("a");
Table<BookRecord> b = BOOK.as("b");
Result<Record> result = create
.select()
.from(a)
.join(b).on(a.field("ID").eq(b.field("AUTHOR_ID")))
.fetch();
// Dynamic field access
Field<?> nameField = AUTHOR.field("FIRST_NAME");
Field<?> firstField = AUTHOR.field(0);Represents database schemas containing tables, procedures, and other objects.
public interface Schema extends Named {
/**
* Get all tables in this schema
* @return List of tables
*/
List<Table<?>> getTables();
/**
* Get a table by name
* @param name Table name
* @return Table with the specified name
*/
Table<Record> getTable(String name);
/**
* Get all sequences in this schema
* @return List of sequences
*/
List<Sequence<?>> getSequences();
/**
* Get all user-defined types in this schema
* @return List of UDTs
*/
List<UDT<?>> getUDTs();
/**
* Get the catalog containing this schema
* @return Catalog or null for default
*/
Catalog getCatalog();
}Static methods for creating fields and tables dynamically.
/**
* Create a field from a string name
* @param name Field name
* @return Field with Object type
*/
public static Field<Object> field(String name);
/**
* Create a typed field from a string name
* @param name Field name
* @param type Field type class
* @return Field with specified type
*/
public static <T> Field<T> field(String name, Class<T> type);
/**
* Create a field from a Name object
* @param name Field name as Name
* @param type Field data type
* @return Field with specified name and type
*/
public static <T> Field<T> field(Name name, DataType<T> type);
/**
* Create a table from a string name
* @param name Table name
* @return Table with Record type
*/
public static Table<Record> table(String name);
/**
* Create a table from a Name object
* @param name Table name as Name
* @return Table with Record type
*/
public static Table<Record> table(Name name);
/**
* Create a derived table from a SELECT
* @param select SELECT statement
* @return Table derived from the SELECT
*/
public static Table<Record> table(Select<?> select);Usage Examples:
// Dynamic field and table creation
Field<String> nameField = field("name", String.class);
Field<Integer> ageField = field("age", Integer.class);
Table<Record> usersTable = table("users");
// Query with dynamic objects
Result<Record> result = create
.select(nameField, ageField)
.from(usersTable)
.where(ageField.gt(18))
.fetch();
// Derived table from subquery
Table<Record> youngAuthors = table(
select(AUTHOR.FIRST_NAME, AUTHOR.LAST_NAME)
.from(AUTHOR)
.where(AUTHOR.DATE_OF_BIRTH.gt(LocalDate.of(1980, 1, 1)))
).as("young_authors");
Result<Record> result = create
.select()
.from(youngAuthors)
.fetch();public interface DataType<T> {
/**
* Get the SQL data type name
* @return SQL type name (e.g., "VARCHAR", "INTEGER")
*/
String getTypeName();
/**
* Get the Java type class
* @return Java class for this data type
*/
Class<T> getType();
/**
* Check if this type is nullable
* @return true if NULL values are allowed
*/
boolean nullable();
/**
* Create a non-nullable version of this type
* @return DataType that doesn't allow NULL
*/
DataType<T> notNull();
/**
* Get the default value for this type
* @return Default value or null
*/
T getDefaultValue();
}Install with Tessl CLI
npx tessl i tessl/maven-org-jooq--jooq