New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for results transformer (e.g. group by) #17

Closed
timowest opened this Issue Oct 3, 2011 · 2 comments

Comments

Projects
None yet
1 participant
@timowest
Member

timowest commented Oct 3, 2011

There should be an easy way of defining (generic) transformers over a whole result set. For example to group results by.

Usage scenario:

Query qry=...;
qry.from(post).innerJoin(post.comments, comment);
...
Collection<Group> groups = qry.transform(new GroupByTransformer(post.id, post.name, comment.id);

for (Group group : groups) {
    Long id = group.get(post.id);
    String name = group.get(post.name);
    List<Long> commentIds = group.getList(comment.id);
    ...
}

Under the hood this boils down to ResultTransformer interface that functions as a callback that gets a Projectable and returns the digested results:

public interface ResultTransformer<T> {

    T transform(Projectable projectable);
}
@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Oct 3, 2011

Member

Samppa Saarela (samppa-saarela) on 2011-09-14
Changed in querydsl:
assignee: nobody → Samppa Saarela (samppa-saarela)
Samppa Saarela (samppa-saarela) wrote on 2011-09-14: #1

Basic functionality is now committed to master:

https://github.com/mysema/querydsl/blob/master/querydsl-core/src/main/java/com/mysema/query/ResultTransformer.java

which is implemented by

https://github.com/mysema/querydsl/blob/master/querydsl-core/src/main/java/com/mysema/query/support/GroupBy.java

This is not yet integrated to Projectable (e.g. SQLQuery), but it is usable none the less with a slight variation to above:

Collection groups = new GroupByTransformer(post.id, post.name, comment.id).transform(qry);
Samppa Saarela (samppa-saarela) wrote on 2011-09-14: #2

A more efficient grouping implementation would be GroupByOrdered which would require that the resuts are ordered by the group id...
Timo Westkämper (timo-westkamper) on 2011-09-14
Changed in querydsl:
status: New → In Progress
Samppa Saarela (samppa-saarela) wrote on 2011-09-16: #3

I'm working on a bit more elegant solution keeping BeanMapper in mind. The idea is to use fluent syntax to build up a group-definition that converts results straight into correct collection types.

With model Post(id, name) 1--N Comment(id, text)

Map<Integer, Group2> results =
GroupBy2.groupBy(postId).first(postName).set(commentId).list(commentText).transform(BASIC_RESULTS);

Group2 group = results.get(1);
assertEquals(toInt(1), group.first(postId));
assertEquals("post 1", group.first(postName));
assertEquals(toSet(1, 2, 3), group.set(commentId));
assertEquals(Arrays.asList("comment 1", "comment 2", "comment 3"), group.list(commentText));

Member

timowest commented Oct 3, 2011

Samppa Saarela (samppa-saarela) on 2011-09-14
Changed in querydsl:
assignee: nobody → Samppa Saarela (samppa-saarela)
Samppa Saarela (samppa-saarela) wrote on 2011-09-14: #1

Basic functionality is now committed to master:

https://github.com/mysema/querydsl/blob/master/querydsl-core/src/main/java/com/mysema/query/ResultTransformer.java

which is implemented by

https://github.com/mysema/querydsl/blob/master/querydsl-core/src/main/java/com/mysema/query/support/GroupBy.java

This is not yet integrated to Projectable (e.g. SQLQuery), but it is usable none the less with a slight variation to above:

Collection groups = new GroupByTransformer(post.id, post.name, comment.id).transform(qry);
Samppa Saarela (samppa-saarela) wrote on 2011-09-14: #2

A more efficient grouping implementation would be GroupByOrdered which would require that the resuts are ordered by the group id...
Timo Westkämper (timo-westkamper) on 2011-09-14
Changed in querydsl:
status: New → In Progress
Samppa Saarela (samppa-saarela) wrote on 2011-09-16: #3

I'm working on a bit more elegant solution keeping BeanMapper in mind. The idea is to use fluent syntax to build up a group-definition that converts results straight into correct collection types.

With model Post(id, name) 1--N Comment(id, text)

Map<Integer, Group2> results =
GroupBy2.groupBy(postId).first(postName).set(commentId).list(commentText).transform(BASIC_RESULTS);

Group2 group = results.get(1);
assertEquals(toInt(1), group.first(postId));
assertEquals("post 1", group.first(postName));
assertEquals(toSet(1, 2, 3), group.set(commentId));
assertEquals(Arrays.asList("comment 1", "comment 2", "comment 3"), group.list(commentText));

@timowest

This comment has been minimized.

Show comment
Hide comment
@timowest

timowest Nov 19, 2011

Member

Released in 2.2.5

Member

timowest commented Nov 19, 2011

Released in 2.2.5

@timowest timowest closed this Nov 19, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment