Hi Aaron
There were some open questions in this thread. Let's see
>> If you really want to do this with jOOQ, ExecuteListener might be a
>> good place to start.
>
> Good. This leads to two questions:
>
> 1. How do I know this is an insert statement? I could check
> "ctx.getSQL().startsWith("INSERT")" but maybe there is a better way?
The ExecuteContext.query() method returns an org.jooq.Query object.
All types of INSERT statements extend org.jooq.Insert. So you might do
a check as such:
ctx.query() instanceof Insert
> 2. How do I find the PK columns in the statement? In my DB, it's always the
> first column. Is the column order guaranteed? Or can I ask the context for
> the index to which a org.jooq.TableField was bound?
The order is supposed to be guaranteed. This means:
1. jooq-meta *should* return fields in the order as defined in the database
2. jooq-codegen generates fields in the order provided by jooq-meta
3. jooq uses the order of fields as generated
If this fails, then it's due to a bug.
Nevertheless, as discussed in other threads, it probably makes sense
to expose a bit more of jOOQ's QueryPart internals. In this case, you
should be able to do something along these lines:
InsertQuery query = ((Insert) ctx.query()).getQuery();
query.getValue(ID);
This is already tracked as #1492, requesting for more exposure of
jOOQ's internals:
https://sourceforge.net/apps/trac/jooq/ticket/1492
Any additional hints welcome!
Cheers
Lukas
2012/6/8 digulla <[email protected]>:
>> > My guess is that I could use ExecuteListener for that but which method
>> > would
>> > be the best place to assign the PK?
>>
>> So I'm guessing that your generator is implemented in Java and you
>> cannot use triggers to fill in the PK value... What is the reason why
>> you wouldn't implement PK generation in a DAO layer "on top" of jOOQ,
>> supplying the correct PK value to jOOQ?
>
>
> Because that's boring :-)
>
> No, really, I use a central PK generator for all instances, so it would make
> sense to inject it in a single place, too. Plus it's something that needs to
> be done all the time - it would be thousands of lines of code that devs
> wouldn't have to write.
>
> I also don't want to encourage developers to clone database rows by
> overwriting the PK and calling "store()". If they don't see how the PK is
> assigned, they're not going to try to mess with it - I hope.
>
>>
>> If you really want to do this with jOOQ, ExecuteListener might be a
>> good place to start.
>
>
> Good. This leads to two questions:
>
> 1. How do I know this is an insert statement? I could check
> "ctx.getSQL().startsWith("INSERT")" but maybe there is a better way?
>
> 2. How do I find the PK columns in the statement? In my DB, it's always the
> first column. Is the column order guaranteed? Or can I ask the context for
> the index to which a org.jooq.TableField was bound?
>
> Regards,
>
> A. Digulla