extensions.mapper.MapperExtensions.kt Maven / Gradle / Ivy
/*
* Copyright (c) 2023-Present, Mybatis-Flex-Kotlin ([email protected]).
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("unused")
package com.mybatisflex.kotlin.extensions.mapper
import com.mybatisflex.core.BaseMapper
import com.mybatisflex.core.activerecord.MapperModel
import com.mybatisflex.core.field.FieldQueryBuilder
import com.mybatisflex.core.query.QueryCondition
import com.mybatisflex.core.util.ClassUtil
import com.mybatisflex.kotlin.extensions.db.baseMapper
import com.mybatisflex.kotlin.scope.QueryScope
import com.mybatisflex.kotlin.scope.queryScope
import java.util.function.Consumer
import kotlin.reflect.KClass
/*
* 映射器操作扩展
* @author KAMOsama
*/
fun BaseMapper.query(init: QueryScope.() -> Unit): List =
queryScope(init = init).let(this::selectListByQuery)
fun BaseMapper.filter(init: () -> QueryCondition): List =
init().let(this::selectListByCondition)
fun BaseMapper.updateByQuery(entity: T, init: QueryScope.() -> Unit): Int =
this.updateByQuery(entity, queryScope(init = init))
fun BaseMapper.updateByCondition(entity: T, init: () -> QueryCondition): Int =
this.updateByCondition(entity, init())
fun BaseMapper.deleteByQuery(init: QueryScope.() -> Unit): Int =
queryScope(init = init).let(this::deleteByQuery)
fun BaseMapper.deleteByCondition(init: () -> QueryCondition): Int =
init().let(this::deleteByCondition)
inline fun BaseMapper.selectListByQueryAs(init: QueryScope.() -> Unit): List =
queryScope(init = init).let { this.selectListByQueryAs(it, R::class.java) }
inline fun BaseMapper.selectListByQueryAs(
vararg consumers: Consumer>,
init: QueryScope.() -> Unit
): List = queryScope(init = init).let { this.selectListByQueryAs(it, R::class.java, *consumers) }
inline fun BaseMapper.selectObjectListByQueryAs(
init: QueryScope.() -> Unit
): List = queryScope(init = init).let { this.selectObjectListByQueryAs(it, R::class.java) }
inline fun BaseMapper.selectObjectByQueryAs(
init: QueryScope.() -> Unit
): R = queryScope(init = init).let { this.selectObjectByQueryAs(it, R::class.java) }
inline fun BaseMapper.selectListWithRelationsByQueryAs(
init: QueryScope.() -> Unit
): List = queryScope(init = init).let { this.selectListWithRelationsByQueryAs(it, R::class.java) }
inline fun BaseMapper.selectListWithRelationsByQueryAs(
vararg consumers: Consumer>,
init: QueryScope.() -> Unit
): List = queryScope(init = init).let { this.selectListWithRelationsByQueryAs(it, R::class.java, *consumers) }
// all-----------
val KClass.all: List
get() = baseMapper.selectAll()
// save-----------
inline fun > save(build: E.() -> Unit): Boolean =
ClassUtil.newInstance(E::class.java).apply(build).run { save() }
// update-----------
inline fun > E.update(conditionBlock: (E) -> QueryCondition): Int =
baseMapper().updateByCondition(this, conditionBlock(this))
// delete-----------
inline fun > E.remove(conditionBlock: (E) -> QueryCondition): Int =
baseMapper().deleteByCondition(conditionBlock(this))