Skip to content

Commit

Permalink
feat: handle mixed values and expressions in parameters (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
shouwn authored Dec 17, 2024
1 parent 7556c84 commit 5974ea7
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 5 deletions.
18 changes: 13 additions & 5 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 @@ -1287,7 +1287,7 @@ open class Jpql : JpqlDsl {
*/
@SinceJdsl("3.0.0")
fun <T : Any> new(type: KClass<T>, vararg args: Any): Expression<T> {
return Expressions.new(type, args.map { Expressions.value(it) })
return Expressions.new(type, args.map { valueOrExpression(it) })
}

/**
Expand Down Expand Up @@ -1708,7 +1708,7 @@ open class Jpql : JpqlDsl {
@LowPriorityInOverloadResolution
@SinceJdsl("3.0.0")
fun <T : Any> function(type: KClass<T>, name: String, vararg args: Any): Expression<T> {
return Expressions.function(type, name, args.map { Expressions.value(it) })
return Expressions.function(type, name, args.map { valueOrExpression(it) })
}

/**
Expand Down Expand Up @@ -1737,7 +1737,7 @@ open class Jpql : JpqlDsl {
@LowPriorityInOverloadResolution
@SinceJdsl("3.0.0")
fun <T : Any> customExpression(type: KClass<T>, template: String, vararg args: Any): Expression<T> {
return Expressions.customExpression(type, template, args.map { Expressions.value(it) })
return Expressions.customExpression(type, template, args.map { valueOrExpression(it) })
}

/**
Expand Down Expand Up @@ -3036,7 +3036,7 @@ open class Jpql : JpqlDsl {
@LowPriorityInOverloadResolution
@SinceJdsl("3.0.0")
fun function(type: KClass<Boolean>, name: String, vararg args: Any): Predicate {
return Predicates.function(name, args.map { Expressions.value(it) })
return Predicates.function(name, args.map { valueOrExpression(it) })
}

/**
Expand Down Expand Up @@ -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) })
}

/**
Expand Down Expand Up @@ -3218,4 +3218,12 @@ open class Jpql : JpqlDsl {
fun <T : Any> deleteFrom(entity: Entityable<T>): DeleteQueryWhereStep<T> {
return DeleteQueryDsl(entity.toEntity())
}

private fun valueOrExpression(value: Any): Expression<*> {
return if (value is Expression<*>) {
value
} else {
Expressions.value(value)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> = expression // for type check

// then
val expected = Expressions.customExpression(
Int::class,
template1,
listOf(
Expressions.value(string1),
stringExpression2,
),
)

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int> = expression // for type check

// then
val expected = Expressions.function(
Int::class,
name1,
listOf(
Expressions.value(string1),
stringExpression2,
),
)

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Row> = expression // for type check

// then
val expected = Expressions.new(
type = Row::class,
args = listOf(
Expressions.value(string1),
stringExpression2,
),
)

assertThat(actual).isEqualTo(expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 5974ea7

Please sign in to comment.