diff --git a/dsl/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/Jpql.kt b/dsl/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/Jpql.kt index f87daf8f5..dc4b409e8 100644 --- a/dsl/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/Jpql.kt +++ b/dsl/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/Jpql.kt @@ -46,6 +46,7 @@ import java.math.BigDecimal import java.math.BigInteger import java.sql.Date import java.sql.Time +import java.sql.Timestamp import java.time.LocalDate import java.time.LocalDateTime import java.time.LocalTime @@ -745,6 +746,16 @@ open class Jpql : JpqlDsl { return Expressions.currentTime() } + /** + * Creates an expression that represents the current timestamp(datetime). + * + * This is the same as ```CURRENT_TIMESTAMP```. + */ + @SinceJdsl("3.4.0") + fun currentTimestamp(): Expression { + return Expressions.currentTimestamp() + } + /** * Creates an expression that represents the local date. * diff --git a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/CurrentTimestampTest.kt b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/CurrentTimestampTest.kt new file mode 100644 index 000000000..500264add --- /dev/null +++ b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/CurrentTimestampTest.kt @@ -0,0 +1,25 @@ +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.sql.Timestamp + +class CurrentTimestampTest : WithAssertions { + @Test + fun currentTimestamp() { + // when + val expression = queryPart { + currentTimestamp() + }.toExpression() + + val actual: Expression = expression // for type check + + // then + val expected = Expressions.currentTimestamp() + + assertThat(actual).isEqualTo(expected) + } +} diff --git a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/JpqlLocalDateTimeDslTest.kt b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/LocalDateTimeDslTest.kt similarity index 82% rename from dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/JpqlLocalDateTimeDslTest.kt rename to dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/LocalDateTimeDslTest.kt index d0e06e140..d05c03f84 100644 --- a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/JpqlLocalDateTimeDslTest.kt +++ b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/LocalDateTimeDslTest.kt @@ -1,12 +1,13 @@ -package com.linecorp.kotlinjdsl.dsl.jpql +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.LocalDateTime -class JpqlLocalDateTimeDslTest { +class LocalDateTimeDslTest { @Test fun localDateTime() { // when diff --git a/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/Expressions.kt b/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/Expressions.kt index cd9708458..d010059ec 100644 --- a/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/Expressions.kt +++ b/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/Expressions.kt @@ -13,6 +13,7 @@ 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.JpqlCurrentDate import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrentTime +import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrentTimestamp 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 @@ -61,6 +62,7 @@ import java.math.BigDecimal import java.math.BigInteger import java.sql.Date import java.sql.Time +import java.sql.Timestamp import java.time.LocalDate import java.time.LocalDateTime import java.time.LocalTime @@ -326,6 +328,16 @@ object Expressions { return JpqlCurrentTime } + /** + * Creates an expression that represents the current timestamp(datetime). + * + * This is the same as ```CURRENT_TIMESTAMP```. + */ + @SinceJdsl("3.4.0") + fun currentTimestamp(): Expression { + return JpqlCurrentTimestamp + } + /** * Creates an expression that represents the local date. * diff --git a/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/impl/JpqlCurrentTimestamp.kt b/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/impl/JpqlCurrentTimestamp.kt new file mode 100644 index 000000000..6fc62f6da --- /dev/null +++ b/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/impl/JpqlCurrentTimestamp.kt @@ -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.sql.Timestamp + +@Internal +object JpqlCurrentTimestamp : Expression diff --git a/query-model/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/ExpressionsTest.kt b/query-model/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/ExpressionsTest.kt index 7dd741341..1926ff3fb 100644 --- a/query-model/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/ExpressionsTest.kt +++ b/query-model/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/ExpressionsTest.kt @@ -15,6 +15,7 @@ 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.JpqlCurrentDate import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrentTime +import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrentTimestamp 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 @@ -501,6 +502,17 @@ class ExpressionsTest : WithAssertions { assertThat(actual).isEqualTo(expected) } + @Test + fun currentTimestamp() { + // when + val actual = Expressions.currentTimestamp() + + // then + val expected = JpqlCurrentTimestamp + + assertThat(actual).isEqualTo(expected) + } + @Test fun localDate() { // when diff --git a/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/JpqlRenderContext.kt b/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/JpqlRenderContext.kt index 08e6a1349..b805b01cc 100644 --- a/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/JpqlRenderContext.kt +++ b/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/JpqlRenderContext.kt @@ -26,6 +26,7 @@ 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.JpqlCurrentTimestampSerializer 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 @@ -286,6 +287,7 @@ private class DefaultModule : JpqlRenderModule { JpqlCountSerializer(), JpqlCurrentDateSerializer(), JpqlCurrentTimeSerializer(), + JpqlCurrentTimestampSerializer(), JpqlCustomExpressionSerializer(), JpqlCustomPredicateSerializer(), JpqlDeleteQuerySerializer(), diff --git a/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlCurrentTimestampSerializer.kt b/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlCurrentTimestampSerializer.kt new file mode 100644 index 000000000..92111b987 --- /dev/null +++ b/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlCurrentTimestampSerializer.kt @@ -0,0 +1,19 @@ +package com.linecorp.kotlinjdsl.render.jpql.serializer.impl + +import com.linecorp.kotlinjdsl.Internal +import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrentTimestamp +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 JpqlCurrentTimestampSerializer : JpqlSerializer { + override fun handledType(): KClass { + return JpqlCurrentTimestamp::class + } + + override fun serialize(part: JpqlCurrentTimestamp, writer: JpqlWriter, context: RenderContext) { + writer.write("CURRENT_TIMESTAMP") + } +} diff --git a/render/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlCurrentTimestampSerializerTest.kt b/render/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlCurrentTimestampSerializerTest.kt new file mode 100644 index 000000000..11e958ec4 --- /dev/null +++ b/render/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlCurrentTimestampSerializerTest.kt @@ -0,0 +1,47 @@ +package com.linecorp.kotlinjdsl.render.jpql.serializer.impl + +import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions +import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrentTimestamp +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 JpqlCurrentTimestampSerializerTest : WithAssertions { + private val sut = JpqlCurrentTimestampSerializer() + + @MockK + private lateinit var writer: JpqlWriter + + @MockK + private lateinit var serializer: JpqlRenderSerializer + + @Test + fun handledType() { + // when + val actual = sut.handledType() + + // then + assertThat(actual).isEqualTo(JpqlCurrentTimestamp::class) + } + + @Test + fun serialize() { + // given + val part = Expressions.currentTimestamp() + val context = TestRenderContext(serializer) + + // when + sut.serialize(part as JpqlCurrentTimestamp, writer, context) + + // then + verifySequence { + writer.write("CURRENT_TIMESTAMP") + } + } +}