From 22b10723a6e5242dc1824507cd4cbe45e7e7f264 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Tue, 17 Nov 2020 12:39:03 +0100 Subject: [PATCH] [jOOQ/jOOQ#8640] Don't throw exceptions if ON DUPLICATE KEY UPDATE isn't supported --- .../java/org/jooq/impl/InsertQueryImpl.java | 82 +++++++++---------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java b/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java index a09b898d8d..8b9be988b3 100644 --- a/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java +++ b/jOOQ/src/main/java/org/jooq/impl/InsertQueryImpl.java @@ -305,46 +305,6 @@ final class InsertQueryImpl extends AbstractStoreQuery impl if (onDuplicateKeyUpdate) { switch (ctx.family()) { - // MySQL has a nice syntax for this - - - - - case CUBRID: - case MARIADB: - case MYSQL: { - - // [#2508] In H2, this syntax is supported in MySQL MODE (we're assuming users will - // set this mode in order to profit from this functionality). Up until - // H2 1.4.197, qualification of columns in the ON DUPLICATE KEY UPDATE clause - // wasn't supported (see https://github.com/h2database/h2database/issues/1027) - boolean oldQualify = ctx.qualify(); - boolean newQualify = ctx.family() == H2 ? false : oldQualify; - - toSQLInsert(ctx); - ctx.formatSeparator() - .start(INSERT_ON_DUPLICATE_KEY_UPDATE) - .visit(K_ON_DUPLICATE_KEY_UPDATE) - .formatIndentStart() - .formatSeparator() - .qualify(newQualify); - - // [#8479] Emulate WHERE clause using CASE - if (condition.hasWhere()) - ctx.data(DATA_ON_DUPLICATE_KEY_WHERE, condition.getWhere()); - - ctx.visit(updateMap); - - if (condition.hasWhere()) - ctx.data().remove(DATA_ON_DUPLICATE_KEY_WHERE); - - ctx.qualify(oldQualify) - .formatIndentEnd() - .end(INSERT_ON_DUPLICATE_KEY_UPDATE); - - break; - } - @@ -450,8 +410,46 @@ final class InsertQueryImpl extends AbstractStoreQuery impl break; } - default: - throw new SQLDialectNotSupportedException("The ON DUPLICATE KEY UPDATE clause cannot be emulated for " + ctx.dialect()); + // MySQL has a nice syntax for this + + + + + case CUBRID: + case MARIADB: + case MYSQL: + default: { + + // [#2508] In H2, this syntax is supported in MySQL MODE (we're assuming users will + // set this mode in order to profit from this functionality). Up until + // H2 1.4.197, qualification of columns in the ON DUPLICATE KEY UPDATE clause + // wasn't supported (see https://github.com/h2database/h2database/issues/1027) + boolean oldQualify = ctx.qualify(); + boolean newQualify = ctx.family() == H2 ? false : oldQualify; + + toSQLInsert(ctx); + ctx.formatSeparator() + .start(INSERT_ON_DUPLICATE_KEY_UPDATE) + .visit(K_ON_DUPLICATE_KEY_UPDATE) + .formatIndentStart() + .formatSeparator() + .qualify(newQualify); + + // [#8479] Emulate WHERE clause using CASE + if (condition.hasWhere()) + ctx.data(DATA_ON_DUPLICATE_KEY_WHERE, condition.getWhere()); + + ctx.visit(updateMap); + + if (condition.hasWhere()) + ctx.data().remove(DATA_ON_DUPLICATE_KEY_WHERE); + + ctx.qualify(oldQualify) + .formatIndentEnd() + .end(INSERT_ON_DUPLICATE_KEY_UPDATE); + + break; + } } }