Skip to content

Commit

Permalink
Merge branch 'master' into java17
Browse files Browse the repository at this point in the history
  • Loading branch information
velo authored Aug 5, 2024
2 parents 6617a0d + 139a59a commit db10585
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package com.querydsl.core.group;

import static com.querydsl.core.util.TupleUtils.toTuple;

import com.querydsl.core.FetchableQuery;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Expression;
Expand Down Expand Up @@ -53,29 +55,30 @@ public RES transform(FetchableQuery<?, ?> query) {
if (hasGroups) {
expr = withoutGroupExpressions(expr);
}
final var iter = query.select(expr).iterate();
try (final var iter = query.select(expr).iterate(); ) {

var list = resultFactory.get();
GroupImpl group = null;
K groupId = null;
while (iter.hasNext()) {
@SuppressWarnings("unchecked") // This type is mandated by the key type
var row = (K[]) iter.next().toArray();
if (group == null) {
group = new GroupImpl(groupExpressions, maps);
groupId = row[0];
} else if (!Objects.equals(groupId, row[0])) {
var list = resultFactory.get();
GroupImpl group = null;
K groupId = null;
while (iter.hasNext()) {
var tuple = toTuple(iter.next(), expressions);
@SuppressWarnings("unchecked") // This type is mandated by the key type
var row = (K[]) tuple.toArray();
if (group == null) {
group = new GroupImpl(groupExpressions, maps);
groupId = row[0];
} else if (!Objects.equals(groupId, row[0])) {
list.add(transform(group));
group = new GroupImpl(groupExpressions, maps);
groupId = row[0];
}
group.add(row);
}
if (group != null) {
list.add(transform(group));
group = new GroupImpl(groupExpressions, maps);
groupId = row[0];
}
group.add(row);
}
if (group != null) {
list.add(transform(group));
return list;
}
iter.close();
return list;
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package com.querydsl.core.group;

import static com.querydsl.core.util.TupleUtils.toTuple;

import com.querydsl.core.FetchableQuery;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Expression;
Expand Down Expand Up @@ -53,11 +55,11 @@ public RES transform(FetchableQuery<?, ?> query) {
if (hasGroups) {
expr = withoutGroupExpressions(expr);
}
var iter = query.select(expr).iterate();
try {
try (var iter = query.select(expr).iterate()) {
while (iter.hasNext()) {
var tuple = toTuple(iter.next(), expressions);
@SuppressWarnings("unchecked") // This type is mandated by the key type
var row = (K[]) iter.next().toArray();
var row = (K[]) tuple.toArray();
var groupId = row[0];
var group = (GroupImpl) groups.get(groupId);
if (group == null) {
Expand All @@ -66,8 +68,6 @@ public RES transform(FetchableQuery<?, ?> query) {
}
group.add(row);
}
} finally {
iter.close();
}

// transform groups
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package com.querydsl.core.group;

import static com.querydsl.core.util.TupleUtils.toTuple;

import com.mysema.commons.lang.CloseableIterator;
import com.querydsl.core.FetchableQuery;
import com.querydsl.core.Tuple;
Expand Down Expand Up @@ -73,8 +75,9 @@ public V next() {
}

while (iter.hasNext()) {
var tuple = toTuple(iter.next(), expressions);
@SuppressWarnings("unchecked") // This type is mandated by the key type
var row = (K[]) iter.next().toArray();
var row = (K[]) tuple.toArray();
if (group == null) {
group = new GroupImpl(groupExpressions, maps);
groupId = row[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
*/
package com.querydsl.core.group;

import static com.querydsl.core.util.TupleUtils.toTuple;

import com.querydsl.core.FetchableQuery;
import com.querydsl.core.Tuple;
import com.querydsl.core.types.Expression;
Expand Down Expand Up @@ -50,8 +52,9 @@ public Map<K, V> transform(FetchableQuery<?, ?> query) {
}
try (var iter = query.select(expr).iterate()) {
while (iter.hasNext()) {
var tuple = toTuple(iter.next(), expressions);
@SuppressWarnings("unchecked") // This type is mandated by the key type
var row = (K[]) iter.next().toArray();
var row = (K[]) tuple.toArray();
var groupId = row[0];
var group = (GroupImpl) groups.get(groupId);
if (group == null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.querydsl.core.util;

import com.querydsl.core.Tuple;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.Projections;

/** TupleUtils provides tuple related utility functionality */
public final class TupleUtils {

public static Tuple toTuple(Object next, Expression<?>[] expressions) {
// workaround from https://github.com/querydsl/querydsl/issues/3264
Tuple tuple;
if (next instanceof Tuple) {
tuple = (Tuple) next;
} else if (next instanceof Object[]) {
tuple = Projections.tuple(expressions).newInstance((Object[]) next);
} else {
throw new IllegalArgumentException(String.format("Could not translate %s into tuple", next));
}
return tuple;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@
package com.querydsl.jpa;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;

import com.mysema.commons.lang.CloseableIterator;
import com.querydsl.core.DefaultQueryMetadata;
import com.querydsl.core.Target;
import com.querydsl.core.Tuple;
import com.querydsl.core.group.GroupBy;
import com.querydsl.core.testutil.ExcludeIn;
import com.querydsl.core.types.EntityPath;
import com.querydsl.core.types.Expression;
import com.querydsl.jpa.domain.Cat;
Expand Down Expand Up @@ -159,4 +163,17 @@ public void createQuery3() {
assertThat(row instanceof String).isTrue();
}
}

@Test
@ExcludeIn(Target.DERBY)
public void createQuery4() {
assertDoesNotThrow(
() ->
query()
.from(cat)
.leftJoin(cat.kittens)
.fetchJoin()
.distinct()
.transform(GroupBy.groupBy(cat.id).as(cat)));
}
}

0 comments on commit db10585

Please sign in to comment.