How do I write my own code generator to generate something similar to master enum data types?

33 views
Skip to first unread message

Felix Rabe

unread,
Mar 6, 2015, 1:57:37 AM3/6/15
to jooq...@googlegroups.com
Hi,

I just recently started using and learning jOOQ.

As it turns out I would love to have master data tables feature, which has been removed as of jOOQ 3.0.

Let me explain, what I would like to do:
First of all, I'm using MySQL. I have some tables, which contain typed items. That means, e.g. I have a table item with a field itemtype_id. Then I also have a table item_type, with the fields id, name, description, etc. So far, to avoid magic numbers, we have used enums or at least int constants in our code. But since every time a type is added in the database the java code needs to be updated manually, I would like to generate it.

I've read about Enum Converters and Custom Data Types in the manual and I think I know how to use them. But still I would have to create the enums myself.
So what I would like to do is to write my own code generator that generates Enum Converters from the item_type tables, somewhat similar to:
public enum ItemType {
/**
 * Description of Type A
 */

TYPE_A
(1),
/**
 * Description of Type B
 */

TYPE_B
(2),
...


This is what the master data table code generation did, right? (http://www.jooq.org/doc/2.6/manual/code-generation/master-data-types/)

But I don't know yet where to start to write a own generator and how to integrate it with the default jOOQ-generator.
Therefore I'm very grateful about any advice from you :)

Cheers,
Felix

Lukas Eder

unread,
Mar 6, 2015, 12:07:56 PM3/6/15
to jooq...@googlegroups.com
Hi Felix,

MasterDataType was indeed a very useful feature in jOOQ 2.x, but it was much too narrow in scope and in vision to be maintained. Master data is very individual:

- Do you need only ID / VALUE? Or more?
- Do you have only one table or several (e.g. key and translations)?
- Do you want to grant access to the data, or completely remove them from your schema representation?
- Is it completely static (at codegen time), or semi-static (at application start time)?

We could have started implementing an extremely generic mechanism, but jOOQ's type system wasn't ready for that (we've added org.jooq.Binding only in jOOQ 3.5). So it would have been a lot of hacks, and an impediment to more visionary developments in jOOQ 3.0 (e.g. type safe Record[N] and Row[N] types).

Writing your own code generator, however, should be rather easy. Either you extend JavaGenerator in order to integrate it in jOOQ's code generation lifecycle, or you just write a standalone code generator. A great tool for writing template-based code generators is Eclipse Xtend (https://eclipse.org/xtend/documentation.html#templates). We're using it very happily to generate those Record[N] types. I wouldn't depend too much on jOOQ-codegen, because we built our API mainly for ourselves. I feel that extending jOOQ-codegen would be more of an impediment to your master data types than rolling your own in a 1-2 hours.

Once you have generated those master data types, it'll be very easy to integrate them in your jOOQ meta data using ordinary Converters.

I hope this helps.
Best Regards,
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+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages