Skip to content

Commit

Permalink
Merge pull request #621 from jbl428/feat/ln
Browse files Browse the repository at this point in the history
Support Arithmetic function LN
  • Loading branch information
shouwn authored Feb 5, 2024
2 parents d3fa944 + a6767b1 commit 0d9febd
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 4 deletions.
5 changes: 3 additions & 2 deletions docs/en/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ Use the following functions to build arithmetic functions:
* CEILING (ceiling)
* EXP (exp)
* FLOOR (floor)
* LN (ln)
* ROUND (round)
* SIGN (sign)
* SQRT (sqrt)
Expand All @@ -240,6 +241,8 @@ exp(path(Book::price))

floor(path(Book::price))

ln(path(Book::price))

round(path(Book::price), 2)

sign(path(Book::price))
Expand All @@ -249,8 +252,6 @@ sqrt(path(Book::price))

| Function | DSL function |
|----------|--------------|
| FLOOR | not yet |
| LN | not yet |
| MOD | not yet |
| POWER | not yet |
| SIZE | not yet |
Expand Down
5 changes: 3 additions & 2 deletions docs/ko/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ locate("Book", path(Book::title))
* CEILING (ceiling)
* EXP (exp)
* FLOOR (floor)
* LN (ln)
* ROUND (round)
* SIGN (sign)
* SQRT (sqrt)
Expand All @@ -236,6 +237,8 @@ exp(path(Book::price))

floor(path(Book::price))

ln(path(Book::price))

round(path(Book::price), 2)

sign(path(Book::price))
Expand All @@ -245,8 +248,6 @@ sqrt(path(Book::price))

| Function | DSL function |
|----------|--------------|
| FLOOR | not yet |
| LN | not yet |
| MOD | not yet |
| POWER | not yet |
| SIZE | not yet |
Expand Down
16 changes: 16 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 @@ -600,6 +600,22 @@ open class Jpql : JpqlDsl {
return Expressions.floor(value.toExpression())
}

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

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

/**
* Creates an expression that represents the rounding of the value to a specified scale.
*/
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 LnDslTest : WithAssertions {
private val expression1 = Paths.path(Book::salePrice)

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

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

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

assertThat(actual).isEqualTo(expected)
}

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

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

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

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFloor
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFunctionExpression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLength
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLiteral
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLn
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocate
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLower
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMax
Expand Down Expand Up @@ -245,6 +246,14 @@ object Expressions {
return JpqlFloor(value)
}

/**
* Creates an expression that represents the natural logarithm of the value.
*/
@SinceJdsl("3.4.0")
fun <T : Number> ln(value: Expression<T>): Expression<Double> {
return JpqlLn(value)
}

/**
* Creates an expression that represents the rounding of the specified value to a specified scale.
*/
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 natural logarithm function to [value].
*/
@Internal
data class JpqlLn<T : Number> internal constructor(
val value: Expression<T>,
) : Expression<Double>
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFloor
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFunctionExpression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLength
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLiteral
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLn
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLocate
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLower
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMax
Expand Down Expand Up @@ -391,6 +392,19 @@ class ExpressionsTest : WithAssertions {
assertThat(actual).isEqualTo(expected)
}

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

// then
val expected = JpqlLn(
doubleExpression1,
)

assertThat(actual).isEqualTo(expected)
}

@Test
fun round() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLessThanOrEqualTo
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLessThanSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLikeSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLiteralSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLnSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLocateSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLowerSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlMaxSerializer
Expand Down Expand Up @@ -325,6 +326,7 @@ private class DefaultModule : JpqlRenderModule {
JpqlLessThanSerializer(),
JpqlLikeSerializer(),
JpqlLiteralSerializer(),
JpqlLnSerializer(),
JpqlLocateSerializer(),
JpqlLowerSerializer(),
JpqlMaxSerializer(),
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.JpqlLn
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 JpqlLnSerializer : JpqlSerializer<JpqlLn<*>> {
override fun handledType(): KClass<JpqlLn<*>> {
return JpqlLn::class
}

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

writer.write("LN")

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.JpqlLn
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 JpqlLnSerializerTest : WithAssertions {
private val sut = JpqlLnSerializer()

@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(JpqlLn::class)
}

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

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

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

0 comments on commit 0d9febd

Please sign in to comment.