Skip to content

Commit

Permalink
feat: support current timestamp function
Browse files Browse the repository at this point in the history
  • Loading branch information
progress0407 committed Feb 20, 2024
1 parent f27809b commit 470de34
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 2 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 @@ -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
Expand Down Expand Up @@ -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<Timestamp> {
return Expressions.currentTimestamp()
}

/**
* Creates an expression that represents the local date.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Timestamp> = expression // for type check

// then
val expected = Expressions.currentTimestamp()

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Timestamp> {
return JpqlCurrentTimestamp
}

/**
* Creates an expression that represents the local date.
*
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.sql.Timestamp

@Internal
object JpqlCurrentTimestamp : Expression<Timestamp>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -286,6 +287,7 @@ private class DefaultModule : JpqlRenderModule {
JpqlCountSerializer(),
JpqlCurrentDateSerializer(),
JpqlCurrentTimeSerializer(),
JpqlCurrentTimestampSerializer(),
JpqlCustomExpressionSerializer(),
JpqlCustomPredicateSerializer(),
JpqlDeleteQuerySerializer(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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<JpqlCurrentTimestamp> {
override fun handledType(): KClass<JpqlCurrentTimestamp> {
return JpqlCurrentTimestamp::class
}

override fun serialize(part: JpqlCurrentTimestamp, writer: JpqlWriter, context: RenderContext) {
writer.write("CURRENT_TIMESTAMP")
}
}
Original file line number Diff line number Diff line change
@@ -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")
}
}
}

0 comments on commit 470de34

Please sign in to comment.