dave08
12/10/2023, 12:54 PMinnerJoin(Meta.baz) { Meta.foo.id eq Meta.tagging.recordId ;Meta.tagging.type eq "Foo" }
but recordId
is an Int
... can I somehow still do this w/o giving up on using those value classes? Something like Meta.tagging.recordId.map { FooId(it) }
?Toshihiro Nakamura
12/11/2023, 12:58 PMclass MyExtension(private val context: CriteriaContext) {
infix fun ColumnExpression<*, Int>.eq(operand: ColumnExpression<FooId, *>) {
val o1 = Operand.Column(this)
val o2 = Operand.Column(operand)
context.add {
visit(o1)
append(" = ")
visit(o2)
}
}
}
In you query, use the above class:
innerJoin(Meta.baz) {
extension(::MyExtension) {
Meta.foo.id eq Meta.tagging.recordId
Meta.tagging.type eq "Foo"
}
}
dave08
12/11/2023, 1:02 PMvisit(..)
manages to "transform" the property type to the value class's type how does it know to take it's value
property to extract the value from it?Toshihiro Nakamura
12/11/2023, 1:33 PMdave08
12/12/2023, 9:33 AMeq
to use that value class type (thanks for the reference to that code -- little by little I'm starting to understand Komapper's internals... which will greatly help using such things in the future...).
Don't you think it might have been nice to be able to do this not per-operator? It does happen to have such use cases (like in my case, it's an Tagging entity that allows to tag multiple types of entities, each having a different value class for their ids... that would be pretty cumbersome to write an extension for each of those id types and each of the matchers being used on them...)dave08
12/12/2023, 9:35 AMMeta.tagging.recordId.map { FooId(it) }
would make sense, but maybe something similar?Toshihiro Nakamura
12/13/2023, 12:43 PM@JvmInline
value class AaaId(val value: Int)
@JvmInline
value class BbbId(val value: Int)
@KomapperEntity
data class MyEntity(@KomapperId val id: Int, val aaaId: AaaId, val bbbId: BbbId)
val m = Meta.myEntity
QueryDsl.from(m).where { m.aaaId eq m.bbbId}
Toshihiro Nakamura
12/13/2023, 12:44 PMinnerJoin(Meta.baz) {
Meta.foo.id eq (Meta.tagging.recordId as ColumnExpression<Int, Int>)
Meta.tagging.type eq "Foo"
}
dave08
12/13/2023, 12:59 PMdave08
12/13/2023, 1:02 PMvalue class
w/o losing the type safety... as long as both inner types are Int
it should work they way you suggested, but if the user changes one to Long, for example, then it should really complain at compile time, not at run time...dave08
12/13/2023, 1:03 PMToshihiro Nakamura
12/13/2023, 1:32 PMdave08
12/13/2023, 2:20 PMToshihiro Nakamura
12/13/2023, 2:28 PM