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

feat: handle mixed values and expressions in parameters #806

Merged
merged 1 commit into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading