Skip to content

Commit

Permalink
Merge branch 'develop' into add-string-functions-upper
Browse files Browse the repository at this point in the history
  • Loading branch information
shouwn authored Oct 24, 2023
2 parents 0b01c6d + b7fe55d commit 5b8124a
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version: 16.x
- name: Install dependencies
Expand Down
2 changes: 1 addition & 1 deletion docs/en/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ Kotlin JDSL provides functions to support built-in functions in JPA.
| CONCAT | not yet |
| SUBSTRING | not yet |
| TRIM | not yet |
| LOWER | not yet |
| LOWER | support |
| UPPER | not yet |
| LENGTH | support |
| LOCATE | support |
Expand Down
2 changes: 1 addition & 1 deletion docs/ko/jpql-with-kotlin-jdsl/expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Kotlin JDSL은 JPA에서 제공하는 여러 함수들을 지원하기 위함
| CONCAT | not yet |
| SUBSTRING | not yet |
| TRIM | not yet |
| LOWER | not yet |
| LOWER | support |
| UPPER | not yet |
| LENGTH | support |
| LOCATE | support |
Expand Down
130 changes: 16 additions & 114 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 @@ -1057,6 +1057,22 @@ open class Jpql : JpqlDsl {
fun upper(string: Expressionable<String>): Expression<String> {
return Expressions.upper(string.toExpression())
}

/**
* Creates an expression that represents the string in lowercase.
*/
@SinceJdsl("3.0.0")
fun lower(value: String): Expression<String> {
return Expressions.lower(Expressions.value(value))
}

/**
* Creates an expression that represents the string in lowercase.
*/
@SinceJdsl("3.0.0")
fun lower(value: Expressionable<String>): Expression<String> {
return Expressions.lower(value.toExpression())
}

/**
* Creates an expression that represents the length of the string as an integer.
Expand Down Expand Up @@ -2498,32 +2514,6 @@ open class Jpql : JpqlDsl {
)
}

/**
* Creates a select clause in a select query.
*/
@Deprecated(
"""
The KClass parameter makes it confusing whether to specify the return type as generic or KClass
in the select function signature.
Therefore, Kotlin JDSL will remove select functions that accept the KClass parameter
to specify the return type as generic only.
Since these will be removed in the 3.0.0 release,
please use select functions that specify the return type via generic.
""",
ReplaceWith("select<T>(expr, *exprs)"),
)
fun <T : Any> select(
returnType: KClass<T>,
expr: Expressionable<*>,
vararg exprs: Expressionable<*>,
): SelectQueryFromStep<T> {
return SelectQueryFromStepDsl(
returnType,
distinct = false,
listOf(expr.toExpression()) + exprs.map { it.toExpression() },
)
}

/**
* Creates a select clause in a select query.
*/
Expand Down Expand Up @@ -2553,32 +2543,6 @@ open class Jpql : JpqlDsl {
)
}

/**
* Creates a select clause in a select query.
*/
@Deprecated(
"""
The KClass parameter makes it confusing whether to specify the return type as generic or KClass
in the select function signature.
Therefore, Kotlin JDSL will remove select functions that accept the KClass parameter
to specify the return type as generic only.
Since these will be removed in the 3.0.0 release,
please use select functions that specify the return type via generic.
""",
ReplaceWith("selectDistinct<T>(expr, *exprs)"),
)
fun <T : Any> selectDistinct(
returnType: KClass<T>,
expr: Expressionable<*>,
vararg exprs: Expressionable<*>,
): SelectQueryFromStep<T> {
return SelectQueryFromStepDsl(
returnType,
distinct = true,
listOf(expr.toExpression()) + exprs.map { it.toExpression() },
)
}

/**
* Creates a select clause with the DTO projection in a select query.
*/
Expand All @@ -2599,37 +2563,6 @@ open class Jpql : JpqlDsl {
)
}

/**
* Creates a select clause with the DTO projection in a select query.
*/
@Deprecated(
"""
The KClass parameter makes it confusing whether to specify the return type as generic or KClass
in the select function signature.
Therefore, Kotlin JDSL will remove select functions that accept the KClass parameter
to specify the return type as generic only.
Since these will be removed in the 3.0.0 release,
please use select functions that specify the return type via generic.
""",
ReplaceWith("selectNew<T>(expr, *exprs)"),
)
fun <T : Any> selectNew(
returnType: KClass<T>,
expr: Expressionable<*>,
vararg exprs: Expressionable<*>,
): SelectQueryFromStep<T> {
return SelectQueryFromStepDsl(
returnType = returnType,
distinct = false,
select = listOf(
Expressions.new(
returnType,
listOf(expr.toExpression()) + exprs.map { it.toExpression() },
),
),
)
}

