From 9587b26e153b03d8f9cc7109a1965dd109f9dc9f Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Tue, 14 Sep 2021 12:46:08 +0200 Subject: [PATCH] [jOOQ/jOOQ#12425] Move NVL functions to API generator --- .../src/main/java/org/jooq/impl/ArrayGet.java | 6 +- jOOQ/src/main/java/org/jooq/impl/DSL.java | 135 ++++++++---------- jOOQ/src/main/java/org/jooq/impl/DateAdd.java | 12 +- jOOQ/src/main/java/org/jooq/impl/Nvl.java | 102 ++++++++++--- .../main/java/org/jooq/impl/Xmlserialize.java | 2 +- 5 files changed, 152 insertions(+), 105 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/ArrayGet.java b/jOOQ/src/main/java/org/jooq/impl/ArrayGet.java index d71aa503d3..20ade67f18 100644 --- a/jOOQ/src/main/java/org/jooq/impl/ArrayGet.java +++ b/jOOQ/src/main/java/org/jooq/impl/ArrayGet.java @@ -75,11 +75,11 @@ extends ) { super( N_ARRAY_GET, - allNotNull((DataType) StringUtils.defaultIfNull(array.getDataType().getArrayComponentDataType(), OTHER)) + allNotNull((DataType) StringUtils.defaultIfNull(array.getDataType().getArrayComponentDataType(), OTHER), array, index) ); - this.array = array; - this.index = index; + this.array = nullSafeNotNull(array, (DataType) OTHER); + this.index = nullSafeNotNull(index, (DataType) OTHER); } // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 37a072de69..596c068ba9 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -15626,73 +15626,6 @@ public class DSL { return nvl(value, defaultValue); } - /** - * Gets the Oracle-style NVL(value, defaultValue) function. - * - * @see #nvl(Field, Field) - */ - @NotNull - @Support - public static Field nvl(T value, T defaultValue) { - return nvl0(Tools.field(value), Tools.field(defaultValue)); - } - - /** - * Gets the Oracle-style NVL(value, defaultValue) function. - * - * @see #nvl(Field, Field) - */ - @NotNull - @Support - public static Field nvl(T value, Field defaultValue) { - return nvl0(Tools.field(value, defaultValue), Tools.nullSafe(defaultValue)); - } - - /** - * Gets the Oracle-style NVL(value, defaultValue) function. - * - * @see #nvl(Field, Field) - */ - @NotNull - @Support - public static Field nvl(Field value, T defaultValue) { - return nvl0(Tools.nullSafe(value), Tools.field(defaultValue, value)); - } - - /** - * Gets the Oracle-style NVL(value, defaultValue) function. - *

- * Returns the dialect's equivalent to NVL: - *

- */ - @NotNull - @Support - public static Field nvl(Field value, Field defaultValue) { - return nvl0(value, defaultValue); - } - /** * The IFNULL() function, a synonym of NVL(). * @@ -15737,12 +15670,6 @@ public class DSL { return nvl(value, defaultValue); } - // Java 8 is stricter than Java 7 with respect to generics and overload - // resolution (http://stackoverflow.com/q/5361513/521799) - static Field nvl0(Field value, Field defaultValue) { - return new Nvl<>(Tools.nullSafe(value), Tools.nullSafe(defaultValue)); - } - /** * Gets the Oracle-style NVL2(value, valueIfNotNull, valueIfNull) function. * @@ -16697,7 +16624,7 @@ public class DSL { @NotNull @Support public static Field widthBucket(Field field, T low, T high, int buckets) { - return new WidthBucket(field, Tools.field(low), Tools.field(high), Tools.field(buckets)); + return new WidthBucket(field, Tools.field(low, field), Tools.field(high, field), Tools.field(buckets)); } /** @@ -19010,6 +18937,66 @@ public class DSL { return new ArrayGet(array, index); } + // ------------------------------------------------------------------------- + // Utility functions + // ------------------------------------------------------------------------- + + /** + * The NVL function. + *

+ * Return the first non-null argument. + * + * @param value The nullable value. + * @param defaultValue The default value if the other value is null. + */ + @NotNull + @Support + public static Field nvl(T value, T defaultValue) { + return new Nvl(Tools.field(value), Tools.field(defaultValue)); + } + + /** + * The NVL function. + *

+ * Return the first non-null argument. + * + * @param value The nullable value. + * @param defaultValue The default value if the other value is null. + */ + @NotNull + @Support + public static Field nvl(T value, Field defaultValue) { + return new Nvl(Tools.field(value), defaultValue); + } + + /** + * The NVL function. + *

+ * Return the first non-null argument. + * + * @param value The nullable value. + * @param defaultValue The default value if the other value is null. + */ + @NotNull + @Support + public static Field nvl(Field value, T defaultValue) { + return new Nvl(value, Tools.field(defaultValue, value)); + } + + /** + * The NVL function. + *

+ * Return the first non-null argument. + * + * @param value The nullable value. + * @param defaultValue The default value if the other value is null. + */ + @NotNull + @Support + public static Field nvl(Field value, Field defaultValue) { + return new Nvl(value, defaultValue); + } + // ------------------------------------------------------------------------- // System functions // ------------------------------------------------------------------------- diff --git a/jOOQ/src/main/java/org/jooq/impl/DateAdd.java b/jOOQ/src/main/java/org/jooq/impl/DateAdd.java index 57cb4797c8..d16278101c 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DateAdd.java +++ b/jOOQ/src/main/java/org/jooq/impl/DateAdd.java @@ -76,11 +76,11 @@ extends ) { super( N_DATE_ADD, - allNotNull((DataType) dataType(date)) + allNotNull((DataType) dataType(date), date, interval) ); - this.date = date; - this.interval = interval; + this.date = nullSafeNotNull(date, (DataType) OTHER); + this.interval = nullSafeNotNull(interval, (DataType) OTHER); this.datePart = null; } @@ -91,11 +91,11 @@ extends ) { super( N_DATE_ADD, - allNotNull((DataType) dataType(date)) + allNotNull((DataType) dataType(date), date, interval) ); - this.date = date; - this.interval = interval; + this.date = nullSafeNotNull(date, (DataType) OTHER); + this.interval = nullSafeNotNull(interval, (DataType) OTHER); this.datePart = datePart; } diff --git a/jOOQ/src/main/java/org/jooq/impl/Nvl.java b/jOOQ/src/main/java/org/jooq/impl/Nvl.java index a42cb67ee2..535c60d449 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Nvl.java +++ b/jOOQ/src/main/java/org/jooq/impl/Nvl.java @@ -37,32 +37,55 @@ */ package org.jooq.impl; -import static org.jooq.impl.DSL.function; -import static org.jooq.impl.DSL.select; -import static org.jooq.impl.Keywords.K_IS_NULL; -import static org.jooq.impl.Names.N_IFNULL; -import static org.jooq.impl.Names.N_IIF; -import static org.jooq.impl.Names.N_NVL; -import static org.jooq.impl.Tools.anyNotNull; +import static org.jooq.impl.DSL.*; +import static org.jooq.impl.Internal.*; +import static org.jooq.impl.Keywords.*; +import static org.jooq.impl.Names.*; +import static org.jooq.impl.SQLDataType.*; +import static org.jooq.impl.Tools.*; +import static org.jooq.impl.Tools.BooleanDataKey.*; +import static org.jooq.impl.Tools.DataExtendedKey.*; +import static org.jooq.impl.Tools.DataKey.*; +import static org.jooq.SQLDialect.*; + +import org.jooq.*; +import org.jooq.Record; +import org.jooq.conf.*; +import org.jooq.impl.*; +import org.jooq.tools.*; + +import java.util.*; -import org.jooq.Context; -import org.jooq.Field; /** - * @author Lukas Eder + * The NVL statement. */ -final class Nvl extends AbstractField { +@SuppressWarnings({ "rawtypes", "unchecked", "unused" }) +final class Nvl +extends + AbstractField +{ - private final Field arg1; - private final Field arg2; + private final Field value; + private final Field defaultValue; - Nvl(Field arg1, Field arg2) { - super(N_NVL, anyNotNull(arg1.getDataType(), arg1, arg2)); + Nvl( + Field value, + Field defaultValue + ) { + super( + N_NVL, + anyNotNull((DataType) dataType(value), value, defaultValue) + ); - this.arg1 = arg1; - this.arg2 = arg2; + this.value = nullSafeNotNull(value, (DataType) OTHER); + this.defaultValue = nullSafeNotNull(defaultValue, (DataType) OTHER); } + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + @Override public final void accept(Context ctx) { switch (ctx.family()) { @@ -85,6 +108,14 @@ final class Nvl extends AbstractField { + + + + + + + + @@ -96,11 +127,11 @@ final class Nvl extends AbstractField { case CUBRID: case DERBY: - case IGNITE: case FIREBIRD: + case IGNITE: case POSTGRES: case YUGABYTE: - ctx.visit(DSL.coalesce(arg1, arg2)); + ctx.visit(DSL.coalesce(value, defaultValue)); break; @@ -109,12 +140,41 @@ final class Nvl extends AbstractField { case MARIADB: case MYSQL: case SQLITE: - ctx.visit(function(N_IFNULL, getDataType(), arg1, arg2)); + ctx.visit(function(N_IFNULL, getDataType(), value, defaultValue)); break; default: - ctx.visit(function(N_NVL, getDataType(), arg1, arg2)); + ctx.visit(function(N_NVL, getDataType(), value, defaultValue)); break; } } + + + + + + + + + + + + + + + // ------------------------------------------------------------------------- + // The Object API + // ------------------------------------------------------------------------- + + @Override + public boolean equals(Object that) { + if (that instanceof Nvl) { + return + StringUtils.equals(value, ((Nvl) that).value) && + StringUtils.equals(defaultValue, ((Nvl) that).defaultValue) + ; + } + else + return super.equals(that); + } } diff --git a/jOOQ/src/main/java/org/jooq/impl/Xmlserialize.java b/jOOQ/src/main/java/org/jooq/impl/Xmlserialize.java index 1df3a42b9a..58d210952d 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Xmlserialize.java +++ b/jOOQ/src/main/java/org/jooq/impl/Xmlserialize.java @@ -81,7 +81,7 @@ extends ); this.content = content; - this.value = value; + this.value = nullSafeNotNull(value, (DataType) OTHER); this.type = type; }