Skip to content

Commit

Permalink
Merge pull request #18270 from geoand/#18240
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi authored Jun 30, 2021
2 parents ba9b6eb + d15488e commit 7b1cbe2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static PanacheQuery<?> find(AbstractJpaOperations<?> jpaOperations, Class
EntityManager em = jpaOperations.getEntityManager();
Query jpaQuery = em.createQuery(sort != null ? findQuery + toOrderBy(sort) : findQuery);
JpaOperations.bindParameters(jpaQuery, params);
return new CustomCountPanacheQuery(em, jpaQuery, findQuery, countQuery, params);
return new CustomCountPanacheQuery(em, jpaQuery, countQuery, params);
}

public static PanacheQuery<?> find(AbstractJpaOperations<?> jpaOperations, Class<?> entityClass, String query,
Expand All @@ -50,7 +50,7 @@ public static PanacheQuery<?> find(AbstractJpaOperations<?> jpaOperations, Class
EntityManager em = jpaOperations.getEntityManager();
Query jpaQuery = em.createQuery(sort != null ? findQuery + toOrderBy(sort) : findQuery);
JpaOperations.bindParameters(jpaQuery, params);
return new CustomCountPanacheQuery(em, jpaQuery, findQuery, countQuery, params);
return new CustomCountPanacheQuery(em, jpaQuery, countQuery, params);
}

public static long deleteAllWithCascade(AbstractJpaOperations<?> jpaOperations, Class<?> entityClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,29 @@
import javax.persistence.EntityManager;
import javax.persistence.Query;

import org.hibernate.query.internal.QueryImpl;

import io.quarkus.hibernate.orm.panache.common.runtime.CommonPanacheQueryImpl;

//TODO this class is only needed by the Spring Data JPA module and would be placed there it it weren't for a dev-mode classloader issue
// see https://github.com/quarkusio/quarkus/issues/6214
public class CustomCountPanacheQuery<Entity> extends PanacheQueryImpl<Entity> {

public CustomCountPanacheQuery(EntityManager em, Query jpaQuery, String query, String customCountQuery,
public CustomCountPanacheQuery(EntityManager em, Query jpaQuery, String customCountQuery,
Object paramsArrayOrMap) {
super(new CommonPanacheQueryImpl<Entity>(em, query, null, paramsArrayOrMap) {
super(new CommonPanacheQueryImpl<>(em, castQuery(jpaQuery).getQueryString(), null, paramsArrayOrMap) {
{
this.countQuery = customCountQuery;
}
});
}

@SuppressWarnings("rawtypes")
private static QueryImpl castQuery(Query jpaQuery) {
if (!(jpaQuery instanceof QueryImpl)) {
throw new IllegalArgumentException("Unexpected Query class: '" + jpaQuery.getClass().getName() + "', where '"
+ QueryImpl.class.getName() + "' is expected.");
}
return (QueryImpl) jpaQuery;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public void add(ClassCreator classCreator, FieldDescriptor entityClassFieldDescr
methodCreator.readStaticField(operationsField),
methodCreator.readInstanceField(entityClassFieldDescriptor, methodCreator.getThis()),
methodCreator.load(queryString), methodCreator.load(countQueryString),
generateSort(sortParameterIndex, methodCreator), parameters);
generateSort(sortParameterIndex, pageableParameterIndex, methodCreator), parameters);

} else {
ResultHandle paramsArray = generateParamsArray(queryParameterIndexes, methodCreator);
Expand All @@ -329,7 +329,7 @@ public void add(ClassCreator classCreator, FieldDescriptor entityClassFieldDescr
methodCreator.readStaticField(operationsField),
methodCreator.readInstanceField(entityClassFieldDescriptor, methodCreator.getThis()),
methodCreator.load(queryString), methodCreator.load(countQueryString),
generateSort(sortParameterIndex, methodCreator), paramsArray);
generateSort(sortParameterIndex, pageableParameterIndex, methodCreator), paramsArray);
}

generateFindQueryResultHandling(methodCreator, panacheQuery, pageableParameterIndex, repositoryClassInfo,
Expand Down Expand Up @@ -393,14 +393,20 @@ private ResultHandle generateParametersObject(Map<String, Integer> namedParamete
}

// ensure that Sort is correctly handled whether it's specified from the method name or a method param
private ResultHandle generateSort(Integer sortParameterIndex, MethodCreator methodCreator) {
private ResultHandle generateSort(Integer sortParameterIndex, Integer pageableParameterIndex, MethodCreator methodCreator) {
ResultHandle sort = methodCreator.loadNull();
if (sortParameterIndex != null) {
sort = methodCreator.invokeStaticMethod(
MethodDescriptor.ofMethod(TypesConverter.class, "toPanacheSort",
io.quarkus.panache.common.Sort.class,
org.springframework.data.domain.Sort.class),
methodCreator.getMethodParam(sortParameterIndex));
} else if (pageableParameterIndex != null) {
sort = methodCreator.invokeStaticMethod(
MethodDescriptor.ofMethod(TypesConverter.class, "pageToPanacheSort",
io.quarkus.panache.common.Sort.class,
org.springframework.data.domain.Pageable.class),
methodCreator.getMethodParam(pageableParameterIndex));
}
return sort;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import io.quarkus.panache.common.Sort;

@SuppressWarnings("unused") // the methods of this class are invoked in the generated bytecode
public final class TypesConverter {

private TypesConverter() {
Expand Down Expand Up @@ -43,4 +44,12 @@ public static io.quarkus.panache.common.Page toPanachePage(org.springframework.d
}
return null; //PanacheQueryImpl#list properly handles null
}

public static io.quarkus.panache.common.Sort pageToPanacheSort(org.springframework.data.domain.Pageable pageable) {
org.springframework.data.domain.Sort sort = pageable.getSort();
if (sort.isSorted()) {
return toPanacheSort(sort);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
Expand Down Expand Up @@ -62,6 +63,14 @@ public String customFind(@PathParam("size") int pageSize, @PathParam("num") int
return page.hasNext() + " / " + page.getNumberOfElements();
}

@GET
@Path("/customFind/all")
public List<Movie> customFindReturnAll() {
Page<Movie> page = movieRepository.customFind(
PageRequest.of(0, 100, Sort.Direction.DESC, "id"));
return page.stream().collect(Collectors.toList());
}

@GET
@Path("/rating/{rating}")
@Produces("application/json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusTest;
import io.restassured.common.mapper.TypeRef;

@QuarkusTest
public class MovieResourceTest {
Expand Down Expand Up @@ -182,6 +185,13 @@ void testCustomFind() {
when().get("/movie/customFind/page/10/0").then()
.statusCode(200)
.body(containsString("false /"));

List<Movie> movies = when().get("/movie/customFind/all").then()
.statusCode(200)
.extract().body().as(new TypeRef<List<Movie>>() {
});
List<Long> ids = movies.stream().map(MovieSuperclass::getId).collect(Collectors.toList());
assertThat(ids).isNotEmpty().isSortedAccordingTo(Comparator.reverseOrder());
}

@Test
Expand Down

0 comments on commit 7b1cbe2

Please sign in to comment.