snp0k
snp0k

Reputation: 398

JOOQ query for result of boolean expression

Please suggest JOOQ DSL to query for result of boolean expression
In SQL I would write:

SELECT sum(apples.quantity) > sum(bananas.quantity)
FROM ...

Upvotes: 3

Views: 3948

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 220762

In order to get a jOOQ Condition for your sum comparison, simply write:

Condition condition = sum(apples.quantity).gt(sum(bananas.quantity));

Now, Condition types currently cannot be put into the SELECT clause in jOOQ, but you can wrap them using DSL.field(Condition):

Field<Boolean> field = field(sum(apples.quantity).gt(sum(bananas.quantity)));

jOOQ will take care of deciding whether your SQL dialect allows for using predicates as column expressions (e.g. MySQL, PostgreSQL, SQLite), or whether this needs to be emulated using an equivalent CASE expression (e.g. DB2, HANA, Oracle, SQL Server).

Which leads to:

Record1<Boolean> result =
DSL.using(configuration)
   .select(field(sum(apples.quantity).gt(sum(bananas.quantity))))
   .from(...)
   .fetchOne();

Upvotes: 5

Related Questions