Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to express this with jOOQ "Select alias.*, otherAlias.Column From .."

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
like image 865
Matthias Oertel Avatar asked Jun 14 '18 13:06

Matthias Oertel


1 Answers

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.

like image 140
Lukas Eder Avatar answered Sep 29 '22 16:09

Lukas Eder