我设置了一个基于 Kotlin Exposed DAO 的数据库,并且想清理重复的列名。
例如,假设我有一个看起来像这样的表:
// Yes, super contrived, not my real use case. Just an minimal example
class ColourComponentTable : IntIdTable() {
val name = text("name")
}
class ColourTable : IntIdTable() {
val name = text("name")
val r = reference("r", ColourComponentTable)
val g = reference("g", ColourComponentTable)
val b = reference("b", ColourComponentTable)
}
// Maybe this is for a colour theme or something
class ColourBasisTable : IntIdTable() {
val name = text("name")
// primary colours
val r1 = reference("r1", ColourTable)
val g1 = reference("g1", ColourTable)
val b1 = reference("b1", ColourTable)
// backup colours
val r2 = reference("r2", ColourTable)
val g2 = reference("g2", ColourTable)
val b2 = reference("b2", ColourTable)
}
// Objects
class ColourComponent(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<ColourComponent>(ColourComponentTable)
var name by ColourComponentTable.name
}
class Colour(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Colour>(ColourTable)
var name by ColourTable.name
var r by ColourComponent referencedOn ColourTable.r
var g by ColourComponent referencedOn ColourTable.g
var b by ColourComponent referencedOn ColourTable.b
}
class ColourBasis(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<ColourBasis>(ColourBasisTable)
var name by ColourBasisTable.name
var r1 by Colour referencedOn ColourBasisTable.r1
var g1 by Colour referencedOn ColourBasisTable.g1
var b1 by Colour referencedOn ColourBasisTable.b1
var r2 by Colour referencedOn ColourBasisTable.r2
var g2 by Colour referencedOn ColourBasisTable.g2
var b2 by Colour referencedOn ColourBasisTable.b2
}
我想在自定义列类型中“包装”重复的类似列。到目前为止,我已经尝试了相当于:
class ColourComponentTable : IntIdTable() {
val name = text("name")
}
class RGBColumn< R : Comparable<R>, T : Comparable<T>>(
suffix: String = "",
parent: IdTable<R>,
ref: IdTable<T>
) {
val r = parent.text("r" + suffix)
val g = parent.text("g" + suffix)
val b = parent.text("b" + suffix)
}
class RGBObject<Target : IntEntity, R : Comparable<R>, T : Comparable<T>>(
ref : RGBColumn<R, T>
) {
var r by Target referencedOn ref.r
var g by Target referencedOn ref.g
var b by Target referencedOn ref.b
}
class ColourTable : IntIdTable() {
val name = text("name")
val c = RGBColumn("", this, ColourComponentTable)
}
class ColourBasisTable : IntIdTable() {
val name = text("name")
// primary colours
val c1 = RGBColumn("1", this, ColourTable)
// backup colours
val c2 = RGBColumn("2", this, ColourTable)
}
// Objects
class ColourComponent(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<ColourComponent>(ColourComponentTable)
var name by ColourComponentTable.name
}
class Colour(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Colour>(ColourTable)
var name by ColourTable.name
var c by RGBObject<ColourComponent> referencedOn ColourTable.c
}
class ColourBasis(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<ColourBasis>(ColourBasisTable)
var name by ColourBasisTable.name
var c1 by RGBObject<Colour> referencedOn ColourBasisTable.c1
var c2 by RGBObject<Colour> referencedOn ColourBasisTable.c2
}
当然,这是行不通的。我觉得我很接近。特别是我觉得我应该能够让自定义列/对象对工作。我已经尝试了各种可能的泛型参数组合RGBObject
,但没有一个可以编译代码。
有什么我想念的想法吗?这甚至可能吗?