From 7cfee02d44382193450fe84666b03d08f9675ea3 Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 25 May 2022 09:32:20 +0200 Subject: [PATCH] [jOOQ/jOOQ#13601] Derby boolean expressions have to be wrapped in parentheses when passed to COALESCE --- .../org/jooq/impl/AbstractDelegateField.java | 61 ++++++++++++++++++ .../src/main/java/org/jooq/impl/Coalesce.java | 8 +++ jOOQ/src/main/java/org/jooq/impl/Nvl.java | 9 ++- .../org/jooq/impl/ParenthesisedField.java | 63 +++++++++++++++++++ 4 files changed, 139 insertions(+), 2 deletions(-) create mode 100644 jOOQ/src/main/java/org/jooq/impl/AbstractDelegateField.java create mode 100644 jOOQ/src/main/java/org/jooq/impl/ParenthesisedField.java diff --git a/jOOQ/src/main/java/org/jooq/impl/AbstractDelegateField.java b/jOOQ/src/main/java/org/jooq/impl/AbstractDelegateField.java new file mode 100644 index 0000000000..8b58559c48 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/AbstractDelegateField.java @@ -0,0 +1,61 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.impl; + +import org.jooq.Context; +import org.jooq.Field; +import org.jooq.impl.QOM.UTransient; + +/** + * @author Lukas Eder + */ +abstract class AbstractDelegateField extends AbstractField implements UTransient { + + private Field delegate; + + AbstractDelegateField(Field delegate) { + super(delegate.getQualifiedName(), delegate.getDataType(), delegate.getCommentPart()); + + this.delegate = delegate; + } + + @Override + public /* non-final */ void accept(Context ctx) { + ctx.visit(delegate); + } +} diff --git a/jOOQ/src/main/java/org/jooq/impl/Coalesce.java b/jOOQ/src/main/java/org/jooq/impl/Coalesce.java index 9b98f7fe9b..ff7bcd3a5f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Coalesce.java +++ b/jOOQ/src/main/java/org/jooq/impl/Coalesce.java @@ -78,6 +78,14 @@ final class Coalesce extends AbstractField implements QOM.Coalesce { + case DERBY: { + // [#13601] Workaround for https://issues.apache.org/jira/browse/DERBY-7139 + ctx.visit(DSL.function(N_COALESCE, getDataType(), + Tools.map(fields, f -> f.getType() == Boolean.class ? new ParenthesisedField<>(f) : f, Field[]::new) + )); + break; + } + default: { ctx.visit(DSL.function(N_COALESCE, getDataType(), fields)); break; diff --git a/jOOQ/src/main/java/org/jooq/impl/Nvl.java b/jOOQ/src/main/java/org/jooq/impl/Nvl.java index 5e4128200c..79e9a72759 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Nvl.java +++ b/jOOQ/src/main/java/org/jooq/impl/Nvl.java @@ -103,6 +103,9 @@ implements + case DERBY: + return false; + @@ -116,7 +119,6 @@ implements case CUBRID: - case DERBY: case FIREBIRD: case IGNITE: case POSTGRES: @@ -162,6 +164,10 @@ implements + + case DERBY: + ctx.visit(DSL.coalesce(value, defaultValue)); + break; @@ -176,7 +182,6 @@ implements case CUBRID: - case DERBY: case FIREBIRD: case IGNITE: case POSTGRES: diff --git a/jOOQ/src/main/java/org/jooq/impl/ParenthesisedField.java b/jOOQ/src/main/java/org/jooq/impl/ParenthesisedField.java new file mode 100644 index 0000000000..36c48b87d7 --- /dev/null +++ b/jOOQ/src/main/java/org/jooq/impl/ParenthesisedField.java @@ -0,0 +1,63 @@ +/* + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Other licenses: + * ----------------------------------------------------------------------------- + * Commercial licenses for this work are available. These replace the above + * ASL 2.0 and offer limited warranties, support, maintenance, and commercial + * database integrations. + * + * For more information, please visit: http://www.jooq.org/licenses + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + * + */ +package org.jooq.impl; + +import org.jooq.Context; +import org.jooq.Field; + +/** + * @author Lukas Eder + */ +final class ParenthesisedField extends AbstractDelegateField { + + ParenthesisedField(Field delegate) { + super(delegate); + } + + @Override + final boolean parenthesised(Context ctx) { + return true; + } + + @Override + public final void accept(Context ctx) { + ctx.sql('('); + super.accept(ctx); + ctx.sql(')'); + } +}