From 6c66fbf0e921218c43df41c1baa8b279bec60aef Mon Sep 17 00:00:00 2001 From: lukaseder Date: Mon, 26 Nov 2018 11:11:25 +0100 Subject: [PATCH] [#2132] Added implementations for H2 and HSQLDB --- jOOQ/src/main/java/org/jooq/DatePart.java | 35 ++----- jOOQ/src/main/java/org/jooq/impl/Extract.java | 98 ++++++++++++++++--- 2 files changed, 91 insertions(+), 42 deletions(-) diff --git a/jOOQ/src/main/java/org/jooq/DatePart.java b/jOOQ/src/main/java/org/jooq/DatePart.java index e9d6ff00fb..56ef13603b 100644 --- a/jOOQ/src/main/java/org/jooq/DatePart.java +++ b/jOOQ/src/main/java/org/jooq/DatePart.java @@ -139,30 +139,21 @@ public enum DatePart { /** * The millennium. The year 2000 is in the 2nd millennium, the year 2001 in * the 3rd. - * - * @deprecated - 3.11 - [#2132] Support for this type is still experimental */ - @Support({ POSTGRES }) - @Deprecated + @Support MILLENNIUM("millennium"), /** * The century. The year 2000 is in the 20th century, the year 2001 in the * 21st. - * - * @deprecated - 3.11 - [#2132] Support for this type is still experimental */ - @Support({ POSTGRES }) - @Deprecated + @Support CENTURY("century"), /** * The decade. The year divided by 10. - * - * @deprecated - 3.11 - [#2132] Support for this type is still experimental */ - @Support({ POSTGRES }) - @Deprecated + @Support DECADE("decade"), /** @@ -179,11 +170,8 @@ public enum DatePart { /** * The week of the year. - * - * @deprecated - 3.11 - [#2132] Support for this type is still experimental */ - @Support({ POSTGRES }) - @Deprecated + @Support({ H2, POSTGRES }) WEEK("week"), /** @@ -209,29 +197,20 @@ public enum DatePart { /** * The timezone offset in seconds. Corresponds to * {@link ChronoField#OFFSET_SECONDS}. - * - * @deprecated - 3.11 - [#2132] Support for this type is still experimental */ - @Support({ POSTGRES }) - @Deprecated + @Support({ H2, POSTGRES }) TIMEZONE("timezone"), /** * The time zone offset's hour part. - * - * @deprecated - 3.11 - [#2132] Support for this type is still experimental */ - @Support({ POSTGRES }) - @Deprecated + @Support({ H2, POSTGRES }) TIMEZONE_HOUR("timezone_hour"), /** * The time zone offset's minute part. - * - * @deprecated - 3.11 - [#2132] Support for this type is still experimental */ - @Support({ POSTGRES }) - @Deprecated + @Support({ H2, POSTGRES }) TIMEZONE_MINUTE("timezone_minute"), ; diff --git a/jOOQ/src/main/java/org/jooq/impl/Extract.java b/jOOQ/src/main/java/org/jooq/impl/Extract.java index f4b31e82cb..3961463b1f 100644 --- a/jOOQ/src/main/java/org/jooq/impl/Extract.java +++ b/jOOQ/src/main/java/org/jooq/impl/Extract.java @@ -73,6 +73,11 @@ final class Extract extends AbstractFunction { switch (configuration.family()) { case SQLITE: switch (datePart) { + case DECADE: + case CENTURY: + case MILLENNIUM: + return getDefaultEmulation(); + case YEAR: return DSL.field("{strftime}('%Y', {0})", INTEGER, field); case MONTH: @@ -114,6 +119,11 @@ final class Extract extends AbstractFunction { + + + + + @@ -122,6 +132,11 @@ final class Extract extends AbstractFunction { case DERBY: switch (datePart) { + case DECADE: + case CENTURY: + case MILLENNIUM: + return getDefaultEmulation(); + case YEAR: return function("year", INTEGER, field); case MONTH: @@ -225,6 +240,21 @@ final class Extract extends AbstractFunction { + + + + + + + + + + + + + + + @@ -244,16 +274,22 @@ final class Extract extends AbstractFunction { case MARIADB: case MYSQL: switch (datePart) { - case EPOCH: - return DSL.field("{unix_timestamp}({0})", INTEGER, field); - case QUARTER: - return DSL.field("{quarter}({0})", INTEGER, field); - case ISO_DAY_OF_WEEK: - return DSL.field("{weekday}({0})", INTEGER, field).add(one()); + case DECADE: + case CENTURY: + case MILLENNIUM: + return getDefaultEmulation(); + case DAY_OF_WEEK: return DSL.field("{dayofweek}({0})", INTEGER, field); case DAY_OF_YEAR: return DSL.field("{dayofyear}({0})", INTEGER, field); + case EPOCH: + return DSL.field("{unix_timestamp}({0})", INTEGER, field); + case ISO_DAY_OF_WEEK: + return DSL.field("{weekday}({0})", INTEGER, field).add(one()); + case QUARTER: + return DSL.field("{quarter}({0})", INTEGER, field); + default: return getNativeFunction(); } @@ -262,33 +298,48 @@ final class Extract extends AbstractFunction { case POSTGRES: switch (datePart) { - case QUARTER: - return DSL.field("{extract}({quarter from} {0})", INTEGER, field); case DAY_OF_WEEK: return DSL.field("({extract}({dow from} {0}) + 1)", INTEGER, field); - case ISO_DAY_OF_WEEK: - return DSL.field("{extract}({isodow from} {0})", INTEGER, field); case DAY_OF_YEAR: return DSL.field("{extract}({doy from} {0})", INTEGER, field); + case ISO_DAY_OF_WEEK: + return DSL.field("{extract}({isodow from} {0})", INTEGER, field); + default: return getNativeFunction(); } case HSQLDB: switch (datePart) { + case DECADE: + case CENTURY: + case MILLENNIUM: + case TIMEZONE: + return getDefaultEmulation(); + case EPOCH: return DSL.field("{unix_timestamp}({0})", INTEGER, field); - case QUARTER: - return DSL.field("{quarter}({0})", INTEGER, field); case ISO_DAY_OF_WEEK: return dowSun1ToISO(DSL.field("{extract}({day_of_week from} {0})", INTEGER, field)); + case QUARTER: + return DSL.field("{quarter}({0})", INTEGER, field); + case WEEK: + return DSL.field("{week}({0})", INTEGER, field); + default: return getNativeFunction(); } case H2: switch (datePart) { + case DECADE: + case CENTURY: + case MILLENNIUM: + case TIMEZONE: + return getDefaultEmulation(); + case QUARTER: return DSL.field("{quarter}({0})", INTEGER, field); + default: return getNativeFunction(); } @@ -310,12 +361,31 @@ final class Extract extends AbstractFunction { return dow.add(inline(6)).mod(inline(7)).add(one()); } - private final Field getNativeFunction() { + private final Field getDefaultEmulation() { switch (datePart) { + case DECADE: + return DSL.cast(DSL.year(field).div(inline(10)), INTEGER); + case CENTURY: + return DSL.cast( + DSL.sign(DSL.year(field)) + .mul(DSL.abs(DSL.year(field)).add(inline(99))) + .div(inline(100)), INTEGER); + case MILLENNIUM: + return DSL.cast( + DSL.sign(DSL.year(field)) + .mul(DSL.abs(DSL.year(field)).add(inline(999))) + .div(inline(1000)), INTEGER); case QUARTER: return DSL.month(field).add(inline(2)).div(inline(3)); + case TIMEZONE: + return DSL.extract(field, DatePart.TIMEZONE_HOUR).mul(inline(3600)) + .add(DSL.extract(field, DatePart.TIMEZONE_MINUTE).mul(inline(60))); default: - return DSL.field("{extract}({0} {from} {1})", INTEGER, datePart.toKeyword(), field); + return getNativeFunction(); } } + + private final Field getNativeFunction() { + return DSL.field("{extract}({0} {from} {1})", INTEGER, datePart.toKeyword(), field); + } }