I have table Car in two DB. Car in first table has column details as TEXT, in second has column details as JSONB. But I have only one generated class with TEXT field. How can I do batch insert into table 2 using generated class Car and I need to change field from TEXT to JSONB. I insert into table with dsl.batchInsert(records).execute();
And its my field in jooq generated class
public final TableField<Car, String> DETAILS = createField("details", org.jooq.impl.SQLDataType.TExT.nullable(false).defaultValue(org.jooq.impl.DSL.field("", org.jooq.impl.SQLDataType.TEXT)), this, "");
I need insert into second table smth like dsl.batchInsert(changeTextToJsonB(records)).execute();
or smth else
The obvious answer is to generate two sets of classes, one for each DB, but I'm assuming this doesn't work for you.
You could then implement a custom data type binding and attach that to your column using the code generator. Your binding could read some context variable, eg from a ThreadLocal
for a quick and dirty solution, or from your Configuration.data()
map to decide whether it wants to delegate to the VARCHAR
binding or to the JSONB
binding.
public class TextOrJsonBinding implements Binding<String, String> {
@Override
public void set(BindingSetStatementContext<U> ctx) throws SQLException {
var db = ctx.configuration().data("my-db");
if ("db1".equals(db))
SQLDataType.VARCHAR.getBinding().set(ctx);
else if ("db2".equals(db))
SQLDataType.JSONB.getBinding().set(ctx);
else
throw new DataAccessException("Unsupported DB: " + db);
}
// Repeat for all methods
}
With the above approach, you can now have two configurations for each DB (you probably already do, because you probably have different DataSource
instances):
Configuration db1 = new DefaultConfiguration();
db1.data("my-db", "db1");
Configuration db2 = new DefaultConfiguration();
db2.data("my-db", "db2");
// Produces VARCHAR bindings
db1.dsl().batchInsert(records).execute();
// Produces JSONB bindings
db2.dsl().batchInsert(records).execute();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.