Skip to content

Commit

Permalink
Merge pull request #624 from meengi07/feat/currentDate&time
Browse files Browse the repository at this point in the history
Support Datetime function CURRENT_DATE & CURRENT_TIME
  • Loading branch information
shouwn authored Feb 6, 2024
2 parents 9ef614e + 0555264 commit 6f48b77
Show file tree
Hide file tree
Showing 14 changed files with 273 additions and 4 deletions.
4 changes: 2 additions & 2 deletions docs/en/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ sqrt(path(Book::price))

| Function | DSL function |
|--------------------|--------------|
| CURRENT\_DATE | not yet |
| CURRENT\_TIME | not yet |
| CURRENT\_DATE | support |
| CURRENT\_TIME | support |
| CURRENT\_TIMESTAMP | not yet |
| LOCAL DATE | not yet |
| LOCAL TIME | not yet |
Expand Down
4 changes: 2 additions & 2 deletions docs/ko/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ sqrt(path(Book::price))

| Function | DSL function |
|--------------------|--------------|
| CURRENT\_DATE | not yet |
| CURRENT\_TIME | not yet |
| CURRENT\_DATE | support |
| CURRENT\_TIME | support |
| CURRENT\_TIMESTAMP | not yet |
| LOCAL DATE | not yet |
| LOCAL TIME | not yet |
Expand Down
22 changes: 22 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 @@ -44,6 +44,8 @@ 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.sql.Date
import java.sql.Time
import kotlin.internal.Exact
import kotlin.internal.LowPriorityInOverloadResolution
import kotlin.reflect.KClass
Expand Down Expand Up @@ -1532,6 +1534,26 @@ open class Jpql : JpqlDsl {
)
}

/**
* Creates an expression that represents the current date.
*
* This is the same as ```CURRENT_DATE```.
*/
@SinceJdsl("3.4.0")
fun currentDate(): Expression<Date> {
return Expressions.currentDate()
}

/**
* Creates an expression that represents the current time.
*
* This is the same as ```CURRENT_TIME```.
*/
@SinceJdsl("3.4.0")
fun currentTime(): Expression<Time> {
return Expressions.currentTime()
}

/**
* Creates an expression that represents predefined database functions and user-defined database functions.
*/
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.Date

class CurrentDateDslTest : WithAssertions {
@Test
fun currentDate() {
// when
val expression = queryPart {
currentDate()
}.toExpression()

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

// then
val expected = Expressions.currentDate()

assertThat(actual).isEqualTo(expected)
}
}
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.Time

class CurrentTimeDslTest : WithAssertions {
@Test
fun currentTime() {
// when
val expression = queryPart {
currentTime()
}.toExpression()

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

// then
val expected = Expressions.currentTime()

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCeiling
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCoalesce
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.JpqlCustomExpression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlDivide
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlEntityType
Expand Down Expand Up @@ -52,6 +54,8 @@ 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.sql.Date
import java.sql.Time
import kotlin.internal.Exact
import kotlin.reflect.KClass

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

/**
* Creates an expression that represents the current date.
*/
@SinceJdsl("3.4.0")
fun currentDate(): Expression<Date> {
return JpqlCurrentDate
}

/**
* Creates an expression that represents the current time.
*/
@SinceJdsl("3.4.0")
fun currentTime(): Expression<Time> {
return JpqlCurrentTime
}
}
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.Date

@Internal
object JpqlCurrentDate : Expression<Date>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl

import com.linecorp.kotlinjdsl.Internal
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression
import java.sql.Time
@Internal
object JpqlCurrentTime : Expression<Time>
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCeiling
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCoalesce
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.JpqlCustomExpression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlDivide
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlEntityType
Expand Down Expand Up @@ -326,6 +328,28 @@ class ExpressionsTest : WithAssertions {
assertThat(actual).isEqualTo(expected)
}

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

// then
val expected = JpqlCurrentDate

assertThat(actual).isEqualTo(expected)
}

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

// then
val expected = JpqlCurrentTime

assertThat(actual).isEqualTo(expected)
}

@Test
fun div() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlCeilingSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlCoalesceSerializer
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.JpqlCustomExpressionSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlCustomPredicateSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlDeleteQuerySerializer
Expand Down Expand Up @@ -277,6 +279,8 @@ private class DefaultModule : JpqlRenderModule {
JpqlCoalesceSerializer(),
JpqlConcatSerializer(),
JpqlCountSerializer(),
JpqlCurrentDateSerializer(),
JpqlCurrentTimeSerializer(),
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.JpqlCurrentDate
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 JpqlCurrentDateSerializer : JpqlSerializer<JpqlCurrentDate> {
override fun handledType(): KClass<JpqlCurrentDate> {
return JpqlCurrentDate::class
}

override fun serialize(part: JpqlCurrentDate, writer: JpqlWriter, context: RenderContext) {
writer.write("CURRENT_DATE")
}
}
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.JpqlCurrentTime
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 JpqlCurrentTimeSerializer : JpqlSerializer<JpqlCurrentTime> {
override fun handledType(): KClass<JpqlCurrentTime> {
return JpqlCurrentTime::class
}

override fun serialize(part: JpqlCurrentTime, writer: JpqlWriter, context: RenderContext) {
writer.write("CURRENT_TIME")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrentDate
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 JpqlCurrentDateSerializerTest : WithAssertions {

private val sut = JpqlCurrentDateSerializer()

@MockK
private lateinit var writer: JpqlWriter

@MockK
private lateinit var serializer: JpqlRenderSerializer

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

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

@Test
fun serialize() {
// given
val part = Expressions.currentDate()
val context = TestRenderContext(serializer)

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

// then
verifySequence {
writer.write("CURRENT_DATE")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrentTime
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 JpqlCurrentTimeSerializerTest : WithAssertions {

private val sut = JpqlCurrentTimeSerializer()

@MockK
private lateinit var writer: JpqlWriter

@MockK
private lateinit var serializer: JpqlRenderSerializer

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

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

@Test
fun serialize() {
// given
val part = Expressions.currentTime()
val context = TestRenderContext(serializer)

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

// then
verifySequence {
writer.write("CURRENT_TIME")
}
}
}

0 comments on commit 6f48b77

Please sign in to comment.