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 'mod' arithmetic function #648

Merged
merged 10 commits into from
Feb 21, 2024
2 changes: 2 additions & 0 deletions docs/en/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ sqrt(path(Book::price))
round(path(Book::price), 2)

size(path(Book::authors))

mod(path(Employee::age), 3)
```

| Function | DSL function |
Expand Down
3 changes: 2 additions & 1 deletion docs/ko/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,12 @@ sqrt(path(Book::price))
round(path(Book::price), 2)

size(path(Book::authors))

mod(path(Employee::age), 3)
```

| Function | DSL function |
|----------|--------------|
| MOD | not yet |
| POWER | not yet |

### Datetime functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,38 @@ open class Jpql : JpqlDsl {
return Expressions.round(value.toExpression(), scale.toExpression())
}

/**
* Creates an expression that represents the mod of values.
*/
@SinceJdsl("3.4.0")
fun mod(value1: Expressionable<Int>, value2: Expressionable<Int>): Expression<Int> {
return Expressions.mod(value1.toExpression(), value2.toExpression())
}

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

/**
* Creates an expression that represents the mod of values.
*/
@SinceJdsl("3.4.0")
fun mod(value1: Expressionable<Int>, value2: Int): Expression<Int> {
return Expressions.mod(value1.toExpression(), Expressions.value(value2))
}

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

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

