Newer
Older
bremer / src / main / kotlin / dao / ArtistDao.kt
/*
 * Copyright (c) 2023. yo-saito. All Rights Reserved.
 */

package net.piedpiper.bremer.dao

import net.piedpiper.bremer.entity.AlbumEntity
import net.piedpiper.bremer.entity.ArtistEntity
import net.piedpiper.bremer.utils.DaoUtils
import net.piedpiper.bremer.utils.SelectSQLBuilder
import net.piedpiper.bremer.utils.WHERE_LIKES
import org.apache.ibatis.annotations.*
import org.springframework.stereotype.Repository

@Repository("bremer.dao.ArtistDao")
@Mapper
interface ArtistDao {

    @ResultMap("net.piedpiper.bremer.ArtistEntity")
    @SelectProvider(type = Sql::class, method = "findAllByNamesLikeLimit")
    fun findAllByNamesLikeLimit(
        @Param("namesLike") namesLike: List<String>,
        @Param("limit") limit: Int
    ): List<ArtistEntity>

    @ResultMap("net.piedpiper.bremer.ArtistEntity")
    @Select("SELECT * FROM artist WHERE id = #{id} FOR UPDATE")
    fun findOneByIdWithLock(@Param("id") id: Long) : ArtistEntity?

    @UpdateProvider(type = Sql::class, method = "updateOne")
    fun updateOne(@Param("entity") entity: ArtistEntity)

    class Sql {
        companion object {
            @JvmStatic
            fun updateOne(@Param("entity") entity: ArtistEntity): String =
                DaoUtils.updateOne(entity)

            @JvmStatic
            fun findAllByNamesLikeLimit(
                @Param("namesLike") namesLike: List<String>,
            ): String = SelectSQLBuilder(ArtistEntity::class)
                .toSql()
                .WHERE_LIKES("name", "namesLike", namesLike.size)
                .LIMIT("#{limit}")
                .toString()
        }
    }
}