add TypeAdapter

This commit is contained in:
tursom 2020-08-11 00:46:40 +08:00
parent 155a77cb19
commit b4b45ddb98
23 changed files with 457 additions and 0 deletions

View File

@ -0,0 +1,13 @@
package cn.tursom.database
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import kotlin.reflect.KProperty1
interface TypeAdapter<T : Any> {
val level: Int get() = 0
fun register(
table: BaseTable<Any>,
field: KProperty1<Any, T>
): Column<T>?
}

View File

@ -0,0 +1,45 @@
package cn.tursom.database
import cn.tursom.core.cast
import cn.tursom.core.getClassByPackage
import java.util.concurrent.ConcurrentLinkedQueue
import java.util.concurrent.ConcurrentSkipListMap
import kotlin.reflect.full.createInstance
import kotlin.reflect.full.isSubclassOf
object TypeAdapterFactory {
private val adapterMap = ConcurrentSkipListMap<Int, ConcurrentLinkedQueue<TypeAdapter<*>>>()
private val adapterQueue = ConcurrentLinkedQueue<TypeAdapter<*>>()
init {
getClassByPackage("cn.tursom.database.typeadapter").forEach {
try {
val clazz = Class.forName(it).kotlin
if (clazz.isSubclassOf(TypeAdapter::class)) {
val adapter: TypeAdapter<*> = clazz.objectInstance?.cast() ?: run {
clazz.createInstance()
}.cast()
registerAdapter(adapter)
}
} catch (e: Error) {
}
}
}
private fun getAdapterQueue(level: Int): ConcurrentLinkedQueue<TypeAdapter<*>> {
var queue = adapterMap[-level]
if (queue == null) {
synchronized(this) {
adapterMap[-level] = ConcurrentLinkedQueue()
queue = adapterMap[-level]!!
}
}
return queue!!
}
fun registerAdapter(adapter: TypeAdapter<*>) {
getAdapterQueue(adapter.level).add(adapter)
}
}

View File

@ -0,0 +1,18 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.boolean
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class BooleanAdapter : TypeAdapter<Boolean> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Boolean>): Column<Boolean>? {
return if (field.returnType.jvmErasure == Boolean::class) {
table.boolean(field)
} else {
null
}
}
}

View File

@ -0,0 +1,18 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.bytes
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class BytesAdapter : TypeAdapter<ByteArray> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, ByteArray>): Column<ByteArray>? {
return if (field.returnType.jvmErasure == ByteArray::class) {
table.bytes(field)
} else {
null
}
}
}

View File

@ -0,0 +1,19 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.jdbcDate
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import java.sql.Date
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class DateAdapter : TypeAdapter<Date> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Date>): Column<Date>? {
return if (field.returnType.jvmErasure == Date::class) {
table.jdbcDate(field)
} else {
null
}
}
}

View File

@ -0,0 +1,18 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.double
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class DoubleAdapter : TypeAdapter<Double> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Double>): Column<Double>? {
return if (field.returnType.jvmErasure == Double::class) {
table.double(field)
} else {
null
}
}
}

View File

@ -0,0 +1,24 @@
package cn.tursom.database.typeadapter
import cn.tursom.core.cast
import cn.tursom.database.SqlUtils
import cn.tursom.database.TypeAdapter
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import me.liuwj.ktorm.schema.EnumSqlType
import kotlin.reflect.KProperty1
import kotlin.reflect.full.isSubclassOf
import kotlin.reflect.jvm.jvmErasure
class EnumAdapter<E : Enum<E>> : TypeAdapter<E> {
private enum class EnumType
override fun register(table: BaseTable<Any>, field: KProperty1<Any, E>): Column<E>? {
val kClass = field.returnType.jvmErasure
return if (kClass.isSubclassOf(Enum::class)) {
table.registerColumn(SqlUtils { field.tableField }, EnumSqlType<EnumType>(kClass.java.cast())).cast()
} else {
null
}
}
}

View File

@ -0,0 +1,18 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.float
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class FloatAdapter : TypeAdapter<Float> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Float>): Column<Float>? {
return if (field.returnType.jvmErasure == Float::class) {
table.float(field)
} else {
null
}
}
}

View File

@ -0,0 +1,19 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.timestamp
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import java.time.Instant
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class InstantAdapter : TypeAdapter<Instant> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Instant>): Column<Instant>? {
return if (field.returnType.jvmErasure == Instant::class) {
table.timestamp(field)
} else {
null
}
}
}

View File

