From a44eca8eb7753305d21dc3e38a314bbc002d354f Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 13 Nov 2024 15:53:39 +0100 Subject: [PATCH] [jOOQ/jOOQ#16522] Add alternative setNonKeyToExcluded(), setNonPrimaryKeyToExcluded(), and setNonConflictingKeyToExcluded() methods to UPSERT syntax --- .../org/jooq/InsertOnDuplicateSetStep.java | 31 +++++++++++++++++++ .../main/java/org/jooq/impl/ParserImpl.java | 18 +++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/InsertOnDuplicateSetStep.java b/jOOQ/src/main/java/org/jooq/InsertOnDuplicateSetStep.java index e149ae0985..d9307e64e8 100644 --- a/jOOQ/src/main/java/org/jooq/InsertOnDuplicateSetStep.java +++ b/jOOQ/src/main/java/org/jooq/InsertOnDuplicateSetStep.java @@ -183,4 +183,35 @@ public interface InsertOnDuplicateSetStep { @Support({ CUBRID, DERBY, DUCKDB, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB }) InsertOnDuplicateSetMoreStep setAllToExcluded(); + /** + * Sets all non-key columns from the insert column list to + * {@link DSL#excluded(Field)}. + *

+ * This excludes any {@link Table#getKeys()} columns. + */ + @NotNull @CheckReturnValue + @Support({ CUBRID, DERBY, DUCKDB, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB }) + InsertOnDuplicateSetMoreStep setNonKeyToExcluded(); + + /** + * Sets all non-primary key columns from the insert column list to + * {@link DSL#excluded(Field)}. + *

+ * This excludes any {@link Table#getPrimaryKey()} columns. + */ + @NotNull @CheckReturnValue + @Support({ CUBRID, DERBY, DUCKDB, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB }) + InsertOnDuplicateSetMoreStep setNonPrimaryKeyToExcluded(); + + /** + * Sets all non-conflicting key columns from the insert column list to + * {@link DSL#excluded(Field)}. + *

+ * This excludes any {@link InsertOnDuplicateStep#onConflict(Field...)} + * columns. + */ + @NotNull @CheckReturnValue + @Support({ CUBRID, DERBY, DUCKDB, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES, SQLITE, YUGABYTEDB }) + InsertOnDuplicateSetMoreStep setNonConflictingKeyToExcluded(); + } diff --git a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java index 88fd9dec9d..7a3a2ef443 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/ParserImpl.java @@ -2452,8 +2452,15 @@ final class DefaultParseContext extends AbstractScope implements ParseContext { parseKeywordIf("SET"); // Cast is necessary, see https://github.com/eclipse-jdt/eclipse.jdt.core/issues/99 - InsertOnConflictWhereStep where = parseKeywordIf("ALL TO EXCLUDED") + InsertOnConflictWhereStep where = + parseKeywordIf("ALL TO EXCLUDED") ? onDuplicate.onDuplicateKeyUpdate().setAllToExcluded() + : parseKeywordIf("NON KEY TO EXCLUDED") + ? onDuplicate.onDuplicateKeyUpdate().setNonKeyToExcluded() + : parseKeywordIf("NON PRIMARY KEY TO EXCLUDED") + ? onDuplicate.onDuplicateKeyUpdate().setNonPrimaryKeyToExcluded() + : parseKeywordIf("NON CONFLICTING KEY TO EXCLUDED") + ? onDuplicate.onDuplicateKeyUpdate().setNonConflictingKeyToExcluded() : onDuplicate.onDuplicateKeyUpdate().set((Map) data(DATA_PARSE_ON_CONFLICT, true, c -> c.parseSetClauseList())); if (parseKeywordIf("WHERE")) @@ -2489,8 +2496,15 @@ final class DefaultParseContext extends AbstractScope implements ParseContext { else if (parseKeywordIf("UPDATE SET")) { // Cast is necessary, see https://github.com/eclipse-jdt/eclipse.jdt.core/issues/99 - InsertOnConflictWhereStep where = parseKeywordIf("ALL TO EXCLUDED") + InsertOnConflictWhereStep where = + parseKeywordIf("ALL TO EXCLUDED") ? doUpdate.doUpdate().setAllToExcluded() + : parseKeywordIf("NON KEY TO EXCLUDED") + ? doUpdate.doUpdate().setNonKeyToExcluded() + : parseKeywordIf("NON PRIMARY KEY TO EXCLUDED") + ? doUpdate.doUpdate().setNonPrimaryKeyToExcluded() + : parseKeywordIf("NON CONFLICTING KEY TO EXCLUDED") + ? doUpdate.doUpdate().setNonConflictingKeyToExcluded() : doUpdate.doUpdate().set((Map) data(DATA_PARSE_ON_CONFLICT, true, c -> c.parseSetClauseList())); if (parseKeywordIf("WHERE"))