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

Optional conditional expressions

Applies to ✅ Open Source Edition   ✅ Express Edition   ✅ Professional Edition   ✅ Enterprise Edition

A key capability when creating dynamic SQL queries is to be able to provide optional conditional expressions.

boolean condition = ...

create.select(BOOK.ID)
      .from(BOOK)
      .where(condition ? BOOK.ID.eq(10) : noCondition())
      .fetch();

The above query produces:

-- If condition is true
SELECT book.id FROM book WHERE book.id = 10

-- If condition is false
SELECT book.id FROM book

The noCondition() expression will be ignored. If that means the clause is empty, then the entire clause will be omitted. This does not apply to clauses that project a Record type, including the SELECT clause, row value expressions, or nested records, as well as function calls, in case of which a NULL value will be projected.

Some additional interactions of the noCondition() can be seen in the section about TRUE and FALSE conditions.

Using a noCondition() as a column expression, e.g. by wrapping it with DSL.field(noCondition()) will produce a DSL.noField(BOOLEAN).

The noCondition() expression is supported only in the DSL API, not in the model API, where the behaviour of noCondition() is undefined.

References to this page

Feedback

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

The jOOQ Logo