@ -0,0 +1,18 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.int
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class IntAdapter : TypeAdapter<Int> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Int>): Column<Int>? {
return if (field.returnType.jvmErasure == Int::class) {
table.int(field)
} else {
null
}
}
}

View File

@ -0,0 +1,19 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.decimal
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import java.math.BigDecimal
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class IntAdapterAdapter : TypeAdapter<BigDecimal> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, BigDecimal>): Column<BigDecimal>? {
return if (field.returnType.jvmErasure == BigDecimal::class) {
table.decimal(field)
} else {
null
}
}
}

View File

@ -0,0 +1,21 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.annotations.Json
import cn.tursom.database.json
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import kotlin.reflect.KProperty1
import kotlin.reflect.full.findAnnotation
class JsonAdapter : TypeAdapter<Int> {
override val level: Int get() = -16
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Int>): Column<Int>? {
return if (field.findAnnotation<Json>() != null) {
table.json(field)
} else {
null
}
}
}

View File

@ -0,0 +1,19 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.date
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import java.time.LocalDate
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class LocalDateAdapter : TypeAdapter<LocalDate> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, LocalDate>): Column<LocalDate>? {
return if (field.returnType.jvmErasure == LocalDate::class) {
table.date(field)
} else {
null
}
}
}

View File

@ -0,0 +1,19 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.datetime
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import java.time.LocalDateTime
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class LocalDateTimeAdapter : TypeAdapter<LocalDateTime> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, LocalDateTime>): Column<LocalDateTime>? {
return if (field.returnType.jvmErasure == LocalDateTime::class) {
table.datetime(field)
} else {
null
}
}
}

View File

@ -0,0 +1,19 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.time
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import java.time.LocalTime
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class LocalTimeAdapter : TypeAdapter<LocalTime> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, LocalTime>): Column<LocalTime>? {
return if (field.returnType.jvmErasure == LocalTime::class) {
table.time(field)
} else {
null
}
}
}

View File

@ -0,0 +1,18 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.long
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class LongAdapter : TypeAdapter<Long> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Long>): Column<Long>? {
return if (field.returnType.jvmErasure == Long::class) {
table.long(field)
} else {
null
}
}
}

View File

@ -0,0 +1,19 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.monthDay
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import java.time.MonthDay
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class MonthDayAdapter : TypeAdapter<MonthDay> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, MonthDay>): Column<MonthDay>? {
return if (field.returnType.jvmErasure == MonthDay::class) {
table.monthDay(field)
} else {
null
}
}
}

View File

@ -0,0 +1,18 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.varchar
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class StringAdapter : TypeAdapter<String> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, String>): Column<String>? {
return if (field.returnType.jvmErasure == String::class) {
table.varchar(field)
} else {
null
}
}
}

View File

@ -0,0 +1,19 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.jdbcTime
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import java.sql.Time
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class TimeAdapter : TypeAdapter<Time> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Time>): Column<Time>? {
return if (field.returnType.jvmErasure == Time::class) {
table.jdbcTime(field)
} else {
null
}
}
}

View File

@ -0,0 +1,19 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.jdbcTimestamp
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import java.sql.Timestamp
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class TimestampAdapter : TypeAdapter<Timestamp> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Timestamp>): Column<Timestamp>? {
return if (field.returnType.jvmErasure == Timestamp::class) {
table.jdbcTimestamp(field)
} else {
null
}
}
}

View File

@ -0,0 +1,19 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.uuid
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import java.util.*
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class UUIDAdapter : TypeAdapter<UUID> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, UUID>): Column<UUID>? {
return if (field.returnType.jvmErasure == UUID::class) {
table.uuid(field)
} else {
null
}
}
}

View File

@ -0,0 +1,19 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.year
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import java.time.Year
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class YearAdapter : TypeAdapter<Year> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Year>): Column<Year>? {
return if (field.returnType.jvmErasure == Year::class) {
table.year(field)
} else {
null
}
}
}

View File

@ -0,0 +1,19 @@
package cn.tursom.database.typeadapter
import cn.tursom.database.TypeAdapter
import cn.tursom.database.yearMonth
import me.liuwj.ktorm.schema.BaseTable
import me.liuwj.ktorm.schema.Column
import java.time.YearMonth
import kotlin.reflect.KProperty1
import kotlin.reflect.jvm.jvmErasure
class YearMonthAdapter : TypeAdapter<YearMonth> {
override fun register(table: BaseTable<Any>, field: KProperty1<Any, YearMonth>): Column<YearMonth>? {
return if (field.returnType.jvmErasure == YearMonth::class) {
table.yearMonth(field)
} else {
null
}
}
}