diff --git a/jOOQ/src/main/java/org/jooq/impl/Abs.java b/jOOQ/src/main/java/org/jooq/impl/Abs.java index c54df3241e..ed7e517454 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Abs.java +++ b/jOOQ/src/main/java/org/jooq/impl/Abs.java @@ -57,7 +57,7 @@ import java.util.*; * The ABS statement. */ @SuppressWarnings({ "rawtypes", "unchecked", "unused" }) -final class Abs +final class Abs extends AbstractField { diff --git a/jOOQ/src/main/java/org/jooq/impl/Ceil.java b/jOOQ/src/main/java/org/jooq/impl/Ceil.java index d157830f2e..b6a82854d6 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Ceil.java +++ b/jOOQ/src/main/java/org/jooq/impl/Ceil.java @@ -37,30 +37,49 @@ */ package org.jooq.impl; -import static org.jooq.impl.Names.N_CEIL; -import static org.jooq.impl.Names.N_CEILING; +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.SQLDialect.*; + +import org.jooq.*; +import org.jooq.impl.*; +import org.jooq.tools.*; + +import java.util.*; -import org.jooq.Context; -import org.jooq.Field; /** - * @author Lukas Eder + * The CEIL statement. */ -final class Ceil extends AbstractField { +@SuppressWarnings({ "rawtypes", "unchecked", "unused" }) +final class Ceil +extends + AbstractField +{ - /** - * Generated UID - */ - private static final long serialVersionUID = -7273879239726265322L; + private static final long serialVersionUID = 1L; - private final Field argument; + private final Field value; - Ceil(Field argument) { - super(N_CEIL, argument.getDataType()); + Ceil( + Field value + ) { + super(N_CEIL, allNotNull((DataType) dataType(INTEGER, value, false), value)); - this.argument = argument; + this.value = nullSafeNotNull(value, INTEGER); } + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + + + @Override public final void accept(Context ctx) { switch (ctx.family()) { @@ -73,8 +92,8 @@ final class Ceil extends AbstractField { // [#8275] Improved emulation for SQLite case SQLITE: - Field cast = DSL.cast(argument, SQLDataType.BIGINT); - ctx.sql('(').visit(cast).sql(" + (").visit(argument).sql(" > ").visit(cast).sql("))"); + Field cast = DSL.cast(value, SQLDataType.BIGINT); + ctx.sql('(').visit(cast).sql(" + (").visit(value).sql(" > ").visit(cast).sql("))"); break; @@ -83,12 +102,29 @@ final class Ceil extends AbstractField { case H2: - ctx.visit(N_CEILING).sql('(').visit(argument).sql(')'); + ctx.visit(N_CEILING).sql('(').visit(value).sql(')'); break; default: - ctx.visit(N_CEIL).sql('(').visit(argument).sql(')'); + ctx.visit(N_CEIL).sql('(').visit(value).sql(')'); break; } } + + + + // ------------------------------------------------------------------------- + // The Object API + // ------------------------------------------------------------------------- + + @Override + public boolean equals(Object that) { + if (that instanceof Ceil) { + return + StringUtils.equals(value, ((Ceil) that).value) + ; + } + else + return super.equals(that); + } } diff --git a/jOOQ/src/main/java/org/jooq/impl/DSL.java b/jOOQ/src/main/java/org/jooq/impl/DSL.java index 8dd422b71c..8a25ebad13 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DSL.java +++ b/jOOQ/src/main/java/org/jooq/impl/DSL.java @@ -14946,6 +14946,28 @@ public class DSL { return new BitLength(string); } + /** + * The CEIL function. + *

+ * Get the smallest integer value equal or greater to a value. + */ + @NotNull + @Support + public static Field ceil(T value) { + return new Ceil(Tools.field(value)); + } + + /** + * The CEIL function. + *

+ * Get the smallest integer value equal or greater to a value. + */ + @NotNull + @Support + public static Field ceil(Field value) { + return new Ceil(value); + } + /** * The CHAR_LENGTH function. *

@@ -15066,6 +15088,28 @@ public class DSL { return new Degrees(radians); } + /** + * The FLOOR function. + *

+ * Get the biggest integer value equal or less than a value. + */ + @NotNull + @Support + public static Field floor(T value) { + return new Floor(Tools.field(value)); + } + + /** + * The FLOOR function. + *

+ * Get the biggest integer value equal or less than a value. + */ + @NotNull + @Support + public static Field floor(Field value) { + return new Floor(value); + } + /** * The LEFT function. *

@@ -16800,6 +16844,62 @@ public class DSL { return new Trim(string); } + /** + * The TRUNC function. + *

+ * Truncate a number to a given number of decimals. + * + * @param value The number to be truncated + * @param decimals The decimals to truncate to. + */ + @NotNull + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) + public static Field trunc(T value, int decimals) { + return new Trunc(Tools.field(value), Tools.field(decimals)); + } + + /** + * The TRUNC function. + *

+ * Truncate a number to a given number of decimals. + * + * @param value The number to be truncated + * @param decimals The decimals to truncate to. + */ + @NotNull + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) + public static Field trunc(T value, Field decimals) { + return new Trunc(Tools.field(value), decimals); + } + + /** + * The TRUNC function. + *

+ * Truncate a number to a given number of decimals. + * + * @param value The number to be truncated + * @param decimals The decimals to truncate to. + */ + @NotNull + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) + public static Field trunc(Field value, int decimals) { + return new Trunc(value, Tools.field(decimals)); + } + + /** + * The TRUNC function. + *

+ * Truncate a number to a given number of decimals. + * + * @param value The number to be truncated + * @param decimals The decimals to truncate to. + */ + @NotNull + @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) + public static Field trunc(Field value, Field decimals) { + return new Trunc(value, decimals); + } + /** * The UPPER function. *

@@ -20201,57 +20301,6 @@ public class DSL { return field.neg(); } - /** - * Get the largest integer value not greater than [this]. - * - * @see #floor(Field) - */ - @NotNull - @Support - public static Field floor(T value) { - return floor(Tools.field(value)); - } - - /** - * Get the largest integer value not greater than [this]. - *

- * This renders the floor function where available: - *

floor([this])
- * ... or emulates it elsewhere using round: - *
round([this] - 0.499999999999999)
- */ - @NotNull - @Support - public static Field floor(Field field) { - return new Floor<>(Tools.nullSafe(field)); - } - - /** - * Get the smallest integer value not less than [this]. - * - * @see #ceil(Field) - */ - @NotNull - @Support - public static Field ceil(T value) { - return ceil(Tools.field(value)); - } - - /** - * Get the smallest integer value not less than [field]. - *

- * This renders the ceil or ceiling function where available: - *

ceil([field]) or
-     * ceiling([field])
- * ... or emulates it elsewhere using round: - *
round([field] + 0.499999999999999)
- */ - @NotNull - @Support - public static Field ceil(Field field) { - return new Ceil<>(Tools.nullSafe(field)); - } - /** * Truncate a number to a given number of decimals. * @@ -20263,88 +20312,6 @@ public class DSL { return trunc(Tools.field(number), inline(0)); } - /** - * Truncate a number to a given number of decimals. - * - * @see #trunc(Field, Field) - */ - @NotNull - @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) - public static Field trunc(T number, int decimals) { - return trunc(Tools.field(number), inline(decimals)); - } - - /** - * Truncate a number to a given number of decimals. - * - * @see #trunc(Field, Field) - */ - @NotNull - @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) - public static Field trunc(Field number, int decimals) { - return trunc(Tools.nullSafe(number), inline(decimals)); - } - - /** - * Truncate a number to a given number of decimals. - * - * @see #trunc(Field, Field) - */ - @NotNull - @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) - public static Field trunc(T number, Field decimals) { - return trunc(Tools.field(number), Tools.nullSafe(decimals)); - } - - /** - * Truncate a number to a given number of decimals. - *

- * This function truncates number to the amount of decimals - * specified in decimals. Passing decimals = 0 to - * this function is the same as using {@link #floor(Field)}. Passing - * positive values for decimal has a similar effect as - * {@link #round(Field, int)}. Passing negative values for - * decimal will truncate number to a given power - * of 10. Some examples - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - * - *
Function callyields...
trunc(125.815)125
trunc(125.815, 0)125
trunc(125.815, 1)125.8
trunc(125.815, 2)125.81
trunc(125.815, -1)120
trunc(125.815, -2)100
- * - * @see #trunc(Field, Field) - */ - @NotNull - @Support({ CUBRID, DERBY, FIREBIRD, H2, HSQLDB, MARIADB, MYSQL, POSTGRES }) - public static Field trunc(Field number, Field decimals) { - return new Trunc<>(Tools.nullSafe(number), Tools.nullSafe(decimals)); - } - /** * Get the sqrt(field) function. * diff --git a/jOOQ/src/main/java/org/jooq/impl/Floor.java b/jOOQ/src/main/java/org/jooq/impl/Floor.java index 4b142ab8d3..0e30222905 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Floor.java +++ b/jOOQ/src/main/java/org/jooq/impl/Floor.java @@ -37,29 +37,49 @@ */ package org.jooq.impl; -import static org.jooq.impl.Names.N_FLOOR; +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.SQLDialect.*; + +import org.jooq.*; +import org.jooq.impl.*; +import org.jooq.tools.*; + +import java.util.*; -import org.jooq.Context; -import org.jooq.Field; /** - * @author Lukas Eder + * The FLOOR statement. */ -final class Floor extends AbstractField { +@SuppressWarnings({ "rawtypes", "unchecked", "unused" }) +final class Floor +extends + AbstractField +{ - /** - * Generated UID - */ - private static final long serialVersionUID = -7273879239726265322L; + private static final long serialVersionUID = 1L; - private final Field argument; + private final Field value; - Floor(Field argument) { - super(N_FLOOR, argument.getDataType()); + Floor( + Field value + ) { + super(N_FLOOR, allNotNull((DataType) dataType(INTEGER, value, false), value)); - this.argument = argument; + this.value = nullSafeNotNull(value, INTEGER); } + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + + + @Override public final void accept(Context ctx) { switch (ctx.family()) { @@ -70,13 +90,30 @@ final class Floor extends AbstractField { // [#8275] Improved emulation for SQLite case SQLITE: - Field cast = DSL.cast(argument, SQLDataType.BIGINT); - ctx.sql('(').visit(cast).sql(" - (").visit(argument).sql(" < ").visit(cast).sql("))"); + Field cast = DSL.cast(value, SQLDataType.BIGINT); + ctx.sql('(').visit(cast).sql(" - (").visit(value).sql(" < ").visit(cast).sql("))"); break; default: - ctx.visit(N_FLOOR).sql('(').visit(argument).sql(')'); + ctx.visit(N_FLOOR).sql('(').visit(value).sql(')'); break; } } + + + + // ------------------------------------------------------------------------- + // The Object API + // ------------------------------------------------------------------------- + + @Override + public boolean equals(Object that) { + if (that instanceof Floor) { + return + StringUtils.equals(value, ((Floor) that).value) + ; + } + else + return super.equals(that); + } } diff --git a/jOOQ/src/main/java/org/jooq/impl/Trunc.java b/jOOQ/src/main/java/org/jooq/impl/Trunc.java index 506d41ab5f..7555a6a9b0 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Trunc.java +++ b/jOOQ/src/main/java/org/jooq/impl/Trunc.java @@ -37,45 +37,52 @@ */ package org.jooq.impl; -import static java.math.BigDecimal.TEN; -import static org.jooq.impl.DSL.inline; -import static org.jooq.impl.DSL.zero; -import static org.jooq.impl.Internal.idiv; -import static org.jooq.impl.Internal.imul; -import static org.jooq.impl.Names.N_ROUND; -import static org.jooq.impl.Names.N_ROUND_DOWN; -import static org.jooq.impl.Names.N_TRUNC; -import static org.jooq.impl.Names.N_TRUNCATE; -import static org.jooq.impl.Names.N_TRUNCNUM; -import static org.jooq.impl.Tools.castIfNeeded; -import static org.jooq.impl.Tools.extractParamValue; +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.SQLDialect.*; -import java.math.BigDecimal; -import java.math.MathContext; +import org.jooq.*; +import org.jooq.impl.*; +import org.jooq.tools.*; + +import java.util.*; -import org.jooq.Context; -import org.jooq.Field; /** - * @author Lukas Eder + * The TRUNC statement. */ -final class Trunc extends AbstractField { +@SuppressWarnings({ "rawtypes", "unchecked", "unused" }) +final class Trunc +extends + AbstractField +{ - /** - * Generated UID - */ - private static final long serialVersionUID = 4291348230758816484L; + private static final long serialVersionUID = 1L; - private final Field field; + private final Field value; private final Field decimals; - Trunc(Field field, Field decimals) { - super(N_TRUNC, field.getDataType()); + Trunc( + Field value, + Field decimals + ) { + super(N_TRUNC, allNotNull((DataType) dataType(INTEGER, value, false), value, decimals)); - this.field = field; - this.decimals = decimals; + this.value = nullSafeNotNull(value, INTEGER); + this.decimals = nullSafeNotNull(decimals, INTEGER); } + // ------------------------------------------------------------------------- + // XXX: QueryPart API + // ------------------------------------------------------------------------- + + + @Override public final void accept(Context ctx) { switch (ctx.family()) { @@ -84,19 +91,19 @@ final class Trunc extends AbstractField { case DERBY: { - Field power; + Field power; // [#1334] if possible, calculate the power in Java to prevent // inaccurate arithmetics in the Derby database Integer decimalsVal = extractParamValue(decimals); if (decimalsVal != null) - power = inline(TEN.pow(decimalsVal, MathContext.DECIMAL128)); + power = inline(java.math.BigDecimal.TEN.pow(decimalsVal, java.math.MathContext.DECIMAL128)); else - power = DSL.power(inline(TEN), decimals); + power = DSL.power(inline(java.math.BigDecimal.TEN), decimals); ctx.visit(DSL.decode() - .when(field.sign().greaterOrEqual(zero()), idiv(imul(field, power).floor(), power)) - .otherwise(idiv(imul(field, power).ceil(), power))); + .when(value.sign().greaterOrEqual(zero()), idiv(imul(value, power).floor(), power)) + .otherwise(idiv(imul(value, power).ceil(), power))); break; } @@ -107,7 +114,7 @@ final class Trunc extends AbstractField { case H2: case MARIADB: case MYSQL: - ctx.visit(N_TRUNCATE).sql('(').visit(field).sql(", ").visit(decimals).sql(')'); + ctx.visit(N_TRUNCATE).sql('(').visit(value).sql(", ").visit(decimals).sql(')'); break; // Postgres TRUNC() only takes NUMERIC arguments, no @@ -118,11 +125,11 @@ final class Trunc extends AbstractField { case POSTGRES: ctx.visit(castIfNeeded( - DSL.function("trunc", SQLDataType.NUMERIC, - castIfNeeded(field, BigDecimal.class), + DSL.function("trunc", NUMERIC, + castIfNeeded(value, NUMERIC), decimals ), - field.getDataType() + value.getDataType() )); break; @@ -150,8 +157,26 @@ final class Trunc extends AbstractField { case CUBRID: case HSQLDB: default: - ctx.visit(N_TRUNC).sql('(').visit(field).sql(", ").visit(decimals).sql(')'); + ctx.visit(N_TRUNC).sql('(').visit(value).sql(", ").visit(decimals).sql(')'); break; } } + + + + // ------------------------------------------------------------------------- + // The Object API + // ------------------------------------------------------------------------- + + @Override + public boolean equals(Object that) { + if (that instanceof Trunc) { + return + StringUtils.equals(value, ((Trunc) that).value) && + StringUtils.equals(decimals, ((Trunc) that).decimals) + ; + } + else + return super.equals(that); + } }