Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
add testenpoint, remove comment, simplify getConstructor, rename getParametersFromClass,
  • Loading branch information
humcqc committed Jan 31, 2024
1 parent ef33d84 commit 43edc3c
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,12 @@ public <T> CommonPanacheQueryImpl<T> project(Class<T> type) {
}

// build select clause with a constructor expression
String selectClause = "SELECT " + getParameterFromClass(type, null);
String selectClause = "SELECT " + getParametersFromClass(type, null);
return new CommonPanacheQueryImpl<>(this, selectClause + selectQuery,
"select count(*) " + selectQuery);
}

private StringBuilder getParameterFromClass(Class<?> type, String parentParameter) {
// TODO: Should we check annotation @ProjectedClass on type ?
private StringBuilder getParametersFromClass(Class<?> type, String parentParameter) {
StringBuilder selectClause = new StringBuilder();
// We use the first constructor that we found and use the parameter names,
// so the projection class must have only one constructor,
Expand All @@ -145,8 +144,7 @@ private StringBuilder getParameterFromClass(Class<?> type, String parentParamete
}

private Constructor<?> getConstructor(Class<?> type) {
return type.isPrimitive() || type.isAssignableFrom(String.class) ? type.getDeclaredConstructors()[1]
: type.getDeclaredConstructors()[0];
return type.getDeclaredConstructors()[0];
}

private String getParameterName(Class<?> parentType, String parentParameter, Parameter parameter) {
Expand All @@ -172,7 +170,7 @@ private String getParameterName(Class<?> parentType, String parentParameter, Par
// Test if the parameter is a nested Class that should be projected too.
if (parameter.getType().isAnnotationPresent(NestedProjectedClass.class)) {
Class<?> nestedType = parameter.getType();
return getParameterFromClass(nestedType, parameterName).toString();
return getParametersFromClass(nestedType, parameterName).toString();
} else {
return parameterName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,12 @@ public <T> CommonPanacheQueryImpl<T> project(Class<T> type) {
}

// build select clause with a constructor expression
String selectClause = "SELECT " + getParameterFromClass(type, null);
String selectClause = "SELECT " + getParametersFromClass(type, null);
return new CommonPanacheQueryImpl<>(this, selectClause + selectQuery,
"select count(*) " + selectQuery);
}

private StringBuilder getParameterFromClass(Class<?> type, String parentParameter) {
// TODO: Should we check annotation @ProjectedClass on type ?
private StringBuilder getParametersFromClass(Class<?> type, String parentParameter) {
StringBuilder selectClause = new StringBuilder();
// We use the first constructor that we found and use the parameter names,
// so the projection class must have only one constructor,
Expand All @@ -135,8 +134,7 @@ private StringBuilder getParameterFromClass(Class<?> type, String parentParamete
}

private Constructor<?> getConstructor(Class<?> type) {
return type.isPrimitive() || type.isAssignableFrom(String.class) ? type.getDeclaredConstructors()[1]
: type.getDeclaredConstructors()[0];
return type.getDeclaredConstructors()[0];
}

private String getParameterName(Class<?> parentType, String parentParameter, Parameter parameter) {
Expand All @@ -162,7 +160,7 @@ private String getParameterName(Class<?> parentType, String parentParameter, Par
// Test if the parameter is a nested Class that should be projected too.
if (parameter.getType().isAnnotationPresent(NestedProjectedClass.class)) {
Class<?> nestedType = parameter.getType();
return getParameterFromClass(nestedType, parameterName).toString();
return getParametersFromClass(nestedType, parameterName).toString();
} else {
return parameterName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1355,6 +1355,64 @@ public String testProjection() {
return "OK";
}

@GET
@Path("projection-nested")
@Transactional
public String testNestedProjection() {
Person person = new Person();
person.name = "2n";
person.uniqueName = "2n";
person.address = new Address("street 2");
person.persist();
PersonDTO personDTO = Person.find(
"select uniqueName, name, " +
" new io.quarkus.it.panache.PersonDTO$AddressDTO(address.street)," +
" new io.quarkus.it.panache.PersonDTO$DescriptionDTO(description.size, description.weight)," +
" description.size" +
" from Person2 where name = ?1",
"2n")
.project(PersonDTO.class)
.firstResult();
person.delete();
Assertions.assertEquals("2n", personDTO.name);
Assertions.assertEquals("street 2", personDTO.address.street);

person = new Person();
person.name = "3";
person.uniqueName = "3";
person.address = new Address("street 3");
person.address.persist();
person.description = new PersonDescription();
person.description.weight = 75;
person.description.size = 170;
person.persist();
personDTO = Person.find(" name = ?1", "3")
.project(PersonDTO.class)
.firstResult();
person.delete();
Assertions.assertEquals("3", personDTO.name);
Assertions.assertEquals("street 3", personDTO.address.street);
Assertions.assertEquals(170, personDTO.directHeight);
Assertions.assertEquals(170, personDTO.description.height);
Assertions.assertEquals("Height: 170, weight: 75", personDTO.description.getDescription());

Person hum = new Person();
hum.name = "hum";
hum.uniqueName = "hum";
Dog kit = new Dog("kit", "bulldog");
hum.dogs.add(kit);
kit.owner = hum;
hum.persist();
DogDto2 dogDto2 = Dog.find(" name = ?1", "kit")
.project(DogDto2.class)
.firstResult();
hum.delete();
Assertions.assertEquals("kit", dogDto2.name);
Assertions.assertEquals("hum", dogDto2.owner.name);

return "OK";
}

@GET
@Path("model3")
@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public void testPanacheFunctionality() throws Exception {
RestAssured.when().get("/test/model1").then().body(is("OK"));
RestAssured.when().get("/test/model2").then().body(is("OK"));
RestAssured.when().get("/test/projection").then().body(is("OK"));
RestAssured.when().get("/test/projection-nested").then().body(is("OK"));
RestAssured.when().get("/test/model3").then().body(is("OK"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1716,6 +1716,69 @@ public Uni<String> testProjection2() {
.replaceWith("OK");
}

@WithTransaction
@GET
@Path("projection-nested")
public Uni<String> testNestedProjection() {
Person person = new Person();
person.name = "1";
person.uniqueName = "1";
Person person3 = new Person();
Person hum = new Person();
return person.persist()
.chain(() -> Person.find("select name from Person2 where name = ?1", "1")
.project(String.class)
.firstResult()

)
.invoke(personName -> Assertions.assertEquals("1", personName))
.chain(() -> {
person3.name = "3";
person3.uniqueName = "3";
person3.address = new Address("street 3");
person3.description = new PersonDescription();
person3.description.weight = 75;
person3.description.size = 170;
return person3.persist();
})
.chain(() -> Person.find(" name = ?1", "3")
.project(PersonDTO.class)
.<PersonDTO> firstResult())
.invoke(
personDTO -> {
Assertions.assertEquals("3", personDTO.name);
Assertions.assertEquals("street 3", personDTO.address.street);
Assertions.assertEquals(170, personDTO.directHeight);
Assertions.assertEquals(170, personDTO.description.height);
Assertions.assertEquals("Height: 170, weight: 75", personDTO.description.getGeneratedDescription());
})
.chain(() -> {
hum.name = "hum";
hum.uniqueName = "hum";
Dog kit = new Dog("kit", "bulldog");
hum.dogs.add(kit);
kit.owner = hum;
return hum.persist();
})
.chain(() -> Dog.find(" name = ?1", "kit")
.project(DogDto.class)
.<DogDto> firstResult())
.invoke(
dogDto -> {
Assertions.assertNotNull(dogDto);
Assertions.assertEquals("kit", dogDto.name);
Assertions.assertEquals("hum", dogDto.owner.name);
})
.flatMap(v -> person.delete())
.flatMap(v -> person3.delete())
.flatMap(v -> hum.delete())
.flatMap(v -> Person.count())
.map(count -> {
Assertions.assertEquals(1, count);
return "OK";
});
}

@WithTransaction
@GET
@Path("model3")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public void testPanacheFunctionality() throws Exception {
RestAssured.when().get("/test/model2").then().body(is("OK"));
RestAssured.when().get("/test/projection1").then().body(is("OK"));
RestAssured.when().get("/test/projection2").then().body(is("OK"));
RestAssured.when().get("/test/projection-nested").then().body(is("OK"));
RestAssured.when().get("/test/model3").then().body(is("OK"));
}

Expand Down

0 comments on commit 43edc3c

Please sign in to comment.