Another point: I'm trying to use a `select 1 from....
# komapper
d
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
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
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
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))