I am trying to use jOOQ to dynamically construct queries. So far it is going really well, but now I stumbled upon a case that I can not seem to express.
This is a simplified version of the query I want to generate:
Select alias.*, otherAlias.aColumn as aAlias
From table as alias
inner join otherTable as otherAlias
on alias.someColumn = otherAlias.someOtherColumn
Where otherAlias.someOtherColumn in (????????)
My problem is that i cannot seem to express the SELECT part how I need it. If I just use:
.select() -> I get select *
.select(alias.fields()) -> I get Select *
.select((alias.fields() :+ field(name(otherAlias, aColumn)).as(aAlias)):_*) -> I get Select otherAlias.aColumn as aAlias
Is there a way to express this with jOOQ?
The rest of the statement seems to work as expected. I am using jOOQ 3.10.7 in Scala and targeting Postgres at the moment and my statement currently looks like this:
sql
.select()
.from(alias)
.innerJoin(otherAlias)
.on(field(name(alias, someColumn)).eq(field(name(otherAlias, someOtherColumn))))
.where(condition)
Thanks a lot.
Update: I found a way to express this that seems to work by falling back to plain SQL. But I am still wondering whether there is a better way to express this. This works as a select:
.select((field(""""Alias".*"""), field(name(otherAlias, aColumn)).as(aAlias)):_*) -> I get Select "Alias".*, otherAlias.aColumn as aAlias
Assuming that you're using jOOQ 3.11 (which added support for unqualified asterisks and qualified asterisks), you can just write
alias.asterisk()
Or even, using jOOQ's scala extensions
alias.*
This is especially powerful when using the code generator.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With