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 INDEX #630

Merged
merged 5 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ locate("Book", path(Book::title))
* CEILING (ceiling)
* EXP (exp)
* FLOOR (floor)
* INDEX (index)
* LN (ln)
* ROUND (round)
* SIGN (sign)
Expand All @@ -240,6 +241,8 @@ exp(path(Book::price))

floor(path(Book::price))

index(path(Book::authors))

ln(path(Book::price))

round(path(Book::price), 2)
Expand All @@ -255,7 +258,6 @@ sqrt(path(Book::price))
|----------|--------------|
| MOD | not yet |
| POWER | not yet |
| INDEX | not yet |

### Datetime functions

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))
* CEILING (ceiling)
* EXP (exp)
* FLOOR (floor)
* INDEX (index)
* LN (ln)
* ROUND (round)
* SIGN (sign)
Expand All @@ -236,6 +237,8 @@ exp(path(Book::price))

floor(path(Book::price))

index(path(Book::authors))

ln(path(Book::price))

round(path(Book::price), 2)
Expand All @@ -251,7 +254,6 @@ sqrt(path(Book::price))
|----------|--------------|
| MOD | not yet |
| POWER | not yet |
| INDEX | not yet |

### Datetime functions

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,22 @@ open class Jpql : JpqlDsl {
return Expressions.floor(value.toExpression())
}

/**
* Creates an expression that represents the index of the value in an ordered list.
*/
@SinceJdsl("3.4.0")
fun <T : Any, V, S : Collection<V>> index(expr: KProperty1<T, @Exact S>): Expression<Int> {
return Expressions.index(Paths.path(expr))
}

/**
* Creates an expression that represents the index of the value in an ordered list.
*/
@SinceJdsl("3.4.0")
fun <T, S : Collection<T>> index(path: Pathable<S>): Expression<Int> {
Copy link
Member

Choose a reason for hiding this comment

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

It seems that the INDEX function can take an alias of join as a parameter, so shouldn't the type of the parameter be Expressionable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ahh, you are right 😄

return Expressions.index(path.toPath())
}

/**
* Creates an expression that represents the natural logarithm of value.
*/
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 IndexDslTest : WithAssertions {
private val expression1 = Paths.path(Book::authors)

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

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

// then
val expected = Expressions.index(
path = Paths.path(Book::authors),
)

assertThat(actual).isEqualTo(expected)
}

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

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

// then
val expected = Expressions.index(
path = expression1,
)

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlUpper
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlValue
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Path
import com.linecorp.kotlinjdsl.querymodel.jpql.predicate.Predicate
import com.linecorp.kotlinjdsl.querymodel.jpql.predicate.impl.JpqlIndex
import com.linecorp.kotlinjdsl.querymodel.jpql.select.SelectQuery
import com.linecorp.kotlinjdsl.querymodel.jpql.select.impl.JpqlSelectQuery
import java.math.BigDecimal
Expand Down Expand Up @@ -251,6 +252,14 @@ object Expressions {
return JpqlFloor(value)
}

/**
* Creates an expression that represents the index of the value in an ordered list.
*/
@SinceJdsl("3.4.0")
fun <T, S : Collection<T>> index(path: Path<S>): Expression<Int> {
return JpqlIndex(path)
}

/**
* Creates an expression that represents the natural logarithm of the value.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.linecorp.kotlinjdsl.querymodel.jpql.predicate.impl

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

@Internal
data class JpqlIndex<T, S : Collection<T>>internal constructor(
val path: Path<S>,
) : Expression<Int>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlUpper
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlValue
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Paths
import com.linecorp.kotlinjdsl.querymodel.jpql.predicate.Predicates
import com.linecorp.kotlinjdsl.querymodel.jpql.predicate.impl.JpqlIndex
import com.linecorp.kotlinjdsl.querymodel.jpql.select.impl.JpqlSelectQuery
import com.linecorp.kotlinjdsl.querymodel.jpql.sort.Sorts
import org.assertj.core.api.WithAssertions
Expand Down Expand Up @@ -418,6 +419,19 @@ class ExpressionsTest : WithAssertions {
assertThat(actual).isEqualTo(expected)
}

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

// then
val expected = JpqlIndex(
path2,
)

assertThat(actual).isEqualTo(expected)
}

@Test
fun ln() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlGreaterThanOrEqua
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlGreaterThanSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlInSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlInSubquerySerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlIndexSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlInnerAssociationFetchJoinSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlInnerAssociationJoinSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlInnerFetchJoinSerializer
Expand Down Expand Up @@ -307,6 +308,7 @@ private class DefaultModule : JpqlRenderModule {
JpqlGreaterThanOrEqualToAnySerializer(),
JpqlGreaterThanOrEqualToSerializer(),
JpqlGreaterThanSerializer(),
JpqlIndexSerializer(),
JpqlInnerAssociationFetchJoinSerializer(),
JpqlInnerAssociationJoinSerializer(),
JpqlInnerFetchJoinSerializer(),
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.predicate.impl.JpqlIndex
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 JpqlIndexSerializer : JpqlSerializer<JpqlIndex<*, *>> {
override fun handledType(): KClass<JpqlIndex<*, *>> {
return JpqlIndex::class
}

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

writer.write("INDEX")

writer.writeParentheses {
delegate.serialize(part.path, 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.path.Paths
import com.linecorp.kotlinjdsl.querymodel.jpql.predicate.impl.JpqlIndex
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 JpqlIndexSerializerTest : WithAssertions {
private val sut = JpqlIndexSerializer()

@MockK
private lateinit var writer: JpqlWriter

@MockK
private lateinit var serializer: JpqlRenderSerializer

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

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

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

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

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

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