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

fix: silently ignores exception when parameter setting to count query #781

Merged
merged 9 commits into from
Oct 15, 2024
1 change: 1 addition & 0 deletions support/eclipselink-javax/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dependencies {
compileOnly(libs.eclipselink2)
compileOnly(libs.javax.persistence.api)
compileOnly(libs.slf4j)
compileOnly(projects.jpqlDsl)
compileOnly(projects.jpqlQueryModel)
compileOnly(projects.jpqlRender)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.linecorp.kotlinjdsl.support.eclipselink.javax

import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery
import com.linecorp.kotlinjdsl.render.RenderContext
import org.slf4j.LoggerFactory
import javax.persistence.EntityManager
import javax.persistence.Query
import javax.persistence.TypedQuery
Expand Down Expand Up @@ -74,8 +75,22 @@ internal object JpqlEntityManagerUtils {
}

private fun setParams(query: Query, params: Map<String, Any?>) {
val parameterNameSet = query.parameters.map { it.name }.toHashSet()

params.forEach { (name, value) ->
query.setParameter(name, value)
if (parameterNameSet.contains(name)) {
query.setParameter(name, value)
} else {
if (log.isDebugEnabled) {
log.debug(
"No parameter named '$name' in query " +
"with named parameters [${parameterNameSet.joinToString()}], " +
"parameter binding skipped",
)
}
}
}
}
}

private val log = LoggerFactory.getLogger(JpqlEntityManagerUtils::class.java)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import javax.persistence.EntityManager
import javax.persistence.Parameter
import javax.persistence.TypedQuery

@ExtendWith(MockKExtension::class)
Expand All @@ -35,6 +36,12 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
@MockK
private lateinit var stringTypedQuery1: TypedQuery<String>

@MockK
private lateinit var stringTypedQueryParam1: Parameter<String>

@MockK
private lateinit var stringTypedQueryParam2: Parameter<String>

private val renderedQuery1 = "query"
private val renderedParam1 = "queryParam1" to "queryParamValue1"
private val renderedParam2 = "queryParam2" to "queryParamValue2"
Expand All @@ -51,6 +58,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

excludeRecords { JpqlRendererHolder.get() }
excludeRecords { stringTypedQuery1.equals(any()) }
excludeRecords { stringTypedQueryParam1.hashCode() }
excludeRecords { stringTypedQueryParam2.hashCode() }
}

