All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.babyfish.jimmer.spring.repo.KotlinRepository.kt Maven / Gradle / Ivy

There is a newer version: 0.9.19
Show newest version
package org.babyfish.jimmer.spring.repo

import org.babyfish.jimmer.Input
import org.babyfish.jimmer.Page
import org.babyfish.jimmer.Slice
import org.babyfish.jimmer.View
import org.babyfish.jimmer.sql.ast.mutation.AssociatedSaveMode
import org.babyfish.jimmer.sql.ast.mutation.DeleteMode
import org.babyfish.jimmer.sql.ast.mutation.SaveMode
import org.babyfish.jimmer.sql.fetcher.Fetcher
import org.babyfish.jimmer.sql.kt.ast.mutation.KBatchSaveResult
import org.babyfish.jimmer.sql.kt.ast.mutation.KSaveCommandDsl
import org.babyfish.jimmer.sql.kt.ast.mutation.KSaveCommandPartialDsl
import org.babyfish.jimmer.sql.kt.ast.mutation.KSimpleSaveResult
import org.babyfish.jimmer.sql.kt.ast.query.SortDsl
import kotlin.reflect.KClass

/**
 * In earlier versions of Jimmer, type [KotlinRepository]
 * was used to support spring data style repository support.
 *
 * However, based on user feedback, this interface was rarely used. The root causes are:
 * - Unlike JPA and MyBatis, which have lifecycle management objects like EntityManager/Session,
 * Jimmer itself is already designed with a stateless API.
 * Therefore, the stateless abstraction of spring dData style repository is meaningless for Jimmer.
 * - Jimmer itself emphasizes type safety and strives to detect problems at compile-time.
 * spring data's approach based on conventional method names and {@code @Query} annotations
 * would lead to problems only being found at runtime (How Intellij helps certain solutions
 * cheat is not discussed here), which goes against Jimmer's design philosophy.
 *
 * Therefore, developer can simply write a class and annotate it with
 * [org.springframework.data.repository.Repository]. At this point, users can choose to implement this interface or extends class
 * [org.babyfish.jimmer.spring.repo.support.AbstractKotlinRepository]. Note, that this is optional, not mandatory.
 */
interface KotlinRepository {

    fun findById(id: ID, fetcher: Fetcher? = null): E?

    fun > findById(id: ID, viewType: KClass): V?

    fun findByIds(ids: Collection): List {
        return findByIds(ids, null as Fetcher?)
    }

    fun findByIds(ids: Collection, fetcher: Fetcher?): List

    fun > findByIds(ids: Collection, viewType: KClass): List

    fun findMapByIds(ids: Collection): Map {
        return findMapByIds(ids, null as Fetcher?)
    }

    fun findMapByIds(ids: Collection, fetcher: Fetcher?): Map

    fun > findMapByIds(ids: Collection, viewType: KClass): Map

    fun findAll(block: (SortDsl.() -> Unit)? = null): List {
        return findAll(null as Fetcher?, block)
    }

    fun findAll(fetcher: Fetcher?, block: (SortDsl.() -> Unit)? = null): List

    fun > findAll(viewType: KClass, block: (SortDsl.() -> Unit)? = null): List

    fun findPage(pageParam: PageParam, block: (SortDsl.() -> Unit)? = null): Page {
        return findPage(pageParam, null as Fetcher?, block)
    }

    fun findPage(pageParam: PageParam, fetcher: Fetcher?, block: (SortDsl.() -> Unit)? = null): Page

    fun > findPage(
        pageParam: PageParam,
        viewType: KClass,
        block: (SortDsl.() -> Unit)? = null
    ): Page

    fun findSlice(limit: Int, offset: Int,block: (SortDsl.() -> Unit)? = null): Slice =
        findSlice(limit, offset, null as Fetcher?, block)

    fun findSlice(
        limit: Int,
        offset: Int,
        fetcher: Fetcher?,
        block: (SortDsl.() -> Unit)? = null
    ): Slice

