Allow to specify fields for "on conflict" postgres instruction in case of "do nothing"

This commit is contained in:
Fabrice Le Roy 2016-12-08 21:03:07 +01:00
parent 180ca47ecf
commit 134919e65e
3 changed files with 25 additions and 9 deletions

View File

@ -66,4 +66,10 @@ public interface InsertOnConflictDoUpdateStep<R extends Record> {
*/
@Support({ POSTGRES_9_5 })
InsertOnDuplicateSetStep<R> doUpdate();
/**
* Add the <code>DO IGNORE</code> clause.
*/
@Support({ POSTGRES_9_5 })
InsertFinalStep<R> doNothing();
}

View File

@ -123,8 +123,7 @@ class InsertImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
InsertValuesStepN<R>,
InsertSetStep<R>,
InsertSetMoreStep<R>,
InsertOnDuplicateSetMoreStep<R>,
InsertOnConflictDoUpdateStep<R>,
InsertOnDuplicateSetMoreStep<R>, InsertOnConflictDoUpdateStep<R>,
InsertResultStep<R> {
/**
@ -580,6 +579,13 @@ class InsertImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
@Override
public final InsertImpl doUpdate() {
onDuplicateKeyUpdate = true;
return this;
}
@Override
public final InsertImpl doNothing() {
getDelegate().onDuplicateKeyIgnore(true);
return this;
}
@ -590,7 +596,6 @@ class InsertImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
@Override
public final InsertImpl onConflict(Collection<? extends Field<?>> keys) {
onDuplicateKeyUpdate = true;
getDelegate().onConflict(keys);
return this;
}

View File

@ -54,10 +54,10 @@ import static org.jooq.impl.DSL.select;
import static org.jooq.impl.DSL.selectFrom;
import static org.jooq.impl.DSL.selectOne;
import static org.jooq.impl.DSL.table;
import static org.jooq.impl.Tools.DataKey.DATA_INSERT_SELECT_WITHOUT_INSERT_COLUMN_LIST;
import static org.jooq.impl.Tools.EMPTY_FIELD;
import static org.jooq.impl.Tools.aliasedFields;
import static org.jooq.impl.Tools.fieldNames;
import static org.jooq.impl.Tools.DataKey.DATA_INSERT_SELECT_WITHOUT_INSERT_COLUMN_LIST;
import java.util.Arrays;
import java.util.Collection;
@ -140,7 +140,6 @@ final class InsertQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
public final void onDuplicateKeyIgnore(boolean flag) {
this.onDuplicateKeyUpdate = false;
this.onDuplicateKeyIgnore = flag;
this.onConflict = null;
}
@Override
@ -278,10 +277,16 @@ final class InsertQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
case POSTGRES_9_5:
case POSTGRES: {
toSQLInsert(ctx);
ctx.formatSeparator()
.start(INSERT_ON_DUPLICATE_KEY_UPDATE)
.keyword("on conflict do nothing")
.end(INSERT_ON_DUPLICATE_KEY_UPDATE);
ctx.formatSeparator().start(INSERT_ON_DUPLICATE_KEY_UPDATE).keyword("on conflict");
if (onConflict != null && onConflict.size() > 0) {
ctx.sql(" (");
boolean qualify = ctx.qualify();
ctx.qualify(false).visit(onConflict.get(0)).qualify(qualify);
ctx.sql(") ");
}
ctx.keyword(" do nothing").end(INSERT_ON_DUPLICATE_KEY_UPDATE);
break;
}