From 1314480e1b9c477d726e495dca0366153a084299 Mon Sep 17 00:00:00 2001 From: tursom Date: Tue, 11 Aug 2020 00:54:28 +0800 Subject: [PATCH] add TypeAdapter --- .../cn/tursom/database/TypeAdapterFactory.kt | 28 +++++++++++-------- .../database/typeadapter/BooleanAdapter.kt | 2 +- .../database/typeadapter/BytesAdapter.kt | 2 +- .../database/typeadapter/DateAdapter.kt | 2 +- .../database/typeadapter/DoubleAdapter.kt | 2 +- .../database/typeadapter/EnumAdapter.kt | 7 +++-- .../database/typeadapter/FloatAdapter.kt | 2 +- .../database/typeadapter/InstantAdapter.kt | 2 +- .../tursom/database/typeadapter/IntAdapter.kt | 2 +- .../database/typeadapter/IntAdapterAdapter.kt | 2 +- .../database/typeadapter/JsonAdapter.kt | 2 +- .../database/typeadapter/LocalDateAdapter.kt | 2 +- .../typeadapter/LocalDateTimeAdapter.kt | 2 +- .../database/typeadapter/LocalTimeAdapter.kt | 2 +- .../database/typeadapter/LongAdapter.kt | 2 +- .../database/typeadapter/MonthDayAdapter.kt | 2 +- .../database/typeadapter/StringAdapter.kt | 2 +- .../database/typeadapter/TimeAdapter.kt | 2 +- .../database/typeadapter/TimestampAdapter.kt | 2 +- .../database/typeadapter/UUIDAdapter.kt | 2 +- .../database/typeadapter/YearAdapter.kt | 2 +- .../database/typeadapter/YearMonthAdapter.kt | 2 +- 22 files changed, 40 insertions(+), 35 deletions(-) diff --git a/database/src/main/kotlin/cn/tursom/database/TypeAdapterFactory.kt b/database/src/main/kotlin/cn/tursom/database/TypeAdapterFactory.kt index 1bae34d..3f9fbd1 100644 --- a/database/src/main/kotlin/cn/tursom/database/TypeAdapterFactory.kt +++ b/database/src/main/kotlin/cn/tursom/database/TypeAdapterFactory.kt @@ -15,18 +15,7 @@ object TypeAdapterFactory { 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) { - } - } + scanPackage("cn.tursom.database.typeadapter") } private fun getAdapterQueue(level: Int): ConcurrentLinkedQueue> { @@ -40,6 +29,21 @@ object TypeAdapterFactory { return queue!! } + fun scanPackage(pkg: String) { + getClassByPackage(pkg).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) { + } + } + } + fun registerAdapter(adapter: TypeAdapter<*>) { getAdapterQueue(adapter.level).add(adapter) } diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/BooleanAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/BooleanAdapter.kt index ff21260..a456477 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/BooleanAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/BooleanAdapter.kt @@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class BooleanAdapter : TypeAdapter { +object BooleanAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == Boolean::class) { table.boolean(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/BytesAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/BytesAdapter.kt index e24913a..f3c72ba 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/BytesAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/BytesAdapter.kt @@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class BytesAdapter : TypeAdapter { +object BytesAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == ByteArray::class) { table.bytes(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/DateAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/DateAdapter.kt index 303efbf..bb79e05 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/DateAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/DateAdapter.kt @@ -8,7 +8,7 @@ import java.sql.Date import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class DateAdapter : TypeAdapter { +object DateAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == Date::class) { table.jdbcDate(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/DoubleAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/DoubleAdapter.kt index 669514b..1427365 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/DoubleAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/DoubleAdapter.kt @@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class DoubleAdapter : TypeAdapter { +object DoubleAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == Double::class) { table.double(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/EnumAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/EnumAdapter.kt index 893bee1..0abdf57 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/EnumAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/EnumAdapter.kt @@ -10,10 +10,10 @@ import kotlin.reflect.KProperty1 import kotlin.reflect.full.isSubclassOf import kotlin.reflect.jvm.jvmErasure -class EnumAdapter> : TypeAdapter { - private enum class EnumType +object EnumAdapter : TypeAdapter { + enum class EnumType - override fun register(table: BaseTable, field: KProperty1): Column? { + 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() @@ -21,4 +21,5 @@ class EnumAdapter> : TypeAdapter { 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 index bc294af..b0a01f3 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/FloatAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/FloatAdapter.kt @@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class FloatAdapter : TypeAdapter { +object FloatAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == Float::class) { table.float(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/InstantAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/InstantAdapter.kt index 9d63923..ef5cf16 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/InstantAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/InstantAdapter.kt @@ -8,7 +8,7 @@ import java.time.Instant import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class InstantAdapter : TypeAdapter { +object InstantAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == Instant::class) { table.timestamp(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapter.kt index 980aaab..33c7d81 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapter.kt @@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class IntAdapter : TypeAdapter { +object IntAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == Int::class) { table.int(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapterAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapterAdapter.kt index 399c36b..a4dc704 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapterAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/IntAdapterAdapter.kt @@ -8,7 +8,7 @@ import java.math.BigDecimal import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class IntAdapterAdapter : TypeAdapter { +object IntAdapterAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == BigDecimal::class) { table.decimal(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/JsonAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/JsonAdapter.kt index ffd0e60..b5af7a8 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/JsonAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/JsonAdapter.kt @@ -8,7 +8,7 @@ import me.liuwj.ktorm.schema.Column import kotlin.reflect.KProperty1 import kotlin.reflect.full.findAnnotation -class JsonAdapter : TypeAdapter { +object JsonAdapter : TypeAdapter { override val level: Int get() = -16 override fun register(table: BaseTable, field: KProperty1): Column? { diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateAdapter.kt index 57514fd..a533ddf 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateAdapter.kt @@ -8,7 +8,7 @@ import java.time.LocalDate import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class LocalDateAdapter : TypeAdapter { +object LocalDateAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == LocalDate::class) { table.date(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateTimeAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateTimeAdapter.kt index 53b2d8d..c9bceda 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateTimeAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalDateTimeAdapter.kt @@ -8,7 +8,7 @@ import java.time.LocalDateTime import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class LocalDateTimeAdapter : TypeAdapter { +object LocalDateTimeAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == LocalDateTime::class) { table.datetime(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalTimeAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalTimeAdapter.kt index b22fd21..19a2eb0 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalTimeAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/LocalTimeAdapter.kt @@ -8,7 +8,7 @@ import java.time.LocalTime import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class LocalTimeAdapter : TypeAdapter { +object LocalTimeAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == LocalTime::class) { table.time(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/LongAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/LongAdapter.kt index a86c0ee..7dd7e0d 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/LongAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/LongAdapter.kt @@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class LongAdapter : TypeAdapter { +object LongAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == Long::class) { table.long(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/MonthDayAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/MonthDayAdapter.kt index 510be39..aa33053 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/MonthDayAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/MonthDayAdapter.kt @@ -8,7 +8,7 @@ import java.time.MonthDay import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class MonthDayAdapter : TypeAdapter { +object MonthDayAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == MonthDay::class) { table.monthDay(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/StringAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/StringAdapter.kt index 041ab67..26a58f4 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/StringAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/StringAdapter.kt @@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class StringAdapter : TypeAdapter { +object StringAdapter : TypeAdapter { override fun register(table: BaseTable, field: KProperty1): Column? { return if (field.returnType.jvmErasure == String::class) { table.varchar(field) diff --git a/database/src/main/kotlin/cn/tursom/database/typeadapter/TimeAdapter.kt b/database/src/main/kotlin/cn/tursom/database/typeadapter/TimeAdapter.kt index b6396ba..2cd5ec7 100644 --- a/database/src/main/kotlin/cn/tursom/database/typeadapter/TimeAdapter.kt +++ b/database/src/main/kotlin/cn/tursom/database/typeadapter/TimeAdapter.kt @@ -8,7 +8,7 @@ import java.sql.Time import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.jvmErasure -class TimeAdapter : TypeAdapter