Depending on the database and schema metadata, a LEFT JOIN
may be a better choice than a projection predicate. You should, of course, verify this in terms of implementation.
Solving this with LEFT JOIN
:
The above assumes, of course, that there is an undefined restriction on cu(user_id, capability_id)
. This then translates to jOOQ as such:
Capabilities c = CAPABILITIES.as("c"); CapabilitiesUsers cu = CAPABILITIES_USERS.as("cu"); Field<String> key = c.NAME.as("key"); Field<Boolean> value = nvl2( CAPABILITIES_USER.CAPABILITY_ID, true, false ).as("value"); Map<String, Boolean> map = DSL.using(configuration) .select(key, value) .from(c) .leftOuterJoin(cu) .on(c.ID.eq(cu.CAPABILITY_ID)) .and(cu.USER_ID.eq(...)) .fetchMap(key, value);
Solving this with a predicate in projection:
If you really prefer a predicate in projection, you can try DSL.field(Condition)
, which allows exactly this:
Field<String> key = CAPABILITIES.NAME.as("key"); Field<Boolean> value = field( CAPABILITIES.ID.in( select(CAPABILITY_ID) .from(CAPABILITIES_USERS) .where(CAPABILITIES_USERS.USER_ID.eq(...)) ) ).as("value"); Map<String, Boolean> map = DSL.using(configuration) .select(key, value) .from(CAPABILITIES) .fetchMap(key, value);
Please note: if you use a standard database that does not allow predicates to be viewed as columns, DSL.field(Condition)
will make the equivalent CASE
statement for you.
source share