Skip to content

Commit

Permalink
[FO-1037] sql in 문에 값이 없는 경우 true으로 변환되는 문제 픽스 (#434)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoodlesOnMyFood authored Apr 25, 2024
1 parent 3841cbd commit 20f8bd0
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 125 deletions.
15 changes: 15 additions & 0 deletions common/src/main/kotlin/com/fone/common/config/jpa/JdslExt.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.fone.common.config.jpa

import com.linecorp.kotlinjdsl.query.spec.expression.ExpressionSpec
import com.linecorp.kotlinjdsl.query.spec.expression.LiteralSpec
import com.linecorp.kotlinjdsl.query.spec.predicate.EqualValueSpec
import com.linecorp.kotlinjdsl.query.spec.predicate.InValueSpec
import com.linecorp.kotlinjdsl.query.spec.predicate.PredicateSpec

fun <R> ExpressionSpec<R>.inValues(values: Collection<R>): PredicateSpec {
if (values.isEmpty()) {
// values가 없으면 항상 false이도록
return EqualValueSpec(LiteralSpec(1), 0)
}
return InValueSpec(this, values)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fone.competition.infrastructure

import com.fone.common.config.jpa.inValues
import com.fone.competition.domain.entity.Competition
import com.fone.competition.domain.entity.CompetitionScrap
import com.fone.competition.domain.repository.CompetitionRepository
Expand All @@ -26,34 +27,35 @@ class CompetitionRepositoryImpl(
private val sessionFactory: Mutiny.SessionFactory,
private val queryFactory: SpringDataHibernateMutinyReactiveQueryFactory,
) : CompetitionRepository {

override suspend fun findAll(pageable: Pageable): Page<Competition> {
return queryFactory.withFactory { factory ->
val ids = factory.pageQuery(pageable) {
select(column(Competition::id))
from(entity(Competition::class))
where(
col(Competition::showStartDate).lessThanOrEqualTo(
LocalDate.now()
)
)
}

val competitions = factory.listQuery {
select(entity(Competition::class))
from(entity(Competition::class))
where(
and(
col(Competition::id).`in`(ids.content),
val ids =
factory.pageQuery(pageable) {
select(column(Competition::id))
from(entity(Competition::class))
where(
col(Competition::showStartDate).lessThanOrEqualTo(
LocalDate.now()
)
)
)
orderBy(
orderSpec(pageable.sort)
)
}.iterator()
}

val competitions =
factory.listQuery {
select(entity(Competition::class))
from(entity(Competition::class))
where(
and(
col(Competition::id).inValues(ids.content),
col(Competition::showStartDate).lessThanOrEqualTo(
LocalDate.now()
)
)
)
orderBy(
orderSpec(pageable.sort)
)
}.iterator()

ids.map { competitions.next() }
}
Expand Down Expand Up @@ -81,18 +83,20 @@ class CompetitionRepositoryImpl(
userId: Long,
): Page<Competition> {
return queryFactory.withFactory { factory ->
val ids = factory.pageQuery(pageable) {
select(column(Competition::id))
from(entity(CompetitionScrap::class))
join(CompetitionScrap::competition)
where(col(CompetitionScrap::userId).equal(userId))
}

val competitions = factory.listQuery {
select(entity(Competition::class))
from(entity(Competition::class))
where(col(Competition::id).`in`(ids.content))
}.associateBy { it!!.id }
val ids =
factory.pageQuery(pageable) {
select(column(Competition::id))
from(entity(CompetitionScrap::class))
join(CompetitionScrap::competition)
where(col(CompetitionScrap::userId).equal(userId))
}

val competitions =
factory.listQuery {
select(entity(Competition::class))
from(entity(Competition::class))
where(col(Competition::id).inValues(ids.content))
}.associateBy { it!!.id }

ids.map { competitions[it] }
}
Expand All @@ -110,46 +114,48 @@ class CompetitionRepositoryImpl(
}
}

private fun SpringDataReactiveCriteriaQueryDsl<Competition?>.orderSpec(
sort: Sort,
): List<OrderSpec> {
val screeningDateAfterToday = case(
`when`(
column(Competition::screeningEndDate).greaterThan(LocalDate.now())
).then(
literal(1)
),
`else` = literal(0)
).desc()

val screeningDateIsNull = case(
`when`(
or(
column(Competition::screeningEndDate).isNull(),
column(Competition::screeningStartDate).isNull()
)
).then(
literal(1)
),
`else` = literal(0)
).desc()
private fun SpringDataReactiveCriteriaQueryDsl<Competition?>.orderSpec(sort: Sort): List<OrderSpec> {
val screeningDateAfterToday =
case(
`when`(
column(Competition::screeningEndDate).greaterThan(LocalDate.now())
).then(
literal(1)
),
`else` = literal(0)
).desc()

val screeningDateIsNull =
case(
`when`(
or(
column(Competition::screeningEndDate).isNull(),
column(Competition::screeningStartDate).isNull()
)
).then(
literal(1)
),
`else` = literal(0)
).desc()

val screeningDateAsc = column(Competition::screeningEndDate).asc()

val res = sort.map {
val columnSpec = when (it.property) {
"viewCount" -> col(Competition::viewCount)
"createdAt" -> col(Competition::createdAt)
"scrapCount" -> col(Competition::scrapCount)
else -> col(Competition::viewCount)
}

if (it.isAscending) {
columnSpec.asc()
} else {
columnSpec.desc()
}
}.toList()
val res =
sort.map {
val columnSpec =
when (it.property) {
"viewCount" -> col(Competition::viewCount)
"createdAt" -> col(Competition::createdAt)
"scrapCount" -> col(Competition::scrapCount)
else -> col(Competition::viewCount)
}

if (it.isAscending) {
columnSpec.asc()
} else {
columnSpec.desc()
}
}.toList()

return if (sort.map { it.property }.contains("screeningEndDate")) {
listOf(screeningDateAfterToday, screeningDateIsNull, screeningDateAsc)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fone.jobOpening.infrastructure

import com.fone.common.config.jpa.inValues
import com.fone.common.entity.CategoryType
import com.fone.jobOpening.domain.entity.JobOpeningCategory
import com.fone.jobOpening.domain.repository.JobOpeningCategoryRepository
Expand All @@ -16,10 +17,7 @@ class JobOpeningCategoryRepositoryImpl(
private val sessionFactory: Mutiny.SessionFactory,
private val queryFactory: SpringDataHibernateMutinyReactiveQueryFactory,
) : JobOpeningCategoryRepository {

override suspend fun saveAll(
jobOpeningCategories: List<JobOpeningCategory>,
): List<JobOpeningCategory> {
override suspend fun saveAll(jobOpeningCategories: List<JobOpeningCategory>): List<JobOpeningCategory> {
return jobOpeningCategories.also {
sessionFactory.withSession { session ->
session.persistAll(*it.toTypedArray()).flatMap { session.flush() }
Expand All @@ -33,13 +31,11 @@ class JobOpeningCategoryRepositoryImpl(
}
}

override suspend fun findByJobOpeningIds(
jobOpeningIds: List<Long>,
): Map<Long, List<CategoryType>> {
override suspend fun findByJobOpeningIds(jobOpeningIds: List<Long>): Map<Long, List<CategoryType>> {
return queryFactory.listQuery {
select(entity(JobOpeningCategory::class))
from(entity(JobOpeningCategory::class))
where(col(JobOpeningCategory::jobOpeningId).`in`(jobOpeningIds))
where(col(JobOpeningCategory::jobOpeningId).inValues(jobOpeningIds))
}.groupBy({ it!!.jobOpeningId }, { it!!.type })
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fone.jobOpening.infrastructure

import com.fone.common.config.jpa.inValues
import com.fone.common.entity.DomainType
import com.fone.jobOpening.domain.entity.JobOpeningDomain
import com.fone.jobOpening.domain.repository.JobOpeningDomainRepository
Expand All @@ -16,7 +17,6 @@ class JobOpeningDomainRepositoryImpl(
private val sessionFactory: Mutiny.SessionFactory,
private val queryFactory: SpringDataHibernateMutinyReactiveQueryFactory,
) : JobOpeningDomainRepository {

override suspend fun saveAll(jobOpeningDomain: List<JobOpeningDomain>?): List<JobOpeningDomain>? {
return jobOpeningDomain?.also {
sessionFactory.withSession { session ->
Expand All @@ -31,13 +31,11 @@ class JobOpeningDomainRepositoryImpl(
}
}

override suspend fun findByJobOpeningIds(
jobOpeningIds: List<Long>,
): Map<Long, List<DomainType>> {
override suspend fun findByJobOpeningIds(jobOpeningIds: List<Long>): Map<Long, List<DomainType>> {
return queryFactory.listQuery {
select(entity(JobOpeningDomain::class))
from(entity(JobOpeningDomain::class))
where(col(JobOpeningDomain::jobOpeningId).`in`(jobOpeningIds))
where(col(JobOpeningDomain::jobOpeningId).inValues(jobOpeningIds))
}.groupBy({ it!!.jobOpeningId }, { it!!.type })
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fone.jobOpening.infrastructure

import com.fone.common.config.jpa.inValues
import com.fone.common.entity.Type
import com.fone.jobOpening.domain.entity.JobOpening
import com.fone.jobOpening.domain.entity.JobOpeningCategory
Expand Down Expand Up @@ -75,7 +76,7 @@ class JobOpeningRepositoryImpl(
from(entity(JobOpeningDomain::class))
where(
and(
col(JobOpeningDomain::type).`in`(request.domains)
col(JobOpeningDomain::type).inValues(request.domains)
)
)
}
Expand All @@ -90,7 +91,7 @@ class JobOpeningRepositoryImpl(
from(entity(JobOpeningCategory::class))
where(
and(
col(JobOpeningCategory::type).`in`(request.categories)
col(JobOpeningCategory::type).inValues(request.categories)
)
)
}
Expand All @@ -103,14 +104,20 @@ class JobOpeningRepositoryImpl(
where(
and(
col(JobOpening::type).equal(request.type),
col(JobOpening::gender).`in`(request.genders),
col(JobOpening::gender).inValues(request.genders),
col(JobOpening::ageMax).greaterThanOrEqualTo(request.ageMin),
col(JobOpening::ageMin).lessThanOrEqualTo(request.ageMax),
if (request.domains.isNotEmpty()) col(JobOpening::id).`in`(domainJobOpeningIds) else null,
if (request.domains.isNotEmpty()) {
col(
JobOpening::id
).inValues(domainJobOpeningIds)
} else {
null
},
if (request.categories.isNotEmpty()) {
col(
JobOpening::id
).`in`(categoryJobOpeningIds)
).inValues(categoryJobOpeningIds)
} else {
null
},
Expand All @@ -133,7 +140,7 @@ class JobOpeningRepositoryImpl(
from(entity(JobOpening::class))
fetch(JobOpening::imageUrls, joinType = JoinType.LEFT)
fetch(JobOpening::location, joinType = JoinType.LEFT)
where(and(col(JobOpening::id).`in`(jobOpeningIds.content)))
where(and(col(JobOpening::id).inValues(jobOpeningIds.content)))
orderBy(orderSpec(pageable.sort))
}

Expand Down Expand Up @@ -197,7 +204,7 @@ class JobOpeningRepositoryImpl(
from(entity(JobOpening::class))
fetch(JobOpening::imageUrls, joinType = JoinType.LEFT)
fetch(JobOpening::location, joinType = JoinType.LEFT)
where(and(col(JobOpening::id).`in`(ids.content)))
where(and(col(JobOpening::id).inValues(ids.content)))
}.associateBy { it?.id }

ids.map { jobOpenings[it] }
Expand Down Expand Up @@ -230,7 +237,7 @@ class JobOpeningRepositoryImpl(
from(entity(JobOpening::class))
fetch(JobOpening::imageUrls, joinType = JoinType.LEFT)
fetch(JobOpening::location, joinType = JoinType.LEFT)
where(col(JobOpening::id).`in`(ids.content))
where(col(JobOpening::id).inValues(ids.content))
}.associateBy { it?.id }

PageImpl(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fone.profile.infrastructure

import com.fone.common.config.jpa.inValues
import com.fone.common.entity.CategoryType
import com.fone.profile.domain.entity.ProfileCategory
import com.fone.profile.domain.repository.ProfileCategoryRepository
Expand All @@ -16,7 +17,6 @@ class ProfileCategoryRepositoryImpl(
private val sessionFactory: Mutiny.SessionFactory,
private val queryFactory: SpringDataHibernateMutinyReactiveQueryFactory,
) : ProfileCategoryRepository {

override suspend fun saveAll(profileCategory: List<ProfileCategory>): List<ProfileCategory> {
return profileCategory.also {
sessionFactory.withSession { session ->
Expand All @@ -35,7 +35,7 @@ class ProfileCategoryRepositoryImpl(
return queryFactory.listQuery {
select(entity(ProfileCategory::class))
from(entity(ProfileCategory::class))
where(col(ProfileCategory::profileId).`in`(profileIds))
where(col(ProfileCategory::profileId).inValues(profileIds))
}.groupBy({ it!!.profileId }, { it!!.type })
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.fone.profile.infrastructure

import com.fone.common.config.jpa.inValues
import com.fone.common.entity.DomainType
import com.fone.profile.domain.entity.ProfileDomain
import com.fone.profile.domain.repository.ProfileDomainRepository
Expand All @@ -16,7 +17,6 @@ class ProfileDomainRepositoryImpl(
private val sessionFactory: Mutiny.SessionFactory,
private val queryFactory: SpringDataHibernateMutinyReactiveQueryFactory,
) : ProfileDomainRepository {

override suspend fun saveAll(profileDomain: List<ProfileDomain>?): List<ProfileDomain>? {
return profileDomain?.also {
sessionFactory.withSession { session ->
Expand All @@ -27,6 +27,7 @@ class ProfileDomainRepositoryImpl(

override suspend fun deleteByProfileId(profileId: Long): Int {
return queryFactory.deleteQuery<ProfileDomain> {
literal(1).equal(1)
where(col(ProfileDomain::profileId).equal(profileId))
}
}
Expand All @@ -35,7 +36,7 @@ class ProfileDomainRepositoryImpl(
return queryFactory.listQuery {
select(entity(ProfileDomain::class))
from(entity(ProfileDomain::class))
where(col(ProfileDomain::profileId).`in`(profileIds))
where(col(ProfileDomain::profileId).inValues(profileIds))
}.groupBy({ it!!.profileId }, { it!!.type })
}

Expand Down
Loading

0 comments on commit 20f8bd0

Please sign in to comment.