mirror of
https://github.com/tursom/TursomServer.git
synced 2025-03-25 17:10:07 +08:00
update
This commit is contained in:
parent
def6a9f752
commit
647d989aa1
@ -17,7 +17,7 @@ ext["excludeTest"] = { project: Project, tasks: TaskContainer ->
|
||||
|
||||
|
||||
plugins {
|
||||
kotlin("jvm") version "1.4.31"
|
||||
kotlin("jvm") version "1.4.32"
|
||||
`maven-publish`
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,7 @@ include("ts-socket")
|
||||
include("ts-web")
|
||||
include("ts-web:ts-web-netty")
|
||||
include("ts-web:ts-web-coroutine")
|
||||
include("ts-database")
|
||||
//include("web", "aop", "database", "utils", "utils:xml", "utils:async-http", "web:netty-web")
|
||||
//include("socket", "socket:socket-async")
|
||||
//include("AsyncSocket")
|
||||
|
@ -4,7 +4,7 @@ plugins {
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation(project(":"))
|
||||
api(project(":"))
|
||||
compileOnly(group = "com.google.code.gson", name = "gson", version = "2.8.6")
|
||||
compileOnly(group = "io.netty", name = "netty-all", version = "4.1.43.Final")
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ plugins {
|
||||
|
||||
dependencies {
|
||||
implementation(project(":ts-core"))
|
||||
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.2")
|
||||
}
|
||||
|
||||
@kotlin.Suppress("UNCHECKED_CAST")
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
interface DecoratorDelegatedField<in T, out V> {
|
||||
val delegatedField: DelegatedField<T, V>
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
interface DecoratorMutableDelegatedField<in T, V> : DecoratorDelegatedField<T, V> {
|
||||
val mutableDelegatedField: MutableDelegatedField<T, V>
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
@ -1,3 +1,3 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
interface DelegatedFieldAttachmentKey<V>
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import java.util.concurrent.Executor
|
||||
import kotlin.reflect.KProperty
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import cn.tursom.core.cast
|
||||
import kotlin.reflect.KProperty
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import kotlin.reflect.KProperty0
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import kotlin.reflect.KMutableProperty0
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import java.util.concurrent.locks.Lock
|
||||
import java.util.concurrent.locks.ReentrantLock
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import cn.tursom.core.cast
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import cn.tursom.core.cast
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import java.util.concurrent.locks.ReadWriteLock
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
class SetterDelegatedField<T, V>(
|
||||
override val delegatedField: DelegatedField<T, V>,
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import cn.tursom.core.SimpThreadLocal
|
||||
|
@ -1,4 +1,4 @@
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
class ThreadLocalMutableDelegatedField<in T, V>(
|
||||
private val threadLocal: ThreadLocal<V?> = ThreadLocal()
|
@ -1,6 +1,6 @@
|
||||
@file:Suppress("unused")
|
||||
|
||||
package cn.tursom.delegation
|
||||
package cn.tursom.core.delegation
|
||||
|
||||
import cn.tursom.core.SimpThreadLocal
|
||||
import cn.tursom.core.cast
|
@ -2,7 +2,6 @@ package cn.tursom.core.encrypt
|
||||
|
||||
import cn.tursom.core.Unsafe
|
||||
import cn.tursom.core.cast
|
||||
import sun.security.ec.CurveDB
|
||||
import java.security.KeyFactory
|
||||
import java.security.KeyPair
|
||||
import java.security.KeyPairGenerator
|
||||
@ -17,7 +16,7 @@ import java.security.spec.X509EncodedKeySpec
|
||||
@Suppress("unused", "MemberVisibilityCanBePrivate")
|
||||
class ECC(
|
||||
publicKey: ECPublicKey,
|
||||
privateKey: ECPrivateKey? = null
|
||||
privateKey: ECPrivateKey? = null,
|
||||
) : AbstractPublicKeyEncrypt("EC", publicKey, privateKey) {
|
||||
|
||||
override val decryptMaxLen = Int.MAX_VALUE
|
||||
@ -41,7 +40,7 @@ class ECC(
|
||||
|
||||
constructor(
|
||||
keySize: Int = 256,
|
||||
standardCurveLine: String = StandardCurveLine.secp256k1.name.replace('_', ' ')
|
||||
standardCurveLine: String = StandardCurveLine.secp256k1.name.replace('_', ' '),
|
||||
) : this(
|
||||
keySize,
|
||||
ECGenParameterSpec(standardCurveLine)
|
||||
@ -71,8 +70,12 @@ class ECC(
|
||||
|
||||
companion object {
|
||||
val standardCurveLineSet by lazy {
|
||||
Unsafe {
|
||||
CurveDB::class.java["nameMap"].cast<Map<String, Any>>().keys
|
||||
try {
|
||||
Unsafe {
|
||||
Class.forName("sun.security.ec.CurveDB")["nameMap"].cast<Map<String, Any>>().keys
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
emptySet()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
34
ts-database/build.gradle.kts
Normal file
34
ts-database/build.gradle.kts
Normal file
@ -0,0 +1,34 @@
|
||||
plugins {
|
||||
kotlin("jvm")
|
||||
`maven-publish`
|
||||
}
|
||||
|
||||
dependencies {
|
||||
api(project(":"))
|
||||
api(group = "me.liuwj.ktorm", name = "ktorm-core", version = "3.1.0")
|
||||
compileOnly(group = "com.baomidou", name = "mybatis-plus", version = "3.4.2")
|
||||
compileOnly(group = "com.google.code.gson", name = "gson", version = "2.8.6")
|
||||
}
|
||||
|
||||
@kotlin.Suppress("UNCHECKED_CAST")
|
||||
(rootProject.ext["excludeTest"] as (Project, TaskContainer) -> Unit)(project, tasks)
|
||||
|
||||
tasks.register("install") {
|
||||
finalizedBy(tasks["publishToMavenLocal"])
|
||||
}
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
create<MavenPublication>("maven") {
|
||||
groupId = project.group.toString()
|
||||
artifactId = project.name
|
||||
version = project.version.toString()
|
||||
|
||||
from(components["java"])
|
||||
try {
|
||||
artifact(tasks["sourcesJar"])
|
||||
} catch (e: Exception) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
84
ts-database/src/main/kotlin/cn/tursom/database/AutoTable.kt
Normal file
84
ts-database/src/main/kotlin/cn/tursom/database/AutoTable.kt
Normal file
@ -0,0 +1,84 @@
|
||||
package cn.tursom.database
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.TableField
|
||||
import com.ddbes.kotlin.clone.Property
|
||||
import com.ddbes.kotlin.clone.inject
|
||||
import com.ddbes.kotlin.clone.instance
|
||||
import com.ddbes.kotlin.jdbc.simpTableField
|
||||
import com.ddbes.kotlin.jdbc.tableName
|
||||
import com.ddbes.kotlin.uncheckedCast
|
||||
import me.liuwj.ktorm.dsl.QueryRowSet
|
||||
import me.liuwj.ktorm.schema.BaseTable
|
||||
import me.liuwj.ktorm.schema.Column
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import kotlin.reflect.KClass
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.reflect.KProperty0
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.full.memberProperties
|
||||
import kotlin.reflect.jvm.javaField
|
||||
|
||||
open class AutoTable<T : Any>(
|
||||
entityClass: KClass<T>,
|
||||
tableName: String = entityClass.tableName,
|
||||
alias: String? = null,
|
||||
catalog: String? = null,
|
||||
schema: String? = null,
|
||||
val unsafe: Boolean = true,
|
||||
) : BaseTable<T>(tableName, alias, catalog, schema, entityClass) {
|
||||
private val fieldMap: Map<String, KProperty<*>>
|
||||
private val fieldColumns: MutableMap<KProperty<*>, Column<*>> = HashMap()
|
||||
private val fieldNameColumnMap: MutableMap<String, Column<*>> = HashMap()
|
||||
|
||||
init {
|
||||
fieldMap = entityClass.memberProperties.associateBy { it.simpTableField }
|
||||
entityClass.memberProperties.forEach {
|
||||
val field = it.javaField ?: return@forEach
|
||||
val tableField: TableField? = field.getAnnotation(TableField::class.java)
|
||||
if (tableField?.exist == false) return@forEach
|
||||
//TypeAdapterFactory.register(this, it)
|
||||
val column = TypeAdapterFactory.register(this, it) ?: return@forEach
|
||||
fieldColumns[it] = column
|
||||
fieldNameColumnMap[it.name] = column
|
||||
}
|
||||
}
|
||||
|
||||
override fun doCreateEntity(row: QueryRowSet, withReferences: Boolean): T {
|
||||
val instance = instance(unsafe, entityClass!!.java)
|
||||
columns.forEach {
|
||||
val field = fieldMap[it.name] ?: return@forEach
|
||||
row[it]?.inject(instance, field.uncheckedCast<Property<Any>>())
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
||||
operator fun <R : Any> get(property: KProperty1<T, R?>): Column<R> = fieldColumns[property].uncheckedCast()
|
||||
//operator fun <R : Any> get(property: KProperty1<T, R?>): Column<R> = this[property.simpTableField].cast()
|
||||
|
||||
fun <V : Any> field(): FieldProxy<T, V> = fieldProxyInstance.uncheckedCast()
|
||||
fun <V : Any> field(property: KProperty0<*>): Column<V> = fieldNameColumnMap[property.name].uncheckedCast()
|
||||
|
||||
companion object {
|
||||
private val fieldProxyInstance = FieldProxy<Any, Any>()
|
||||
private val autoTableMap = ConcurrentHashMap<Class<*>, AutoTable<*>>()
|
||||
operator fun <T : Any> get(clazz: KClass<T>): AutoTable<T> = get(clazz.java)
|
||||
|
||||
operator fun <T : Any> get(clazz: Class<T>): AutoTable<T> {
|
||||
var autoTable = autoTableMap[clazz]
|
||||
if (autoTable == null) {
|
||||
synchronized(autoTableMap) {
|
||||
autoTable = AutoTable(clazz.kotlin)
|
||||
autoTableMap[clazz] = autoTable.uncheckedCast()
|
||||
}
|
||||
}
|
||||
return autoTable.uncheckedCast()
|
||||
}
|
||||
|
||||
class FieldProxy<T : Any, V : Any> {
|
||||
operator fun getValue(
|
||||
autoTable: AutoTable<T>,
|
||||
property: KProperty<*>,
|
||||
): Column<V> = autoTable.fieldNameColumnMap[property.name].uncheckedCast()
|
||||
}
|
||||
}
|
||||
}
|
27
ts-database/src/main/kotlin/cn/tursom/database/JsonType.kt
Normal file
27
ts-database/src/main/kotlin/cn/tursom/database/JsonType.kt
Normal file
@ -0,0 +1,27 @@
|
||||
package cn.tursom.database
|
||||
|
||||
import com.ddbes.kotlin.Utils
|
||||
import com.google.gson.Gson
|
||||
import me.liuwj.ktorm.schema.SqlType
|
||||
import java.lang.reflect.Type
|
||||
import java.sql.PreparedStatement
|
||||
import java.sql.ResultSet
|
||||
import java.sql.Types
|
||||
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
class JsonType<T : Any>(
|
||||
val clazz: Type,
|
||||
type: Int = Types.VARCHAR,
|
||||
val gson: Gson = Utils.gson
|
||||
) : SqlType<T>(type, "json") {
|
||||
constructor(clazz: Class<T>, type: Int = Types.VARCHAR) : this(clazz as Type, type)
|
||||
|
||||
override fun doGetResult(rs: ResultSet, index: Int): T? {
|
||||
val result = rs.getString(index) ?: return null
|
||||
return gson.fromJson(result, clazz)
|
||||
}
|
||||
|
||||
override fun doSetParameter(ps: PreparedStatement, index: Int, parameter: T) {
|
||||
ps.setString(index, gson.toJson(parameter))
|
||||
}
|
||||
}
|
151
ts-database/src/main/kotlin/cn/tursom/database/SqlExtension.kt
Normal file
151
ts-database/src/main/kotlin/cn/tursom/database/SqlExtension.kt
Normal file
@ -0,0 +1,151 @@
|
||||
@file:Suppress("unused")
|
||||
|
||||
package cn.tursom.database
|
||||
|
||||
import com.ddbes.kotlin.Utils
|
||||
import com.ddbes.kotlin.jdbc.simpTableField
|
||||
import com.ddbes.kotlin.uncheckedCast
|
||||
import com.google.gson.Gson
|
||||
import me.liuwj.ktorm.dsl.Query
|
||||
import me.liuwj.ktorm.dsl.QueryRowSet
|
||||
import me.liuwj.ktorm.schema.*
|
||||
import java.math.BigDecimal
|
||||
import java.sql.Date
|
||||
import java.sql.Time
|
||||
import java.sql.Timestamp
|
||||
import java.sql.Types
|
||||
import java.time.*
|
||||
import java.util.*
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.javaField
|
||||
import kotlin.reflect.jvm.javaType
|
||||
|
||||
val KProperty<*>.table
|
||||
get() = AutoTable[javaField!!.declaringClass].uncheckedCast<AutoTable<Any>>()
|
||||
val <T : Any> KProperty<T>.sql
|
||||
get() = table[this.uncheckedCast<KProperty1<Any, T>>()]
|
||||
|
||||
inline val <reified T : Any> KProperty1<T, *>.table
|
||||
get() = AutoTable[T::class.java]
|
||||
|
||||
inline val <reified T : Any, R : Any> KProperty1<T, R?>.sql
|
||||
get() = table[this]
|
||||
|
||||
fun <T> Query.getOne(transform: (rowSet: QueryRowSet) -> T): T? = if (rowSet.next()) {
|
||||
transform(rowSet)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> Query.getOne(): T? = if (rowSet.next()) {
|
||||
AutoTable[T::class].createEntity(rowSet)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
fun <C : Any, E : Any> BaseTable<E>.json(
|
||||
field: KProperty1<E, C?>,
|
||||
type: Int = Types.VARCHAR,
|
||||
gson: Gson = Utils.gson,
|
||||
): Column<C> {
|
||||
val sqlType = JsonType<C>(field.returnType.javaType, type, gson)
|
||||
return this.registerColumn(field.simpTableField, sqlType)
|
||||
}
|
||||
|
||||
fun <E : Any> BaseTable<E>.boolean(
|
||||
field: KProperty1<E, Boolean?>,
|
||||
) = boolean(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.int(
|
||||
field: KProperty1<E, Int?>,
|
||||
) = int(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.long(
|
||||
field: KProperty1<E, Long?>,
|
||||
) = long(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.float(
|
||||
field: KProperty1<E, Float?>,
|
||||
) = float(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.double(
|
||||
field: KProperty1<E, Double?>,
|
||||
) = double(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.decimal(
|
||||
field: KProperty1<E, BigDecimal?>,
|
||||
) = decimal(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.varchar(
|
||||
field: KProperty1<E, String?>,
|
||||
) = varchar(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.text(
|
||||
field: KProperty1<E, String?>,
|
||||
) = text(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.blob(
|
||||
field: KProperty1<E, ByteArray?>,
|
||||
) = blob(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.bytes(
|
||||
field: KProperty1<E, ByteArray?>,
|
||||
) = bytes(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.jdbcTimestamp(
|
||||
field: KProperty1<E, Timestamp?>,
|
||||
) = jdbcTimestamp(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.jdbcDate(
|
||||
field: KProperty1<E, Date?>,
|
||||
) = jdbcDate(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.jdbcTime(
|
||||
field: KProperty1<E, Time?>,
|
||||
) = jdbcTime(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.timestamp(
|
||||
field: KProperty1<E, Instant?>,
|
||||
) = timestamp(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.datetime(
|
||||
field: KProperty1<E, LocalDateTime?>,
|
||||
) = datetime(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.date(
|
||||
field: KProperty1<E, LocalDate?>,
|
||||
) = date(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.time(
|
||||
field: KProperty1<E, LocalTime?>,
|
||||
) = time(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.monthDay(
|
||||
field: KProperty1<E, MonthDay?>,
|
||||
) = monthDay(field.simpTableField)
|
||||
|
||||
|
||||
fun <E : Any> BaseTable<E>.yearMonth(
|
||||
field: KProperty1<E, YearMonth?>,
|
||||
) = yearMonth(field.simpTableField)
|
||||
|
||||
|
||||
fun <E : Any> BaseTable<E>.year(
|
||||
field: KProperty1<E, Year?>,
|
||||
) = year(field.simpTableField)
|
||||
|
||||
fun <E : Any> BaseTable<E>.uuid(
|
||||
field: KProperty1<E, UUID?>,
|
||||
) = uuid(field.simpTableField)
|
||||
|
||||
fun <E : Any, C : Enum<C>> BaseTable<E>.enum(
|
||||
field: KProperty1<E, C?>,
|
||||
typeRef: TypeReference<C>,
|
||||
) = enum(field.simpTableField, typeRef)
|
||||
|
||||
fun <E : Any, C : Enum<C>> BaseTable<E>.enum(
|
||||
field: KProperty1<E, C?>,
|
||||
type: Class<C>,
|
||||
) = registerColumn(field.simpTableField, EnumSqlType(type))
|
||||
|
@ -0,0 +1,25 @@
|
||||
package cn.tursom.database
|
||||
|
||||
import me.liuwj.ktorm.schema.BaseTable
|
||||
import me.liuwj.ktorm.schema.Column
|
||||
import kotlin.reflect.KProperty1
|
||||
|
||||
/**
|
||||
* 将属性转化为表列的适配器
|
||||
*/
|
||||
interface TypeAdapter<T : Any> {
|
||||
/**
|
||||
* 该适配器的优先级,优先级越高则越优先匹配
|
||||
* 同等优先级先注册者先匹配
|
||||
*/
|
||||
val level: Int get() = 0
|
||||
|
||||
/**
|
||||
* 注册列
|
||||
* 如果失败则返回null
|
||||
*/
|
||||
fun register(
|
||||
table: BaseTable<Any>,
|
||||
field: KProperty1<Any, T>
|
||||
): Column<T>?
|
||||
}
|
@ -0,0 +1,76 @@
|
||||
package cn.tursom.database
|
||||
|
||||
import com.ddbes.kotlin.classutil.getClassByPackage
|
||||
import com.ddbes.kotlin.clone.InstantAllocator
|
||||
import com.ddbes.kotlin.uncheckedCast
|
||||
import me.liuwj.ktorm.schema.BaseTable
|
||||
import me.liuwj.ktorm.schema.Column
|
||||
import java.util.concurrent.ConcurrentSkipListMap
|
||||
import java.util.concurrent.CopyOnWriteArraySet
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.full.isSubclassOf
|
||||
|
||||
/**
|
||||
* 注册类型适配器的工厂
|
||||
*/
|
||||
object TypeAdapterFactory {
|
||||
private val adapterMap = ConcurrentSkipListMap<Int, MutableSet<TypeAdapter<*>>>()
|
||||
|
||||
init {
|
||||
scanPackage(TypeAdapterFactory::class.java.`package`.name + ".typeadapter")
|
||||
}
|
||||
|
||||
private fun getAdapterQueue(level: Int): MutableCollection<TypeAdapter<*>> {
|
||||
var set = adapterMap[-level]
|
||||
if (set == null) {
|
||||
synchronized(this) {
|
||||
adapterMap[-level] = CopyOnWriteArraySet()
|
||||
set = adapterMap[-level]!!
|
||||
}
|
||||
}
|
||||
return set!!
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描包并注册适配器
|
||||
*/
|
||||
fun scanPackage(
|
||||
pkg: String,
|
||||
classLoader: ClassLoader = this.javaClass.classLoader,
|
||||
) {
|
||||
classLoader.getClassByPackage(pkg).forEach {
|
||||
try {
|
||||
val clazz = Class.forName(it).kotlin
|
||||
if (clazz.isSubclassOf(TypeAdapter::class)) {
|
||||
val adapter: TypeAdapter<*> = InstantAllocator[clazz].uncheckedCast()
|
||||
registerAdapter(adapter)
|
||||
}
|
||||
} catch (e: Throwable) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册适配器实例
|
||||
*/
|
||||
fun registerAdapter(adapter: TypeAdapter<*>) {
|
||||
getAdapterQueue(adapter.level).add(adapter)
|
||||
}
|
||||
|
||||
fun register(
|
||||
table: BaseTable<*>,
|
||||
field: KProperty1<*, *>,
|
||||
): Column<*>? {
|
||||
adapterMap.forEach { (_, queue) ->
|
||||
queue.forEach {
|
||||
val column = it.register(table.uncheckedCast(), field.uncheckedCast())
|
||||
if (column != null) {
|
||||
return column
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
override fun toString() = adapterMap.toString()
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package cn.tursom.database.annotations
|
||||
|
||||
|
||||
@MustBeDocumented
|
||||
@Retention(AnnotationRetention.RUNTIME)
|
||||
@Target(AnnotationTarget.PROPERTY)
|
||||
annotation class Json
|
@ -0,0 +1,20 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "BooleanAdapter"
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "BytesAdapter"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "DateAdapter"
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "DoubleAdapter"
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
package cn.tursom.database.typeadapter
|
||||
|
||||
import cn.tursom.database.TypeAdapter
|
||||
import com.ddbes.kotlin.jdbc.simpTableField
|
||||
import com.ddbes.kotlin.uncheckedCast
|
||||
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
|
||||
|
||||
object EnumAdapter : TypeAdapter<EnumAdapter.EnumType> {
|
||||
enum class EnumType
|
||||
|
||||
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(field.simpTableField, EnumSqlType<EnumType>(kClass.java.uncheckedCast()))
|
||||
.uncheckedCast()
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "EnumAdapter"
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "FloatAdapter"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "InstantAdapter"
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "IntAdapter"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "IntAdapterAdapter"
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
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
|
||||
|
||||
object JsonAdapter : TypeAdapter<Int> {
|
||||
override val level: Int get() = -16
|
||||
|
||||
override fun register(table: BaseTable<Any>, field: KProperty1<Any, Int>): Column<Int>? {
|
||||
return if (field.findAnnotation<Json>() != null) {
|
||||
table.json(field)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "JsonAdapter"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "LocalDateAdapter"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "LocalDateTimeAdapter"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "LocalTimeAdapter"
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "LongAdapter"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "MonthDayAdapter"
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "StringAdapter"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
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
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "TimeAdapter"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package cn.tursom.database.typeadapter
|
||||
|
||||
import cn.tursom.database.TypeAdapter
|
||||
import cn.tursom.database.jdbcTimestamp
|
||||
import me.liuwj.ktorm.schema.BaseTable
|
||||
import me.liuwj.ktorm.schema.Column
|
||||
import java.sql.Timestamp
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "TimestampAdapter"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package cn.tursom.database.typeadapter
|
||||
|
||||
import cn.tursom.database.TypeAdapter
|
||||
import cn.tursom.database.uuid
|
||||
import me.liuwj.ktorm.schema.BaseTable
|
||||
import me.liuwj.ktorm.schema.Column
|
||||
import java.util.*
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "UUIDAdapter"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package cn.tursom.database.typeadapter
|
||||
|
||||
import cn.tursom.database.TypeAdapter
|
||||
import cn.tursom.database.year
|
||||
import me.liuwj.ktorm.schema.BaseTable
|
||||
import me.liuwj.ktorm.schema.Column
|
||||
import java.time.Year
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "YearAdapter"
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package cn.tursom.database.typeadapter
|
||||
|
||||
import cn.tursom.database.TypeAdapter
|
||||
import cn.tursom.database.yearMonth
|
||||
import me.liuwj.ktorm.schema.BaseTable
|
||||
import me.liuwj.ktorm.schema.Column
|
||||
import java.time.YearMonth
|
||||
import kotlin.reflect.KProperty1
|
||||
import kotlin.reflect.jvm.jvmErasure
|
||||
|
||||
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)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
override fun toString() = "YearMonthAdapter"
|
||||
}
|
Loading…
Reference in New Issue
Block a user