[#5699] Add support for PostgreSQL ON CONFLICT .. DO NOTHING
This commit is contained in:
parent
593944d290
commit
a8616f9abd
@ -58,6 +58,7 @@ import static org.jooq.SQLDialect.POSTGRES_9_5;
|
||||
* </pre></code>
|
||||
*
|
||||
* @author Lukas Eder
|
||||
* @author Fabrice Le Roy
|
||||
*/
|
||||
public interface InsertOnConflictDoUpdateStep<R extends Record> {
|
||||
|
||||
|
||||
@ -84,6 +84,12 @@ public interface InsertOnDuplicateStep<R extends Record> extends InsertReturning
|
||||
@Support({ POSTGRES_9_5 })
|
||||
InsertOnConflictDoUpdateStep<R> onConflict(Collection<? extends Field<?>> keys);
|
||||
|
||||
/**
|
||||
* Add an <code>ON CONFLICT DO NOTHING</code> clause to this insert query.
|
||||
*/
|
||||
@Support({ POSTGRES_9_5 })
|
||||
InsertFinalStep<R> onConflictDoNothing();
|
||||
|
||||
/**
|
||||
* Add an <code>ON DUPLICATE KEY UPDATE</code> clause to this insert query.
|
||||
* <p>
|
||||
|
||||
@ -123,7 +123,8 @@ 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> {
|
||||
|
||||
/**
|
||||
@ -579,14 +580,12 @@ 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;
|
||||
return onDuplicateKeyUpdate();
|
||||
}
|
||||
|
||||
@Override
|
||||
public final InsertImpl doNothing() {
|
||||
getDelegate().onDuplicateKeyIgnore(true);
|
||||
return this;
|
||||
return onDuplicateKeyIgnore();
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -600,6 +599,12 @@ class InsertImpl<R extends Record, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11,
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final InsertImpl onConflictDoNothing() {
|
||||
onConflict().doNothing();
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public final InsertImpl onDuplicateKeyUpdate() {
|
||||
onDuplicateKeyUpdate = true;
|
||||
|
||||
@ -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;
|
||||
@ -125,7 +125,6 @@ final class InsertQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
|
||||
|
||||
@Override
|
||||
public final void onConflict(Collection<? extends Field<?>> fields) {
|
||||
onDuplicateKeyUpdate(true);
|
||||
this.onConflict = new QueryPartList<Field<?>>(fields);
|
||||
}
|
||||
|
||||
@ -133,7 +132,6 @@ final class InsertQueryImpl<R extends Record> extends AbstractStoreQuery<R> impl
|
||||
public final void onDuplicateKeyUpdate(boolean flag) {
|
||||
this.onDuplicateKeyIgnore = false;
|
||||
this.onDuplicateKeyUpdate = flag;
|
||||
this.onConflict = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -277,16 +275,23 @@ 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");
|
||||
ctx.formatSeparator()
|
||||
.start(INSERT_ON_DUPLICATE_KEY_UPDATE)
|
||||
.keyword("on conflict")
|
||||
.sql(' ');
|
||||
|
||||
if (onConflict != null && onConflict.size() > 0) {
|
||||
ctx.sql(" (");
|
||||
boolean qualify = ctx.qualify();
|
||||
ctx.qualify(false).visit(onConflict.get(0)).qualify(qualify);
|
||||
ctx.sql(") ");
|
||||
|
||||
ctx.sql('(')
|
||||
.qualify(false)
|
||||
.visit(onConflict)
|
||||
.qualify(qualify)
|
||||
.sql(") ");
|
||||
}
|
||||
|
||||
ctx.keyword(" do nothing").end(INSERT_ON_DUPLICATE_KEY_UPDATE);
|
||||
ctx.keyword("do nothing")
|
||||
.end(INSERT_ON_DUPLICATE_KEY_UPDATE);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@ -3507,14 +3507,15 @@ class ParserImpl implements Parser {
|
||||
|
||||
private static final Field<?> parseWindowFunction(ParserContext ctx, WindowIgnoreNullsStep s1, WindowOverStep<?> s2) {
|
||||
if (s1 != null) {
|
||||
boolean respectNulls = parseKeywordIf(ctx, "RESPECT NULLS");
|
||||
boolean ignoreNulls = !respectNulls && parseKeywordIf(ctx, "IGNORE NULLS");
|
||||
|
||||
s2 = respectNulls
|
||||
? s1.respectNulls()
|
||||
: ignoreNulls
|
||||
? s1.ignoreNulls()
|
||||
: s1;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
s2 = s1;
|
||||
}
|
||||
|
||||
parseKeyword(ctx, "OVER");
|
||||
|
||||
@ -14,6 +14,7 @@ Authors and contributors of jOOQ or parts of jOOQ in alphabetical order:
|
||||
- Ed Schaller
|
||||
- Eric Peters
|
||||
- Espen Stromsnes
|
||||
- Fabrice Le Roy
|
||||
- Gonzalo Ortiz Jaureguizar
|
||||
- Gregory Hlavac
|
||||
- Henrik Sjöstrand
|
||||
|
||||
Loading…
Reference in New Issue
Block a user