diff --git a/ts-database/src/main/kotlin/cn/tursom/database/AutoTable.kt b/ts-database/src/main/kotlin/cn/tursom/database/AutoTable.kt index 1e5c450..64d1d39 100644 --- a/ts-database/src/main/kotlin/cn/tursom/database/AutoTable.kt +++ b/ts-database/src/main/kotlin/cn/tursom/database/AutoTable.kt @@ -50,7 +50,7 @@ open class AutoTable( return instance } - operator fun get(property: KProperty1): Column = fieldColumns[property].uncheckedCast() + operator fun get(property: KProperty1): Column = fieldColumns[property].uncheckedCast() //operator fun get(property: KProperty1): Column = this[property.simpTableField].cast() fun field(): FieldProxy = fieldProxyInstance.uncheckedCast() diff --git a/ts-database/src/main/kotlin/cn/tursom/database/KtormOperators.kt b/ts-database/src/main/kotlin/cn/tursom/database/KtormOperators.kt new file mode 100644 index 0000000..a7b1ca6 --- /dev/null +++ b/ts-database/src/main/kotlin/cn/tursom/database/KtormOperators.kt @@ -0,0 +1,196 @@ +@file:Suppress("unused") + +package cn.tursom.database + +import org.ktorm.dsl.* +import org.ktorm.expression.* +import org.ktorm.schema.SqlType +import kotlin.reflect.KProperty1 + +inline fun KProperty1.isNull(): UnaryExpression = sql.isNull() +inline fun KProperty1.isNotNull(): UnaryExpression = sql.isNotNull() +inline operator fun KProperty1.unaryMinus(): UnaryExpression = + table[this].unaryMinus() + +inline operator fun KProperty1.unaryPlus(): UnaryExpression = + table[this].unaryPlus() + +inline operator fun KProperty1.not(): UnaryExpression = table[this].not() + +inline infix operator fun KProperty1.plus(expr: KProperty1): BinaryExpression = + table[this].plus(expr.table[expr]) + +inline infix operator fun KProperty1.plus(value: T): BinaryExpression = + table[this].plus(value) + +inline infix operator fun T.plus(expr: KProperty1): BinaryExpression = + plus(expr.table[expr]) + +inline infix operator fun KProperty1.minus(expr: KProperty1): BinaryExpression = + table[this].minus(expr.table[expr]) + +inline infix operator fun KProperty1.minus(value: T): BinaryExpression = + table[this].minus(value) + +inline infix operator fun T.minus(expr: KProperty1): BinaryExpression = + minus(expr.table[expr]) + +inline infix operator fun KProperty1.times(expr: KProperty1): BinaryExpression = + table[this].times(expr.table[expr]) + +inline infix operator fun KProperty1.times(value: T): BinaryExpression = + table[this].times(value) + +inline infix operator fun T.times(expr: KProperty1): BinaryExpression = + times(expr.table[expr]) + +inline infix operator fun KProperty1.div(expr: KProperty1): BinaryExpression = + table[this].div(expr.table[expr]) + +inline infix operator fun KProperty1.div(value: T): BinaryExpression = + table[this].div(value) + +inline infix operator fun T.div(expr: KProperty1): BinaryExpression = + div(expr.table[expr]) + +inline infix operator fun KProperty1.rem(expr: KProperty1): BinaryExpression = + table[this].rem(expr.table[expr]) + +inline infix operator fun KProperty1.rem(value: T): BinaryExpression = + table[this].rem(value) + +inline infix operator fun T.rem(expr: KProperty1): BinaryExpression = + rem(expr.table[expr]) + +inline infix fun KProperty1.like(expr: KProperty1): BinaryExpression = + table[this].like(expr.table[expr]) + +inline infix fun KProperty1.like(value: String): BinaryExpression = + table[this].like(value) + +inline infix fun KProperty1.notLike(expr: KProperty1): BinaryExpression = + table[this].notLike(expr.table[expr]) + +inline infix fun KProperty1.notLike(value: String): BinaryExpression = + table[this].notLike(value) + +inline infix fun KProperty1.and(expr: KProperty1): BinaryExpression = + table[this].and(expr.table[expr]) + +inline infix fun KProperty1.and(value: Boolean): BinaryExpression = + table[this].and(value) + +inline infix fun Boolean.and(expr: KProperty1): BinaryExpression = + and(expr.table[expr]) + +inline infix fun KProperty1.or(expr: KProperty1): BinaryExpression = + table[this].or(expr.table[expr]) + +inline infix fun KProperty1.or(value: Boolean): BinaryExpression = + table[this].or(value) + +inline infix fun Boolean.or(expr: KProperty1): BinaryExpression = + or(expr.table[expr]) + +inline infix fun KProperty1.xor(expr: KProperty1): BinaryExpression = + table[this].xor(expr.table[expr]) + +inline infix fun KProperty1.xor(value: Boolean): BinaryExpression = + table[this].xor(value) + +inline infix fun Boolean.xor(expr: KProperty1): BinaryExpression = + xor(expr.table[expr]) + +inline infix fun > KProperty1.less(expr: KProperty1): BinaryExpression = + table[this].less(expr.table[expr]) + +inline infix fun > KProperty1.less(value: T): BinaryExpression = + table[this].less(value) + +inline infix fun > T.less(expr: KProperty1): BinaryExpression = + less(expr.table[expr]) + +inline infix fun > KProperty1.lessEq(expr: KProperty1): BinaryExpression = + table[this].lessEq(expr.table[expr]) + +inline infix fun > KProperty1.lessEq(value: T): BinaryExpression = + table[this].lessEq(value) + +inline infix fun > T.lessEq(expr: KProperty1): BinaryExpression = + lessEq(expr.table[expr]) + +inline infix fun > KProperty1.greater(expr: KProperty1): BinaryExpression = + table[this].greater(expr.table[expr]) + +inline infix fun > KProperty1.greater(value: T): BinaryExpression = + table[this].greater(value) + +inline infix fun > T.greater(expr: KProperty1): BinaryExpression = + greater(expr.table[expr]) + +inline infix fun > KProperty1.greaterEq(expr: KProperty1): BinaryExpression = + table[this].greaterEq(expr.table[expr]) + +inline infix fun > KProperty1.greaterEq(value: T): BinaryExpression = + table[this].greaterEq(value) + +inline infix fun > T.greaterEq(expr: KProperty1): BinaryExpression = + greaterEq(expr.table[expr]) + +inline infix fun KProperty1.eq(expr: KProperty1): BinaryExpression = + table[this].eq(expr.table[expr]) + +inline infix fun KProperty1.eq(value: T): BinaryExpression = + table[this].eq(value) + +inline infix fun KProperty1.notEq(expr: KProperty1): BinaryExpression = + table[this].notEq(expr.table[expr]) + +inline infix fun KProperty1.notEq(value: T): BinaryExpression = + table[this].notEq(value) + +inline infix fun > KProperty1.between(range: ClosedRange): BetweenExpression = + table[this].between(range) + +inline infix fun > KProperty1.notBetween(range: ClosedRange): BetweenExpression = + table[this].notBetween(range) + +inline fun KProperty1.inList(vararg list: T): InListExpression = + table[this].inList(list = list) + +inline infix fun KProperty1.inList(list: Collection): InListExpression = + table[this].inList(list) + +inline infix fun KProperty1.inList(query: Query): InListExpression = + table[this].inList(query) + +inline fun KProperty1.notInList(vararg list: T): InListExpression = + table[this].notInList(list = list) + +inline infix fun KProperty1.notInList(list: Collection): InListExpression = + table[this].notInList(list) + +inline infix fun KProperty1.notInList(query: Query): InListExpression = + table[this].notInList(query) + +inline fun KProperty1.toDouble(): CastingExpression = + table[this].toDouble() + +inline fun KProperty1.toFloat(): CastingExpression = + table[this].toFloat() + +inline fun KProperty1.toInt(): CastingExpression = + table[this].toInt() + +inline fun KProperty1.toShort(): CastingExpression = + table[this].toShort() + +inline fun KProperty1.toLong(): CastingExpression = + table[this].toLong() + +@JvmName("booleanToInt") +inline fun KProperty1.toInt(): CastingExpression = + table[this].toInt() + +inline fun KProperty1.cast(sqlType: SqlType): CastingExpression = + table[this].cast(sqlType) diff --git a/ts-database/src/main/kotlin/cn/tursom/database/SqlExtension.kt b/ts-database/src/main/kotlin/cn/tursom/database/SqlExtension.kt index ad6d363..213685f 100644 --- a/ts-database/src/main/kotlin/cn/tursom/database/SqlExtension.kt +++ b/ts-database/src/main/kotlin/cn/tursom/database/SqlExtension.kt @@ -5,8 +5,8 @@ package cn.tursom.database import cn.tursom.core.Utils import cn.tursom.core.uncheckedCast import com.google.gson.Gson -import org.ktorm.dsl.Query -import org.ktorm.dsl.QueryRowSet +import org.ktorm.database.Database +import org.ktorm.dsl.* import org.ktorm.schema.* import java.math.BigDecimal import java.sql.Date @@ -20,15 +20,18 @@ import kotlin.reflect.KProperty1 import kotlin.reflect.jvm.javaField import kotlin.reflect.jvm.javaType +inline fun Database.from(): QuerySource = from(AutoTable[T::class]) +fun QuerySource.select(): Query = select(sourceTable.columns) + val KProperty<*>.table get() = AutoTable[javaField!!.declaringClass].uncheckedCast>() val KProperty.sql get() = table[this.uncheckedCast>()] -inline val KProperty1.table +inline val KProperty1.table get() = AutoTable[T::class.java] -inline val KProperty1.sql +inline val KProperty1.sql get() = table[this] fun Query.getOne(transform: (rowSet: QueryRowSet) -> T): T? = if (rowSet.next()) { diff --git a/ts-database/src/main/kotlin/cn/tursom/database/TableField.kt b/ts-database/src/main/kotlin/cn/tursom/database/TableField.kt index 6b2add2..932cb22 100644 --- a/ts-database/src/main/kotlin/cn/tursom/database/TableField.kt +++ b/ts-database/src/main/kotlin/cn/tursom/database/TableField.kt @@ -64,7 +64,7 @@ val Iterable, *>>.filterNotExists val Iterable>.filterNotExists get() = filter { it.javaField != null && - it.findAnnotation() ?: it.javaField?.getAnnotation(Transient::class.java) == null && + (it.findAnnotation() ?: it.javaField?.getAnnotation(Transient::class.java)) == null && !Modifier.isTransient(it.javaField?.modifiers ?: Modifier.TRANSIENT) && it.javaField?.getAnnotation(com.baomidou.mybatisplus.annotation.TableField::class.java)?.exist != false }