feat:1.加入Range支持 2.orderBy中缀 3.通过重写操作符生成QueryOrderBy对象

This commit is contained in:
kamosama 2023-08-13 10:12:54 +08:00
parent ba39d4d9aa
commit 301b54fd43
2 changed files with 48 additions and 15 deletions

View File

@ -23,9 +23,11 @@ import java.util.function.Consumer
* @author 卡莫sama(yuanjiashuai) * @author 卡莫sama(yuanjiashuai)
* @date 2023/8/7 * @date 2023/8/7
*/ */
infix fun QueryColumn.eq(value: Any?): QueryCondition {
return this.eq(value)
} //logic------
inline fun `if`(test: Boolean, block: () -> QueryCondition): QueryCondition =
if (test) block() else QueryCondition.createEmpty()
inline fun QueryCondition.andIf(test: Boolean, block: () -> QueryCondition): QueryCondition = inline fun QueryCondition.andIf(test: Boolean, block: () -> QueryCondition): QueryCondition =
if (test) this.and(block()) else this if (test) this.and(block()) else this
@ -33,15 +35,17 @@ inline fun QueryCondition.andIf(test: Boolean, block: () -> QueryCondition): Que
inline fun QueryCondition.orIf(test: Boolean, block: () -> QueryCondition): QueryCondition = inline fun QueryCondition.orIf(test: Boolean, block: () -> QueryCondition): QueryCondition =
if (test) this.or(block()) else this if (test) this.or(block()) else this
inline fun `if`(test: Boolean, block: () -> QueryCondition): QueryCondition =
if (test) block() else QueryCondition.createEmpty()
infix fun QueryColumn.like(value: String): QueryCondition = this.like(value)
infix fun QueryCondition.and(other: QueryCondition): QueryCondition = this.and(other) infix fun QueryCondition.and(other: QueryCondition): QueryCondition = this.and(other)
infix fun QueryCondition.or(other: QueryCondition): QueryCondition = this.or(other) infix fun QueryCondition.or(other: QueryCondition): QueryCondition = this.or(other)
//Comparable------
infix fun QueryColumn.like(value: String): QueryCondition = this.like(value)
infix fun QueryColumn.eq(value: Any?): QueryCondition = this.eq(value)
infix fun QueryColumn.ne(value: Any?): QueryCondition = this.ne(value)
infix fun QueryColumn.`=`(value: Any?): QueryCondition = this.eq(value) infix fun QueryColumn.`=`(value: Any?): QueryCondition = this.eq(value)
infix fun QueryColumn.`!=`(value: Any?): QueryCondition = this.ne(value) infix fun QueryColumn.`!=`(value: Any?): QueryCondition = this.ne(value)
@ -54,10 +58,16 @@ infix fun QueryColumn.le(value: Any?): QueryCondition = this.le(value)
infix fun QueryColumn.lt(value: Any?): QueryCondition = this.lt(value) infix fun QueryColumn.lt(value: Any?): QueryCondition = this.lt(value)
//range-----
infix fun QueryColumn.between(pair: Pair<Any?, Any?>): QueryCondition = this.between(pair.first, pair.second) infix fun QueryColumn.between(pair: Pair<Any?, Any?>): QueryCondition = this.between(pair.first, pair.second)
infix fun QueryColumn.notBetween(pair: Pair<Any?, Any?>): QueryCondition = this.notBetween(pair.first, pair.second) infix fun QueryColumn.notBetween(pair: Pair<Any?, Any?>): QueryCondition = this.notBetween(pair.first, pair.second)
infix fun QueryColumn.between(range: ClosedRange<*>): QueryCondition = this.between(range.start, range.endInclusive)
infix fun QueryColumn.notBetween(range: ClosedRange<*>): QueryCondition =
this.notBetween(range.start, range.endInclusive)
infix fun QueryColumn.notIn(value: Collection<Any?>): QueryCondition = this.notIn(value) infix fun QueryColumn.notIn(value: Collection<Any?>): QueryCondition = this.notIn(value)
infix fun QueryColumn.notIn(values: Array<Any?>): QueryCondition = this.notIn(values) infix fun QueryColumn.notIn(values: Array<Any?>): QueryCondition = this.notIn(values)
@ -66,8 +76,12 @@ infix fun QueryColumn.`in`(value: Collection<Any?>): QueryCondition = this.`in`(
infix fun QueryColumn.`in`(values: Array<Any?>): QueryCondition = this.`in`(values) infix fun QueryColumn.`in`(values: Array<Any?>): QueryCondition = this.`in`(values)
infix fun QueryColumn.`in`(range: IntRange): QueryCondition = this.`in`(range.toList())
//as-----
infix fun QueryWrapper.`as`(alias: String?) = this.`as`(alias) infix fun QueryWrapper.`as`(alias: String?) = this.`as`(alias)
//join------
infix fun <M> Joiner<M>.`as`(alias: String?): Joiner<M> = this.`as`(alias) infix fun <M> Joiner<M>.`as`(alias: String?): Joiner<M> = this.`as`(alias)
infix fun <M> Joiner<M>.on(on: String?): M = this.on(on) infix fun <M> Joiner<M>.on(on: String?): M = this.on(on)
@ -76,8 +90,20 @@ infix fun <M> Joiner<M>.on(on: QueryCondition?): M = this.on(on)
infix fun <M> Joiner<M>.on(consumer: Consumer<QueryWrapper?>): M = this.on(consumer) infix fun <M> Joiner<M>.on(consumer: Consumer<QueryWrapper?>): M = this.on(consumer)
infix fun QueryWrapper.orderBy(orderBys: Collection<QueryOrderBy?>): QueryWrapper = this.orderBy(*orderBys.toTypedArray()) // orderBy------
infix fun QueryWrapper.orderBy(orderBys: Collection<QueryOrderBy?>): QueryWrapper =
this.orderBy(*orderBys.toTypedArray())
infix fun QueryWrapper.orderBy(orderBy: QueryOrderBy?): QueryWrapper = this.orderBy(orderBy)
operator fun QueryColumn.unaryPlus(): QueryOrderBy = this.asc()
operator fun QueryColumn.unaryMinus(): QueryOrderBy = this.desc()
// limit------
infix fun QueryWrapper.limit(rows: Number): QueryWrapper = this.limit(rows) infix fun QueryWrapper.limit(rows: Number): QueryWrapper = this.limit(rows)
infix fun QueryWrapper.limit(pair: Pair<Number?, Number?>): QueryWrapper = this.limit(pair.first,pair.second) infix fun QueryWrapper.limit(pair: Pair<Number?, Number?>): QueryWrapper = this.limit(pair.first, pair.second)
infix fun QueryWrapper.limit(range: IntRange): QueryWrapper = this.limit(range.first, range.last)

View File

@ -79,15 +79,22 @@ class KotlinTest {
// 查询表对象对应的所有实体数据 // 查询表对象对应的所有实体数据
ACCOUNT.all<Account>().forEach(::println) ACCOUNT.all<Account>().forEach(::println)
// ACCOUNT.query<Account> {}.forEach(::println) // ACCOUNT.query<Account> {}.forEach(::println)
// a and (b or c) // a and (b or c)
// filter<Account> { // filter:
// ACCOUNT.AGE `=` 12 and
// (ACCOUNT.ID `in` listOf(1, 2) or (ACCOUNT.ID `in` listOf(1, 2)))
// }
filter<Account> { filter<Account> {
ACCOUNT.ID `=` 1 and ACCOUNT.ID `=` 1 and
(ACCOUNT.AGE `in` listOf(18,19) or (ACCOUNT.BIRTHDAY between ("2020-01-10" to "2020-01-12")) ) (ACCOUNT.AGE `in` (17..19) or (ACCOUNT.BIRTHDAY between ("2020-01-10" to "2020-01-12")) )
}.forEach(::println) }.forEach(::println)
// query:
query <Account> {
from(Account)
where(Account) {
(AGE `in` (17..19) or (BIRTHDAY between ("2020-01-10" .. "2020-01-12")) )
} orderBy -Account.ID
}.forEach(::println)
// 查询表对象对应的实体数据并根据条件过滤 // 查询表对象对应的实体数据并根据条件过滤
filter<Account> { filter<Account> {
ACCOUNT.AGE `=` 12 or ACCOUNT.AGE `=` 12 or