dave08
03/03/2024, 4:38 PMselect 1 from...
as a column (with a Boolean value), I tried defining:
private fun literalOne(): ScalarExpression<Boolean, Int> {
val name = "literalOne"
return columnExpression(Boolean::class, Int::class, { it > 0 }, name, listOf()) {
append("1")
} as ScalarExpression<Boolean, Int>
}
but it seems like a ScalarExpression
can't be defined... so I can't use a subquery with select(literalOne())
like this as a column value... is there any way to work around this?Toshihiro Nakamura
03/04/2024, 11:23 AMQueryDsl.select(literal(true))
dave08
03/04/2024, 11:24 AMexists(subquery)
dave08
03/04/2024, 11:24 AMdave08
03/04/2024, 11:25 AMselect 1 ....
dave08
03/04/2024, 11:25 AMdave08
03/04/2024, 11:26 AMdave08
03/04/2024, 11:30 AMSELECT foo, EXISTS(select 1 from bar where baz = foo) as isInBar) from fooTable
dave08
03/04/2024, 11:44 AMToshihiro Nakamura
03/04/2024, 12:07 PMprivate fun exists(expression: SubqueryExpression<*>): ColumnExpression<Boolean, Boolean> {
val name = "exists"
val operand = Operand.Subquery(expression)
return columnExpression(Boolean::class, name, listOf(operand)) {
append("$name(")
visit(operand)
append(")")
}
}
You can use the above function as follows:
val subquery = QueryDsl.select(literal(1))
QueryDsl.from(e).select(exists(subquery))