Skip to content

Commit

Permalink
feat: Support Datetime function LOCAL DATE
Browse files Browse the repository at this point in the history
  • Loading branch information
chicori3 committed Feb 8, 2024
1 parent 755c8d5 commit f99753f
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/en/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ Kotlin JDSL provides a series of functions to support built-in functions in JPA.
| CURRENT\_DATE | not yet |
| CURRENT\_TIME | not yet |
| CURRENT\_TIMESTAMP | not yet |
| LOCAL DATE | not yet |
| LOCAL DATE | support |
| LOCAL TIME | not yet |
| LOCAL DATETIME | not yet |
| EXTRACT | not yet |
Expand Down
2 changes: 1 addition & 1 deletion docs/ko/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ Kotlin JDSL은 JPA에서 제공하는 여러 함수들을 지원하기 위함
| CURRENT\_DATE | not yet |
| CURRENT\_TIME | not yet |
| CURRENT\_TIMESTAMP | not yet |
| LOCAL DATE | not yet |
| LOCAL DATE | support |
| LOCAL TIME | not yet |
| LOCAL DATETIME | not yet |
| EXTRACT | not yet |
Expand Down
11 changes: 11 additions & 0 deletions dsl/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/Jpql.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ 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 kotlin.internal.Exact
import kotlin.internal.LowPriorityInOverloadResolution
import kotlin.reflect.KClass
Expand Down Expand Up @@ -1380,6 +1381,16 @@ open class Jpql : JpqlDsl {
)
}

/**
* Creates an expression that represents the local date.
*
* This is the same as ```LOCAL DATE```.
*/
@SinceJdsl("3.4.0")
fun localDate(): Expression<LocalDate> {
return Expressions.localDate()
}

/**
* Creates an expression that represents predefined database functions and user-defined database functions.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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.Assertions.assertThat
import org.junit.jupiter.api.Test
import java.time.LocalDate

class LocalDateDslTest {

@Test
fun localDate() {
// when
val expression = queryPart {
localDate()
}.toExpression()

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

// then
val expected = Expressions.localDate()

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlExpressionPar
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFunctionExpression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLength
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLiteral
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocalDate
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocate
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLower
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMax
Expand Down Expand Up @@ -44,6 +45,7 @@ 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 kotlin.internal.Exact
import kotlin.reflect.KClass

Expand Down Expand Up @@ -543,6 +545,14 @@ object Expressions {
return JpqlLocate(substring, string, start)
}

/**
* Creates an expression that represents the local date.
*/
@SinceJdsl("3.4.0")
fun localDate(): Expression<LocalDate> {
return JpqlLocalDate
}

/**
* Creates an expression that represents predefined database functions and user-defined database functions.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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

@Internal
object JpqlLocalDate : Expression<LocalDate>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlExpressionPar
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFunctionExpression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLength
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLiteral
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocalDate
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocate
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLower
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMax
Expand Down Expand Up @@ -774,6 +775,17 @@ class ExpressionsTest : WithAssertions {
assertThat(actual).isEqualTo(expected)
}

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

// then
val expected = JpqlLocalDate

assertThat(actual).isEqualTo(expected)
}

@Test
fun function() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLessThanOrEqualTo
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLessThanSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLikeSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLiteralSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLocalDateSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLocateSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLowerSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlMaxSerializer
Expand Down Expand Up @@ -314,6 +315,7 @@ private class DefaultModule : JpqlRenderModule {
JpqlLessThanSerializer(),
JpqlLikeSerializer(),
JpqlLiteralSerializer(),
JpqlLocalDateSerializer(),
JpqlLocateSerializer(),
JpqlLowerSerializer(),
JpqlMaxSerializer(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

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

@Internal
class JpqlLocalDateSerializer : JpqlSerializer<JpqlLocalDate> {

override fun handledType(): KClass<JpqlLocalDate> {
return JpqlLocalDate::class
}

override fun serialize(part: JpqlLocalDate, writer: JpqlWriter, context: RenderContext) {
writer.write("LOCAL DATE")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocalDate
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.Assertions.assertThat
import org.junit.jupiter.api.Test

@JpqlSerializerTest
class JpqlLocalDateSerializerTest {
private val sut = JpqlLocalDateSerializer()

@MockK
private lateinit var writer: JpqlWriter

@MockK
private lateinit var serializer: JpqlRenderSerializer

@Test
fun handle() {
// when
val actual = sut.handledType()

// then
assertThat(actual).isEqualTo(JpqlLocalDate::class)
}

@Test
fun serialize() {
// given
val part = JpqlLocalDate
val context = TestRenderContext(serializer)

// when
sut.serialize(part, writer, context)

// then
verifySequence {
writer.write("LOCAL DATE")
}
}
}

0 comments on commit f99753f

Please sign in to comment.