Last active
July 28, 2022 02:59
-
-
Save stella6767/698a3a2e0be00cfedb990559940181af to your computer and use it in GitHub Desktop.
kotiln-jdsl 동적쿼리 예시
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val fetch = queryFactory.listQuery<Song> { | |
select(entity(Song::class)) | |
from(entity(Song::class)) | |
fetch(Song::album, JoinType.LEFT) | |
offset(pageable.offset.toInt()) | |
limit(pageable.pageSize) | |
where( | |
isSongSearchable(searchCondition, Song::deletedAt) | |
) | |
orderBy(ExpressionOrderSpec(column(Song::id), false)) | |
} | |
private inline fun <reified T, R> SpringDataCriteriaQueryDsl<T>.isSongSearchable( | |
searchCondition: SearchCondition?, property: KProperty1<T, R> | |
): PredicateSpec { | |
//if (searchCondition?.searchType == null || searchCondition.keyword == null) return null | |
val keyword = searchCondition?.keyword | |
return when (searchCondition?.searchType) { | |
SearchType.SONG_NAME -> or(keyword?.let { column(Song::name).equal(keyword) }) | |
SearchType.SONG_NO -> or(keyword?.let { column(Song::no).equal(keyword.toDouble().toInt()) }) | |
SearchType.ALBUM_NAME -> or(keyword?.let { column(Album::name).like("%$keyword%") }) | |
SearchType.USEYN -> if (keyword == "Y") column(property).isNotNull() else return column(property).isNull() | |
else -> or(keyword?.let { column(Song::no).equal(keyword.toDouble().toInt()) }) | |
} | |
} | |
enum class SearchType { | |
USEYN... 등등 | |
} | |
inline fun <reified T, R> SpringDataCriteriaQueryDsl<T>.isCustomeSearchable( | |
searchCondition: SearchCondition?, | |
property: KProperty1<T, R> | |
): PredicateSpec { | |
//if (searchCondition?.searchType == null || searchCondition?.keyword == null) return null | |
val keyword = searchCondition?.keyword | |
return when (searchCondition?.searchType) { | |
SearchType.MEMBER_EMAIL -> or(keyword?.let { column(Member::email).like("%$keyword%") }) | |
SearchType.MEMBER_NICKNAME -> or(keyword?.let { column(Member::name).like("%$keyword%") }) | |
SearchType.SONG_NAME -> or(keyword?.let { column(Song::name).like("%$keyword%") }) | |
SearchType.ALBUM_NAME -> or(keyword?.let { column(Album::name).like("%$keyword%") }) | |
SearchType.CATEGORY_NAME -> or(keyword?.let { column(Category::name).like("%$keyword%") }) | |
SearchType.ARTIST_NAME -> or(keyword?.let { column(Artist::name).like("%$keyword%") }) | |
SearchType.PLAYLIST_NAME -> or(keyword?.let { column(Playlist::name).like("%$keyword%") }) | |
SearchType.CATEGORY_LEVEL -> or(keyword?.let { column(Category::level).equal(CategoryLevel.valueOf(keyword)) }) | |
SearchType.SONG_NO -> or(keyword?.let { column(Song::no).equal(keyword.toDouble().toInt()) }) | |
SearchType.USEYN -> if (keyword == "N") column(property).isNull() else column(property).isNotNull() | |
SearchType.MANAGER_EMAIL -> or(keyword?.let { column(Manager::email).like("%$keyword%") }) | |
SearchType.MANAGER_ROLE -> or(keyword?.let { column(Manager::role).equal(Role.valueOf(keyword)) }) | |
SearchType.MEMBER_PROVIDER -> or(keyword?.let { column(Member::provider).equal(Provider.valueOf(keyword)) }) | |
SearchType.MEMBER_PHONE -> or(keyword?.let { column(Member::phone).equal(keyword) }) | |
null -> { | |
println("전체 조회") | |
PredicateSpec.empty | |
} | |
//null -> throw SearchTypeNotFoundException() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment