[#1537] Factory.batchStore() renders bad SQL for Postgres. The RETURNING

clause is not allowed in batch INSERTs
This commit is contained in:
Lukas Eder 2012-07-06 19:18:08 +02:00
parent e523a9fa6f
commit 633d1a43a9
2 changed files with 25 additions and 5 deletions

View File

@ -93,6 +93,8 @@ class BatchStore implements Batch {
Settings orig = SettingsTools.clone(work);
try {
// [#1537] Communicate with TableRecordImpl
create.setData(TableRecordImpl.OMIT_RETURNING_CLAUSE, true);
// Add the QueryCollector to intercept query execution after rendering
work.setExecuteListeners(Arrays.asList(QueryCollector.class.getName()));
@ -131,6 +133,8 @@ class BatchStore implements Batch {
// Restore the original factory
finally {
create.getData().remove(TableRecordImpl.OMIT_RETURNING_CLAUSE);
work.setExecuteListeners(orig.getExecuteListeners());
work.setExecuteLogging(orig.isExecuteLogging());
}

View File

@ -35,6 +35,7 @@
*/
package org.jooq.impl;
import static java.lang.Boolean.TRUE;
import static org.jooq.impl.Factory.val;
import java.util.Collection;
@ -45,11 +46,13 @@ import org.jooq.DeleteQuery;
import org.jooq.Field;
import org.jooq.Identity;
import org.jooq.InsertQuery;
import org.jooq.SQLDialect;
import org.jooq.SimpleSelectQuery;
import org.jooq.StoreQuery;
import org.jooq.Table;
import org.jooq.TableField;
import org.jooq.TableRecord;
import org.jooq.UpdatableRecord;
import org.jooq.UpdateQuery;
import org.jooq.exception.InvalidResultException;
@ -65,7 +68,14 @@ public class TableRecordImpl<R extends TableRecord<R>> extends TypeRecord<Table<
/**
* Generated UID
*/
private static final long serialVersionUID = 3216746611562261641L;
private static final long serialVersionUID = 3216746611562261641L;
/**
* [#1537] This constant is used internally by jOOQ to omit the RETURNING
* clause in {@link Factory#batchStore(UpdatableRecord...)} calls for
* {@link SQLDialect#POSTGRES}
*/
static final String OMIT_RETURNING_CLAUSE = "JOOQ.OMIT_RETURNING_CLAUSE";
public TableRecordImpl(Table<R> table) {
super(table);
@ -112,7 +122,8 @@ public class TableRecordImpl<R extends TableRecord<R>> extends TypeRecord<Table<
@SuppressWarnings("unchecked")
private final int storeInsert() {
InsertQuery<R> insert = create().insertQuery(getTable());
Factory create = create();
InsertQuery<R> insert = create.insertQuery(getTable());
for (Field<?> field : getFields()) {
if (getValue0(field).isChanged()) {
@ -122,12 +133,17 @@ public class TableRecordImpl<R extends TableRecord<R>> extends TypeRecord<Table<
// [#814] Refresh identity and/or main unique key values
// [#1002] Consider also identity columns of non-updatable records
Collection<Field<?>> key = getReturning();
insert.setReturning(key);
// [#1537] Avoid refreshing identity columns on batch inserts
Collection<Field<?>> key = null;
if (!TRUE.equals(create.getData(OMIT_RETURNING_CLAUSE))) {
key = getReturning();
insert.setReturning(key);
}
int result = insert.execute();
// If an insert was successful try fetching the generated IDENTITY value
if (!key.isEmpty() && result > 0) {
if (key != null && !key.isEmpty() && result > 0) {
if (insert.getReturnedRecord() != null) {
for (Field<?> field : key) {
setValue0(field, new Value<Object>(insert.getReturnedRecord().getValue(field)));