spring-data-relational
spring-data-relational copied to clipboard
Add Type-safe Kotlin query extension
I am useing R2dbcEntityTemplate. and I try to create dynamic where sql using kotlin but there are not util function and I would like to get column name using kotlin KProperty.
can I make this one and contribute this repo?
Let's take a step back here before proposing code changes. Kotlin provides a lot of exciting opportunities to create a DSL for querying. Please also look at MongoDB first, where we have a similar approach.
We should clarify the goal first before proceeding here.
Hello @mp911de Sorry I didn't notice that one I can not find r2dbc mongodb project can you let me know which repo is that? then I will research about that one show you what I will do!
And I'll write down what I want to do as a PR
- I would like to create dynamic sql where stmt function
- I want to use reflection to get column names when I use R2dbcEntityTemplate. Because when I use R2dbcEntityTemplate I always use String type for putting the column name. so I always got a runtime exception when a column was changed. so I want to identify errors at compile time. example
// code
R2dbcEntityTemplate<Person, String>.column(Person::name);
R2dbcEntityTemplate<Person, Long>.column(Person::roleId);
// result: "name", "role_id"
this is the goal I would like to do. and if you have any opinion please let me know!
The code I was referring to is at:
https://github.com/spring-projects/spring-data-mongodb/blob/main/spring-data-mongodb/src/test/kotlin/org/springframework/data/mongodb/core/query/TypedCriteriaExtensionsTests.kt
Our extension provides then a criteria along the lines of:
val typed = Book::price gte 100
val criteria = Criteria("price").gte(100)
See also https://github.com/spring-projects/spring-data-mongodb/pull/622
@mp911de
I wonder one stuff how to get column name?? because in R2dbcEntityTemplate you used DefaultNamingStrategy.INSTANCE
for default.
But if people used different type of strategy then kotlin extension can not use like this Book::price gte 100
what about creating new R2dbcEntityTemplate function for creating column name in String type
this is the link: https://github.com/spring-projects/spring-data-relational/pull/1492/commits/acbc8141156a3c107dbfef9a9df3ee3d1faf4b99