mirror of
https://github.com/tursom/TursomServer.git
synced 2025-01-30 14:20:07 +08:00
add TypeAdapter
This commit is contained in:
parent
e6fc970b25
commit
1314480e1b
@ -15,18 +15,7 @@ object TypeAdapterFactory {
|
||||
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) {
|
||||
}
|
||||
}
|
||||
scanPackage("cn.tursom.database.typeadapter")
|
||||
}
|
||||
|
||||
private fun getAdapterQueue(level: Int): ConcurrentLinkedQueue<TypeAdapter<*>> {
|
||||
@ -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)
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class BooleanAdapter : TypeAdapter<Boolean> {
|
||||
object 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)
|
||||
|
@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class BytesAdapter : TypeAdapter<ByteArray> {
|
||||
object 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)
|
||||
|
@ -8,7 +8,7 @@ import java.sql.Date
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class DateAdapter : TypeAdapter<Date> {
|
||||
object 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)
|
||||
|
@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class DoubleAdapter : TypeAdapter<Double> {
|
||||
object 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)
|
||||
|
@ -10,10 +10,10 @@ 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
|
||||
object EnumAdapter : TypeAdapter<EnumAdapter.EnumType> {
|
||||
enum class EnumType
|
||||
|
||||
override fun register(table: BaseTable<Any>, field: KProperty1<Any, E>): Column<E>? {
|
||||
override fun register(table: BaseTable<Any>, field: KProperty1<Any, EnumType>): Column<EnumType>? {
|
||||
val kClass = field.returnType.jvmErasure
|
||||
return if (kClass.isSubclassOf(Enum::class)) {
|
||||
table.registerColumn(SqlUtils { field.tableField }, EnumSqlType<EnumType>(kClass.java.cast())).cast()
|
||||
@ -21,4 +21,5 @@ class EnumAdapter<E : Enum<E>> : TypeAdapter<E> {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class FloatAdapter : TypeAdapter<Float> {
|
||||
object 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)
|
||||
|
@ -8,7 +8,7 @@ import java.time.Instant
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class InstantAdapter : TypeAdapter<Instant> {
|
||||
object 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)
|
||||
|
@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class IntAdapter : TypeAdapter<Int> {
|
||||
object 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)
|
||||
|
@ -8,7 +8,7 @@ import java.math.BigDecimal
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class IntAdapterAdapter : TypeAdapter<BigDecimal> {
|
||||
object 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)
|
||||
|
@ -8,7 +8,7 @@ import me.liuwj.ktorm.schema.Column
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.full.findAnnotation
|
||||
|
||||
class JsonAdapter : TypeAdapter<Int> {
|
||||
object JsonAdapter : TypeAdapter<Int> {
|
||||
override val level: Int get() = -16
|
||||
|
||||
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Int>): Column<Int>? {
|
||||
|
@ -8,7 +8,7 @@ import java.time.LocalDate
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class LocalDateAdapter : TypeAdapter<LocalDate> {
|
||||
object 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)
|
||||
|
@ -8,7 +8,7 @@ import java.time.LocalDateTime
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class LocalDateTimeAdapter : TypeAdapter<LocalDateTime> {
|
||||
object 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)
|
||||
|
@ -8,7 +8,7 @@ import java.time.LocalTime
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class LocalTimeAdapter : TypeAdapter<LocalTime> {
|
||||
object 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)
|
||||
|
@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class LongAdapter : TypeAdapter<Long> {
|
||||
object 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)
|
||||
|
@ -8,7 +8,7 @@ import java.time.MonthDay
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class MonthDayAdapter : TypeAdapter<MonthDay> {
|
||||
object 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)
|
||||
|
@ -7,7 +7,7 @@ import me.liuwj.ktorm.schema.Column
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class StringAdapter : TypeAdapter<String> {
|
||||
object 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)
|
||||
|
@ -8,7 +8,7 @@ import java.sql.Time
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class TimeAdapter : TypeAdapter<Time> {
|
||||
object 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)
|
||||
|
@ -8,7 +8,7 @@ import java.sql.Timestamp
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class TimestampAdapter : TypeAdapter<Timestamp> {
|
||||
object 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)
|
||||
|
@ -8,7 +8,7 @@ import java.util.*
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class UUIDAdapter : TypeAdapter<UUID> {
|
||||
object 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)
|
||||
|
@ -8,7 +8,7 @@ import java.time.Year
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class YearAdapter : TypeAdapter<Year> {
|
||||
object 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)
|
||||
|
@ -8,7 +8,7 @@ import java.time.YearMonth
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
class YearMonthAdapter : TypeAdapter<YearMonth> {
|
||||
object 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)
|
||||
|
Loading…
Reference in New Issue
Block a user