diff --git a/.github/workflows/lint.yaml b/.github/workflows/lint.yaml index cacc6c951..858a7f3bb 100644 --- a/.github/workflows/lint.yaml +++ b/.github/workflows/lint.yaml @@ -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 diff --git a/docs/en/jpql-with-kotlin-jdsl/expressions.md b/docs/en/jpql-with-kotlin-jdsl/expressions.md index 16ce9b1d4..6dd359985 100644 --- a/docs/en/jpql-with-kotlin-jdsl/expressions.md +++ b/docs/en/jpql-with-kotlin-jdsl/expressions.md @@ -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 | diff --git a/docs/ko/jpql-with-kotlin-jdsl/expressions.md b/docs/ko/jpql-with-kotlin-jdsl/expressions.md index 2b047c459..4fb5c9875 100644 --- a/docs/ko/jpql-with-kotlin-jdsl/expressions.md +++ b/docs/ko/jpql-with-kotlin-jdsl/expressions.md @@ -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 | diff --git a/dsl/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/Jpql.kt b/dsl/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/Jpql.kt index cfb415da5..7aa26ab76 100644 --- a/dsl/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/Jpql.kt +++ b/dsl/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/Jpql.kt @@ -1057,6 +1057,22 @@ open class Jpql : JpqlDsl { fun upper(string: Expressionable): Expression { return Expressions.upper(string.toExpression()) } + + /** + * Creates an expression that represents the string in lowercase. + */ + @SinceJdsl("3.0.0") + fun lower(value: String): Expression { + return Expressions.lower(Expressions.value(value)) + } + + /** + * Creates an expression that represents the string in lowercase. + */ + @SinceJdsl("3.0.0") + fun lower(value: Expressionable): Expression { + return Expressions.lower(value.toExpression()) + } /** * Creates an expression that represents the length of the string as an integer. @@ -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(expr, *exprs)"), - ) - fun select( - returnType: KClass, - expr: Expressionable<*>, - vararg exprs: Expressionable<*>, - ): SelectQueryFromStep { - return SelectQueryFromStepDsl( - returnType, - distinct = false, - listOf(expr.toExpression()) + exprs.map { it.toExpression() }, - ) - } - /** * Creates a select clause in a select query. */ @@ -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(expr, *exprs)"), - ) - fun selectDistinct( - returnType: KClass, - expr: Expressionable<*>, - vararg exprs: Expressionable<*>, - ): SelectQueryFromStep { - return SelectQueryFromStepDsl( - returnType, - distinct = true, - listOf(expr.toExpression()) + exprs.map { it.toExpression() }, - ) - } - /** * Creates a select clause with the DTO projection in a select query. */ @@ -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(expr, *exprs)"), - ) - fun selectNew( - returnType: KClass, - expr: Expressionable<*>, - vararg exprs: Expressionable<*>, - ): SelectQueryFromStep { - 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. */ @@ -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(expr, *exprs)"), - ) - fun selectDistinctNew( - returnType: KClass, - expr: Expressionable<*>, - vararg exprs: Expressionable<*>, - ): SelectQueryFromStep { - 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. */ diff --git a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/LowerDslTest.kt b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/LowerDslTest.kt new file mode 100644 index 000000000..bc45c2eac --- /dev/null +++ b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/LowerDslTest.kt @@ -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 = 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 = expression // for type check + + // then + val expected = Expressions.lower( + stringExpression1, + ) + + assertThat(actual).isEqualTo(expected) + } +} diff --git a/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/Expressions.kt b/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/Expressions.kt index afc7b5de2..0a0d0abaa 100644 --- a/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/Expressions.kt +++ b/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/Expressions.kt @@ -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 @@ -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): Expression { + return JpqlLower(value) + } + /** * Creates an expression that represents the length of the string as an integer. */ diff --git a/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/impl/JpqlLower.kt b/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/impl/JpqlLower.kt new file mode 100644 index 000000000..595f40890 --- /dev/null +++ b/query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/impl/JpqlLower.kt @@ -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, +) : Expression diff --git a/query-model/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/ExpressionsTest.kt b/query-model/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/ExpressionsTest.kt index bace98a9c..97295a0c3 100644 --- a/query-model/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/ExpressionsTest.kt +++ b/query-model/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/expression/ExpressionsTest.kt @@ -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 @@ -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() { diff --git a/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/JpqlRenderContext.kt b/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/JpqlRenderContext.kt index 10b7fcd67..47380d5b0 100644 --- a/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/JpqlRenderContext.kt +++ b/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/JpqlRenderContext.kt @@ -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 @@ -300,6 +301,7 @@ private class DefaultModule : JpqlRenderModule { JpqlLikeSerializer(), JpqlLiteralSerializer(), JpqlLocateSerializer(), + JpqlLowerSerializer(), JpqlMaxSerializer(), JpqlMinSerializer(), JpqlMinusSerializer(), diff --git a/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlLowerSerializer.kt b/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlLowerSerializer.kt new file mode 100644 index 000000000..6706b577d --- /dev/null +++ b/render/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlLowerSerializer.kt @@ -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 { + override fun handledType(): KClass { + 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) + } + } +} diff --git a/render/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlLowerSerializerTest.kt b/render/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlLowerSerializerTest.kt new file mode 100644 index 000000000..b424cca5f --- /dev/null +++ b/render/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/render/jpql/serializer/impl/JpqlLowerSerializerTest.kt @@ -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) + } + } +}