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 Arithmetic function ROUND #615

Merged
merged 10 commits into from
Jan 31, 2024
6 changes: 5 additions & 1 deletion docs/en/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,11 @@ Kotlin JDSL provides a series of functions to support built-in functions in JPA.
| LOCATE | support |

### Arithmetic functions
* round

```kotlin
round(path(Book::price), Expressions.value(Int::class))
```

| Function | DSL function |
|----------|--------------|
Expand All @@ -213,7 +218,6 @@ Kotlin JDSL provides a series of functions to support built-in functions in JPA.
| LN | not yet |
| MOD | not yet |
| POWER | not yet |
| ROUND | not yet |
| SIGN | not yet |
| SQRT | not yet |
| SIZE | not yet |
Expand Down
6 changes: 5 additions & 1 deletion docs/ko/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,11 @@ Kotlin JDSL은 JPA에서 제공하는 여러 함수들을 지원하기 위함
| LOCATE | support |

### Arithmetic functions
* round

```kotlin
round(path(Book::price), Expressions.value(Int::class))
```

| Function | DSL function |
|----------|--------------|
Expand All @@ -211,7 +216,6 @@ Kotlin JDSL은 JPA에서 제공하는 여러 함수들을 지원하기 위함
| LN | not yet |
| MOD | not yet |
| POWER | not yet |
| ROUND | not yet |
| SIGN | not yet |
| SQRT | not yet |
| SIZE | not yet |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,22 @@ open class Jpql : JpqlDsl {
)
}

/**
* Creates an expression that represents the rounding of the specified property's value to a specified scale.
Copy link
Member

Choose a reason for hiding this comment

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

I think it would be great to at least value instead of property's value, since not only the value of the property can be passed as a parameter to round, but also the literal.

*/
@SinceJdsl("3.4.0")
fun <T : Any, V : Number> round(expr: KProperty1<T, @Exact V>, scale: Expression<Int>): Expression<Double> {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the ROUND is a primary function, so I think it would be better to have the function located below the DIV.

return Expressions.round(Paths.path(expr), scale.toExpression())
}

/**
* Creates an expression that represents the rounding of the specified property's value to a specified scale.
*/
@SinceJdsl("3.4.0")
fun <T : Number> round(value: Expressionable<T>, scale: Expressionable<Int>): Expression<Double> {
Copy link
Member

Choose a reason for hiding this comment

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

In the jakarta specification, the return type of ROUND is the same type as the first argument: value.

Could you test this to make sure it's really correct? I think it would be okay to test with Int, Double, and BigDecimal only.

Copy link
Member

Choose a reason for hiding this comment

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

image

return Expressions.round(value.toExpression(), scale.toExpression())
}

/**
* Creates an expression that represents the plus of values.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.linecorp.kotlinjdsl.dsl.jpql.expression

import com.linecorp.kotlinjdsl.dsl.jpql.entity.book.Book
import com.linecorp.kotlinjdsl.dsl.jpql.queryPart
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Paths
import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.Test

class RoundDslTest : WithAssertions {
private val expression1 = Paths.path(Book::salePrice)
private val intExpression1: Expression<Int> = Expressions.value(3)
private val int1 = 3

@Test
fun `round() with a expression`() {
// when
val expression = queryPart {
round(expression1, intExpression1)
}.toExpression()

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

// then
val expected = Expressions.round(
value = expression1,
scale = intExpression1,
)

assertThat(actual).isEqualTo(expected)
}

@Test
fun `round() with a property`() {
// when
val expression = queryPart {
round(Book::price, intExpression1)
}.toExpression()

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

// then
val expected = Expressions.round(
value = Paths.path(Book::price),
scale = Expressions.value(int1),
)

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlTrimLeading
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlTrimTrailing
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlUpper
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlValue
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlRound
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Path
import com.linecorp.kotlinjdsl.querymodel.jpql.predicate.Predicate
import com.linecorp.kotlinjdsl.querymodel.jpql.select.SelectQuery
Expand Down Expand Up @@ -246,6 +247,14 @@ object Expressions {
return JpqlAvg(distinct, expr)
}

/**
* Creates an expression that represents the rounding of the specified property's value to a specified scale.
*/
@SinceJdsl("3.4.0")
fun <T : Number> round(value: Expression<T>, scale: Expression<Int>): Expression<Double> {
return JpqlRound(value, scale)
}

/**
* Creates an expression that represents the sum of values.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl

import com.linecorp.kotlinjdsl.Internal
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression

/**
* Expression that calculates the rounding of a numeric [value] to a specified [scale].
*/
@Internal
data class JpqlRound<T: Number> internal constructor(
val value: Expression<T>,
val scale: Expression<Int>,
) : Expression<Double> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlNullIf
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlParam
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlPathType
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlPlus
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlRound
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlSubquery
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlSubstring
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlSum
Expand Down Expand Up @@ -392,6 +393,20 @@ class ExpressionsTest : WithAssertions {
assertThat(actual).isEqualTo(expected)
}

@Test
fun round() {
// when
val actual = Expressions.round(doubleExpression1, intExpression1)

// then
val expected = JpqlRound(
doubleExpression1,
intExpression1
)

assertThat(actual).isEqualTo(expected)
}

@ParameterizedTest
@ValueSource(booleans = [true, false])
fun `sum() with an int expression`(distinct: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlPathTreatSerializ
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlPathTypeSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlPlusSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlPredicateParenthesesSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlRoundSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlSelectQuerySerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlSortSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlSubquerySerializer
Expand Down Expand Up @@ -338,6 +339,7 @@ private class DefaultModule : JpqlRenderModule {
JpqlPathTypeSerializer(),
JpqlPlusSerializer(),
JpqlPredicateParenthesesSerializer(),
JpqlRoundSerializer(),
JpqlSelectQuerySerializer(),
JpqlSortSerializer(),
JpqlSubquerySerializer(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

import com.linecorp.kotlinjdsl.Internal
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlRound
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 JpqlRoundSerializer : JpqlSerializer<JpqlRound<*>>{
override fun handledType(): KClass<JpqlRound<*>> {
return JpqlRound::class
}

override fun serialize(part: JpqlRound<*>, writer: JpqlWriter, context: RenderContext) {
val delegate = context.getValue(JpqlRenderSerializer)

writer.write("ROUND")

writer.writeParentheses {
delegate.serialize(part.value, writer, context)

writer.write(", ")
delegate.serialize(part.scale, writer, context)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlRound
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Paths
import com.linecorp.kotlinjdsl.render.TestRenderContext
import com.linecorp.kotlinjdsl.render.jpql.entity.book.Book
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.Assertions.assertThat
import org.junit.jupiter.api.Test

@JpqlSerializerTest
class JpqlRoundSerializerTest {
private val sut = JpqlRoundSerializer()

@MockK
private lateinit var writer: JpqlWriter

@MockK
private lateinit var serializer: JpqlRenderSerializer

private val expression1 = Paths.path(Book::price)
private val expression2 = Expressions.value(3)

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

// then
assertThat(actual).isEqualTo(JpqlRound::class)
}
@Test
fun serialize() {
// given
val part = Expressions.round(
value = expression1,
scale = expression2
)
val context = TestRenderContext(serializer)

// when
sut.serialize(part as JpqlRound<*>, writer, context)

// then
verifySequence {
writer.write("ROUND")
writer.writeParentheses(any())
serializer.serialize(expression1, writer, context)
writer.write(", ")
serializer.serialize(expression2, writer, context)
}
}
}
Loading