@Test
Expand All @@ -60,7 +69,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>(), any<Class<String>>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, String::class, context)
Expand All @@ -71,6 +83,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
verifySequence {
renderer.render(query1, context)
entityManager.createQuery(rendered1.query, String::class.java)
stringTypedQuery1.parameters
stringTypedQueryParam1.name
stringTypedQueryParam2.name
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
Expand All @@ -83,7 +98,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>(), any<Class<String>>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils
Expand All @@ -95,6 +113,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
verifySequence {
renderer.render(query1, mapOf(queryParam1, queryParam2), context)
entityManager.createQuery(rendered1.query, String::class.java)
stringTypedQuery1.parameters
stringTypedQueryParam1.name
stringTypedQueryParam2.name
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
Expand All @@ -107,7 +128,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context)
Expand All @@ -118,6 +142,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
verifySequence {
renderer.render(query1, context)
entityManager.createQuery(rendered1.query)
stringTypedQuery1.parameters
stringTypedQueryParam1.name
stringTypedQueryParam2.name
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
Expand All @@ -130,7 +157,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils
Expand All @@ -142,6 +172,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
verifySequence {
renderer.render(query1, mapOf(queryParam1, queryParam2), context)
entityManager.createQuery(rendered1.query)
stringTypedQuery1.parameters
stringTypedQueryParam1.name
stringTypedQueryParam2.name
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
Expand Down
1 change: 1 addition & 0 deletions support/eclipselink/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
dependencies {
compileOnly(libs.eclipselink3)
compileOnly(libs.jakarta.persistence.api)
compileOnly(libs.slf4j)
compileOnly(projects.jpqlDsl)
compileOnly(projects.jpqlQueryModel)
compileOnly(projects.jpqlRender)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.linecorp.kotlinjdsl.render.RenderContext
import jakarta.persistence.EntityManager
import jakarta.persistence.Query
import jakarta.persistence.TypedQuery
import org.slf4j.LoggerFactory
import kotlin.reflect.KClass

internal object JpqlEntityManagerUtils {
Expand Down Expand Up @@ -74,8 +75,22 @@ internal object JpqlEntityManagerUtils {
}

private fun setParams(query: Query, params: Map<String, Any?>) {
val parameterNameSet = query.parameters.map { it.name }.toHashSet()

params.forEach { (name, value) ->
query.setParameter(name, value)
if (parameterNameSet.contains(name)) {
query.setParameter(name, value)
} else {
if (log.isDebugEnabled) {
log.debug(
"No parameter named '$name' in query " +
"with named parameters [${parameterNameSet.joinToString()}], " +
"parameter binding skipped",
)
}
}
}
}
}

private val log = LoggerFactory.getLogger(JpqlEntityManagerUtils::class.java)
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import io.mockk.junit5.MockKExtension
import io.mockk.mockkObject
import io.mockk.verifySequence
import jakarta.persistence.EntityManager
import jakarta.persistence.Parameter
import jakarta.persistence.TypedQuery
import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.BeforeEach
Expand All @@ -35,6 +36,12 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
@MockK
private lateinit var stringTypedQuery1: TypedQuery<String>

@MockK
private lateinit var stringTypedQueryParam1: Parameter<String>

@MockK
private lateinit var stringTypedQueryParam2: Parameter<String>

private val renderedQuery1 = "query"
private val renderedParam1 = "queryParam1" to "queryParamValue1"
private val renderedParam2 = "queryParam2" to "queryParamValue2"
Expand All @@ -51,6 +58,8 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

excludeRecords { JpqlRendererHolder.get() }
excludeRecords { stringTypedQuery1.equals(any()) }
excludeRecords { stringTypedQueryParam1.hashCode() }
excludeRecords { stringTypedQueryParam2.hashCode() }
}

@Test
Expand All @@ -60,7 +69,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>(), any<Class<String>>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, String::class, context)
Expand All @@ -71,6 +83,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
verifySequence {
renderer.render(query1, context)
entityManager.createQuery(rendered1.query, String::class.java)
stringTypedQuery1.parameters
stringTypedQueryParam1.name
stringTypedQueryParam2.name
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
Expand All @@ -83,7 +98,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>(), any<Class<String>>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils
Expand All @@ -95,6 +113,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
verifySequence {
renderer.render(query1, mapOf(queryParam1, queryParam2), context)
entityManager.createQuery(rendered1.query, String::class.java)
stringTypedQuery1.parameters
stringTypedQueryParam1.name
stringTypedQueryParam2.name
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
Expand All @@ -107,7 +128,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils.createQuery(entityManager, query1, context)
Expand All @@ -118,6 +142,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
verifySequence {
renderer.render(query1, context)
entityManager.createQuery(rendered1.query)
stringTypedQuery1.parameters
stringTypedQueryParam1.name
stringTypedQueryParam2.name
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
Expand All @@ -130,7 +157,10 @@ class JpqlEntityManagerUtilsTest : WithAssertions {

every { renderer.render(any(), any(), any()) } returns rendered1
every { entityManager.createQuery(any<String>()) } returns stringTypedQuery1
every { stringTypedQuery1.parameters } returns setOf(stringTypedQueryParam1, stringTypedQueryParam2)
every { stringTypedQuery1.setParameter(any<String>(), any()) } returns stringTypedQuery1
every { stringTypedQueryParam1.name } returns renderedParam1.first
every { stringTypedQueryParam2.name } returns renderedParam2.first

// when
val actual = JpqlEntityManagerUtils
Expand All @@ -142,6 +172,9 @@ class JpqlEntityManagerUtilsTest : WithAssertions {
verifySequence {
renderer.render(query1, mapOf(queryParam1, queryParam2), context)
entityManager.createQuery(rendered1.query)
stringTypedQuery1.parameters
stringTypedQueryParam1.name
stringTypedQueryParam2.name
stringTypedQuery1.setParameter(renderedParam1.first, renderedParam1.second)
stringTypedQuery1.setParameter(renderedParam2.first, renderedParam2.second)
}
Expand Down
1 change: 1 addition & 0 deletions support/hibernate-javax/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ dependencies {
@Suppress("VulnerableLibrariesLocal", "RedundantSuppression")
compileOnly(libs.hibernate5.core)
compileOnly(libs.javax.persistence.api)
compileOnly(libs.slf4j)
compileOnly(projects.jpqlDsl)
compileOnly(projects.jpqlQueryModel)
compileOnly(projects.jpqlRender)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package com.linecorp.kotlinjdsl.support.hibernate

import com.linecorp.kotlinjdsl.querymodel.jpql.JpqlQuery
import com.linecorp.kotlinjdsl.render.RenderContext
import org.slf4j.LoggerFactory
import javax.persistence.EntityManager
import javax.persistence.Query
import javax.persistence.TypedQuery
Expand Down Expand Up @@ -76,8 +77,22 @@ internal object JpqlEntityManagerUtils {
}

private fun setParams(query: Query, params: Map<String, Any?>) {
val parameterNameSet = query.parameters.map { it.name }.toHashSet()

params.forEach { (name, value) ->
query.setParameter(name, value)
if (parameterNameSet.contains(name)) {
query.setParameter(name, value)
} else {
if (log.isDebugEnabled) {
log.debug(
"No parameter named '$name' in query " +
"with named parameters [${parameterNameSet.joinToString()}], " +
"parameter binding skipped",
)
}
}
}
}
}

private val log = LoggerFactory.getLogger(JpqlEntityManagerUtils::class.java)
Loading
Loading