You don't need any specific jOOQ API to accomplish this. Here's how I'd do
it:

private Optional<Foo> findBaseFoo(Supplier<Condition> where) {
  return dslContext.select(Foo.FIELD1, Foo.FIELD2)
                   .from(Foo.FOO_TABLE)
                   .where(where.get())
.fetchOne(FOO_RECORD_MAPPER));}

public Optional<Foo> findFoos() {
  return findBaseFoo(() -> Foo.FIELD1.eq(.....));


Alternatively, instead of a Supplier<Condition>, you could just simply pass
the Condition itself, defaulting to DSL.trueCondition() if no Condition is
supplied.

Hope this helps,
Lukas

2015-12-01 10:04 GMT+01:00 Mark Derricutt <[email protected]>:

> 'lo all,
>
> I was wondering if it was possible to specify a mapping function on a
> select() prior to completing the where clause etc.
>
> I have a bunch of code currently like:
>
> public Optional<Foo> findFoos() {
>   return Optional.fromNullable(dslContext.select(Foo.FIELD1, Foo.FIELD2)
>                                          .from(Foo.FOO_TABLE
>                                          .where(Foo.FIELD1.eq(.....))
>                                          .fetchOne(FOO_RECORD_MAPPER)));}
>
> where I have several different variants, that only vary by the where
> clause. In this code, I've extracted FOO_RECORD_MAPPER out, but as the
> base query and field count increases, the code starts to look ugly in java
> lang generics ( it's now a Record11<.....> ).
>
> Ideally I'd love to be able to refactor this somehow to read more like:
>
> private SelectJoinStep<Foo> findBaseFoo() {
>   return dslContext.select(Foo.FIELD1, Foo.FIELD2)
>                    .from(Foo.FOO_TABLE
>                    .map(FOO_RECORD_MAPPER));}
> public Optional<Foo> findFoos() {
>   return findBaseFoo().where(Foo.FIELD1.eq(.....))
>                       .fetchOne());
>
> Where I could then inline FOO_RECORD_MAPPER into findBaseFoo without
> exposing all the types ( via lambda syntax ) but it would also clean up the
> code more.
>
> However, it doesn't appear I can do anything like this currently? Or am I
> missing something? A variant of select() maybe?
>
> Mark
>
> --
> Mark Derricutt
> http://www.theoryinpractice.net
> http://www.chaliceofblood.net
> http://plus.google.com/+MarkDerricutt
> http://twitter.com/talios
> http://facebook.com/mderricutt
>
> --
> 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.

Reply via email to