/**
* Creates a select clause with the DTO projection in a select query.
*/
Expand All @@ -2650,37 +2583,6 @@ open class Jpql : JpqlDsl {
)
}

/**
* Creates a select clause with the DTO projection in a select query.
*/
@Deprecated(
"""
The KClass parameter makes it confusing whether to specify the return type as generic or KClass
in the select function signature.
Therefore, Kotlin JDSL will remove select functions that accept the KClass parameter
to specify the return type as generic only.
Since these will be removed in the 3.0.0 release,
please use select functions that specify the return type via generic.
""",
ReplaceWith("selectDistinctNew<T>(expr, *exprs)"),
)
fun <T : Any> selectDistinctNew(
returnType: KClass<T>,
expr: Expressionable<*>,
vararg exprs: Expressionable<*>,
): SelectQueryFromStep<T> {
return SelectQueryFromStepDsl(
returnType = returnType,
distinct = true,
select = listOf(
Expressions.new(
returnType,
listOf(expr.toExpression()) + exprs.map { it.toExpression() },
),
),
)
}

/**
* Creates an update clause in an update query.
*/
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.queryPart
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expression
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import org.assertj.core.api.WithAssertions
import org.junit.jupiter.api.Test

class LowerDslTest : WithAssertions {
private val string1 = "string1"

private val stringExpression1 = Expressions.value("string1")

@Test
fun `lower() with a string`() {
// when
val expression = queryPart {
lower(string1)
}.toExpression()

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

// then
val expected = Expressions.lower(
Expressions.value(string1),
)

assertThat(actual).isEqualTo(expected)
}

@Test
fun `lower() with a string expression`() {
// when
val expression = queryPart {
lower(stringExpression1)
}.toExpression()

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

// then
val expected = Expressions.lower(
stringExpression1,
)

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFunction
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.JpqlLocate
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
Expand Down Expand Up @@ -423,6 +424,14 @@ object Expressions {
return JpqlUpper(string)
}

/**
* Creates an expression that represents the string in lowercase.
*/
@SinceJdsl("3.0.0")
fun lower(value: Expression<String>): Expression<String> {
return JpqlLower(value)
}

/**
* Creates an expression that represents the length of the string as an integer.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl

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

@Internal
data class JpqlLower internal constructor(
val value: Expression<String>,
) : Expression<String>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlFunction
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.JpqlLocate
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
Expand Down Expand Up @@ -611,6 +612,19 @@ class ExpressionsTest : WithAssertions {

assertThat(actual).isEqualTo(expected)
}

@Test
fun lower() {
// when
val actual = Expressions.lower(stringExpression1)

// then
val expected = JpqlLower(
stringExpression1,
)

assertThat(actual).isEqualTo(expected)
}

@Test
fun length() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ import com.linecorp.kotlinjdsl.render.jpql.serializer.impl.JpqlLessThanSerialize
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.JpqlLocateSerializer
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
Expand Down Expand Up @@ -300,6 +301,7 @@ private class DefaultModule : JpqlRenderModule {
JpqlLikeSerializer(),
JpqlLiteralSerializer(),
JpqlLocateSerializer(),
JpqlLowerSerializer(),
JpqlMaxSerializer(),
JpqlMinSerializer(),
JpqlMinusSerializer(),
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.JpqlLower
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 JpqlLowerSerializer : JpqlSerializer<JpqlLower> {
override fun handledType(): KClass<JpqlLower> {
return JpqlLower::class
}

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

writer.write("LOWER")

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

import com.linecorp.kotlinjdsl.querymodel.jpql.expression.Expressions
import com.linecorp.kotlinjdsl.querymodel.jpql.expression.impl.JpqlLower
import com.linecorp.kotlinjdsl.render.TestRenderContext
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 JpqlLowerSerializerTest : WithAssertions {
private val sut = JpqlLowerSerializer()

@MockK
private lateinit var writer: JpqlWriter

@MockK
private lateinit var serializer: JpqlRenderSerializer

private val stringExpression1 = Expressions.value("string1")

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

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

@Test
fun serialize() {
// given
val part = Expressions.lower(
stringExpression1,
)
val context = TestRenderContext(serializer)

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

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

0 comments on commit 5b8124a

Please sign in to comment.