com.vladsch.kotlin.jdbc.ModelPropertyProviderBase.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-jdbc Show documentation
Show all versions of kotlin-jdbc Show documentation
A thin library that exposes JDBC API with the convenience of Kotlin and gets out of the way when not needed.
The newest version!
package com.vladsch.kotlin.jdbc
import kotlin.reflect.KProperty
open class ModelPropertyProviderBase(val provider: ModelProperties, val propType: PropertyType, override val columnName: String?, override val defaultValue:Any? = Unit) : InternalModelPropertyProvider {
final override fun provideDelegate(thisRef: T, prop: KProperty<*>): ModelProperties {
return provider.registerProp(prop, propType, columnName, defaultValue)
}
final override val autoKey: ModelPropertyProvider
get() = provider.autoKey
override val key: ModelPropertyProvider
get() = provider.key
override val auto: ModelPropertyProvider
get() = provider.auto
override val default: ModelPropertyProvider
get() = provider.default
final override operator fun getValue(thisRef: T, property: KProperty<*>): V {
return provider.getValue(thisRef, property)
}
final override operator fun setValue(thisRef: T, property: KProperty<*>, value: V) {
provider.setValue(thisRef, property, value)
}
override fun column(columnName: String?): ModelPropertyProvider {
return if (this.columnName == columnName) {
this
} else {
if (columnName == null) {
provider
} else {
ModelPropertyProviderBase(provider, propType, columnName)
}
}
}
override fun default(value: Any?): ModelPropertyProvider {
return ModelPropertyProviderBase(provider, propType, columnName, value)
}
}
class ModelPropertyProviderAutoKey(provider: ModelProperties, columnName: String?) : ModelPropertyProviderBase(provider, PropertyType.AUTO_KEY, columnName) {
override val key: ModelPropertyProvider
get() = this
override val auto: ModelPropertyProvider
get() = this
override val default: ModelPropertyProvider
get() = this
override fun column(columnName: String?): ModelPropertyProvider {
return if (this.columnName == columnName) {
this
} else {
if (columnName == null) {
provider.autoKey
} else {
ModelPropertyProviderAutoKey(provider, columnName)
}
}
}
override fun default(value: Any?): ModelPropertyProvider {
if (value === Unit) return this
throw IllegalStateException("Auto Key column cannot have a default value")
}
}
class ModelPropertyProviderKey(provider: ModelProperties, columnName: String?) : ModelPropertyProviderBase(provider, PropertyType.KEY, columnName) {
override val key: ModelPropertyProvider
get() = this
override val auto: ModelPropertyProvider
get() = provider.autoKey
override val default: ModelPropertyProvider
get() = provider.autoKey
override fun column(columnName: String?): ModelPropertyProvider {
return if (this.columnName == columnName) {
this
} else {
if (columnName == null) {
provider.key
} else {
ModelPropertyProviderKey(provider, columnName)
}
}
}
override fun default(value: Any?): ModelPropertyProvider {
if (value === Unit) return this
throw IllegalStateException("Key column cannot have a default value")
}
}
class ModelPropertyProviderAuto(provider: ModelProperties, columnName: String?) : ModelPropertyProviderBase(provider, PropertyType.AUTO, columnName) {
override val key: ModelPropertyProvider
get() = provider.autoKey
override val auto: ModelPropertyProvider
get() = this
override val default: ModelPropertyProvider
get() = this
override fun column(columnName: String?): ModelPropertyProvider {
return if (this.columnName == columnName) {
this
} else {
if (columnName == null) {
provider.auto
} else {
ModelPropertyProviderAuto(provider, columnName)
}
}
}
override fun default(value: Any?): ModelPropertyProvider {
if (value === Unit) return this
throw IllegalStateException("Auto column cannot have a default value")
}
}
class ModelPropertyProviderDefault(provider: ModelProperties, columnName: String?, value: Any?) : ModelPropertyProviderBase(provider, PropertyType.DEFAULT, columnName, value) {
override val key: ModelPropertyProvider
get() = provider.autoKey
override val auto: ModelPropertyProvider
get() = provider.auto
override val default: ModelPropertyProvider
get() = this
override fun column(columnName: String?): ModelPropertyProvider {
return if (this.columnName == columnName) {
this
} else {
if (columnName == null) {
provider.default
} else {
ModelPropertyProviderDefault(provider, columnName, defaultValue)
}
}
}
override fun default(value: Any?): ModelPropertyProvider {
if (value === defaultValue) return this
return ModelPropertyProviderBase(provider, propType, columnName, value)
}
}