From 9619d01527305f129a0f8bccf667961e2a0e9044 Mon Sep 17 00:00:00 2001 From: kamosama <837080904@qq.com> Date: Mon, 18 Mar 2024 16:00:31 +0800 Subject: [PATCH 01/11] =?UTF-8?q?feat:=201.=E6=96=B0=E5=A2=9ERow.toObject?= =?UTF-8?q?=E5=86=85=E8=81=94=E6=96=B9=E6=B3=95=EF=BC=9B2.=E6=96=B0?= =?UTF-8?q?=E5=A2=9EandAll=EF=BC=8CorAll=E7=AD=89=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=8B=BC=E6=8E=A5=E5=90=8C=E6=A0=B7=E6=93=8D?= =?UTF-8?q?=E4=BD=9C=E7=AC=A6=E7=9A=84=E6=9D=A1=E4=BB=B6=EF=BC=9B3?= =?UTF-8?q?=E3=80=82=E6=96=B0=E5=A2=9Eupdate=E6=96=B9=E6=B3=95=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../condition/QueryConditionExtensions.kt | 7 +++ .../main/kotlin/extensions/db/DbExtensions.kt | 39 ++++++++---- .../extensions/model/ModelExtensions.kt | 5 ++ .../wrapper/QueryWrapperExtensions.kt | 24 +++++--- .../src/main/kotlin/scope/QueryScope.kt | 3 +- .../src/main/kotlin/scope/UpdateScope.kt | 47 ++++++++++++++ .../src/test/kotlin/example/KotlinExample.kt | 61 +++++++++++++++++-- 7 files changed, 161 insertions(+), 25 deletions(-) create mode 100644 mybatis-flex-kotlin-extensions/src/main/kotlin/scope/UpdateScope.kt diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/QueryConditionExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/QueryConditionExtensions.kt index b5f4353..06fdf2a 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/QueryConditionExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/QueryConditionExtensions.kt @@ -40,3 +40,10 @@ infix fun QueryCondition.or(other: QueryCondition): QueryCondition = this.or(oth inline fun `if`(test: Boolean, block: () -> QueryCondition): QueryCondition = if (test) block() else QueryCondition.createEmpty() +fun QueryCondition.andAll(vararg conditions: QueryCondition): QueryCondition = this and allAnd(*conditions) + +fun QueryCondition.orAll(vararg conditions: QueryCondition): QueryCondition = this or allOr(*conditions) + +fun allAnd(vararg conditions: QueryCondition): QueryCondition = conditions.reduce(QueryCondition::and) + +fun allOr(vararg conditions: QueryCondition): QueryCondition = conditions.reduce(QueryCondition::or) diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt index 34d547d..3483ca5 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt @@ -33,7 +33,9 @@ import com.mybatisflex.kotlin.extensions.kproperty.toQueryColumns import com.mybatisflex.kotlin.extensions.model.toEntities import com.mybatisflex.kotlin.extensions.model.toEntityPage import com.mybatisflex.kotlin.scope.QueryScope +import com.mybatisflex.kotlin.scope.UpdateScope import com.mybatisflex.kotlin.scope.queryScope +import com.mybatisflex.kotlin.scope.updateScope import kotlin.reflect.KClass import kotlin.reflect.KProperty import kotlin.reflect.full.isSubclassOf @@ -66,7 +68,7 @@ val KClass.baseMapperOrNull: BaseMapper? val KClass.tableInfo: TableInfo get() = requireNotNull(tableInfoOrNull) { - "The class TableInfo cannot be found through $this" + + "Ehe class TableInfo cannot be found through $this" + " because the entity class corresponding to the generic used by this interface to inherit from BaseMapper cannot be found." } @@ -84,19 +86,19 @@ val KClass.tableInfoOrNull: TableInfo? * @param columns 查询的列 * @param init 查询作用域初始化函数 */ -inline fun queryOne( +inline fun queryOne( vararg columns: QueryColumn, init: QueryScope.() -> Unit -): T? = T::class.baseMapperOrNull?.let { +): E? = E::class.baseMapperOrNull?.let { val scope = QueryScope().apply(init) if (!scope.hasSelect() && columns.isNotEmpty()) scope.select(*columns) it.selectOneByQuery(scope) -} ?: T::class.tableInfo.let { +} ?: E::class.tableInfo.let { queryRow(schema = it.schema, tableName = it.tableName, columns = columns) { init() // 如果未调用select方法,则默认查询所有列 - if (this.hasSelect().not()) select(T::class.allColumns) - }?.toEntity(T::class.java) + if (this.hasSelect().not()) select(E::class.allColumns) + }?.toEntity(E::class.java) } /** @@ -104,17 +106,17 @@ inline fun queryOne( * @param columns 查询的列 * @param init 查询作用域初始化函数 */ -inline fun query( +inline fun query( vararg columns: QueryColumn, init: QueryScope.() -> Unit -): List = try { - T::class.baseMapper.selectListByQuery(queryScope(columns = columns, init = init)) +): List = try { + E::class.baseMapper.selectListByQuery(queryScope(columns = columns, init = init)) } catch (e: MybatisFlexException) { - T::class.tableInfo.run { + E::class.tableInfo.run { queryRows(schema = schema, tableName = tableName, columns = columns) { init() // 如果未调用select方法,则默认查询所有列 - if (this.hasSelect().not()) select(*T::class.defaultColumns) + if (this.hasSelect().not()) select(*E::class.defaultColumns) }.toEntities() } } @@ -257,3 +259,18 @@ inline fun queryPage( init: QueryScope.() -> Unit ): Page = Db.paginate(schema, tableName, page, queryScope(init = init)) +// update---------- +inline fun update(scope: UpdateScope.() -> Unit): Int { + updateScope().run { + scope() + val entity = updateWrapper.toEntity() + return try { + E::class.baseMapper.updateByQuery(entity, this) + } catch (e: MybatisFlexException) { + TODO() +// E::class.tableInfo.let { +// Db.updateByQuery(it.schema, it.tableName, entity.toRow(), this) +// } + } + } +} \ No newline at end of file diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/model/ModelExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/model/ModelExtensions.kt index df63578..2692c77 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/model/ModelExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/model/ModelExtensions.kt @@ -33,6 +33,11 @@ inline fun Row.toEntity(): T = RowUtil.toEntity(this, T::class.java) inline fun Collection.toEntities(): MutableList = RowUtil.toEntityList(this.toMutableList(), E::class.java) +inline fun Row.toObject(): T = RowUtil.toObject(this, T::class.java) + +inline fun Collection.toObjects(): MutableList = + RowUtil.toObjectList(this.toMutableList(), E::class.java) + inline fun Page.toEntityPage(): Page = Page(records.toEntities(), pageNumber, pageSize, totalRow) inline fun > List.batchInsert() = E::class.baseMapper.insertBatch(this) diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/wrapper/QueryWrapperExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/wrapper/QueryWrapperExtensions.kt index 2e20307..948be4b 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/wrapper/QueryWrapperExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/wrapper/QueryWrapperExtensions.kt @@ -17,6 +17,8 @@ package com.mybatisflex.kotlin.extensions.wrapper import com.mybatisflex.core.query.* import com.mybatisflex.core.util.MapperUtil +import com.mybatisflex.kotlin.extensions.condition.allAnd +import com.mybatisflex.kotlin.extensions.condition.allOr import com.mybatisflex.kotlin.extensions.kproperty.defaultColumns import com.mybatisflex.kotlin.extensions.kproperty.toQueryColumns import com.mybatisflex.kotlin.scope.QueryScope @@ -79,6 +81,20 @@ fun QueryWrapper.where(queryCondition: QueryCondition, consumer: (QueryWrapper) inline fun QueryWrapper.whereWith(queryCondition: () -> QueryCondition): QueryWrapper = where(queryCondition()) + +@OptIn(ExperimentalContracts::class) +inline fun QueryWrapper.having(predicate: () -> QueryCondition): QueryWrapper { + contract { + callsInPlace(predicate, InvocationKind.EXACTLY_ONCE) + } + return having(predicate()) +} + +fun QueryWrapper.andAll(vararg conditions: QueryCondition): QueryWrapper = this and allAnd(*conditions) + +fun QueryWrapper.orAll(vararg conditions: QueryCondition): QueryWrapper = this or allOr(*conditions) + + /** * wrapper的内部实现的访问,基于官方CPI而编写。其目的用于简化开发时的 * 代码编写,避免在开发时频繁地调用官方的API, @@ -151,13 +167,7 @@ value class QueryWrapperDevelopEntry(val wrapper: T) { var rows: Long get() = CPI.getLimitRows(wrapper) set(value) = CPI.setLimitRows(wrapper, value) -} -@OptIn(ExperimentalContracts::class) -inline fun QueryWrapper.having(predicate: () -> QueryCondition): QueryWrapper { - contract { - callsInPlace(predicate, InvocationKind.EXACTLY_ONCE) - } + fun setFromIfNecessary(schema: String, tableName: String) = CPI.setFromIfNecessary(wrapper, schema, tableName) - return having(predicate()) } diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/scope/QueryScope.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/scope/QueryScope.kt index 3a95d12..c4a7d43 100755 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/scope/QueryScope.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/scope/QueryScope.kt @@ -16,7 +16,6 @@ package com.mybatisflex.kotlin.scope import com.mybatisflex.core.query.QueryColumn -import com.mybatisflex.core.query.QueryWrapper import com.mybatisflex.core.query.QueryWrapperAdapter import com.mybatisflex.core.util.LambdaGetter import com.mybatisflex.kotlin.extensions.kproperty.column @@ -52,7 +51,7 @@ class QueryScope : QueryWrapperAdapter() { } -inline fun queryScope(vararg columns: QueryColumn, init: QueryScope.() -> Unit = {}): QueryWrapper = +inline fun queryScope(vararg columns: QueryColumn, init: QueryScope.() -> Unit = {}): QueryScope = QueryScope().apply(init).apply { if (columns.isNotEmpty() && !hasSelect()) select(*columns) } diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/scope/UpdateScope.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/scope/UpdateScope.kt new file mode 100644 index 0000000..16984b7 --- /dev/null +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/scope/UpdateScope.kt @@ -0,0 +1,47 @@ +package com.mybatisflex.kotlin.scope + +import com.mybatisflex.core.query.QueryColumn +import com.mybatisflex.core.query.QueryWrapperAdapter +import com.mybatisflex.core.table.TableInfoFactory +import com.mybatisflex.core.update.UpdateWrapper +import com.mybatisflex.core.util.UpdateEntity +import kotlin.reflect.KProperty1 + +class UpdateScope( + private val entityClass: Class, +) : QueryWrapperAdapter>() { + + @PublishedApi + internal val updateWrapper: UpdateWrapper = UpdateEntity.of(entityClass) as UpdateWrapper + + private val tableInfo = TableInfoFactory.ofEntityClass(entityClass) + + + infix fun KProperty1.set(value: V) { + updateWrapper.set(tableInfo.getColumnByProperty(this.name), value) + } + + infix fun KProperty1.setRaw(queryWrapper: QueryScope.() -> Unit) { + updateWrapper.setRaw(tableInfo.getColumnByProperty(this.name), queryScope().apply(queryWrapper)) + } + + fun KProperty1.setRaw(column: QueryColumn, queryWrapper: QueryScope.() -> Unit) { + setRaw { + queryWrapper() + select(column) + } + } + + fun KProperty1.setRaw(property: KProperty1<*, *>, queryWrapper: QueryScope.() -> Unit) { + setRaw { + queryWrapper() + select(property) + } + } + +} + + +inline fun updateScope(): UpdateScope { + return UpdateScope(T::class.java) +} \ No newline at end of file diff --git a/mybatis-flex-kotlin-extensions/src/test/kotlin/example/KotlinExample.kt b/mybatis-flex-kotlin-extensions/src/test/kotlin/example/KotlinExample.kt index 9c67c49..c646f30 100644 --- a/mybatis-flex-kotlin-extensions/src/test/kotlin/example/KotlinExample.kt +++ b/mybatis-flex-kotlin-extensions/src/test/kotlin/example/KotlinExample.kt @@ -11,6 +11,7 @@ import com.mybatisflex.kotlin.extensions.condition.and import com.mybatisflex.kotlin.extensions.condition.or import com.mybatisflex.kotlin.extensions.db.* import com.mybatisflex.kotlin.extensions.kproperty.* +import com.mybatisflex.kotlin.extensions.mapper.all import com.mybatisflex.kotlin.extensions.mapper.remove import com.mybatisflex.kotlin.extensions.mapper.save import com.mybatisflex.kotlin.extensions.mapper.update @@ -18,9 +19,7 @@ import com.mybatisflex.kotlin.extensions.model.batchDeleteById import com.mybatisflex.kotlin.extensions.model.batchInsert import com.mybatisflex.kotlin.extensions.model.batchUpdateById import com.mybatisflex.kotlin.extensions.sql.orderBy -import com.mybatisflex.kotlin.extensions.wrapper.and -import com.mybatisflex.kotlin.extensions.wrapper.from -import com.mybatisflex.kotlin.extensions.wrapper.selectFrom +import com.mybatisflex.kotlin.extensions.wrapper.* import com.mybatisflex.kotlin.scope.runFlex import org.apache.ibatis.logging.stdout.StdOutImpl import org.junit.jupiter.api.Test @@ -136,7 +135,7 @@ class KotlinExample { fun testUpdate() { // 通过条件更新 filterOne { Account::id eq 2 }?.apply { age = 20 }?.update { - Account::userName eq it.userName + Account::userName eq it.userName and (Account::age le 18) } // 通过id更新 // filterOne { Account::id eq 2 }?.apply { age = 20 }?.updateById() @@ -147,7 +146,7 @@ class KotlinExample { fun testDelete() { // 通过条件删除 filterOne { Account::id eq 2 }?.remove { - Account::userName eq it.userName + Account::userName eq it.userName and (Account::age le 18) } // 通过id删除 // filterOne { Account::id eq 2 }?.apply { age = 20 }?.removeById() @@ -274,4 +273,56 @@ class KotlinExample { Account.findByAge(18, 1).forEach(::println) } + @Test + fun testAllCondition() { + query { +// andAll: + andAll( + Account::id eq 1, + Account::age eq 18, + Account::userName eq "张三", + ) +// or +// (Account::id eq 1).andAll( +// Account::age eq 18, +// Account::userName eq "张三", +// ) + +// orAll: +// orAll( +// Account::id eq 1, +// Account::age `in` (17..20), +// Account::userName eq "张三", +// ) +// or +// (Account::id eq 1).orAll( +// Account::age `in` (17..20), +// Account::userName eq "张三", +// ) + + }.also { println(it) } + } + + @Test + fun testUpdate2() { + println("更新前: ${Account::class.all.first()}") + update { + Account::id set 5 + Account::age setRaw { + select(Account::age) + from(Account::class) + this.and(Account::age `in` (19..20)) + limit(1) + } +// or +// Account::age.setRaw(Account::age){ +// from(Account::class) +// this.and(Account::age `in` (19..20)) +// limit(1) +// } + whereWith { Account::id eq 1 and (Account::userName eq "张三") } + } + println("更新后: ${Account::class.all.first()}") + } + } -- Gitee From 673eaea4ec8ad582fca8dd83cc6544c91d746d55 Mon Sep 17 00:00:00 2001 From: CloudPlayer <2909078582@qq.com> Date: Fri, 22 Mar 2024 00:35:22 +0800 Subject: [PATCH 02/11] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BB=8E=20KClass=20?= =?UTF-8?q?=E4=B8=AD=E8=8E=B7=E5=8F=96=20QueryTable=20=E7=9A=84=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E5=B1=9E=E6=80=A7=EF=BC=9B=E4=BF=AE=E6=AD=A3=E6=8A=A5?= =?UTF-8?q?=E9=94=99=E4=BF=A1=E6=81=AF=E4=B8=AD=E4=B8=80=E4=B8=AA=E5=B0=8F?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/extensions/db/DbExtensions.kt | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt index 3483ca5..ec871dd 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt @@ -21,6 +21,7 @@ import com.mybatisflex.core.mybatis.Mappers import com.mybatisflex.core.paginate.Page import com.mybatisflex.core.query.QueryColumn import com.mybatisflex.core.query.QueryCondition +import com.mybatisflex.core.query.QueryTable import com.mybatisflex.core.row.Db import com.mybatisflex.core.row.Db.selectListByQuery import com.mybatisflex.core.row.Db.selectOneByQuery @@ -68,10 +69,15 @@ val KClass.baseMapperOrNull: BaseMapper? val KClass.tableInfo: TableInfo get() = requireNotNull(tableInfoOrNull) { - "Ehe class TableInfo cannot be found through $this" + + "The class TableInfo cannot be found through $this" + " because the entity class corresponding to the generic used by this interface to inherit from BaseMapper cannot be found." } +val KClass.queryTable: QueryTable + get() { + val info = tableInfo + return QueryTable(info.schema, info.tableName) + } val KClass.tableInfoOrNull: TableInfo? get() = if (isSubclassOf(BaseMapper::class)) { -- Gitee From 04000f1b70011bf1a2f73c4a29772d43578a7b4e Mon Sep 17 00:00:00 2001 From: CloudPlayer <2909078582@qq.com> Date: Fri, 22 Mar 2024 00:39:05 +0800 Subject: [PATCH 03/11] =?UTF-8?q?MybatisFlexDsl=20=E7=8E=B0=E4=BA=A6?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=B3=A8=E8=A7=A3=E4=BA=8E=E7=B1=BB=E4=B8=8A?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/annotation/MybatisFlexDsl.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/annotation/MybatisFlexDsl.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/annotation/MybatisFlexDsl.kt index dc1d1ab..e29836c 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/annotation/MybatisFlexDsl.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/annotation/MybatisFlexDsl.kt @@ -16,6 +16,6 @@ package com.mybatisflex.kotlin.annotation @Retention(AnnotationRetention.BINARY) -@Target(AnnotationTarget.FUNCTION) +@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS) @DslMarker internal annotation class MybatisFlexDsl -- Gitee From 3dd4b58bfef94d49ff4b64ddc04d76bb3c2b5752 Mon Sep 17 00:00:00 2001 From: CloudPlayer <2909078582@qq.com> Date: Fri, 22 Mar 2024 00:44:25 +0800 Subject: [PATCH 04/11] =?UTF-8?q?QueryWrapperDevelopEntry=20=E7=B1=BB?= =?UTF-8?q?=E8=A1=A5=E5=85=A8=E5=8E=9F=20CPI=20=E4=B8=AD=E5=AF=B9=E5=BA=94?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E4=B8=94=E5=85=B6=20where=20?= =?UTF-8?q?=E5=B1=9E=E6=80=A7=E5=9C=A8set=E6=97=B6=E4=B8=8D=E5=86=8D?= =?UTF-8?q?=E8=A6=81=E6=B1=82=E9=9D=9E=E7=A9=BA=EF=BC=9BdataSource=20?= =?UTF-8?q?=E5=8F=AF=E7=A9=BA=E6=80=A7=E7=94=B1=E9=9D=9E=E7=A9=BA=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E4=B8=BA=E5=8F=AF=E7=A9=BA=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wrapper/QueryWrapperExtensions.kt | 31 +++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/wrapper/QueryWrapperExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/wrapper/QueryWrapperExtensions.kt index 948be4b..22b31cc 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/wrapper/QueryWrapperExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/wrapper/QueryWrapperExtensions.kt @@ -58,7 +58,7 @@ fun QueryWrapper.selectProperties(vararg properties: KProperty<*>): QueryWrapper fun QueryWrapper.select(entityType: KClass<*>): QueryWrapper = this.select(*entityType.defaultColumns) -val T.self +val T.self: QueryWrapperDevelopEntry get() = QueryWrapperDevelopEntry(this) inline fun QueryWrapper.and(isEffective: Boolean, predicate: () -> QueryCondition): QueryWrapper = @@ -121,9 +121,7 @@ value class QueryWrapperDevelopEntry(val wrapper: T) { var where: QueryCondition? get() = CPI.getWhereQueryCondition(wrapper) - set(value) = CPI.setWhereQueryCondition(wrapper, requireNotNull(value) { - "An error occurred while setting `where`, QueryCondition must not be empty." - }) + set(value) = CPI.setWhereQueryCondition(wrapper, value) var groupBy: List get() = CPI.getGroupByColumns(wrapper) ?: emptyList() @@ -135,6 +133,18 @@ value class QueryWrapperDevelopEntry(val wrapper: T) { val isDistinct: Boolean get() = MapperUtil.hasDistinct(selectColumns) + var joins: List + get() = CPI.getJoins(wrapper) ?: emptyList() + set(value) = CPI.setJoins(wrapper, value) + + var hint: String? + get() = CPI.getHint(wrapper) + set(value) = CPI.setHint(wrapper, value) + + var endFragments: List + get() = CPI.getEndFragments(wrapper) ?: emptyList() + set(value) = CPI.setEndFragments(wrapper, value) + var orderBys: List get() = CPI.getOrderBys(wrapper) ?: emptyList() set(value) = CPI.setOrderBys(wrapper, value) @@ -144,15 +154,18 @@ value class QueryWrapperDevelopEntry(val wrapper: T) { set(value) = CPI.setContext(wrapper, value) val childSelect: List - get() = CPI.getChildSelect(wrapper) + get() = CPI.getChildSelect(wrapper) ?: emptyList() - val valueArray: Array get() = CPI.getValueArray(wrapper) + val valueArray: Array + get() = CPI.getValueArray(wrapper) ?: emptyArray() - val joinValue: Array get() = CPI.getJoinValueArray(wrapper) + val joinValue: Array + get() = CPI.getJoinValueArray(wrapper) ?: emptyArray() - val conditionValue: Array get() = CPI.getConditionValueArray(wrapper) + val conditionValue: Array + get() = CPI.getConditionValueArray(wrapper) ?: emptyArray() - var dataSource: String + var dataSource: String? get() = CPI.getDataSource(wrapper) set(value) = CPI.setDataSource(wrapper, value) -- Gitee From ca599fd75ff783ae6cafce868b9a184d6a2bbd06 Mon Sep 17 00:00:00 2001 From: CloudPlayer <2909078582@qq.com> Date: Fri, 22 Mar 2024 00:49:54 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=20QueryColumn=20=E7=9A=84=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E5=87=BD=E6=95=B0=20`=3D`=20=E5=92=8C=20`!=3D`=20?= =?UTF-8?q?=E6=96=B0=E5=A2=9E=20JvmName=20=EF=BC=8C=E4=BB=A5=E4=BE=BF?= =?UTF-8?q?=E7=89=B9=E6=AE=8A=E6=83=85=E5=86=B5=E4=B8=8B=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/extensions/sql/SqlExtensions.kt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/sql/SqlExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/sql/SqlExtensions.kt index 33bb781..939dca2 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/sql/SqlExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/sql/SqlExtensions.kt @@ -35,8 +35,10 @@ infix fun QueryColumn.eq(value: Any?): QueryCondition = this.eq(value) infix fun QueryColumn.ne(value: Any?): QueryCondition = this.ne(value) +@JvmName("equals") infix fun QueryColumn.`=`(value: Any?): QueryCondition = this.eq(value) +@JvmName("notEquals") infix fun QueryColumn.`!=`(value: Any?): QueryCondition = this.ne(value) infix fun QueryColumn.gt(value: Any?): QueryCondition = this.gt(value) -- Gitee From 25d744a2ae042da6717e0cfe1002393a4f4f1d36 Mon Sep 17 00:00:00 2001 From: kamosama <837080904@qq.com> Date: Fri, 22 Mar 2024 10:07:38 +0800 Subject: [PATCH 06/11] =?UTF-8?q?fix:=20queryPage=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E6=AD=A7=E4=B9=89=E6=94=B9=E4=B8=BApaginateR?= =?UTF-8?q?ows?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/kotlin/extensions/db/DbExtensions.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt index 3483ca5..879eb61 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt @@ -244,7 +244,7 @@ inline fun paginate( E::class.baseMapper.paginate(page, queryScope(init = init)) } catch (e: MybatisFlexException) { E::class.tableInfo.run { - queryPage(schema, tableName, Page(page.pageNumber, page.pageSize)) { + paginateRows(schema, tableName, Page(page.pageNumber, page.pageSize)) { init() if (this.hasSelect().not()) select(*E::class.defaultColumns) }.toEntityPage() @@ -252,7 +252,7 @@ inline fun paginate( } -inline fun queryPage( +inline fun paginateRows( schema: String? = null, tableName: String? = null, page: Page? = null, -- Gitee From 69bc9e86976a6943d575b3129dea1cc308bc7d99 Mon Sep 17 00:00:00 2001 From: kamosama <837080904@qq.com> Date: Fri, 22 Mar 2024 15:21:50 +0800 Subject: [PATCH 07/11] =?UTF-8?q?doc:=20=E5=AE=8C=E5=96=84update=E7=AD=89?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E4=B8=8E=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/extensions.md | 133 ++++++++++++++++-- .../main/kotlin/extensions/db/DbExtensions.kt | 2 +- .../src/main/kotlin/scope/UpdateScope.kt | 8 +- .../src/test/kotlin/example/KotlinExample.kt | 110 +++++++++------ .../src/test/kotlin/example/entity/Account.kt | 6 +- readme.md | 4 +- readme_zh.md | 4 +- 7 files changed, 202 insertions(+), 65 deletions(-) diff --git a/docs/extensions.md b/docs/extensions.md index f4594cc..6d7cc61 100644 --- a/docs/extensions.md +++ b/docs/extensions.md @@ -1,13 +1,17 @@ # 简单查询与扩展 -## 快速获取mapper与tableInfo +> Tips: 阅读本文档时请已核心库使用文档为主,此文档为辅,扩展模块只是基于核心库的提供了符合Kotlin的便捷函数, +> 并不是另立门户,二者可以混用。 + +## 获取mapper与tableInfo - **mapper** - - 通过mapper接口类型作为泛型调用 mapper() 方法直接获取 + - 通过mapper接口类型作为泛型调用 `mapper()` 方法直接获取 + > 注意:范型M为Mapper接口类型,不是实体类型 ````kotlin val accountMapper: AccountMapper = mapper() ```` - - 通过实体型 KClass 的 baseMapper 属性直接获取 + - 通过实体型 `KClass` 的 `baseMapper` 属性直接获取 > 注意:此方式获得的实例类型是 BaseMapper ,并不是 AccountMapper 所以无法使用 AccountMapper 接口定义的方法 ````kotlin @@ -19,17 +23,19 @@ val accountTableInfo: TableInfo = Account::class.tableInfo ```` -## 快速查询(无需注册Mapper接口) +## 查询 -1. `all<实体类>()` : 查泛型对应的表的所有数据 +1. `all(): List` : 查泛型对应的表的所有数据 (无需注册Mapper接口) ```kotlin val accounts: List = all() // 或者 Account::class.all (需要注册Mapper接口) ``` -2. `filter<实体类>(vararg KProperty<*>, () -> QueryCondition)`: 按条件查泛型对应的表的数据 - > 默认查所有列,可通过 vararg KProperty<*> 指定要查的列 +2. `filter(vararg KProperty<*>, () -> QueryCondition): List`: 按条件查泛型对应的表的数据 (无需注册Mapper接口) + * 默认查所有列,可通过 `vararg KProperty<*>` 指定要查的列 + + > 除此之外还有filterOne(只查一个),filterColumn(第一个入参改为Column),filterOneColumn(前两者结合) ```kotlin // a and b and (c or d) @@ -40,16 +46,121 @@ } ``` -3. `query<实体类>(QueryScope.() -> Unit)`: 较复杂查泛型对应的表的数据 (如分组,分页,排序等) - > 可以使用queryOne快速查询一个实体对象 +3. `query(QueryScope.() -> Unit): List`: 较复杂查泛型对应的表的数据 (如分组,分页,排序等)(无需注册Mapper接口) + > 除此之外还有queryOne(只查一个), + > queryRows(强制走默认RowMapper返回值为Row), + > queryRow(前两者结合) ```kotlin val accounts: List = query { select(Account::id, Account::userName) where { - and(Account::age `in` (17..19)) - and(Account::birthday between (start to end)) + Account::age `in` (17..19) and (Account::birthday between (start to end)) }orderBy -Account::id limit(2) } + ``` +4. `paginate( + pageNumber: Number, + pageSize: Number, + totalRow: Number? = null, + init: QueryScope.() -> Unit + ): Page` : 分页查询 (无需注册Mapper接口) + > 除此之外还有paginateWith(简单按条件分页), + > paginateRows(返回值为Row) + + ```kotlin + val accounts: List = paginate(1, 10) { + select(Account::id, Account::userName) + orderBy(-Account::id) + } + ``` +5. 使用原生的baseMapper的扩展方法查询: + ```kotlin + Account::class.baseMapper.query { + select(Account::id, Account::userName) + whereWith { Account::age ge 18 } + } + ``` + +## 更新 + +1. `update(scope: UpdateScope.() -> Unit): Int`: 更新单表数据 (无需注册Mapper接口) + ```kotlin + update { + Account::id set 5 + whereWith { Account::id eq 1 and (Account::userName eq "张三") } + } + ``` + 其中set方法后可以接QueryWrapper,QueryScope类型的参数用于设置子查询 + ```kotlin + Account::age set queryScope(Account::age.column){ + from(Account::class) + and(Account::age `in` (19..20)) + limit(1) + } + ``` + 或者使用更便捷地的setRow方法: + ```kotlin + Account::age.setRow(Account::age){ + from(Account::class) + and(Account::age `in` (19..20)) + } + ``` + > setRow方法会自动select单列并limit 1,所以不需要再使用limit方法 + + 如果有需求可以写成以下方式,此时会执行两次sql + ```kotlin + Account::age set filterOne(Account::age){ + Account::age `in` (19..20) + }?.age + ``` + > 注意: + > * 更新的实体类必须是open class,例如data class会报错,因为核心库中的`UpdateWrapper`使用了动态代理,而data + class的构造函数是final,从而导致的 + > * 用set子查询时flex核心库低版本存在[BUG](https://gitee.com/mybatis-flex/mybatis-flex/issues/I96XJA) + 会导致子查询参数丢失问题,需要更新核心库到1.8.2以上(不包含)版本 +2. 使用原生的baseMapper的扩展方法更新: + ```kotlin + val account = Account( + id = 5, + // 此时会执行一次sql + age = filterOne(Account::age) { + Account::age `in` (19..20) + }?.age + ) + Account::class.baseMapper.updateByCondition(account){ + Account::age `in` (19..20) + } + ``` + +## 操作符 + +1. `and`,`or` 等条件关联的中缀方法,返回值为`QueryCondition` +2. `eq`,`ne`,`gt`,`ge`,`lt`,`le`,`in`,`between` 等条件构建符的中缀方法,返回值为`QueryCondition` +3. `allAnd`,`allOr` 多条件相同关联的构建方法,返回值为`QueryCondition` +4. 基于第三点扩展`QueryCondition`和`QueryWrapper`的`andAll`,`orAll` 方法,返回值为`QueryCondition`或`QueryWrapper` + ````kotlin + query { + andAll( + Account::id eq 1, + Account::age le 18, + Account::userName eq "张三", + ) + } + ```` +5. `inTriple`,`inPair` 构建多属性组合 IN ,返回值为`QueryCondition` + ````kotlin + filter { + (Account::id to Account::userName to Account::age).inTriple( + 1 to "张三" to 18, + 2 to "李四" to 19, + ) + } + ```` + 执行的SQL: + ```sql + SELECT * FROM `tb_account` + WHERE (`id` = 1 AND `user_name` = '张三' AND `age` = 18) + OR (`id` = 2 AND `user_name` = '李四' AND `age` = 19) ``` \ No newline at end of file diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt index 879eb61..eef7fea 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/db/DbExtensions.kt @@ -251,7 +251,6 @@ inline fun paginate( } } - inline fun paginateRows( schema: String? = null, tableName: String? = null, @@ -259,6 +258,7 @@ inline fun paginateRows( init: QueryScope.() -> Unit ): Page = Db.paginate(schema, tableName, page, queryScope(init = init)) + // update---------- inline fun update(scope: UpdateScope.() -> Unit): Int { updateScope().run { diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/scope/UpdateScope.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/scope/UpdateScope.kt index 16984b7..bfa84a2 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/scope/UpdateScope.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/scope/UpdateScope.kt @@ -5,6 +5,7 @@ import com.mybatisflex.core.query.QueryWrapperAdapter import com.mybatisflex.core.table.TableInfoFactory import com.mybatisflex.core.update.UpdateWrapper import com.mybatisflex.core.util.UpdateEntity +import com.mybatisflex.kotlin.extensions.kproperty.column import kotlin.reflect.KProperty1 class UpdateScope( @@ -22,7 +23,7 @@ class UpdateScope( } infix fun KProperty1.setRaw(queryWrapper: QueryScope.() -> Unit) { - updateWrapper.setRaw(tableInfo.getColumnByProperty(this.name), queryScope().apply(queryWrapper)) + updateWrapper.setRaw(tableInfo.getColumnByProperty(this.name), queryScope().apply(queryWrapper).limit(1)) } fun KProperty1.setRaw(column: QueryColumn, queryWrapper: QueryScope.() -> Unit) { @@ -33,10 +34,7 @@ class UpdateScope( } fun KProperty1.setRaw(property: KProperty1<*, *>, queryWrapper: QueryScope.() -> Unit) { - setRaw { - queryWrapper() - select(property) - } + setRaw(property.column, queryWrapper) } } diff --git a/mybatis-flex-kotlin-extensions/src/test/kotlin/example/KotlinExample.kt b/mybatis-flex-kotlin-extensions/src/test/kotlin/example/KotlinExample.kt index c646f30..226d20e 100644 --- a/mybatis-flex-kotlin-extensions/src/test/kotlin/example/KotlinExample.kt +++ b/mybatis-flex-kotlin-extensions/src/test/kotlin/example/KotlinExample.kt @@ -7,14 +7,12 @@ import com.mybatisflex.core.query.QueryColumn import com.mybatisflex.core.query.QueryWrapper import com.mybatisflex.kotlin.example.entity.Account import com.mybatisflex.kotlin.example.mapper.AccountMapper +import com.mybatisflex.kotlin.extensions.condition.allAnd import com.mybatisflex.kotlin.extensions.condition.and import com.mybatisflex.kotlin.extensions.condition.or import com.mybatisflex.kotlin.extensions.db.* import com.mybatisflex.kotlin.extensions.kproperty.* -import com.mybatisflex.kotlin.extensions.mapper.all -import com.mybatisflex.kotlin.extensions.mapper.remove -import com.mybatisflex.kotlin.extensions.mapper.save -import com.mybatisflex.kotlin.extensions.mapper.update +import com.mybatisflex.kotlin.extensions.mapper.* import com.mybatisflex.kotlin.extensions.model.batchDeleteById import com.mybatisflex.kotlin.extensions.model.batchInsert import com.mybatisflex.kotlin.extensions.model.batchUpdateById @@ -133,7 +131,7 @@ class KotlinExample { @Test fun testUpdate() { - // 通过条件更新 + // 通过条件查询到后更新 filterOne { Account::id eq 2 }?.apply { age = 20 }?.update { Account::userName eq it.userName and (Account::age le 18) } @@ -142,6 +140,50 @@ class KotlinExample { filterOne { Account::id eq 2 }?.also(::println) } + @Test + fun testUpdate2() { + println("更新前: ${Account::class.all.first()}") + filterOne(Account::age) { + Account::age `in` (19..20) + } + update { + Account::id set 5 +// Account::age setRaw { +// select(Account::age) +// from(Account::class) +// this.and(Account::age `in` (19..20)) +// limit(1) +// } +// 或者写成: + Account::age.setRaw(Account::age) { + from(Account::class) + and(Account::age `in` (19..20)) + } +// 或者写成: +// Account::age set queryScope(Account::age.column){ +// from(Account::class) +// and(Account::age `in` (19..20)) +// limit(1) +// } +// 或者写成 (此时会执行两次sql): +// Account::age set filterOne(Account::age){ +// Account::age `in` (19..20) +// }?.age + whereWith { Account::id eq 1 and (Account::userName eq "张三") } + } +// val account = Account( +// id = 5, +// // 此时会执行一次sql +// age = filterOne(Account::age) { +// Account::age `in` (19..20) +// }?.age +// ) +// Account::class.baseMapper.updateByCondition(account){ +// Account::age `in` (19..20) +// } + println("更新后: ${Account::class.all.first()}") + } + @Test fun testDelete() { // 通过条件删除 @@ -159,14 +201,14 @@ class KotlinExample { @Test fun testFilter() { val accounts: List = filter { - (Account::id.isNotNull) - .and { - (Account::id to Account::userName to Account::age).inTriple( - 1 to "张三" to 18, - 2 to "李四" to 19, - ) - } - .and(Account::age.`in`(17..19) or { Account::birthday between (start to end) }) + allAnd( + Account::id.isNotNull, + (Account::id to Account::userName to Account::age).inTriple( + 1 to "张三" to 18, + 2 to "李四" to 19 + ), + Account::age.`in`(17..19) + ) or { Account::birthday between (start to end) } } accounts.forEach(::println) } @@ -177,14 +219,14 @@ class KotlinExample { @Test fun testFilterOne() { val account: Account? = filterOne(Account::age) { - (Account::id.isNotNull) - .and { - (Account::id to Account::userName to Account::age).inTriple( - 1 to "张三" to 18, - 2 to "李四" to 19, - ) - } - .and(Account::age.`in`(17..19) or { Account::birthday between (start to end) }) + allAnd( + Account::id.isNotNull, + (Account::id to Account::userName to Account::age).inTriple( + 1 to "张三" to 18, + 2 to "李四" to 19 + ), + Account::age.`in`(17..19) + ) or { Account::birthday between (start to end) } } println(account) } @@ -196,9 +238,8 @@ class KotlinExample { fun testQuery() { val accounts: List = query { selectFrom(Account::id, Account::userName) - where { - and(Account::age `in` (17..19)) - and(Account::birthday between (start to end)) + whereWith { + Account::age `in` (17..19) and (Account::birthday between (start to end)) } orderBy -Account::id limit(2) } @@ -303,26 +344,5 @@ class KotlinExample { }.also { println(it) } } - @Test - fun testUpdate2() { - println("更新前: ${Account::class.all.first()}") - update { - Account::id set 5 - Account::age setRaw { - select(Account::age) - from(Account::class) - this.and(Account::age `in` (19..20)) - limit(1) - } -// or -// Account::age.setRaw(Account::age){ -// from(Account::class) -// this.and(Account::age `in` (19..20)) -// limit(1) -// } - whereWith { Account::id eq 1 and (Account::userName eq "张三") } - } - println("更新后: ${Account::class.all.first()}") - } } diff --git a/mybatis-flex-kotlin-extensions/src/test/kotlin/example/entity/Account.kt b/mybatis-flex-kotlin-extensions/src/test/kotlin/example/entity/Account.kt index 84b07a3..32cff45 100755 --- a/mybatis-flex-kotlin-extensions/src/test/kotlin/example/entity/Account.kt +++ b/mybatis-flex-kotlin-extensions/src/test/kotlin/example/entity/Account.kt @@ -21,7 +21,7 @@ import java.util.* */ @Table("tb_account") -data class Account( +open class Account( @Id var id: Int = -1, var userName: String? = null, var age: Int? = null, @@ -36,5 +36,9 @@ data class Account( } } } + + override fun toString(): String { + return "Account(id=$id, userName=$userName, age=$age, birthday=$birthday)" + } } diff --git a/readme.md b/readme.md index bf9653a..a6e92a5 100644 --- a/readme.md +++ b/readme.md @@ -157,7 +157,9 @@ data class Account( - 使用 `@Table("tb_account")` 设置实体类与表名的映射关系 - 使用 `@Id` 标识主键 -> ⚠️ 最好不要写成 data class ,否则没有无参构造某些情况下会报错; + +> ⚠️ 最好不要写成 data class ,否则没有无参构造某些情况下会报错,例如属性的顺序与数据库字段不一致会导致报错; +> > 如有需要可以安装官方 [noArg](https://kotlinlang.org/docs/no-arg-plugin.html) 插件 **第 4 步:开始使用** diff --git a/readme_zh.md b/readme_zh.md index eda0b33..fb82ab3 100644 --- a/readme_zh.md +++ b/readme_zh.md @@ -157,7 +157,9 @@ data class Account( - 使用 `@Table("tb_account")` 设置实体类与表名的映射关系 - 使用 `@Id` 标识主键 -> ⚠️ 最好不要写成 data class ,否则没有无参构造某些情况下会报错; + +> ⚠️ 最好不要写成 data class ,否则没有无参构造某些情况下会报错,例如属性的顺序与数据库字段不一致会导致报错; +> > 如有需要可以安装官方 [noArg](https://kotlinlang.org/docs/no-arg-plugin.html) 插件 **第 4 步:开始使用** -- Gitee From 387d986c31187942fe7b64e4965af9befcb4b729 Mon Sep 17 00:00:00 2001 From: CloudPlayer <2909078582@qq.com> Date: Sat, 23 Mar 2024 09:20:44 +0800 Subject: [PATCH 08/11] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E7=A9=BA=E6=9D=A1?= =?UTF-8?q?=E4=BB=B6=E4=BB=A5=E5=8F=8A=E5=85=B6=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../extensions/condition/EmptyCondition.kt | 160 ++++++++++++++++++ .../test/kotlin/test/EmptyConditionTest.kt | 75 ++++++++ 2 files changed, 235 insertions(+) create mode 100644 mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/EmptyCondition.kt create mode 100644 mybatis-flex-kotlin-extensions/src/test/kotlin/test/EmptyConditionTest.kt diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/EmptyCondition.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/EmptyCondition.kt new file mode 100644 index 0000000..37691e6 --- /dev/null +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/EmptyCondition.kt @@ -0,0 +1,160 @@ +package com.mybatisflex.kotlin.extensions.condition + +import com.mybatisflex.core.constant.SqlConnector +import com.mybatisflex.core.dialect.DialectFactory +import com.mybatisflex.core.dialect.IDialect +import com.mybatisflex.core.query.QueryColumn +import com.mybatisflex.core.query.QueryCondition +import com.mybatisflex.core.query.QueryTable +import com.mybatisflex.core.query.RawQueryCondition +import com.mybatisflex.kotlin.extensions.condition.annotation.ExperimentalEmptyCondition +import java.util.function.BooleanSupplier + +@ExperimentalEmptyCondition +//@Suppress("unused") +object EmptyCondition : QueryCondition() { + @get:JvmName("value") + val value: Nothing? + get() = null + + @get:JvmName("column") + val column: Nothing? + get() = null + + @get:JvmName("logic") + val logic: Nothing? + get() = null + + @get:JvmName("nextEffectiveCondition") + val nextEffectiveCondition: QueryCondition? + get() = super.getNextEffectiveCondition() + + @get:JvmName("prevEffectiveCondition") + val prevEffectiveCondition: QueryCondition? + get() = super.getPrevEffectiveCondition() + + override fun toString(): String { + return "com.mybatisflex.kotlin.extensions.condition.EmptyCondition" + } + + @Deprecated( + message = "Attempting to clone a singleton is illegal.", + replaceWith = ReplaceWith("this"), + level = DeprecationLevel.ERROR, + ) + override fun clone(): EmptyCondition { + return this + } + + override fun notEmpty(): Boolean { + return false + } + + @Deprecated("The property of EmptyCondition is immutable.", level = DeprecationLevel.HIDDEN) + override fun setEmpty(empty: Boolean): EmptyCondition { + return this + } + + @Deprecated("use property, instead of getter.", replaceWith = ReplaceWith("EmptyCondition.column")) + override fun getColumn(): Nothing? { + return null + } + + @Deprecated("The property of EmptyCondition is immutable.", level = DeprecationLevel.HIDDEN) + override fun setColumn(column: QueryColumn) { + // do nothing here + } + + @Deprecated("use property, instead of getter.", replaceWith = ReplaceWith("EmptyCondition.value")) + override fun getValue(): Nothing? { + return null + } + + @Deprecated("The property of EmptyCondition is immutable.", level = DeprecationLevel.HIDDEN) + override fun setValue(value: Any?) { + // do nothing here + } + + @Deprecated("use property, instead of getter.", replaceWith = ReplaceWith("EmptyCondition.logic")) + override fun getLogic(): Nothing? { + return null + } + + @Deprecated("The property of EmptyCondition is immutable.", level = DeprecationLevel.HIDDEN) + override fun setLogic(logic: String?) { + // do nothing here + } + + @Deprecated("The property of EmptyCondition is immutable.", level = DeprecationLevel.HIDDEN) + override fun `when`(effective: Boolean): EmptyCondition { + return this + } + + @Deprecated("The property of EmptyCondition is immutable.", level = DeprecationLevel.HIDDEN) + override fun `when`(fn: BooleanSupplier): EmptyCondition { + return this + } + + override fun checkEffective(): Boolean { + return false + } + + override infix fun and(sql: String): QueryCondition { + return RawQueryCondition(sql) + } + + override fun and(sql: String, vararg params: Any?): QueryCondition { + return RawQueryCondition(sql, *params) + } + + override infix fun and(nextCondition: QueryCondition): QueryCondition { + return prev?.and(nextCondition) ?: nextCondition + } + + override infix fun or(sql: String): QueryCondition { + return RawQueryCondition(sql) + } + + override fun or(sql: String, vararg params: Any?): QueryCondition { + return RawQueryCondition(sql, *params) + } + + override infix fun or(nextCondition: QueryCondition): QueryCondition { + return prev?.or(nextCondition) ?: nextCondition + } + + override fun connect(nextCondition: QueryCondition, connector: SqlConnector) { + if (nextCondition is EmptyCondition) { + return + } + super.connect(nextCondition, connector) + } + + override fun toSql(queryTables: List, dialect: IDialect): String { + return super.toSql(queryTables, dialect) + } + + fun toSql(): String = toSql(emptyList(), DialectFactory.getDialect()) + + @Deprecated( + "use property, instead of getter.", + replaceWith = ReplaceWith("EmptyCondition.prevEffectiveCondition"), + level = DeprecationLevel.HIDDEN + ) + override fun getPrevEffectiveCondition(): QueryCondition? { + return super.getPrevEffectiveCondition() + } + + @Deprecated( + "use property, instead of getter.", + replaceWith = ReplaceWith("EmptyCondition.nextEffectiveCondition"), + level = DeprecationLevel.HIDDEN + ) + override fun getNextEffectiveCondition(): QueryCondition? { + return super.getNextEffectiveCondition() + } + + override fun appendQuestionMark(sqlBuilder: StringBuilder) { + super.appendQuestionMark(sqlBuilder) + } +} \ No newline at end of file diff --git a/mybatis-flex-kotlin-extensions/src/test/kotlin/test/EmptyConditionTest.kt b/mybatis-flex-kotlin-extensions/src/test/kotlin/test/EmptyConditionTest.kt new file mode 100644 index 0000000..57b3529 --- /dev/null +++ b/mybatis-flex-kotlin-extensions/src/test/kotlin/test/EmptyConditionTest.kt @@ -0,0 +1,75 @@ +package com.mybatisflex.kotlin.test + +import com.mybatisflex.core.query.CPI +import com.mybatisflex.core.query.QueryCondition +import com.mybatisflex.core.query.QueryWrapper +import com.mybatisflex.kotlin.example.entity.Account +import com.mybatisflex.kotlin.extensions.condition.EmptyCondition +import com.mybatisflex.kotlin.extensions.condition.and +import com.mybatisflex.kotlin.extensions.condition.annotation.ExperimentalEmptyCondition +import com.mybatisflex.kotlin.extensions.condition.or +import com.mybatisflex.kotlin.extensions.kproperty.eq +import org.junit.jupiter.api.Test + +@OptIn(ExperimentalEmptyCondition::class) +class EmptyConditionTest { + + @Test + fun test() { + val cond1 = Account::id eq 1 + val cond2 = Account::id eq 114514 + val condition = + EmptyCondition and cond2 and EmptyCondition or cond1 and EmptyCondition + println(condition.contentToString()) + println(cond1.prev() to cond1.next()) + println(cond2.prev() to cond2.next()) + println(condition.prev() to condition.next()) + } + + private val flexEmptyCondition: QueryCondition + get() = QueryCondition.createEmpty() + + private fun QueryCondition.contentToString(): String { + return QueryWrapper().where(this).toSQL() + } + + private fun QueryCondition.prev(): QueryCondition? = CPI.getPrevEffectiveCondition(this) + + private fun QueryCondition.next(): QueryCondition? = CPI.getNextCondition(this) + + @Test + fun test2() { + val condition = + flexEmptyCondition and (Account::id eq 114514) and flexEmptyCondition or (Account::id eq 1) + println(condition.contentToString()) + } + + @Test + fun test3() { + val condition = + EmptyCondition and EmptyCondition and (Account::id eq 114514) and EmptyCondition and EmptyCondition + println(condition.contentToString()) + } + + @Test + fun test4() { + val condition = EmptyCondition and EmptyCondition and EmptyCondition and (Account::id eq 114514) + println(condition.contentToString()) + } + + @Test + fun test5() { + var condition = EmptyCondition and flexEmptyCondition and EmptyCondition + println(condition.contentToString()) + condition = flexEmptyCondition and EmptyCondition and flexEmptyCondition + println(condition.contentToString()) + } + + @Test + fun test6() { + Account::id eq 114514 and EmptyCondition + println(EmptyCondition.prev()) + Account::id eq 1919810 and EmptyCondition + println(EmptyCondition.prev()) + } +} \ No newline at end of file -- Gitee From 6c50685ff1838ea44d08fdd2a0b58247dcd85750 Mon Sep 17 00:00:00 2001 From: CloudPlayer <2909078582@qq.com> Date: Sat, 23 Mar 2024 23:22:04 +0800 Subject: [PATCH 09/11] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20toRow=20=E5=87=BD?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E7=94=A8=E4=BA=8E=E5=B0=86=E5=AE=9E=E4=BD=93?= =?UTF-8?q?=E7=B1=BB=E8=BD=AC=E6=8D=A2=E4=B8=BARow=EF=BC=9B=E7=8E=B0?= =?UTF-8?q?=E5=9C=A8=20KProperty=20=E5=92=8C=20QueryColumn=20=E4=BD=BF?= =?UTF-8?q?=E7=94=A8=20eq=20=E5=92=8C=20ne=20=E4=B8=8E=20null=20=E7=9B=B4?= =?UTF-8?q?=E6=8E=A5=E8=BF=9B=E8=A1=8C=E6=AF=94=E8=BE=83=E6=97=B6=EF=BC=8C?= =?UTF-8?q?=E5=B0=86=E4=BC=9A=E6=8F=90=E9=86=92=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../annotation/ExperimentalEmptyCondition.kt | 5 +++++ .../extensions/kproperty/KPropertyExtensions.kt | 6 ++++++ .../kotlin/extensions/model/ModelExtensions.kt | 17 +++++++++++++++++ .../main/kotlin/extensions/sql/SqlExtensions.kt | 15 +++++++-------- 4 files changed, 35 insertions(+), 8 deletions(-) create mode 100644 mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/annotation/ExperimentalEmptyCondition.kt diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/annotation/ExperimentalEmptyCondition.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/annotation/ExperimentalEmptyCondition.kt new file mode 100644 index 0000000..4882f86 --- /dev/null +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/annotation/ExperimentalEmptyCondition.kt @@ -0,0 +1,5 @@ +package com.mybatisflex.kotlin.extensions.condition.annotation + +@RequiresOptIn("EmptyCondition 单例仍在测试,请避免将其使用至生产项目中。" + + "如若遇到问题,请改用 QueryCondition.createEmpty() 方法。") +annotation class ExperimentalEmptyCondition diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/kproperty/KPropertyExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/kproperty/KPropertyExtensions.kt index 69242f7..9d63555 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/kproperty/KPropertyExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/kproperty/KPropertyExtensions.kt @@ -192,12 +192,18 @@ infix fun KProperty.`in`(other: QueryWrapper): QueryCondition = column.` infix fun KProperty.eq(other: T): QueryCondition = column.eq(other) +@Deprecated("使用 eq 和 null 进行比较可能是个错误。", ReplaceWith("this.isNull")) +infix fun KProperty.eq(other: Nothing?): QueryCondition = column.eq(other) + infix fun KProperty.eq(other: QueryColumn): QueryCondition = column.eq(other) infix fun KProperty.eq(other: KProperty): QueryCondition = column.eq(other.column) infix fun KProperty.ne(other: T): QueryCondition = column.ne(other) +@Deprecated("使用 ne 和 null 进行比较可能是个错误。", ReplaceWith("this.isNotNull")) +infix fun KProperty.ne(other: Nothing?): QueryCondition = column.ne(other) + infix fun KProperty.ne(other: QueryColumn): QueryCondition = column.ne(other) infix fun KProperty.ne(other: KProperty): QueryCondition = column.ne(other.column) diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/model/ModelExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/model/ModelExtensions.kt index 2692c77..f1d0cf5 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/model/ModelExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/model/ModelExtensions.kt @@ -19,6 +19,9 @@ import com.mybatisflex.core.activerecord.MapperModel import com.mybatisflex.core.paginate.Page import com.mybatisflex.core.row.Row import com.mybatisflex.core.row.RowUtil +import com.mybatisflex.core.table.EntityMetaObject +import com.mybatisflex.core.table.TableInfo +import com.mybatisflex.core.table.TableInfoFactory import com.mybatisflex.core.util.SqlUtil import com.mybatisflex.kotlin.extensions.db.* import java.io.Serializable @@ -28,8 +31,22 @@ import java.io.Serializable * @author KAMOsama */ +@Suppress("EXTENSION_SHADOWED_BY_MEMBER") inline fun Row.toEntity(): T = RowUtil.toEntity(this, T::class.java) +fun T.toRow(): Row { + val tableInfo: TableInfo = TableInfoFactory.ofEntityClass(javaClass) + val metaObject = EntityMetaObject.forObject(this, tableInfo.reflectorFactory) + val row = Row() + tableInfo.primaryKeyList.forEach { + row[it.column] = metaObject.getValue(it.property) + } + tableInfo.columnInfoList.forEach { + row[it.column] = metaObject.getValue(it.property) + } + return row +} + inline fun Collection.toEntities(): MutableList = RowUtil.toEntityList(this.toMutableList(), E::class.java) diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/sql/SqlExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/sql/SqlExtensions.kt index 939dca2..7665a4e 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/sql/SqlExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/sql/SqlExtensions.kt @@ -33,8 +33,14 @@ infix fun QueryColumn.like(value: String): QueryCondition = this.like(value) infix fun QueryColumn.eq(value: Any?): QueryCondition = this.eq(value) +@Deprecated("使用 eq 和 null 进行比较可能是个错误。", ReplaceWith("this.isNull")) +infix fun QueryColumn.eq(value: Nothing?): QueryCondition = this.eq(value) + infix fun QueryColumn.ne(value: Any?): QueryCondition = this.ne(value) +@Deprecated("使用 ne 和 null 进行比较可能是个错误。", ReplaceWith("this.isNull")) +infix fun QueryColumn.ne(value: Nothing?): QueryCondition = this.ne(value) + @JvmName("equals") infix fun QueryColumn.`=`(value: Any?): QueryCondition = this.eq(value) @@ -141,11 +147,4 @@ fun QueryColumn.toOrd(order: Order = Order.ASC): QueryOrderBy = when (order) { Order.DESC -> desc() } -operator fun QueryCondition.not(): QueryCondition = - if (this is OperatorQueryCondition) { // 如果是OperatorQueryCondition,则需要判断是否已经反转 - val field = OperatorQueryCondition::class.java.getDeclaredField("operator") - field.isAccessible = true - val operator = field[this] as? String? - if (operator !== null && "NOT" in operator.uppercase()) childCondition - else OperatorQueryCondition("NOT ", this) - } else OperatorQueryCondition("NOT ", this) \ No newline at end of file +operator fun QueryCondition.not(): QueryCondition = QueryMethods.not(this) \ No newline at end of file -- Gitee From 594f1bd34c15ce37201a6f3e3f2008d8f5bb270a Mon Sep 17 00:00:00 2001 From: kamosama <837080904@qq.com> Date: Sun, 24 Mar 2024 11:50:57 +0800 Subject: [PATCH 10/11] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E7=A9=BA?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E5=8D=95=E4=BE=8B=EF=BC=8C=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../extensions/condition/EmptyCondition.kt | 160 ------------------ .../condition/QueryConditionExtensions.kt | 3 + .../test/kotlin/test/EmptyConditionTest.kt | 75 -------- 3 files changed, 3 insertions(+), 235 deletions(-) delete mode 100644 mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/EmptyCondition.kt delete mode 100644 mybatis-flex-kotlin-extensions/src/test/kotlin/test/EmptyConditionTest.kt diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/EmptyCondition.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/EmptyCondition.kt deleted file mode 100644 index 37691e6..0000000 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/EmptyCondition.kt +++ /dev/null @@ -1,160 +0,0 @@ -package com.mybatisflex.kotlin.extensions.condition - -import com.mybatisflex.core.constant.SqlConnector -import com.mybatisflex.core.dialect.DialectFactory -import com.mybatisflex.core.dialect.IDialect -import com.mybatisflex.core.query.QueryColumn -import com.mybatisflex.core.query.QueryCondition -import com.mybatisflex.core.query.QueryTable -import com.mybatisflex.core.query.RawQueryCondition -import com.mybatisflex.kotlin.extensions.condition.annotation.ExperimentalEmptyCondition -import java.util.function.BooleanSupplier - -@ExperimentalEmptyCondition -//@Suppress("unused") -object EmptyCondition : QueryCondition() { - @get:JvmName("value") - val value: Nothing? - get() = null - - @get:JvmName("column") - val column: Nothing? - get() = null - - @get:JvmName("logic") - val logic: Nothing? - get() = null - - @get:JvmName("nextEffectiveCondition") - val nextEffectiveCondition: QueryCondition? - get() = super.getNextEffectiveCondition() - - @get:JvmName("prevEffectiveCondition") - val prevEffectiveCondition: QueryCondition? - get() = super.getPrevEffectiveCondition() - - override fun toString(): String { - return "com.mybatisflex.kotlin.extensions.condition.EmptyCondition" - } - - @Deprecated( - message = "Attempting to clone a singleton is illegal.", - replaceWith = ReplaceWith("this"), - level = DeprecationLevel.ERROR, - ) - override fun clone(): EmptyCondition { - return this - } - - override fun notEmpty(): Boolean { - return false - } - - @Deprecated("The property of EmptyCondition is immutable.", level = DeprecationLevel.HIDDEN) - override fun setEmpty(empty: Boolean): EmptyCondition { - return this - } - - @Deprecated("use property, instead of getter.", replaceWith = ReplaceWith("EmptyCondition.column")) - override fun getColumn(): Nothing? { - return null - } - - @Deprecated("The property of EmptyCondition is immutable.", level = DeprecationLevel.HIDDEN) - override fun setColumn(column: QueryColumn) { - // do nothing here - } - - @Deprecated("use property, instead of getter.", replaceWith = ReplaceWith("EmptyCondition.value")) - override fun getValue(): Nothing? { - return null - } - - @Deprecated("The property of EmptyCondition is immutable.", level = DeprecationLevel.HIDDEN) - override fun setValue(value: Any?) { - // do nothing here - } - - @Deprecated("use property, instead of getter.", replaceWith = ReplaceWith("EmptyCondition.logic")) - override fun getLogic(): Nothing? { - return null - } - - @Deprecated("The property of EmptyCondition is immutable.", level = DeprecationLevel.HIDDEN) - override fun setLogic(logic: String?) { - // do nothing here - } - - @Deprecated("The property of EmptyCondition is immutable.", level = DeprecationLevel.HIDDEN) - override fun `when`(effective: Boolean): EmptyCondition { - return this - } - - @Deprecated("The property of EmptyCondition is immutable.", level = DeprecationLevel.HIDDEN) - override fun `when`(fn: BooleanSupplier): EmptyCondition { - return this - } - - override fun checkEffective(): Boolean { - return false - } - - override infix fun and(sql: String): QueryCondition { - return RawQueryCondition(sql) - } - - override fun and(sql: String, vararg params: Any?): QueryCondition { - return RawQueryCondition(sql, *params) - } - - override infix fun and(nextCondition: QueryCondition): QueryCondition { - return prev?.and(nextCondition) ?: nextCondition - } - - override infix fun or(sql: String): QueryCondition { - return RawQueryCondition(sql) - } - - override fun or(sql: String, vararg params: Any?): QueryCondition { - return RawQueryCondition(sql, *params) - } - - override infix fun or(nextCondition: QueryCondition): QueryCondition { - return prev?.or(nextCondition) ?: nextCondition - } - - override fun connect(nextCondition: QueryCondition, connector: SqlConnector) { - if (nextCondition is EmptyCondition) { - return - } - super.connect(nextCondition, connector) - } - - override fun toSql(queryTables: List, dialect: IDialect): String { - return super.toSql(queryTables, dialect) - } - - fun toSql(): String = toSql(emptyList(), DialectFactory.getDialect()) - - @Deprecated( - "use property, instead of getter.", - replaceWith = ReplaceWith("EmptyCondition.prevEffectiveCondition"), - level = DeprecationLevel.HIDDEN - ) - override fun getPrevEffectiveCondition(): QueryCondition? { - return super.getPrevEffectiveCondition() - } - - @Deprecated( - "use property, instead of getter.", - replaceWith = ReplaceWith("EmptyCondition.nextEffectiveCondition"), - level = DeprecationLevel.HIDDEN - ) - override fun getNextEffectiveCondition(): QueryCondition? { - return super.getNextEffectiveCondition() - } - - override fun appendQuestionMark(sqlBuilder: StringBuilder) { - super.appendQuestionMark(sqlBuilder) - } -} \ No newline at end of file diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/QueryConditionExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/QueryConditionExtensions.kt index 06fdf2a..50911f3 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/QueryConditionExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/QueryConditionExtensions.kt @@ -13,6 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +@file:Suppress("MemberVisibilityCanBePrivate", "unused") package com.mybatisflex.kotlin.extensions.condition import com.mybatisflex.core.query.QueryCondition @@ -47,3 +48,5 @@ fun QueryCondition.orAll(vararg conditions: QueryCondition): QueryCondition = th fun allAnd(vararg conditions: QueryCondition): QueryCondition = conditions.reduce(QueryCondition::and) fun allOr(vararg conditions: QueryCondition): QueryCondition = conditions.reduce(QueryCondition::or) + +fun emptyCondition(): QueryCondition = QueryCondition.createEmpty() diff --git a/mybatis-flex-kotlin-extensions/src/test/kotlin/test/EmptyConditionTest.kt b/mybatis-flex-kotlin-extensions/src/test/kotlin/test/EmptyConditionTest.kt deleted file mode 100644 index 57b3529..0000000 --- a/mybatis-flex-kotlin-extensions/src/test/kotlin/test/EmptyConditionTest.kt +++ /dev/null @@ -1,75 +0,0 @@ -package com.mybatisflex.kotlin.test - -import com.mybatisflex.core.query.CPI -import com.mybatisflex.core.query.QueryCondition -import com.mybatisflex.core.query.QueryWrapper -import com.mybatisflex.kotlin.example.entity.Account -import com.mybatisflex.kotlin.extensions.condition.EmptyCondition -import com.mybatisflex.kotlin.extensions.condition.and -import com.mybatisflex.kotlin.extensions.condition.annotation.ExperimentalEmptyCondition -import com.mybatisflex.kotlin.extensions.condition.or -import com.mybatisflex.kotlin.extensions.kproperty.eq -import org.junit.jupiter.api.Test - -@OptIn(ExperimentalEmptyCondition::class) -class EmptyConditionTest { - - @Test - fun test() { - val cond1 = Account::id eq 1 - val cond2 = Account::id eq 114514 - val condition = - EmptyCondition and cond2 and EmptyCondition or cond1 and EmptyCondition - println(condition.contentToString()) - println(cond1.prev() to cond1.next()) - println(cond2.prev() to cond2.next()) - println(condition.prev() to condition.next()) - } - - private val flexEmptyCondition: QueryCondition - get() = QueryCondition.createEmpty() - - private fun QueryCondition.contentToString(): String { - return QueryWrapper().where(this).toSQL() - } - - private fun QueryCondition.prev(): QueryCondition? = CPI.getPrevEffectiveCondition(this) - - private fun QueryCondition.next(): QueryCondition? = CPI.getNextCondition(this) - - @Test - fun test2() { - val condition = - flexEmptyCondition and (Account::id eq 114514) and flexEmptyCondition or (Account::id eq 1) - println(condition.contentToString()) - } - - @Test - fun test3() { - val condition = - EmptyCondition and EmptyCondition and (Account::id eq 114514) and EmptyCondition and EmptyCondition - println(condition.contentToString()) - } - - @Test - fun test4() { - val condition = EmptyCondition and EmptyCondition and EmptyCondition and (Account::id eq 114514) - println(condition.contentToString()) - } - - @Test - fun test5() { - var condition = EmptyCondition and flexEmptyCondition and EmptyCondition - println(condition.contentToString()) - condition = flexEmptyCondition and EmptyCondition and flexEmptyCondition - println(condition.contentToString()) - } - - @Test - fun test6() { - Account::id eq 114514 and EmptyCondition - println(EmptyCondition.prev()) - Account::id eq 1919810 and EmptyCondition - println(EmptyCondition.prev()) - } -} \ No newline at end of file -- Gitee From 912a4312917c1fde9e20f811e4e9137b3fdc3027 Mon Sep 17 00:00:00 2001 From: kamosama <837080904@qq.com> Date: Sun, 24 Mar 2024 11:51:18 +0800 Subject: [PATCH 11/11] =?UTF-8?q?refactor:=20=E7=A7=BB=E9=99=A4=E7=A9=BA?= =?UTF-8?q?=E6=9D=A1=E4=BB=B6=E5=8D=95=E4=BE=8B=EF=BC=8C=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../kotlin/extensions/condition/QueryConditionExtensions.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/QueryConditionExtensions.kt b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/QueryConditionExtensions.kt index 50911f3..c7fc65c 100644 --- a/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/QueryConditionExtensions.kt +++ b/mybatis-flex-kotlin-extensions/src/main/kotlin/extensions/condition/QueryConditionExtensions.kt @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -@file:Suppress("MemberVisibilityCanBePrivate", "unused") +@file:Suppress("unused") package com.mybatisflex.kotlin.extensions.condition import com.mybatisflex.core.query.QueryCondition -- Gitee