Search code examples
javajooq

Custom type for create table statement in JOOQ


I am trying to create a runtime DDL script for a postgresql database using jooq, this DDL script includes the use of Postgresql types such as "regclass".

I tried using DSL.field("reference regclass") or similar approaches, but the end result usually ends as something like:

create table meta_reference(id uuid, reference regclass any);

I saw other questions but those usually involves situations where casting is allowed, which is not the case for table creation or similar DDL scripts.

It seems that the .column part of DSL.createTable uses the dataType of the given field. I was thinking about implementing the DataType interface myself, but there are too many methods to override, I would like to know if implementing DataType is the only way or if there are other ways (such as Bindings) to create such script and achieve something like this:

create table meta_reference(id uuid, reference regclass);

I understand that the "regclass" type could be considered a bad example, because it is just a glorified int/bigint with some checks (but its integrity fails, like for example, when dropping tables), but the same logic above could be applied to any vendor-specific type or user-defined-type.


Solution

  • There's a pending feature request for this:

    In the meantime, you could create your own DefaultDataType, though beware, this means using internal API, which might break in the future (especially, once #11806 is implemented)