Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Datetime function CURRENT_TIMESTAMP #646

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The datetime and timestamp may be different in different contexts. For example, to a MySQL user, datetime and timestamp will look like separate types.

I don't think the Kotlin JDSL needs to interpret them separately and guide the user to datetime.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find any other issues, so I'll fix this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh! Thanks, I also noticed that timestamp didn't seem to include time, so you're right!
Thanks for letting me know, that's helpful haha

*
* 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")
}
}
}