Skip to content

Commit

Permalink
feat: supporting localDateTime function for datetime function
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgolf committed Feb 6, 2024
1 parent 4219a32 commit 1bc08e1
Show file tree
Hide file tree
Showing 10 changed files with 147 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 @@ -228,7 +228,7 @@ Kotlin JDSL provides a series of functions to support built-in functions in JPA.
| CURRENT\_TIMESTAMP | not yet |
| LOCAL DATE | not yet |
| LOCAL TIME | not yet |
| LOCAL DATETIME | not yet |
| LOCAL DATETIME | support |
| EXTRACT | not yet |

### Database function
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 @@ -226,7 +226,7 @@ Kotlin JDSL은 JPA에서 제공하는 여러 함수들을 지원하기 위함
| CURRENT\_TIMESTAMP | not yet |
| LOCAL DATE | not yet |
| LOCAL TIME | not yet |
| LOCAL DATETIME | not yet |
| LOCAL DATETIME | support |
| EXTRACT | not yet |

### Database function
Expand Down
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.LocalDateTime
import kotlin.internal.Exact
import kotlin.internal.LowPriorityInOverloadResolution
import kotlin.reflect.KClass
Expand Down Expand Up @@ -166,6 +167,14 @@ open class Jpql : JpqlDsl {
return Expressions.nullLiteral()
}

/**
* Creates a datetime expression with the localDateTime.
*/
@SinceJdsl("3.4.0")
fun localDateTime(): Expression<LocalDateTime> {
return Expressions.localDateTime()
}

/**
* Creates a parameter expression with the name.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.linecorp.kotlinjdsl.dsl.jpql

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.LocalDateTime

class LocalDateTimeExpressionDslTest {

@Test
fun `localDateTime to support LOCAL DATETIME in jpql`() {
// when
val expression = queryPart {
localDateTime()
}.toExpression()

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

// then
val expected = Expressions.localDateTime()

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlTrimLeading
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlTrimTrailing
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlUpper
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlValue
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.LocalDateTimeExpression
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Path
import com.linecorp.kotlinjdsl.querymodel.jpql.predicate.Predicate
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.LocalDateTime
import kotlin.internal.Exact
import kotlin.reflect.KClass

Expand Down Expand Up @@ -149,6 +151,14 @@ object Expressions {
return JpqlLiteral.NullLiteral as Expression<T>
}

/**
* Create a LOCAL DATETIME Expression by LocalDateTime.
*/
@SinceJdsl("3.4.0")
fun localDateTime(): Expression<LocalDateTime> {
return LocalDateTimeExpression
}

/**
* Creates a parameter expression with the name.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl

import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression
import java.time.LocalDateTime

object LocalDateTimeExpression : Expression<LocalDateTime>
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlTrimLeading
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlTrimTrailing
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlUpper
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlValue
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.LocalDateTimeExpression
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Paths
import com.linecorp.kotlinjdsl.querymodel.jpql.predicate.Predicates
import com.linecorp.kotlinjdsl.querymodel.jpql.select.impl.JpqlSelectQuery
Expand Down Expand Up @@ -887,4 +888,15 @@ class ExpressionsTest : WithAssertions {

assertThat(actual).isEqualTo(expected)
}

@Test
fun `localDateTime to support LOCAL DATETIME in jpql`() {
// when
val actual = Expressions.localDateTime()

// then
val expected = LocalDateTimeExpression.toExpression()

assertThat(actual).isEqualTo(expected)
}
}
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.JpqlLocalDateTimeSerializer
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(),
JpqlLocalDateTimeSerializer(),
JpqlLocateSerializer(),
JpqlLowerSerializer(),
JpqlMaxSerializer(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

import com.linecorp.kotlinjdsl.Internal
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.LocalDateTimeExpression
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 JpqlLocalDateTimeSerializer : JpqlSerializer<LocalDateTimeExpression> {

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

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

writer.write("LOCAL DATETIME")

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

import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.LocalDateTimeExpression
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.every
import io.mockk.impl.annotations.MockK
import io.mockk.just
import io.mockk.runs
import io.mockk.verifySequence
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test

@JpqlSerializerTest
class JpqlLocalDateTimeExpressionSerializerTest {
private val sut = JpqlLocalDateTimeSerializer()

@MockK
private lateinit var writer: JpqlWriter

@MockK
private lateinit var serializer: JpqlRenderSerializer

@Test
fun `handle type only JpqlLocalDateTime`() {
// when
val actual = sut.handledType()

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

@Test
fun `serialize LOCAL DATETIME function in Jpql`() {
// given
every { writer.write(any<String>()) } just runs

val part = Expressions.localDateTime() as LocalDateTimeExpression
val context = TestRenderContext(serializer)

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

// then
verifySequence {
writer.write("LOCAL DATETIME")
writer.writeParentheses(any())
serializer.serialize(part, writer, context)
}
}
}

0 comments on commit 1bc08e1

Please sign in to comment.