Reputation: 1247
I've been playing around with jOOQ (in Scala) and would appreciate an answer to the (seemingly simple) question: how can I view a field which has been aliased?
Hopefully the following example illustrates my problem:
import org.jooq.impl._
import org.jooq.impl.DSL._
val a = field("a", SQLDataType.DOUBLE)
val b = a.as("b")
// prints "b"
println(b)
// however, I would like it to print a "b" as in
select(b)
I have found a case where the answer provided by @Lukas Eder doesn't work. A modified answer of the one below for which the following case works would be much appreciated (cheers):
val f = field("amount", SQLDataType.DOUBLE)
val p = field("partition", SQLDataType.INTEGER)
val o = field("order_by", SQLDataType.DATE)
// prints as expected
val r = coalesce(sum(f).over().partitionBy(p).orderBy(o), 0)
// coalesce(
// sum(amount) over (partition by partition order by order_by asc),
// 0)
val a = r.as("test")
// question mark instead of 0
println(
DSL.using(SQLDialect.POSTGRES)
.renderContext()
.declareFields(true)
.render(a)
)
// coalesce(sum(amount) over (partition by partition order by order_by asc), ?) as "test"
Upvotes: 1
Views: 457
Reputation: 221145
The "normal" way to generate SQL for an aliased field is the way you've seen. The way the field is referenced, e.g. when put in the ORDER BY
clause.
The "exceptional" way to generate SQL for an aliased field is when it is placed in the SELECT
clause, i.e. when it declares itself.
All QueryPart.toString()
implementations generate the "normal" SQL string. If you explicitly want to generate the field declaration as in a SELECT
clause, you can use jOOQ's rendering API directly:
println(
DSL.using(SQLDialect.ORACLE)
.renderContext()
.declareFields(true)
.render(b)
)
Upvotes: 1