Skip to content

Commit

Permalink
Merge pull request #665 from line/feature/spring-method-metadata
Browse files Browse the repository at this point in the history
feat: support crud method metadata of spring
  • Loading branch information
shouwn authored Mar 6, 2024
2 parents e294d18 + 8e8b229 commit c59b4c3
Show file tree
Hide file tree
Showing 26 changed files with 1,514 additions and 916 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.linecorp.kotlinjdsl.support.spring.batch.javax

import com.linecorp.kotlinjdsl.querymodel.jpql.select.SelectQuery
import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery
import com.linecorp.kotlinjdsl.render.RenderContext
import com.linecorp.kotlinjdsl.render.jpql.JpqlRendered
import com.linecorp.kotlinjdsl.render.jpql.JpqlRenderedParams
Expand All @@ -12,13 +12,14 @@ import kotlin.reflect.KClass
internal object JpqlEntityManagerUtils {
fun <T : Any> createQuery(
entityManager: EntityManager,
query: SelectQuery<T>,
query: JpqlQuery<*>,
queryParams: Map<String, Any?>,
returnType: KClass<T>,
context: RenderContext,
): TypedQuery<T> {
val rendered = JpqlRendererHolder.get().render(query, queryParams, context)

return createQuery(entityManager, rendered, query.returnType)
return createQuery(entityManager, rendered, returnType)
}

private fun <T : Any> createQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class KotlinJdslQueryProvider<T : Any>(
private val context: RenderContext,
) : AbstractJpaQueryProvider() {
override fun createQuery(): Query {
return JpqlEntityManagerUtils.createQuery(entityManager, query, queryParams, context)
return JpqlEntityManagerUtils.createQuery(entityManager, query, queryParams, query.returnType, context)
}

override fun afterPropertiesSet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,18 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
val rendered1 = JpqlRendered(renderedQuery1, JpqlRenderedParams(mapOf(renderedParam1, renderedParam2)))

every { renderer.render(any(), any(), any()) } returns rendered1
every { selectQuery1.returnType } returns String::class
every { entityManager.createQuery(any<String>(), any<Class<String>>()) } returns stringTypedQuery1
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1

// when
val actual = JpqlEntityManagerUtils
.createQuery(entityManager, selectQuery1, mapOf(queryParam1, queryParam2), context)
.createQuery(entityManager, selectQuery1, mapOf(queryParam1, queryParam2), String::class, context)

// then
assertThat(actual).isEqualTo(stringTypedQuery1)

verifySequence {
renderer.render(selectQuery1, mapOf(queryParam1, queryParam2), context)
selectQuery1.returnType
entityManager.createQuery(rendered1.query, String::class.java)
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import javax.persistence.EntityManager
import javax.persistence.TypedQuery
import kotlin.reflect.KClass

@ExtendWith(MockKExtension::class)
class KotlinJdslQueryProviderTest : WithAssertions {
Expand Down Expand Up @@ -46,8 +47,9 @@ class KotlinJdslQueryProviderTest : WithAssertions {
@Test
fun createQuery() {
// given
every { query1.returnType } returns String::class
every {
JpqlEntityManagerUtils.createQuery(any(), any<SelectQuery<String>>(), any(), any())
JpqlEntityManagerUtils.createQuery(any(), any<SelectQuery<String>>(), any(), any<KClass<*>>(), any())
} returns stringTypedQuery1

// when
Expand All @@ -57,10 +59,12 @@ class KotlinJdslQueryProviderTest : WithAssertions {
assertThat(actual).isEqualTo(stringTypedQuery1)

verifySequence {
query1.returnType
JpqlEntityManagerUtils.createQuery(
entityManager = entityManager,
query = query1,
queryParams = queryParams1,
String::class,
context = context,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.linecorp.kotlinjdsl.support.spring.batch

import com.linecorp.kotlinjdsl.querymodel.jpql.select.SelectQuery
import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery
import com.linecorp.kotlinjdsl.render.RenderContext
import com.linecorp.kotlinjdsl.render.jpql.JpqlRendered
import com.linecorp.kotlinjdsl.render.jpql.JpqlRenderedParams
Expand All @@ -12,13 +12,14 @@ import kotlin.reflect.KClass
internal object JpqlEntityManagerUtils {
fun <T : Any> createQuery(
entityManager: EntityManager,
query: SelectQuery<T>,
query: JpqlQuery<*>,
queryParams: Map<String, Any?>,
returnType: KClass<T>,
context: RenderContext,
): TypedQuery<T> {
val rendered = JpqlRendererHolder.get().render(query, queryParams, context)

return createQuery(entityManager, rendered, query.returnType)
return createQuery(entityManager, rendered, returnType)
}

private fun <T : Any> createQuery(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class KotlinJdslQueryProvider<T : Any>(
private val context: RenderContext,
) : AbstractJpaQueryProvider() {
override fun createQuery(): Query {
return JpqlEntityManagerUtils.createQuery(entityManager, query, queryParams, context)
return JpqlEntityManagerUtils.createQuery(entityManager, query, queryParams, query.returnType, context)
}

override fun afterPropertiesSet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,18 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
val rendered1 = JpqlRendered(renderedQuery1, JpqlRenderedParams(mapOf(renderedParam1, renderedParam2)))

every { renderer.render(any(), any(), any()) } returns rendered1
every { selectQuery1.returnType } returns String::class
every { entityManager.createQuery(any<String>(), any<Class<String>>()) } returns stringTypedQuery1
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1

// when
val actual = JpqlEntityManagerUtils
.createQuery(entityManager, selectQuery1, mapOf(queryParam1, queryParam2), context)
.createQuery(entityManager, selectQuery1, mapOf(queryParam1, queryParam2), String::class, context)

// then
assertThat(actual).isEqualTo(stringTypedQuery1)

verifySequence {
renderer.render(selectQuery1, mapOf(queryParam1, queryParam2), context)
selectQuery1.returnType
entityManager.createQuery(rendered1.query, String::class.java)
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import kotlin.reflect.KClass

@ExtendWith(MockKExtension::class)
class KotlinJdslQueryProviderTest : WithAssertions {
Expand Down Expand Up @@ -46,8 +47,9 @@ class KotlinJdslQueryProviderTest : WithAssertions {
@Test
fun createQuery() {
// given
every { query1.returnType } returns String::class
every {
JpqlEntityManagerUtils.createQuery(any(), any<SelectQuery<String>>(), any(), any())
JpqlEntityManagerUtils.createQuery(any(), any<SelectQuery<String>>(), any(), any<KClass<*>>(), any())
} returns stringTypedQuery1

// when
Expand All @@ -57,10 +59,12 @@ class KotlinJdslQueryProviderTest : WithAssertions {
assertThat(actual).isEqualTo(stringTypedQuery1)

verifySequence {
query1.returnType
JpqlEntityManagerUtils.createQuery(
entityManager = entityManager,
query = query1,
queryParams = queryParams1,
String::class,
context = context,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.linecorp.kotlinjdsl.support.spring.data.jpa.javax

import javax.persistence.TypedQuery

internal class EnhancedTypedQuery<T : Any>(
val sortedQuery: TypedQuery<T>,
countQuery: () -> TypedQuery<Long>,
) {
val countQuery: TypedQuery<Long> by lazy(LazyThreadSafetyMode.NONE) { countQuery() }
}
Loading

0 comments on commit c59b4c3

Please sign in to comment.