-
Notifications
You must be signed in to change notification settings - Fork 94
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_DATE & CURRENT_TIME #624
Changes from 2 commits
c1219b4
c963cc7
8e2574d
a237af3
4ad8cab
491a1a1
0555264
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.time.LocalDate | ||
import java.time.LocalTime | ||
import kotlin.internal.Exact | ||
import kotlin.internal.LowPriorityInOverloadResolution | ||
import kotlin.reflect.KClass | ||
|
@@ -486,6 +488,26 @@ open class Jpql : JpqlDsl { | |
return Expressions.times(this.toExpression(), value.toExpression()) | ||
} | ||
|
||
/** | ||
* Creates an expression that represents the current date. | ||
* | ||
* This is the same as ```CURRENT_DATE```. | ||
*/ | ||
@SinceJdsl("3.4.0") | ||
fun currentDate(): Expression<LocalDate> { | ||
return Expressions.currentDate() | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the Criteria API, the return type of The Jakarta specification also specifies that the return type is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the same reason as above, I would like to request that the return type of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i got it, I'll change currentTime & currentDate |
||
|
||
/** | ||
* Creates an expression that represents the current time. | ||
* | ||
* This is the same as ```CURRENT_TIME```. | ||
*/ | ||
@SinceJdsl("3.4.0") | ||
fun currentTime(): Expression<LocalTime> { | ||
return Expressions.currentTime() | ||
} | ||
|
||
/** | ||
* Creates an expression that represents the divide of values. | ||
* The values are each enclosed in parentheses. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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.time.LocalDate | ||
|
||
class CurrentDateDslTest : WithAssertions { | ||
@Test | ||
fun `currentDate() with a property`() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 특정 property나 expression을 가지고 테스트하는 코드가 아니므로 CurrentTimeDslTest 쪽도 비슷하니 같이 수정해주시면 좋을것같습니다. |
||
// when | ||
val expression = queryPart { | ||
currentDate() | ||
}.toExpression() | ||
|
||
val actual: Expression<LocalDate> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.currentDate() | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `currentDate() with a expression`() { | ||
// when | ||
val expression = queryPart { | ||
currentDate() | ||
}.toExpression() | ||
|
||
val actual: Expression<LocalDate> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.currentDate() | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 위 테스트와 중복된 내용이므로 제거해도 될것같아요. |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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.time.LocalTime | ||
|
||
class CurrentTimeDslTest : WithAssertions { | ||
@Test | ||
fun `currentDate() with a property`() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 테스트 함수 이름이 테스트 대상(currentTime)과 일치하지 않습니다. 알맞게 변경해주시면 좋을것같습니다. |
||
// when | ||
val expression = queryPart { | ||
currentTime() | ||
}.toExpression() | ||
|
||
val actual: Expression<LocalTime> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.currentTime() | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
|
||
@Test | ||
fun `currentDate() with a expression`() { | ||
// when | ||
val expression = queryPart { | ||
currentTime() | ||
}.toExpression() | ||
|
||
val actual: Expression<LocalTime> = expression // for type check | ||
|
||
// then | ||
val expected = Expressions.currentTime() | ||
|
||
assertThat(actual).isEqualTo(expected) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression | ||
import java.time.LocalDate | ||
import java.time.LocalTime | ||
|
||
/** | ||
* Expression that represents the current date & time. | ||
*/ | ||
@Internal | ||
sealed interface JpqlCurrent<T : Any> : Expression<T> { | ||
|
||
object CurrentDate : JpqlCurrent<LocalDate> | ||
|
||
object CurrentTime : JpqlCurrent<LocalTime> | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrent | ||
import com.linecorp.kotlinjdsl.render.RenderContext | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter | ||
import kotlin.reflect.KClass | ||
|
||
@Internal | ||
class JpqlCurrentDateSerializer : JpqlSerializer<JpqlCurrent.CurrentDate> { | ||
override fun handledType(): KClass<JpqlCurrent.CurrentDate> { | ||
return JpqlCurrent.CurrentDate::class | ||
} | ||
|
||
override fun serialize(part: JpqlCurrent.CurrentDate, writer: JpqlWriter, context: RenderContext) { | ||
val delegate = context.getValue(JpqlRenderSerializer) | ||
|
||
writer.write("CURRENT_DATE") | ||
|
||
writer.writeParentheses { | ||
delegate.serialize(part, writer, context) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
JpqlCurrentDateSerializer의 경우 괄호와 추가적인 serialize 호출이 필요가 없어보이므로
각 SerializerTest도 수정된 내용에 맞게 다시 봐주시면 좋을것같습니다! |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl | ||
|
||
import com.linecorp.kotlinjdsl.Internal | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrent | ||
import com.linecorp.kotlinjdsl.render.RenderContext | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlRenderSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.serializer.JpqlSerializer | ||
import com.linecorp.kotlinjdsl.render.jpql.writer.JpqlWriter | ||
import kotlin.reflect.KClass | ||
|
||
@Internal | ||
class JpqlCurrentTimeSerializer : JpqlSerializer<JpqlCurrent.CurrentTime> { | ||
override fun handledType(): KClass<JpqlCurrent.CurrentTime> { | ||
return JpqlCurrent.CurrentTime::class | ||
} | ||
|
||
override fun serialize(part: JpqlCurrent.CurrentTime, writer: JpqlWriter, context: RenderContext) { | ||
val delegate = context.getValue(JpqlRenderSerializer) | ||
|
||
writer.write("CURRENT_TIME") | ||
|
||
writer.writeParentheses { | ||
delegate.serialize(part, writer, context) | ||
} | ||
} | ||
} |
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.Expressions.currentDate | ||
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCurrent | ||
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 | ||
|
||
private val expression = currentDate() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be nice if you could use |
||
|
||
@Test | ||
fun handledType() { // when | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 코드 스타일을 맞추기위해 |
||
val actual = sut.handledType() | ||
|
||
// then | ||
assertThat(actual).isEqualTo(JpqlCurrent.CurrentDate::class) | ||
} | ||
|
||
@Test | ||
fun serialize() { | ||
// given | ||
val part = Expressions.currentDate() | ||
val context = TestRenderContext(serializer) | ||
|
||
// when | ||
sut.serialize(part as JpqlCurrent.CurrentDate, writer, context) | ||
|
||
// then | ||
verifySequence { | ||
writer.write("CURRENT_DATE") | ||
writer.writeParentheses(any()) | ||
serializer.serialize(expression, writer, context) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
represents
와the
사이에 불필요한 whitespace가 하나 더 포함 되어있습니다.currentTime()의 주석도 둥일하니 같이 수정해주시면 좋을것같습니다.