Hello, Assuming you cannot use triggers for some reason, one simple way to do this is to:
- Intercept all INSERT queries with an ExecuteListener - Use the experimental query object model (QOM) API to set QOM.Insert::$columns and QOM.Insert::$values, if QOM.Insert::$into is the right target table - Set the modified query back on the ExecuteContext For the second part, you'll have to upgrade jOOQ, as QOM.Insert has been added in jOOQ 3.18 only. On Thu, May 30, 2024 at 11:50 PM 'Gang Luo' via jOOQ User Group < [email protected]> wrote: > I have a table Foo defined as > > create table FOO { > name varchar(123), > from varchar(123) default null > } > > I am using JOOQ 3.16 and already have following code > `db.insertInto(Foo).set(Foo.name, "xxx").execute()` everywhere. Now I want > to find a way to re-write that query to effectively be > `db.insertInto(Foo).set(Foo.name).set(Foo.from, "XYZ")` where "XYZ" is a > dynamically computed value. But I don't want to make that actual change > into 100s of places. Instead I want to do it once and centrally in JOOQ > level to re-write the insert query dynamically. > > Basically below is what I want to achieve > > - Intercept the query before it get executed > - if the query is an insert query, for FOO table, set column "FOO.From" to > be something meaningful > - execute that re-written query to insert the entry with "from" column > properly populated. > > I am assessing the option to have a new "ExecuteListener" where the > "ExecuteContext" is given. I can get a "Query" (or batched queries) from > it, inspect if it is insert statement, and call `addValue` to add the > right value for "from". The only blocker is, there doesn't seem to be a > good way for me to tell what table it is, as I only want to add value for > table "FOO". "Query" does has "table" property which is not exposed > publicly unfortunately. > > Would appreciate some help about the best way to achieve my goal. Thanks. > > -- > 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 [email protected]. > To view this discussion on the web visit > https://groups.google.com/d/msgid/jooq-user/66986af7-18cc-4cfc-a3db-f4b90d12e0dfn%40googlegroups.com > <https://groups.google.com/d/msgid/jooq-user/66986af7-18cc-4cfc-a3db-f4b90d12e0dfn%40googlegroups.com?utm_medium=email&utm_source=footer> > . > -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/jooq-user/CAB4ELO4LoeNHjOh22HDQM1MWcJN%2BORB8XFHDuzErxW1H56uQmw%40mail.gmail.com.
