Kotlin公开的自定义列模块化

时间:2019-12-05 01:44:23

标签: kotlin dao kotlin-exposed

我已经建立了一个基于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可能的通用参数的各种组合,但是 他们都不会得到要编译的代码。

有什么主意我想念的吗?这有可能吗?

0 个答案:

没有答案