From b4b45ddb9836747cf3546fee9694e74c3105434e Mon Sep 17 00:00:00 2001 From: tursom Date: Tue, 11 Aug 2020 00:46:40 +0800 Subject: [PATCH] add TypeAdapter --- .../kotlin/cn/tursom/database/TypeAdapter.kt | 13 ++++++ .../cn/tursom/database/TypeAdapterFactory.kt | 45 +++++++++++++++++++ .../database/typeadapter/BooleanAdapter.kt | 18 ++++++++ .../database/typeadapter/BytesAdapter.kt | 18 ++++++++ .../database/typeadapter/DateAdapter.kt | 19 ++++++++ .../database/typeadapter/DoubleAdapter.kt | 18 ++++++++ .../database/typeadapter/EnumAdapter.kt | 24 ++++++++++ .../database/typeadapter/FloatAdapter.kt | 18 ++++++++ .../database/typeadapter/InstantAdapter.kt | 19 ++++++++ .../tursom/database/typeadapter/IntAdapter.kt | 18 ++++++++ .../database/typeadapter/IntAdapterAdapter.kt | 19 ++++++++ .../database/typeadapter/JsonAdapter.kt | 21 +++++++++ .../database/typeadapter/LocalDateAdapter.kt | 19 ++++++++ .../typeadapter/LocalDateTimeAdapter.kt | 19 ++++++++ .../database/typeadapter/LocalTimeAdapter.kt | 19 ++++++++ .../database/typeadapter/LongAdapter.kt | 18 ++++++++ .../database/typeadapter/MonthDayAdapter.kt | 19 ++++++++ .../database/typeadapter/StringAdapter.kt | 18 ++++++++ .../database/typeadapter/TimeAdapter.kt | 19 ++++++++ .../database/typeadapter/TimestampAdapter.kt | 19 ++++++++ .../database/typeadapter/UUIDAdapter.kt | 19 ++++++++ .../database/typeadapter/YearAdapter.kt | 19 ++++++++ .../database/typeadapter/YearMonthAdapter.kt | 19 ++++++++ 23 files changed, 457 insertions(+) create mode 100644 database/src/main/kotlin/cn/tursom/database/TypeAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/TypeAdapterFactory.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/BooleanAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/BytesAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/DateAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/DoubleAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/EnumAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/FloatAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/InstantAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapterAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/JsonAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateTimeAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/LocalTimeAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/LongAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/MonthDayAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/StringAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/TimeAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/TimestampAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/UUIDAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/YearAdapter.kt create mode 100644 database/src/main/kotlin/cn/tursom/database/typeadapter/YearMonthAdapter.kt diff --git a/database/src/main/kotlin/cn/tursom/database/TypeAdapter.kt b/database/src/main/kotlin/cn/tursom/database/TypeAdapter.kt new file mode 100644 index 0000000..427228e --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/TypeAdapter.kt @@ -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 { + val level: Int get() = 0 + fun register( + table: BaseTable, + field: KProperty1 + ): Column? +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/TypeAdapterFactory.kt b/database/src/main/kotlin/cn/tursom/database/TypeAdapterFactory.kt new file mode 100644 index 0000000..4c0c73b --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/TypeAdapterFactory.kt @@ -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>>() + private val adapterQueue = ConcurrentLinkedQueue>() + + 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> { + 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) + } + + +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/BooleanAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/BooleanAdapter.kt new file mode 100644 index 0000000..ff21260 --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/BooleanAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == Boolean::class) { + table.boolean(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/BytesAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/BytesAdapter.kt new file mode 100644 index 0000000..e24913a --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/BytesAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == ByteArray::class) { + table.bytes(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/DateAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/DateAdapter.kt new file mode 100644 index 0000000..303efbf --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/DateAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == Date::class) { + table.jdbcDate(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/DoubleAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/DoubleAdapter.kt new file mode 100644 index 0000000..669514b --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/DoubleAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == Double::class) { + table.double(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/EnumAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/EnumAdapter.kt new file mode 100644 index 0000000..893bee1 --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/EnumAdapter.kt @@ -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> : TypeAdapter { + private enum class EnumType + + override fun register(table: BaseTable, field: KProperty1): Column? { + val kClass = field.returnType.jvmErasure + return if (kClass.isSubclassOf(Enum::class)) { + table.registerColumn(SqlUtils { field.tableField }, EnumSqlType(kClass.java.cast())).cast() + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/FloatAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/FloatAdapter.kt new file mode 100644 index 0000000..bc294af --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/FloatAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == Float::class) { + table.float(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/InstantAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/InstantAdapter.kt new file mode 100644 index 0000000..9d63923 --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/InstantAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == Instant::class) { + table.timestamp(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapter.kt new file mode 100644 index 0000000..980aaab --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == Int::class) { + table.int(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapterAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapterAdapter.kt new file mode 100644 index 0000000..399c36b --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapterAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == BigDecimal::class) { + table.decimal(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/JsonAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/JsonAdapter.kt new file mode 100644 index 0000000..ffd0e60 --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/JsonAdapter.kt @@ -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 { + override val level: Int get() = -16 + + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.findAnnotation() != null) { + table.json(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateAdapter.kt new file mode 100644 index 0000000..57514fd --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == LocalDate::class) { + table.date(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateTimeAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateTimeAdapter.kt new file mode 100644 index 0000000..53b2d8d --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateTimeAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == LocalDateTime::class) { + table.datetime(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalTimeAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalTimeAdapter.kt new file mode 100644 index 0000000..b22fd21 --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalTimeAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == LocalTime::class) { + table.time(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/LongAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/LongAdapter.kt new file mode 100644 index 0000000..a86c0ee --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/LongAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == Long::class) { + table.long(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/MonthDayAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/MonthDayAdapter.kt new file mode 100644 index 0000000..510be39 --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/MonthDayAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == MonthDay::class) { + table.monthDay(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/StringAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/StringAdapter.kt new file mode 100644 index 0000000..041ab67 --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/StringAdapter.kt @@ -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 { + override fun register(table: BaseTable, field: KProperty1): Column? { + return if (field.returnType.jvmErasure == String::class) { + table.varchar(field) + } else { + null + } + } +} \ No newline at end of file diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/TimeAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/TimeAdapter.kt new file mode 100644 index 0000000..b6396ba --- /dev/null +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/TimeAdapter.kt @@ -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