add TypeAdapter

This commit is contained in:
tursom 2020-08-11 00:54:28 +08:00
parent e6fc970b25
commit 1314480e1b
22 changed files with 40 additions and 35 deletions

View File

@ -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)
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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
}
}
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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>? {

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)