https://kotlinlang.org logo
#komapper
Title
# komapper
d

dave08

03/03/2024, 4:38 PM
Another point: I'm trying to use a
select 1 from...
as a column (with a Boolean value), I tried defining:
Copy code
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?
t

Toshihiro Nakamura

03/04/2024, 11:23 AM
Could you tell me what kind of SQL you want to generate? Is this not the query you are looking for?
Copy code
QueryDsl.select(literal(true))
d

dave08

03/04/2024, 11:24 AM
I think what I'm looking for is a columnexpression
exists(subquery)
That returns a boolean
and the subquery is
select 1 ....
which is a scalar subquery, but it doesn't seem I can define custom scalar subqueries currently
QueryDsl.select(literal(true)) is considered a scalar subquery, and if so, all that would be left is to make an exists function for it to appear in select(...)
Copy code
SELECT foo, EXISTS(select 1 from bar where baz = foo) as isInBar) from fooTable
I guess there's no way to do this @Toshihiro Nakamura?
t

Toshihiro Nakamura

03/04/2024, 12:07 PM
If you want to use the EXISTS operator in the SELECT clause, you can define it like this.
Copy code
private 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:
Copy code
val subquery = QueryDsl.select(literal(1))
QueryDsl.from(e).select(exists(subquery))
2 Views