lumo
lumo

Reputation: 800

JOOQ get Columns from a Table

i try to get all Columns of a Table with JOOQ from a H2 database (for testing - later it might be something like MySQL or PostGRE)

all is fine, but when i loop through my retrieved Columns and log the results to console i noticed a Problem (probably a BUG?)

my log code looks like this:

System.out.println(String.format("%d > [%s].[%s].[%s]", col.getOrdinalPosition(), col.getTableCatalog(), col.getTableName(), col.getColumnName()));

and the Output of my Table is this:

0 > [TEST].[PERSON].[PERSON]
1 > [TEST].[PERSON].[PERSON]
2 > [TEST].[PERSON].[PERSON]

i expected it to be:

0 > [TEST].[PERSON].[ID]
1 > [TEST].[PERSON].[FIRSTNAME]
2 > [TEST].[PERSON].[LASTNAME]

as the create script for the table is:

CREATE TABLE PERSON
(
   ID         INTEGER        NOT NULL,
   FIRSTNAME  VARCHAR(255),
   LASTNAME   VARCHAR(255)
);

ALTER TABLE PERSON
   ADD PRIMARY KEY (ID);

so finally my question is: how do i get the 'real' Column-Name?

EDIT: tried using JOOQ version 3.9.0 and 3.9.1

UPDATE: I found another way to retrieve the Column-Names:

if you already have the instance of Table<?> you can use this code to 'fix' the bug

// ordinal position starts at 1 but the fields-array starts at 0!
Field<?> f = tbl.fields()[col.getOrdinalPosition() - 1];
// this is needed due a bug in JOOQ, where the ColumnName is returned incorrect
col.setColumnName(f.getName());

Question: Where does Column come from? Answer:

DSLContext dslCtx = DSL.using(cfg);
InformationSchema is = dslCtx.informationSchema(tbl);
List<Column> columns = is.getColumns();

the Class Column is out of JOOQ's org.jooq.util.xml.jaxb.Columnpackage;

Upvotes: 3

Views: 3395

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221210

This is a bug in jOOQ 3.9.1 and will be fixed in 3.10.0 and 3.9.2: https://github.com/jOOQ/jOOQ/issues/5812

Upvotes: 1

Related Questions