From a0dacbcc0eeeca990daf859599ecd88dd15c64dc Mon Sep 17 00:00:00 2001 From: Lukas Eder Date: Wed, 4 Apr 2012 20:23:23 +0000 Subject: [PATCH] [#585] Add support for DATE, TIME and INTERVAL arithmetic - Implementation done for HSQLDB, implementation attempts for Ingres, Postgres --- .../src/main/java/org/jooq/impl/DateDiff.java | 2 + .../main/java/org/jooq/impl/Expression.java | 35 ++++++++++-- jOOQ/src/main/java/org/jooq/impl/Factory.java | 53 ------------------- .../java/org/jooq/impl/TimestampDiff.java | 2 + jOOQ/src/main/java/org/jooq/impl/Val.java | 2 +- 5 files changed, 36 insertions(+), 58 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/impl/DateDiff.java b/jOOQ/src/main/java/org/jooq/impl/DateDiff.java index e3ac7c1846..897a18e3f8 100644 --- a/jOOQ/src/main/java/org/jooq/impl/DateDiff.java +++ b/jOOQ/src/main/java/org/jooq/impl/DateDiff.java @@ -82,10 +82,12 @@ class DateDiff extends AbstractFunction { getDataType(), field("SQL_TSI_DAY"), date2, date1); case H2: + case HSQLDB: return function("datediff", getDataType(), literal("'day'"), date2, date1); case CUBRID: case ORACLE: + case POSTGRES: // TODO [#585] This cast shouldn't be necessary return date1.sub(date2).cast(Integer.class); diff --git a/jOOQ/src/main/java/org/jooq/impl/Expression.java b/jOOQ/src/main/java/org/jooq/impl/Expression.java index 180477e859..f64dca8c2f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Expression.java +++ b/jOOQ/src/main/java/org/jooq/impl/Expression.java @@ -66,6 +66,7 @@ import static org.jooq.impl.Factory.function; import static org.jooq.impl.Factory.literal; import static org.jooq.impl.Factory.val; +import java.sql.Date; import java.sql.Timestamp; import java.util.Arrays; import java.util.List; @@ -220,6 +221,9 @@ class Expression extends AbstractFunction { } } + /** + * Return the expression to be rendered when the RHS is an interval type + */ private final Field getIntervalExpression(Configuration configuration) { SQLDialect dialect = configuration.getDialect(); @@ -266,8 +270,7 @@ class Expression extends AbstractFunction { } } - case DB2: - case HSQLDB: { + case DB2: { if (rhs.get(0).getType() == YearToMonth.class) { YearToMonth interval = ((Param) rhs.get(0)).getValue(); @@ -280,7 +283,6 @@ class Expression extends AbstractFunction { } else { DayToSecond interval = ((Param) rhs.get(0)).getValue(); - if (operator == ADD) { return (Field) lhs.cast(Timestamp.class).add(new IntervalLiteral(val(interval.getTotalMicro()), "microseconds")); } @@ -290,7 +292,8 @@ class Expression extends AbstractFunction { } } - case DERBY: { + case DERBY: + case HSQLDB: { if (rhs.get(0).getType() == YearToMonth.class) { YearToMonth interval = ((Param) rhs.get(0)).getValue(); @@ -349,11 +352,15 @@ class Expression extends AbstractFunction { } } + case ORACLE: default: return new DefaultExpression(); } } + /** + * Return the expression to be rendered when the RHS is a number type + */ private final Field getNumberExpression(Configuration configuration) { switch (configuration.getDialect()) { case ASE: { @@ -396,6 +403,26 @@ class Expression extends AbstractFunction { } } + // That implementation seems a bit off... + case INGRES: { + if (operator == ADD) { + return lhs.add(field("date('" + rhsAsNumber() + " days')", Object.class)); + } + else { + return lhs.sub(field("date('" + rhsAsNumber() + " days')", Object.class)); + } + } + + // Postgres can add / subtract days using +/- operators only to DATE + case POSTGRES: { + if (getType() == Date.class) { + return new DefaultExpression(); + } + else { + return new Expression(operator, lhs.cast(Date.class), rhsAsNumber()); + } + } + // These dialects can add / subtract days using +/- operators case H2: case ORACLE: diff --git a/jOOQ/src/main/java/org/jooq/impl/Factory.java b/jOOQ/src/main/java/org/jooq/impl/Factory.java index d5e3c375cd..d8aa5e1273 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Factory.java +++ b/jOOQ/src/main/java/org/jooq/impl/Factory.java @@ -4637,59 +4637,6 @@ public class Factory implements FactoryOperations { return new TimestampDiff(timestamp1, timestamp2); } - /** - * Get the timestamp difference as a INTERVAL DAY TO SECOND - * type - *

- * This translates into any dialect - * - * @see Field#sub(Field) - */ - @Support - public static Field timeDiff(Time time1, Time time2) { - return timeDiff(val(time1), val(time2)); - } - - /** - * Get the timestamp difference as a INTERVAL DAY TO SECOND - * type - *

- * This translates into any dialect - * - * @see Field#sub(Field) - */ - @Support - public static Field timeDiff(Field

- * This translates into any dialect - * - * @see Field#sub(Field) - */ - @Support - public static Field timeDiff(Time time1, Field

- * This translates into any dialect - * - * @see Field#sub(Field) - */ - @Support - public static Field timeDiff(Field