Skip to content

Commit

Permalink
feat: supporting localDateTime function feat: supporting localDateTim…
Browse files Browse the repository at this point in the history
…e function for datetime function
  • Loading branch information
ilgolf committed Feb 13, 2024
1 parent ccea406 commit b1eb048
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 0 deletions.
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 @@ -47,6 +47,7 @@ import java.math.BigInteger
import java.sql.Date
import java.sql.Time
import java.time.LocalDate
import java.time.LocalDateTime
import kotlin.internal.Exact
import kotlin.internal.LowPriorityInOverloadResolution
import kotlin.reflect.KClass
Expand Down Expand Up @@ -3113,4 +3114,14 @@ open class Jpql : JpqlDsl {
fun <T : Any> deleteFrom(entity: Entityable<T>): DeleteQueryWhereStep<T> {
return DeleteQueryDsl(entity.toEntity())
}

/**
* Creates an expression that represents the local datetime.
*
* This is the same as ```LOCAL DATETIME```.
*/
@SinceJdsl("3.4.0")
fun localDateTime(): Expression<LocalDateTime> {
return Expressions.localDateTime()
}
}
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 JpqlLocalDateTimeDslTest {

@Test
fun localDateTime() {
// 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 @@ -25,6 +25,7 @@ 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.JpqlLn
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocalDate
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocalDateTime
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 @@ -60,6 +61,7 @@ import java.math.BigInteger
import java.sql.Date
import java.sql.Time
import java.time.LocalDate
import java.time.LocalDateTime
import kotlin.internal.Exact
import kotlin.reflect.KClass

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

/**
* Creates an expression that represents the local datetime in jpql.
*/
@SinceJdsl("3.4.0")
fun localDateTime(): Expression<LocalDateTime> {
return JpqlLocalDateTime
}
}
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.LocalDateTime

@Internal
object JpqlLocalDateTime : Expression<LocalDateTime>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ 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.JpqlLn
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocalDate
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocalDateTime
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 @@ -1065,4 +1066,15 @@ class ExpressionsTest : WithAssertions {

assertThat(actual).isEqualTo(expected)
}

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

// then
val expected = JpqlLocalDateTime.toExpression()

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ 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.JpqlLnSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLocalDateSerializer
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 @@ -336,6 +337,7 @@ private class DefaultModule : JpqlRenderModule {
JpqlLiteralSerializer(),
JpqlLnSerializer(),
JpqlLocalDateSerializer(),
JpqlLocalDateTimeSerializer(),
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.JpqlLocalDateTime
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 JpqlLocalDateTimeSerializer : JpqlSerializer<JpqlLocalDateTime> {

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

override fun serialize(part: JpqlLocalDateTime, writer: JpqlWriter, context: RenderContext) {
writer.write("LOCAL DATETIME")
}
}
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.impl.JpqlLocalDateTime
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 JpqlJpqlLocalDateTimeSerializerTest {
private val sut = JpqlLocalDateTimeSerializer()

@MockK
private lateinit var writer: JpqlWriter

@MockK
private lateinit var serializer: JpqlRenderSerializer

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

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

@Test
fun serialize() {
// given
every { writer.write(any<String>()) } just runs

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

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

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

0 comments on commit b1eb048

Please sign in to comment.