Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FO-1037] sql in 문에 값이 없는 경우 true으로 변환되는 문제 픽스 #434

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions common/src/main/kotlin/com/fone/common/config/jpa/DslConfig.kt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DslConfig 대신 jdslExt 같은 네이밍은 어떨까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네 좋습니다

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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

class DslConfig
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

요거 lint때문에 추가한거죠?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아 아뇨, 원래 인터페이스 갖고 뭐하려다가 지웠는데 깜빡한 모양입니다.


fun <R> ExpressionSpec<R>.inValues(values: Collection<R>): PredicateSpec {
if (values.isEmpty()) {
// values가 없으면 항상 false이도록
return EqualValueSpec(LiteralSpec(1), 0)
}
return InValueSpec(this, values)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 함수는 어떻게 찾으셨나여?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kotlin-jdsl 라이브러리 코드 보고 적당히 원하는 부분 합쳤습니다.

}
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
Loading