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 13d54c210..ccfc4924a 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 @@ -1287,7 +1287,7 @@ open class Jpql : JpqlDsl { */ @SinceJdsl("3.0.0") fun new(type: KClass, vararg args: Any): Expression { - return Expressions.new(type, args.map { Expressions.value(it) }) + return Expressions.new(type, args.map { valueOrExpression(it) }) } /** @@ -1708,7 +1708,7 @@ open class Jpql : JpqlDsl { @LowPriorityInOverloadResolution @SinceJdsl("3.0.0") fun function(type: KClass, name: String, vararg args: Any): Expression { - return Expressions.function(type, name, args.map { Expressions.value(it) }) + return Expressions.function(type, name, args.map { valueOrExpression(it) }) } /** @@ -1737,7 +1737,7 @@ open class Jpql : JpqlDsl { @LowPriorityInOverloadResolution @SinceJdsl("3.0.0") fun customExpression(type: KClass, template: String, vararg args: Any): Expression { - return Expressions.customExpression(type, template, args.map { Expressions.value(it) }) + return Expressions.customExpression(type, template, args.map { valueOrExpression(it) }) } /** @@ -3036,7 +3036,7 @@ open class Jpql : JpqlDsl { @LowPriorityInOverloadResolution @SinceJdsl("3.0.0") fun function(type: KClass, name: String, vararg args: Any): Predicate { - return Predicates.function(name, args.map { Expressions.value(it) }) + return Predicates.function(name, args.map { valueOrExpression(it) }) } /** @@ -3066,7 +3066,7 @@ open class Jpql : JpqlDsl { @LowPriorityInOverloadResolution @SinceJdsl("3.3.0") fun customPredicate(template: String, vararg args: Any): Predicate { - return Predicates.customPredicate(template, args.map { Expressions.value(it) }) + return Predicates.customPredicate(template, args.map { valueOrExpression(it) }) } /** @@ -3218,4 +3218,12 @@ open class Jpql : JpqlDsl { fun deleteFrom(entity: Entityable): DeleteQueryWhereStep { return DeleteQueryDsl(entity.toEntity()) } + + private fun valueOrExpression(value: Any): Expression<*> { + return if (value is Expression<*>) { + value + } else { + Expressions.value(value) + } + } } diff --git a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/CustomExpressionDslTest.kt b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/CustomExpressionDslTest.kt index 40c640bba..a32847970 100644 --- a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/CustomExpressionDslTest.kt +++ b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/CustomExpressionDslTest.kt @@ -58,4 +58,26 @@ class CustomExpressionDslTest : WithAssertions { assertThat(actual).isEqualTo(expected) } + + @Test + fun `customExpression() with a string and a string expression`() { + // when + val expression = queryPart { + customExpression(Int::class, template1, string1, stringExpression2) + }.toExpression() + + val actual: Expression = expression // for type check + + // then + val expected = Expressions.customExpression( + Int::class, + template1, + listOf( + Expressions.value(string1), + stringExpression2, + ), + ) + + assertThat(actual).isEqualTo(expected) + } } diff --git a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/FunctionDslTest.kt b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/FunctionDslTest.kt index 76a6166b8..2f60e0d85 100644 --- a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/FunctionDslTest.kt +++ b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/FunctionDslTest.kt @@ -58,4 +58,26 @@ class FunctionDslTest : WithAssertions { assertThat(actual).isEqualTo(expected) } + + @Test + fun `function() with a string and a string expression`() { + // when + val expression = queryPart { + function(Int::class, name1, string1, stringExpression2) + }.toExpression() + + val actual: Expression = expression // for type check + + // then + val expected = Expressions.function( + Int::class, + name1, + listOf( + Expressions.value(string1), + stringExpression2, + ), + ) + + assertThat(actual).isEqualTo(expected) + } } diff --git a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/NewDslTest.kt b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/NewDslTest.kt index 88e3f163d..6e209bc47 100644 --- a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/NewDslTest.kt +++ b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/expression/NewDslTest.kt @@ -56,4 +56,25 @@ class NewDslTest : WithAssertions { assertThat(actual).isEqualTo(expected) } + + @Test + fun `new() with a string and a string expression`() { + // when + val expression = queryPart { + new(Row::class, string1, stringExpression2) + }.toExpression() + + val actual: Expression = expression // for type check + + // then + val expected = Expressions.new( + type = Row::class, + args = listOf( + Expressions.value(string1), + stringExpression2, + ), + ) + + assertThat(actual).isEqualTo(expected) + } } diff --git a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/predicate/CustomPredicateDslTest.kt b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/predicate/CustomPredicateDslTest.kt index 2800a89a8..bfe172ec1 100644 --- a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/predicate/CustomPredicateDslTest.kt +++ b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/predicate/CustomPredicateDslTest.kt @@ -57,4 +57,25 @@ class CustomPredicateDslTest : WithAssertions { assertThat(actual).isEqualTo(expected) } + + @Test + fun `customPredicate() with a string and a string expression`() { + // when + val predicate = queryPart { + customPredicate(template1, string1, stringExpression2) + } + + val actual: Predicate = predicate // for type check + + // then + val expected = Predicates.customPredicate( + template1, + listOf( + Expressions.value(string1), + stringExpression2, + ), + ) + + assertThat(actual).isEqualTo(expected) + } } diff --git a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/predicate/FunctionDslTest.kt b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/predicate/FunctionDslTest.kt index 9a6f29188..11fab4a7b 100644 --- a/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/predicate/FunctionDslTest.kt +++ b/dsl/jpql/src/test/kotlin/com/linecorp/kotlinjdsl/dsl/jpql/predicate/FunctionDslTest.kt @@ -57,4 +57,25 @@ class FunctionDslTest : WithAssertions { assertThat(actual).isEqualTo(expected) } + + @Test + fun `function() with a string and a string expression`() { + // when + val predicate = queryPart { + function(Boolean::class, name1, string1, stringExpression2) + } + + val actual: Predicate = predicate // for type check + + // then + val expected = Predicates.function( + name1, + listOf( + Expressions.value(string1), + stringExpression2, + ), + ) + + assertThat(actual).isEqualTo(expected) + } }