Skip to content

Commit

Permalink
fix: silently ignores exception when parameter setting to count query (
Browse files Browse the repository at this point in the history
…#781)

* fix: silently ignores exception when parameter setting to count query

* fix: use map instead of try-catch

* fix: test error fix and change map to set

* fix: revert divided methods into one method

* fix: debug logging when parameter binding is skipped

* fix: minor grammar fix

* fix: apply exception ignore to all in support

* refactor: logging with string template

* refactor: debug logic into else statement
  • Loading branch information
bagger3025 authored Oct 15, 2024
1 parent e6a4209 commit 7105c9a
Show file tree
Hide file tree
Showing 32 changed files with 512 additions and 20 deletions.
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

0 comments on commit 7105c9a

Please sign in to comment.