Get table name by FieldAlias

16 views
Skip to first unread message

sca...@live.com

unread,
Nov 17, 2017, 11:20:27 AM11/17/17
to jOOQ User Group
Is it possible to get the table name from a field constructed as the following?
field( name( "MyTable", "myField" ) ).as( "myAlias" )

When I try to get it through .getQualifiedName() it returns only the alias part, without a table name.

Lukas Eder

unread,
Nov 20, 2017, 6:31:00 AM11/20/17
to jooq...@googlegroups.com
Hello,

There currently isn't such a public API. There's a pending feature request to help discover such things:

But I'm currently not sure if opening up such API is a good idea - it's hard to keep the internal QueryPart APIs backwards compatible. Also, an aliased field is quite a different thing from the original field reference. For one, it can alias anything, including expressions based on several field references and other expressions, such as:

first_name || ' ' || last_name AS name

Of course, you can keep a reference to your unaliased field and fetch the qualified name from that:

Field<?> original = field( name( "MyTable", "myField" ) );
Field<?> aliased = original.as( "myAlias" );
String tableName = original.getQualifiedName().qualifier().last();

But I suspect the logic creating the alias is disconnected from the logic referencing it.

As another workaround, you can use reflection on jOOQ's internals.

A third workaround would be to traverse the expression tree using a VisitListener and introspect the individual parts being traversed...

I hope this helps,
Lukas

--
You received this message because you are subscribed to the Google Groups "jOOQ User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

sca...@live.com

unread,
Nov 20, 2017, 6:45:22 AM11/20/17
to jOOQ User Group
Hi Lukas,

Thank you for your answer, I get it now why an alias sometimes shouldn't have a reference to a table: even considering that some expression like "'myvar' as myvar" is in context of some table, it is not really related to the table by its nature.

I think I'll go with keeping a reference to the original field - it is already kind of what I'm already doing.

Also it's nice to see that a lot of cases already have corresponding issues on GitHub :)
To unsubscribe from this group and stop receiving emails from it, send an email to jooq-user+...@googlegroups.com.

Lukas Eder

unread,
Nov 20, 2017, 7:48:24 AM11/20/17
to jooq...@googlegroups.com
Also it's nice to see that a lot of cases already have corresponding issues on GitHub :)

Oh yeah - jOOQ has been around for a while, and so have been the various ideas to enhance it. The question is just: Is there a more generic new feature that would allow for solving dozens of issues at once, rather than solving them individually :)

Cheers,
Lukas
Reply all
Reply to author
Forward
0 new messages