The record construction API is available through DSLContext.newRecord():
https://www.jooq.org/javadoc/latest/org/jooq/DSLContext.html#newRecord-org.jooq.Field...-
E.g.
orElseGet(() -> ctx.newRecord(FOO.MY_INT).values(1)));
Of course, you could also solve this with SQL :) E.g.
Integer result = ctx
.select(FOO.MY_INT)
.from(FOO)
.where(FOO.BAR.eq("baz"))
.unionAll(select(inline(-1)))
.orderBy(inline(1).desc())
.limit(1)
.fetchOne(FOO.MY_INT);
In this case, I think the Java solution is better, though. In fact, I think
you were actually looking for this solution:
Integer result = ctx
.select(FOO.MY_INT)
.from(FOO)
.where(FOO.BAR.eq("baz"))
.fetchOptional()
.map(Record1::value1)
.orElse(-1);
Hope this helps,
Lukas
2017-05-08 16:36 GMT+02:00 Daniel Einspanjer <[email protected]
>:
> If you have a query that returns Record1<Integer> and you execute the
> query using fetchOptional(), how can you provide an alternative value in
> the .orElse() method of the optional?
>
> i.e.
>
> Integer result =
> select(FOO.MY_INT).from(FOO).where(FOO.BAR.eq("baz")).fetchOptional().orElse(new
> Record1<Integer>() { -1 });
>
> --
> You received this message because you are subscribed to the Google Groups
> "jOOQ User Group" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.