Skip to content

Instantly share code, notes, and snippets.

@nakamura-to
Created July 21, 2024 13:55
Show Gist options
  • Save nakamura-to/716674f0d921226707fdbb361fd979fe to your computer and use it in GitHub Desktop.
Save nakamura-to/716674f0d921226707fdbb361fd979fe to your computer and use it in GitHub Desktop.
KomapperのEntityMetamodelのためにインタフェースを定義する案
import org.komapper.annotation.*
import org.komapper.core.dsl.expression.PropertyExpression
import org.komapper.core.dsl.expression.WhereDeclaration
import java.time.LocalDateTime
// EntityMetamodelのためのインタフェース
interface BiTemporal {
val validFrom: PropertyExpression<LocalDateTime, LocalDateTime>
val validTo: PropertyExpression<LocalDateTime, LocalDateTime>
}
// 上記インタフェースを指定
@KomapperEntity(interfaces = [BiTemporal::class])
data class Employee(
@KomapperId @KomapperAutoIncrement
val id: Int = 0,
val name: String,
val validFrom: LocalDateTime = LocalDateTime.MIN,
val validTime: LocalDateTime = LocalDateTime.MIN,
)
// 上記インタフェースを指定
@KomapperEntity(interfaces = [BiTemporal::class])
data class Address(
@KomapperId @KomapperAutoIncrement
val id: Int = 0,
val name: String,
val validFrom: LocalDateTime = LocalDateTime.MIN,
val validTime: LocalDateTime = LocalDateTime.MIN,
)
// KSPで生成されるメタモデルは指定したインタフェースを実装する
fun main() {
val e = Meta.employee
val a = Meta.address
val dateTime = LocalDateTime.of(2024, 1, 1, 0, 0, 0)
val w1 = commonWhere(e, dateTime)
val w2 = commonWhere(a, dateTime)
}
// 共通の検索条件
fun commonWhere(metamodel: BiTemporal, dateTime: LocalDateTime): WhereDeclaration {
return {
metamodel.validFrom less dateTime
metamodel.validTo greater dateTime
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment