Newer
Older
bremer / src / main / kotlin / dao / AlbumDao.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.AudioEntity
import net.piedpiper.bremer.entity.AudioPlayHistoryEntity
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.apache.ibatis.jdbc.SelectBuilder
import org.springframework.stereotype.Repository

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

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

    @ResultMap("net.piedpiper.bremer.AlbumEntity")
    @Select(
        """<script>
    SELECT * FROM album WHERE artist_id IN
    <foreach item="artistId" collection="artistIds" open="(" separator="," close=")">
    #{artistId}
    </foreach>
    LIMIT #{limit}
    </script>"""
    )
    fun findAllByArtistIdInLimit(
        @Param("artistIds") artistIds: List<Long>,
        @Param("limit") limit: Int
    ): List<AlbumEntity>

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

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

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

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