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

Support Datetime function CURRENT_DATE & CURRENT_TIME #624

Merged
merged 7 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
4 changes: 2 additions & 2 deletions docs/en/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ Kotlin JDSL provides a series of functions to support built-in functions in JPA.

| Function | DSL function |
|--------------------|--------------|
| CURRENT\_DATE | not yet |
| CURRENT\_TIME | not yet |
| CURRENT\_DATE | support |
| CURRENT\_TIME | support |
| CURRENT\_TIMESTAMP | not yet |
| LOCAL DATE | not yet |
| LOCAL TIME | not yet |
Expand Down
4 changes: 2 additions & 2 deletions docs/ko/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ Kotlin JDSL은 JPA에서 제공하는 여러 함수들을 지원하기 위함

| Function | DSL function |
|--------------------|--------------|
| CURRENT\_DATE | not yet |
| CURRENT\_TIME | not yet |
| CURRENT\_DATE | support |
| CURRENT\_TIME | support |
| CURRENT\_TIMESTAMP | not yet |
| LOCAL DATE | not yet |
| LOCAL TIME | not yet |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.select.SelectQuery
import com.linecorp.kotlinjdsl.querymodel.jpql.sort.Sort
import java.math.BigDecimal
import java.math.BigInteger
import java.time.LocalDate
import java.time.LocalTime
import kotlin.internal.Exact
import kotlin.internal.LowPriorityInOverloadResolution
import kotlin.reflect.KClass
Expand Down Expand Up @@ -486,6 +488,26 @@ open class Jpql : JpqlDsl {
return Expressions.times(this.toExpression(), value.toExpression())
}

/**
* Creates an expression that represents the current date.
Copy link
Contributor

Choose a reason for hiding this comment

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

representsthe 사이에 불필요한 whitespace가 하나 더 포함 되어있습니다.

currentTime()의 주석도 둥일하니 같이 수정해주시면 좋을것같습니다.

*
* This is the same as ```CURRENT_DATE```.
*/
@SinceJdsl("3.4.0")
fun currentDate(): Expression<LocalDate> {
return Expressions.currentDate()
}
Copy link
Member

Choose a reason for hiding this comment

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

In the Criteria API, the return type of currentDate() is the java.sql.Date.

image

The Jakarta specification also specifies that the return type is the java.sql.Date. Therefore, please change the return type to the java.sql.Date.

image

Copy link
Member

Choose a reason for hiding this comment

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

For the same reason as above, I would like to request that the return type of currentTime() be changed to the java.sql.Time.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i got it, I'll change currentTime & currentDate


/**
* Creates an expression that represents the current time.
*
* This is the same as ```CURRENT_TIME```.
*/
@SinceJdsl("3.4.0")
fun currentTime(): Expression<LocalTime> {
return Expressions.currentTime()
}

/**
* Creates an expression that represents the divide of values.
* The values are each enclosed in parentheses.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.linecorp.kotlinjdsl.dsl.jpql.expression

import com.linecorp.kotlinjdsl.dsl.jpql.queryPart
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.Test
import java.time.LocalDate

class CurrentDateDslTest : WithAssertions {
@Test
fun `currentDate() with a property`() {
Copy link
Contributor

Choose a reason for hiding this comment

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

특정 property나 expression을 가지고 테스트하는 코드가 아니므로
테스트 함수 이름이 fun currentDate() 정도로 표현되도 좋을것같습니다.

CurrentTimeDslTest 쪽도 비슷하니 같이 수정해주시면 좋을것같습니다.

// when
val expression = queryPart {
currentDate()
}.toExpression()

val actual: Expression<LocalDate> = expression // for type check

// then
val expected = Expressions.currentDate()

assertThat(actual).isEqualTo(expected)
}

@Test
fun `currentDate() with a expression`() {
// when
val expression = queryPart {
currentDate()
}.toExpression()

val actual: Expression<LocalDate> = expression // for type check

// then
val expected = Expressions.currentDate()

assertThat(actual).isEqualTo(expected)
}
Copy link
Contributor

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,40 @@
package com.linecorp.kotlinjdsl.dsl.jpql.expression

import com.linecorp.kotlinjdsl.dsl.jpql.queryPart
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.Test
import java.time.LocalTime

class CurrentTimeDslTest : WithAssertions {
@Test
fun `currentDate() with a property`() {
Copy link
Contributor

Choose a reason for hiding this comment

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

테스트 함수 이름이 테스트 대상(currentTime)과 일치하지 않습니다. 알맞게 변경해주시면 좋을것같습니다.

// when
val expression = queryPart {
currentTime()
}.toExpression()

val actual: Expression<LocalTime> = expression // for type check

// then
val expected = Expressions.currentTime()

assertThat(actual).isEqualTo(expected)
}

@Test
fun `currentDate() with a expression`() {
// when
val expression = queryPart {
currentTime()
}.toExpression()

val actual: Expression<LocalTime> = expression // for type check

// then
val expected = Expressions.currentTime()

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCaseWhen
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCoalesce
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlConcat
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCount
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrent
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCustomExpression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlDivide
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlEntityType
Expand Down Expand Up @@ -44,6 +45,8 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.select.SelectQuery
import com.linecorp.kotlinjdsl.querymodel.jpql.select.impl.JpqlSelectQuery
import java.math.BigDecimal
import java.math.BigInteger
import java.time.LocalDate
import java.time.LocalTime
import kotlin.internal.Exact
import kotlin.reflect.KClass

Expand Down Expand Up @@ -633,4 +636,20 @@ object Expressions {
fun <T : Any> parentheses(expr: Expression<T>): Expression<T> {
return JpqlExpressionParentheses(expr)
}

/**
* Creates an expression that represents the current date.
*/
@SinceJdsl("3.4.0")
fun currentDate(): Expression<LocalDate> {
return JpqlCurrent.CurrentDate
}

