Skip to content

Commit

Permalink
refactor: remove duplicated code in support modules
Browse files Browse the repository at this point in the history
  • Loading branch information
shouwn committed Mar 30, 2024
1 parent ea8b998 commit 5780ef7
Show file tree
Hide file tree
Showing 50 changed files with 899 additions and 1,725 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package com.linecorp.kotlinjdsl.support.eclipselink.javax

import com.linecorp.kotlinjdsl.querymodel.jpql.delete.DeleteQuery
import com.linecorp.kotlinjdsl.querymodel.jpql.select.SelectQuery
import com.linecorp.kotlinjdsl.querymodel.jpql.update.UpdateQuery
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
import javax.persistence.EntityManager
import javax.persistence.Query
import javax.persistence.TypedQuery
Expand All @@ -14,87 +10,70 @@ import kotlin.reflect.KClass
internal object JpqlEntityManagerUtils {
fun <T : Any> createQuery(
entityManager: EntityManager,
query: SelectQuery<T>,
query: JpqlQuery<*>,
returnType: KClass<T>,
context: RenderContext,
): TypedQuery<T> {
val rendered = JpqlRendererHolder.get().render(query, context)

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

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.query, rendered.params, returnType.java)
}

fun <T : Any> createQuery(
fun createQuery(
entityManager: EntityManager,
query: UpdateQuery<T>,
query: JpqlQuery<*>,
context: RenderContext,
): Query {
val rendered = JpqlRendererHolder.get().render(query, context)

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

fun <T : Any> createQuery(
fun createQuery(
entityManager: EntityManager,
query: UpdateQuery<T>,
query: JpqlQuery<*>,
queryParams: Map<String, Any?>,
context: RenderContext,
): Query {
val rendered = JpqlRendererHolder.get().render(query, queryParams, context)

return createQuery(entityManager, rendered)
}

fun <T : Any> createQuery(
entityManager: EntityManager,
query: DeleteQuery<T>,
context: RenderContext,
): Query {
val rendered = JpqlRendererHolder.get().render(query, context)

return createQuery(entityManager, rendered)
}

fun <T : Any> createQuery(
entityManager: EntityManager,
query: DeleteQuery<T>,
queryParams: Map<String, Any?>,
context: RenderContext,
): Query {
val rendered = JpqlRendererHolder.get().render(query, queryParams, context)

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

private fun <T : Any> createQuery(
entityManager: EntityManager,
rendered: JpqlRendered,
resultClass: KClass<T>,
query: String,
queryParams: Map<String, Any?>,
returnType: Class<T>,
): TypedQuery<T> {
return entityManager.createQuery(rendered.query, resultClass.java).apply {
setParams(this, rendered.params)
return entityManager.createQuery(query, returnType).apply {
setParams(this, queryParams)
}
}

private fun createQuery(
entityManager: EntityManager,
rendered: JpqlRendered,
query: String,
queryParams: Map<String, Any?>,
): Query {
return entityManager.createQuery(rendered.query).apply {
setParams(this, rendered.params)
return entityManager.createQuery(query).apply {
setParams(this, queryParams)
}
}

private fun setParams(query: Query, params: JpqlRenderedParams) {
private fun setParams(query: Query, params: Map<String, Any?>) {
params.forEach { (name, value) ->
query.setParameter(name, value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ import javax.persistence.Query
import javax.persistence.TypedQuery

/**
* Creates [javax.persistence.TypedQuery] from the [SelectQuery] and [RenderContext].
* Creates a [javax.persistence.TypedQuery] from the [SelectQuery] and [RenderContext].
*/
@SinceJdsl("3.0.0")
fun <T : Any> EntityManager.createQuery(
query: SelectQuery<T>,
context: RenderContext,
): TypedQuery<T> = JpqlEntityManagerUtils.createQuery(this, query, context)
): TypedQuery<T> = JpqlEntityManagerUtils.createQuery(this, query, query.returnType, context)

/**
* Creates [javax.persistence.TypedQuery] from the [SelectQuery] and [RenderContext].
* Creates a [javax.persistence.TypedQuery] from the [SelectQuery] and [RenderContext].
*/
@SinceJdsl("3.0.0")
fun <T : Any> EntityManager.createQuery(
query: SelectQuery<T>,
queryParams: Map<String, Any?>,
context: RenderContext,
): TypedQuery<T> = JpqlEntityManagerUtils.createQuery(this, query, queryParams, context)
): TypedQuery<T> = JpqlEntityManagerUtils.createQuery(this, query, queryParams, query.returnType, context)

/**
* Creates [javax.persistence.Query] from the [UpdateQuery] and [RenderContext].
* Creates a [javax.persistence.Query] from the [UpdateQuery] and [RenderContext].
*/
@SinceJdsl("3.0.0")
fun <T : Any> EntityManager.createQuery(
Expand All @@ -39,7 +39,7 @@ fun <T : Any> EntityManager.createQuery(
): Query = JpqlEntityManagerUtils.createQuery(this, query, context)

/**
* Creates [javax.persistence.Query] from the [UpdateQuery] and [RenderContext].
* Creates a [javax.persistence.Query] from the [UpdateQuery] and [RenderContext].
*/
@SinceJdsl("3.0.0")
fun <T : Any> EntityManager.createQuery(
Expand All @@ -49,7 +49,7 @@ fun <T : Any> EntityManager.createQuery(
): Query = JpqlEntityManagerUtils.createQuery(this, query, queryParams, context)

/**
* Creates [javax.persistence.Query] from the [DeleteQuery] and [RenderContext].
* Creates a [javax.persistence.Query] from the [DeleteQuery] and [RenderContext].
*/
@SinceJdsl("3.0.0")
fun <T : Any> EntityManager.createQuery(
Expand All @@ -58,7 +58,7 @@ fun <T : Any> EntityManager.createQuery(
): Query = JpqlEntityManagerUtils.createQuery(this, query, context)

/**
* Creates [javax.persistence.Query] from the [DeleteQuery] and [RenderContext].
* Creates a [javax.persistence.Query] from the [DeleteQuery] and [RenderContext].
*/
@SinceJdsl("3.0.0")
fun <T : Any> EntityManager.createQuery(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.linecorp.kotlinjdsl.support.eclipselink.javax

import com.linecorp.kotlinjdsl.querymodel.jpql.delete.DeleteQuery
import com.linecorp.kotlinjdsl.querymodel.jpql.select.SelectQuery
import com.linecorp.kotlinjdsl.querymodel.jpql.update.UpdateQuery
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 Down Expand Up @@ -32,13 +30,7 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
private lateinit var context: RenderContext

@MockK
private lateinit var selectQuery1: SelectQuery<String>

@MockK
private lateinit var updateQuery1: UpdateQuery<String>

@MockK
private lateinit var deleteQuery1: DeleteQuery<String>
private lateinit var query1: JpqlQuery<*>

@MockK
private lateinit var stringTypedQuery1: TypedQuery<String>
Expand All @@ -62,105 +54,54 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
}

@Test
fun `createQuery() with a select query`() {
fun `createQuery() with a query and a return type`() {
// given
val rendered1 = JpqlRendered(renderedQuery1, JpqlRenderedParams(mapOf(renderedParam1, renderedParam2)))

every { renderer.render(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, context)
val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, String::class, context)

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

verifySequence {
renderer.render(selectQuery1, context)
selectQuery1.returnType
renderer.render(query1, context)
entityManager.createQuery(rendered1.query, String::class.java)
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
}

@Test
fun `createQuery() with a select query and query params`() {
fun `createQuery() with a query and query params and a return type`() {
// given
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)
val actual = JpqlEntityManagerUtils
.createQuery(entityManager, query1, mapOf(queryParam1, queryParam2), String::class, context)

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

verifySequence {
renderer.render(selectQuery1, mapOf(queryParam1, queryParam2), context)
selectQuery1.returnType
renderer.render(query1, mapOf(queryParam1, queryParam2), context)
entityManager.createQuery(rendered1.query, String::class.java)
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
}

@Test
fun `createQuery() with an update query`() {
// given
val rendered1 = JpqlRendered(renderedQuery1, JpqlRenderedParams(mapOf(renderedParam1, renderedParam2)))

every { renderer.render(any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>()) } returns stringTypedQuery1
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1

// when
val actual = JpqlEntityManagerUtils.createQuery(entityManager, updateQuery1, context)

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

verifySequence {
renderer.render(updateQuery1, context)
entityManager.createQuery(rendered1.query)
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
}

@Test
fun `createQuery() with an update query and query params`() {
// given
val rendered1 = JpqlRendered(renderedQuery1, JpqlRenderedParams(mapOf(renderedParam1, renderedParam2)))

every { renderer.render(any(), any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>()) } returns stringTypedQuery1
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1

// when
val actual =
JpqlEntityManagerUtils.createQuery(entityManager, updateQuery1, mapOf(queryParam1, queryParam2), context)

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

verifySequence {
renderer.render(updateQuery1, mapOf(queryParam1, queryParam2), context)
entityManager.createQuery(rendered1.query)
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
}

@Test
fun `createQuery() with a delete query`() {
fun `createQuery() with a query`() {
// given
val rendered1 = JpqlRendered(renderedQuery1, JpqlRenderedParams(mapOf(renderedParam1, renderedParam2)))

Expand All @@ -169,21 +110,21 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1

// when
val actual = JpqlEntityManagerUtils.createQuery(entityManager, deleteQuery1, context)
val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context)

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

verifySequence {
renderer.render(deleteQuery1, context)
renderer.render(query1, context)
entityManager.createQuery(rendered1.query)
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
}

@Test
fun `createQuery() with a delete query and query params`() {
fun `createQuery() with a query and query params`() {
// given
val rendered1 = JpqlRendered(renderedQuery1, JpqlRenderedParams(mapOf(renderedParam1, renderedParam2)))

Expand All @@ -192,14 +133,14 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1

// when
val actual =
JpqlEntityManagerUtils.createQuery(entityManager, deleteQuery1, mapOf(queryParam1, queryParam2), context)
val actual = JpqlEntityManagerUtils
.createQuery(entityManager, query1, mapOf(queryParam1, queryParam2), context)

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

verifySequence {
renderer.render(deleteQuery1, mapOf(queryParam1, queryParam2), context)
renderer.render(query1, mapOf(queryParam1, queryParam2), context)
entityManager.createQuery(rendered1.query)
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
Expand Down
Loading

0 comments on commit 5780ef7

Please sign in to comment.