com.sxtanna.database.task.builder.CreateBuilder.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Kuery Show documentation
Show all versions of Kuery Show documentation
MySQL Kotlin wrapper based on HikariCP
package com.sxtanna.database.task.builder
import com.sxtanna.database.ext.co
import com.sxtanna.database.struct.Resolver
import com.sxtanna.database.struct.base.Duo
import com.sxtanna.database.struct.obj.SqlType
import com.sxtanna.database.type.base.SqlObject
import kotlin.reflect.full.declaredMemberProperties
data class CreateBuilder(val table : String) {
val columns = mutableListOf>()
private var inited = false
fun co(name : String, type : SqlType) = apply { columns.add(name co type) }
@JvmSynthetic
fun co(name : String, type : () -> SqlType) = apply { columns.add(name co type()) }
fun cos(vararg columns : Duo) = apply { columns.forEach { co(it.name, it.value) } }
@JvmSynthetic
inline fun init() = init(T::class.java)
fun init(clazz : Class) = apply {
if (inited) return@apply
columns.addAll(clazz.kotlin.declaredMemberProperties.map { Duo(it.name, Resolver[it]) })
inited = true
}
companion object Create {
fun table(table : String) = CreateBuilder(table)
@JvmSynthetic
inline fun from(table : String = T::class.simpleName!!) = CreateBuilder(table).init()
@JvmOverloads
fun from(clazz : Class, table : String = clazz.simpleName!!) = CreateBuilder(table).init(clazz)
}
}