mirror of
https://github.com/tursom/TursomServer.git
synced 2025-03-13 11:20:10 +08:00
update ts-ktorm
This commit is contained in:
parent
5f3f30b403
commit
5e6edec783
@ -1,5 +1,6 @@
|
||||
package cn.tursom.database.ktorm
|
||||
|
||||
import cn.tursom.core.uncheckedCast
|
||||
import org.ktorm.dsl.AssignmentsBuilder
|
||||
import org.ktorm.schema.ColumnDeclaring
|
||||
import kotlin.reflect.KProperty1
|
||||
@ -17,3 +18,13 @@ inline fun <reified T : Any, C : Any> AssignmentsBuilder.set(
|
||||
) {
|
||||
set(AutoTable[T::class.java][column], expr)
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> AssignmentsBuilder.set(
|
||||
table: AutoTable<T>,
|
||||
value: T,
|
||||
) {
|
||||
table.fieldColumnsMap.forEach { (property, column) ->
|
||||
val columnValue = property.get(value) ?: return@forEach
|
||||
set(column.uncheckedCast(), columnValue)
|
||||
}
|
||||
}
|
||||
|
@ -1,59 +1,78 @@
|
||||
package cn.tursom.database.ktorm
|
||||
|
||||
import cn.tursom.core.uncheckedCast
|
||||
import org.ktorm.database.Database
|
||||
import org.ktorm.dsl.*
|
||||
import org.ktorm.schema.Column
|
||||
import org.ktorm.schema.ColumnDeclaring
|
||||
|
||||
inline fun <reified T : Any> Database.update(
|
||||
noinline block: UpdateStatementBuilder.(AutoTable<T>) -> Unit
|
||||
noinline block: UpdateStatementBuilder.(AutoTable<T>) -> Unit,
|
||||
): Int {
|
||||
return update(AutoTable[T::class], block)
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> Database.batchUpdate(
|
||||
noinline block: BatchUpdateStatementBuilder<AutoTable<T>>.() -> Unit
|
||||
noinline block: BatchUpdateStatementBuilder<AutoTable<T>>.() -> Unit,
|
||||
): IntArray {
|
||||
return batchUpdate(AutoTable[T::class], block)
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> Database.insert(
|
||||
noinline block: AssignmentsBuilder.(AutoTable<T>) -> Unit
|
||||
noinline block: AssignmentsBuilder.(AutoTable<T>) -> Unit,
|
||||
): Int {
|
||||
return insert(AutoTable[T::class], block)
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> Database.insert(
|
||||
value: T
|
||||
value: T,
|
||||
): Int {
|
||||
val table = AutoTable[T::class]
|
||||
return insert(table) {
|
||||
table.fieldColumnsMap.forEach { (property, column) ->
|
||||
val columnValue = property.get(value) ?: return@forEach
|
||||
set(column.uncheckedCast(), columnValue)
|
||||
}
|
||||
set(table, value)
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> Database.insertAndGenerateKey(
|
||||
noinline block: AssignmentsBuilder.(AutoTable<T>) -> Unit
|
||||
noinline block: AssignmentsBuilder.(AutoTable<T>) -> Unit,
|
||||
): Any {
|
||||
return insertAndGenerateKey(AutoTable[T::class], block)
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> Database.batchInsert(
|
||||
noinline block: BatchInsertStatementBuilder<AutoTable<T>>.() -> Unit
|
||||
noinline block: BatchInsertStatementBuilder<AutoTable<T>>.() -> Unit,
|
||||
): IntArray {
|
||||
return batchInsert(AutoTable[T::class], block)
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> Database.batchInsert(
|
||||
values: Iterable<T>,
|
||||
): IntArray = batchInsert<T> {
|
||||
val table = AutoTable[T::class]
|
||||
values.forEach { value ->
|
||||
item {
|
||||
set(table, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> Database.batchInsert(
|
||||
values: Iterable<T>,
|
||||
noinline block: AssignmentsBuilder.(AutoTable<T>, value: T) -> Unit,
|
||||
): IntArray = batchInsert<T> {
|
||||
val table = AutoTable[T::class]
|
||||
values.forEach { value ->
|
||||
item {
|
||||
block(table, value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> Query.insertTo(vararg columns: Column<*>): Int {
|
||||
return insertTo(AutoTable[T::class], columns = columns)
|
||||
}
|
||||
|
||||
inline fun <reified T : Any> Database.delete(
|
||||
noinline predicate: (AutoTable<T>) -> ColumnDeclaring<Boolean>
|
||||
noinline predicate: (AutoTable<T>) -> ColumnDeclaring<Boolean>,
|
||||
): Int {
|
||||
return delete(AutoTable[T::class], predicate)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user