mirror of
https://github.com/tursom/TursomServer.git
synced 2025-02-06 17:50:32 +08:00
add TypeAdapter
This commit is contained in:
parent
b4b45ddb98
commit
e6fc970b25
@ -1,26 +1,15 @@
|
||||
package cn.tursom.database
|
||||
|
||||
import cn.tursom.core.cast
|
||||
import cn.tursom.database.annotations.Json
|
||||
import cn.tursom.utils.clone.Property
|
||||
import cn.tursom.utils.clone.inject
|
||||
import cn.tursom.utils.clone.instance
|
||||
import me.liuwj.ktorm.dsl.QueryRowSet
|
||||
import me.liuwj.ktorm.schema.BaseTable
|
||||
import me.liuwj.ktorm.schema.EnumSqlType
|
||||
import java.math.BigDecimal
|
||||
import java.sql.Date
|
||||
import java.sql.Time
|
||||
import java.sql.Timestamp
|
||||
import java.time.*
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.full.findAnnotation
|
||||
import kotlin.reflect.full.isSubclassOf
|
||||
import kotlin.reflect.full.memberProperties
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
open class Table<T : Any>(
|
||||
entityClass: KClass<T>,
|
||||
@ -28,45 +17,12 @@ open class Table<T : Any>(
|
||||
alias: String? = null,
|
||||
val unsafe: Boolean = true
|
||||
) : BaseTable<T>(tableName, alias, entityClass) {
|
||||
private enum class EnumType
|
||||
|
||||
private val fieldMap: Map<String, KProperty<*>>
|
||||
|
||||
init {
|
||||
fieldMap = entityClass.memberProperties.associateBy { SqlUtils { it.tableField } }
|
||||
entityClass.memberProperties.forEach {
|
||||
when (val kClass = it.returnType.jvmErasure) {
|
||||
Boolean::class -> boolean(it.cast())
|
||||
//Byte::class->registerColumn(SqlUtils { it.tableField }, ByteSqlType)
|
||||
//Short::class->registerColumn(SqlUtils { it.tableField }, ShortSqlType)
|
||||
Int::class -> int(it.cast())
|
||||
Long::class -> long(it.cast())
|
||||
Float::class -> float(it.cast())
|
||||
Double::class -> double(it.cast())
|
||||
BigDecimal::class -> decimal(it.cast())
|
||||
String::class -> varchar(it.cast())
|
||||
ByteArray::class -> bytes(it.cast())
|
||||
Timestamp::class -> jdbcTimestamp(it.cast())
|
||||
Date::class -> jdbcDate(it.cast())
|
||||
Time::class -> jdbcTime(it.cast())
|
||||
Instant::class -> timestamp(it.cast())
|
||||
LocalDateTime::class -> datetime(it.cast())
|
||||
LocalDate::class -> date(it.cast())
|
||||
LocalTime::class -> time(it.cast())
|
||||
MonthDay::class -> monthDay(it.cast())
|
||||
YearMonth::class -> yearMonth(it.cast())
|
||||
Year::class -> year(it.cast())
|
||||
UUID::class -> uuid(it.cast())
|
||||
else -> {
|
||||
if (kClass.isSubclassOf(Enum::class)) {
|
||||
registerColumn(SqlUtils { it.tableField }, EnumSqlType<EnumType>(kClass.java.cast()))
|
||||
} else {
|
||||
if (it.findAnnotation<Json>() != null) {
|
||||
json(it.cast())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
TypeAdapterFactory.register(this, it)
|
||||
}
|
||||
}
|
||||
|
||||
@ -81,4 +37,3 @@ open class Table<T : Any>(
|
||||
|
||||
operator fun <R> get(property: KProperty1<T, R>) = this[SqlUtils { property.tableField }]
|
||||
}
|
||||
|
||||
|
@ -2,8 +2,11 @@ package cn.tursom.database
|
||||
|
||||
import cn.tursom.core.cast
|
||||
import cn.tursom.core.getClassByPackage
|
||||
import me.liuwj.ktorm.schema.BaseTable
|
||||
import me.liuwj.ktorm.schema.Column
|
||||
import java.util.concurrent.ConcurrentLinkedQueue
|
||||
import java.util.concurrent.ConcurrentSkipListMap
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.full.createInstance
|
||||
import kotlin.reflect.full.isSubclassOf
|
||||
|
||||
@ -41,5 +44,18 @@ object TypeAdapterFactory {
|
||||
getAdapterQueue(adapter.level).add(adapter)
|
||||
}
|
||||
|
||||
|
||||
fun register(
|
||||
table: BaseTable<*>,
|
||||
field: KProperty1<*, *>
|
||||
): Column<*>? {
|
||||
adapterMap.forEach { (_, queue) ->
|
||||
queue.forEach {
|
||||
val column = it.register(table.cast(), field.cast())
|
||||
if (column != null) {
|
||||
return column
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user