diff --git a/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtils.kt b/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtils.kt index 4c3e7447a..5184ff813 100644 --- a/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtils.kt +++ b/support/spring-data-jpa-javax/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtils.kt @@ -12,7 +12,7 @@ import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable import org.springframework.data.domain.Slice import org.springframework.data.domain.SliceImpl -import org.springframework.data.jpa.repository.query.QueryUtilsAdaptor +import org.springframework.data.jpa.repository.query.QueryEnhancerFactoryAdaptor import org.springframework.data.support.PageableExecutionUtilsAdaptor import javax.persistence.EntityManager import javax.persistence.Query @@ -141,7 +141,9 @@ internal object JpqlEntityManagerUtils { pageable: Pageable, resultClass: KClass, ): List { - val sortedQuery = QueryUtilsAdaptor.applySorting(rendered.query, pageable.sort) + val queryEnhancer = QueryEnhancerFactoryAdaptor.forQuery(rendered.query) + + val sortedQuery = queryEnhancer.applySorting(pageable.sort) val jpaQuery = entityManager.createQuery(sortedQuery, resultClass.java).apply { setParams(this, rendered.params) @@ -161,8 +163,10 @@ internal object JpqlEntityManagerUtils { pageable: Pageable, resultClass: KClass, ): Page { - val sortedQuery = QueryUtilsAdaptor.applySorting(rendered.query, pageable.sort) - val countQuery = QueryUtilsAdaptor.createCountQueryFor(rendered.query, null, false) + val queryEnhancer = QueryEnhancerFactoryAdaptor.forQuery(rendered.query) + + val sortedQuery = queryEnhancer.applySorting(pageable.sort) + val countQuery = queryEnhancer.createCountQueryFor() val sortedJpaQuery = entityManager.createQuery(sortedQuery, resultClass.java).apply { setParams(this, rendered.params) @@ -194,7 +198,9 @@ internal object JpqlEntityManagerUtils { pageable: Pageable, resultClass: KClass, ): Slice { - val sortedQuery = QueryUtilsAdaptor.applySorting(rendered.query, pageable.sort) + val queryEnhancer = QueryEnhancerFactoryAdaptor.forQuery(rendered.query) + + val sortedQuery = queryEnhancer.applySorting(pageable.sort) val jpaQuery = entityManager.createQuery(sortedQuery, resultClass.java).apply { setParams(this, rendered.params) diff --git a/support/spring-data-jpa-javax/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryEnhancerFactoryAdaptor.kt b/support/spring-data-jpa-javax/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryEnhancerFactoryAdaptor.kt new file mode 100644 index 000000000..8e8216807 --- /dev/null +++ b/support/spring-data-jpa-javax/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryEnhancerFactoryAdaptor.kt @@ -0,0 +1,7 @@ +package org.springframework.data.jpa.repository.query + +internal object QueryEnhancerFactoryAdaptor { + fun forQuery(query: String): QueryEnhancer { + return QueryEnhancerFactory.forQuery(StringQuery(query, false)) + } +} diff --git a/support/spring-data-jpa-javax/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryUtilsAdaptor.kt b/support/spring-data-jpa-javax/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryUtilsAdaptor.kt deleted file mode 100644 index 222afa021..000000000 --- a/support/spring-data-jpa-javax/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryUtilsAdaptor.kt +++ /dev/null @@ -1,13 +0,0 @@ -package org.springframework.data.jpa.repository.query - -import org.springframework.data.domain.Sort - -internal object QueryUtilsAdaptor { - fun applySorting(query: String, sort: Sort): String { - return QueryUtils.applySorting(query, sort) - } - - fun createCountQueryFor(originalQuery: String, countProjection: String?, nativeQuery: Boolean): String { - return QueryUtils.createCountQueryFor(originalQuery, countProjection, nativeQuery) - } -} diff --git a/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtilsTest.kt b/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtilsTest.kt index c888518ff..c4234b4b7 100644 --- a/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtilsTest.kt +++ b/support/spring-data-jpa-javax/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/javax/JpqlEntityManagerUtilsTest.kt @@ -20,7 +20,8 @@ import org.junit.jupiter.api.extension.ExtendWith import org.springframework.data.domain.Page import org.springframework.data.domain.PageRequest import org.springframework.data.domain.SliceImpl -import org.springframework.data.jpa.repository.query.QueryUtilsAdaptor +import org.springframework.data.jpa.repository.query.QueryEnhancer +import org.springframework.data.jpa.repository.query.QueryEnhancerFactoryAdaptor import org.springframework.data.support.PageableExecutionUtilsAdaptor import java.util.function.LongSupplier import javax.persistence.EntityManager @@ -37,6 +38,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @MockK private lateinit var context: RenderContext + @MockK + private lateinit var queryEnhancer: QueryEnhancer + @MockK private lateinit var selectQuery1: SelectQuery @@ -69,7 +73,7 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @Suppress("UnusedEquals") fun setUp() { mockkObject(JpqlRendererHolder) - mockkObject(QueryUtilsAdaptor) + mockkObject(QueryEnhancerFactoryAdaptor) mockkObject(PageableExecutionUtilsAdaptor) every { JpqlRendererHolder.get() } returns renderer @@ -233,7 +237,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { selectQuery1.returnType } returns String::class - every { QueryUtilsAdaptor.applySorting(any(), any()) } returns sortedQuery1 + every { QueryEnhancerFactoryAdaptor.forQuery(any()) } returns queryEnhancer + every { queryEnhancer.applySorting(any()) } returns sortedQuery1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 every { stringTypedQuery1.setFirstResult(any()) } returns stringTypedQuery1 @@ -250,7 +255,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { renderer.render(selectQuery1, context) selectQuery1.returnType - QueryUtilsAdaptor.applySorting(rendered1.query, pageable1.sort) + QueryEnhancerFactoryAdaptor.forQuery(rendered1.query) + queryEnhancer.applySorting(pageable1.sort) entityManager.createQuery(sortedQuery1, String::class.java) stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) @@ -273,8 +279,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { selectQuery1.returnType } returns String::class - every { QueryUtilsAdaptor.applySorting(any(), any()) } returns sortedQuery1 - every { QueryUtilsAdaptor.createCountQueryFor(any(), any(), any()) } returns countQuery1 + every { QueryEnhancerFactoryAdaptor.forQuery(any()) } returns queryEnhancer + every { queryEnhancer.applySorting(any()) } returns sortedQuery1 + every { queryEnhancer.createCountQueryFor() } returns countQuery1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 andThen longTypedQuery1 @@ -300,8 +307,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { renderer.render(selectQuery1, context) selectQuery1.returnType - QueryUtilsAdaptor.applySorting(rendered1.query, pageable1.sort) - QueryUtilsAdaptor.createCountQueryFor(rendered1.query, null, false) + QueryEnhancerFactoryAdaptor.forQuery(rendered1.query) + queryEnhancer.applySorting(pageable1.sort) + queryEnhancer.createCountQueryFor() entityManager.createQuery(sortedQuery1, String::class.java) stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) @@ -329,7 +337,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { selectQuery1.returnType } returns String::class - every { QueryUtilsAdaptor.applySorting(any(), any()) } returns sortedQuery1 + every { QueryEnhancerFactoryAdaptor.forQuery(any()) } returns queryEnhancer + every { queryEnhancer.applySorting(any()) } returns sortedQuery1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 every { stringTypedQuery1.setFirstResult(any()) } returns stringTypedQuery1 @@ -346,7 +355,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { renderer.render(selectQuery1, context) selectQuery1.returnType - QueryUtilsAdaptor.applySorting(rendered1.query, pageable1.sort) + QueryEnhancerFactoryAdaptor.forQuery(rendered1.query) + queryEnhancer.applySorting(pageable1.sort) entityManager.createQuery(sortedQuery1, String::class.java) stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) diff --git a/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt b/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt index 9d06d020c..6fbd61ad0 100644 --- a/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt +++ b/support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt @@ -15,7 +15,7 @@ import org.springframework.data.domain.Page import org.springframework.data.domain.Pageable import org.springframework.data.domain.Slice import org.springframework.data.domain.SliceImpl -import org.springframework.data.jpa.repository.query.QueryUtilsAdaptor +import org.springframework.data.jpa.repository.query.QueryEnhancerFactoryAdaptor import org.springframework.data.support.PageableExecutionUtilsAdaptor import kotlin.reflect.KClass @@ -141,7 +141,9 @@ internal object JpqlEntityManagerUtils { pageable: Pageable, resultClass: KClass, ): List { - val sortedQuery = QueryUtilsAdaptor.applySorting(rendered.query, pageable.sort) + val queryEnhancer = QueryEnhancerFactoryAdaptor.forQuery(rendered.query) + + val sortedQuery = queryEnhancer.applySorting(pageable.sort) val jpaQuery = entityManager.createQuery(sortedQuery, resultClass.java).apply { setParams(this, rendered.params) @@ -161,8 +163,10 @@ internal object JpqlEntityManagerUtils { pageable: Pageable, resultClass: KClass, ): Page { - val sortedQuery = QueryUtilsAdaptor.applySorting(rendered.query, pageable.sort) - val countQuery = QueryUtilsAdaptor.createCountQueryFor(rendered.query, null, false) + val queryEnhancer = QueryEnhancerFactoryAdaptor.forQuery(rendered.query) + + val sortedQuery = queryEnhancer.applySorting(pageable.sort) + val countQuery = queryEnhancer.createCountQueryFor() val sortedJpaQuery = entityManager.createQuery(sortedQuery, resultClass.java).apply { setParams(this, rendered.params) @@ -194,7 +198,9 @@ internal object JpqlEntityManagerUtils { pageable: Pageable, resultClass: KClass, ): Slice { - val sortedQuery = QueryUtilsAdaptor.applySorting(rendered.query, pageable.sort) + val queryEnhancer = QueryEnhancerFactoryAdaptor.forQuery(rendered.query) + + val sortedQuery = queryEnhancer.applySorting(pageable.sort) val jpaQuery = entityManager.createQuery(sortedQuery, resultClass.java).apply { setParams(this, rendered.params) diff --git a/support/spring-data-jpa/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryEnhancerFactoryAdaptor.kt b/support/spring-data-jpa/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryEnhancerFactoryAdaptor.kt new file mode 100644 index 000000000..8e8216807 --- /dev/null +++ b/support/spring-data-jpa/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryEnhancerFactoryAdaptor.kt @@ -0,0 +1,7 @@ +package org.springframework.data.jpa.repository.query + +internal object QueryEnhancerFactoryAdaptor { + fun forQuery(query: String): QueryEnhancer { + return QueryEnhancerFactory.forQuery(StringQuery(query, false)) + } +} diff --git a/support/spring-data-jpa/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryUtilsAdaptor.kt b/support/spring-data-jpa/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryUtilsAdaptor.kt deleted file mode 100644 index 222afa021..000000000 --- a/support/spring-data-jpa/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryUtilsAdaptor.kt +++ /dev/null @@ -1,13 +0,0 @@ -package org.springframework.data.jpa.repository.query - -import org.springframework.data.domain.Sort - -internal object QueryUtilsAdaptor { - fun applySorting(query: String, sort: Sort): String { - return QueryUtils.applySorting(query, sort) - } - - fun createCountQueryFor(originalQuery: String, countProjection: String?, nativeQuery: Boolean): String { - return QueryUtils.createCountQueryFor(originalQuery, countProjection, nativeQuery) - } -} diff --git a/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt b/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt index c48e78116..647af6c15 100644 --- a/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt +++ b/support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt @@ -22,7 +22,8 @@ import org.junit.jupiter.api.extension.ExtendWith import org.springframework.data.domain.Page import org.springframework.data.domain.PageRequest import org.springframework.data.domain.SliceImpl -import org.springframework.data.jpa.repository.query.QueryUtilsAdaptor +import org.springframework.data.jpa.repository.query.QueryEnhancer +import org.springframework.data.jpa.repository.query.QueryEnhancerFactoryAdaptor import org.springframework.data.support.PageableExecutionUtilsAdaptor import java.util.function.LongSupplier @@ -37,6 +38,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @MockK private lateinit var context: RenderContext + @MockK + private lateinit var queryEnhancer: QueryEnhancer + @MockK private lateinit var selectQuery1: SelectQuery @@ -69,7 +73,7 @@ class JpqlEntityManagerUtilsTest : WithAssertions { @Suppress("UnusedEquals") fun setUp() { mockkObject(JpqlRendererHolder) - mockkObject(QueryUtilsAdaptor) + mockkObject(QueryEnhancerFactoryAdaptor) mockkObject(PageableExecutionUtilsAdaptor) every { JpqlRendererHolder.get() } returns renderer @@ -233,7 +237,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { selectQuery1.returnType } returns String::class - every { QueryUtilsAdaptor.applySorting(any(), any()) } returns sortedQuery1 + every { QueryEnhancerFactoryAdaptor.forQuery(any()) } returns queryEnhancer + every { queryEnhancer.applySorting(any()) } returns sortedQuery1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 every { stringTypedQuery1.setFirstResult(any()) } returns stringTypedQuery1 @@ -250,7 +255,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { renderer.render(selectQuery1, context) selectQuery1.returnType - QueryUtilsAdaptor.applySorting(rendered1.query, pageable1.sort) + QueryEnhancerFactoryAdaptor.forQuery(rendered1.query) + queryEnhancer.applySorting(pageable1.sort) entityManager.createQuery(sortedQuery1, String::class.java) stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) @@ -273,8 +279,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { selectQuery1.returnType } returns String::class - every { QueryUtilsAdaptor.applySorting(any(), any()) } returns sortedQuery1 - every { QueryUtilsAdaptor.createCountQueryFor(any(), any(), any()) } returns countQuery1 + every { QueryEnhancerFactoryAdaptor.forQuery(any()) } returns queryEnhancer + every { queryEnhancer.applySorting(any()) } returns sortedQuery1 + every { queryEnhancer.createCountQueryFor() } returns countQuery1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 andThen longTypedQuery1 @@ -300,8 +307,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions { renderer.render(selectQuery1, context) selectQuery1.returnType - QueryUtilsAdaptor.applySorting(rendered1.query, pageable1.sort) - QueryUtilsAdaptor.createCountQueryFor(rendered1.query, null, false) + QueryEnhancerFactoryAdaptor.forQuery(rendered1.query) + queryEnhancer.applySorting(pageable1.sort) + queryEnhancer.createCountQueryFor() entityManager.createQuery(sortedQuery1, String::class.java) stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second) @@ -329,7 +337,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { every { renderer.render(any(), any()) } returns rendered1 every { selectQuery1.returnType } returns String::class - every { QueryUtilsAdaptor.applySorting(any(), any()) } returns sortedQuery1 + every { QueryEnhancerFactoryAdaptor.forQuery(any()) } returns queryEnhancer + every { queryEnhancer.applySorting(any()) } returns sortedQuery1 every { entityManager.createQuery(any(), any>()) } returns stringTypedQuery1 every { stringTypedQuery1.setParameter(any(), any()) } returns stringTypedQuery1 every { stringTypedQuery1.setFirstResult(any()) } returns stringTypedQuery1 @@ -346,7 +355,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions { renderer.render(selectQuery1, context) selectQuery1.returnType - QueryUtilsAdaptor.applySorting(rendered1.query, pageable1.sort) + QueryEnhancerFactoryAdaptor.forQuery(rendered1.query) + queryEnhancer.applySorting(pageable1.sort) entityManager.createQuery(sortedQuery1, String::class.java) stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)