From d876efb860aaeeb116f9a5bbd9442611da4ea284 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Sun, 20 Aug 2023 20:49:02 +0200 Subject: [PATCH] HHH-17109 fix pre-existing issues with date/time arithmetic --- .../hibernate/dialect/CockroachDialect.java | 21 +++++++++++-------- .../hibernate/dialect/PostgreSQLDialect.java | 21 +++++++++++-------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java index 32e7aa0f85..0c8ba520bb 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/CockroachDialect.java @@ -753,20 +753,23 @@ public class CockroachDialect extends Dialect { @Override public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType, IntervalType intervalType) { - if ( intervalType != null ) { - return "(?2+?3)"; - } - switch ( unit ) { + return intervalType != null + ? "(?2+?3)" + : "cast(?3+" + intervalPattern( unit ) + " as " + temporalType.name().toLowerCase() + ")"; + } + + private static String intervalPattern(TemporalUnit unit) { + switch (unit) { case NANOSECOND: - return "(?3+(?2)/1e3*interval '1 microsecond')"; + return "(?2)/1e3*interval '1 microsecond'"; case NATIVE: - return "(?3+(?2)*interval '1 microsecond')"; + return "(?2)*interval '1 second'"; case QUARTER: //quarter is not supported in interval literals - return "(?3+(?2)*interval '3 month')"; + return "(?2)*interval '3 month'"; case WEEK: //week is not supported in interval literals - return "(?3+(?2)*interval '7 day')"; + return "(?2)*interval '7 day'"; default: - return "(?3+(?2)*interval '1 ?1')"; + return "(?2)*interval '1 " + unit + "'"; } } diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLDialect.java b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLDialect.java index bae8aa4b94..94b524951f 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLDialect.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLDialect.java @@ -465,20 +465,23 @@ public class PostgreSQLDialect extends Dialect { @Override public String timestampaddPattern(TemporalUnit unit, TemporalType temporalType, IntervalType intervalType) { - if ( intervalType != null ) { - return "(?2+?3)"; - } - switch ( unit ) { + return intervalType != null + ? "(?2+?3)" + : "cast(?3+" + intervalPattern( unit ) + " as " + temporalType.name().toLowerCase() + ")"; + } + + private static String intervalPattern(TemporalUnit unit) { + switch (unit) { case NANOSECOND: - return "(?3+(?2)/1e3*interval '1 microsecond')"; + return "(?2)/1e3*interval '1 microsecond'"; case NATIVE: - return "(?3+(?2)*interval '1 second')"; + return "(?2)*interval '1 second'"; case QUARTER: //quarter is not supported in interval literals - return "(?3+(?2)*interval '3 month')"; + return "(?2)*interval '3 month'"; case WEEK: //week is not supported in interval literals - return "(?3+(?2)*interval '7 day')"; + return "(?2)*interval '7 day'"; default: - return "(?3+(?2)*interval '1 ?1')"; + return "(?2)*interval '1 " + unit + "'"; } }