Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Type-safe Kotlin query extension like mongo template #1937

Closed
jurogrammer opened this issue Nov 13, 2024 · 2 comments
Closed

Type-safe Kotlin query extension like mongo template #1937

jurogrammer opened this issue Nov 13, 2024 · 2 comments
Labels
status: duplicate A duplicate of another issue

Comments

@jurogrammer
Copy link

jurogrammer commented Nov 13, 2024

I would like to perform type-safe queries using R2dbcTemplate. My goal is to achieve functionality similar to MongoDB's template, as you suggested—reference test code.

However, there’s an issue with handling column names, as described in this issue. I’m considering a default strategy for resolving column names, though this might limit the flexibility of custom column name strategies, similar to MongoDB’s template, which only considers property names.

Are there any considerations I should keep in mind?

extension

import org.springframework.data.relational.core.query.Criteria
import org.springframework.data.relational.core.query.Criteria.CriteriaStep
import org.springframework.data.util.ParsingUtils
import kotlin.reflect.KProperty


fun where(key: KProperty<*>): CriteriaStep = Criteria.where(key.toColumn())
internal fun KProperty<*>.toColumn(): String = ParsingUtils.reconcatenateCamelCase(this.name, "_")

test

Since CriteriaBuilder doesn't override equals, I'm using toString for comparison.

import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.springframework.data.relational.core.query.Criteria

class CriteriaExtensionsKtTest {

    @Test
    fun `where(KProperty) should equal Criteria where()`() {
        val typedCriteria = where(Customer::firstName).`is`("John").toString()
        val classicCriteria = Criteria.where("first_name").`is`("John").toString()

        assertThat(typedCriteria).isEqualTo(classicCriteria)
    }


    class Customer(val firstName: String, val lastName: String)
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 13, 2024
@mp911de
Copy link
Member

mp911de commented Nov 14, 2024

This is a duplicate of #1491. The comment about column names is misleading. Spring's API works primarily with property names (KProperty maps to property names as well). Having a Criteria object that is being mapped against the domain model removes the need in a Kotlin extension to resolve column names.

I suggest that you file a Pull Request and use MongoDB's implementation as inspiration for the Relational implementation.

Therefore, I'm closing this ticket as a duplicate. Happy to continue the discussion over an actual PR.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Nov 14, 2024
@mp911de mp911de added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 14, 2024
@jurogrammer
Copy link
Author

jurogrammer commented Nov 15, 2024

thank you for your help!👍

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants