Skip to content
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

Handle text blocks when creating Panache find query #31419

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public static String createFindQuery(Class<?> entityClass, String query, int par
return "FROM " + getEntityName(entityClass);
}

String trimmed = query.trim();
String trimmed = query.replace('\n', ' ').replace('\r', ' ').trim();
if (trimmed.isEmpty()) {
return "FROM " + getEntityName(entityClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import io.quarkus.panache.common.Page
import io.quarkus.panache.common.Parameters
import io.quarkus.panache.common.Sort
import io.quarkus.panache.common.exception.PanacheQueryException
import io.quarkus.runtime.annotations.RegisterForReflection
import jakarta.inject.Inject
import jakarta.persistence.LockModeType
import jakarta.persistence.NoResultException
Expand Down Expand Up @@ -1091,4 +1092,47 @@ class TestEndpoint {

return "OK"
}

@GET
@Path("project")
@Transactional
fun testProject(): String {
@RegisterForReflection
data class MyProjection(
val projectedName: String
)

val mark = Person()
mark.name = "Mark"
mark.persistAndFlush()

val hqlWithoutSpace = """
select
name as projectedName
from
io.quarkus.it.panache.kotlin.Person
where
name = ?1
""".trimIndent()
val withoutSpace = Person.find(hqlWithoutSpace, "Mark").project(MyProjection::class.java).firstResult()
Assertions.assertNotNull(withoutSpace)
Assertions.assertEquals(mark.name, withoutSpace?.projectedName)

// There is a space behind "select "
val hqlWithSpace = """
select
name as projectedName
from
io.quarkus.it.panache.kotlin.Person
where
name = ?1
""".trimIndent()
val withSpace = Person.find(hqlWithSpace, "Mark").project(MyProjection::class.java).firstResult()
Assertions.assertNotNull(withSpace)
Assertions.assertEquals(mark.name, withSpace?.projectedName)

Person.deleteAll()

return "OK"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.it.panache

import io.quarkus.test.junit.QuarkusIntegrationTest
import io.quarkus.test.junit.QuarkusTest
import io.restassured.RestAssured
import org.hamcrest.Matchers
import org.junit.jupiter.api.Test

// Native tests
@QuarkusIntegrationTest
class ProjectionIT : ProjectionTest()

// Quarkus/JVM tests
@QuarkusTest
open class ProjectionTest {

@Test
fun testProject() {
RestAssured.`when`()["/test/project"].then().body(Matchers.`is`("OK"))
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.quarkus.it.hibernate.panache.person;

import java.net.URI;
import java.util.Arrays;
import java.util.List;

import jakarta.transaction.Transactional;
Expand All @@ -24,4 +25,39 @@ public Response addPerson(Person person) {
return Response.created(URI.create("/persons/entity/" + id)).build();
}


@GET
@Path("hql-project")
@Transactional
public Response testPanacheHqlProject() {
var mark = new Person();
mark.firstname = "Mark";
mark.lastname = "Mark";
mark.persistAndFlush();

var hqlWithoutSpace = """
select
firstname,
lastname
from
io.quarkus.it.hibernate.panache.person.Person
where
firstname = ?1
""";
var persistedWithoutSpace = Person.find(hqlWithoutSpace, "Mark").project(PersonName.class).firstResult();

// We need to escape the whitespace in Java otherwise the compiler removes it.
var hqlWithSpace = """
select\s
firstname,
lastname
from
io.quarkus.it.hibernate.panache.person.Person
where
firstname = ?1
""";
var persistedWithSpace = Person.find(hqlWithSpace, "Mark").project(PersonName.class).firstResult();

return Response.ok(Arrays.asList(persistedWithoutSpace, persistedWithSpace)).build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static io.restassured.RestAssured.when;
import static org.hamcrest.CoreMatchers.is;

import io.quarkus.test.TestTransaction;
import org.junit.jupiter.api.Test;

import io.quarkus.it.mongodb.panache.person.Person;
Expand All @@ -19,6 +20,7 @@ class PersonResourceTest {
private static final String ROOT_URL = "/hibernate/persons";

@Test
@TestTransaction
void testRecordInPanache() {
var person1 = new Person();
person1.firstname = "Loïc";
Expand All @@ -42,4 +44,11 @@ void testRecordInPanache() {
.body("size()", is(2));
}

@Test
@TestTransaction
void testHqlPanacheProject() {
when().get(ROOT_URL + "/hql-project")
.then().statusCode(200)
.body("size()", is(2));
}
}