    fun > findSlice(
        limit: Int,
        offset: Int,
        viewType: KClass,
        block: (SortDsl.() -> Unit)? = null
    ): Slice

    fun save(entity: E, block: (KSaveCommandDsl.() -> Unit)? = null): KSimpleSaveResult

    fun saveEntities(
        entities: Collection,
        block: (KSaveCommandDsl.() -> Unit)? = null
    ): KBatchSaveResult

    fun save(
        input: Input,
        block: (KSaveCommandDsl.() -> Unit)? = null
    ): KSimpleSaveResult

    fun saveInputs(
        inputs: Collection>,
        block: (KSaveCommandDsl.() -> Unit)? = null
    ): KBatchSaveResult

    fun insert(
        entity: E,
        associatedMode: AssociatedSaveMode = AssociatedSaveMode.APPEND,
        block: (KSaveCommandPartialDsl.() -> Unit)? = null
    ): KSimpleSaveResult =
        save(entity) {
            setMode(SaveMode.INSERT_ONLY)
            setAssociatedModeAll(associatedMode)
            if (block !== null) {
                block(this)
            }
        }

    fun insertIfAbsent(
        entity: E,
        associatedMode: AssociatedSaveMode = AssociatedSaveMode.APPEND_IF_ABSENT,
        block: (KSaveCommandPartialDsl.() -> Unit)? = null
    ): KSimpleSaveResult =
        save(entity) {
            setMode(SaveMode.INSERT_IF_ABSENT)
            setAssociatedModeAll(associatedMode)
            if (block !== null) {
                block(this)
            }
        }

    fun update(
        entity: E,
        associatedMode: AssociatedSaveMode = AssociatedSaveMode.UPDATE,
        block: (KSaveCommandPartialDsl.() -> Unit)? = null
    ): KSimpleSaveResult =
        save(entity) {
            setMode(SaveMode.UPDATE_ONLY)
            setAssociatedModeAll(associatedMode)
            if (block !== null) {
                block(this)
            }
        }

    fun merge(
        entity: E,
        associatedMode: AssociatedSaveMode = AssociatedSaveMode.MERGE,
        block: (KSaveCommandPartialDsl.() -> Unit)? = null
    ): KSimpleSaveResult =
        save(entity) {
            setMode(SaveMode.UPSERT)
            setAssociatedModeAll(associatedMode)
            if (block !== null) {
                block(this)
            }
        }

    fun insert(
        input: Input,
        associatedMode: AssociatedSaveMode = AssociatedSaveMode.APPEND,
        block: (KSaveCommandPartialDsl.() -> Unit)? = null
    ): KSimpleSaveResult =
        insert(input.toEntity(), associatedMode, block)

    fun insertIfAbsent(
        input: Input,
        associatedMode: AssociatedSaveMode = AssociatedSaveMode.APPEND_IF_ABSENT,
        block: (KSaveCommandPartialDsl.() -> Unit)? = null
    ): KSimpleSaveResult =
        insertIfAbsent(input.toEntity(), associatedMode, block)

    fun update(
        input: Input,
        associatedMode: AssociatedSaveMode = AssociatedSaveMode.UPDATE,
        block: (KSaveCommandPartialDsl.() -> Unit)? = null
    ): KSimpleSaveResult =
        update(input.toEntity(), associatedMode, block)

    fun merge(
        input: Input,
        associatedMode: AssociatedSaveMode = AssociatedSaveMode.MERGE,
        block: (KSaveCommandPartialDsl.() -> Unit)? = null
    ): KSimpleSaveResult =
        merge(input.toEntity(), associatedMode, block)

    fun deleteById(id: ID, deleteMode: DeleteMode = DeleteMode.AUTO): Int

    fun deleteByIds(ids: Collection, deleteMode: DeleteMode = DeleteMode.AUTO): Int
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy