All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.dbobjekts.metadata.column.SequenceKeyColumns.kt Maven / Gradle / Ivy

There is a newer version: 0.6.0-RC2
Show newest version
package com.dbobjekts.metadata.column

import com.dbobjekts.api.AnyTable
import com.dbobjekts.api.exception.DBObjektsException

interface SequenceKeyColumn : IsGeneratedPrimaryKey {
    fun qualifiedSequence(): String
    fun createValueForUpdate(key: Long): NullableColumnAndValue
}

class SequenceKeyLongColumn(
    table: AnyTable,
    name: String,
    val sequence: String,
    aggregateType: AggregateType?
) : LongColumn(table, name, aggregateType), SequenceKeyColumn {
    constructor(table: AnyTable, name: String, sequence: String) : this(table, name, sequence, null)

    override val nullable: NullableColumn = NullableLongColumn(table, name, aggregateType)

    override fun distinct() =
        throw DBObjektsException("distinct() operation is not supported on an auto-generated key, as this is most certainly not what you want. All values will be distinct.")

    override fun qualifiedSequence(): String =
        if (sequence.startsWith(table.schemaName().value)) sequence else "${table.schemaName()}.$sequence"

    override fun createValueForUpdate(key: Long): NullableColumnAndValue = NullableColumnAndValue(this, key)
}


class SequenceKeyIntegerColumn(
    table: AnyTable,
    name: String,
    val sequence: String,
    aggregateType: AggregateType?
) :
    IntegerColumn(table, name, aggregateType), SequenceKeyColumn {
    constructor(table: AnyTable, name: String, sequence: String) : this(table, name, sequence, null)

    override val nullable: NullableColumn = NullableIntegerColumn(table, name, aggregateType)

    override fun distinct() =
        throw DBObjektsException("distinct() operation is not supported on an auto-generated key, as this is most certainly not what you want. All values will be distinct.")

    override fun qualifiedSequence() = "${table.schemaName()}.$sequence"

    override fun createValueForUpdate(key: Long): NullableColumnAndValue = NullableColumnAndValue(this, key.toInt())
}