Hi
We're currently struggling to insert a list of data into a table having an auto-generated ID (or using a sequence on Oracle). Because our code must be runnable both on Oracle and other DBs (for example H2, SQL Server, MariaDB), we cannot completely optimizie this query.
The problem is the following, assuming we have to insert data in a table having a PK column (numerical, autogenerated on most DBs and using a sequence on oracle).
For example:
db.insertInto(TABLE).columns(PK, COL)
.valuesOfRows(
rows.stream().map(rows -> DLS.row(
sequence.map(seq -> db.dsl().fetchValue(seq.nextval())).orElse(null), // <- does not work for example on H2
rows.col()))
However, this does not work for example for H2, as inserting NULL in that explicit NON-NULL column doesn't seem to trigger the autogeneration of the value.
Using .insertInto(TABLE).set(..).set(..).newRecord() would work, as we could conditionally add the .set. However, the different return types makes this very very hard to use in a loop, as newRecord has to be called rows.size()-1 times.
Is there a better way to insert such dynamic data and adding or not adding the sequence as needed?
Thanks Matt