import com.linecorp.kotlinjdsl.dsl.jpql.entity.employee.Employee
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 ModDslTest : WithAssertions {
private val path = Paths.path(Employee::age)
private val intExpression1 = Expressions.value(2)

private val int1 = 2

@Test
fun `mod() with a property`() {
// when
val expression1 = queryPart {
mod(Employee::age, int1)
}.toExpression()

val expression2 = queryPart {
mod(Employee::age, intExpression1)
}.toExpression()

val actual1: Expression<Int> = expression1 // for type check
val actual2: Expression<Int> = expression2 // for type check

// then
val expected1 = Expressions.mod(
value1 = Paths.path(Employee::age),
value2 = Expressions.value(int1),
)

val expected2 = Expressions.mod(
value1 = Paths.path(Employee::age),
value2 = intExpression1,
)

assertThat(actual1).isEqualTo(expected1)
assertThat(actual2).isEqualTo(expected2)
}

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

val expression2 = queryPart {
mod(path, intExpression1)
}.toExpression()

val actual1: Expression<Int> = expression1 // for type check
val actual2: Expression<Int> = expression2 // for type check

// then
val expected1 = Expressions.mod(
value1 = path,
value2 = Expressions.value(int1),
)

val expected2 = Expressions.mod(
value1 = path,
value2 = intExpression1,
)

assertThat(actual1).isEqualTo(expected1)
assertThat(actual2).isEqualTo(expected2)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ abstract class Employee(
val employeeId: Long,
val name: String,
val nickname: String?,
val age: Int,
val phone: String,
val address: EmployeeAddress,
val departments: MutableSet<EmployeeDepartment>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class FullTimeEmployee(
employeeId: Long,
name: String,
nickname: String?,
age: Int,
phone: String,
address: EmployeeAddress,
departments: MutableSet<EmployeeDepartment>,
Expand All @@ -14,6 +15,7 @@ class FullTimeEmployee(
employeeId = employeeId,
name = name,
nickname = nickname,
age = age,
phone = phone,
address = address,
departments = departments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class PartTimeEmployee(
employeeId: Long,
name: String,
nickname: String?,
age: Int,
phone: String,
address: EmployeeAddress,
departments: MutableSet<EmployeeDepartment>,
Expand All @@ -14,6 +15,7 @@ class PartTimeEmployee(
employeeId = employeeId,
name = name,
nickname = nickname,
age = age,
phone = phone,
address = address,
departments = departments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLower
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMax
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMin
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMinus
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMod
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlNew
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlNull
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlNullIf
Expand Down Expand Up @@ -300,6 +301,14 @@ object Expressions {
return JpqlRound(value, scale)
}

/**
* Creates an expression that represents the mod of values.
*/
@SinceJdsl("3.4.0")
fun mod(value1: Expression<Int>, value2: Expression<Int>): Expression<Int> {
return JpqlMod(value1, value2)
}

/**
* Creates an expression that represents the number of elements of the collection.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
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 mod of a integer [value1] to a specified integer [value2].
*/
@Internal
data class JpqlMod internal constructor(
val value1: Expression<Int>,
val value2: Expression<Int>,
) : Expression<Int>
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLower
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMax
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMin
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMinus
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMod
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlNew
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlNull
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlNullIf
Expand Down Expand Up @@ -467,6 +468,20 @@ class ExpressionsTest : WithAssertions {
assertThat(actual).isEqualTo(expected)
}

@Test
fun mod() {
// when
val actual = Expressions.mod(intExpression1, intExpression2)

// then
val expected = JpqlMod(
intExpression1,
intExpression2,
)

assertThat(actual).isEqualTo(expected)
}

@Test
fun size() {
// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLowerSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlMaxSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlMinSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlMinusSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlModSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlNewSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlNotBetweenSerializer
import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlNotEqualAllSerializer
Expand Down Expand Up @@ -367,6 +368,7 @@ private class DefaultModule : JpqlRenderModule {
JpqlPlusSerializer(),
JpqlPredicateParenthesesSerializer(),
JpqlRoundSerializer(),
JpqlModSerializer(),
JpqlSelectQuerySerializer(),
JpqlSignSerializer(),
JpqlSizeSerializer(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.linecorp.kotlinjdsl.render.jpql.serializer.impl

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

override fun serialize(part: JpqlMod, writer: JpqlWriter, context: RenderContext) {
val delegate = context.getValue(JpqlRenderSerializer)

writer.write("MOD")

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

writer.write(",")
writer.write(" ")

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

import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlMod
import com.linecorp.kotlinjdsl.querymodel.jpql.path.Paths
import com.linecorp.kotlinjdsl.render.TestRenderContext
import com.linecorp.kotlinjdsl.render.jpql.entity.employee.Employee
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 JpqlModSerializerTest {
private val sut = JpqlModSerializer()

@MockK
private lateinit var writer: JpqlWriter

@MockK
private lateinit var serializer: JpqlRenderSerializer

private val expression1 = Paths.path(Employee::age)
private val expression2 = Expressions.value(3)

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

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

@Test
fun serialize() {
// given
val part = Expressions.mod(
value1 = expression1,
value2 = expression2,
)
val context = TestRenderContext(serializer)

// when
sut.serialize(part as JpqlMod, writer, context)

// then
verifySequence {
writer.write("MOD")
writer.writeParentheses(any())
serializer.serialize(expression1, writer, context)
writer.write(",")
writer.write(" ")
serializer.serialize(expression2, writer, context)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ abstract class Employee(
val employeeId: Long,
val name: String,
val nickname: String?,
val age: Int,
val phone: String,
val address: EmployeeAddress,
val departments: MutableSet<EmployeeDepartment>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class FullTimeEmployee(
employeeId: Long,
name: String,
nickname: String?,
age: Int,
phone: String,
address: EmployeeAddress,
departments: MutableSet<EmployeeDepartment>,
Expand All @@ -14,6 +15,7 @@ class FullTimeEmployee(
employeeId = employeeId,
name = name,
nickname = nickname,
age = age,
phone = phone,
address = address,
departments = departments,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class PartTimeEmployee(
employeeId: Long,
name: String,
nickname: String?,
age: Int,
phone: String,
address: EmployeeAddress,
departments: MutableSet<EmployeeDepartment>,
Expand All @@ -14,6 +15,7 @@ class PartTimeEmployee(
employeeId = employeeId,
name = name,
nickname = nickname,
age = age,
phone = phone,
address = address,
departments = departments,
Expand Down