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 EXP #620

Merged
merged 4 commits into from
Feb 5, 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
4 changes: 3 additions & 1 deletion docs/en/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ Use the following functions to build arithmetic functions:

* ABS (abs)
* CEILING (ceiling)
* EXP (exp)
* FLOOR (floor)
* ROUND (round)
* SQRT (sqrt)
Expand All @@ -234,6 +235,8 @@ abs(path(Book::price))

ceiling(path(Book::price))

exp(path(Book::price))

floor(path(Book::price))

round(path(Book::price), 2)
Expand All @@ -243,7 +246,6 @@ sqrt(path(Book::price))

| Function | DSL function |
|----------|--------------|
| EXP | not yet |
| FLOOR | not yet |
| LN | not yet |
| MOD | not yet |
Expand Down
4 changes: 3 additions & 1 deletion docs/ko/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ locate("Book", path(Book::title))

* ABS (abs)
* CEILING (ceiling)
* EXP (exp)
* FLOOR (floor)
* ROUND (round)
* SQRT (sqrt)
Expand All @@ -230,6 +231,8 @@ abs(path(Book::price))

ceiling(path(Book::price))

exp(path(Book::price))

floor(path(Book::price))

round(path(Book::price), 2)
Expand All @@ -239,7 +242,6 @@ sqrt(path(Book::price))

| Function | DSL function |
|----------|--------------|
| EXP | not yet |
| FLOOR | not yet |
| LN | not yet |
| MOD | not yet |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,22 @@ open class Jpql : JpqlDsl {
return Expressions.ceiling(value.toExpression())
}

/**
* Creates an expression that represents the exponential value.
*/
@SinceJdsl("3.4.0")
fun <T : Any, V : Number> exp(expr: KProperty1<T, @Exact V>): Expression<Double> {
return Expressions.exp(Paths.path(expr))
}

/**
* Creates an expression that represents the exponential value.
*/
@SinceJdsl("3.4.0")
fun <T : Number> exp(value: Expressionable<T>): Expression<Double> {
return Expressions.exp(value.toExpression())
}

/**
* Creates an expression that is enclosed in floor.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 ExpDslTest : WithAssertions {
private val expression1 = Paths.path(Book::salePrice)

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

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

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

assertThat(actual).isEqualTo(expected)
}

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

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

// then
val expected = Expressions.exp(
value = expression1,
)

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCount
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
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlExp
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlExpression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlExpressionParentheses
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFloor
Expand Down Expand Up @@ -227,6 +228,14 @@ object Expressions {
return JpqlCeiling(value)
}

/**
* Creates an expression that represents the exponential value.
*/
@SinceJdsl("3.4.0")
fun <T : Number> exp(value: Expression<T>): Expression<Double> {
return JpqlExp(value)
}

/**
* Creates an expression that is enclosed in floor.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl

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

/**
* Expression that applies exponential function to [value].
*/
@Internal
data class JpqlExp<T : Number> internal constructor(
val value: Expression<T>,
) : Expression<Double>
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlCount
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
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlExp
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlExpression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlExpressionParentheses
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFloor
Expand Down Expand Up @@ -363,6 +364,19 @@ class ExpressionsTest : WithAssertions {
assertThat(actual).isEqualTo(expected)
}

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

// then
val expected = JpqlExp(
doubleExpression1,
)

assertThat(actual).isEqualTo(expected)
}

@Test
fun floor() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlEqualAllSerialize
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlEqualAnySerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlEqualSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlExistsSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlExpSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlExpressionParenthesesSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlExpressionSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlFloorSerializer
Expand Down Expand Up @@ -289,6 +290,7 @@ private class DefaultModule : JpqlRenderModule {
JpqlExistsSerializer(),
JpqlExpressionParenthesesSerializer(),
JpqlExpressionSerializer(),
JpqlExpSerializer(),
JpqlFloorSerializer(),
JpqlFunctionExpressionSerializer(),
JpqlFunctionPredicateSerializer(),
Expand Down
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.JpqlExp
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 JpqlExpSerializer : JpqlSerializer<JpqlExp<*>> {
override fun handledType(): KClass<JpqlExp<*>> {
return JpqlExp::class
}

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

writer.write("EXP")

writer.writeParentheses {
delegate.serialize(part.value, writer, context)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlExp
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.WithAssertions
import org.junit.jupiter.api.Test

@JpqlSerializerTest
class JpqlExpSerializerTest : WithAssertions {
private val sut = JpqlExpSerializer()

@MockK
private lateinit var writer: JpqlWriter

@MockK
private lateinit var serializer: JpqlRenderSerializer

private val expression1 = Paths.path(Book::price)

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

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

@Test
fun serialize() {
// given
val part = Expressions.exp(
value = expression1,
)
val context = TestRenderContext(serializer)

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

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