2015-05-05 14:37 GMT+02:00 Ben Hood <[email protected]>:
> On Tue, May 5, 2015 at 12:38 PM, Lukas Eder <[email protected]> wrote:
> > You're looking for this, I suspect?
> >
> http://www.jooq.org/javadoc/latest/org/jooq/ResultQuery.html#fetchGroups-org.jooq.Field:A-
> >
> > Of course, with Java 8 Streams you'll get access to even more powerful
> > transformations...
>
> So this would return
>
> Map<Record, Result<R>>
>
> I guess that you would use the static column metadata to extract the
> fields from the Record key and Result<R> list respectively?
>
Yes.
> In the meantime, I've hacked together this agricultural solution,
>
I like the term ;-)
> which does the job, but is not very elegant:
>
> public List<Group> groupsForOrganization(final Long organization) {
> return db.execute(ctx ->
> ctx.select(GROUP_MEMBERS.GROUP_ID, GROUPS.NAME,
> GROUP_MEMBERS.USER_ID).
> from(GROUP_MEMBERS).
> join(GROUPS).on(GROUP_MEMBERS.GROUP_ID.eq(GROUPS.ID)).
> where(GROUP_MEMBERS.REGISTRANT.eq(organization)).
> orderBy(GROUP_MEMBERS.GROUP_ID, GROUPS.NAME,
> GROUP_MEMBERS.USER_ID).
> fetch().
> stream().
> collect(Collectors.groupingBy(x ->
> Pair.of(x.getValue(GROUP_MEMBERS.GROUP_ID),
> x.getValue(GROUPS.NAME)))).
> entrySet().
> stream().
> map(x -> {
> Pair<Long,String> key = x.getKey();
> List<Record3<Long, String, Long>> records = x.getValue();
> List<Long> members = records.stream().map(y ->
> y.getValue(GROUP_MEMBERS.USER_ID)).collect(Collectors.toList());
> return Group.create(key.getLeft(), key.getRight(), members);
> }).
> collect(Collectors.toList())
> );
> }
>
> This hack uses the Apache tuple definition - on reflection I guess I
> could have used some other tuple implementation - does JOOQ lambda
> have some kind of tuple thingy?
Yes, here's the tuple package:
http://www.jooq.org/products/jOO%CE%BB/javadoc/latest/org/jooq/lambda/tuple/package-summary.html
You might also like wrapping the jOOQ Result in an org.jooq.lambda.Seq,
which provides a couple of shortcuts to commonly used Stream operations,
like grouping (the following might not compile out of the box):
return db.execute(ctx ->
seq(ctx.select(GROUP_MEMBERS.GROUP_ID, GROUPS.NAME <http://groups.name/>,
GROUP_MEMBERS.USER_ID).
from(GROUP_MEMBERS).
join(GROUPS).on(GROUP_MEMBERS.GROUP_ID.eq(GROUPS.ID
<http://groups.id/>)).
where(GROUP_MEMBERS.REGISTRANT.eq(organization)).
orderBy(GROUP_MEMBERS.GROUP_ID, GROUPS.NAME <http://groups.name/>,
GROUP_MEMBERS.USER_ID).
fetch()).
groupBy(x -> Tuple.tuple(x.getValue(GROUP_MEMBERS.GROUP_ID),
x.getValue(GROUPS.NAME
<http://groups.name/>))),
Collectors.mapping(
x -> x.getValue(GROUP_MEMBERS.USER_ID),
Collectors.toList()
))
);
I've just noticed that the Collectors.mapping() bit might probably be
simplified as well. The JDK's approach is too verbose.
Let me know if this works for you,
Cheers,
Lukas
--
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.