/**
* Creates an expression that represents the current time.
*/
@SinceJdsl("3.4.0")
fun currentTime(): Expression<LocalTime> {
return JpqlCurrent.CurrentTime
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl

import com.linecorp.kotlinjdsl.Internal
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression
import java.time.LocalDate
import java.time.LocalTime

/**
* Expression that represents the current date & time.
*/
@Internal
sealed interface JpqlCurrent<T : Any> : Expression<T> {

object CurrentDate : JpqlCurrent<LocalDate>

object CurrentTime : JpqlCurrent<LocalTime>
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCaseWhen
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCoalesce
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlConcat
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCount
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrent
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCustomExpression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlDivide
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlEntityType
Expand Down Expand Up @@ -318,6 +319,28 @@ class ExpressionsTest : WithAssertions {
assertThat(actual).isEqualTo(expected)
}

@Test
fun currentDate() {
// when
val actual = Expressions.currentDate()

// then
val expected = JpqlCurrent.CurrentDate

assertThat(actual).isEqualTo(expected)
}

@Test
fun currentTime() {
// when
val actual = Expressions.currentTime()

// then
val expected = JpqlCurrent.CurrentTime

assertThat(actual).isEqualTo(expected)
}

@Test
fun div() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlCaseWhenSerialize
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlCoalesceSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlConcatSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlCountSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlCurrentDateSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlCurrentTimeSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlCustomExpressionSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlCustomPredicateSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlDeleteQuerySerializer
Expand Down Expand Up @@ -267,6 +269,8 @@ private class DefaultModule : JpqlRenderModule {
JpqlCoalesceSerializer(),
JpqlConcatSerializer(),
JpqlCountSerializer(),
JpqlCurrentDateSerializer(),
JpqlCurrentTimeSerializer(),
JpqlCustomExpressionSerializer(),
JpqlCustomPredicateSerializer(),
JpqlDeleteQuerySerializer(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

import com.linecorp.kotlinjdsl.Internal
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrent
import com.linecorp.kotlinjdsl.render.RenderContext
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializer
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter
import kotlin.reflect.KClass

@Internal
class JpqlCurrentDateSerializer : JpqlSerializer<JpqlCurrent.CurrentDate> {
override fun handledType(): KClass<JpqlCurrent.CurrentDate> {
return JpqlCurrent.CurrentDate::class
}

override fun serialize(part: JpqlCurrent.CurrentDate, writer: JpqlWriter, context: RenderContext) {
val delegate = context.getValue(JpqlRenderSerializer)

writer.write("CURRENT_DATE")

writer.writeParentheses {
delegate.serialize(part, writer, context)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

serializepart: JpqlCurrent.CurrentDate을 전달하면,
JpqlCurrent.CurrentDate 타입에 대해 serialize 재귀 호출이 일어나서 StackOverflowError가 발생합니다.

JpqlCurrentDateSerializer의 경우 괄호와 추가적인 serialize 호출이 필요가 없어보이므로
writer.write("CURRENT_DATE") 한줄만 있어도 충분해 보입니다.

JpqlCurrentTimeSerializer.kt도 마찬가지이므로 같이 수정해주시면 좋을것같습니다.

각 SerializerTest도 수정된 내용에 맞게 다시 봐주시면 좋을것같습니다!

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

import com.linecorp.kotlinjdsl.Internal
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrent
import com.linecorp.kotlinjdsl.render.RenderContext
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializer
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter
import kotlin.reflect.KClass

@Internal
class JpqlCurrentTimeSerializer : JpqlSerializer<JpqlCurrent.CurrentTime> {
override fun handledType(): KClass<JpqlCurrent.CurrentTime> {
return JpqlCurrent.CurrentTime::class
}

override fun serialize(part: JpqlCurrent.CurrentTime, writer: JpqlWriter, context: RenderContext) {
val delegate = context.getValue(JpqlRenderSerializer)

writer.write("CURRENT_TIME")

writer.writeParentheses {
delegate.serialize(part, writer, context)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions.currentDate
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrent
import com.linecorp.kotlinjdsl.render.TestRenderContext
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializerTest
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter
import io.mockk.impl.annotations.MockK
import io.mockk.verifySequence
import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.Test

@JpqlSerializerTest
class JpqlCurrentDateSerializerTest : WithAssertions {

private val sut = JpqlCurrentDateSerializer()

@MockK
private lateinit var writer: JpqlWriter

@MockK
private lateinit var serializer: JpqlRenderSerializer

private val expression = currentDate()
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice if you could use Expressions.currentDate() instead of static import.


@Test
fun handledType() { // when
Copy link
Contributor

Choose a reason for hiding this comment

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

코드 스타일을 맞추기위해 // when 주석의 줄내림이 필요해 보입니다.
JpqlCurrentTimeSerializerTest.kt 코드에도 동일한 부분이 있으니 같이 수정해주시면 좋을것같습니다.

val actual = sut.handledType()

// then
assertThat(actual).isEqualTo(JpqlCurrent.CurrentDate::class)
}

@Test
fun serialize() {
// given
val part = Expressions.currentDate()
val context = TestRenderContext(serializer)

// when
sut.serialize(part as JpqlCurrent.CurrentDate, writer, context)

// then
verifySequence {
writer.write("CURRENT_DATE")
writer.writeParentheses(any())
serializer.serialize(expression, writer, context)
}
}
}
Loading