package gaf3.core.services.bind.dao

import gaf3.core.services.bind.entity.GafBindItem
import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Modifying
import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.query.Param

interface GafBindItemDAO : JpaRepository<GafBindItem, String> {
    @Query(value = "select case when count(p) > 0 then true else false end from GafBindItem p where p.type = ?1 and p.source = ?2 and p.target = ?3")
    fun exists(type: String, source: String, target: String): Boolean

    @Modifying
    @Query(value = "delete from GafBindItem p where p.type = :type and (p.source = :source or :source is null) and (p.source = :target or :target is null) ")
    fun delete(@Param("type") type: String, @Param("source") source: String? = null, @Param("target") target: String? = null): Int

    fun findByTypeAndSource(type: String, source: String): List<GafBindItem>
}
