Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JOOQ: Get table and columns with string?

Hello I'm using JOOQ with Spring Boot and was wondering if there was a way to obtain a table and its columns with a string of their name? For example:

I want to be able to get a table by doing something like:

someObject.getTable("user")

Then using the result of that get method I also want to obtain all of that table's columns and be able to compare the column names to other strings. In other words if there is a way to get a table, can I also get the table's column names from that same object?

I would really appreciate any help with this.

like image 906
Tuco Avatar asked Mar 07 '23 20:03

Tuco


2 Answers

Yes you can:

Using the code generator

When you generate your schema, all this information is available to you from generated code. Just go to your schema and look for the table (case-sensitive!):

Table<?> table = PUBLIC.getTable("user");

And then:

Field<?>[] fields = table.fields();

Using org.jooq.Meta

If you don't have generated meta data, you can still look up things from your JDBC connection using DSLContext.meta(), from where you can navigate your schemas / tables / etc.

like image 126
Lukas Eder Avatar answered Mar 10 '23 10:03

Lukas Eder


Yes. As correctly stated above, only one addition to further clarify the upcoming users, PUBLIC is your DefaultSchema, if you have with you, meta generated code. So,

Table<?> table = new DefaultSchema().getTable("user");
like image 42
Anmol Deora Avatar answered Mar 10 '23 11:03

Anmol Deora